epgsql_SUITE.erl 41 KB

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