mysql_tests.erl 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. %% MySQL/OTP – MySQL client library for Erlang/OTP
  2. %% Copyright (C) 2014 Viktor Söderqvist
  3. %%
  4. %% This file is part of MySQL/OTP.
  5. %%
  6. %% MySQL/OTP is free software: you can redistribute it and/or modify it under
  7. %% the terms of the GNU Lesser General Public License as published by the Free
  8. %% Software Foundation, either version 3 of the License, or (at your option)
  9. %% any later version.
  10. %%
  11. %% This program is distributed in the hope that it will be useful, but WITHOUT
  12. %% ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. %% FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. %% more details.
  15. %%
  16. %% You should have received a copy of the GNU Lesser General Public License
  17. %% along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. %% @doc This module performs test to an actual database.
  19. -module(mysql_tests).
  20. -include_lib("eunit/include/eunit.hrl").
  21. -define(user, "otptest").
  22. -define(password, "otptest").
  23. -define(create_table_t, <<"CREATE TABLE t ("
  24. " id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,"
  25. " bl BLOB,"
  26. " tx TEXT NOT NULL," %% No default value
  27. " f FLOAT,"
  28. " d DOUBLE,"
  29. " dc DECIMAL(5,3),"
  30. " y YEAR,"
  31. " ti TIME,"
  32. " ts TIMESTAMP,"
  33. " da DATE,"
  34. " c CHAR(2)"
  35. ") ENGINE=InnoDB">>).
  36. connect_test() ->
  37. %% A connection with a registered name
  38. Options = [{name, {local, tardis}}, {user, ?user}, {password, ?password}],
  39. {ok, Pid} = mysql:start_link(Options),
  40. %% Test some gen_server callbacks not tested elsewhere
  41. State = get_state(Pid),
  42. ?assertMatch({ok, State}, mysql:code_change("0.1.0", State, [])),
  43. ?assertMatch({error, _}, mysql:code_change("2.0.0", unknown_state, [])),
  44. exit(whereis(tardis), normal).
  45. %% For R16B where sys:get_state/1 is not available.
  46. get_state(Process) ->
  47. {status,_,_,[_,_,_,_,Misc]} = sys:get_status(Process),
  48. hd([State || {data,[{"State", State}]} <- Misc]).
  49. query_test_() ->
  50. {setup,
  51. fun () ->
  52. {ok, Pid} = mysql:start_link([{user, ?user}, {password, ?password},
  53. {log_warnings, false}]),
  54. ok = mysql:query(Pid, <<"DROP DATABASE IF EXISTS otptest">>),
  55. ok = mysql:query(Pid, <<"CREATE DATABASE otptest">>),
  56. ok = mysql:query(Pid, <<"USE otptest">>),
  57. ok = mysql:query(Pid, <<"SET autocommit = 1">>),
  58. Pid
  59. end,
  60. fun (Pid) ->
  61. ok = mysql:query(Pid, <<"DROP DATABASE otptest">>),
  62. exit(Pid, normal)
  63. end,
  64. {with, [fun connect_with_db/1,
  65. fun autocommit/1,
  66. fun basic_queries/1,
  67. fun text_protocol/1,
  68. fun binary_protocol/1,
  69. fun float_rounding/1,
  70. fun decimal/1,
  71. fun int/1,
  72. fun bit/1,
  73. fun date/1,
  74. fun time/1,
  75. fun datetime/1,
  76. fun microseconds/1]}}.
  77. connect_with_db(_Pid) ->
  78. %% Make another connection and set the db in the handshake phase
  79. {ok, Pid} = mysql:start_link([{user, ?user}, {password, ?password},
  80. {database, "otptest"}]),
  81. ?assertMatch({ok, _, [[<<"otptest">>]]},
  82. mysql:query(Pid, "SELECT DATABASE()")),
  83. %% Capture error log to check that we get a warning logged
  84. {ok, ok, LoggedErrors} = error_logger_acc:capture(fun () ->
  85. mysql:query(Pid, <<"DROP TABLE IF EXISTS dummy">>)
  86. end),
  87. ?assertMatch([{_, "Note 1051: Unknown table 'dummy'\n"
  88. " in DROP TABLE IF EXISTS dummy" ++ _}],
  89. LoggedErrors),
  90. exit(Pid, normal).
  91. autocommit(Pid) ->
  92. ?assert(mysql:autocommit(Pid)),
  93. ok = mysql:query(Pid, <<"SET autocommit = 0">>),
  94. ?assertNot(mysql:autocommit(Pid)),
  95. ok = mysql:query(Pid, <<"SET autocommit = 1">>),
  96. ?assert(mysql:autocommit(Pid)).
  97. basic_queries(Pid) ->
  98. %% warning count
  99. ?assertEqual(ok, mysql:query(Pid, <<"DROP TABLE IF EXISTS foo">>)),
  100. ?assertEqual(1, mysql:warning_count(Pid)),
  101. %% SQL parse error
  102. ?assertMatch({error, {1064, <<"42000">>, <<"You have an erro", _/binary>>}},
  103. mysql:query(Pid, <<"FOO">>)),
  104. %% Simple resultset with various types
  105. ?assertEqual({ok, [<<"i">>, <<"s">>], [[42, <<"foo">>]]},
  106. mysql:query(Pid, <<"SELECT 42 AS i, 'foo' AS s;">>)),
  107. ok.
  108. text_protocol(Pid) ->
  109. ok = mysql:query(Pid, ?create_table_t),
  110. ok = mysql:query(Pid, <<"INSERT INTO t (bl, f, d, dc, y, ti, ts, da, c)"
  111. " VALUES ('blob', 3.14, 3.14, 3.14, 2014,"
  112. "'00:22:11', '2014-11-03 00:22:24', '2014-11-03',"
  113. " NULL)">>),
  114. ?assertEqual(1, mysql:warning_count(Pid)), %% tx has no default value
  115. ?assertEqual(1, mysql:insert_id(Pid)), %% auto_increment starts from 1
  116. ?assertEqual(1, mysql:affected_rows(Pid)),
  117. %% select
  118. {ok, Columns, Rows} = mysql:query(Pid, <<"SELECT * FROM t">>),
  119. ?assertEqual([<<"id">>, <<"bl">>, <<"tx">>, <<"f">>, <<"d">>, <<"dc">>,
  120. <<"y">>, <<"ti">>, <<"ts">>, <<"da">>, <<"c">>], Columns),
  121. ?assertEqual([[1, <<"blob">>, <<>>, 3.14, 3.14, 3.14,
  122. 2014, {0, {0, 22, 11}},
  123. {{2014, 11, 03}, {00, 22, 24}}, {2014, 11, 03}, null]],
  124. Rows),
  125. ok = mysql:query(Pid, <<"DROP TABLE t">>).
  126. binary_protocol(Pid) ->
  127. ok = mysql:query(Pid, ?create_table_t),
  128. %% The same queries as in the text protocol. Expect the same results.
  129. {ok, Ins} = mysql:prepare(Pid, <<"INSERT INTO t (bl, f, d, dc, y, ti,"
  130. " ts, da, c)"
  131. " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)">>),
  132. ok = mysql:execute(Pid, Ins, [<<"blob">>, 3.14, 3.14, 3.14,
  133. 2014, {0, {0, 22, 11}},
  134. {{2014, 11, 03}, {0, 22, 24}},
  135. {2014, 11, 03}, null]),
  136. {ok, Stmt} = mysql:prepare(Pid, <<"SELECT * FROM t WHERE id=?">>),
  137. {ok, Columns, Rows} = mysql:execute(Pid, Stmt, [1]),
  138. ?assertEqual([<<"id">>, <<"bl">>, <<"tx">>, <<"f">>, <<"d">>, <<"dc">>,
  139. <<"y">>, <<"ti">>,
  140. <<"ts">>, <<"da">>, <<"c">>], Columns),
  141. ?assertEqual([[1, <<"blob">>, <<>>, 3.14, 3.14, 3.14,
  142. 2014, {0, {0, 22, 11}},
  143. {{2014, 11, 03}, {00, 22, 24}}, {2014, 11, 03}, null]],
  144. Rows),
  145. ok = mysql:query(Pid, <<"DROP TABLE t">>).
  146. float_rounding(Pid) ->
  147. %% This is to make sure we get the same values for 32-bit FLOATs in the text
  148. %% and binary protocols for ordinary queries and prepared statements
  149. %% respectively.
  150. %%
  151. %% MySQL rounds to 6 significant digits when "printing" floats over the
  152. %% text protocol. When we receive a float on the binary protocol, we round
  153. %% it in the same way to match what MySQL does on the text protocol. This
  154. %% way we should to get the same values regardless of which protocol is
  155. %% used.
  156. %% Table for testing floats
  157. ok = mysql:query(Pid, "CREATE TABLE f (f FLOAT)"),
  158. %% Prepared statements
  159. {ok, Insert} = mysql:prepare(Pid, "INSERT INTO f (f) VALUES (?)"),
  160. {ok, Select} = mysql:prepare(Pid, "SELECT f FROM f"),
  161. %% [{Input, Expected}]
  162. TestData = [{1.0, 1.0}, {3.14, 3.14}, {0.2, 0.2},
  163. {0.20082111, 0.200821}, {0.000123456789, 0.000123457},
  164. {33.3333333, 33.3333}, {-33.2233443322, -33.2233},
  165. {400.0123, 400.012}, {1000.1234, 1000.12},
  166. {999.00009, 999.0},
  167. {1234.5678, 1234.57}, {68888.8888, 68888.9},
  168. {123456.789, 123457.0}, {7654321.0, 7654320.0},
  169. {80001111.1, 80001100.0}, {987654321.0, 987654000.0},
  170. {-123456789.0, -123457000.0},
  171. {2.12345111e-23, 2.12345e-23}, {-2.12345111e-23, -2.12345e-23},
  172. {2.12345111e23, 2.12345e23}, {-2.12345111e23, -2.12345e23}],
  173. lists:foreach(fun ({Input, Expected}) ->
  174. %% Insert using binary protocol (sending it as a double)
  175. ok = mysql:execute(Pid, Insert, [Input]),
  176. %% Text (plain query)
  177. {ok, _, [[Value]]} = mysql:query(Pid, "SELECT f FROM f"),
  178. ?assertEqual(Expected, Value),
  179. %% Binary (prepared statement)
  180. {ok, _, [[BinValue]]} = mysql:execute(Pid, Select, []),
  181. ?assertEqual(Expected, BinValue),
  182. %% cleanup before the next test
  183. ok = mysql:query(Pid, "DELETE FROM f")
  184. end,
  185. TestData),
  186. ok = mysql:query(Pid, "DROP TABLE f").
  187. decimal(Pid) ->
  188. %% As integer when S == 0
  189. ok = mysql:query(Pid, "CREATE TABLE dec0 (d DECIMAL(50, 0))"),
  190. write_read_text_binary(
  191. Pid, 14159265358979323846264338327950288419716939937510,
  192. <<"14159265358979323846264338327950288419716939937510">>,
  193. <<"dec0">>, <<"d">>
  194. ),
  195. write_read_text_binary(
  196. Pid, -14159265358979323846264338327950288419716939937510,
  197. <<"-14159265358979323846264338327950288419716939937510">>,
  198. <<"dec0">>, <<"d">>
  199. ),
  200. ok = mysql:query(Pid, "DROP TABLE dec0"),
  201. %% As float when P =< 15, S > 0
  202. ok = mysql:query(Pid, "CREATE TABLE dec15 (d DECIMAL(15, 14))"),
  203. write_read_text_binary(Pid, 3.14159265358979, <<"3.14159265358979">>,
  204. <<"dec15">>, <<"d">>),
  205. write_read_text_binary(Pid, -3.14159265358979, <<"-3.14159265358979">>,
  206. <<"dec15">>, <<"d">>),
  207. write_read_text_binary(Pid, 3.0, <<"3">>, <<"dec15">>, <<"d">>),
  208. ok = mysql:query(Pid, "DROP TABLE dec15"),
  209. %% As binary when P >= 16, S > 0
  210. ok = mysql:query(Pid, "CREATE TABLE dec16 (d DECIMAL(16, 15))"),
  211. write_read_text_binary(Pid, <<"3.141592653589793">>,
  212. <<"3.141592653589793">>, <<"dec16">>, <<"d">>),
  213. write_read_text_binary(Pid, <<"-3.141592653589793">>,
  214. <<"-3.141592653589793">>, <<"dec16">>, <<"d">>),
  215. write_read_text_binary(Pid, <<"3.000000000000000">>, <<"3">>,
  216. <<"dec16">>, <<"d">>),
  217. ok = mysql:query(Pid, "DROP TABLE dec16").
  218. int(Pid) ->
  219. ok = mysql:query(Pid, "CREATE TABLE ints (i INT)"),
  220. write_read_text_binary(Pid, 42, <<"42">>, <<"ints">>, <<"i">>),
  221. write_read_text_binary(Pid, -42, <<"-42">>, <<"ints">>, <<"i">>),
  222. write_read_text_binary(Pid, 987654321, <<"987654321">>, <<"ints">>,
  223. <<"i">>),
  224. write_read_text_binary(Pid, -987654321, <<"-987654321">>,
  225. <<"ints">>, <<"i">>),
  226. ok = mysql:query(Pid, "DROP TABLE ints"),
  227. %% Overflow with TINYINT
  228. ok = mysql:query(Pid, "CREATE TABLE tint (i TINYINT)"),
  229. write_read_text_binary(Pid, 127, <<"1000">>, <<"tint">>, <<"i">>),
  230. write_read_text_binary(Pid, -128, <<"-1000">>, <<"tint">>, <<"i">>),
  231. ok = mysql:query(Pid, "DROP TABLE tint"),
  232. %% SMALLINT
  233. ok = mysql:query(Pid, "CREATE TABLE sint (i SMALLINT)"),
  234. write_read_text_binary(Pid, 32000, <<"32000">>, <<"sint">>, <<"i">>),
  235. write_read_text_binary(Pid, -32000, <<"-32000">>, <<"sint">>, <<"i">>),
  236. ok = mysql:query(Pid, "DROP TABLE sint"),
  237. %% BIGINT
  238. ok = mysql:query(Pid, "CREATE TABLE bint (i BIGINT)"),
  239. write_read_text_binary(Pid, 123456789012, <<"123456789012">>,
  240. <<"bint">>, <<"i">>),
  241. write_read_text_binary(Pid, -123456789012, <<"-123456789012">>,
  242. <<"bint">>, <<"i">>),
  243. ok = mysql:query(Pid, "DROP TABLE bint").
  244. %% The BIT(N) datatype in MySQL 5.0.3 and later: the equivallent to bitstring()
  245. bit(Pid) ->
  246. ok = mysql:query(Pid, "CREATE TABLE bits (b BIT(11))"),
  247. write_read_text_binary(Pid, <<16#ff, 0:3>>, <<"b'11111111000'">>,
  248. <<"bits">>, <<"b">>),
  249. write_read_text_binary(Pid, <<16#7f, 6:3>>, <<"b'01111111110'">>,
  250. <<"bits">>, <<"b">>),
  251. ok = mysql:query(Pid, "DROP TABLE bits").
  252. date(Pid) ->
  253. ok = mysql:query(Pid, "CREATE TABLE d (d DATE)"),
  254. lists:foreach(
  255. fun ({Value, SqlLiteral}) ->
  256. write_read_text_binary(Pid, Value, SqlLiteral, <<"d">>, <<"d">>)
  257. end,
  258. [{{2014, 11, 03}, <<"'2014-11-03'">>},
  259. {{0, 0, 0}, <<"'0000-00-00'">>}]
  260. ),
  261. ok = mysql:query(Pid, "DROP TABLE d").
  262. %% Test TIME value representation. There are a few things to check.
  263. time(Pid) ->
  264. ok = mysql:query(Pid, "CREATE TABLE tm (tm TIME)"),
  265. lists:foreach(
  266. fun ({Value, SqlLiteral}) ->
  267. write_read_text_binary(Pid, Value, SqlLiteral, <<"tm">>, <<"tm">>)
  268. end,
  269. [{{0, {10, 11, 12}}, <<"'10:11:12'">>},
  270. {{5, {0, 0, 1}}, <<"'120:00:01'">>},
  271. {{-1, {23, 59, 59}}, <<"'-00:00:01'">>},
  272. {{-1, {23, 59, 0}}, <<"'-00:01:00'">>},
  273. {{-1, {23, 0, 0}}, <<"'-01:00:00'">>},
  274. {{-1, {0, 0, 0}}, <<"'-24:00:00'">>},
  275. {{-5, {10, 0, 0}}, <<"'-110:00:00'">>},
  276. {{0, {0, 0, 0}}, <<"'00:00:00'">>}]
  277. ),
  278. %% Zero seconds as a float.
  279. ok = mysql:query(Pid, "INSERT INTO tm (tm) VALUES (?)",
  280. [{-1, {1, 2, 0.0}}]),
  281. ?assertEqual({ok, [<<"tm">>], [[{-1, {1, 2, 0}}]]},
  282. mysql:query(Pid, "SELECT tm FROM tm")),
  283. ok = mysql:query(Pid, "DROP TABLE tm").
  284. datetime(Pid) ->
  285. ok = mysql:query(Pid, "CREATE TABLE dt (dt DATETIME)"),
  286. lists:foreach(
  287. fun ({Value, SqlLiteral}) ->
  288. write_read_text_binary(Pid, Value, SqlLiteral, <<"dt">>, <<"dt">>)
  289. end,
  290. [{{{2014, 12, 14}, {19, 39, 20}}, <<"'2014-12-14 19:39:20'">>},
  291. {{{2014, 12, 14}, {0, 0, 0}}, <<"'2014-12-14 00:00:00'">>},
  292. {{{0, 0, 0}, {0, 0, 0}}, <<"'0000-00-00 00:00:00'">>}]
  293. ),
  294. ok = mysql:query(Pid, "DROP TABLE dt").
  295. microseconds(Pid) ->
  296. %% Check whether we have the required version for this testcase.
  297. {ok, _, [[Version]]} = mysql:query(Pid, <<"SELECT @@version">>),
  298. try
  299. %% Remove stuff after dash for e.g. "5.5.40-0ubuntu0.12.04.1-log"
  300. [Version1 | _] = binary:split(Version, <<"-">>),
  301. Version2 = lists:map(fun binary_to_integer/1,
  302. binary:split(Version1, <<".">>, [global])),
  303. Version2 >= [5, 6, 4] orelse throw(nope)
  304. of _ ->
  305. test_time_microseconds(Pid),
  306. test_datetime_microseconds(Pid)
  307. catch _:_ ->
  308. error_logger:info_msg("Skipping microseconds test. Current MySQL"
  309. " version is ~s. Required version is >= 5.6.4.~n",
  310. [Version])
  311. end.
  312. test_time_microseconds(Pid) ->
  313. ok = mysql:query(Pid, "CREATE TABLE m (t TIME(6))"),
  314. %% Positive time
  315. write_read_text_binary(Pid, {0, {23, 59, 57.654321}},
  316. <<"'23:59:57.654321'">>, <<"m">>, <<"t">>),
  317. %% Negative time
  318. write_read_text_binary(Pid, {-1, {23, 59, 57.654321}},
  319. <<"'-00:00:02.345679'">>, <<"m">>, <<"t">>),
  320. ok = mysql:query(Pid, "DROP TABLE m").
  321. test_datetime_microseconds(Pid) ->
  322. ok = mysql:query(Pid, "CREATE TABLE dt (dt DATETIME(6))"),
  323. write_read_text_binary(Pid, {{2014, 11, 23}, {23, 59, 57.654321}},
  324. <<"'2014-11-23 23:59:57.654321'">>, <<"dt">>,
  325. <<"dt">>),
  326. ok = mysql:query(Pid, "DROP TABLE dt").
  327. %% @doc Tests write and read in text and the binary protocol, all combinations.
  328. %% This helper function assumes an empty table with a single column.
  329. write_read_text_binary(Conn, Term, SqlLiteral, Table, Column) ->
  330. SelectQuery = <<"SELECT ", Column/binary, " FROM ", Table/binary>>,
  331. {ok, SelectStmt} = mysql:prepare(Conn, SelectQuery),
  332. %% Insert as text, read text and binary, delete
  333. InsertQuery = <<"INSERT INTO ", Table/binary, " (", Column/binary, ")"
  334. " VALUES (", SqlLiteral/binary, ")">>,
  335. ok = mysql:query(Conn, InsertQuery),
  336. ?assertEqual({ok, [Column], [[Term]]}, mysql:query(Conn, SelectQuery)),
  337. ?assertEqual({ok, [Column], [[Term]]}, mysql:execute(Conn, SelectStmt, [])),
  338. mysql:query(Conn, <<"DELETE FROM ", Table/binary>>),
  339. %% Insert as binary, read text and binary, delete
  340. InsertQ = <<"INSERT INTO ", Table/binary, " (", Column/binary, ")",
  341. " VALUES (?)">>,
  342. {ok, InsertStmt} = mysql:prepare(Conn, InsertQ),
  343. ok = mysql:execute(Conn, InsertStmt, [Term]),
  344. ok = mysql:unprepare(Conn, InsertStmt),
  345. ?assertEqual({ok, [Column], [[Term]]}, mysql:query(Conn, SelectQuery)),
  346. ?assertEqual({ok, [Column], [[Term]]}, mysql:execute(Conn, SelectStmt, [])),
  347. mysql:query(Conn, <<"DELETE FROM ", Table/binary>>),
  348. %% Cleanup
  349. ok = mysql:unprepare(Conn, SelectStmt).
  350. %% --------------------------------------------------------------------------
  351. timeout_test_() ->
  352. {setup,
  353. fun () ->
  354. {ok, Pid} = mysql:start_link([{user, ?user}, {password, ?password},
  355. {log_warnings, false}]),
  356. Pid
  357. end,
  358. fun (Pid) ->
  359. exit(Pid, normal)
  360. end,
  361. {with, [fun (Pid) ->
  362. %% SLEEP was added in MySQL 5.0.12
  363. ?assertEqual({ok, [<<"SLEEP(5)">>], [[1]]},
  364. mysql:query(Pid, <<"SELECT SLEEP(5)">>, 40)),
  365. %% A query after an interrupted query shouldn't get a timeout.
  366. ?assertMatch({ok,[<<"42">>], [[42]]},
  367. mysql:query(Pid, <<"SELECT 42">>)),
  368. %% Parametrized query
  369. ?assertEqual({ok, [<<"SLEEP(?)">>], [[1]]},
  370. mysql:query(Pid, <<"SELECT SLEEP(?)">>, [5], 40)),
  371. %% Prepared statement
  372. {ok, Stmt} = mysql:prepare(Pid, <<"SELECT SLEEP(?)">>),
  373. ?assertEqual({ok, [<<"SLEEP(?)">>], [[1]]},
  374. mysql:execute(Pid, Stmt, [5], 40)),
  375. ok = mysql:unprepare(Pid, Stmt)
  376. end]}}.
  377. %% --------------------------------------------------------------------------
  378. %% Prepared statements
  379. with_table_foo_test_() ->
  380. {setup,
  381. fun () ->
  382. {ok, Pid} = mysql:start_link([{user, ?user}, {password, ?password},
  383. {query_cache_time, 50},
  384. {log_warnings, false}]),
  385. ok = mysql:query(Pid, <<"DROP DATABASE IF EXISTS otptest">>),
  386. ok = mysql:query(Pid, <<"CREATE DATABASE otptest">>),
  387. ok = mysql:query(Pid, <<"USE otptest">>),
  388. ok = mysql:query(Pid, <<"CREATE TABLE foo (bar INT) engine=InnoDB">>),
  389. Pid
  390. end,
  391. fun (Pid) ->
  392. ok = mysql:query(Pid, <<"DROP DATABASE otptest">>),
  393. exit(Pid, normal)
  394. end,
  395. {with, [fun prepared_statements/1,
  396. fun parameterized_query/1]}}.
  397. prepared_statements(Pid) ->
  398. %% Unnamed
  399. ?assertEqual({error,{1146, <<"42S02">>,
  400. <<"Table 'otptest.tab' doesn't exist">>}},
  401. mysql:prepare(Pid, "SELECT * FROM tab WHERE id = ?")),
  402. {ok, StmtId} = mysql:prepare(Pid, "SELECT * FROM foo WHERE bar = ?"),
  403. ?assert(is_integer(StmtId)),
  404. ?assertEqual(ok, mysql:unprepare(Pid, StmtId)),
  405. ?assertEqual({error, not_prepared}, mysql:unprepare(Pid, StmtId)),
  406. %% Named
  407. ?assertEqual({error,{1146, <<"42S02">>,
  408. <<"Table 'otptest.tab' doesn't exist">>}},
  409. mysql:prepare(Pid, tab, "SELECT * FROM tab WHERE id = ?")),
  410. ?assertEqual({ok, foo},
  411. mysql:prepare(Pid, foo, "SELECT * FROM foo WHERE bar = ?")),
  412. %% Prepare again unprepares the old stmt associated with this name.
  413. ?assertEqual({ok, foo},
  414. mysql:prepare(Pid, foo, "SELECT bar FROM foo WHERE bar = ?")),
  415. ?assertEqual(ok, mysql:unprepare(Pid, foo)),
  416. ?assertEqual({error, not_prepared}, mysql:unprepare(Pid, foo)),
  417. %% Execute when not prepared
  418. ?assertEqual({error, not_prepared}, mysql:execute(Pid, not_a_stmt, [])),
  419. ok.
  420. parameterized_query(Conn) ->
  421. %% To see that cache eviction works as expected, look at the code coverage.
  422. {ok, _, []} = mysql:query(Conn, "SELECT * FROM foo WHERE bar = ?", [1]),
  423. {ok, _, []} = mysql:query(Conn, "SELECT * FROM foo WHERE bar = ?", [2]),
  424. receive after 150 -> ok end, %% Now the query cache should emptied
  425. {ok, _, []} = mysql:query(Conn, "SELECT * FROM foo WHERE bar = ?", [3]).
  426. %% --- simple gen_server callbacks ---
  427. gen_server_coverage_test() ->
  428. {noreply, state} = mysql:handle_cast(foo, state),
  429. {noreply, state} = mysql:handle_info(foo, state),
  430. ok = mysql:terminate(kill, state).