epgsql_SUITE.erl 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443
  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, 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. character_type(Config) ->
  706. Alpha = unicode:characters_to_binary([16#03B1]),
  707. Ka = unicode:characters_to_binary([16#304B]),
  708. One = unicode:characters_to_binary([16#10D360]),
  709. check_type(Config, bpchar, "'A'", $A, [1, $1, 16#7F, Alpha, Ka, One], "c_char"),
  710. check_type(Config, text, "'hi'", <<"hi">>, [<<"">>, <<"hi">>]),
  711. check_type(Config, varchar, "'hi'", <<"hi">>, [<<"">>, <<"hi">>]),
  712. epgsql_ct:with_connection(
  713. Config,
  714. fun(C) ->
  715. Module = ?config(module, Config),
  716. %% IOlists
  717. ?assertMatch({ok, _, [{<<1087/utf8, 1088/utf8, 1080/utf8,
  718. 1074/utf8, 1077/utf8, 1090/utf8>>}]},
  719. Module:equery(C, "SELECT $1::text", [[1087,1088,1080,1074,1077,1090]])),
  720. %% Deprecated casts
  721. ?assertMatch({ok, _, [{<<"my_atom">>}]},
  722. Module:equery(C, "SELECT $1::varchar", [my_atom])),
  723. ?assertMatch({ok, _, [{<<"12345">>}]},
  724. Module:equery(C, "SELECT $1::varchar", [12345])),
  725. FloatBin = erlang:float_to_binary(1.2345),
  726. ?assertMatch({ok, _, [{FloatBin}]},
  727. Module:equery(C, "SELECT $1::varchar", [1.2345])),
  728. %% String bpchar
  729. ?assertMatch({ok, _, [{<<"hello world">>}]},
  730. Module:equery(C, "SELECT $1::bpchar", ["hello world"]))
  731. end).
  732. uuid_type(Config) ->
  733. check_type(Config, uuid,
  734. io_lib:format("'~s'", [uuid_to_bin_string(?UUID1)]),
  735. uuid_to_bin_string(?UUID1), []).
  736. point_type(Config) ->
  737. check_type(Config, point, "'(23.15, 100)'", {23.15, 100.0}, []).
  738. geometry_type(Config) ->
  739. check_type(Config, geometry, "'COMPOUNDCURVE(CIRCULARSTRING(0 0,1 1,1 0),(1 0,0 1))'",
  740. {compound_curve,'2d', [
  741. {circular_string,'2d', [
  742. {point,'2d',0.0,0.0,undefined,undefined},
  743. {point,'2d',1.0,1.0,undefined,undefined},
  744. {point,'2d',1.0,0.0,undefined,undefined}
  745. ]},
  746. {line_string,'2d', [
  747. {point,'2d',1.0,0.0,undefined,undefined},
  748. {point,'2d',0.0,1.0,undefined,undefined}
  749. ]}
  750. ]}, []).
  751. uuid_select(Config) ->
  752. Module = ?config(module, Config),
  753. epgsql_ct:with_rollback(Config, fun(C) ->
  754. U1 = uuid_to_bin_string(?UUID1),
  755. U2 = uuid_to_bin_string(?UUID2),
  756. U3 = uuid_to_bin_string(?UUID3),
  757. {ok, 1} =
  758. Module:equery(C, "insert into test_table2 (c_varchar, c_uuid) values ('UUID1', $1)",
  759. [U1]),
  760. {ok, 1} =
  761. Module:equery(C, "insert into test_table2 (c_varchar, c_uuid) values ('UUID2', $1)",
  762. [U2]),
  763. {ok, 1} =
  764. Module:equery(C, "insert into test_table2 (c_varchar, c_uuid) values ('UUID3', $1)",
  765. [U3]),
  766. Res = Module:equery(C, "select c_varchar, c_uuid from test_table2 where c_uuid = any($1)",
  767. [[U1, U2]]),
  768. ?assertMatch(
  769. {ok,[#column{name = <<"c_varchar">>, type = varchar},
  770. #column{name = <<"c_uuid">>, type = uuid}],
  771. [{<<"UUID1">>, U1},
  772. {<<"UUID2">>, U2}]}, Res)
  773. end).
  774. date_time_type(Config) ->
  775. Module = ?config(module, Config),
  776. epgsql_ct:with_connection(Config, fun(C) ->
  777. case Module:get_parameter(C, "integer_datetimes") of
  778. {ok, <<"on">>} -> MaxTsDate = 294276;
  779. {ok, <<"off">>} -> MaxTsDate = 5874897
  780. end,
  781. check_type(Config, date, "'2008-01-02'", {2008,1,2}, [{-4712,1,1}, {5874897,1,1}]),
  782. check_type(Config, time, "'00:01:02'", {0,1,2.0}, [{0,0,0.0}, {24,0,0.0}]),
  783. check_type(Config, timetz, "'00:01:02-01'", {{0,1,2.0},1*60*60},
  784. [{{0,0,0.0},0}, {{24,0,0.0},-13*60*60}]),
  785. check_type(Config, timestamp, "'2008-01-02 03:04:05'", {{2008,1,2},{3,4,5.0}},
  786. [{{-4712,1,1},{0,0,0.0}}, {{MaxTsDate,12,31}, {23,59,59.0}}, {1322,334285,440966}]),
  787. check_type(Config, timestamptz, "'2011-01-02 03:04:05+3'", {{2011, 1, 2}, {0, 4, 5.0}}, [{1324,875970,286983}]),
  788. check_type(Config, interval, "'1 hour 2 minutes 3.1 seconds'", {{1,2,3.1},0,0},
  789. [{{0,0,0.0},0,-178000000 * 12}, {{0,0,0.0},0,178000000 * 12}])
  790. end).
  791. json_type(Config) ->
  792. Module = ?config(module, Config),
  793. epgsql_ct:with_connection(Config, fun(C) ->
  794. check_type(Config, json, "'{}'", <<"{}">>,
  795. [<<"{}">>, <<"[]">>, <<"1">>, <<"1.0">>, <<"true">>, <<"\"string\"">>, <<"{\"key\": []}">>],
  796. get_type_col(json), C),
  797. check_type(Config, jsonb, "'{}'", <<"{}">>,
  798. [<<"{}">>, <<"[]">>, <<"1">>, <<"1.0">>, <<"true">>, <<"\"string\"">>, <<"{\"key\": []}">>],
  799. get_type_col(jsonb), C),
  800. epgsql:update_type_cache(C, [{epgsql_codec_json, epgsql_fake_json_mod}]),
  801. RowId = "json_type_custom_mod_" ++ atom_to_list(Module),
  802. {ok, 1} = Module:equery(C, "insert into test_table2 (c_varchar, c_json, c_jsonb) values ($1, $2, $3)", [RowId, {"{}"}, {"{}"}]),
  803. Res = Module:equery(C, "select c_json, c_jsonb from test_table2 where c_varchar = $1", [RowId]),
  804. ?assertMatch(
  805. {ok, [#column{name = <<"c_json">>, type = json}, #column{name = <<"c_jsonb">>, type = jsonb}],
  806. [{{<<"{}">>}, {<<"{}">>}}]},
  807. Res
  808. )
  809. end).
  810. misc_type(Config) ->
  811. check_type(Config, bool, "true", true, [true, false]),
  812. check_type(Config, bytea, "E'\001\002'", <<1,2>>, [<<>>, <<0,128,255>>]).
  813. hstore_type(Config) ->
  814. Module = ?config(module, Config),
  815. Values = [
  816. {[]},
  817. {[{null, null}]},
  818. {[{null, undefined}]},
  819. {[{1, null}]},
  820. {[{1.0, null}]},
  821. {[{1, undefined}]},
  822. {[{1.0, undefined}]},
  823. {[{<<"a">>, <<"c">>}, {<<"c">>, <<"d">>}]},
  824. {[{<<"a">>, <<"c">>}, {<<"c">>, null}]},
  825. {[{<<"a">>, <<"c">>}, {<<"c">>, undefined}]}
  826. ],
  827. check_type(Config, hstore, "''", {[]}, []),
  828. check_type(Config, hstore,
  829. "'a => 1, b => 2.0, c => null'",
  830. {[{<<"a">>, <<"1">>}, {<<"b">>, <<"2.0">>}, {<<"c">>, null}]}, Values),
  831. epgsql_ct:with_connection(
  832. Config,
  833. fun(C) ->
  834. %% Maps as input
  835. [begin
  836. {ok, _, [{Res}]} = Module:equery(C, "select $1::hstore", [maps:from_list(KV)]),
  837. ?assert(compare(hstore, Res, Jiffy))
  838. end || {KV} = Jiffy <- Values],
  839. %% Maps as output
  840. {ok, [hstore]} = epgsql:update_type_cache(
  841. C, [{epgsql_codec_hstore, #{return => map}}]),
  842. [begin
  843. {ok, _, [{Res}]} = Module:equery(C, "select $1::hstore", [maps:from_list(KV)]),
  844. HstoreMap = maps:from_list([{format_hstore_key(K), format_hstore_value(V)} || {K, V} <- KV]),
  845. ?assertEqual(HstoreMap, Res)
  846. end || {KV} <- Values],
  847. %% Proplist as output
  848. {ok, [hstore]} = epgsql:update_type_cache(
  849. C, [{epgsql_codec_hstore, #{return => proplist}}]),
  850. [begin
  851. {ok, _, [{Res}]} = Module:equery(C, "select $1::hstore", [Jiffy]),
  852. HstoreProplist = [{format_hstore_key(K), format_hstore_value(V)} || {K, V} <- KV],
  853. ?assertEqual(lists:sort(HstoreProplist), lists:sort(Res))
  854. end || {KV} = Jiffy <- Values],
  855. %% Custom nulls
  856. Nulls = [nil, 'NULL', aaaa],
  857. {ok, [hstore]} = epgsql:update_type_cache(
  858. C, [{epgsql_codec_hstore, #{return => map,
  859. nulls => Nulls}}]),
  860. K = <<"k">>,
  861. [begin
  862. {ok, _, [{Res}]} = Module:equery(C, "select $1::hstore", [#{K => V}]),
  863. ?assertEqual(#{K => nil}, Res)
  864. end || V <- Nulls]
  865. end).
  866. net_type(Config) ->
  867. 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}]),
  868. check_type(Config, inet, "'127.0.0.1'", {127,0,0,1}, [{127,0,0,1}, {0,0,0,0,0,0,0,1}]),
  869. %% macaddr8 available only on PG>=10
  870. check_type(Config, macaddr,
  871. "'FF:FF:FF:FF:FF:FF'", {255, 255, 255, 255, 255, 255},
  872. [{255, 255, 255, 255, 255, 255},
  873. {6, 0, 0, 0, 0, 0}]).
  874. array_type(Config) ->
  875. Module = ?config(module, Config),
  876. epgsql_ct:with_connection(Config, fun(C) ->
  877. {ok, _, [{[1, 2]}]} = Module:equery(C, "select ($1::int[])[1:2]", [[1, 2, 3]]),
  878. {ok, _, [{[{1, <<"one">>}, {2, <<"two">>}]}]} =
  879. Module:equery(C, "select Array(select (id, value) from test_table1)", []),
  880. {ok, _, [{ [[1], [null], [3], [null]] }]} =
  881. Module:equery(C, "select $1::int2[]", [ [[1], [null], [3], [undefined]] ]),
  882. Select = fun(Type, AIn) ->
  883. Query = "select $1::" ++ atom_to_list(Type) ++ "[]",
  884. {ok, _Cols, [{AOut}]} = Module:equery(C, Query, [AIn]),
  885. case lists:all(fun({VIn, VOut}) ->
  886. compare(Type, VIn, VOut)
  887. end, lists:zip(AIn, AOut)) of
  888. true -> ok;
  889. false -> ?assertEqual(AIn, AOut)
  890. end
  891. end,
  892. Select(int2, []),
  893. Select(int2, [1, 2, 3, 4]),
  894. Select(int2, [[1], [2], [3], [4]]),
  895. Select(int2, [[[[[[1, 2]]]]]]),
  896. Select(int2, [1, null, 3, undefined]),
  897. Select(int2, [[1], [null], [3], [null]]),
  898. Select(bool, [true]),
  899. Select(char, [$a, $b, $c]),
  900. Select(int4, [[1, 2]]),
  901. Select(int8, [[[[1, 2]], [[3, 4]]]]),
  902. Select(text, [<<"one">>, <<"two>">>]),
  903. Select(varchar, [<<"one">>, <<"two>">>]),
  904. Select(float4, [0.0, 1.0, 0.123]),
  905. Select(float8, [0.0, 1.0, 0.123]),
  906. Select(date, [{2008,1,2}, {2008,1,3}]),
  907. Select(time, [{0,1,2.0}, {0,1,3.0}]),
  908. Select(timetz, [{{0,1,2.0},1*60*60}, {{0,1,3.0},1*60*60}]),
  909. Select(timestamp, [{{2008,1,2},{3,4,5.0}}, {{2008,1,2},{3,4,6.0}}]),
  910. Select(timestamptz, [{{2008,1,2},{3,4,5.0}}, {{2008,1,2},{3,4,6.0}}]),
  911. Select(interval, [{{1,2,3.1},0,0}, {{1,2,3.2},0,0}]),
  912. Select(hstore, [{[{null, null}, {a, 1}, {1, 2}, {b, undefined}]}]),
  913. Select(hstore, [[{[{null, null}, {a, 1}, {1, 2}, {b, undefined}]}, {[]}], [{[{a, 1}]}, {[{null, 2}]}]]),
  914. Select(cidr, [{{127,0,0,1}, 32}, {{0,0,0,0,0,0,0,1}, 128}]),
  915. Select(inet, [{127,0,0,1}, {0,0,0,0,0,0,0,1}]),
  916. Select(json, [<<"{}">>, <<"[]">>, <<"1">>, <<"1.0">>, <<"true">>, <<"\"string\"">>, <<"{\"key\": []}">>]),
  917. Select(jsonb, [<<"{}">>, <<"[]">>, <<"1">>, <<"1.0">>, <<"true">>, <<"\"string\"">>, <<"{\"key\": []}">>])
  918. end).
  919. record_type(Config) ->
  920. Module = ?config(module, Config),
  921. epgsql_ct:with_connection(Config, fun(C) ->
  922. Select = fun(Sql, Expected) ->
  923. {ok, _Columns, [Row]} = Module:equery(C, Sql, []),
  924. ?assertMatch(Expected, Row)
  925. end,
  926. %% Simple record
  927. Select("select (1,2)", {{1, 2}}),
  928. %% Record inside other record
  929. Select("select (1, (select (2,3)))", {{1, {2, 3}}}),
  930. %% Array inside record
  931. Select("select (1, '{2,3}'::int[])", {{1, [2, 3]}}),
  932. %% Array of records inside record
  933. Select("select (0, ARRAY(select (id, value) from test_table1))", {{0,[{1,<<"one">>},{2,<<"two">>}]}}),
  934. %% Record with NULLs
  935. Select("select (1, NULL::integer, 2)", {{1, null, 2}})
  936. end).
  937. custom_types(Config) ->
  938. Module = ?config(module, Config),
  939. epgsql_ct:with_connection(Config, fun(C) ->
  940. Module:squery(C, "drop table if exists t_foo;"),
  941. Module:squery(C, "drop type if exists my_type;"),
  942. {ok, [], []} = Module:squery(C, "create type my_type as enum('foo', 'bar');"),
  943. {ok, [my_type]} = epgsql:update_type_cache(C, [{epgsql_codec_test_enum, [foo, bar]}]),
  944. {ok, [], []} = Module:squery(C, "create table t_foo (col my_type);"),
  945. {ok, S} = Module:parse(C, "insert_foo", "insert into t_foo values ($1)", [my_type]),
  946. ok = Module:bind(C, S, [bar]),
  947. {ok, 1} = Module:execute(C, S),
  948. ?assertMatch({ok, _, [{bar}]}, Module:equery(C, "SELECT col FROM t_foo"))
  949. end).
  950. custom_null(Config) ->
  951. Module = ?config(module, Config),
  952. epgsql_ct:with_connection(Config, fun(C) ->
  953. Test3 = fun(Type, In, Out) ->
  954. Q = ["SELECT $1::", Type],
  955. {ok, _, [{Res}]} = Module:equery(C, Q, [In]),
  956. ?assertEqual(Out, Res)
  957. end,
  958. Test = fun(Type, In) ->
  959. Test3(Type, In, In)
  960. end,
  961. Test("int2", nil),
  962. Test3("int2", 'NULL', nil),
  963. Test("text", nil),
  964. Test3("text", 'NULL', nil),
  965. Test3("text", null, <<"null">>),
  966. Test("int2[]", [nil, 1, nil, 2]),
  967. Test3("text[]", [null, <<"ok">>], [<<"null">>, <<"ok">>]),
  968. Test3("int2[]", ['NULL', 1, nil, 2], [nil, 1, nil, 2]),
  969. Test("int2[]", [[nil], [1], [nil], [2]]),
  970. Test3("int2[]", [['NULL'], [1], [nil], [2]], [[nil], [1], [nil], [2]]),
  971. ?assertMatch(
  972. {ok, _, [{ {1, nil, {2, nil, 3}} }]},
  973. Module:equery(C, "SELECT (1, NULL, (2, NULL, 3))", []))
  974. end,
  975. [{nulls, [nil, 'NULL']}]).
  976. text_format(Config) ->
  977. Module = ?config(module, Config),
  978. epgsql_ct:with_connection(Config, fun(C) ->
  979. Select = fun(Type, V) ->
  980. V2 = list_to_binary(V),
  981. Query = "select $1::" ++ Type,
  982. ?assertMatch({ok, _Cols, [{V2}]}, Module:equery(C, Query, [V])),
  983. ?assertMatch({ok, _Cols, [{V2}]}, Module:equery(C, Query, [V2]))
  984. end,
  985. Select("numeric", "123456")
  986. end).
  987. query_timeout(Config) ->
  988. Module = ?config(module, Config),
  989. epgsql_ct:with_connection(Config, fun(C) ->
  990. {ok, _, _} = Module:squery(C, "SET statement_timeout = 500"),
  991. ?assertMatch(?TIMEOUT_ERROR, Module:squery(C, "SELECT pg_sleep(1)")),
  992. ?assertMatch(?TIMEOUT_ERROR, Module:equery(C, "SELECT pg_sleep(2)")),
  993. {ok, _Cols, [{1}]} = Module:equery(C, "SELECT 1")
  994. end, []).
  995. execute_timeout(Config) ->
  996. Module = ?config(module, Config),
  997. epgsql_ct:with_connection(Config, fun(C) ->
  998. {ok, _, _} = Module:squery(C, "SET statement_timeout = 500"),
  999. {ok, S} = Module:parse(C, "select pg_sleep($1)"),
  1000. ok = Module:bind(C, S, [2]),
  1001. ?assertMatch(?TIMEOUT_ERROR, Module:execute(C, S, 0)),
  1002. ok = Module:sync(C),
  1003. ok = Module:bind(C, S, [0]),
  1004. {ok, [{<<>>}]} = Module:execute(C, S, 0),
  1005. ok = Module:close(C, S),
  1006. ok = Module:sync(C)
  1007. end, []).
  1008. connection_closed(Config) ->
  1009. {Host, Port} = epgsql_ct:connection_data(Config),
  1010. Module = ?config(module, Config),
  1011. P = self(),
  1012. spawn_link(fun() ->
  1013. process_flag(trap_exit, true),
  1014. {ok, C} = Module:connect(Host, "epgsql_test",[
  1015. {port, Port},
  1016. {database, "epgsql_test_db1"}
  1017. ]),
  1018. P ! {connected, C},
  1019. receive
  1020. Any -> P ! Any
  1021. end
  1022. end),
  1023. receive
  1024. {connected, C} ->
  1025. timer:sleep(100),
  1026. Module:close(C),
  1027. {'EXIT', C, _} = receive R -> R end
  1028. end,
  1029. epgsql_ct:flush().
  1030. connection_closed_by_server(Config) ->
  1031. Module = ?config(module, Config),
  1032. epgsql_ct:with_connection(Config, fun(C1) ->
  1033. P = self(),
  1034. spawn_link(fun() ->
  1035. process_flag(trap_exit, true),
  1036. epgsql_ct:with_connection(Config, fun(C2) ->
  1037. {ok, _, [{Pid}]} = Module:equery(C2, "select pg_backend_pid()"),
  1038. % emulate of disconnection
  1039. {ok, _, [{true}]} = Module:equery(C1,
  1040. "select pg_terminate_backend($1)", [Pid]),
  1041. receive
  1042. {'EXIT', C2, {shutdown, #error{code = <<"57P01">>}}} ->
  1043. P ! ok;
  1044. Other ->
  1045. ?debugFmt("Unexpected msg: ~p~n", [Other]),
  1046. P ! error
  1047. end
  1048. end)
  1049. end),
  1050. receive ok -> ok end
  1051. end).
  1052. active_connection_closed(Config) ->
  1053. {Host, Port} = epgsql_ct:connection_data(Config),
  1054. Module = ?config(module, Config),
  1055. P = self(),
  1056. F = fun() ->
  1057. process_flag(trap_exit, true),
  1058. {ok, C} = Module:connect(Host, [
  1059. {database, "epgsql_test_db1"},
  1060. {port, Port}
  1061. ]),
  1062. P ! {connected, C},
  1063. R = Module:squery(C, "select pg_sleep(10)"),
  1064. P ! R
  1065. end,
  1066. spawn_link(F),
  1067. receive
  1068. {connected, C} ->
  1069. timer:sleep(100),
  1070. Module:close(C),
  1071. {error, closed} = receive R -> R end
  1072. end,
  1073. epgsql_ct:flush().
  1074. warning_notice(Config) ->
  1075. Module = ?config(module, Config),
  1076. epgsql_ct:with_connection(Config, fun(C) ->
  1077. Q = "create function pg_temp.raise() returns void as $$
  1078. begin
  1079. raise warning 'oops';
  1080. end;
  1081. $$ language plpgsql;
  1082. select pg_temp.raise()",
  1083. [{ok, _, _}, _] = Module:squery(C, Q),
  1084. receive
  1085. {epgsql, C, {notice, #error{message = <<"oops">>, extra = Extra}}} ->
  1086. ?assertMatch([{file, _},{line, _},{routine, _} | _], Extra),
  1087. ok
  1088. after
  1089. 100 -> erlang:error(didnt_receive_notice)
  1090. end
  1091. end, [{async, self()}]).
  1092. listen_notify(Config) ->
  1093. Module = ?config(module, Config),
  1094. epgsql_ct:with_connection(Config, fun(C) ->
  1095. {ok, [], []} = Module:squery(C, "listen epgsql_test"),
  1096. {ok, _, [{Pid}]} = Module:equery(C, "select pg_backend_pid()"),
  1097. {ok, [], []} = Module:squery(C, "notify epgsql_test"),
  1098. receive
  1099. {epgsql, C, {notification, <<"epgsql_test">>, Pid, <<>>}} -> ok
  1100. after
  1101. 100 -> erlang:error(didnt_receive_notification)
  1102. end
  1103. end, [{async, self()}]).
  1104. listen_notify_payload(Config) ->
  1105. Module = ?config(module, Config),
  1106. epgsql_ct:with_min_version(Config, [9, 0], fun(C) ->
  1107. {ok, [], []} = Module:squery(C, "listen epgsql_test"),
  1108. {ok, _, [{Pid}]} = Module:equery(C, "select pg_backend_pid()"),
  1109. {ok, [], []} = Module:squery(C, "notify epgsql_test, 'test!'"),
  1110. receive
  1111. {epgsql, C, {notification, <<"epgsql_test">>, Pid, <<"test!">>}} -> ok
  1112. after
  1113. 100 -> erlang:error(didnt_receive_notification)
  1114. end
  1115. end, [{async, self()}]).
  1116. set_notice_receiver(Config) ->
  1117. Module = ?config(module, Config),
  1118. epgsql_ct:with_min_version(Config, [9, 0], fun(C) ->
  1119. {ok, [], []} = Module:squery(C, "listen epgsql_test"),
  1120. {ok, _, [{Pid}]} = Module:equery(C, "select pg_backend_pid()"),
  1121. EnsureNoNotification = fun(Payload) ->
  1122. {ok, [], []} = Module:squery(C, ["notify epgsql_test, '", Payload, "'"]),
  1123. receive
  1124. {epgsql, _, _} -> erlang:error(got_unexpected_notification)
  1125. after
  1126. 10 -> ok
  1127. end
  1128. end,
  1129. EnsureNotification = fun(Payload) ->
  1130. {ok, [], []} = Module:squery(C, ["notify epgsql_test, '", Payload, "'"]),
  1131. receive
  1132. {epgsql, C, {notification, <<"epgsql_test">>, Pid, Payload}} -> ok
  1133. after
  1134. 100 -> erlang:error(didnt_receive_notification)
  1135. end
  1136. end,
  1137. Self = self(),
  1138. EnsureNoNotification(<<"test1">>),
  1139. % Set pid()
  1140. {ok, undefined} = Module:set_notice_receiver(C, Self),
  1141. EnsureNotification(<<"test2">>),
  1142. %% test PL/PgSQL NOTICE
  1143. {ok, [], []} = Module:squery(C, ["DO $$ BEGIN RAISE WARNING 'test notice'; END $$;"]),
  1144. receive
  1145. {epgsql, C, {notice, #error{severity = warning,
  1146. code = <<"01000">>,
  1147. message = <<"test notice">>,
  1148. extra = _}}} -> ok
  1149. after
  1150. 100 -> erlang:error(didnt_receive_notice)
  1151. end,
  1152. % set registered pid
  1153. Receiver = pg_notification_receiver,
  1154. register(Receiver, Self),
  1155. {ok, Self} = Module:set_notice_receiver(C, Receiver),
  1156. EnsureNotification(<<"test3">>),
  1157. % make registered name invalid
  1158. unregister(Receiver),
  1159. EnsureNoNotification(<<"test4">>),
  1160. % disable
  1161. {ok, Receiver} = Module:set_notice_receiver(C, undefined),
  1162. EnsureNoNotification(<<"test5">>)
  1163. end, []).
  1164. get_cmd_status(Config) ->
  1165. Module = ?config(module, Config),
  1166. epgsql_ct:with_connection(Config, fun(C) ->
  1167. {ok, [], []} = Module:squery(C, "BEGIN"),
  1168. ?assertEqual({ok, 'begin'}, Module:get_cmd_status(C)),
  1169. {ok, [], []} = epgsql:equery(C, "CREATE TEMPORARY TABLE cmd_status_t(col INTEGER)"),
  1170. ?assertEqual({ok, 'create'}, Module:get_cmd_status(C)),
  1171. %% Some commands have number of affected rows in status
  1172. {ok, N} = Module:squery(C, "INSERT INTO cmd_status_t (col) VALUES (1), (2)"),
  1173. ?assertEqual({ok, {'insert', N}}, Module:get_cmd_status(C)),
  1174. {ok, 1} = Module:squery(C, "UPDATE cmd_status_t SET col=3 WHERE col=1"),
  1175. ?assertEqual({ok, {'update', 1}}, Module:get_cmd_status(C)),
  1176. %% Failed queries have no status
  1177. {error, _} = Module:squery(C, "DELETE FROM cmd_status_t WHERE not_col=2"),
  1178. ?assertEqual({ok, undefined}, Module:get_cmd_status(C)),
  1179. %% if COMMIT failed, status will be 'rollback'
  1180. {ok, [], []} = Module:squery(C, "COMMIT"),
  1181. ?assertEqual({ok, 'rollback'}, Module:get_cmd_status(C)),
  1182. %% Only last command's status returned
  1183. [_, _, _] = Module:squery(C, "BEGIN; SELECT 1; COMMIT"),
  1184. ?assertEqual({ok, 'commit'}, Module:get_cmd_status(C))
  1185. end).
  1186. range_type(Config) ->
  1187. epgsql_ct:with_min_version(Config, [9, 2], fun(_C) ->
  1188. check_type(Config, int4range, "int4range(10, 20)", {10, 20}, [
  1189. {1, 58}, {-1, 12}, {-985521, 5412687}, {minus_infinity, 0},
  1190. {984655, plus_infinity}, {minus_infinity, plus_infinity}
  1191. ]),
  1192. check_type(Config, int4range, "int4range(10, 10)", empty, [])
  1193. end, []).
  1194. range8_type(Config) ->
  1195. epgsql_ct:with_min_version(Config, [9, 2], fun(_C) ->
  1196. check_type(Config, int8range, "int8range(10, 20)", {10, 20}, [
  1197. {1, 58}, {-1, 12}, {-9223372036854775808, 5412687},
  1198. {minus_infinity, 9223372036854775807},
  1199. {984655, plus_infinity}, {minus_infinity, plus_infinity}
  1200. ]),
  1201. check_type(Config, int8range, "int8range(10, 10)", empty, [])
  1202. end, []).
  1203. date_time_range_type(Config) ->
  1204. epgsql_ct:with_min_version(Config, [9, 2], fun(_C) ->
  1205. 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}}}, []),
  1206. 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}}}, []),
  1207. 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}}}, []),
  1208. 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}}}, []),
  1209. 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}}}, []),
  1210. check_type(Config, tsrange, "tsrange('2008-01-02 03:04:05', '2008-01-02 03:04:05')", empty, []),
  1211. check_type(Config, daterange, "daterange('2008-01-02', '2008-02-02')", {{2008,1,2}, {2008, 2, 2}}, [{{-4712,1,1}, {5874897,1,1}}]),
  1212. 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}}}]),
  1213. check_type(Config, tstzrange, "tstzrange('2008-01-02 03:04:05', null)", {{{2008,1,2},{3,4,5.0}}, plus_infinity}, []),
  1214. check_type(Config, tstzrange, "tstzrange('2008-01-02 03:04:05', null, '[]')", {{{2008,1,2},{3,4,5.0}}, plus_infinity}, []),
  1215. check_type(Config, tstzrange, "tstzrange('2008-01-02 03:04:05', null, '()')", {{{2008,1,2},{3,4,5.0}}, plus_infinity}, []),
  1216. check_type(Config, tstzrange, "tstzrange(null, '2008-01-02 03:04:05')", {minus_infinity, {{2008,1,2},{3,4,5.0}}}, []),
  1217. check_type(Config, tstzrange, "tstzrange(null, '2008-01-02 03:04:05', '[]')", {minus_infinity, {{2008,1,2},{3,4,5.0}}}, []),
  1218. check_type(Config, tstzrange, "tstzrange(null, '2008-01-02 03:04:05', '()')", {minus_infinity, {{2008,1,2},{3,4,5.0}}}, [])
  1219. end, []).
  1220. with_transaction(Config) ->
  1221. Module = ?config(module, Config),
  1222. epgsql_ct:with_connection(
  1223. Config,
  1224. fun(C) ->
  1225. %% Success case
  1226. ?assertEqual(
  1227. success, Module:with_transaction(C, fun(_) -> success end)),
  1228. ?assertEqual(
  1229. success, Module:with_transaction(C, fun(_) -> success end,
  1230. [{ensure_committed, true}])),
  1231. %% begin_opts
  1232. ?assertMatch(
  1233. [{ok, _, [{<<"serializable">>}]},
  1234. {ok, _, [{<<"on">>}]}],
  1235. Module:with_transaction(
  1236. C, fun(C1) ->
  1237. Module:squery(C1, ("SHOW transaction_isolation; "
  1238. "SHOW transaction_read_only"))
  1239. end,
  1240. [{begin_opts, "READ ONLY ISOLATION LEVEL SERIALIZABLE"}])),
  1241. %% ensure_committed failure
  1242. ?assertError(
  1243. {ensure_committed_failed, rollback},
  1244. Module:with_transaction(
  1245. C, fun(C1) ->
  1246. {error, _} = Module:squery(C1, "SELECT col FROM _nowhere_"),
  1247. ok
  1248. end,
  1249. [{ensure_committed, true}])),
  1250. %% reraise
  1251. ?assertEqual(
  1252. {rollback, my_err},
  1253. Module:with_transaction(
  1254. C, fun(_) -> error(my_err) end,
  1255. [{reraise, false}])),
  1256. ?assertError(
  1257. my_err,
  1258. Module:with_transaction(
  1259. C, fun(_) -> error(my_err) end, []))
  1260. end, []).
  1261. %% =============================================================================
  1262. %% Internal functions
  1263. %% ============================================================================
  1264. get_type_col(Type) ->
  1265. "c_" ++ atom_to_list(Type).
  1266. check_type(Config, Type, In, Out, Values) ->
  1267. Column = get_type_col(Type),
  1268. check_type(Config, Type, In, Out, Values, Column).
  1269. check_type(Config, Type, In, Out, Values, Column) ->
  1270. epgsql_ct:with_connection(Config, fun(C) ->
  1271. check_type(Config, Type, In, Out, Values, Column, C)
  1272. end).
  1273. check_type(Config, Type, In, Out, Values, Column, C) ->
  1274. Module = ?config(module, Config),
  1275. Select = io_lib:format("select ~s::~w", [In, Type]),
  1276. Res = Module:equery(C, Select),
  1277. ?assertMatch({ok, [#column{type = Type}], [{Out}]}, Res),
  1278. Sql = io_lib:format("insert into test_table2 (~s) values ($1) returning ~s", [Column, Column]),
  1279. {ok, #statement{columns = [#column{type = Type}]} = S} = Module:parse(C, Sql),
  1280. Insert = fun(V) ->
  1281. ok = Module:bind(C, S, [V]),
  1282. {ok, 1, [{V2}]} = Module:execute(C, S),
  1283. case compare(Type, V, V2) of
  1284. true -> ok;
  1285. false -> error({write_read_compare_failed, io_lib:format("~p =/= ~p", [V, V2])})
  1286. end,
  1287. ok = Module:sync(C)
  1288. end,
  1289. lists:foreach(Insert, [null, undefined | Values]).
  1290. compare(_Type, null, null) -> true;
  1291. compare(_Type, undefined, null) -> true;
  1292. compare(float4, V1, V2) when is_float(V1) -> abs(V2 - V1) < 0.000001;
  1293. compare(float8, V1, V2) when is_float(V1) -> abs(V2 - V1) < 0.000000000000001;
  1294. compare(hstore, {V1}, V2) -> compare(hstore, V1, V2);
  1295. compare(hstore, V1, {V2}) -> compare(hstore, V1, V2);
  1296. compare(hstore, V1, V2) ->
  1297. orddict:from_list(format_hstore(V1)) =:= orddict:from_list(format_hstore(V2));
  1298. compare(Type, V1 = {_, _, MS}, {D2, {H2, M2, S2}}) when Type == timestamp;
  1299. Type == timestamptz ->
  1300. {D1, {H1, M1, S1}} = calendar:now_to_universal_time(V1),
  1301. ({D1, H1, M1} =:= {D2, H2, M2}) and (abs(S1 + MS/1000000 - S2) < 0.000000000000001);
  1302. compare(_Type, V1, V2) -> V1 =:= V2.
  1303. format_hstore({Hstore}) -> Hstore;
  1304. format_hstore(Hstore) ->
  1305. [{format_hstore_key(Key), format_hstore_value(Value)} || {Key, Value} <- Hstore].
  1306. format_hstore_key(Key) -> format_hstore_string(Key).
  1307. format_hstore_value(null) -> null;
  1308. format_hstore_value(undefined) -> null;
  1309. format_hstore_value(Value) -> format_hstore_string(Value).
  1310. format_hstore_string(Num) when is_number(Num) -> iolist_to_binary(io_lib:format("~w", [Num]));
  1311. format_hstore_string(Str) -> iolist_to_binary(io_lib:format("~s", [Str])).