epgsql_SUITE.erl 41 KB

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