Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ To try `pgo` simply modify `config/example.config` by replacing the `host`, `dat

```erlang
[
{pgo, [{pools, [{default, #{pool_size => 10,
host => "127.0.0.1",
database => "test",
user => "test"}}]}]}
{pgo, [{pools, [{pgo_default, #{pool_size => 10,
host => "127.0.0.1",
database => "test",
user => "test"}}]}]}
].
```

Expand All @@ -38,7 +38,7 @@ To try `pgo` simply modify `config/example.config` by replacing the `host`, `dat
``` erlang
> application:ensure_all_started(pgo).
{ok,[backoff,opentelemetry_api,pg_types,pgo]}
> pgo:start_pool(default, #{pool_size => 5, host => "127.0.0.1", database => "test", user => "test"}).
> pgo:start_pool(pgo_default, #{pool_size => 5, host => "127.0.0.1", database => "test", user => "test"}).
```

Or start a pool as a child of your application's supervisor:
Expand Down Expand Up @@ -104,7 +104,7 @@ decode_option() :: return_rows_as_maps | {return_rows_as_maps, boolean()} |

### Query Options

* `pool` (default: `default`): Name of the pool to use for checking out a connection to the database.
* `pool` (default: `pgo_default`): Name of the pool to use for checking out a connection to the database.
* `return_rows_as_maps` (default: `false`): When `true` each row is returned as a map of column name to value instead of a list of values.
* `column_name_as_atom` (default: `false`): If `true` converts each column name in the result to an atom.
* `decode_fun` (default: `undefined`): Optional function for performing transformations on each row in a result. It must be a 2-arity function returning a list or map for the row and takes the row (as a list or map) and a list of `#row_description_field{}` records.
Expand Down
5 changes: 3 additions & 2 deletions src/pgo.erl
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ query(Query, Params) ->
query(Query, Params, Options) ->
case get(pgo_transaction_connection) of
undefined ->
Pool = maps:get(pool, Options, default),
Pool = maps:get(pool, Options, pgo_default),
PoolOptions = maps:get(pool_options, Options, []),
case checkout(Pool, PoolOptions) of
{ok, Ref, Conn} ->
Expand Down Expand Up @@ -149,7 +149,7 @@ transaction(Fun) ->
%% @equiv transaction(default, Fun, Options)
-spec transaction(fun(() -> any()), options()) -> any() | {error, any()}.
transaction(Fun, Options) when is_function(Fun) ->
Pool = maps:get(pool, Options, default),
Pool = maps:get(pool, Options, pgo_default),
transaction(Pool, Fun, Options).

%% @doc Runs a function, passing it a connection, in a SQL transaction.
Expand Down Expand Up @@ -234,3 +234,4 @@ format_error(Error=#{module := Module}) ->
Module:format_error(Error);
format_error(Error) ->
io_lib:format("Unknown error: ~p", [Error]).

69 changes: 36 additions & 33 deletions test/pgo_basic_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -37,51 +37,51 @@ end_per_suite(_Config) ->
init_per_group(clear, Config) ->
application:ensure_all_started(pgo),

{ok, _} = pgo_sup:start_child(default, #{pool_size => 1,
port => 5432,
database => "test",
user => "test",
password => "password"}),
{ok, _} = pgo_sup:start_child(pgo_default, #{pool_size => 1,
port => 5432,
database => "test",
user => "test",
password => "password"}),

Config;
init_per_group(ssl, Config) ->
application:ensure_all_started(pgo),
{ok, _} = pgo_sup:start_child(default, #{pool_size => 1,
port => 5434,
ssl => true,
ssl_options => [{verify_type, verify_peer},
{cacertfile, "../../../../test/certs/server.crt"},
{verify_fun, {fun (_, valid, U) ->
{valid, U};
(_, valid_peer, U) ->
{valid, U};
(_, {bad_cert, selfsigned_peer}, U) ->
{valid, U};
(_, Reason, _U) ->
{fail, Reason}
end, []}}
],
database => "test",
user => "test",
password => "password"}),
{ok, _} = pgo_sup:start_child(pgo_default, #{pool_size => 1,
port => 5434,
ssl => true,
ssl_options => [{verify_type, verify_peer},
{cacertfile, "../../../../test/certs/server.crt"},
{verify_fun, {fun (_, valid, U) ->
{valid, U};
(_, valid_peer, U) ->
{valid, U};
(_, {bad_cert, selfsigned_peer}, U) ->
{valid, U};
(_, Reason, _U) ->
{fail, Reason}
end, []}}
],
database => "test",
user => "test",
password => "password"}),

