TestSqlStatementExecution.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. /*
  2. * Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
  3. * Copyright (c) 2021, Mahmoud Mandour <ma.mandourr@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <unistd.h>
  8. #include <AK/QuickSort.h>
  9. #include <AK/ScopeGuard.h>
  10. #include <LibSQL/AST/Parser.h>
  11. #include <LibSQL/Database.h>
  12. #include <LibSQL/Result.h>
  13. #include <LibSQL/ResultSet.h>
  14. #include <LibSQL/Row.h>
  15. #include <LibSQL/Value.h>
  16. #include <LibTest/TestCase.h>
  17. namespace {
  18. constexpr char const* db_name = "/tmp/test.db";
  19. SQL::ResultOr<SQL::ResultSet> try_execute(NonnullRefPtr<SQL::Database> database, String const& sql)
  20. {
  21. auto parser = SQL::AST::Parser(SQL::AST::Lexer(sql));
  22. auto statement = parser.next_statement();
  23. EXPECT(!parser.has_errors());
  24. if (parser.has_errors())
  25. outln("{}", parser.errors()[0].to_string());
  26. return statement->execute(move(database));
  27. }
  28. SQL::ResultSet execute(NonnullRefPtr<SQL::Database> database, String const& sql)
  29. {
  30. auto result = try_execute(move(database), sql);
  31. if (result.is_error()) {
  32. outln("{}", result.release_error().error_string());
  33. VERIFY_NOT_REACHED();
  34. }
  35. return result.release_value();
  36. }
  37. void create_schema(NonnullRefPtr<SQL::Database> database)
  38. {
  39. auto result = execute(database, "CREATE SCHEMA TestSchema;");
  40. EXPECT_EQ(result.command(), SQL::SQLCommand::Create);
  41. }
  42. void create_table(NonnullRefPtr<SQL::Database> database)
  43. {
  44. create_schema(database);
  45. auto result = execute(database, "CREATE TABLE TestSchema.TestTable ( TextColumn text, IntColumn integer );");
  46. EXPECT_EQ(result.command(), SQL::SQLCommand::Create);
  47. }
  48. void create_two_tables(NonnullRefPtr<SQL::Database> database)
  49. {
  50. create_schema(database);
  51. auto result = execute(database, "CREATE TABLE TestSchema.TestTable1 ( TextColumn1 text, IntColumn integer );");
  52. EXPECT_EQ(result.command(), SQL::SQLCommand::Create);
  53. result = execute(database, "CREATE TABLE TestSchema.TestTable2 ( TextColumn2 text, IntColumn integer );");
  54. EXPECT_EQ(result.command(), SQL::SQLCommand::Create);
  55. }
  56. TEST_CASE(create_schema)
  57. {
  58. ScopeGuard guard([]() { unlink(db_name); });
  59. auto database = SQL::Database::construct(db_name);
  60. EXPECT(!database->open().is_error());
  61. create_schema(database);
  62. auto schema_or_error = database->get_schema("TESTSCHEMA");
  63. EXPECT(!schema_or_error.is_error());
  64. }
  65. TEST_CASE(create_table)
  66. {
  67. ScopeGuard guard([]() { unlink(db_name); });
  68. auto database = SQL::Database::construct(db_name);
  69. EXPECT(!database->open().is_error());
  70. create_table(database);
  71. auto table_or_error = database->get_table("TESTSCHEMA", "TESTTABLE");
  72. EXPECT(!table_or_error.is_error());
  73. }
  74. TEST_CASE(insert_into_table)
  75. {
  76. ScopeGuard guard([]() { unlink(db_name); });
  77. auto database = SQL::Database::construct(db_name);
  78. EXPECT(!database->open().is_error());
  79. create_table(database);
  80. auto result = execute(database, "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test', 42 );");
  81. EXPECT(result.size() == 1);
  82. auto table = MUST(database->get_table("TESTSCHEMA", "TESTTABLE"));
  83. int count = 0;
  84. auto rows_or_error = database->select_all(*table);
  85. EXPECT(!rows_or_error.is_error());
  86. for (auto& row : rows_or_error.value()) {
  87. EXPECT_EQ(row["TEXTCOLUMN"].to_string(), "Test");
  88. EXPECT_EQ(row["INTCOLUMN"].to_int().value(), 42);
  89. count++;
  90. }
  91. EXPECT_EQ(count, 1);
  92. }
  93. TEST_CASE(insert_into_table_wrong_data_types)
  94. {
  95. ScopeGuard guard([]() { unlink(db_name); });
  96. auto database = SQL::Database::construct(db_name);
  97. EXPECT(!database->open().is_error());
  98. create_table(database);
  99. auto result = try_execute(database, "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES (43, 'Test_2');");
  100. EXPECT(result.is_error());
  101. EXPECT(result.release_error().error() == SQL::SQLErrorCode::InvalidValueType);
  102. }
  103. TEST_CASE(insert_into_table_multiple_tuples_wrong_data_types)
  104. {
  105. ScopeGuard guard([]() { unlink(db_name); });
  106. auto database = SQL::Database::construct(db_name);
  107. EXPECT(!database->open().is_error());
  108. create_table(database);
  109. auto result = try_execute(database, "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ('Test_1', 42), (43, 'Test_2');");
  110. EXPECT(result.is_error());
  111. EXPECT(result.release_error().error() == SQL::SQLErrorCode::InvalidValueType);
  112. }
  113. TEST_CASE(insert_wrong_number_of_values)
  114. {
  115. ScopeGuard guard([]() { unlink(db_name); });
  116. auto database = SQL::Database::construct(db_name);
  117. EXPECT(!database->open().is_error());
  118. create_table(database);
  119. auto result = try_execute(database, "INSERT INTO TestSchema.TestTable VALUES ( 42 );");
  120. EXPECT(result.is_error());
  121. EXPECT(result.release_error().error() == SQL::SQLErrorCode::InvalidNumberOfValues);
  122. }
  123. TEST_CASE(insert_identifier_as_value)
  124. {
  125. ScopeGuard guard([]() { unlink(db_name); });
  126. auto database = SQL::Database::construct(db_name);
  127. EXPECT(!database->open().is_error());
  128. create_table(database);
  129. auto result = try_execute(database, "INSERT INTO TestSchema.TestTable VALUES ( identifier, 42 );");
  130. EXPECT(result.is_error());
  131. EXPECT(result.release_error().error() == SQL::SQLErrorCode::SyntaxError);
  132. }
  133. TEST_CASE(insert_quoted_identifier_as_value)
  134. {
  135. ScopeGuard guard([]() { unlink(db_name); });
  136. auto database = SQL::Database::construct(db_name);
  137. EXPECT(!database->open().is_error());
  138. create_table(database);
  139. auto result = try_execute(database, "INSERT INTO TestSchema.TestTable VALUES ( \"QuotedIdentifier\", 42 );");
  140. EXPECT(result.is_error());
  141. EXPECT(result.release_error().error() == SQL::SQLErrorCode::SyntaxError);
  142. }
  143. TEST_CASE(insert_without_column_names)
  144. {
  145. ScopeGuard guard([]() { unlink(db_name); });
  146. auto database = SQL::Database::construct(db_name);
  147. EXPECT(!database->open().is_error());
  148. create_table(database);
  149. auto result = execute(database, "INSERT INTO TestSchema.TestTable VALUES ('Test_1', 42), ('Test_2', 43);");
  150. EXPECT(result.size() == 2);
  151. auto table = MUST(database->get_table("TESTSCHEMA", "TESTTABLE"));
  152. auto rows_or_error = database->select_all(*table);
  153. EXPECT(!rows_or_error.is_error());
  154. EXPECT_EQ(rows_or_error.value().size(), 2u);
  155. }
  156. TEST_CASE(select_from_empty_table)
  157. {
  158. ScopeGuard guard([]() { unlink(db_name); });
  159. auto database = SQL::Database::construct(db_name);
  160. EXPECT(!database->open().is_error());
  161. create_table(database);
  162. auto result = execute(database, "SELECT * FROM TestSchema.TestTable;");
  163. EXPECT(result.is_empty());
  164. }
  165. TEST_CASE(select_from_table)
  166. {
  167. ScopeGuard guard([]() { unlink(db_name); });
  168. auto database = SQL::Database::construct(db_name);
  169. EXPECT(!database->open().is_error());
  170. create_table(database);
  171. auto result = execute(database,
  172. "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES "
  173. "( 'Test_1', 42 ), "
  174. "( 'Test_2', 43 ), "
  175. "( 'Test_3', 44 ), "
  176. "( 'Test_4', 45 ), "
  177. "( 'Test_5', 46 );");
  178. EXPECT(result.size() == 5);
  179. result = execute(database, "SELECT * FROM TestSchema.TestTable;");
  180. EXPECT_EQ(result.size(), 5u);
  181. }
  182. TEST_CASE(select_with_column_names)
  183. {
  184. ScopeGuard guard([]() { unlink(db_name); });
  185. auto database = SQL::Database::construct(db_name);
  186. EXPECT(!database->open().is_error());
  187. create_table(database);
  188. auto result = execute(database,
  189. "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES "
  190. "( 'Test_1', 42 ), "
  191. "( 'Test_2', 43 ), "
  192. "( 'Test_3', 44 ), "
  193. "( 'Test_4', 45 ), "
  194. "( 'Test_5', 46 );");
  195. EXPECT(result.size() == 5);
  196. result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable;");
  197. EXPECT_EQ(result.size(), 5u);
  198. EXPECT_EQ(result[0].row.size(), 1u);
  199. }
  200. TEST_CASE(select_with_nonexisting_column_name)
  201. {
  202. ScopeGuard guard([]() { unlink(db_name); });
  203. auto database = SQL::Database::construct(db_name);
  204. EXPECT(!database->open().is_error());
  205. create_table(database);
  206. auto result = execute(database,
  207. "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES "
  208. "( 'Test_1', 42 ), "
  209. "( 'Test_2', 43 ), "
  210. "( 'Test_3', 44 ), "
  211. "( 'Test_4', 45 ), "
  212. "( 'Test_5', 46 );");
  213. EXPECT(result.size() == 5);
  214. auto insert_result = try_execute(database, "SELECT Bogus FROM TestSchema.TestTable;");
  215. EXPECT(insert_result.is_error());
  216. EXPECT(insert_result.release_error().error() == SQL::SQLErrorCode::ColumnDoesNotExist);
  217. }
  218. TEST_CASE(select_with_where)
  219. {
  220. ScopeGuard guard([]() { unlink(db_name); });
  221. auto database = SQL::Database::construct(db_name);
  222. EXPECT(!database->open().is_error());
  223. create_table(database);
  224. auto result = execute(database,
  225. "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES "
  226. "( 'Test_1', 42 ), "
  227. "( 'Test_2', 43 ), "
  228. "( 'Test_3', 44 ), "
  229. "( 'Test_4', 45 ), "
  230. "( 'Test_5', 46 );");
  231. EXPECT(result.size() == 5);
  232. result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable WHERE IntColumn > 44;");
  233. EXPECT_EQ(result.size(), 2u);
  234. for (auto& row : result) {
  235. EXPECT(row.row[1].to_int().value() > 44);
  236. }
  237. }
  238. TEST_CASE(select_cross_join)
  239. {
  240. ScopeGuard guard([]() { unlink(db_name); });
  241. auto database = SQL::Database::construct(db_name);
  242. EXPECT(!database->open().is_error());
  243. create_two_tables(database);
  244. auto result = execute(database,
  245. "INSERT INTO TestSchema.TestTable1 ( TextColumn1, IntColumn ) VALUES "
  246. "( 'Test_1', 42 ), "
  247. "( 'Test_2', 43 ), "
  248. "( 'Test_3', 44 ), "
  249. "( 'Test_4', 45 ), "
  250. "( 'Test_5', 46 );");
  251. EXPECT(result.size() == 5);
  252. result = execute(database,
  253. "INSERT INTO TestSchema.TestTable2 ( TextColumn2, IntColumn ) VALUES "
  254. "( 'Test_10', 40 ), "
  255. "( 'Test_11', 41 ), "
  256. "( 'Test_12', 42 ), "
  257. "( 'Test_13', 47 ), "
  258. "( 'Test_14', 48 );");
  259. EXPECT(result.size() == 5);
  260. result = execute(database, "SELECT * FROM TestSchema.TestTable1, TestSchema.TestTable2;");
  261. EXPECT_EQ(result.size(), 25u);
  262. for (auto& row : result) {
  263. EXPECT(row.row.size() == 4);
  264. EXPECT(row.row[1].to_int().value() >= 42);
  265. EXPECT(row.row[1].to_int().value() <= 46);
  266. EXPECT(row.row[3].to_int().value() >= 40);
  267. EXPECT(row.row[3].to_int().value() <= 48);
  268. }
  269. }
  270. TEST_CASE(select_inner_join)
  271. {
  272. ScopeGuard guard([]() { unlink(db_name); });
  273. auto database = SQL::Database::construct(db_name);
  274. EXPECT(!database->open().is_error());
  275. create_two_tables(database);
  276. auto result = execute(database,
  277. "INSERT INTO TestSchema.TestTable1 ( TextColumn1, IntColumn ) VALUES "
  278. "( 'Test_1', 42 ), "
  279. "( 'Test_2', 43 ), "
  280. "( 'Test_3', 44 ), "
  281. "( 'Test_4', 45 ), "
  282. "( 'Test_5', 46 );");
  283. EXPECT(result.size() == 5);
  284. result = execute(database,
  285. "INSERT INTO TestSchema.TestTable2 ( TextColumn2, IntColumn ) VALUES "
  286. "( 'Test_10', 40 ), "
  287. "( 'Test_11', 41 ), "
  288. "( 'Test_12', 42 ), "
  289. "( 'Test_13', 47 ), "
  290. "( 'Test_14', 48 );");
  291. EXPECT(result.size() == 5);
  292. result = execute(database,
  293. "SELECT TestTable1.IntColumn, TextColumn1, TextColumn2 "
  294. "FROM TestSchema.TestTable1, TestSchema.TestTable2 "
  295. "WHERE TestTable1.IntColumn = TestTable2.IntColumn;");
  296. EXPECT_EQ(result.size(), 1u);
  297. EXPECT_EQ(result[0].row.size(), 3u);
  298. EXPECT_EQ(result[0].row[0].to_int().value(), 42);
  299. EXPECT_EQ(result[0].row[1].to_string(), "Test_1");
  300. EXPECT_EQ(result[0].row[2].to_string(), "Test_12");
  301. }
  302. TEST_CASE(select_with_like)
  303. {
  304. ScopeGuard guard([]() { unlink(db_name); });
  305. auto database = SQL::Database::construct(db_name);
  306. EXPECT(!database->open().is_error());
  307. create_table(database);
  308. auto result = execute(database,
  309. "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES "
  310. "( 'Test+1', 42 ), "
  311. "( 'Test+2', 43 ), "
  312. "( 'Test+3', 44 ), "
  313. "( 'Test+4', 45 ), "
  314. "( 'Test+5', 46 ), "
  315. "( 'Another+Test_6', 47 );");
  316. EXPECT(result.size() == 6);
  317. // Simple match
  318. result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn LIKE 'Test+1';");
  319. EXPECT_EQ(result.size(), 1u);
  320. // Use % to match most rows
  321. result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn LIKE 'T%';");
  322. EXPECT_EQ(result.size(), 5u);
  323. // Same as above but invert the match
  324. result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn NOT LIKE 'T%';");
  325. EXPECT_EQ(result.size(), 1u);
  326. // Use _ and % to match all rows
  327. result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn LIKE '%e_t%';");
  328. EXPECT_EQ(result.size(), 6u);
  329. // Use escape to match a single row. The escape character happens to be a
  330. // Regex metacharacter, let's make sure we don't get confused by that.
  331. result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn LIKE '%Test^_%' ESCAPE '^';");
  332. EXPECT_EQ(result.size(), 1u);
  333. // Same as above but escape the escape character happens to be a SQL
  334. // metacharacter - we want to make sure it's treated as an escape.
  335. result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn LIKE '%Test__%' ESCAPE '_';");
  336. EXPECT_EQ(result.size(), 1u);
  337. // (Unnecessarily) escaping a character that happens to be a Regex
  338. // metacharacter should have no effect.
  339. result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn LIKE 'Test:+_' ESCAPE ':';");
  340. EXPECT_EQ(result.size(), 5u);
  341. // Make sure we error out if the ESCAPE is empty
  342. auto select_result = try_execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn LIKE '%' ESCAPE '';");
  343. EXPECT(select_result.is_error());
  344. EXPECT(select_result.release_error().error() == SQL::SQLErrorCode::SyntaxError);
  345. // Make sure we error out if the ESCAPE has more than a single character
  346. select_result = try_execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn LIKE '%' ESCAPE 'whf';");
  347. EXPECT(select_result.is_error());
  348. EXPECT(select_result.release_error().error() == SQL::SQLErrorCode::SyntaxError);
  349. }
  350. TEST_CASE(select_with_order)
  351. {
  352. ScopeGuard guard([]() { unlink(db_name); });
  353. auto database = SQL::Database::construct(db_name);
  354. EXPECT(!database->open().is_error());
  355. create_table(database);
  356. auto result = execute(database,
  357. "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES "
  358. "( 'Test_5', 44 ), "
  359. "( 'Test_2', 42 ), "
  360. "( 'Test_1', 47 ), "
  361. "( 'Test_3', 40 ), "
  362. "( 'Test_4', 41 );");
  363. EXPECT(result.size() == 5);
  364. result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable ORDER BY IntColumn;");
  365. EXPECT_EQ(result.size(), 5u);
  366. EXPECT_EQ(result[0].row[1].to_int().value(), 40);
  367. EXPECT_EQ(result[1].row[1].to_int().value(), 41);
  368. EXPECT_EQ(result[2].row[1].to_int().value(), 42);
  369. EXPECT_EQ(result[3].row[1].to_int().value(), 44);
  370. EXPECT_EQ(result[4].row[1].to_int().value(), 47);
  371. result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable ORDER BY TextColumn;");
  372. EXPECT_EQ(result.size(), 5u);
  373. EXPECT_EQ(result[0].row[0].to_string(), "Test_1");
  374. EXPECT_EQ(result[1].row[0].to_string(), "Test_2");
  375. EXPECT_EQ(result[2].row[0].to_string(), "Test_3");
  376. EXPECT_EQ(result[3].row[0].to_string(), "Test_4");
  377. EXPECT_EQ(result[4].row[0].to_string(), "Test_5");
  378. }
  379. TEST_CASE(select_with_regexp)
  380. {
  381. ScopeGuard guard([]() { unlink(db_name); });
  382. auto database = SQL::Database::construct(db_name);
  383. EXPECT(!database->open().is_error());
  384. create_table(database);
  385. auto result = execute(database,
  386. "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES "
  387. "( 'Test+1', 42 ), "
  388. "( 'Pröv+2', 43 ), "
  389. "( 'Test(3)', 44 ), "
  390. "( 'Test[4]', 45 ), "
  391. "( 'Test+5', 46 ), "
  392. "( 'Another-Test_6', 47 );");
  393. EXPECT(result.size() == 6);
  394. // Simple match
  395. result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn REGEXP 'Test\\+1';");
  396. EXPECT_EQ(result.size(), 1u);
  397. // Match all
  398. result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn REGEXP '.*';");
  399. EXPECT_EQ(result.size(), 6u);
  400. // Match with wildcards
  401. result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn REGEXP '^Test.+';");
  402. EXPECT_EQ(result.size(), 4u);
  403. // Match with case insensitive basic Latin and case sensitive Swedish ö
  404. // FIXME: If LibRegex is changed to support case insensitive matches of Unicode characters
  405. // This test should be updated and changed to match 'PRÖV'.
  406. result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn REGEXP 'PRöV.*';");
  407. EXPECT_EQ(result.size(), 1u);
  408. }
  409. TEST_CASE(handle_regexp_errors)
  410. {
  411. ScopeGuard guard([]() { unlink(db_name); });
  412. auto database = SQL::Database::construct(db_name);
  413. EXPECT(!database->open().is_error());
  414. create_table(database);
  415. auto result = execute(database,
  416. "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES "
  417. "( 'Test', 0 );");
  418. EXPECT(result.size() == 1);
  419. // Malformed regex, unmatched square bracket
  420. auto select_result = try_execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn REGEXP 'Test\\+[0-9.*';");
  421. EXPECT(select_result.is_error());
  422. }
  423. TEST_CASE(select_with_order_two_columns)
  424. {
  425. ScopeGuard guard([]() { unlink(db_name); });
  426. auto database = SQL::Database::construct(db_name);
  427. EXPECT(!database->open().is_error());
  428. create_table(database);
  429. auto result = execute(database,
  430. "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES "
  431. "( 'Test_5', 44 ), "
  432. "( 'Test_2', 42 ), "
  433. "( 'Test_1', 47 ), "
  434. "( 'Test_2', 40 ), "
  435. "( 'Test_4', 41 );");
  436. EXPECT(result.size() == 5);
  437. result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable ORDER BY TextColumn, IntColumn;");
  438. EXPECT_EQ(result.size(), 5u);
  439. EXPECT_EQ(result[0].row[0].to_string(), "Test_1");
  440. EXPECT_EQ(result[0].row[1].to_int().value(), 47);
  441. EXPECT_EQ(result[1].row[0].to_string(), "Test_2");
  442. EXPECT_EQ(result[1].row[1].to_int().value(), 40);
  443. EXPECT_EQ(result[2].row[0].to_string(), "Test_2");
  444. EXPECT_EQ(result[2].row[1].to_int().value(), 42);
  445. EXPECT_EQ(result[3].row[0].to_string(), "Test_4");
  446. EXPECT_EQ(result[3].row[1].to_int().value(), 41);
  447. EXPECT_EQ(result[4].row[0].to_string(), "Test_5");
  448. EXPECT_EQ(result[4].row[1].to_int().value(), 44);
  449. }
  450. TEST_CASE(select_with_order_by_column_not_in_result)
  451. {
  452. ScopeGuard guard([]() { unlink(db_name); });
  453. auto database = SQL::Database::construct(db_name);
  454. EXPECT(!database->open().is_error());
  455. create_table(database);
  456. auto result = execute(database,
  457. "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES "
  458. "( 'Test_5', 44 ), "
  459. "( 'Test_2', 42 ), "
  460. "( 'Test_1', 47 ), "
  461. "( 'Test_3', 40 ), "
  462. "( 'Test_4', 41 );");
  463. EXPECT(result.size() == 5);
  464. result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable ORDER BY IntColumn;");
  465. EXPECT_EQ(result.size(), 5u);
  466. EXPECT_EQ(result[0].row[0].to_string(), "Test_3");
  467. EXPECT_EQ(result[1].row[0].to_string(), "Test_4");
  468. EXPECT_EQ(result[2].row[0].to_string(), "Test_2");
  469. EXPECT_EQ(result[3].row[0].to_string(), "Test_5");
  470. EXPECT_EQ(result[4].row[0].to_string(), "Test_1");
  471. }
  472. TEST_CASE(select_with_limit)
  473. {
  474. ScopeGuard guard([]() { unlink(db_name); });
  475. auto database = SQL::Database::construct(db_name);
  476. EXPECT(!database->open().is_error());
  477. create_table(database);
  478. for (auto count = 0; count < 100; count++) {
  479. auto result = execute(database,
  480. String::formatted("INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test_{}', {} );", count, count));
  481. EXPECT(result.size() == 1);
  482. }
  483. auto result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable LIMIT 10;");
  484. auto rows = result;
  485. EXPECT_EQ(rows.size(), 10u);
  486. }
  487. TEST_CASE(select_with_limit_and_offset)
  488. {
  489. ScopeGuard guard([]() { unlink(db_name); });
  490. auto database = SQL::Database::construct(db_name);
  491. EXPECT(!database->open().is_error());
  492. create_table(database);
  493. for (auto count = 0; count < 100; count++) {
  494. auto result = execute(database,
  495. String::formatted("INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test_{}', {} );", count, count));
  496. EXPECT(result.size() == 1);
  497. }
  498. auto result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable LIMIT 10 OFFSET 10;");
  499. EXPECT_EQ(result.size(), 10u);
  500. }
  501. TEST_CASE(select_with_order_limit_and_offset)
  502. {
  503. ScopeGuard guard([]() { unlink(db_name); });
  504. auto database = SQL::Database::construct(db_name);
  505. EXPECT(!database->open().is_error());
  506. create_table(database);
  507. for (auto count = 0; count < 100; count++) {
  508. auto result = execute(database,
  509. String::formatted("INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test_{}', {} );", count, count));
  510. EXPECT(result.size() == 1);
  511. }
  512. auto result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable ORDER BY IntColumn LIMIT 10 OFFSET 10;");
  513. EXPECT_EQ(result.size(), 10u);
  514. EXPECT_EQ(result[0].row[1].to_int().value(), 10);
  515. EXPECT_EQ(result[1].row[1].to_int().value(), 11);
  516. EXPECT_EQ(result[2].row[1].to_int().value(), 12);
  517. EXPECT_EQ(result[3].row[1].to_int().value(), 13);
  518. EXPECT_EQ(result[4].row[1].to_int().value(), 14);
  519. EXPECT_EQ(result[5].row[1].to_int().value(), 15);
  520. EXPECT_EQ(result[6].row[1].to_int().value(), 16);
  521. EXPECT_EQ(result[7].row[1].to_int().value(), 17);
  522. EXPECT_EQ(result[8].row[1].to_int().value(), 18);
  523. EXPECT_EQ(result[9].row[1].to_int().value(), 19);
  524. }
  525. TEST_CASE(select_with_limit_out_of_bounds)
  526. {
  527. ScopeGuard guard([]() { unlink(db_name); });
  528. auto database = SQL::Database::construct(db_name);
  529. EXPECT(!database->open().is_error());
  530. create_table(database);
  531. for (auto count = 0; count < 100; count++) {
  532. auto result = execute(database,
  533. String::formatted("INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test_{}', {} );", count, count));
  534. EXPECT(result.size() == 1);
  535. }
  536. auto result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable LIMIT 500;");
  537. EXPECT_EQ(result.size(), 100u);
  538. }
  539. TEST_CASE(select_with_offset_out_of_bounds)
  540. {
  541. ScopeGuard guard([]() { unlink(db_name); });
  542. auto database = SQL::Database::construct(db_name);
  543. EXPECT(!database->open().is_error());
  544. create_table(database);
  545. for (auto count = 0; count < 100; count++) {
  546. auto result = execute(database,
  547. String::formatted("INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test_{}', {} );", count, count));
  548. EXPECT(result.size() == 1);
  549. }
  550. auto result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable LIMIT 10 OFFSET 200;");
  551. EXPECT_EQ(result.size(), 0u);
  552. }
  553. TEST_CASE(describe_table)
  554. {
  555. ScopeGuard guard([]() { unlink(db_name); });
  556. auto database = SQL::Database::construct(db_name);
  557. EXPECT(!database->open().is_error());
  558. create_table(database);
  559. auto result = execute(database, "DESCRIBE TABLE TestSchema.TestTable;");
  560. EXPECT_EQ(result.size(), 2u);
  561. EXPECT_EQ(result[0].row[0].to_string(), "TEXTCOLUMN");
  562. EXPECT_EQ(result[0].row[1].to_string(), "text");
  563. EXPECT_EQ(result[1].row[0].to_string(), "INTCOLUMN");
  564. EXPECT_EQ(result[1].row[1].to_string(), "int");
  565. }
  566. TEST_CASE(binary_operator_execution)
  567. {
  568. ScopeGuard guard([]() { unlink(db_name); });
  569. auto database = SQL::Database::construct(db_name);
  570. EXPECT(!database->open().is_error());
  571. create_table(database);
  572. for (auto count = 0; count < 10; ++count) {
  573. auto result = execute(database, String::formatted("INSERT INTO TestSchema.TestTable VALUES ( 'T{}', {} );", count, count));
  574. EXPECT_EQ(result.size(), 1u);
  575. }
  576. auto compare_result = [](SQL::ResultSet const& result, Vector<int> const& expected) {
  577. EXPECT_EQ(result.command(), SQL::SQLCommand::Select);
  578. EXPECT_EQ(result.size(), expected.size());
  579. Vector<int> result_values;
  580. result_values.ensure_capacity(result.size());
  581. for (size_t i = 0; i < result.size(); ++i) {
  582. auto const& result_row = result.at(i).row;
  583. EXPECT_EQ(result_row.size(), 1u);
  584. auto result_column = result_row[0].to_int();
  585. result_values.append(result_column.value());
  586. }
  587. quick_sort(result_values);
  588. EXPECT_EQ(result_values, expected);
  589. };
  590. auto result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn + 1) < 5);");
  591. compare_result(result, { 0, 1, 2, 3 });
  592. result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn + 1) <= 5);");
  593. compare_result(result, { 0, 1, 2, 3, 4 });
  594. result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn - 1) > 4);");
  595. compare_result(result, { 6, 7, 8, 9 });
  596. result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn - 1) >= 4);");
  597. compare_result(result, { 5, 6, 7, 8, 9 });
  598. result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn * 2) < 10);");
  599. compare_result(result, { 0, 1, 2, 3, 4 });
  600. result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn * 2) <= 10);");
  601. compare_result(result, { 0, 1, 2, 3, 4, 5 });
  602. result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn / 3) > 2);");
  603. compare_result(result, { 7, 8, 9 });
  604. result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn / 3) >= 2);");
  605. compare_result(result, { 6, 7, 8, 9 });
  606. result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn % 2) = 0);");
  607. compare_result(result, { 0, 2, 4, 6, 8 });
  608. result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn % 2) = 1);");
  609. compare_result(result, { 1, 3, 5, 7, 9 });
  610. result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((1 << IntColumn) <= 32);");
  611. compare_result(result, { 0, 1, 2, 3, 4, 5 });
  612. result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((1024 >> IntColumn) >= 32);");
  613. compare_result(result, { 0, 1, 2, 3, 4, 5 });
  614. result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn | 1) != IntColumn);");
  615. compare_result(result, { 0, 2, 4, 6, 8 });
  616. result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn & 1) = 1);");
  617. compare_result(result, { 1, 3, 5, 7, 9 });
  618. }
  619. TEST_CASE(binary_operator_failure)
  620. {
  621. ScopeGuard guard([]() { unlink(db_name); });
  622. auto database = SQL::Database::construct(db_name);
  623. EXPECT(!database->open().is_error());
  624. create_table(database);
  625. for (auto count = 0; count < 10; ++count) {
  626. auto result = execute(database, String::formatted("INSERT INTO TestSchema.TestTable VALUES ( 'T{}', {} );", count, count));
  627. EXPECT_EQ(result.size(), 1u);
  628. }
  629. auto expect_failure = [](auto result, auto op) {
  630. EXPECT(result.is_error());
  631. auto error = result.release_error();
  632. EXPECT_EQ(error.error(), SQL::SQLErrorCode::NumericOperatorTypeMismatch);
  633. auto message = String::formatted("NumericOperatorTypeMismatch: Cannot apply '{}' operator to non-numeric operands", op);
  634. EXPECT_EQ(error.error_string(), message);
  635. };
  636. auto result = try_execute(database, "SELECT * FROM TestSchema.TestTable WHERE ((IntColumn + TextColumn) < 5);");
  637. expect_failure(move(result), '+');
  638. result = try_execute(database, "SELECT * FROM TestSchema.TestTable WHERE ((IntColumn - TextColumn) < 5);");
  639. expect_failure(move(result), '-');
  640. result = try_execute(database, "SELECT * FROM TestSchema.TestTable WHERE ((IntColumn * TextColumn) < 5);");
  641. expect_failure(move(result), '*');
  642. result = try_execute(database, "SELECT * FROM TestSchema.TestTable WHERE ((IntColumn / TextColumn) < 5);");
  643. expect_failure(move(result), '/');
  644. result = try_execute(database, "SELECT * FROM TestSchema.TestTable WHERE ((IntColumn % TextColumn) < 5);");
  645. expect_failure(move(result), '%');
  646. result = try_execute(database, "SELECT * FROM TestSchema.TestTable WHERE ((IntColumn << TextColumn) < 5);");
  647. expect_failure(move(result), "<<"sv);
  648. result = try_execute(database, "SELECT * FROM TestSchema.TestTable WHERE ((IntColumn >> TextColumn) < 5);");
  649. expect_failure(move(result), ">>"sv);
  650. result = try_execute(database, "SELECT * FROM TestSchema.TestTable WHERE ((IntColumn | TextColumn) < 5);");
  651. expect_failure(move(result), '|');
  652. result = try_execute(database, "SELECT * FROM TestSchema.TestTable WHERE ((IntColumn & TextColumn) < 5);");
  653. expect_failure(move(result), '&');
  654. }
  655. TEST_CASE(describe_large_table_after_persist)
  656. {
  657. ScopeGuard guard([]() { unlink(db_name); });
  658. {
  659. auto database = SQL::Database::construct(db_name);
  660. EXPECT(!database->open().is_error());
  661. auto result = execute(database, "CREATE TABLE Cookies ( name TEXT, value TEXT, same_site INTEGER, creation_time INTEGER, last_access_time INTEGER, expiry_time INTEGER, domain TEXT, path TEXT, secure INTEGER, http_only INTEGER, host_only INTEGER, persistent INTEGER );");
  662. EXPECT_EQ(result.command(), SQL::SQLCommand::Create);
  663. }
  664. {
  665. auto database = SQL::Database::construct(db_name);
  666. EXPECT(!database->open().is_error());
  667. auto result = execute(database, "DESCRIBE TABLE Cookies;");
  668. EXPECT_EQ(result.size(), 12u);
  669. }
  670. }
  671. TEST_CASE(delete_single_row)
  672. {
  673. ScopeGuard guard([]() { unlink(db_name); });
  674. {
  675. auto database = SQL::Database::construct(db_name);
  676. EXPECT(!database->open().is_error());
  677. create_table(database);
  678. for (auto count = 0; count < 10; ++count) {
  679. auto result = execute(database, String::formatted("INSERT INTO TestSchema.TestTable VALUES ( 'T{}', {} );", count, count));
  680. EXPECT_EQ(result.size(), 1u);
  681. }
  682. auto result = execute(database, "SELECT * FROM TestSchema.TestTable;");
  683. EXPECT_EQ(result.size(), 10u);
  684. }
  685. {
  686. auto database = SQL::Database::construct(db_name);
  687. EXPECT(!database->open().is_error());
  688. execute(database, "DELETE FROM TestSchema.TestTable WHERE (IntColumn = 4);");
  689. auto result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable ORDER BY IntColumn;");
  690. EXPECT_EQ(result.size(), 9u);
  691. for (auto i = 0u; i < 4; ++i)
  692. EXPECT_EQ(result[i].row[0], i);
  693. for (auto i = 5u; i < 9; ++i)
  694. EXPECT_EQ(result[i].row[0], i + 1);
  695. }
  696. {
  697. auto database = SQL::Database::construct(db_name);
  698. EXPECT(!database->open().is_error());
  699. auto result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable ORDER BY IntColumn;");
  700. EXPECT_EQ(result.size(), 9u);
  701. for (auto i = 0u; i < 4; ++i)
  702. EXPECT_EQ(result[i].row[0], i);
  703. for (auto i = 5u; i < 9; ++i)
  704. EXPECT_EQ(result[i].row[0], i + 1);
  705. }
  706. }
  707. TEST_CASE(delete_multiple_rows)
  708. {
  709. ScopeGuard guard([]() { unlink(db_name); });
  710. {
  711. auto database = SQL::Database::construct(db_name);
  712. EXPECT(!database->open().is_error());
  713. create_table(database);
  714. for (auto count = 0; count < 10; ++count) {
  715. auto result = execute(database, String::formatted("INSERT INTO TestSchema.TestTable VALUES ( 'T{}', {} );", count, count));
  716. EXPECT_EQ(result.size(), 1u);
  717. }
  718. auto result = execute(database, "SELECT * FROM TestSchema.TestTable;");
  719. EXPECT_EQ(result.size(), 10u);
  720. }
  721. {
  722. auto database = SQL::Database::construct(db_name);
  723. EXPECT(!database->open().is_error());
  724. execute(database, "DELETE FROM TestSchema.TestTable WHERE (IntColumn >= 4);");
  725. auto result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable ORDER BY IntColumn;");
  726. EXPECT_EQ(result.size(), 4u);
  727. for (auto i = 0u; i < result.size(); ++i)
  728. EXPECT_EQ(result[i].row[0], i);
  729. }
  730. {
  731. auto database = SQL::Database::construct(db_name);
  732. EXPECT(!database->open().is_error());
  733. auto result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable ORDER BY IntColumn;");
  734. EXPECT_EQ(result.size(), 4u);
  735. for (auto i = 0u; i < result.size(); ++i)
  736. EXPECT_EQ(result[i].row[0], i);
  737. }
  738. }
  739. TEST_CASE(delete_all_rows)
  740. {
  741. ScopeGuard guard([]() { unlink(db_name); });
  742. {
  743. auto database = SQL::Database::construct(db_name);
  744. EXPECT(!database->open().is_error());
  745. create_table(database);
  746. for (auto count = 0; count < 10; ++count) {
  747. auto result = execute(database, String::formatted("INSERT INTO TestSchema.TestTable VALUES ( 'T{}', {} );", count, count));
  748. EXPECT_EQ(result.size(), 1u);
  749. }
  750. auto result = execute(database, "SELECT * FROM TestSchema.TestTable;");
  751. EXPECT_EQ(result.size(), 10u);
  752. }
  753. {
  754. auto database = SQL::Database::construct(db_name);
  755. EXPECT(!database->open().is_error());
  756. execute(database, "DELETE FROM TestSchema.TestTable;");
  757. auto result = execute(database, "SELECT * FROM TestSchema.TestTable;");
  758. EXPECT(result.is_empty());
  759. }
  760. {
  761. auto database = SQL::Database::construct(db_name);
  762. EXPECT(!database->open().is_error());
  763. auto result = execute(database, "SELECT * FROM TestSchema.TestTable;");
  764. EXPECT(result.is_empty());
  765. }
  766. }
  767. }