epgsql_SUITE.erl 41 KB

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