epgsql_SUITE.erl 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  1. -module(epgsql_SUITE).
  2. -include_lib("stdlib/include/assert.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, nowarn_export_all]).
  16. modules() ->
  17. [
  18. epgsql,
  19. epgsql_cast,
  20. epgsql_incremental
  21. ].
  22. init_per_suite(Config) ->
  23. Config.
  24. all() ->
  25. [{group, M} || M <- modules()].
  26. groups() ->
  27. Groups = [
  28. {connect, [parrallel], [
  29. connect,
  30. connect_to_db,
  31. connect_as,
  32. connect_with_cleartext,
  33. connect_with_md5,
  34. connect_with_scram,
  35. connect_with_invalid_user,
  36. connect_with_invalid_password,
  37. connect_to_invalid_database,
  38. connect_with_other_error,
  39. connect_with_ssl,
  40. connect_with_client_cert,
  41. connect_with_invalid_client_cert,
  42. connect_to_closed_port,
  43. connect_map,
  44. connect_proplist
  45. ]},
  46. {types, [parallel], [
  47. numeric_type,
  48. character_type,
  49. uuid_type,
  50. point_type,
  51. geometry_type,
  52. uuid_select,
  53. date_time_type,
  54. json_type,
  55. misc_type,
  56. hstore_type,
  57. net_type,
  58. array_type,
  59. record_type,
  60. range_type,
  61. range8_type,
  62. date_time_range_type,
  63. custom_types,
  64. custom_null
  65. ]},
  66. {generic, [parallel], [
  67. with_transaction
  68. ]}
  69. ],
  70. Tests = [
  71. {group, connect},
  72. {group, types},
  73. prepared_query,
  74. select,
  75. insert,
  76. update,
  77. delete,
  78. create_and_drop_table,
  79. cursor,
  80. multiple_result,
  81. execute_batch,
  82. execute_batch_3_named_stmt,
  83. execute_batch_3_unnamed_stmt,
  84. execute_batch_3_sql,
  85. batch_error,
  86. single_batch,
  87. extended_select,
  88. extended_sync_ok,
  89. extended_sync_error,
  90. returning_from_insert,
  91. returning_from_update,
  92. returning_from_delete,
  93. parse,
  94. parse_column_format,
  95. parse_error,
  96. parse_and_close,
  97. bind,
  98. bind_parameter_format,
  99. bind_error,
  100. bind_and_close,
  101. execute_error,
  102. describe,
  103. describe_with_param,
  104. describe_named,
  105. describe_error,
  106. describe_portal,
  107. portal,
  108. returning,
  109. multiple_statement,
  110. multiple_portal,
  111. execute_function,
  112. parameter_get,
  113. parameter_set,
  114. text_format,
  115. query_timeout,
  116. execute_timeout,
  117. connection_closed,
  118. connection_closed_by_server,
  119. active_connection_closed,
  120. warning_notice,
  121. listen_notify,
  122. listen_notify_payload,
  123. set_notice_receiver,
  124. get_cmd_status
  125. ],
  126. Groups ++ [case Module of
  127. epgsql ->
  128. {Module, [], [{group, generic} | Tests]};
  129. _ ->
  130. {Module, [], Tests}
  131. end || Module <- modules()].
  132. end_per_suite(_Config) ->
  133. ok.
  134. init_per_group(GroupName, Config) ->
  135. case lists:member(GroupName, modules()) of
  136. true -> [{module, GroupName}|Config];
  137. false -> Config
  138. end.
  139. end_per_group(_GroupName, _Config) ->
  140. ok.
  141. -define(UUID1,
  142. <<163,189,240,40,149,151,17,227,141,6,112,24,139,130,16,73>>).
  143. -define(UUID2,
  144. <<183,55,22,52,149,151,17,227,187,167,112,24,139,130,16,73>>).
  145. -define(UUID3,
  146. <<198,188,155,66,149,151,17,227,138,98,112,24,139,130,16,73>>).
  147. -define(TIMEOUT_ERROR, {error, #error{
  148. severity = error,
  149. code = <<"57014">>,
  150. codename = query_canceled,
  151. message = <<"canceling statement due to statement timeout">>,
  152. extra = [{file, <<"postgres.c">>},
  153. {line, _},
  154. {routine, _} | _]
  155. }}).
  156. %% From uuid.erl in http://gitorious.org/avtobiff/erlang-uuid
  157. uuid_to_bin_string(<<U0:32, U1:16, U2:16, U3:16, U4:48>>) ->
  158. iolist_to_binary(io_lib:format(
  159. "~8.16.0b-~4.16.0b-~4.16.0b-~4.16.0b-~12.16.0b",
  160. [U0, U1, U2, U3, U4])).
  161. connect(Config) ->
  162. epgsql_ct:connect_only(Config, []).
  163. connect_to_db(Connect) ->
  164. epgsql_ct:connect_only(Connect, [{database, "epgsql_test_db1"}]).
  165. connect_as(Config) ->
  166. epgsql_ct:connect_only(Config, ["epgsql_test", [{database, "epgsql_test_db1"}]]).
  167. connect_with_cleartext(Config) ->
  168. epgsql_ct:connect_only(Config, [
  169. "epgsql_test_cleartext",
  170. "epgsql_test_cleartext",
  171. [{database, "epgsql_test_db1"}]
  172. ]).
  173. connect_with_md5(Config) ->
  174. epgsql_ct:connect_only(Config, [
  175. "epgsql_test_md5",
  176. "epgsql_test_md5",
  177. [{database, "epgsql_test_db1"}]
  178. ]).
  179. connect_with_scram(Config) ->
  180. PgConf = ?config(pg_config, Config),
  181. Ver = ?config(version, PgConf),
  182. (Ver >= [10, 0])
  183. andalso
  184. epgsql_ct:connect_only(
  185. Config,
  186. [
  187. "epgsql_test_scram",
  188. "epgsql_test_scram",
  189. [{database, "epgsql_test_db1"}]
  190. ]).
  191. connect_with_invalid_user(Config) ->
  192. {Host, Port} = epgsql_ct:connection_data(Config),
  193. Module = ?config(module, Config),
  194. {error, Why} = Module:connect(
  195. Host,
  196. "epgsql_test_invalid",
  197. "epgsql_test_invalid",
  198. [{port, Port}, {database, "epgsql_test_db1"}]),
  199. case Why of
  200. invalid_authorization_specification -> ok; % =< 8.4
  201. invalid_password -> ok % >= 9.0
  202. end.
  203. connect_with_invalid_password(Config) ->
  204. {Host, Port} = epgsql_ct:connection_data(Config),
  205. Module = ?config(module, Config),
  206. {error, Why} = Module:connect(
  207. Host,
  208. "epgsql_test_md5",
  209. "epgsql_test_invalid",
  210. [{port, Port}, {database, "epgsql_test_db1"}]),
  211. case Why of
  212. invalid_authorization_specification -> ok; % =< 8.4
  213. invalid_password -> ok % >= 9.0
  214. end.
  215. connect_to_invalid_database(Config) ->
  216. {Host, Port} = epgsql_ct:connection_data(Config),
  217. Module = ?config(module, Config),
  218. ?assertMatch(
  219. {error, invalid_authorization_specification},
  220. Module:connect(
  221. Host,
  222. "epgsql_test_md5",
  223. "epgsql_test_md5",
  224. [{port, Port}, {database, "epgsql_test_invalid_db"}])).
  225. connect_with_other_error(Config) ->
  226. {Host, Port} = epgsql_ct:connection_data(Config),
  227. Module = ?config(module, Config),
  228. ?assertMatch(
  229. {error,
  230. #error{severity = fatal,
  231. codename = protocol_violation}},
  232. Module:connect(
  233. Host,
  234. <<0, 0>>,
  235. "epgsql_test_invalid",
  236. [{port, Port}, {database, <<0, 0>>}])).
  237. connect_with_ssl(Config) ->
  238. Module = ?config(module, Config),
  239. epgsql_ct:with_connection(Config,
  240. fun(C) ->
  241. {ok, _Cols, [{true}]} = Module:equery(C, "select ssl_is_used()")
  242. end,
  243. "epgsql_test",
  244. [{ssl, true}]).
  245. connect_with_client_cert(Config) ->
  246. Module = ?config(module, Config),
  247. Dir = filename:join(code:lib_dir(epgsql), ?TEST_DATA_DIR),
  248. File = fun(Name) -> filename:join(Dir, Name) end,
  249. {ok, Pem} = file:read_file(File("client.crt")),
  250. [{'Certificate', Der, not_encrypted}] = public_key:pem_decode(Pem),
  251. Cert = public_key:pkix_decode_cert(Der, plain),
  252. #'TBSCertificate'{serialNumber = Serial} = Cert#'Certificate'.tbsCertificate,
  253. Serial2 = list_to_binary(integer_to_list(Serial)),
  254. epgsql_ct:with_connection(Config,
  255. fun(C) ->
  256. ?assertMatch({ok, _, [{true}]}, Module:equery(C, "select ssl_is_used()")),
  257. ?assertMatch({ok, _, [{Serial2}]}, Module:equery(C, "select ssl_client_serial()"))
  258. end,
  259. "epgsql_test_cert",
  260. [{ssl, true}, {ssl_opts, [{keyfile, File("client.key")},
  261. {certfile, File("client.crt")}]}]).
  262. connect_with_invalid_client_cert(Config) ->
  263. {Host, Port} = epgsql_ct:connection_data(Config),
  264. Module = ?config(module, Config),
  265. Dir = filename:join(code:lib_dir(epgsql), ?TEST_DATA_DIR),
  266. File = fun(Name) -> filename:join(Dir, Name) end,
  267. Trap = process_flag(trap_exit, true),
  268. ?assertMatch(
  269. {error, {ssl_negotiation_failed, _}},
  270. Module:connect(
  271. #{username => "epgsql_test_cert",
  272. database => "epgsql_test_db1",
  273. host => Host,
  274. port => Port,
  275. ssl => true,
  276. ssl_opts =>
  277. [{keyfile, File("bad-client.key")},
  278. {certfile, File("bad-client.crt")}]}
  279. )),
  280. ?assertMatch({'EXIT', _, {ssl_negotiation_failed, _}}, receive Stop -> Stop end),
  281. process_flag(trap_exit, Trap).
  282. connect_map(Config) ->
  283. {Host, Port} = epgsql_ct:connection_data(Config),
  284. Module = ?config(module, Config),
  285. Opts = #{
  286. host => Host,
  287. port => Port,
  288. database => "epgsql_test_db1",
  289. username => "epgsql_test_md5",
  290. password => "epgsql_test_md5"
  291. },
  292. {ok, C} = Module:connect(Opts),
  293. Module:close(C),
  294. epgsql_ct:flush(),
  295. ok.
  296. connect_proplist(Config) ->
  297. {Host, Port} = epgsql_ct:connection_data(Config),
  298. Module = ?config(module, Config),
  299. Opts = [
  300. {host, Host},
  301. {port, Port},
  302. {database, "epgsql_test_db1"},
  303. {username, "epgsql_test_md5"},
  304. {password, "epgsql_test_md5"}
  305. ],
  306. {ok, C} = Module:connect(Opts),
  307. Module:close(C),
  308. epgsql_ct:flush(),
  309. ok.
  310. connect_to_closed_port(Config) ->
  311. {Host, Port} = epgsql_ct:connection_data(Config),
  312. Module = ?config(module, Config),
  313. Trap = process_flag(trap_exit, true),
  314. ?assertEqual({error, econnrefused},
  315. Module:connect(
  316. Host,
  317. "epgsql_test",
  318. "epgsql_test",
  319. [{port, Port + 1}, {database, "epgsql_test_db1"}])),
  320. ?assertMatch({'EXIT', _, econnrefused}, receive Stop -> Stop end),
  321. process_flag(trap_exit, Trap).
  322. prepared_query(Config) ->
  323. Module = ?config(module, Config),
  324. epgsql_ct:with_connection(Config, fun(C) ->
  325. {ok, _} = Module:parse(C, "inc", "select $1+1", []),
  326. {ok, Cols, [{5}]} = Module:prepared_query(C, "inc", [4]),
  327. {ok, Cols, [{2}]} = Module:prepared_query(C, "inc", [1]),
  328. {ok, Cols, [{23}]} = Module:prepared_query(C, "inc", [22]),
  329. {error, _} = Module:prepared_query(C, "non_existent_query", [4])
  330. end).
  331. select(Config) ->
  332. Module = ?config(module, Config),
  333. epgsql_ct:with_connection(Config, fun(C) ->
  334. {ok, Cols, Rows} = Module:squery(C, "select * from test_table1"),
  335. [
  336. #column{name = <<"id">>, type = int4, size = 4},
  337. #column{name = <<"value">>, type = text, size = -1}
  338. ] = Cols,
  339. [{<<"1">>, <<"one">>}, {<<"2">>, <<"two">>}] = Rows
  340. end).
  341. insert(Config) ->
  342. Module = ?config(module, Config),
  343. epgsql_ct:with_rollback(Config, fun(C) ->
  344. {ok, 1} = Module:squery(C, "insert into test_table1 (id, value) values (3, 'three')")
  345. end).
  346. update(Config) ->
  347. Module = ?config(module, Config),
  348. epgsql_ct:with_rollback(Config, fun(C) ->
  349. {ok, 1} = Module:squery(C, "insert into test_table1 (id, value) values (3, 'three')"),
  350. {ok, 1} = Module:squery(C, "insert into test_table1 (id, value) values (4, 'four')"),
  351. {ok, 2} = Module:squery(C, "update test_table1 set value = 'foo' where id > 2"),
  352. {ok, _, [{<<"2">>}]} = Module:squery(C, "select count(*) from test_table1 where value = 'foo'")
  353. end).
  354. delete(Config) ->
  355. Module = ?config(module, Config),
  356. epgsql_ct:with_rollback(Config, fun(C) ->
  357. {ok, 1} = Module:squery(C, "insert into test_table1 (id, value) values (3, 'three')"),
  358. {ok, 1} = Module:squery(C, "insert into test_table1 (id, value) values (4, 'four')"),
  359. {ok, 2} = Module:squery(C, "delete from test_table1 where id > 2"),
  360. {ok, _, [{<<"2">>}]} = Module:squery(C, "select count(*) from test_table1")
  361. end).
  362. create_and_drop_table(Config) ->
  363. Module = ?config(module, Config),
  364. epgsql_ct:with_rollback(Config, fun(C) ->
  365. {ok, [], []} = Module:squery(C, "create table test_table3 (id int4)"),
  366. {ok, [#column{type = int4}], []} = Module:squery(C, "select * from test_table3"),
  367. {ok, [], []} = Module:squery(C, "drop table test_table3")
  368. end).
  369. cursor(Config) ->
  370. Module = ?config(module, Config),
  371. epgsql_ct:with_connection(Config, fun(C) ->
  372. {ok, [], []} = Module:squery(C, "begin"),
  373. {ok, [], []} = Module:squery(C, "declare c cursor for select id from test_table1"),
  374. {ok, 2} = Module:squery(C, "move forward 2 from c"),
  375. {ok, 1} = Module:squery(C, "move backward 1 from c"),
  376. {ok, 1, _Cols, [{<<"2">>}]} = Module:squery(C, "fetch next from c"),
  377. {ok, [], []} = Module:squery(C, "close c")
  378. end).
  379. multiple_result(Config) ->
  380. Module = ?config(module, Config),
  381. epgsql_ct:with_connection(Config, fun(C) ->
  382. [{ok, _, [{<<"1">>}]}, {ok, _, [{<<"2">>}]}] = Module:squery(C, "select 1; select 2"),
  383. [{ok, _, [{<<"1">>}]}, {error, #error{}}] = Module:squery(C, "select 1; select foo;")
  384. end).
  385. execute_batch(Config) ->
  386. Module = ?config(module, Config),
  387. epgsql_ct:with_connection(Config, fun(C) ->
  388. {ok, S1} = Module:parse(C, "one", "select $1", [int4]),
  389. {ok, S2} = Module:parse(C, "two", "select $1 + $2", [int4, int4]),
  390. [{ok, [{1}]}, {ok, [{3}]}] =
  391. Module:execute_batch(C, [{S1, [1]}, {S2, [1, 2]}])
  392. end).
  393. execute_batch_3_named_stmt(Config) ->
  394. Module = ?config(module, Config),
  395. epgsql_ct:with_connection(Config, fun(C) ->
  396. {ok, Stmt} = Module:parse(C, "my_stmt", "select $1 + $2", [int4, int4]),
  397. ?assertMatch(
  398. {[#column{type = int4, _ = _}], [{ok, [{3}]}, {ok, [{7}]}]},
  399. Module:execute_batch(C, Stmt, [[1, 2], [3, 4]]))
  400. end).
  401. execute_batch_3_unnamed_stmt(Config) ->
  402. Module = ?config(module, Config),
  403. epgsql_ct:with_connection(Config, fun(C) ->
  404. {ok, Stmt} = Module:parse(C, "select $1::integer + $2::integer"),
  405. ?assertMatch(
  406. {[#column{type = int4, _ = _}], [{ok, [{3}]}, {ok, [{7}]}]},
  407. Module:execute_batch(C, Stmt, [[2, 1], [4, 3]]))
  408. end).
  409. execute_batch_3_sql(Config) ->
  410. Module = ?config(module, Config),
  411. epgsql_ct:with_connection(Config, fun(C) ->
  412. ?assertMatch(
  413. {[#column{type = int4, _ = _}], [{ok, [{3}]}, {ok, [{7}]}]},
  414. Module:execute_batch(C, "select $1::integer + $2::integer", [[1, 2], [3, 4]]))
  415. end).
  416. batch_error(Config) ->
  417. Module = ?config(module, Config),
  418. epgsql_ct:with_rollback(Config, fun(C) ->
  419. {ok, S} = Module:parse(C, "insert into test_table1(id, value) values($1, $2)"),
  420. [{ok, 1}, {error, _}] =
  421. Module:execute_batch(
  422. C,
  423. [{S, [3, "batch_error 3"]},
  424. {S, [2, "batch_error 2"]}, % duplicate key error
  425. {S, [5, "batch_error 5"]} % won't be executed
  426. ])
  427. end).
  428. single_batch(Config) ->
  429. Module = ?config(module, Config),
  430. epgsql_ct:with_connection(Config, fun(C) ->
  431. {ok, S1} = Module:parse(C, "one", "select $1", [int4]),
  432. [{ok, [{1}]}] = Module:execute_batch(C, [{S1, [1]}])
  433. end).
  434. extended_select(Config) ->
  435. Module = ?config(module, Config),
  436. epgsql_ct:with_connection(Config, fun(C) ->
  437. {ok, Cols, Rows} = Module:equery(C, "select * from test_table1", []),
  438. [#column{name = <<"id">>, type = int4, size = 4},
  439. #column{name = <<"value">>, type = text, size = -1}] = Cols,
  440. [{1, <<"one">>}, {2, <<"two">>}] = Rows
  441. end).
  442. extended_sync_ok(Config) ->
  443. Module = ?config(module, Config),
  444. epgsql_ct:with_connection(Config, fun(C) ->
  445. {ok, _Cols, [{<<"one">>}]} = Module:equery(C, "select value from test_table1 where id = $1", [1]),
  446. {ok, _Cols, [{<<"two">>}]} = Module:equery(C, "select value from test_table1 where id = $1", [2])
  447. end).
  448. extended_sync_error(Config) ->
  449. Module = ?config(module, Config),
  450. epgsql_ct:with_connection(Config, fun(C) ->
  451. {error, #error{}} = Module:equery(C, "select _alue from test_table1 where id = $1", [1]),
  452. {ok, _Cols, [{<<"one">>}]} = Module:equery(C, "select value from test_table1 where id = $1", [1])
  453. end).
  454. returning_from_insert(Config) ->
  455. Module = ?config(module, Config),
  456. epgsql_ct:with_rollback(Config, fun(C) ->
  457. {ok, 1, _Cols, [{3}]} = Module:equery(C, "insert into test_table1 (id) values (3) returning id")
  458. end).
  459. returning_from_update(Config) ->
  460. Module = ?config(module, Config),
  461. epgsql_ct:with_rollback(Config, fun(C) ->
  462. {ok, 2, _Cols, [{1}, {2}]} = Module:equery(C, "update test_table1 set value = 'hi' returning id"),
  463. ?assertMatch({ok, 0, [#column{}], []},
  464. Module:equery(C, "update test_table1 set value = 'hi' where false returning id")),
  465. ?assertMatch([{ok, 2, [#column{}], [{<<"1">>}, {<<"2">>}]},
  466. {ok, 0, [#column{}], []}],
  467. Module:squery(C,
  468. "update test_table1 set value = 'hi2' returning id; "
  469. "update test_table1 set value = 'hi' where false returning id"))
  470. end).
  471. returning_from_delete(Config) ->
  472. Module = ?config(module, Config),
  473. epgsql_ct:with_rollback(Config, fun(C) ->
  474. {ok, 2, _Cols, [{1}, {2}]} = Module:equery(C, "delete from test_table1 returning id"),
  475. ?assertMatch({ok, 0, [#column{}], []},
  476. Module:equery(C, "delete from test_table1 returning id"))
  477. end).
  478. parse(Config) ->
  479. Module = ?config(module, Config),
  480. epgsql_ct:with_connection(Config, fun(C) ->
  481. {ok, S} = Module:parse(C, "select * from test_table1"),
  482. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  483. ok = Module:close(C, S),
  484. ok = Module:sync(C)
  485. end).
  486. parse_column_format(Config) ->
  487. Module = ?config(module, Config),
  488. epgsql_ct:with_connection(Config, fun(C) ->
  489. {ok, S} = Module:parse(C, "select 1::int4, false::bool, 2.0::float4"),
  490. [#column{type = int4, table_oid = 0, table_attr_number = 0},
  491. #column{type = bool, table_oid = 0, table_attr_number = 0},
  492. #column{type = float4, table_oid = 0, table_attr_number = 0}] = S#statement.columns,
  493. ok = Module:bind(C, S, []),
  494. {ok, [{1, false, 2.0}]} = Module:execute(C, S, 0),
  495. ok = Module:close(C, S),
  496. ok = Module:sync(C)
  497. end).
  498. parse_error(Config) ->
  499. Module = ?config(module, Config),
  500. epgsql_ct:with_connection(Config, fun(C) ->
  501. {error, #error{
  502. extra = [{file, _}, {line, _}, {position, <<"8">>}, {routine, _} | _]
  503. }} = Module:parse(C, "select _ from test_table1"),
  504. {ok, S} = Module:parse(C, "select * from test_table1"),
  505. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  506. ok = Module:close(C, S),
  507. ok = Module:sync(C)
  508. end).
  509. parse_and_close(Config) ->
  510. Module = ?config(module, Config),
  511. epgsql_ct:with_connection(Config, fun(C) ->
  512. Parse = fun() -> Module:parse(C, "test", "select * from test_table1", []) end,
  513. {ok, S} = Parse(),
  514. {error, #error{code = <<"42P05">>, codename = duplicate_prepared_statement}} = Parse(),
  515. Module:close(C, S),
  516. {ok, S} = Parse(),
  517. ok = Module:sync(C)
  518. end).
  519. bind(Config) ->
  520. Module = ?config(module, Config),
  521. epgsql_ct:with_connection(Config, fun(C) ->
  522. {ok, S} = Module:parse(C, "select value from test_table1 where id = $1"),
  523. ok = Module:bind(C, S, [1]),
  524. ok = Module:close(C, S),
  525. ok = Module:sync(C)
  526. end).
  527. bind_parameter_format(Config) ->
  528. Module = ?config(module, Config),
  529. epgsql_ct:with_connection(Config, fun(C) ->
  530. {ok, S} = Module:parse(C, "select $1, $2, $3", [int2, text, bool]),
  531. [int2, text, bool] = S#statement.types,
  532. ok = Module:bind(C, S, [1, "hi", true]),
  533. {ok, [{1, <<"hi">>, true}]} = Module:execute(C, S, 0),
  534. ok = Module:close(C, S),
  535. ok = Module:sync(C)
  536. end).
  537. bind_error(Config) ->
  538. Module = ?config(module, Config),
  539. epgsql_ct:with_connection(Config, fun(C) ->
  540. {ok, S} = Module:parse(C, "select $1::char"),
  541. {error, #error{}} = Module:bind(C, S, [0]),
  542. ok = Module:bind(C, S, [$A]),
  543. ok = Module:close(C, S),
  544. ok = Module:sync(C)
  545. end).
  546. bind_and_close(Config) ->
  547. Module = ?config(module, Config),
  548. epgsql_ct:with_connection(Config, fun(C) ->
  549. {ok, S} = Module:parse(C, "select * from test_table1"),
  550. ok = Module:bind(C, S, "one", []),
  551. {error, #error{code = <<"42P03">>, codename = duplicate_cursor}} = Module:bind(C, S, "one", []),
  552. ok = Module:close(C, portal, "one"),
  553. ok = Module:bind(C, S, "one", []),
  554. ok = Module:sync(C)
  555. end).
  556. execute_error(Config) ->
  557. Module = ?config(module, Config),
  558. epgsql_ct:with_connection(Config, fun(C) ->
  559. {ok, S} = Module:parse(C, "insert into test_table1 (id, value) values ($1, $2)"),
  560. ok = Module:bind(C, S, [1, <<"foo">>]),
  561. {error, #error{
  562. code = <<"23505">>, codename = unique_violation,
  563. extra = [
  564. {constraint_name, <<"test_table1_pkey">>},
  565. {detail, _},
  566. {file, _},
  567. {line, _},
  568. {routine, _},
  569. {schema_name, <<"public">>} | _%,
  570. %{table_name, <<"test_table1">>}
  571. ]
  572. }} = Module:execute(C, S, 0),
  573. {error, sync_required} = Module:bind(C, S, [3, <<"quux">>]),
  574. ok = Module:sync(C),
  575. ok = Module:bind(C, S, [3, <<"quux">>]),
  576. {ok, _} = Module:execute(C, S, 0),
  577. {ok, 1} = Module:squery(C, "delete from test_table1 where id = 3")
  578. end).
  579. describe(Config) ->
  580. Module = ?config(module, Config),
  581. epgsql_ct:with_connection(Config, fun(C) ->
  582. {ok, S} = Module:parse(C, "select * from test_table1"),
  583. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  584. {ok, S} = Module:describe(C, S),
  585. ok = Module:close(C, S),
  586. ok = Module:sync(C)
  587. end).
  588. describe_with_param(Config) ->
  589. Module = ?config(module, Config),
  590. epgsql_ct:with_connection(Config, fun(C) ->
  591. {ok, S} = Module:parse(C, "select id from test_table1 where id = $1"),
  592. ?assertEqual([int4], S#statement.types),
  593. ?assertMatch([#column{name = <<"id">>}], S#statement.columns),
  594. {ok, S} = Module:describe(C, S),
  595. ok = Module:close(C, S),
  596. ok = Module:sync(C)
  597. end).
  598. describe_named(Config) ->
  599. Module = ?config(module, Config),
  600. epgsql_ct:with_connection(Config, fun(C) ->
  601. {ok, S} = Module:parse(C, "name", "select * from test_table1", []),
  602. [#column{name = <<"id">>}, #column{name = <<"value">>}] = S#statement.columns,
  603. {ok, S} = Module:describe(C, S),
  604. ok = Module:close(C, S),
  605. ok = Module:sync(C)
  606. end).
  607. describe_error(Config) ->
  608. Module = ?config(module, Config),
  609. epgsql_ct:with_connection(Config, fun(C) ->
  610. {error, #error{}} = Module:describe(C, statement, ""),
  611. {ok, S} = Module:parse(C, "select * from test_table1"),
  612. {ok, S} = Module:describe(C, statement, ""),
  613. ok = Module:sync(C)
  614. end).
  615. describe_portal(Config) ->
  616. Module = ?config(module, Config),
  617. epgsql_ct:with_connection(Config, fun(C) ->
  618. {ok, Stmt} = Module:parse(C, "my_stmt", "select * from test_table1 WHERE id = $1", []),
  619. ok = Module:bind(C, Stmt, "my_portal", [1]),
  620. {ok, Columns} = Module:describe(C, portal, "my_portal"),
  621. ?assertMatch(
  622. [#column{name = <<"id">>,
  623. type = int4},
  624. #column{name = <<"value">>,
  625. type = text}],
  626. Columns
  627. ),
  628. ok = Module:close(C, Stmt),
  629. ok = Module:sync(C)
  630. end).
  631. portal(Config) ->
  632. Module = ?config(module, Config),
  633. epgsql_ct:with_connection(Config, fun(C) ->
  634. {ok, S} = Module:parse(C, "select value from test_table1"),
  635. ok = Module:bind(C, S, []),
  636. {partial, [{<<"one">>}]} = Module:execute(C, S, 1),
  637. {partial, [{<<"two">>}]} = Module:execute(C, S, 1),
  638. {ok, []} = Module:execute(C, S,1),
  639. ok = Module:close(C, S),
  640. ok = Module:sync(C)
  641. end).
  642. returning(Config) ->
  643. Module = ?config(module, Config),
  644. epgsql_ct:with_rollback(Config, fun(C) ->
  645. {ok, S} = Module:parse(C, "update test_table1 set value = $1 returning id"),
  646. ok = Module:bind(C, S, ["foo"]),
  647. {ok, 2, [{1}, {2}]} = Module:execute(C, S),
  648. ok = Module:sync(C)
  649. end).
  650. multiple_statement(Config) ->
  651. Module = ?config(module, Config),
  652. epgsql_ct:with_connection(Config, fun(C) ->
  653. {ok, S1} = Module:parse(C, "one", "select value from test_table1 where id = 1", []),
  654. ok = Module:bind(C, S1, []),
  655. {partial, [{<<"one">>}]} = Module:execute(C, S1, 1),
  656. {ok, S2} = Module:parse(C, "two", "select value from test_table1 where id = 2", []),
  657. ok = Module:bind(C, S2, []),
  658. {partial, [{<<"two">>}]} = Module:execute(C, S2, 1),
  659. {ok, []} = Module:execute(C, S1, 1),
  660. {ok, []} = Module:execute(C, S2, 1),
  661. ok = Module:close(C, S1),
  662. ok = Module:close(C, S2),
  663. ok = Module:sync(C)
  664. end).
  665. multiple_portal(Config) ->
  666. Module = ?config(module, Config),
  667. epgsql_ct:with_connection(Config, fun(C) ->
  668. {ok, S} = Module:parse(C, "select value from test_table1 where id = $1"),
  669. ok = Module:bind(C, S, "one", [1]),
  670. ok = Module:bind(C, S, "two", [2]),
  671. {ok, [{<<"one">>}]} = Module:execute(C, S, "one", 0),
  672. {ok, [{<<"two">>}]} = Module:execute(C, S, "two", 0),
  673. ok = Module:close(C, S),
  674. ok = Module:sync(C)
  675. end).
  676. execute_function(Config) ->
  677. Module = ?config(module, Config),
  678. epgsql_ct:with_rollback(Config, fun(C) ->
  679. {ok, _Cols1, [{3}]} = Module:equery(C, "select insert_test1(3, 'three')"),
  680. {ok, _Cols2, [{<<>>}]} = Module:equery(C, "select do_nothing()")
  681. end).
  682. parameter_get(Config) ->
  683. Module = ?config(module, Config),
  684. epgsql_ct:with_connection(Config, fun(C) ->
  685. {ok, <<"off">>} = Module:get_parameter(C, "is_superuser")
  686. end).
  687. parameter_set(Config) ->
  688. Module = ?config(module, Config),
  689. epgsql_ct:with_connection(Config, fun(C) ->
  690. {ok, [], []} = Module:squery(C, "set DateStyle = 'ISO, MDY'"),
  691. {ok, <<"ISO, MDY">>} = Module:get_parameter(C, "DateStyle"),
  692. {ok, _Cols, [{<<"2000-01-02">>}]} = Module:squery(C, "select '2000-01-02'::date"),
  693. {ok, [], []} = Module:squery(C, "set DateStyle = 'German'"),
  694. {ok, <<"German, DMY">>} = Module:get_parameter(C, "DateStyle"),
  695. {ok, _Cols, [{<<"02.01.2000">>}]} = Module:squery(C, "select '2000-01-02'::date")
  696. end).
  697. numeric_type(Config) ->
  698. check_type(Config, int2, "1", 1, [0, 256, -32768, +32767]),
  699. check_type(Config, int4, "1", 1, [0, 512, -2147483648, +2147483647]),
  700. check_type(Config, int8, "1", 1, [0, 1024, -9223372036854775808, +9223372036854775807]),
  701. check_type(Config, float4, "1.0", 1.0, [0.0, 1.23456, -1.23456]),
  702. check_type(Config, float4, "'-Infinity'", minus_infinity, [minus_infinity, plus_infinity, nan]),
  703. check_type(Config, float8, "1.0", 1.0, [0.0, 1.23456789012345, -1.23456789012345]),
  704. check_type(Config, float8, "'nan'", nan, [minus_infinity, plus_infinity, nan]),
  705. %% Check overflow protection. Connection just crashes for now instead of silently
  706. %% truncating the data. Some cleaner behaviour can be introduced later.
  707. epgsql_ct:with_connection(Config, fun(C) ->
  708. Module = ?config(module, Config),
  709. Trap = process_flag(trap_exit, true),
  710. try Module:equery(C, "SELECT $1::int2", [32768]) of
  711. {error, closed} ->
  712. %% epgsqla/epgsqli
  713. ok
  714. catch exit:Reason ->
  715. %% epgsql
  716. ?assertMatch({{{integer_overflow, int2, _}, _}, _}, Reason),
  717. receive {'EXIT', C, _} -> ok
  718. after 1000 -> error(timeout)
  719. end
  720. end,
  721. process_flag(trap_exit, Trap)
  722. end).
  723. character_type(Config) ->
  724. Alpha = unicode:characters_to_binary([16#03B1]),
  725. Ka = unicode:characters_to_binary([16#304B]),
  726. One = unicode:characters_to_binary([16#10D360]),
  727. check_type(Config, bpchar, "'A'", $A, [1, $1, 16#7F, Alpha, Ka, One], "c_char"),
  728. check_type(Config, text, "'hi'", <<"hi">>, [<<"">>, <<"hi">>]),
  729. check_type(Config, varchar, "'hi'", <<"hi">>, [<<"">>, <<"hi">>]),
  730. epgsql_ct:with_connection(
  731. Config,
  732. fun(C) ->
  733. Module = ?config(module, Config),
  734. %% IOlists
  735. ?assertMatch({ok, _, [{<<1087/utf8, 1088/utf8, 1080/utf8,
  736. 1074/utf8, 1077/utf8, 1090/utf8>>}]},
  737. Module:equery(C, "SELECT $1::text", [[1087,1088,1080,1074,1077,1090]])),
  738. %% Deprecated casts
  739. ?assertMatch({ok, _, [{<<"my_atom">>}]},
  740. Module:equery(C, "SELECT $1::varchar", [my_atom])),
  741. ?assertMatch({ok, _, [{<<"12345">>}]},
  742. Module:equery(C, "SELECT $1::varchar", [12345])),
  743. FloatBin = erlang:float_to_binary(1.2345),
  744. ?assertMatch({ok, _, [{FloatBin}]},
  745. Module:equery(C, "SELECT $1::varchar", [1.2345])),
  746. %% String bpchar
  747. ?assertMatch({ok, _, [{<<"hello world">>}]},
  748. Module:equery(C, "SELECT $1::bpchar", ["hello world"]))
  749. end).
  750. uuid_type(Config) ->
  751. check_type(Config, uuid,
  752. io_lib:format("'~s'", [uuid_to_bin_string(?UUID1)]),
  753. uuid_to_bin_string(?UUID1), []).
  754. point_type(Config) ->
  755. check_type(Config, point, "'(23.15, 100)'", {23.15, 100.0}, []).
  756. geometry_type(Config) ->
  757. check_type(Config, geometry, "'COMPOUNDCURVE(CIRCULARSTRING(0 0,1 1,1 0),(1 0,0 1))'",
  758. {compound_curve,'2d', [
  759. {circular_string,'2d', [
  760. {point,'2d',0.0,0.0,undefined,undefined},
  761. {point,'2d',1.0,1.0,undefined,undefined},
  762. {point,'2d',1.0,0.0,undefined,undefined}
  763. ]},
  764. {line_string,'2d', [
  765. {point,'2d',1.0,0.0,undefined,undefined},
  766. {point,'2d',0.0,1.0,undefined,undefined}
  767. ]}
  768. ]}, []).
  769. uuid_select(Config) ->
  770. Module = ?config(module, Config),
  771. epgsql_ct:with_rollback(Config, fun(C) ->
  772. U1 = uuid_to_bin_string(?UUID1),
  773. U2 = uuid_to_bin_string(?UUID2),
  774. U3 = uuid_to_bin_string(?UUID3),
  775. {ok, 1} =
  776. Module:equery(C, "insert into test_table2 (c_varchar, c_uuid) values ('UUID1', $1)",
  777. [U1]),
  778. {ok, 1} =
  779. Module:equery(C, "insert into test_table2 (c_varchar, c_uuid) values ('UUID2', $1)",
  780. [U2]),
  781. {ok, 1} =
  782. Module:equery(C, "insert into test_table2 (c_varchar, c_uuid) values ('UUID3', $1)",
  783. [U3]),
  784. Res = Module:equery(C, "select c_varchar, c_uuid from test_table2 where c_uuid = any($1)",
  785. [[U1, U2]]),
  786. ?assertMatch(
  787. {ok,[#column{name = <<"c_varchar">>, type = varchar},
  788. #column{name = <<"c_uuid">>, type = uuid}],
  789. [{<<"UUID1">>, U1},
  790. {<<"UUID2">>, U2}]}, Res)
  791. end).
  792. date_time_type(Config) ->
  793. Module = ?config(module, Config),
  794. epgsql_ct:with_connection(Config, fun(C) ->
  795. case Module:get_parameter(C, "integer_datetimes") of
  796. {ok, <<"on">>} -> MaxTsDate = 294276;
  797. {ok, <<"off">>} -> MaxTsDate = 5874897
  798. end,
  799. check_type(Config, date, "'2008-01-02'", {2008,1,2}, [{-4712,1,1}, {5874897,1,1}]),
  800. check_type(Config, time, "'00:01:02'", {0,1,2.0}, [{0,0,0.0}, {24,0,0.0}]),
  801. check_type(Config, timetz, "'00:01:02-01'", {{0,1,2.0},1*60*60},
  802. [{{0,0,0.0},0}, {{24,0,0.0},-13*60*60}]),
  803. check_type(Config, timestamp, "'2008-01-02 03:04:05'", {{2008,1,2},{3,4,5.0}},
  804. [{{-4712,1,1},{0,0,0.0}}, {{MaxTsDate,12,31}, {23,59,59.0}}, {1322,334285,440966}]),
  805. check_type(Config, timestamptz, "'2011-01-02 03:04:05+3'", {{2011, 1, 2}, {0, 4, 5.0}}, [{1324,875970,286983}]),
  806. check_type(Config, interval, "'1 hour 2 minutes 3.1 seconds'", {{1,2,3.1},0,0},
  807. [{{0,0,0.0},0,-178000000 * 12}, {{0,0,0.0},0,178000000 * 12}])
  808. end).
  809. json_type(Config) ->
  810. Module = ?config(module, Config),
  811. epgsql_ct:with_connection(Config, fun(C) ->
  812. check_type(Config, json, "'{}'", <<"{}">>,
  813. [<<"{}">>, <<"[]">>, <<"1">>, <<"1.0">>, <<"true">>, <<"\"string\"">>, <<"{\"key\": []}">>],
  814. get_type_col(json), C),
  815. check_type(Config, jsonb, "'{}'", <<"{}">>,
  816. [<<"{}">>, <<"[]">>, <<"1">>, <<"1.0">>, <<"true">>, <<"\"string\"">>, <<"{\"key\": []}">>],
  817. get_type_col(jsonb), C),
  818. epgsql:update_type_cache(C, [{epgsql_codec_json, epgsql_fake_json_mod}]),
  819. RowId = "json_type_custom_mod_" ++ atom_to_list(Module),
  820. {ok, 1} = Module:equery(C, "insert into test_table2 (c_varchar, c_json, c_jsonb) values ($1, $2, $3)", [RowId, {"{}"}, {"{}"}]),
  821. Res = Module:equery(C, "select c_json, c_jsonb from test_table2 where c_varchar = $1", [RowId]),
  822. ?assertMatch(
  823. {ok, [#column{name = <<"c_json">>, type = json}, #column{name = <<"c_jsonb">>, type = jsonb}],
  824. [{{<<"{}">>}, {<<"{}">>}}]},
  825. Res
  826. )
  827. end).
  828. misc_type(Config) ->
  829. check_type(Config, bool, "true", true, [true, false]),
  830. check_type(Config, bytea, "E'\001\002'", <<1,2>>, [<<>>, <<0,128,255>>]).
  831. hstore_type(Config) ->
  832. Module = ?config(module, Config),
  833. Values = [
  834. {[]},
  835. {[{null, null}]},
  836. {[{null, undefined}]},
  837. {[{1, null}]},
  838. {[{1.0, null}]},
  839. {[{1, undefined}]},
  840. {[{1.0, undefined}]},
  841. {[{<<"a">>, <<"c">>}, {<<"c">>, <<"d">>}]},
  842. {[{<<"a">>, <<"c">>}, {<<"c">>, null}]},
  843. {[{<<"a">>, <<"c">>}, {<<"c">>, undefined}]}
  844. ],
  845. check_type(Config, hstore, "''", {[]}, []),
  846. check_type(Config, hstore,
  847. "'a => 1, b => 2.0, c => null'",
  848. {[{<<"a">>, <<"1">>}, {<<"b">>, <<"2.0">>}, {<<"c">>, null}]}, Values),
  849. epgsql_ct:with_connection(
  850. Config,
  851. fun(C) ->
  852. %% Maps as input
  853. [begin
  854. {ok, _, [{Res}]} = Module:equery(C, "select $1::hstore", [maps:from_list(KV)]),
  855. ?assert(compare(hstore, Res, Jiffy))
  856. end || {KV} = Jiffy <- Values],
  857. %% Maps as output
  858. {ok, [hstore]} = epgsql:update_type_cache(
  859. C, [{epgsql_codec_hstore, #{return => map}}]),
  860. [begin
  861. {ok, _, [{Res}]} = Module:equery(C, "select $1::hstore", [maps:from_list(KV)]),
  862. HstoreMap = maps:from_list([{format_hstore_key(K), format_hstore_value(V)} || {K, V} <- KV]),
  863. ?assertEqual(HstoreMap, Res)
  864. end || {KV} <- Values],
  865. %% Proplist as output
  866. {ok, [hstore]} = epgsql:update_type_cache(
  867. C, [{epgsql_codec_hstore, #{return => proplist}}]),
  868. [begin
  869. {ok, _, [{Res}]} = Module:equery(C, "select $1::hstore", [Jiffy]),
  870. HstoreProplist = [{format_hstore_key(K), format_hstore_value(V)} || {K, V} <- KV],
  871. ?assertEqual(lists:sort(HstoreProplist), lists:sort(Res))
  872. end || {KV} = Jiffy <- Values],
  873. %% Custom nulls
  874. Nulls = [nil, 'NULL', aaaa],
  875. {ok, [hstore]} = epgsql:update_type_cache(
  876. C, [{epgsql_codec_hstore, #{return => map,
  877. nulls => Nulls}}]),
  878. K = <<"k">>,
  879. [begin
  880. {ok, _, [{Res}]} = Module:equery(C, "select $1::hstore", [#{K => V}]),
  881. ?assertEqual(#{K => nil}, Res)
  882. end || V <- Nulls]
  883. end).
  884. net_type(Config) ->
  885. 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}]),
  886. check_type(Config, inet, "'127.0.0.1'", {127,0,0,1}, [{127,0,0,1}, {0,0,0,0,0,0,0,1}]),
  887. %% macaddr8 available only on PG>=10
  888. check_type(Config, macaddr,
  889. "'FF:FF:FF:FF:FF:FF'", {255, 255, 255, 255, 255, 255},
  890. [{255, 255, 255, 255, 255, 255},
  891. {6, 0, 0, 0, 0, 0}]).
  892. array_type(Config) ->
  893. Module = ?config(module, Config),
  894. epgsql_ct:with_connection(Config, fun(C) ->
  895. {ok, _, [{[1, 2]}]} = Module:equery(C, "select ($1::int[])[1:2]", [[1, 2, 3]]),
  896. {ok, _, [{[{1, <<"one">>}, {2, <<"two">>}]}]} =
  897. Module:equery(C, "select Array(select (id, value) from test_table1)", []),
  898. {ok, _, [{ [[1], [null], [3], [null]] }]} =
  899. Module:equery(C, "select $1::int2[]", [ [[1], [null], [3], [undefined]] ]),
  900. Select = fun(Type, AIn) ->
  901. Query = "select $1::" ++ atom_to_list(Type) ++ "[]",
  902. {ok, _Cols, [{AOut}]} = Module:equery(C, Query, [AIn]),
  903. case lists:all(fun({VIn, VOut}) ->
  904. compare(Type, VIn, VOut)
  905. end, lists:zip(AIn, AOut)) of
  906. true -> ok;
  907. false -> ?assertEqual(AIn, AOut)
  908. end
  909. end,
  910. Select(int2, []),
  911. Select(int2, [1, 2, 3, 4]),
  912. Select(int2, [[1], [2], [3], [4]]),
  913. Select(int2, [[[[[[1, 2]]]]]]),
  914. Select(int2, [1, null, 3, undefined]),
  915. Select(int2, [[1], [null], [3], [null]]),
  916. Select(bool, [true]),
  917. Select(char, [$a, $b, $c]),
  918. Select(int4, [[1, 2]]),
  919. Select(int8, [[[[1, 2]], [[3, 4]]]]),
  920. Select(text, [<<"one">>, <<"two>">>]),
  921. Select(varchar, [<<"one">>, <<"two>">>]),
  922. Select(float4, [0.0, 1.0, 0.123]),
  923. Select(float8, [0.0, 1.0, 0.123]),
  924. Select(date, [{2008,1,2}, {2008,1,3}]),
  925. Select(time, [{0,1,2.0}, {0,1,3.0}]),
  926. Select(timetz, [{{0,1,2.0},1*60*60}, {{0,1,3.0},1*60*60}]),
  927. Select(timestamp, [{{2008,1,2},{3,4,5.0}}, {{2008,1,2},{3,4,6.0}}]),
  928. Select(timestamptz, [{{2008,1,2},{3,4,5.0}}, {{2008,1,2},{3,4,6.0}}]),
  929. Select(interval, [{{1,2,3.1},0,0}, {{1,2,3.2},0,0}]),
  930. Select(hstore, [{[{null, null}, {a, 1}, {1, 2}, {b, undefined}]}]),
  931. Select(hstore, [[{[{null, null}, {a, 1}, {1, 2}, {b, undefined}]}, {[]}], [{[{a, 1}]}, {[{null, 2}]}]]),
  932. Select(cidr, [{{127,0,0,1}, 32}, {{0,0,0,0,0,0,0,1}, 128}]),
  933. Select(inet, [{127,0,0,1}, {0,0,0,0,0,0,0,1}]),
  934. Select(json, [<<"{}">>, <<"[]">>, <<"1">>, <<"1.0">>, <<"true">>, <<"\"string\"">>, <<"{\"key\": []}">>]),
  935. Select(jsonb, [<<"{}">>, <<"[]">>, <<"1">>, <<"1.0">>, <<"true">>, <<"\"string\"">>, <<"{\"key\": []}">>])
  936. end).
  937. record_type(Config) ->
  938. Module = ?config(module, Config),
  939. epgsql_ct:with_connection(Config, fun(C) ->
  940. Select = fun(Sql, Expected) ->
  941. {ok, _Columns, [Row]} = Module:equery(C, Sql, []),
  942. ?assertMatch(Expected, Row)
  943. end,
  944. %% Simple record
  945. Select("select (1,2)", {{1, 2}}),
  946. %% Record inside other record
  947. Select("select (1, (select (2,3)))", {{1, {2, 3}}}),
  948. %% Array inside record
  949. Select("select (1, '{2,3}'::int[])", {{1, [2, 3]}}),
  950. %% Array of records inside record
  951. Select("select (0, ARRAY(select (id, value) from test_table1))", {{0,[{1,<<"one">>},{2,<<"two">>}]}}),
  952. %% Record with NULLs
  953. Select("select (1, NULL::integer, 2)", {{1, null, 2}})
  954. end).
  955. custom_types(Config) ->
  956. Module = ?config(module, Config),
  957. epgsql_ct:with_connection(Config, fun(C) ->
  958. Module:squery(C, "drop table if exists t_foo;"),
  959. Module:squery(C, "drop type if exists my_type;"),
  960. {ok, [], []} = Module:squery(C, "create type my_type as enum('foo', 'bar');"),
  961. {ok, [my_type]} = epgsql:update_type_cache(C, [{epgsql_codec_test_enum, [foo, bar]}]),
  962. {ok, [], []} = Module:squery(C, "create table t_foo (col my_type);"),
  963. {ok, S} = Module:parse(C, "insert_foo", "insert into t_foo values ($1)", [my_type]),
  964. ok = Module:bind(C, S, [bar]),
  965. {ok, 1} = Module:execute(C, S),
  966. ?assertMatch({ok, _, [{bar}]}, Module:equery(C, "SELECT col FROM t_foo"))
  967. end).
  968. custom_null(Config) ->
  969. Module = ?config(module, Config),
  970. epgsql_ct:with_connection(Config, fun(C) ->
  971. Test3 = fun(Type, In, Out) ->
  972. Q = ["SELECT $1::", Type],
  973. {ok, _, [{Res}]} = Module:equery(C, Q, [In]),
  974. ?assertEqual(Out, Res)
  975. end,
  976. Test = fun(Type, In) ->
  977. Test3(Type, In, In)
  978. end,
  979. Test("int2", nil),
  980. Test3("int2", 'NULL', nil),
  981. Test("text", nil),
  982. Test3("text", 'NULL', nil),
  983. Test3("text", null, <<"null">>),
  984. Test("int2[]", [nil, 1, nil, 2]),
  985. Test3("text[]", [null, <<"ok">>], [<<"null">>, <<"ok">>]),
  986. Test3("int2[]", ['NULL', 1, nil, 2], [nil, 1, nil, 2]),
  987. Test("int2[]", [[nil], [1], [nil], [2]]),
  988. Test3("int2[]", [['NULL'], [1], [nil], [2]], [[nil], [1], [nil], [2]]),
  989. ?assertMatch(
  990. {ok, _, [{ {1, nil, {2, nil, 3}} }]},
  991. Module:equery(C, "SELECT (1, NULL, (2, NULL, 3))", []))
  992. end,
  993. [{nulls, [nil, 'NULL']}]).
  994. text_format(Config) ->
  995. Module = ?config(module, Config),
  996. epgsql_ct:with_connection(Config, fun(C) ->
  997. Select = fun(Type, V) ->
  998. V2 = list_to_binary(V),
  999. Query = "select $1::" ++ Type,
  1000. ?assertMatch({ok, _Cols, [{V2}]}, Module:equery(C, Query, [V])),
  1001. ?assertMatch({ok, _Cols, [{V2}]}, Module:equery(C, Query, [V2]))
  1002. end,
  1003. Select("numeric", "123456")
  1004. end).
  1005. query_timeout(Config) ->
  1006. Module = ?config(module, Config),
  1007. epgsql_ct:with_connection(Config, fun(C) ->
  1008. {ok, _, _} = Module:squery(C, "SET statement_timeout = 500"),
  1009. ?assertMatch(?TIMEOUT_ERROR, Module:squery(C, "SELECT pg_sleep(1)")),
  1010. ?assertMatch(?TIMEOUT_ERROR, Module:equery(C, "SELECT pg_sleep(2)")),
  1011. {ok, _Cols, [{1}]} = Module:equery(C, "SELECT 1")
  1012. end, []).
  1013. execute_timeout(Config) ->
  1014. Module = ?config(module, Config),
  1015. epgsql_ct:with_connection(Config, fun(C) ->
  1016. {ok, _, _} = Module:squery(C, "SET statement_timeout = 500"),
  1017. {ok, S} = Module:parse(C, "select pg_sleep($1)"),
  1018. ok = Module:bind(C, S, [2]),
  1019. ?assertMatch(?TIMEOUT_ERROR, Module:execute(C, S, 0)),
  1020. ok = Module:sync(C),
  1021. ok = Module:bind(C, S, [0]),
  1022. {ok, [{<<>>}]} = Module:execute(C, S, 0),
  1023. ok = Module:close(C, S),
  1024. ok = Module:sync(C)
  1025. end, []).
  1026. connection_closed(Config) ->
  1027. {Host, Port} = epgsql_ct:connection_data(Config),
  1028. Module = ?config(module, Config),
  1029. P = self(),
  1030. spawn_link(fun() ->
  1031. process_flag(trap_exit, true),
  1032. {ok, C} = Module:connect(Host, "epgsql_test",[
  1033. {port, Port},
  1034. {database, "epgsql_test_db1"}
  1035. ]),
  1036. P ! {connected, C},
  1037. receive
  1038. Any -> P ! Any
  1039. end
  1040. end),
  1041. receive
  1042. {connected, C} ->
  1043. timer:sleep(100),
  1044. Module:close(C),
  1045. {'EXIT', C, _} = receive R -> R end
  1046. end,
  1047. epgsql_ct:flush().
  1048. connection_closed_by_server(Config) ->
  1049. Module = ?config(module, Config),
  1050. epgsql_ct:with_connection(Config, fun(C1) ->
  1051. P = self(),
  1052. spawn_link(fun() ->
  1053. process_flag(trap_exit, true),
  1054. epgsql_ct:with_connection(Config, fun(C2) ->
  1055. {ok, _, [{Pid}]} = Module:equery(C2, "select pg_backend_pid()"),
  1056. % emulate of disconnection
  1057. {ok, _, [{true}]} = Module:equery(C1,
  1058. "select pg_terminate_backend($1)", [Pid]),
  1059. receive
  1060. {'EXIT', C2, {shutdown, #error{code = <<"57P01">>}}} ->
  1061. P ! ok;
  1062. Other ->
  1063. ct:pal("Unexpected msg: ~p~n", [Other]),
  1064. P ! error
  1065. end
  1066. end)
  1067. end),
  1068. receive ok -> ok end
  1069. end).
  1070. active_connection_closed(Config) ->
  1071. {Host, Port} = epgsql_ct:connection_data(Config),
  1072. Module = ?config(module, Config),
  1073. P = self(),
  1074. F = fun() ->
  1075. process_flag(trap_exit, true),
  1076. {ok, C} = Module:connect(Host, [
  1077. {database, "epgsql_test_db1"},
  1078. {port, Port}
  1079. ]),
  1080. P ! {connected, C},
  1081. R = Module:squery(C, "select pg_sleep(10)"),
  1082. P ! R
  1083. end,
  1084. spawn_link(F),
  1085. receive
  1086. {connected, C} ->
  1087. timer:sleep(100),
  1088. Module:close(C),
  1089. {error, closed} = receive R -> R end
  1090. end,
  1091. epgsql_ct:flush().
  1092. warning_notice(Config) ->
  1093. Module = ?config(module, Config),
  1094. epgsql_ct:with_connection(Config, fun(C) ->
  1095. Q = "create function pg_temp.raise() returns void as $$
  1096. begin
  1097. raise warning 'oops';
  1098. end;
  1099. $$ language plpgsql;
  1100. select pg_temp.raise()",
  1101. [{ok, _, _}, _] = Module:squery(C, Q),
  1102. receive
  1103. {epgsql, C, {notice, #error{message = <<"oops">>, extra = Extra}}} ->
  1104. ?assertMatch([{file, _},{line, _},{routine, _} | _], Extra),
  1105. ok
  1106. after
  1107. 100 -> erlang:error(didnt_receive_notice)
  1108. end
  1109. end, [{async, self()}]).
  1110. listen_notify(Config) ->
  1111. Module = ?config(module, Config),
  1112. epgsql_ct:with_connection(Config, fun(C) ->
  1113. {ok, [], []} = Module:squery(C, "listen epgsql_test"),
  1114. {ok, _, [{Pid}]} = Module:equery(C, "select pg_backend_pid()"),
  1115. {ok, [], []} = Module:squery(C, "notify epgsql_test"),
  1116. receive
  1117. {epgsql, C, {notification, <<"epgsql_test">>, Pid, <<>>}} -> ok
  1118. after
  1119. 100 -> erlang:error(didnt_receive_notification)
  1120. end
  1121. end, [{async, self()}]).
  1122. listen_notify_payload(Config) ->
  1123. Module = ?config(module, Config),
  1124. epgsql_ct:with_min_version(Config, [9, 0], fun(C) ->
  1125. {ok, [], []} = Module:squery(C, "listen epgsql_test"),
  1126. {ok, _, [{Pid}]} = Module:equery(C, "select pg_backend_pid()"),
  1127. {ok, [], []} = Module:squery(C, "notify epgsql_test, 'test!'"),
  1128. receive
  1129. {epgsql, C, {notification, <<"epgsql_test">>, Pid, <<"test!">>}} -> ok
  1130. after
  1131. 100 -> erlang:error(didnt_receive_notification)
  1132. end
  1133. end, [{async, self()}]).
  1134. set_notice_receiver(Config) ->
  1135. Module = ?config(module, Config),
  1136. epgsql_ct:with_min_version(Config, [9, 0], fun(C) ->
  1137. {ok, [], []} = Module:squery(C, "listen epgsql_test"),
  1138. {ok, _, [{Pid}]} = Module:equery(C, "select pg_backend_pid()"),
  1139. EnsureNoNotification = fun(Payload) ->
  1140. {ok, [], []} = Module:squery(C, ["notify epgsql_test, '", Payload, "'"]),
  1141. receive
  1142. {epgsql, _, _} -> erlang:error(got_unexpected_notification)
  1143. after
  1144. 10 -> ok
  1145. end
  1146. end,
  1147. EnsureNotification = fun(Payload) ->
  1148. {ok, [], []} = Module:squery(C, ["notify epgsql_test, '", Payload, "'"]),
  1149. receive
  1150. {epgsql, C, {notification, <<"epgsql_test">>, Pid, Payload}} -> ok
  1151. after
  1152. 100 -> erlang:error(didnt_receive_notification)
  1153. end
  1154. end,
  1155. Self = self(),
  1156. EnsureNoNotification(<<"test1">>),
  1157. % Set pid()
  1158. {ok, undefined} = Module:set_notice_receiver(C, Self),
  1159. EnsureNotification(<<"test2">>),
  1160. %% test PL/PgSQL NOTICE
  1161. {ok, [], []} = Module:squery(C, ["DO $$ BEGIN RAISE WARNING 'test notice'; END $$;"]),
  1162. receive
  1163. {epgsql, C, {notice, #error{severity = warning,
  1164. code = <<"01000">>,
  1165. message = <<"test notice">>,
  1166. extra = _}}} -> ok
  1167. after
  1168. 100 -> erlang:error(didnt_receive_notice)
  1169. end,
  1170. % set registered pid
  1171. Receiver = pg_notification_receiver,
  1172. register(Receiver, Self),
  1173. {ok, Self} = Module:set_notice_receiver(C, Receiver),
  1174. EnsureNotification(<<"test3">>),
  1175. % make registered name invalid
  1176. unregister(Receiver),
  1177. EnsureNoNotification(<<"test4">>),
  1178. % disable
  1179. {ok, Receiver} = Module:set_notice_receiver(C, undefined),
  1180. EnsureNoNotification(<<"test5">>)
  1181. end, []).
  1182. get_cmd_status(Config) ->
  1183. Module = ?config(module, Config),
  1184. epgsql_ct:with_connection(Config, fun(C) ->
  1185. {ok, [], []} = Module:squery(C, "BEGIN"),
  1186. ?assertEqual({ok, 'begin'}, Module:get_cmd_status(C)),
  1187. {ok, [], []} = epgsql:equery(C, "CREATE TEMPORARY TABLE cmd_status_t(col INTEGER)"),
  1188. ?assertEqual({ok, 'create'}, Module:get_cmd_status(C)),
  1189. %% Some commands have number of affected rows in status
  1190. {ok, N} = Module:squery(C, "INSERT INTO cmd_status_t (col) VALUES (1), (2)"),
  1191. ?assertEqual({ok, {'insert', N}}, Module:get_cmd_status(C)),
  1192. {ok, 1} = Module:squery(C, "UPDATE cmd_status_t SET col=3 WHERE col=1"),
  1193. ?assertEqual({ok, {'update', 1}}, Module:get_cmd_status(C)),
  1194. %% Failed queries have no status
  1195. {error, _} = Module:squery(C, "DELETE FROM cmd_status_t WHERE not_col=2"),
  1196. ?assertEqual({ok, undefined}, Module:get_cmd_status(C)),
  1197. %% if COMMIT failed, status will be 'rollback'
  1198. {ok, [], []} = Module:squery(C, "COMMIT"),
  1199. ?assertEqual({ok, 'rollback'}, Module:get_cmd_status(C)),
  1200. %% Only last command's status returned
  1201. [_, _, _] = Module:squery(C, "BEGIN; SELECT 1; COMMIT"),
  1202. ?assertEqual({ok, 'commit'}, Module:get_cmd_status(C))
  1203. end).
  1204. range_type(Config) ->
  1205. epgsql_ct:with_min_version(Config, [9, 2], fun(_C) ->
  1206. check_type(Config, int4range, "int4range(10, 20)", {10, 20}, [
  1207. {1, 58}, {-1, 12}, {-985521, 5412687}, {minus_infinity, 0},
  1208. {984655, plus_infinity}, {minus_infinity, plus_infinity}
  1209. ]),
  1210. check_type(Config, int4range, "int4range(10, 10)", empty, [])
  1211. end, []).
  1212. range8_type(Config) ->
  1213. epgsql_ct:with_min_version(Config, [9, 2], fun(_C) ->
  1214. check_type(Config, int8range, "int8range(10, 20)", {10, 20}, [
  1215. {1, 58}, {-1, 12}, {-9223372036854775808, 5412687},
  1216. {minus_infinity, 9223372036854775807},
  1217. {984655, plus_infinity}, {minus_infinity, plus_infinity}
  1218. ]),
  1219. check_type(Config, int8range, "int8range(10, 10)", empty, [])
  1220. end, []).
  1221. date_time_range_type(Config) ->
  1222. epgsql_ct:with_min_version(Config, [9, 2], fun(_C) ->
  1223. check_type(Config, tsrange, "tsrange('2008-01-02 03:04:05', '2008-02-02 03:04:05')", {{{2008,1,2},{3,4,5.0}}, {{2008,2,2},{3,4,5.0}}}, []),
  1224. check_type(Config, tsrange, "tsrange('2008-01-02 03:04:05', '2008-02-02 03:04:05', '[]')", {{{2008,1,2},{3,4,5.0}}, {{2008,2,2},{3,4,5.0}}}, []),
  1225. check_type(Config, tsrange, "tsrange('2008-01-02 03:04:05', '2008-02-02 03:04:05', '()')", {{{2008,1,2},{3,4,5.0}}, {{2008,2,2},{3,4,5.0}}}, []),
  1226. check_type(Config, tsrange, "tsrange('2008-01-02 03:04:05', '2008-02-02 03:04:05', '[)')", {{{2008,1,2},{3,4,5.0}}, {{2008,2,2},{3,4,5.0}}}, []),
  1227. check_type(Config, tsrange, "tsrange('2008-01-02 03:04:05', '2008-02-02 03:04:05', '(]')", {{{2008,1,2},{3,4,5.0}}, {{2008,2,2},{3,4,5.0}}}, []),
  1228. check_type(Config, tsrange, "tsrange('2008-01-02 03:04:05', '2008-01-02 03:04:05')", empty, []),
  1229. check_type(Config, daterange, "daterange('2008-01-02', '2008-02-02')", {{2008,1,2}, {2008, 2, 2}}, [{{-4712,1,1}, {5874897,1,1}}]),
  1230. check_type(Config, tstzrange, "tstzrange('2011-01-02 03:04:05+3', '2011-01-02 04:04:05+3')", {{{2011, 1, 2}, {0, 4, 5.0}}, {{2011, 1, 2}, {1, 4, 5.0}}}, [{{{2011, 1, 2}, {0, 4, 5.0}}, {{2011, 1, 2}, {1, 4, 5.0}}}]),
  1231. check_type(Config, tstzrange, "tstzrange('2008-01-02 03:04:05', null)", {{{2008,1,2},{3,4,5.0}}, plus_infinity}, []),
  1232. check_type(Config, tstzrange, "tstzrange('2008-01-02 03:04:05', null, '[]')", {{{2008,1,2},{3,4,5.0}}, plus_infinity}, []),
  1233. check_type(Config, tstzrange, "tstzrange('2008-01-02 03:04:05', null, '()')", {{{2008,1,2},{3,4,5.0}}, plus_infinity}, []),
  1234. check_type(Config, tstzrange, "tstzrange(null, '2008-01-02 03:04:05')", {minus_infinity, {{2008,1,2},{3,4,5.0}}}, []),
  1235. check_type(Config, tstzrange, "tstzrange(null, '2008-01-02 03:04:05', '[]')", {minus_infinity, {{2008,1,2},{3,4,5.0}}}, []),
  1236. check_type(Config, tstzrange, "tstzrange(null, '2008-01-02 03:04:05', '()')", {minus_infinity, {{2008,1,2},{3,4,5.0}}}, [])
  1237. end, []).
  1238. with_transaction(Config) ->
  1239. Module = ?config(module, Config),
  1240. epgsql_ct:with_connection(
  1241. Config,
  1242. fun(C) ->
  1243. %% Success case
  1244. ?assertEqual(
  1245. success, Module:with_transaction(C, fun(_) -> success end)),
  1246. ?assertEqual(
  1247. success, Module:with_transaction(C, fun(_) -> success end,
  1248. [{ensure_committed, true}])),
  1249. %% begin_opts
  1250. ?assertMatch(
  1251. [{ok, _, [{<<"serializable">>}]},
  1252. {ok, _, [{<<"on">>}]}],
  1253. Module:with_transaction(
  1254. C, fun(C1) ->
  1255. Module:squery(C1, ("SHOW transaction_isolation; "
  1256. "SHOW transaction_read_only"))
  1257. end,
  1258. [{begin_opts, "READ ONLY ISOLATION LEVEL SERIALIZABLE"}])),
  1259. %% ensure_committed failure
  1260. ?assertError(
  1261. {ensure_committed_failed, rollback},
  1262. Module:with_transaction(
  1263. C, fun(C1) ->
  1264. {error, _} = Module:squery(C1, "SELECT col FROM _nowhere_"),
  1265. ok
  1266. end,
  1267. [{ensure_committed, true}])),
  1268. %% reraise
  1269. ?assertEqual(
  1270. {rollback, my_err},
  1271. Module:with_transaction(
  1272. C, fun(_) -> error(my_err) end,
  1273. [{reraise, false}])),
  1274. ?assertError(
  1275. my_err,
  1276. Module:with_transaction(
  1277. C, fun(_) -> error(my_err) end, []))
  1278. end, []).
  1279. %% =============================================================================
  1280. %% Internal functions
  1281. %% ============================================================================
  1282. get_type_col(Type) ->
  1283. "c_" ++ atom_to_list(Type).
  1284. check_type(Config, Type, In, Out, Values) ->
  1285. Column = get_type_col(Type),
  1286. check_type(Config, Type, In, Out, Values, Column).
  1287. check_type(Config, Type, In, Out, Values, Column) ->
  1288. epgsql_ct:with_connection(Config, fun(C) ->
  1289. check_type(Config, Type, In, Out, Values, Column, C)
  1290. end).
  1291. check_type(Config, Type, In, Out, Values, Column, C) ->
  1292. Module = ?config(module, Config),
  1293. Select = io_lib:format("select ~s::~w", [In, Type]),
  1294. Res = Module:equery(C, Select),
  1295. ?assertMatch({ok, [#column{type = Type}], [{Out}]}, Res),
  1296. Sql = io_lib:format("insert into test_table2 (~s) values ($1) returning ~s", [Column, Column]),
  1297. {ok, #statement{columns = [#column{type = Type}]} = S} = Module:parse(C, Sql),
  1298. Insert = fun(V) ->
  1299. ok = Module:bind(C, S, [V]),
  1300. {ok, 1, [{V2}]} = Module:execute(C, S),
  1301. case compare(Type, V, V2) of
  1302. true -> ok;
  1303. false -> error({write_read_compare_failed, io_lib:format("~p =/= ~p", [V, V2])})
  1304. end,
  1305. ok = Module:sync(C)
  1306. end,
  1307. lists:foreach(Insert, [null, undefined | Values]).
  1308. compare(_Type, null, null) -> true;
  1309. compare(_Type, undefined, null) -> true;
  1310. compare(float4, V1, V2) when is_float(V1) -> abs(V2 - V1) < 0.000001;
  1311. compare(float8, V1, V2) when is_float(V1) -> abs(V2 - V1) < 0.000000000000001;
  1312. compare(hstore, {V1}, V2) -> compare(hstore, V1, V2);
  1313. compare(hstore, V1, {V2}) -> compare(hstore, V1, V2);
  1314. compare(hstore, V1, V2) ->
  1315. orddict:from_list(format_hstore(V1)) =:= orddict:from_list(format_hstore(V2));
  1316. compare(Type, V1 = {_, _, MS}, {D2, {H2, M2, S2}}) when Type == timestamp;
  1317. Type == timestamptz ->
  1318. {D1, {H1, M1, S1}} = calendar:now_to_universal_time(V1),
  1319. ({D1, H1, M1} =:= {D2, H2, M2}) and (abs(S1 + MS/1000000 - S2) < 0.000000000000001);
  1320. compare(_Type, V1, V2) -> V1 =:= V2.
  1321. format_hstore({Hstore}) -> Hstore;
  1322. format_hstore(Hstore) ->
  1323. [{format_hstore_key(Key), format_hstore_value(Value)} || {Key, Value} <- Hstore].
  1324. format_hstore_key(Key) -> format_hstore_string(Key).
  1325. format_hstore_value(null) -> null;
  1326. format_hstore_value(undefined) -> null;
  1327. format_hstore_value(Value) -> format_hstore_string(Value).
  1328. format_hstore_string(Num) when is_number(Num) -> iolist_to_binary(io_lib:format("~w", [Num]));
  1329. format_hstore_string(Str) -> iolist_to_binary(io_lib:format("~s", [Str])).