TestSqlStatementExecution.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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. EXPECT(table_or_error.value());
  74. }
  75. TEST_CASE(insert_into_table)
  76. {
  77. ScopeGuard guard([]() { unlink(db_name); });
  78. auto database = SQL::Database::construct(db_name);
  79. EXPECT(!database->open().is_error());
  80. create_table(database);
  81. auto result = execute(database, "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test', 42 );");
  82. EXPECT(result.size() == 1);
  83. auto table_or_error = database->get_table("TESTSCHEMA", "TESTTABLE");
  84. EXPECT(!table_or_error.is_error());
  85. auto table = table_or_error.value();
  86. int count = 0;
  87. auto rows_or_error = database->select_all(*table);
  88. EXPECT(!rows_or_error.is_error());
  89. for (auto& row : rows_or_error.value()) {
  90. EXPECT_EQ(row["TEXTCOLUMN"].to_string(), "Test");
  91. EXPECT_EQ(row["INTCOLUMN"].to_int().value(), 42);
  92. count++;
  93. }
  94. EXPECT_EQ(count, 1);
  95. }
  96. TEST_CASE(insert_into_table_wrong_data_types)
  97. {
  98. ScopeGuard guard([]() { unlink(db_name); });
  99. auto database = SQL::Database::construct(db_name);
  100. EXPECT(!database->open().is_error());
  101. create_table(database);
  102. auto result = try_execute(database, "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES (43, 'Test_2');");
  103. EXPECT(result.is_error());
  104. EXPECT(result.release_error().error() == SQL::SQLErrorCode::InvalidValueType);
  105. }
  106. TEST_CASE(insert_into_table_multiple_tuples_wrong_data_types)
  107. {
  108. ScopeGuard guard([]() { unlink(db_name); });
  109. auto database = SQL::Database::construct(db_name);
  110. EXPECT(!database->open().is_error());
  111. create_table(database);
  112. auto result = try_execute(database, "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ('Test_1', 42), (43, 'Test_2');");
  113. EXPECT(result.is_error());
  114. EXPECT(result.release_error().error() == SQL::SQLErrorCode::InvalidValueType);
  115. }
  116. TEST_CASE(insert_wrong_number_of_values)
  117. {
  118. ScopeGuard guard([]() { unlink(db_name); });
  119. auto database = SQL::Database::construct(db_name);
  120. EXPECT(!database->open().is_error());
  121. create_table(database);
  122. auto result = try_execute(database, "INSERT INTO TestSchema.TestTable VALUES ( 42 );");
  123. EXPECT(result.is_error());
  124. EXPECT(result.release_error().error() == SQL::SQLErrorCode::InvalidNumberOfValues);
  125. }
  126. TEST_CASE(insert_identifier_as_value)
  127. {
  128. ScopeGuard guard([]() { unlink(db_name); });
  129. auto database = SQL::Database::construct(db_name);
  130. EXPECT(!database->open().is_error());
  131. create_table(database);
  132. auto result = try_execute(database, "INSERT INTO TestSchema.TestTable VALUES ( identifier, 42 );");
  133. EXPECT(result.is_error());
  134. EXPECT(result.release_error().error() == SQL::SQLErrorCode::SyntaxError);
  135. }
  136. TEST_CASE(insert_quoted_identifier_as_value)
  137. {
  138. ScopeGuard guard([]() { unlink(db_name); });
  139. auto database = SQL::Database::construct(db_name);
  140. EXPECT(!database->open().is_error());
  141. create_table(database);
  142. auto result = try_execute(database, "INSERT INTO TestSchema.TestTable VALUES ( \"QuotedIdentifier\", 42 );");
  143. EXPECT(result.is_error());
  144. EXPECT(result.release_error().error() == SQL::SQLErrorCode::SyntaxError);
  145. }
  146. TEST_CASE(insert_without_column_names)
  147. {
  148. ScopeGuard guard([]() { unlink(db_name); });
  149. auto database = SQL::Database::construct(db_name);
  150. EXPECT(!database->open().is_error());
  151. create_table(database);
  152. auto result = execute(database, "INSERT INTO TestSchema.TestTable VALUES ('Test_1', 42), ('Test_2', 43);");
  153. EXPECT(result.size() == 2);
  154. auto table_or_error = database->get_table("TESTSCHEMA", "TESTTABLE");
  155. EXPECT(!table_or_error.is_error());
  156. auto rows_or_error = database->select_all(*(table_or_error.value()));
  157. EXPECT(!rows_or_error.is_error());
  158. EXPECT_EQ(rows_or_error.value().size(), 2u);
  159. }
  160. TEST_CASE(select_from_empty_table)
  161. {
  162. ScopeGuard guard([]() { unlink(db_name); });
  163. auto database = SQL::Database::construct(db_name);
  164. EXPECT(!database->open().is_error());
  165. create_table(database);
  166. auto result = execute(database, "SELECT * FROM TestSchema.TestTable;");
  167. EXPECT(result.is_empty());
  168. }
  169. TEST_CASE(select_from_table)
  170. {
  171. ScopeGuard guard([]() { unlink(db_name); });
  172. auto database = SQL::Database::construct(db_name);
  173. EXPECT(!database->open().is_error());
  174. create_table(database);
  175. auto result = execute(database,
  176. "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES "
  177. "( 'Test_1', 42 ), "
  178. "( 'Test_2', 43 ), "
  179. "( 'Test_3', 44 ), "
  180. "( 'Test_4', 45 ), "
  181. "( 'Test_5', 46 );");
  182. EXPECT(result.size() == 5);
  183. result = execute(database, "SELECT * FROM TestSchema.TestTable;");
  184. EXPECT_EQ(result.size(), 5u);
  185. }
  186. TEST_CASE(select_with_column_names)
  187. {
  188. ScopeGuard guard([]() { unlink(db_name); });
  189. auto database = SQL::Database::construct(db_name);
  190. EXPECT(!database->open().is_error());
  191. create_table(database);
  192. auto result = execute(database,
  193. "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES "
  194. "( 'Test_1', 42 ), "
  195. "( 'Test_2', 43 ), "
  196. "( 'Test_3', 44 ), "
  197. "( 'Test_4', 45 ), "
  198. "( 'Test_5', 46 );");
  199. EXPECT(result.size() == 5);
  200. result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable;");
  201. EXPECT_EQ(result.size(), 5u);
  202. EXPECT_EQ(result[0].row.size(), 1u);
  203. }
  204. TEST_CASE(select_with_nonexisting_column_name)
  205. {
  206. ScopeGuard guard([]() { unlink(db_name); });
  207. auto database = SQL::Database::construct(db_name);
  208. EXPECT(!database->open().is_error());
  209. create_table(database);
  210. auto result = execute(database,
  211. "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES "
  212. "( 'Test_1', 42 ), "
  213. "( 'Test_2', 43 ), "
  214. "( 'Test_3', 44 ), "
  215. "( 'Test_4', 45 ), "
  216. "( 'Test_5', 46 );");
  217. EXPECT(result.size() == 5);
  218. auto insert_result = try_execute(database, "SELECT Bogus FROM TestSchema.TestTable;");
  219. EXPECT(insert_result.is_error());
  220. EXPECT(insert_result.release_error().error() == SQL::SQLErrorCode::ColumnDoesNotExist);
  221. }
  222. TEST_CASE(select_with_where)
  223. {
  224. ScopeGuard guard([]() { unlink(db_name); });
  225. auto database = SQL::Database::construct(db_name);
  226. EXPECT(!database->open().is_error());
  227. create_table(database);
  228. auto result = execute(database,
  229. "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES "
  230. "( 'Test_1', 42 ), "
  231. "( 'Test_2', 43 ), "
  232. "( 'Test_3', 44 ), "
  233. "( 'Test_4', 45 ), "
  234. "( 'Test_5', 46 );");
  235. EXPECT(result.size() == 5);
  236. result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable WHERE IntColumn > 44;");
  237. EXPECT_EQ(result.size(), 2u);
  238. for (auto& row : result) {
  239. EXPECT(row.row[1].to_int().value() > 44);
  240. }
  241. }
  242. TEST_CASE(select_cross_join)
  243. {
  244. ScopeGuard guard([]() { unlink(db_name); });
  245. auto database = SQL::Database::construct(db_name);
  246. EXPECT(!database->open().is_error());
  247. create_two_tables(database);
  248. auto result = execute(database,
  249. "INSERT INTO TestSchema.TestTable1 ( TextColumn1, IntColumn ) VALUES "
  250. "( 'Test_1', 42 ), "
  251. "( 'Test_2', 43 ), "
  252. "( 'Test_3', 44 ), "
  253. "( 'Test_4', 45 ), "
  254. "( 'Test_5', 46 );");
  255. EXPECT(result.size() == 5);
  256. result = execute(database,
  257. "INSERT INTO TestSchema.TestTable2 ( TextColumn2, IntColumn ) VALUES "
  258. "( 'Test_10', 40 ), "
  259. "( 'Test_11', 41 ), "
  260. "( 'Test_12', 42 ), "
  261. "( 'Test_13', 47 ), "
  262. "( 'Test_14', 48 );");
  263. EXPECT(result.size() == 5);
  264. result = execute(database, "SELECT * FROM TestSchema.TestTable1, TestSchema.TestTable2;");
  265. EXPECT_EQ(result.size(), 25u);
  266. for (auto& row : result) {
  267. EXPECT(row.row.size() == 4);
  268. EXPECT(row.row[1].to_int().value() >= 42);
  269. EXPECT(row.row[1].to_int().value() <= 46);
  270. EXPECT(row.row[3].to_int().value() >= 40);
  271. EXPECT(row.row[3].to_int().value() <= 48);
  272. }
  273. }
  274. TEST_CASE(select_inner_join)
  275. {
  276. ScopeGuard guard([]() { unlink(db_name); });
  277. auto database = SQL::Database::construct(db_name);
  278. EXPECT(!database->open().is_error());
  279. create_two_tables(database);
  280. auto result = execute(database,
  281. "INSERT INTO TestSchema.TestTable1 ( TextColumn1, IntColumn ) VALUES "
  282. "( 'Test_1', 42 ), "
  283. "( 'Test_2', 43 ), "
  284. "( 'Test_3', 44 ), "
  285. "( 'Test_4', 45 ), "
  286. "( 'Test_5', 46 );");
  287. EXPECT(result.size() == 5);
  288. result = execute(database,
  289. "INSERT INTO TestSchema.TestTable2 ( TextColumn2, IntColumn ) VALUES "
  290. "( 'Test_10', 40 ), "
  291. "( 'Test_11', 41 ), "
  292. "( 'Test_12', 42 ), "
  293. "( 'Test_13', 47 ), "
  294. "( 'Test_14', 48 );");
  295. EXPECT(result.size() == 5);
  296. result = execute(database,
  297. "SELECT TestTable1.IntColumn, TextColumn1, TextColumn2 "
  298. "FROM TestSchema.TestTable1, TestSchema.TestTable2 "
  299. "WHERE TestTable1.IntColumn = TestTable2.IntColumn;");
  300. EXPECT_EQ(result.size(), 1u);
  301. EXPECT_EQ(result[0].row.size(), 3u);
  302. EXPECT_EQ(result[0].row[0].to_int().value(), 42);
  303. EXPECT_EQ(result[0].row[1].to_string(), "Test_1");
  304. EXPECT_EQ(result[0].row[2].to_string(), "Test_12");
  305. }
  306. TEST_CASE(select_with_like)
  307. {
  308. ScopeGuard guard([]() { unlink(db_name); });
  309. auto database = SQL::Database::construct(db_name);
  310. EXPECT(!database->open().is_error());
  311. create_table(database);
  312. auto result = execute(database,
  313. "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES "
  314. "( 'Test+1', 42 ), "
  315. "( 'Test+2', 43 ), "
  316. "( 'Test+3', 44 ), "
  317. "( 'Test+4', 45 ), "
  318. "( 'Test+5', 46 ), "
  319. "( 'Another+Test_6', 47 );");
  320. EXPECT(result.size() == 6);
  321. // Simple match
  322. result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn LIKE 'Test+1';");
  323. EXPECT_EQ(result.size(), 1u);
  324. // Use % to match most rows
  325. result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn LIKE 'T%';");
  326. EXPECT_EQ(result.size(), 5u);
  327. // Same as above but invert the match
  328. result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn NOT LIKE 'T%';");
  329. EXPECT_EQ(result.size(), 1u);
  330. // Use _ and % to match all rows
  331. result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn LIKE '%e_t%';");
  332. EXPECT_EQ(result.size(), 6u);
  333. // Use escape to match a single row. The escape character happens to be a
  334. // Regex metacharacter, let's make sure we don't get confused by that.
  335. result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn LIKE '%Test^_%' ESCAPE '^';");
  336. EXPECT_EQ(result.size(), 1u);
  337. // Same as above but escape the escape character happens to be a SQL
  338. // metacharacter - we want to make sure it's treated as an escape.
  339. result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn LIKE '%Test__%' ESCAPE '_';");
  340. EXPECT_EQ(result.size(), 1u);
  341. // (Unnecessarily) escaping a character that happens to be a Regex
  342. // metacharacter should have no effect.
  343. result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn LIKE 'Test:+_' ESCAPE ':';");
  344. EXPECT_EQ(result.size(), 5u);
  345. // Make sure we error out if the ESCAPE is empty
  346. auto select_result = try_execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn LIKE '%' ESCAPE '';");
  347. EXPECT(select_result.is_error());
  348. EXPECT(select_result.release_error().error() == SQL::SQLErrorCode::SyntaxError);
  349. // Make sure we error out if the ESCAPE has more than a single character
  350. select_result = try_execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn LIKE '%' ESCAPE 'whf';");
  351. EXPECT(select_result.is_error());
  352. EXPECT(select_result.release_error().error() == SQL::SQLErrorCode::SyntaxError);
  353. }
  354. TEST_CASE(select_with_order)
  355. {
  356. ScopeGuard guard([]() { unlink(db_name); });
  357. auto database = SQL::Database::construct(db_name);
  358. EXPECT(!database->open().is_error());
  359. create_table(database);
  360. auto result = execute(database,
  361. "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES "
  362. "( 'Test_5', 44 ), "
  363. "( 'Test_2', 42 ), "
  364. "( 'Test_1', 47 ), "
  365. "( 'Test_3', 40 ), "
  366. "( 'Test_4', 41 );");
  367. EXPECT(result.size() == 5);
  368. result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable ORDER BY IntColumn;");
  369. EXPECT_EQ(result.size(), 5u);
  370. EXPECT_EQ(result[0].row[1].to_int().value(), 40);
  371. EXPECT_EQ(result[1].row[1].to_int().value(), 41);
  372. EXPECT_EQ(result[2].row[1].to_int().value(), 42);
  373. EXPECT_EQ(result[3].row[1].to_int().value(), 44);
  374. EXPECT_EQ(result[4].row[1].to_int().value(), 47);
  375. result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable ORDER BY TextColumn;");
  376. EXPECT_EQ(result.size(), 5u);
  377. EXPECT_EQ(result[0].row[0].to_string(), "Test_1");
  378. EXPECT_EQ(result[1].row[0].to_string(), "Test_2");
  379. EXPECT_EQ(result[2].row[0].to_string(), "Test_3");
  380. EXPECT_EQ(result[3].row[0].to_string(), "Test_4");
  381. EXPECT_EQ(result[4].row[0].to_string(), "Test_5");
  382. }
  383. TEST_CASE(select_with_regexp)
  384. {
  385. ScopeGuard guard([]() { unlink(db_name); });
  386. auto database = SQL::Database::construct(db_name);
  387. EXPECT(!database->open().is_error());
  388. create_table(database);
  389. auto result = execute(database,
  390. "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES "
  391. "( 'Test+1', 42 ), "
  392. "( 'Pröv+2', 43 ), "
  393. "( 'Test(3)', 44 ), "
  394. "( 'Test[4]', 45 ), "
  395. "( 'Test+5', 46 ), "
  396. "( 'Another-Test_6', 47 );");
  397. EXPECT(result.size() == 6);
  398. // Simple match
  399. result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn REGEXP 'Test\\+1';");
  400. EXPECT_EQ(result.size(), 1u);
  401. // Match all
  402. result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn REGEXP '.*';");
  403. EXPECT_EQ(result.size(), 6u);
  404. // Match with wildcards
  405. result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn REGEXP '^Test.+';");
  406. EXPECT_EQ(result.size(), 4u);
  407. // Match with case insensitive basic Latin and case sensitive Swedish ö
  408. // FIXME: If LibRegex is changed to support case insensitive matches of Unicode characters
  409. // This test should be updated and changed to match 'PRÖV'.
  410. result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn REGEXP 'PRöV.*';");
  411. EXPECT_EQ(result.size(), 1u);
  412. }
  413. TEST_CASE(handle_regexp_errors)
  414. {
  415. ScopeGuard guard([]() { unlink(db_name); });
  416. auto database = SQL::Database::construct(db_name);
  417. EXPECT(!database->open().is_error());
  418. create_table(database);
  419. auto result = execute(database,
  420. "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES "
  421. "( 'Test', 0 );");
  422. EXPECT(result.size() == 1);
  423. // Malformed regex, unmatched square bracket
  424. auto select_result = try_execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn REGEXP 'Test\\+[0-9.*';");
  425. EXPECT(select_result.is_error());
  426. }
  427. TEST_CASE(select_with_order_two_columns)
  428. {
  429. ScopeGuard guard([]() { unlink(db_name); });
  430. auto database = SQL::Database::construct(db_name);
  431. EXPECT(!database->open().is_error());
  432. create_table(database);
  433. auto result = execute(database,
  434. "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES "
  435. "( 'Test_5', 44 ), "
  436. "( 'Test_2', 42 ), "
  437. "( 'Test_1', 47 ), "
  438. "( 'Test_2', 40 ), "
  439. "( 'Test_4', 41 );");
  440. EXPECT(result.size() == 5);
  441. result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable ORDER BY TextColumn, IntColumn;");
  442. EXPECT_EQ(result.size(), 5u);
  443. EXPECT_EQ(result[0].row[0].to_string(), "Test_1");
  444. EXPECT_EQ(result[0].row[1].to_int().value(), 47);
  445. EXPECT_EQ(result[1].row[0].to_string(), "Test_2");
  446. EXPECT_EQ(result[1].row[1].to_int().value(), 40);
  447. EXPECT_EQ(result[2].row[0].to_string(), "Test_2");
  448. EXPECT_EQ(result[2].row[1].to_int().value(), 42);
  449. EXPECT_EQ(result[3].row[0].to_string(), "Test_4");
  450. EXPECT_EQ(result[3].row[1].to_int().value(), 41);
  451. EXPECT_EQ(result[4].row[0].to_string(), "Test_5");
  452. EXPECT_EQ(result[4].row[1].to_int().value(), 44);
  453. }
  454. TEST_CASE(select_with_order_by_column_not_in_result)
  455. {
  456. ScopeGuard guard([]() { unlink(db_name); });
  457. auto database = SQL::Database::construct(db_name);
  458. EXPECT(!database->open().is_error());
  459. create_table(database);
  460. auto result = execute(database,
  461. "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES "
  462. "( 'Test_5', 44 ), "
  463. "( 'Test_2', 42 ), "
  464. "( 'Test_1', 47 ), "
  465. "( 'Test_3', 40 ), "
  466. "( 'Test_4', 41 );");
  467. EXPECT(result.size() == 5);
  468. result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable ORDER BY IntColumn;");
  469. EXPECT_EQ(result.size(), 5u);
  470. EXPECT_EQ(result[0].row[0].to_string(), "Test_3");
  471. EXPECT_EQ(result[1].row[0].to_string(), "Test_4");
  472. EXPECT_EQ(result[2].row[0].to_string(), "Test_2");
  473. EXPECT_EQ(result[3].row[0].to_string(), "Test_5");
  474. EXPECT_EQ(result[4].row[0].to_string(), "Test_1");
  475. }
  476. TEST_CASE(select_with_limit)
  477. {
  478. ScopeGuard guard([]() { unlink(db_name); });
  479. auto database = SQL::Database::construct(db_name);
  480. EXPECT(!database->open().is_error());
  481. create_table(database);
  482. for (auto count = 0; count < 100; count++) {
  483. auto result = execute(database,
  484. String::formatted("INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test_{}', {} );", count, count));
  485. EXPECT(result.size() == 1);
  486. }
  487. auto result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable LIMIT 10;");
  488. auto rows = result;
  489. EXPECT_EQ(rows.size(), 10u);
  490. }
  491. TEST_CASE(select_with_limit_and_offset)
  492. {
  493. ScopeGuard guard([]() { unlink(db_name); });
  494. auto database = SQL::Database::construct(db_name);
  495. EXPECT(!database->open().is_error());
  496. create_table(database);
  497. for (auto count = 0; count < 100; count++) {
  498. auto result = execute(database,
  499. String::formatted("INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test_{}', {} );", count, count));
  500. EXPECT(result.size() == 1);
  501. }
  502. auto result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable LIMIT 10 OFFSET 10;");
  503. EXPECT_EQ(result.size(), 10u);
  504. }
  505. TEST_CASE(select_with_order_limit_and_offset)
  506. {
  507. ScopeGuard guard([]() { unlink(db_name); });
  508. auto database = SQL::Database::construct(db_name);
  509. EXPECT(!database->open().is_error());
  510. create_table(database);
  511. for (auto count = 0; count < 100; count++) {
  512. auto result = execute(database,
  513. String::formatted("INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test_{}', {} );", count, count));
  514. EXPECT(result.size() == 1);
  515. }
  516. auto result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable ORDER BY IntColumn LIMIT 10 OFFSET 10;");
  517. EXPECT_EQ(result.size(), 10u);
  518. EXPECT_EQ(result[0].row[1].to_int().value(), 10);
  519. EXPECT_EQ(result[1].row[1].to_int().value(), 11);
  520. EXPECT_EQ(result[2].row[1].to_int().value(), 12);
  521. EXPECT_EQ(result[3].row[1].to_int().value(), 13);
  522. EXPECT_EQ(result[4].row[1].to_int().value(), 14);
  523. EXPECT_EQ(result[5].row[1].to_int().value(), 15);
  524. EXPECT_EQ(result[6].row[1].to_int().value(), 16);
  525. EXPECT_EQ(result[7].row[1].to_int().value(), 17);
  526. EXPECT_EQ(result[8].row[1].to_int().value(), 18);
  527. EXPECT_EQ(result[9].row[1].to_int().value(), 19);
  528. }
  529. TEST_CASE(select_with_limit_out_of_bounds)
  530. {
  531. ScopeGuard guard([]() { unlink(db_name); });
  532. auto database = SQL::Database::construct(db_name);
  533. EXPECT(!database->open().is_error());
  534. create_table(database);
  535. for (auto count = 0; count < 100; count++) {
  536. auto result = execute(database,
  537. String::formatted("INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test_{}', {} );", count, count));
  538. EXPECT(result.size() == 1);
  539. }
  540. auto result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable LIMIT 500;");
  541. EXPECT_EQ(result.size(), 100u);
  542. }
  543. TEST_CASE(select_with_offset_out_of_bounds)
  544. {
  545. ScopeGuard guard([]() { unlink(db_name); });
  546. auto database = SQL::Database::construct(db_name);
  547. EXPECT(!database->open().is_error());
  548. create_table(database);
  549. for (auto count = 0; count < 100; count++) {
  550. auto result = execute(database,
  551. String::formatted("INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test_{}', {} );", count, count));
  552. EXPECT(result.size() == 1);
  553. }
  554. auto result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable LIMIT 10 OFFSET 200;");
  555. EXPECT_EQ(result.size(), 0u);
  556. }
  557. TEST_CASE(describe_table)
  558. {
  559. ScopeGuard guard([]() { unlink(db_name); });
  560. auto database = SQL::Database::construct(db_name);
  561. EXPECT(!database->open().is_error());
  562. create_table(database);
  563. auto result = execute(database, "DESCRIBE TABLE TestSchema.TestTable;");
  564. EXPECT_EQ(result.size(), 2u);
  565. EXPECT_EQ(result[0].row[0].to_string(), "TEXTCOLUMN");
  566. EXPECT_EQ(result[0].row[1].to_string(), "text");
  567. EXPECT_EQ(result[1].row[0].to_string(), "INTCOLUMN");
  568. EXPECT_EQ(result[1].row[1].to_string(), "int");
  569. }
  570. TEST_CASE(binary_operator_execution)
  571. {
  572. ScopeGuard guard([]() { unlink(db_name); });
  573. auto database = SQL::Database::construct(db_name);
  574. EXPECT(!database->open().is_error());
  575. create_table(database);
  576. for (auto count = 0; count < 10; ++count) {
  577. auto result = execute(database, String::formatted("INSERT INTO TestSchema.TestTable VALUES ( 'T{}', {} );", count, count));
  578. EXPECT_EQ(result.size(), 1u);
  579. }
  580. auto compare_result = [](SQL::ResultSet const& result, Vector<int> const& expected) {
  581. EXPECT_EQ(result.command(), SQL::SQLCommand::Select);
  582. EXPECT_EQ(result.size(), expected.size());
  583. Vector<int> result_values;
  584. result_values.ensure_capacity(result.size());
  585. for (size_t i = 0; i < result.size(); ++i) {
  586. auto const& result_row = result.at(i).row;
  587. EXPECT_EQ(result_row.size(), 1u);
  588. auto result_column = result_row[0].to_int();
  589. result_values.append(result_column.value());
  590. }
  591. quick_sort(result_values);
  592. EXPECT_EQ(result_values, expected);
  593. };
  594. auto result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn + 1) < 5);");
  595. compare_result(result, { 0, 1, 2, 3 });
  596. result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn + 1) <= 5);");
  597. compare_result(result, { 0, 1, 2, 3, 4 });
  598. result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn - 1) > 4);");
  599. compare_result(result, { 6, 7, 8, 9 });
  600. result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn - 1) >= 4);");
  601. compare_result(result, { 5, 6, 7, 8, 9 });
  602. result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn * 2) < 10);");
  603. compare_result(result, { 0, 1, 2, 3, 4 });
  604. result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn * 2) <= 10);");
  605. compare_result(result, { 0, 1, 2, 3, 4, 5 });
  606. result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn / 3) > 2);");
  607. compare_result(result, { 7, 8, 9 });
  608. result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn / 3) >= 2);");
  609. compare_result(result, { 6, 7, 8, 9 });
  610. result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn % 2) = 0);");
  611. compare_result(result, { 0, 2, 4, 6, 8 });
  612. result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn % 2) = 1);");
  613. compare_result(result, { 1, 3, 5, 7, 9 });
  614. result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((1 << IntColumn) <= 32);");
  615. compare_result(result, { 0, 1, 2, 3, 4, 5 });
  616. result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((1024 >> IntColumn) >= 32);");
  617. compare_result(result, { 0, 1, 2, 3, 4, 5 });
  618. result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn | 1) != IntColumn);");
  619. compare_result(result, { 0, 2, 4, 6, 8 });
  620. result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn & 1) = 1);");
  621. compare_result(result, { 1, 3, 5, 7, 9 });
  622. }
  623. TEST_CASE(binary_operator_failure)
  624. {
  625. ScopeGuard guard([]() { unlink(db_name); });
  626. auto database = SQL::Database::construct(db_name);
  627. EXPECT(!database->open().is_error());
  628. create_table(database);
  629. for (auto count = 0; count < 10; ++count) {
  630. auto result = execute(database, String::formatted("INSERT INTO TestSchema.TestTable VALUES ( 'T{}', {} );", count, count));
  631. EXPECT_EQ(result.size(), 1u);
  632. }
  633. auto expect_failure = [](auto result, auto op) {
  634. EXPECT(result.is_error());
  635. auto error = result.release_error();
  636. EXPECT_EQ(error.error(), SQL::SQLErrorCode::NumericOperatorTypeMismatch);
  637. auto message = String::formatted("NumericOperatorTypeMismatch: Cannot apply '{}' operator to non-numeric operands", op);
  638. EXPECT_EQ(error.error_string(), message);
  639. };
  640. auto 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), '/');
  648. result = try_execute(database, "SELECT * FROM TestSchema.TestTable WHERE ((IntColumn % TextColumn) < 5);");
  649. expect_failure(move(result), '%');
  650. result = try_execute(database, "SELECT * FROM TestSchema.TestTable WHERE ((IntColumn << TextColumn) < 5);");
  651. expect_failure(move(result), "<<"sv);
  652. result = try_execute(database, "SELECT * FROM TestSchema.TestTable WHERE ((IntColumn >> TextColumn) < 5);");
  653. expect_failure(move(result), ">>"sv);
  654. result = try_execute(database, "SELECT * FROM TestSchema.TestTable WHERE ((IntColumn | TextColumn) < 5);");
  655. expect_failure(move(result), '|');
  656. result = try_execute(database, "SELECT * FROM TestSchema.TestTable WHERE ((IntColumn & TextColumn) < 5);");
  657. expect_failure(move(result), '&');
  658. }
  659. TEST_CASE(describe_large_table_after_persist)
  660. {
  661. ScopeGuard guard([]() { unlink(db_name); });
  662. {
  663. auto database = SQL::Database::construct(db_name);
  664. EXPECT(!database->open().is_error());
  665. 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 );");
  666. EXPECT_EQ(result.command(), SQL::SQLCommand::Create);
  667. }
  668. {
  669. auto database = SQL::Database::construct(db_name);
  670. EXPECT(!database->open().is_error());
  671. auto result = execute(database, "DESCRIBE TABLE Cookies;");
  672. EXPECT_EQ(result.size(), 12u);
  673. }
  674. }
  675. }