epgsql_SUITE.erl 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. -module(epgsql_SUITE).
  2. -include_lib("eunit/include/eunit.hrl").
  3. -include_lib("common_test/include/ct.hrl").
  4. -include_lib("public_key/include/public_key.hrl").
  5. -include("epgsql_tests.hrl").
  6. -include("epgsql.hrl").
  7. -export([
  8. init_per_suite/1,
  9. init_per_group/2,
  10. all/0,
  11. groups/0,
  12. end_per_group/2,
  13. end_per_suite/1
  14. ]).
  15. -compile(export_all).
  16. modules() ->
  17. [
  18. epgsql,
  19. epgsql_cast,
  20. epgsql_incremental
  21. ].
  22. init_per_suite(Config) ->
  23. Config.
  24. all() ->
  25. [{group, M} || M <- modules()].
  26. groups() ->
  27. Groups = [
  28. {connect, [parrallel], [
  29. connect,
  30. connect_to_db,
  31. connect_as,
  32. connect_with_cleartext,
  33. connect_with_md5,
  34. connect_with_invalid_user,
  35. connect_with_invalid_password,
  36. connect_with_ssl,
  37. connect_with_client_cert
  38. ]},
  39. {types, [parallel], [
  40. numeric_type,
  41. character_type,
  42. uuid_type,
  43. point_type,
  44. geometry_type,
  45. uuid_select,
  46. date_time_type,
  47. json_type,
  48. misc_type,
  49. hstore_type,
  50. net_type,
  51. array_type,
  52. range_type,
  53. range8_type,
  54. custom_types
  55. ]}
  56. ],
  57. Tests = [
  58. {group, connect},
  59. {group, types},
  60. prepared_query,
  61. select,
  62. insert,
  63. update,
  64. delete,
  65. create_and_drop_table,
  66. cursor,
  67. multiple_result,
  68. execute_batch,
  69. batch_error,
  70. single_batch,
  71. extended_select,
  72. extended_sync_ok,
  73. extended_sync_error,
  74. returning_from_insert,
  75. returning_from_update,
  76. returning_from_delete,
  77. parse,
  78. parse_column_format,
  79. parse_error,
  80. parse_and_close,
  81. bind,
  82. bind_parameter_format,
  83. bind_error,
  84. bind_and_close,
  85. execute_error,
  86. describe,
  87. describe_with_param,
  88. describe_named,
  89. describe_error,
  90. portal,
  91. returning,
  92. multiple_statement,
  93. multiple_portal,
  94. execute_function,
  95. parameter_get,
  96. parameter_set,
  97. text_format,
  98. query_timeout,
  99. execute_timeout,
  100. connection_closed,
  101. connection_closed_by_server,
  102. active_connection_closed,
  103. warning_notice,
  104. listen_notify,
  105. listen_notify_payload,
  106. set_notice_receiver,
  107. get_cmd_status
  108. ],
  109. Groups ++ [{Module, [], Tests} || Module <- modules()].
  110. end_per_suite(_Config) ->
  111. ok.
  112. init_per_group(GroupName, Config) ->
  113. case lists:member(GroupName, modules()) of
  114. true -> [{module, GroupName}|Config];
  115. false -> Config
  116. end.
  117. end_per_group(_GroupName, _Config) ->
  118. ok.
  119. -define(UUID1,
  120. <<163,189,240,40,149,151,17,227,141,6,112,24,139,130,16,73>>).
  121. -define(UUID2,
  122. <<183,55,22,52,149,151,17,227,187,167,112,24,139,130,16,73>>).
  123. -define(UUID3,
  124. <<198,188,155,66,149,151,17,227,138,98,112,24,139,130,16,73>>).
  125. -define(TIMEOUT_ERROR, {error, #error{
  126. severity = error,
  127. code = <<"57014">>,
  128. codename = query_canceled,
  129. message = <<"canceling statement due to statement timeout">>,
  130. extra = [{file, <<"postgres.c">>},
  131. {line, _},
  132. {routine, _}]
  133. }}).
  134. %% From uuid.erl in http://gitorious.org/avtobiff/erlang-uuid
  135. uuid_to_string(<<U0:32, U1:16, U2:16, U3:16, U4:48>>) ->
  136. lists:flatten(io_lib:format(
  137. "~8.16.0b-~4.16.0b-~4.16.0b-~4.16.0b-~12.16.0b",
  138. [U0, U1, U2, U3, U4])).
  139. connect(Config) ->
  140. epgsql_ct:connect_only(Config, []).
  141. connect_to_db(Connect) ->
  142. epgsql_ct:connect_only(Connect, [{database, "epgsql_test_db1"}]).
  143. connect_as(Config) ->
  144. epgsql_ct:connect_only(Config, ["epgsql_test", [{database, "epgsql_test_db1"}]]).
  145. connect_with_cleartext(Config) ->
  146. epgsql_ct:connect_only(Config, [
  147. "epgsql_test_cleartext",
  148. "epgsql_test_cleartext",
  149. [{database, "epgsql_test_db1"}]
  150. ]).
  151. connect_with_md5(Config) ->
  152. epgsql_ct:connect_only(Config, [
  153. "epgsql_test_md5",
  154. "epgsql_test_md5",
  155. [{database, "epgsql_test_db1"}]
  156. ]).
  157. connect_with_invalid_user(Config) ->
  158. #{pg_port := Port, pg_host := Host} = ?config(pg_config, Config),
  159. Module = ?config(module, Config),
  160. {error, Why} = Module:connect(
  161. Host,
  162. "epgsql_test_invalid",
  163. "epgsql_test_invalid",
  164. [{port, Port}, {database, "epgsql_test_db1"}]),
  165. case Why of
  166. invalid_authorization_specification -> ok; % =< 8.4
  167. invalid_password -> ok % >= 9.0
  168. end.
  169. connect_with_invalid_password(Config) ->
  170. #{pg_port := Port, pg_host := Host} = ?config(pg_config, Config),
  171. Module = ?config(module, Config),
  172. {error, Why} = Module:connect(
  173. Host,
  174. "epgsql_test_md5",
  175. "epgsql_test_invalid",
  176. [{port, Port}, {database, "epgsql_test_db1"}]),
  177. case Why of
  178. invalid_authorization_specification -> ok; % =< 8.4
  179. invalid_password -> ok % >= 9.0
  180. end.
  181. connect_with_ssl(Config) ->
  182. Module = ?config(module, Config),
  183. epgsql_ct:with_connection(Config,
  184. fun(C) ->
  185. {ok, _Cols, [{true}]} = Module:equery(C, "select ssl_is_used()")
  186. end,
  187. "epgsql_test",
  188. [{ssl, true}]).
  189. connect_with_client_cert(Config) ->
  190. Module = ?config(module, Config),
  191. Dir = filename:join(code:lib_dir(epgsql), ?TEST_DATA_DIR),
  192. File = fun(Name) -> filename:join(Dir, Name) end,
  193. {ok, Pem} = file:read_file(File("epgsql.crt")),
  194. [{'Certificate', Der, not_encrypted}] = public_key:pem_decode(Pem),
  195. Cert = public_key:pkix_decode_cert(Der, plain),
  196. #'TBSCertificate'{serialNumber = Serial} = Cert#'Certificate'.tbsCertificate,
  197. Serial2 = list_to_binary(integer_to_list(Serial)),
  198. epgsql_ct:with_connection(Config,
  199. fun(C) ->
  200. {ok, _, [{true}]} = Module:equery(C, "select ssl_is_used()"),
  201. {ok, _, [{Serial2}]} = Module:equery(C, "select ssl_client_serial()")
  202. end,
  203. "epgsql_test_cert",
  204. [{ssl, true}, {keyfile, File("epgsql.key")}, {certfile, File("epgsql.crt")}]).
  205. %% -ifdef(have_maps).
  206. %% connect_map_test(Module) ->
  207. %% Opts = #{host => ?host,
  208. %% port => ?port,
  209. %% database => "epgsql_test_db1",
  210. %% username => "epgsql_test_md5",
  211. %% password => "epgsql_test_md5"
  212. %% },
  213. %% {ok, C} = Module:connect(Opts),
  214. %% Module:close(C),
  215. %% epgsql_ct:flush(),
  216. %% ok.
  217. %% -endif.
  218. prepared_query(Config) ->
  219. Module = ?config(module, Config),
  220. epgsql_ct:with_connection(Config, fun(C) ->
  221. {ok, _} = Module:parse(C, "inc", "select $1+1", []),
  222. {ok, Cols, [{5}]} = Module:prepared_query(C, "inc", [4]),
  223. {ok, Cols, [{2}]} = Module:prepared_query(C, "inc", [1]),
  224. {ok, Cols, [{23}]} = Module:prepared_query(C, "inc", [22]),
  225. {error, _} = Module:prepared_query(C, "non_existent_query", [4])
  226. end).
  227. select(Config) ->
  228. Module = ?config(module, Config),
  229. epgsql_ct:with_connection(Config, fun(C) ->
  230. {ok, Cols, Rows} = Module:squery(C, "select * from test_table1"),
  231. [
  232. #column{name = <<"id">>, type = int4, size = 4},
  233. #column{name = <<"value">>, type = text, size = -1}
  234. ] = Cols,
  235. [{<<"1">>, <<"one">>}, {<<"2">>, <<"two">>}] = Rows
  236. end).
  237. insert(Config) ->
  238. Module = ?config(module, Config),
  239. epgsql_ct:with_rollback(Config, fun(C) ->
  240. {ok, 1} = Module:squery(C, "insert into test_table1 (id, value) values (3, 'three')")
  241. end).
  242. update(Config) ->
  243. Module = ?config(module, Config),
  244. epgsql_ct:with_rollback(Config, fun(C) ->
  245. {ok, 1} = Module:squery(C, "insert into test_table1 (id, value) values (3, 'three')"),
  246. {ok, 1} = Module:squery(C, "insert into test_table1 (id, value) values (4, 'four')"),
  247. {ok, 2} = Module:squery(C, "update test_table1 set value = 'foo' where id > 2"),
  248. {ok, _, [{<<"2">>}]} = Module:squery(C, "select count(*) from test_table1 where value = 'foo'")
  249. end).
  250. delete(Config) ->
  251. Module = ?config(module, Config),
  252. epgsql_ct:with_rollback(Config, fun(C) ->
  253. {ok, 1} = Module:squery(C, "insert into test_table1 (id, value) values (3, 'three')"),
  254. {ok, 1} = Module:squery(C, "insert into test_table1 (id, value) values (4, 'four')"),
  255. {ok, 2} = Module:squery(C, "delete from test_table1 where id > 2"),
  256. {ok, _, [{<<"2">>}]} = Module:squery(C, "select count(*) from test_table1")
  257. end).
  258. create_and_drop_table(Config) ->
  259. Module = ?config(module, Config),
  260. epgsql_ct:with_rollback(Config, fun(C) ->
  261. {ok, [], []} = Module:squery(C, "create table test_table3 (id int4)"),
  262. {ok, [#column{type = int4}], []} = Module:squery(C, "select * from test_table3"),
  263. {ok, [], []} = Module:squery(C, "drop table test_table3")
  264. end).
  265. cursor(Config) ->
  266. Module = ?config(module, Config),
  267. epgsql_ct:with_connection(Config, fun(C) ->
  268. {ok, [], []} = Module:squery(C, "begin"),
  269. {ok, [], []} = Module:squery(C, "declare c cursor for select id from test_table1"),
  270. {ok, 2} = Module:squery(C, "move forward 2 from c"),
  271. {ok, 1} = Module:squery(C, "move backward 1 from c"),
  272. {ok, 1, _Cols, [{<<"2">>}]} = Module:squery(C, "fetch next from c"),
  273. {ok, [], []} = Module:squery(C, "close c")
  274. end).
  275. multiple_result(Config) ->
  276. Module = ?config(module, Config),
  277. epgsql_ct:with_connection(Config, fun(C) ->
  278. [{ok, _, [{<<"1">>}]}, {ok, _, [{<<"2">>}]}] = Module:squery(C, "select 1; select 2"),
  279. [{ok, _, [{<<"1">>}]}, {error, #error{}}] = Module:squery(C, "select 1; select foo;")
  280. end).
  281. execute_batch(Config) ->
  282. Module = ?config(module, Config),
  283. epgsql_ct:with_connection(Config, fun(C) ->
  284. {ok, S1} = Module:parse(C, "one", "select $1", [int4]),
  285. {ok, S2} = Module:parse(C, "two", "select $1 + $2", [int4, int4]),
  286. [{ok, [{1}]}, {ok, [{3}]}] =
  287. Module:execute_batch(C, [{S1, [1]}, {S2, [1, 2]}])
  288. end).
  289. batch_error(Config) ->
  290. Module = ?config(module, Config),
  291. epgsql_ct:with_rollback(Config, fun(C) ->
  292. {ok, S} = Module:parse(C, "insert into test_table1(id, value) values($1, $2)"),
  293. [{ok, 1}, {error, _}] =
  294. Module:execute_batch(
  295. C,
  296. [{S, [3, "batch_error 3"]},
  297. {S, [2, "batch_error 2"]}, % duplicate key error
  298. {S, [5, "batch_error 5"]} % won't be executed
  299. ])
  300. end).
  301. single_batch(Config) ->
  302. Module = ?config(module, Config),
  303. epgsql_ct:with_connection(Config, fun(C) ->
  304. {ok, S1} = Module:parse(C, "one", "select $1", [int4]),
  305. [{ok, [{1}]}] = Module:execute_batch(C, [{S1, [1]}])
  306. end).
  307. extended_select(Config) ->
  308. Module = ?config(module, Config),
  309. epgsql_ct:with_connection(Config, fun(C) ->
  310. {ok, Cols, Rows} = Module:equery(C, "select * from test_table1", []),
  311. [#column{name = <<"id">>, type = int4, size = 4},
  312. #column{name = <<"value">>, type = text, size = -1}] = Cols,
  313. [{1, <<"one">>}, {2, <<"two">>}] = Rows
  314. end).
  315. extended_sync_ok(Config) ->
  316. Module = ?config(module, Config),
  317. epgsql_ct:with_connection(Config, fun(C) ->
  318. {ok, _Cols, [{<<"one">>}]} = Module:equery(C, "select value from test_table1 where id = $1", [1]),
  319. {ok, _Cols, [{<<"two">>}]} = Module:equery(C, "select value from test_table1 where id = $1", [2])
  320. end).
  321. extended_sync_error(Config) ->
  322. Module = ?config(module, Config),
  323. epgsql_ct:with_connection(Config, fun(C) ->
  324. {error, #error{}} = Module:equery(C, "select _alue from test_table1 where id = $1", [1]),
  325. {ok, _Cols, [{<<"one">>}]} = Module:equery(C, "select value from test_table1 where id = $1", [1])
  326. end).
  327. returning_from_insert(Config) ->
  328. Module = ?config(module, Config),
  329. epgsql_ct:with_rollback(Config, fun(C) ->
  330. {ok, 1, _Cols, [{3}]} = Module:equery(C, "insert into test_table1 (id) values (3) returning id")
  331. end).
  332. returning_from_update(Config) ->
  333. Module = ?config(module, Config),
  334. epgsql_ct:with_rollback(Config, fun(C) ->
  335. {ok, 2, _Cols, [{1}, {2}]} = Module:equery(C, "update test_table1 set value = 'hi' returning id"),
  336. ?assertMatch({ok, 0, [#column{}], []},
  337. Module:equery(C, "update test_table1 set value = 'hi' where false returning id")),
  338. ?assertMatch([{ok, 2, [#column{}], [{<<"1">>}, {<<"2">>}]},
  339. {ok, 0, [#column{}], []}],
  340. Module:squery(C,
  341. "update test_table1 set value = 'hi2' returning id; "
  342. "update test_table1 set value = 'hi' where false returning id"))
  343. end).
  344. returning_from_delete(Config) ->
  345. Module = ?config(module, Config),
  346. epgsql_ct:with_rollback(Config, fun(C) ->
  347. {ok, 2, _Cols, [{1}, {2}]} = Module:equery(C, "delete from test_table1 returning id"),
  348. ?assertMatch({ok, 0, [#column{}], []},
  349. Module:equery(C, "delete from test_table1 returning id"))
  350. end).
  351. parse(Config) ->
  352. Module = ?config(module, Config),
  353. epgsql_ct:with_connection(Config, fun(C) ->
  354. {ok, S} = Module:parse(C, "select * from test_table1"),
  355. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  356. ok = Module:close(C, S),
  357. ok = Module:sync(C)
  358. end).
  359. parse_column_format(Config) ->
  360. Module = ?config(module, Config),
  361. epgsql_ct:with_connection(Config, fun(C) ->
  362. {ok, S} = Module:parse(C, "select 1::int4, false::bool, 2.0::float4"),
  363. [#column{type = int4},
  364. #column{type = bool},
  365. #column{type = float4}] = S#statement.columns,
  366. ok = Module:bind(C, S, []),
  367. {ok, [{1, false, 2.0}]} = Module:execute(C, S, 0),
  368. ok = Module:close(C, S),
  369. ok = Module:sync(C)
  370. end).
  371. parse_error(Config) ->
  372. Module = ?config(module, Config),
  373. epgsql_ct:with_connection(Config, fun(C) ->
  374. {error, #error{
  375. extra = [{file, _}, {line, _}, {position, <<"8">>}, {routine, _}]
  376. }} = Module:parse(C, "select _ from test_table1"),
  377. {ok, S} = Module:parse(C, "select * from test_table1"),
  378. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  379. ok = Module:close(C, S),
  380. ok = Module:sync(C)
  381. end).
  382. parse_and_close(Config) ->
  383. Module = ?config(module, Config),
  384. epgsql_ct:with_connection(Config, fun(C) ->
  385. Parse = fun() -> Module:parse(C, "test", "select * from test_table1", []) end,
  386. {ok, S} = Parse(),
  387. {error, #error{code = <<"42P05">>, codename = duplicate_prepared_statement}} = Parse(),
  388. Module:close(C, S),
  389. {ok, S} = Parse(),
  390. ok = Module:sync(C)
  391. end).
  392. bind(Config) ->
  393. Module = ?config(module, Config),
  394. epgsql_ct:with_connection(Config, fun(C) ->
  395. {ok, S} = Module:parse(C, "select value from test_table1 where id = $1"),
  396. ok = Module:bind(C, S, [1]),
  397. ok = Module:close(C, S),
  398. ok = Module:sync(C)
  399. end).
  400. bind_parameter_format(Config) ->
  401. Module = ?config(module, Config),
  402. epgsql_ct:with_connection(Config, fun(C) ->
  403. {ok, S} = Module:parse(C, "select $1, $2, $3", [int2, text, bool]),
  404. [int2, text, bool] = S#statement.types,
  405. ok = Module:bind(C, S, [1, "hi", true]),
  406. {ok, [{1, <<"hi">>, true}]} = Module:execute(C, S, 0),
  407. ok = Module:close(C, S),
  408. ok = Module:sync(C)
  409. end).
  410. bind_error(Config) ->
  411. Module = ?config(module, Config),
  412. epgsql_ct:with_connection(Config, fun(C) ->
  413. {ok, S} = Module:parse(C, "select $1::char"),
  414. {error, #error{}} = Module:bind(C, S, [0]),
  415. ok = Module:bind(C, S, [$A]),
  416. ok = Module:close(C, S),
  417. ok = Module:sync(C)
  418. end).
  419. bind_and_close(Config) ->
  420. Module = ?config(module, Config),
  421. epgsql_ct:with_connection(Config, fun(C) ->
  422. {ok, S} = Module:parse(C, "select * from test_table1"),
  423. ok = Module:bind(C, S, "one", []),
  424. {error, #error{code = <<"42P03">>, codename = duplicate_cursor}} = Module:bind(C, S, "one", []),
  425. ok = Module:close(C, portal, "one"),
  426. ok = Module:bind(C, S, "one", []),
  427. ok = Module:sync(C)
  428. end).
  429. execute_error(Config) ->
  430. Module = ?config(module, Config),
  431. epgsql_ct:with_connection(Config, fun(C) ->
  432. {ok, S} = Module:parse(C, "insert into test_table1 (id, value) values ($1, $2)"),
  433. ok = Module:bind(C, S, [1, <<"foo">>]),
  434. {error, #error{
  435. code = <<"23505">>, codename = unique_violation,
  436. extra = [
  437. {constraint_name, <<"test_table1_pkey">>},
  438. {detail, _},
  439. {file, _},
  440. {line, _},
  441. {routine, _},
  442. {schema_name, <<"public">>},
  443. {table_name, <<"test_table1">>}
  444. ]
  445. }} = Module:execute(C, S, 0),
  446. {error, sync_required} = Module:bind(C, S, [3, <<"quux">>]),
  447. ok = Module:sync(C),
  448. ok = Module:bind(C, S, [3, <<"quux">>]),
  449. {ok, _} = Module:execute(C, S, 0),
  450. {ok, 1} = Module:squery(C, "delete from test_table1 where id = 3")
  451. end).
  452. describe(Config) ->
  453. Module = ?config(module, Config),
  454. epgsql_ct:with_connection(Config, fun(C) ->
  455. {ok, S} = Module:parse(C, "select * from test_table1"),
  456. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  457. {ok, S} = Module:describe(C, S),
  458. ok = Module:close(C, S),
  459. ok = Module:sync(C)
  460. end).
  461. describe_with_param(Config) ->
  462. Module = ?config(module, Config),
  463. epgsql_ct:with_connection(Config, fun(C) ->
  464. {ok, S} = Module:parse(C, "select id from test_table1 where id = $1"),
  465. [int4] = S#statement.types,
  466. [#column{name = <<"id">>}] = S#statement.columns,
  467. {ok, S} = Module:describe(C, S),
  468. ok = Module:close(C, S),
  469. ok = Module:sync(C)
  470. end).
  471. describe_named(Config) ->
  472. Module = ?config(module, Config),
  473. epgsql_ct:with_connection(Config, fun(C) ->
  474. {ok, S} = Module:parse(C, "name", "select * from test_table1", []),
  475. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  476. {ok, S} = Module:describe(C, S),
  477. ok = Module:close(C, S),
  478. ok = Module:sync(C)
  479. end).
  480. describe_error(Config) ->
  481. Module = ?config(module, Config),
  482. epgsql_ct:with_connection(Config, fun(C) ->
  483. {error, #error{}} = Module:describe(C, statement, ""),
  484. {ok, S} = Module:parse(C, "select * from test_table1"),
  485. {ok, S} = Module:describe(C, statement, ""),
  486. ok = Module:sync(C)
  487. end).
  488. portal(Config) ->
  489. Module = ?config(module, Config),
  490. epgsql_ct:with_connection(Config, fun(C) ->
  491. {ok, S} = Module:parse(C, "select value from test_table1"),
  492. ok = Module:bind(C, S, []),
  493. {partial, [{<<"one">>}]} = Module:execute(C, S, 1),
  494. {partial, [{<<"two">>}]} = Module:execute(C, S, 1),
  495. {ok, []} = Module:execute(C, S,1),
  496. ok = Module:close(C, S),
  497. ok = Module:sync(C)
  498. end).
  499. returning(Config) ->
  500. Module = ?config(module, Config),
  501. epgsql_ct:with_rollback(Config, fun(C) ->
  502. {ok, S} = Module:parse(C, "update test_table1 set value = $1 returning id"),
  503. ok = Module:bind(C, S, ["foo"]),
  504. {ok, 2, [{1}, {2}]} = Module:execute(C, S),
  505. ok = Module:sync(C)
  506. end).
  507. multiple_statement(Config) ->
  508. Module = ?config(module, Config),
  509. epgsql_ct:with_connection(Config, fun(C) ->
  510. {ok, S1} = Module:parse(C, "one", "select value from test_table1 where id = 1", []),
  511. ok = Module:bind(C, S1, []),
  512. {partial, [{<<"one">>}]} = Module:execute(C, S1, 1),
  513. {ok, S2} = Module:parse(C, "two", "select value from test_table1 where id = 2", []),
  514. ok = Module:bind(C, S2, []),
  515. {partial, [{<<"two">>}]} = Module:execute(C, S2, 1),
  516. {ok, []} = Module:execute(C, S1, 1),
  517. {ok, []} = Module:execute(C, S2, 1),
  518. ok = Module:close(C, S1),
  519. ok = Module:close(C, S2),
  520. ok = Module:sync(C)
  521. end).
  522. multiple_portal(Config) ->
  523. Module = ?config(module, Config),
  524. epgsql_ct:with_connection(Config, fun(C) ->
  525. {ok, S} = Module:parse(C, "select value from test_table1 where id = $1"),
  526. ok = Module:bind(C, S, "one", [1]),
  527. ok = Module:bind(C, S, "two", [2]),
  528. {ok, [{<<"one">>}]} = Module:execute(C, S, "one", 0),
  529. {ok, [{<<"two">>}]} = Module:execute(C, S, "two", 0),
  530. ok = Module:close(C, S),
  531. ok = Module:sync(C)
  532. end).
  533. execute_function(Config) ->
  534. Module = ?config(module, Config),
  535. epgsql_ct:with_rollback(Config, fun(C) ->
  536. {ok, _Cols1, [{3}]} = Module:equery(C, "select insert_test1(3, 'three')"),
  537. {ok, _Cols2, [{<<>>}]} = Module:equery(C, "select do_nothing()")
  538. end).
  539. parameter_get(Config) ->
  540. Module = ?config(module, Config),
  541. epgsql_ct:with_connection(Config, fun(C) ->
  542. {ok, <<"off">>} = Module:get_parameter(C, "is_superuser")
  543. end).
  544. parameter_set(Config) ->
  545. Module = ?config(module, Config),
  546. epgsql_ct:with_connection(Config, fun(C) ->
  547. {ok, [], []} = Module:squery(C, "set DateStyle = 'ISO, MDY'"),
  548. {ok, <<"ISO, MDY">>} = Module:get_parameter(C, "DateStyle"),
  549. {ok, _Cols, [{<<"2000-01-02">>}]} = Module:squery(C, "select '2000-01-02'::date"),
  550. {ok, [], []} = Module:squery(C, "set DateStyle = 'German'"),
  551. {ok, <<"German, DMY">>} = Module:get_parameter(C, "DateStyle"),
  552. {ok, _Cols, [{<<"02.01.2000">>}]} = Module:squery(C, "select '2000-01-02'::date")
  553. end).
  554. numeric_type(Config) ->
  555. check_type(Config, int2, "1", 1, [0, 256, -32768, +32767]),
  556. check_type(Config, int4, "1", 1, [0, 512, -2147483648, +2147483647]),
  557. check_type(Config, int8, "1", 1, [0, 1024, -9223372036854775808, +9223372036854775807]),
  558. check_type(Config, float4, "1.0", 1.0, [0.0, 1.23456, -1.23456]),
  559. check_type(Config, float8, "1.0", 1.0, [0.0, 1.23456789012345, -1.23456789012345]).
  560. character_type(Config) ->
  561. Alpha = unicode:characters_to_binary([16#03B1]),
  562. Ka = unicode:characters_to_binary([16#304B]),
  563. One = unicode:characters_to_binary([16#10D360]),
  564. check_type(Config, bpchar, "'A'", $A, [1, $1, 16#7F, Alpha, Ka, One], "c_char"),
  565. check_type(Config, text, "'hi'", <<"hi">>, [<<"">>, <<"hi">>]),
  566. check_type(Config, varchar, "'hi'", <<"hi">>, [<<"">>, <<"hi">>]).
  567. uuid_type(Config) ->
  568. check_type(Config, uuid,
  569. io_lib:format("'~s'", [uuid_to_string(?UUID1)]),
  570. list_to_binary(uuid_to_string(?UUID1)), []).
  571. point_type(Config) ->
  572. check_type(Config, point, "'(23.15, 100)'", {23.15, 100.0}, []).
  573. geometry_type(Config) ->
  574. check_type(Config, geometry, "'COMPOUNDCURVE(CIRCULARSTRING(0 0,1 1,1 0),(1 0,0 1))'",
  575. {compound_curve,'2d', [
  576. {circular_string,'2d', [
  577. {point,'2d',0.0,0.0,undefined,undefined},
  578. {point,'2d',1.0,1.0,undefined,undefined},
  579. {point,'2d',1.0,0.0,undefined,undefined}
  580. ]},
  581. {line_string,'2d', [
  582. {point,'2d',1.0,0.0,undefined,undefined},
  583. {point,'2d',0.0,1.0,undefined,undefined}
  584. ]}
  585. ]}, []).
  586. uuid_select(Config) ->
  587. Module = ?config(module, Config),
  588. epgsql_ct:with_rollback(Config, fun(C) ->
  589. U1 = uuid_to_string(?UUID1),
  590. U2 = uuid_to_string(?UUID2),
  591. U3 = uuid_to_string(?UUID3),
  592. {ok, 1} =
  593. Module:equery(C, "insert into test_table2 (c_varchar, c_uuid) values ('UUID1', $1)",
  594. [U1]),
  595. {ok, 1} =
  596. Module:equery(C, "insert into test_table2 (c_varchar, c_uuid) values ('UUID2', $1)",
  597. [U2]),
  598. {ok, 1} =
  599. Module:equery(C, "insert into test_table2 (c_varchar, c_uuid) values ('UUID3', $1)",
  600. [U3]),
  601. Res = Module:equery(C, "select c_varchar, c_uuid from test_table2 where c_uuid = any($1)",
  602. [[U1, U2]]),
  603. U1Bin = list_to_binary(U1),
  604. U2Bin = list_to_binary(U2),
  605. {ok,[{column,<<"c_varchar">>,varchar,_,_,_},
  606. {column,<<"c_uuid">>,uuid,_,_,_}],
  607. [{<<"UUID1">>, U1Bin},
  608. {<<"UUID2">>, U2Bin}]} = Res
  609. end).
  610. date_time_type(Config) ->
  611. Module = ?config(module, Config),
  612. epgsql_ct:with_connection(Config, fun(C) ->
  613. case Module:get_parameter(C, "integer_datetimes") of
  614. {ok, <<"on">>} -> MaxTsDate = 294276;
  615. {ok, <<"off">>} -> MaxTsDate = 5874897
  616. end,
  617. check_type(Config, date, "'2008-01-02'", {2008,1,2}, [{-4712,1,1}, {5874897,1,1}]),
  618. check_type(Config, time, "'00:01:02'", {0,1,2.0}, [{0,0,0.0}, {24,0,0.0}]),
  619. check_type(Config, timetz, "'00:01:02-01'", {{0,1,2.0},1*60*60},
  620. [{{0,0,0.0},0}, {{24,0,0.0},-13*60*60}]),
  621. check_type(Config, timestamp, "'2008-01-02 03:04:05'", {{2008,1,2},{3,4,5.0}},
  622. [{{-4712,1,1},{0,0,0.0}}, {{MaxTsDate,12,31}, {23,59,59.0}}, {1322,334285,440966}]),
  623. check_type(Config, timestamptz, "'2011-01-02 03:04:05+3'", {{2011, 1, 2}, {0, 4, 5.0}}, [{1324,875970,286983}]),
  624. check_type(Config, interval, "'1 hour 2 minutes 3.1 seconds'", {{1,2,3.1},0,0},
  625. [{{0,0,0.0},0,-178000000 * 12}, {{0,0,0.0},0,178000000 * 12}])
  626. end).
  627. json_type(Config) ->
  628. check_type(Config, json, "'{}'", <<"{}">>,
  629. [<<"{}">>, <<"[]">>, <<"1">>, <<"1.0">>, <<"true">>, <<"\"string\"">>, <<"{\"key\": []}">>]),
  630. check_type(Config, jsonb, "'{}'", <<"{}">>,
  631. [<<"{}">>, <<"[]">>, <<"1">>, <<"1.0">>, <<"true">>, <<"\"string\"">>, <<"{\"key\": []}">>]).
  632. misc_type(Config) ->
  633. check_type(Config, bool, "true", true, [true, false]),
  634. check_type(Config, bytea, "E'\001\002'", <<1,2>>, [<<>>, <<0,128,255>>]).
  635. hstore_type(Config) ->
  636. Values = [
  637. {[]},
  638. {[{null, null}]},
  639. {[{null, undefined}]},
  640. {[{1, null}]},
  641. {[{1.0, null}]},
  642. {[{1, undefined}]},
  643. {[{1.0, undefined}]},
  644. {[{<<"a">>, <<"c">>}, {<<"c">>, <<"d">>}]},
  645. {[{<<"a">>, <<"c">>}, {<<"c">>, null}]},
  646. {[{<<"a">>, <<"c">>}, {<<"c">>, undefined}]}
  647. ],
  648. check_type(Config, hstore, "''", {[]}, []),
  649. check_type(Config, hstore,
  650. "'a => 1, b => 2.0, c => null'",
  651. {[{<<"c">>, null}, {<<"b">>, <<"2.0">>}, {<<"a">>, <<"1">>}]}, Values).
  652. net_type(Config) ->
  653. check_type(Config, cidr, "'127.0.0.1/32'", {{127,0,0,1}, 32}, [{{127,0,0,1}, 32}, {{0,0,0,0,0,0,0,1}, 128}]),
  654. check_type(Config, inet, "'127.0.0.1'", {127,0,0,1}, [{127,0,0,1}, {0,0,0,0,0,0,0,1}]).
  655. array_type(Config) ->
  656. Module = ?config(module, Config),
  657. epgsql_ct:with_connection(Config, fun(C) ->
  658. {ok, _, [{[1, 2]}]} = Module:equery(C, "select ($1::int[])[1:2]", [[1, 2, 3]]),
  659. {ok, _, [{[{1, <<"one">>}, {2, <<"two">>}]}]} =
  660. Module:equery(C, "select Array(select (id, value) from test_table1)", []),
  661. Select = fun(Type, A) ->
  662. Query = "select $1::" ++ atom_to_list(Type) ++ "[]",
  663. {ok, _Cols, [{A2}]} = Module:equery(C, Query, [A]),
  664. case lists:all(fun({V, V2}) -> compare(Type, V, V2) end, lists:zip(A, A2)) of
  665. true -> ok;
  666. false -> ?assertMatch(A, A2)
  667. end
  668. end,
  669. Select(int2, []),
  670. Select(int2, [1, 2, 3, 4]),
  671. Select(int2, [[1], [2], [3], [4]]),
  672. Select(int2, [[[[[[1, 2]]]]]]),
  673. Select(bool, [true]),
  674. Select(char, [$a, $b, $c]),
  675. Select(int4, [[1, 2]]),
  676. Select(int8, [[[[1, 2]], [[3, 4]]]]),
  677. Select(text, [<<"one">>, <<"two>">>]),
  678. Select(varchar, [<<"one">>, <<"two>">>]),
  679. Select(float4, [0.0, 1.0, 0.123]),
  680. Select(float8, [0.0, 1.0, 0.123]),
  681. Select(date, [{2008,1,2}, {2008,1,3}]),
  682. Select(time, [{0,1,2.0}, {0,1,3.0}]),
  683. Select(timetz, [{{0,1,2.0},1*60*60}, {{0,1,3.0},1*60*60}]),
  684. Select(timestamp, [{{2008,1,2},{3,4,5.0}}, {{2008,1,2},{3,4,6.0}}]),
  685. Select(timestamptz, [{{2008,1,2},{3,4,5.0}}, {{2008,1,2},{3,4,6.0}}]),
  686. Select(interval, [{{1,2,3.1},0,0}, {{1,2,3.2},0,0}]),
  687. Select(hstore, [{[{null, null}, {a, 1}, {1, 2}, {b, undefined}]}]),
  688. Select(hstore, [[{[{null, null}, {a, 1}, {1, 2}, {b, undefined}]}, {[]}], [{[{a, 1}]}, {[{null, 2}]}]]),
  689. Select(cidr, [{{127,0,0,1}, 32}, {{0,0,0,0,0,0,0,1}, 128}]),
  690. Select(inet, [{127,0,0,1}, {0,0,0,0,0,0,0,1}]),
  691. Select(json, [<<"{}">>, <<"[]">>, <<"1">>, <<"1.0">>, <<"true">>, <<"\"string\"">>, <<"{\"key\": []}">>]),
  692. Select(jsonb, [<<"{}">>, <<"[]">>, <<"1">>, <<"1.0">>, <<"true">>, <<"\"string\"">>, <<"{\"key\": []}">>])
  693. end).
  694. custom_types(Config) ->
  695. Module = ?config(module, Config),
  696. epgsql_ct:with_connection(Config, fun(C) ->
  697. Module:squery(C, "drop table if exists t_foo;"),
  698. Module:squery(C, "drop type foo;"),
  699. {ok, [], []} = Module:squery(C, "create type foo as enum('foo', 'bar');"),
  700. ok = epgsql:update_type_cache(C, [<<"foo">>]),
  701. {ok, [], []} = Module:squery(C, "create table t_foo (col foo);"),
  702. {ok, S} = Module:parse(C, "insert_foo", "insert into t_foo values ($1)", [foo]),
  703. ok = Module:bind(C, S, ["bar"]),
  704. {ok, 1} = Module:execute(C, S)
  705. end).
  706. text_format(Config) ->
  707. Module = ?config(module, Config),
  708. epgsql_ct:with_connection(Config, fun(C) ->
  709. Select = fun(Type, V) ->
  710. V2 = list_to_binary(V),
  711. Query = "select $1::" ++ Type,
  712. {ok, _Cols, [{V2}]} = Module:equery(C, Query, [V]),
  713. {ok, _Cols, [{V2}]} = Module:equery(C, Query, [V2])
  714. end,
  715. Select("numeric", "123456")
  716. end).
  717. query_timeout(Config) ->
  718. Module = ?config(module, Config),
  719. epgsql_ct:with_connection(Config, fun(C) ->
  720. {ok, _, _} = Module:squery(C, "SET statement_timeout = 500"),
  721. ?TIMEOUT_ERROR = Module:squery(C, "SELECT pg_sleep(1)"),
  722. ?TIMEOUT_ERROR = Module:equery(C, "SELECT pg_sleep(2)"),
  723. {ok, _Cols, [{1}]} = Module:equery(C, "SELECT 1")
  724. end, []).
  725. execute_timeout(Config) ->
  726. Module = ?config(module, Config),
  727. epgsql_ct:with_connection(Config, fun(C) ->
  728. {ok, _, _} = Module:squery(C, "SET statement_timeout = 500"),
  729. {ok, S} = Module:parse(C, "select pg_sleep($1)"),
  730. ok = Module:bind(C, S, [2]),
  731. ?TIMEOUT_ERROR = Module:execute(C, S, 0),
  732. ok = Module:sync(C),
  733. ok = Module:bind(C, S, [0]),
  734. {ok, [{<<>>}]} = Module:execute(C, S, 0),
  735. ok = Module:close(C, S),
  736. ok = Module:sync(C)
  737. end, []).
  738. connection_closed(Config) ->
  739. Module = ?config(module, Config),
  740. #{pg_host := Host, pg_port := Port} = ?config(pg_config, Config),
  741. P = self(),
  742. spawn_link(fun() ->
  743. process_flag(trap_exit, true),
  744. {ok, C} = Module:connect(Host, "epgsql_test",[
  745. {port, Port},
  746. {database, "epgsql_test_db1"}
  747. ]),
  748. P ! {connected, C},
  749. receive
  750. Any -> P ! Any
  751. end
  752. end),
  753. receive
  754. {connected, C} ->
  755. timer:sleep(100),
  756. Module:close(C),
  757. {'EXIT', C, _} = receive R -> R end
  758. end,
  759. epgsql_ct:flush().
  760. connection_closed_by_server(Config) ->
  761. Module = ?config(module, Config),
  762. epgsql_ct:with_connection(Config, fun(C1) ->
  763. P = self(),
  764. spawn_link(fun() ->
  765. process_flag(trap_exit, true),
  766. epgsql_ct:with_connection(Config, fun(C2) ->
  767. {ok, _, [{Pid}]} = Module:equery(C2, "select pg_backend_pid()"),
  768. % emulate of disconnection
  769. {ok, _, [{true}]} = Module:equery(C1,
  770. "select pg_terminate_backend($1)", [Pid]),
  771. receive
  772. {'EXIT', C2, {shutdown, #error{code = <<"57P01">>}}} ->
  773. P ! ok;
  774. Other ->
  775. ?debugFmt("Unexpected msg: ~p~n", [Other]),
  776. P ! error
  777. end
  778. end)
  779. end),
  780. receive ok -> ok end
  781. end).
  782. active_connection_closed(Config) ->
  783. Module = ?config(module, Config),
  784. #{pg_host := Host, pg_port := Port} = ?config(pg_config, Config),
  785. P = self(),
  786. F = fun() ->
  787. process_flag(trap_exit, true),
  788. {ok, C} = Module:connect(Host, [
  789. {database, "epgsql_test_db1"},
  790. {port, Port}
  791. ]),
  792. P ! {connected, C},
  793. R = Module:squery(C, "select pg_sleep(10)"),
  794. P ! R
  795. end,
  796. spawn_link(F),
  797. receive
  798. {connected, C} ->
  799. timer:sleep(100),
  800. Module:close(C),
  801. {error, closed} = receive R -> R end
  802. end,
  803. epgsql_ct:flush().
  804. warning_notice(Config) ->
  805. Module = ?config(module, Config),
  806. epgsql_ct:with_connection(Config, fun(C) ->
  807. Q = "create function pg_temp.raise() returns void as $$
  808. begin
  809. raise warning 'oops';
  810. end;
  811. $$ language plpgsql;
  812. select pg_temp.raise()",
  813. [{ok, _, _}, _] = Module:squery(C, Q),
  814. receive
  815. {epgsql, C, {notice, #error{message = <<"oops">>, extra = Extra}}} ->
  816. ?assertMatch([{file, _},{line, _},{routine, _}], Extra),
  817. ok
  818. after
  819. 100 -> erlang:error(didnt_receive_notice)
  820. end
  821. end, [{async, self()}]).
  822. listen_notify(Config) ->
  823. Module = ?config(module, Config),
  824. epgsql_ct:with_connection(Config, fun(C) ->
  825. {ok, [], []} = Module:squery(C, "listen epgsql_test"),
  826. {ok, _, [{Pid}]} = Module:equery(C, "select pg_backend_pid()"),
  827. {ok, [], []} = Module:squery(C, "notify epgsql_test"),
  828. receive
  829. {epgsql, C, {notification, <<"epgsql_test">>, Pid, <<>>}} -> ok
  830. after
  831. 100 -> erlang:error(didnt_receive_notification)
  832. end
  833. end, [{async, self()}]).
  834. listen_notify_payload(Config) ->
  835. Module = ?config(module, Config),
  836. epgsql_ct:with_min_version(Config, 9.0, fun(C) ->
  837. {ok, [], []} = Module:squery(C, "listen epgsql_test"),
  838. {ok, _, [{Pid}]} = Module:equery(C, "select pg_backend_pid()"),
  839. {ok, [], []} = Module:squery(C, "notify epgsql_test, 'test!'"),
  840. receive
  841. {epgsql, C, {notification, <<"epgsql_test">>, Pid, <<"test!">>}} -> ok
  842. after
  843. 100 -> erlang:error(didnt_receive_notification)
  844. end
  845. end, [{async, self()}]).
  846. set_notice_receiver(Config) ->
  847. Module = ?config(module, Config),
  848. epgsql_ct:with_min_version(Config, 9.0, fun(C) ->
  849. {ok, [], []} = Module:squery(C, "listen epgsql_test"),
  850. {ok, _, [{Pid}]} = Module:equery(C, "select pg_backend_pid()"),
  851. EnsureNoNotification = fun(Payload) ->
  852. {ok, [], []} = Module:squery(C, ["notify epgsql_test, '", Payload, "'"]),
  853. receive
  854. {epgsql, _, _} -> erlang:error(got_unexpected_notification)
  855. after
  856. 10 -> ok
  857. end
  858. end,
  859. EnsureNotification = fun(Payload) ->
  860. {ok, [], []} = Module:squery(C, ["notify epgsql_test, '", Payload, "'"]),
  861. receive
  862. {epgsql, C, {notification, <<"epgsql_test">>, Pid, Payload}} -> ok
  863. after
  864. 100 -> erlang:error(didnt_receive_notification)
  865. end
  866. end,
  867. Self = self(),
  868. EnsureNoNotification(<<"test1">>),
  869. % Set pid()
  870. {ok, undefined} = Module:set_notice_receiver(C, Self),
  871. EnsureNotification(<<"test2">>),
  872. %% test PL/PgSQL NOTICE
  873. {ok, [], []} = Module:squery(C, ["DO $$ BEGIN RAISE WARNING 'test notice'; END $$;"]),
  874. receive
  875. {epgsql, C, {notice, #error{severity = warning,
  876. code = <<"01000">>,
  877. message = <<"test notice">>,
  878. extra = _}}} -> ok
  879. after
  880. 100 -> erlang:error(didnt_receive_notice)
  881. end,
  882. % set registered pid
  883. Receiver = pg_notification_receiver,
  884. register(Receiver, Self),
  885. {ok, Self} = Module:set_notice_receiver(C, Receiver),
  886. EnsureNotification(<<"test3">>),
  887. % make registered name invalid
  888. unregister(Receiver),
  889. EnsureNoNotification(<<"test4">>),
  890. % disable
  891. {ok, Receiver} = Module:set_notice_receiver(C, undefined),
  892. EnsureNoNotification(<<"test5">>)
  893. end, []).
  894. get_cmd_status(Config) ->
  895. Module = ?config(module, Config),
  896. epgsql_ct:with_connection(Config, fun(C) ->
  897. {ok, [], []} = Module:squery(C, "BEGIN"),
  898. ?assertEqual({ok, 'begin'}, Module:get_cmd_status(C)),
  899. {ok, [], []} = epgsql:equery(C, "CREATE TEMPORARY TABLE cmd_status_t(col INTEGER)"),
  900. ?assertEqual({ok, 'create'}, Module:get_cmd_status(C)),
  901. %% Some commands have number of affected rows in status
  902. {ok, N} = Module:squery(C, "INSERT INTO cmd_status_t (col) VALUES (1), (2)"),
  903. ?assertEqual({ok, {'insert', N}}, Module:get_cmd_status(C)),
  904. {ok, 1} = Module:squery(C, "UPDATE cmd_status_t SET col=3 WHERE col=1"),
  905. ?assertEqual({ok, {'update', 1}}, Module:get_cmd_status(C)),
  906. %% Failed queries have no status
  907. {error, _} = Module:squery(C, "UPDATE cmd_status_t SET col='text' WHERE col=2"),
  908. ?assertEqual({ok, undefined}, Module:get_cmd_status(C)),
  909. %% if COMMIT failed, status will be 'rollback'
  910. {ok, [], []} = Module:squery(C, "COMMIT"),
  911. ?assertEqual({ok, 'rollback'}, Module:get_cmd_status(C)),
  912. %% Only last command's status returned
  913. [_, _, _] = Module:squery(C, "BEGIN; SELECT 1; COMMIT"),
  914. ?assertEqual({ok, 'commit'}, Module:get_cmd_status(C))
  915. end).
  916. range_type(Config) ->
  917. epgsql_ct:with_min_version(Config, 9.2, fun(_C) ->
  918. check_type(Config, int4range, "int4range(10, 20)", {10, 20}, [
  919. {1, 58}, {-1, 12}, {-985521, 5412687}, {minus_infinity, 0},
  920. {984655, plus_infinity}, {minus_infinity, plus_infinity}
  921. ])
  922. end, []).
  923. range8_type(Config) ->
  924. epgsql_ct:with_min_version(Config, 9.2, fun(_C) ->
  925. check_type(Config, int8range, "int8range(10, 20)", {10, 20}, [
  926. {1, 58}, {-1, 12}, {-9223372036854775808, 5412687},
  927. {minus_infinity, 9223372036854775807},
  928. {984655, plus_infinity}, {minus_infinity, plus_infinity}
  929. ])
  930. end, []).
  931. %% =============================================================================
  932. %% Internal functions
  933. %% ============================================================================
  934. check_type(Config, Type, In, Out, Values) ->
  935. Column = "c_" ++ atom_to_list(Type),
  936. check_type(Config, Type, In, Out, Values, Column).
  937. check_type(Config, Type, In, Out, Values, Column) ->
  938. Module = ?config(module, Config),
  939. epgsql_ct:with_connection(Config, fun(C) ->
  940. Select = io_lib:format("select ~s::~w", [In, Type]),
  941. Res = Module:equery(C, Select),
  942. {ok, [#column{type = Type}], [{Out}]} = Res,
  943. Sql = io_lib:format("insert into test_table2 (~s) values ($1) returning ~s", [Column, Column]),
  944. {ok, #statement{columns = [#column{type = Type}]} = S} = Module:parse(C, Sql),
  945. Insert = fun(V) ->
  946. ok = Module:bind(C, S, [V]),
  947. {ok, 1, [{V2}]} = Module:execute(C, S),
  948. case compare(Type, V, V2) of
  949. true -> ok;
  950. false -> ?debugFmt("~p =/= ~p~n", [V, V2]), ?assert(false)
  951. end,
  952. ok = Module:sync(C)
  953. end,
  954. lists:foreach(Insert, [null, undefined | Values])
  955. end).
  956. compare(_Type, null, null) -> true;
  957. compare(_Type, undefined, null) -> true;
  958. compare(float4, V1, V2) -> abs(V2 - V1) < 0.000001;
  959. compare(float8, V1, V2) -> abs(V2 - V1) < 0.000000000000001;
  960. compare(hstore, {V1}, V2) -> compare(hstore, V1, V2);
  961. compare(hstore, V1, {V2}) -> compare(hstore, V1, V2);
  962. compare(hstore, V1, V2) ->
  963. orddict:from_list(format_hstore(V1)) =:= orddict:from_list(format_hstore(V2));
  964. compare(Type, V1 = {_, _, MS}, {D2, {H2, M2, S2}}) when Type == timestamp;
  965. Type == timestamptz ->
  966. {D1, {H1, M1, S1}} = calendar:now_to_universal_time(V1),
  967. ({D1, H1, M1} =:= {D2, H2, M2}) and (abs(S1 + MS/1000000 - S2) < 0.000000000000001);
  968. compare(_Type, V1, V2) -> V1 =:= V2.
  969. format_hstore({Hstore}) -> Hstore;
  970. format_hstore(Hstore) ->
  971. [{format_hstore_key(Key), format_hstore_value(Value)} || {Key, Value} <- Hstore].
  972. format_hstore_key(Key) -> format_hstore_string(Key).
  973. format_hstore_value(null) -> null;
  974. format_hstore_value(undefined) -> null;
  975. format_hstore_value(Value) -> format_hstore_string(Value).
  976. format_hstore_string(Num) when is_number(Num) -> iolist_to_binary(io_lib:format("~w", [Num]));
  977. format_hstore_string(Str) -> iolist_to_binary(io_lib:format("~s", [Str])).