epgsql_SUITE.erl 43 KB

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