Config;
init_per_group(domain_socket, Config) ->
application:ensure_all_started(pgo),

{ok, _} = pgo_sup:start_child(default, #{pool_size => 1,
port => 5432,
host => "/tmp/postgresql",
database => "test",
user => "test",
password => "password"}),
{ok, _} = pgo_sup:start_child(pgo_default, #{pool_size => 1,
port => 5432,
host => "/tmp/postgresql",
database => "test",
user => "test",
password => "password"}),

Config.


end_per_group(Group, _Config) when Group =:= ssl ; Group =:= domain_socket ->
application:stop(pgo),
pgo_test_utils:clear_types(default),
pgo_test_utils:clear_types(pgo_default),
ok;
end_per_group(_, _Config) ->
pgo:query("drop table tmp"),
Expand All @@ -94,7 +94,7 @@ end_per_group(_, _Config) ->

application:stop(pgo),

pgo_test_utils:clear_types(default),
pgo_test_utils:clear_types(pgo_default),

ok.

Expand Down Expand Up @@ -281,6 +281,8 @@ types(_Config) ->
[10, 42, 1099511627776, <<"And in the end, the love you take is equal to the love you make">>, ?BIN_UUID, <<"deadbeef">>, 3.1415])),


?assertMatch(#{command := insert}, pgo:query("insert into types (id, an_integer, a_bigint, a_text, a_uuid, a_bytea, a_real) values ($1, $2, $3, $4, $5, $6, $7)", [101, null, null, null, null, "deadbeef", null])),

R = pgo:query("select * from types where id = 10"),
?assertMatch(#{command := select, rows := [_Row]}, R),
#{command := select, rows := [Row]} = R,
Expand Down Expand Up @@ -423,7 +425,8 @@ bit_string(_Config) ->
?assertMatch(#{rows := [{<<1:1,1:1,0:1>>}]}, pgo:query("SELECT bit '110' :: varbit", [])).

arrays(_Config) ->
?assertMatch(#{rows := [{[<<"s1">>,null]}]}, pgo:query("SELECT $1::text[]", [[<<"s1">>, null]])).
?assertMatch(#{rows := [{[<<"s1">>,null]}]}, pgo:query("SELECT $1::text[]", [[<<"s1">>, null]])),
?assertMatch(#{rows := [{[{array,[{array,[<<"s1">>,null]}]}]}]}, pgo:query("SELECT $1::text[][][]", [[{array, [{array, [<<"s1">>, null]}]}]])).

tsvector(_Config) ->
?assertMatch(#{rows := [{[{<<"fat">>,[{2,null}]},{<<"rat">>,[{3,null}]}]}]},
Expand All @@ -440,7 +443,7 @@ tsvector(_Config) ->
{<<"fat">>,[{2,'B'},{4,'C'}]}]])).

query_arity_4(_Config) ->
{ok, Ref, Conn} = pgo:checkout(default),
{ok, Ref, Conn} = pgo:checkout(pgo_default),

?assertMatch(#{rows := [{1}]}, pgo:query("select 1", [], #{}, Conn)),

Expand Down
22 changes: 11 additions & 11 deletions test/pgo_datetime_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ init_per_group(erl_datetime, Config) ->

{ok, _} = application:ensure_all_started(pgo),

{ok, _} = pgo_sup:start_child(default, #{pool_size => 1,
{ok, _} = pgo_sup:start_child(pgo_default, #{pool_size => 1,
database => "test",
user => "test",
password => "password"}),
Expand All @@ -37,7 +37,7 @@ init_per_group(as_micro, Config) ->

application:ensure_all_started(pgo),

{ok, _} = pgo_sup:start_child(default, #{pool_size => 1,
{ok, _} = pgo_sup:start_child(pgo_default, #{pool_size => 1,
database => "test",
user => "test",
password => "password"}),
Expand All @@ -48,29 +48,29 @@ init_per_group(as_integer, Config) ->

application:ensure_all_started(pgo),

{ok, _} = pgo_sup:start_child(default, #{pool_size => 1,
database => "test",
user => "test",
password => "password"}),
{ok, _} = pgo_sup:start_child(pgo_default, #{pool_size => 1,
database => "test",
user => "test",
password => "password"}),
Config;
init_per_group(as_float, Config) ->
application:load(pg_types),
application:set_env(pg_types, timestamp_config, float_system_time_seconds),

application:ensure_all_started(pgo),

{ok, _} = pgo_sup:start_child(default, #{pool_size => 1,
database => "test",
user => "test",
password => "password"}),
{ok, _} = pgo_sup:start_child(pgo_default, #{pool_size => 1,
database => "test",
user => "test",
password => "password"}),
Config.

end_per_group(_, _Config) ->
application:unset_env(pg_types, timestamp_config),
application:stop(pgo),
application:unload(pg_types),

pgo_test_utils:clear_types(default),
pgo_test_utils:clear_types(pgo_default),

ok.

Expand Down
10 changes: 5 additions & 5 deletions test/pgo_enum_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ all() -> [select, insert].
init_per_suite(Config) ->
application:ensure_all_started(pgo),

{ok, _} = pgo_sup:start_child(default, #{pool_size => 1,
database => "test",
user => "test",
password => "password"}),
{ok, _} = pgo_sup:start_child(pgo_default, #{pool_size => 1,
database => "test",
user => "test",
password => "password"}),

?assertMatch(#{command := create},
pgo:query("CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy')")),
Expand All @@ -25,7 +25,7 @@ end_per_suite(_Config) ->

application:stop(pgo),

pgo_test_utils:clear_types(default),
pgo_test_utils:clear_types(pgo_default),

ok.

Expand Down
10 changes: 5 additions & 5 deletions test/pgo_geometric_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ all() -> [select, insert].
init_per_suite(Config) ->
application:ensure_all_started(pgo),

{ok, _} = pgo_sup:start_child(default, #{pool_size => 1,
database => "test",
user => "test",
password => "password"}),
{ok, _} = pgo_sup:start_child(pgo_default, #{pool_size => 1,
database => "test",
user => "test",
password => "password"}),

Config.

end_per_suite(_Config) ->
application:stop(pgo),
pgo_test_utils:clear_types(default),
pgo_test_utils:clear_types(pgo_default),
ok.

select(_Config) ->
Expand Down
12 changes: 6 additions & 6 deletions test/pgo_query_opts_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ all() ->
init_per_suite(Config) ->
application:ensure_all_started(pgo),

{ok, _} = pgo_sup:start_child(default, #{database => "test",
user => "test",
password => "password",
pool_size => 1,
decode_opts => [return_rows_as_maps]}),
{ok, _} = pgo_sup:start_child(pgo_default, #{database => "test",
user => "test",
password => "password",
pool_size => 1,
decode_opts => [return_rows_as_maps]}),

Config.

Expand All @@ -27,7 +27,7 @@ end_per_suite(_Config) ->

application:stop(pgo),

pgo_test_utils:clear_types(default),
pgo_test_utils:clear_types(pgo_default),

ok.

Expand Down
12 changes: 6 additions & 6 deletions test/pgo_trace_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ all() ->
init_per_suite(Config) ->
application:ensure_all_started(pgo),
application:ensure_all_started(tls_certificate_check),
{ok, _} = pgo_sup:start_child(default, #{pool_size => 1,
port => 5432,
database => "test",
user => "test",
password => "password",
trace => true}),
{ok, _} = pgo_sup:start_child(pgo_default, #{pool_size => 1,
port => 5432,
database => "test",
user => "test",
password => "password",
trace => true}),
_ = application:load(opentelemetry),
application:set_env(opentelemetry, processors, [{otel_simple_processor, #{}}]),
{ok, _} = application:ensure_all_started(opentelemetry),
Expand Down
Loading