mirror of
https://github.com/RGBCube/serenity
synced 2025-05-14 06:24:58 +00:00
LibSQL+SQLServer: Introduce and use ResultOr<ValueType>
The result of a SQL statement execution is either: 1. An error. 2. The list of rows inserted, deleted, selected, etc. (2) is currently represented by a combination of the Result class and the ResultSet list it holds. This worked okay, but issues start to arise when trying to use Result in non-statement contexts (for example, when introducing Result to SQL expression execution). What we really need is for Result to be a thin wrapper that represents both (1) and (2), and to not have any explicit members like a ResultSet. So this commit removes ResultSet from Result, and introduces ResultOr, which is just an alias for AK::ErrorOrr. Statement execution now returns ResultOr<ResultSet> instead of Result. This further opens the door for expression execution to return ResultOr<Value> in the future. Lastly, this moves some other context held by Result over to ResultSet. This includes the row count (which is really just the size of ResultSet) and the command for which the result is for.
This commit is contained in:
parent
6409618413
commit
2397836f8e
15 changed files with 259 additions and 330 deletions
|
@ -11,6 +11,7 @@
|
|||
#include <LibSQL/AST/Parser.h>
|
||||
#include <LibSQL/Database.h>
|
||||
#include <LibSQL/Result.h>
|
||||
#include <LibSQL/ResultSet.h>
|
||||
#include <LibSQL/Row.h>
|
||||
#include <LibSQL/Value.h>
|
||||
#include <LibTest/TestCase.h>
|
||||
|
@ -19,43 +20,46 @@ namespace {
|
|||
|
||||
constexpr const char* db_name = "/tmp/test.db";
|
||||
|
||||
SQL::Result execute(NonnullRefPtr<SQL::Database> database, String const& sql)
|
||||
SQL::ResultOr<SQL::ResultSet> try_execute(NonnullRefPtr<SQL::Database> database, String const& sql)
|
||||
{
|
||||
auto parser = SQL::AST::Parser(SQL::AST::Lexer(sql));
|
||||
auto statement = parser.next_statement();
|
||||
EXPECT(!parser.has_errors());
|
||||
if (parser.has_errors())
|
||||
outln("{}", parser.errors()[0].to_string());
|
||||
auto result = statement->execute(move(database));
|
||||
if (result.is_error())
|
||||
outln("{}", result.error_string());
|
||||
return result;
|
||||
return statement->execute(move(database));
|
||||
}
|
||||
|
||||
SQL::ResultSet execute(NonnullRefPtr<SQL::Database> database, String const& sql)
|
||||
{
|
||||
auto result = try_execute(move(database), sql);
|
||||
if (result.is_error()) {
|
||||
outln("{}", result.release_error().error_string());
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
return result.release_value();
|
||||
}
|
||||
|
||||
void create_schema(NonnullRefPtr<SQL::Database> database)
|
||||
{
|
||||
auto result = execute(database, "CREATE SCHEMA TestSchema;");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.inserted() == 1);
|
||||
EXPECT_EQ(result.command(), SQL::SQLCommand::Create);
|
||||
}
|
||||
|
||||
void create_table(NonnullRefPtr<SQL::Database> database)
|
||||
{
|
||||
create_schema(database);
|
||||
auto result = execute(database, "CREATE TABLE TestSchema.TestTable ( TextColumn text, IntColumn integer );");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.inserted() == 1);
|
||||
EXPECT_EQ(result.command(), SQL::SQLCommand::Create);
|
||||
}
|
||||
|
||||
void create_two_tables(NonnullRefPtr<SQL::Database> database)
|
||||
{
|
||||
create_schema(database);
|
||||
auto result = execute(database, "CREATE TABLE TestSchema.TestTable1 ( TextColumn1 text, IntColumn integer );");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.inserted() == 1);
|
||||
EXPECT_EQ(result.command(), SQL::SQLCommand::Create);
|
||||
result = execute(database, "CREATE TABLE TestSchema.TestTable2 ( TextColumn2 text, IntColumn integer );");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.inserted() == 1);
|
||||
EXPECT_EQ(result.command(), SQL::SQLCommand::Create);
|
||||
}
|
||||
|
||||
TEST_CASE(create_schema)
|
||||
|
@ -87,8 +91,7 @@ TEST_CASE(insert_into_table)
|
|||
EXPECT(!database->open().is_error());
|
||||
create_table(database);
|
||||
auto result = execute(database, "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test', 42 );");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.inserted() == 1);
|
||||
EXPECT(result.size() == 1);
|
||||
|
||||
auto table_or_error = database->get_table("TESTSCHEMA", "TESTTABLE");
|
||||
EXPECT(!table_or_error.is_error());
|
||||
|
@ -111,9 +114,9 @@ TEST_CASE(insert_into_table_wrong_data_types)
|
|||
auto database = SQL::Database::construct(db_name);
|
||||
EXPECT(!database->open().is_error());
|
||||
create_table(database);
|
||||
auto result = execute(database, "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES (43, 'Test_2');");
|
||||
EXPECT(result.inserted() == 0);
|
||||
EXPECT(result.error() == SQL::SQLErrorCode::InvalidValueType);
|
||||
auto result = try_execute(database, "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES (43, 'Test_2');");
|
||||
EXPECT(result.is_error());
|
||||
EXPECT(result.release_error().error() == SQL::SQLErrorCode::InvalidValueType);
|
||||
}
|
||||
|
||||
TEST_CASE(insert_into_table_multiple_tuples_wrong_data_types)
|
||||
|
@ -122,9 +125,9 @@ TEST_CASE(insert_into_table_multiple_tuples_wrong_data_types)
|
|||
auto database = SQL::Database::construct(db_name);
|
||||
EXPECT(!database->open().is_error());
|
||||
create_table(database);
|
||||
auto result = execute(database, "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ('Test_1', 42), (43, 'Test_2');");
|
||||
EXPECT(result.inserted() == 0);
|
||||
EXPECT(result.error() == SQL::SQLErrorCode::InvalidValueType);
|
||||
auto result = try_execute(database, "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ('Test_1', 42), (43, 'Test_2');");
|
||||
EXPECT(result.is_error());
|
||||
EXPECT(result.release_error().error() == SQL::SQLErrorCode::InvalidValueType);
|
||||
}
|
||||
|
||||
TEST_CASE(insert_wrong_number_of_values)
|
||||
|
@ -133,9 +136,9 @@ TEST_CASE(insert_wrong_number_of_values)
|
|||
auto database = SQL::Database::construct(db_name);
|
||||
EXPECT(!database->open().is_error());
|
||||
create_table(database);
|
||||
auto result = execute(database, "INSERT INTO TestSchema.TestTable VALUES ( 42 );");
|
||||
EXPECT(result.error() == SQL::SQLErrorCode::InvalidNumberOfValues);
|
||||
EXPECT(result.inserted() == 0);
|
||||
auto result = try_execute(database, "INSERT INTO TestSchema.TestTable VALUES ( 42 );");
|
||||
EXPECT(result.is_error());
|
||||
EXPECT(result.release_error().error() == SQL::SQLErrorCode::InvalidNumberOfValues);
|
||||
}
|
||||
|
||||
TEST_CASE(insert_identifier_as_value)
|
||||
|
@ -144,9 +147,9 @@ TEST_CASE(insert_identifier_as_value)
|
|||
auto database = SQL::Database::construct(db_name);
|
||||
EXPECT(!database->open().is_error());
|
||||
create_table(database);
|
||||
auto result = execute(database, "INSERT INTO TestSchema.TestTable VALUES ( identifier, 42 );");
|
||||
EXPECT(result.error() == SQL::SQLErrorCode::SyntaxError);
|
||||
EXPECT(result.inserted() == 0);
|
||||
auto result = try_execute(database, "INSERT INTO TestSchema.TestTable VALUES ( identifier, 42 );");
|
||||
EXPECT(result.is_error());
|
||||
EXPECT(result.release_error().error() == SQL::SQLErrorCode::SyntaxError);
|
||||
}
|
||||
|
||||
TEST_CASE(insert_quoted_identifier_as_value)
|
||||
|
@ -155,9 +158,9 @@ TEST_CASE(insert_quoted_identifier_as_value)
|
|||
auto database = SQL::Database::construct(db_name);
|
||||
EXPECT(!database->open().is_error());
|
||||
create_table(database);
|
||||
auto result = execute(database, "INSERT INTO TestSchema.TestTable VALUES ( \"QuotedIdentifier\", 42 );");
|
||||
EXPECT(result.error() == SQL::SQLErrorCode::SyntaxError);
|
||||
EXPECT(result.inserted() == 0);
|
||||
auto result = try_execute(database, "INSERT INTO TestSchema.TestTable VALUES ( \"QuotedIdentifier\", 42 );");
|
||||
EXPECT(result.is_error());
|
||||
EXPECT(result.release_error().error() == SQL::SQLErrorCode::SyntaxError);
|
||||
}
|
||||
|
||||
TEST_CASE(insert_without_column_names)
|
||||
|
@ -167,8 +170,7 @@ TEST_CASE(insert_without_column_names)
|
|||
EXPECT(!database->open().is_error());
|
||||
create_table(database);
|
||||
auto result = execute(database, "INSERT INTO TestSchema.TestTable VALUES ('Test_1', 42), ('Test_2', 43);");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.inserted() == 2);
|
||||
EXPECT(result.size() == 2);
|
||||
|
||||
auto table_or_error = database->get_table("TESTSCHEMA", "TESTTABLE");
|
||||
EXPECT(!table_or_error.is_error());
|
||||
|
@ -184,8 +186,7 @@ TEST_CASE(select_from_empty_table)
|
|||
EXPECT(!database->open().is_error());
|
||||
create_table(database);
|
||||
auto result = execute(database, "SELECT * FROM TestSchema.TestTable;");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(!result.has_results());
|
||||
EXPECT(result.is_empty());
|
||||
}
|
||||
|
||||
TEST_CASE(select_from_table)
|
||||
|
@ -201,12 +202,9 @@ TEST_CASE(select_from_table)
|
|||
"( 'Test_3', 44 ), "
|
||||
"( 'Test_4', 45 ), "
|
||||
"( 'Test_5', 46 );");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.inserted() == 5);
|
||||
EXPECT(result.size() == 5);
|
||||
result = execute(database, "SELECT * FROM TestSchema.TestTable;");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.has_results());
|
||||
EXPECT_EQ(result.results().size(), 5u);
|
||||
EXPECT_EQ(result.size(), 5u);
|
||||
}
|
||||
|
||||
TEST_CASE(select_with_column_names)
|
||||
|
@ -222,13 +220,10 @@ TEST_CASE(select_with_column_names)
|
|||
"( 'Test_3', 44 ), "
|
||||
"( 'Test_4', 45 ), "
|
||||
"( 'Test_5', 46 );");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.inserted() == 5);
|
||||
EXPECT(result.size() == 5);
|
||||
result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable;");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.has_results());
|
||||
EXPECT_EQ(result.results().size(), 5u);
|
||||
EXPECT_EQ(result.results()[0].row.size(), 1u);
|
||||
EXPECT_EQ(result.size(), 5u);
|
||||
EXPECT_EQ(result[0].row.size(), 1u);
|
||||
}
|
||||
|
||||
TEST_CASE(select_with_nonexisting_column_name)
|
||||
|
@ -244,10 +239,11 @@ TEST_CASE(select_with_nonexisting_column_name)
|
|||
"( 'Test_3', 44 ), "
|
||||
"( 'Test_4', 45 ), "
|
||||
"( 'Test_5', 46 );");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.inserted() == 5);
|
||||
result = execute(database, "SELECT Bogus FROM TestSchema.TestTable;");
|
||||
EXPECT(result.error() == SQL::SQLErrorCode::ColumnDoesNotExist);
|
||||
EXPECT(result.size() == 5);
|
||||
|
||||
auto insert_result = try_execute(database, "SELECT Bogus FROM TestSchema.TestTable;");
|
||||
EXPECT(insert_result.is_error());
|
||||
EXPECT(insert_result.release_error().error() == SQL::SQLErrorCode::ColumnDoesNotExist);
|
||||
}
|
||||
|
||||
TEST_CASE(select_with_where)
|
||||
|
@ -263,13 +259,10 @@ TEST_CASE(select_with_where)
|
|||
"( 'Test_3', 44 ), "
|
||||
"( 'Test_4', 45 ), "
|
||||
"( 'Test_5', 46 );");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.inserted() == 5);
|
||||
EXPECT(result.size() == 5);
|
||||
result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable WHERE IntColumn > 44;");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.has_results());
|
||||
EXPECT_EQ(result.results().size(), 2u);
|
||||
for (auto& row : result.results()) {
|
||||
EXPECT_EQ(result.size(), 2u);
|
||||
for (auto& row : result) {
|
||||
EXPECT(row.row[1].to_int().value() > 44);
|
||||
}
|
||||
}
|
||||
|
@ -287,8 +280,7 @@ TEST_CASE(select_cross_join)
|
|||
"( 'Test_3', 44 ), "
|
||||
"( 'Test_4', 45 ), "
|
||||
"( 'Test_5', 46 );");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.inserted() == 5);
|
||||
EXPECT(result.size() == 5);
|
||||
result = execute(database,
|
||||
"INSERT INTO TestSchema.TestTable2 ( TextColumn2, IntColumn ) VALUES "
|
||||
"( 'Test_10', 40 ), "
|
||||
|
@ -296,13 +288,10 @@ TEST_CASE(select_cross_join)
|
|||
"( 'Test_12', 42 ), "
|
||||
"( 'Test_13', 47 ), "
|
||||
"( 'Test_14', 48 );");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.inserted() == 5);
|
||||
EXPECT(result.size() == 5);
|
||||
result = execute(database, "SELECT * FROM TestSchema.TestTable1, TestSchema.TestTable2;");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.has_results());
|
||||
EXPECT_EQ(result.results().size(), 25u);
|
||||
for (auto& row : result.results()) {
|
||||
EXPECT_EQ(result.size(), 25u);
|
||||
for (auto& row : result) {
|
||||
EXPECT(row.row.size() == 4);
|
||||
EXPECT(row.row[1].to_int().value() >= 42);
|
||||
EXPECT(row.row[1].to_int().value() <= 46);
|
||||
|
@ -324,8 +313,7 @@ TEST_CASE(select_inner_join)
|
|||
"( 'Test_3', 44 ), "
|
||||
"( 'Test_4', 45 ), "
|
||||
"( 'Test_5', 46 );");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.inserted() == 5);
|
||||
EXPECT(result.size() == 5);
|
||||
result = execute(database,
|
||||
"INSERT INTO TestSchema.TestTable2 ( TextColumn2, IntColumn ) VALUES "
|
||||
"( 'Test_10', 40 ), "
|
||||
|
@ -333,20 +321,16 @@ TEST_CASE(select_inner_join)
|
|||
"( 'Test_12', 42 ), "
|
||||
"( 'Test_13', 47 ), "
|
||||
"( 'Test_14', 48 );");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.inserted() == 5);
|
||||
EXPECT(result.size() == 5);
|
||||
result = execute(database,
|
||||
"SELECT TestTable1.IntColumn, TextColumn1, TextColumn2 "
|
||||
"FROM TestSchema.TestTable1, TestSchema.TestTable2 "
|
||||
"WHERE TestTable1.IntColumn = TestTable2.IntColumn;");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.has_results());
|
||||
EXPECT_EQ(result.results().size(), 1u);
|
||||
auto& row = result.results()[0];
|
||||
EXPECT_EQ(row.row.size(), 3u);
|
||||
EXPECT_EQ(row.row[0].to_int().value(), 42);
|
||||
EXPECT_EQ(row.row[1].to_string(), "Test_1");
|
||||
EXPECT_EQ(row.row[2].to_string(), "Test_12");
|
||||
EXPECT_EQ(result.size(), 1u);
|
||||
EXPECT_EQ(result[0].row.size(), 3u);
|
||||
EXPECT_EQ(result[0].row[0].to_int().value(), 42);
|
||||
EXPECT_EQ(result[0].row[1].to_string(), "Test_1");
|
||||
EXPECT_EQ(result[0].row[2].to_string(), "Test_12");
|
||||
}
|
||||
|
||||
TEST_CASE(select_with_like)
|
||||
|
@ -363,63 +347,48 @@ TEST_CASE(select_with_like)
|
|||
"( 'Test+4', 45 ), "
|
||||
"( 'Test+5', 46 ), "
|
||||
"( 'Another+Test_6', 47 );");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.inserted() == 6);
|
||||
EXPECT(result.size() == 6);
|
||||
|
||||
// Simple match
|
||||
result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn LIKE 'Test+1';");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.has_results());
|
||||
EXPECT_EQ(result.results().size(), 1u);
|
||||
EXPECT_EQ(result.size(), 1u);
|
||||
|
||||
// Use % to match most rows
|
||||
result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn LIKE 'T%';");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.has_results());
|
||||
EXPECT_EQ(result.results().size(), 5u);
|
||||
EXPECT_EQ(result.size(), 5u);
|
||||
|
||||
// Same as above but invert the match
|
||||
result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn NOT LIKE 'T%';");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.has_results());
|
||||
EXPECT_EQ(result.results().size(), 1u);
|
||||
EXPECT_EQ(result.size(), 1u);
|
||||
|
||||
// Use _ and % to match all rows
|
||||
result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn LIKE '%e_t%';");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.has_results());
|
||||
EXPECT_EQ(result.results().size(), 6u);
|
||||
EXPECT_EQ(result.size(), 6u);
|
||||
|
||||
// Use escape to match a single row. The escape character happens to be a
|
||||
// Regex metacharacter, let's make sure we don't get confused by that.
|
||||
result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn LIKE '%Test^_%' ESCAPE '^';");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.has_results());
|
||||
EXPECT_EQ(result.results().size(), 1u);
|
||||
EXPECT_EQ(result.size(), 1u);
|
||||
|
||||
// Same as above but escape the escape character happens to be a SQL
|
||||
// metacharacter - we want to make sure it's treated as an escape.
|
||||
result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn LIKE '%Test__%' ESCAPE '_';");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.has_results());
|
||||
EXPECT_EQ(result.results().size(), 1u);
|
||||
EXPECT_EQ(result.size(), 1u);
|
||||
|
||||
// (Unnecessarily) escaping a character that happens to be a Regex
|
||||
// metacharacter should have no effect.
|
||||
result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn LIKE 'Test:+_' ESCAPE ':';");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.has_results());
|
||||
EXPECT_EQ(result.results().size(), 5u);
|
||||
EXPECT_EQ(result.size(), 5u);
|
||||
|
||||
// Make sure we error out if the ESCAPE is empty
|
||||
result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn LIKE '%' ESCAPE '';");
|
||||
EXPECT(result.error() == SQL::SQLErrorCode::SyntaxError);
|
||||
EXPECT(!result.has_results());
|
||||
auto select_result = try_execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn LIKE '%' ESCAPE '';");
|
||||
EXPECT(select_result.is_error());
|
||||
EXPECT(select_result.release_error().error() == SQL::SQLErrorCode::SyntaxError);
|
||||
|
||||
// Make sure we error out if the ESCAPE has more than a single character
|
||||
result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn LIKE '%' ESCAPE 'whf';");
|
||||
EXPECT(result.error() == SQL::SQLErrorCode::SyntaxError);
|
||||
EXPECT(!result.has_results());
|
||||
select_result = try_execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn LIKE '%' ESCAPE 'whf';");
|
||||
EXPECT(select_result.is_error());
|
||||
EXPECT(select_result.release_error().error() == SQL::SQLErrorCode::SyntaxError);
|
||||
}
|
||||
|
||||
TEST_CASE(select_with_order)
|
||||
|
@ -435,30 +404,23 @@ TEST_CASE(select_with_order)
|
|||
"( 'Test_1', 47 ), "
|
||||
"( 'Test_3', 40 ), "
|
||||
"( 'Test_4', 41 );");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.inserted() == 5);
|
||||
EXPECT(result.size() == 5);
|
||||
|
||||
result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable ORDER BY IntColumn;");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.has_results());
|
||||
auto rows = result.results();
|
||||
EXPECT_EQ(rows.size(), 5u);
|
||||
EXPECT_EQ(rows[0].row[1].to_int().value(), 40);
|
||||
EXPECT_EQ(rows[1].row[1].to_int().value(), 41);
|
||||
EXPECT_EQ(rows[2].row[1].to_int().value(), 42);
|
||||
EXPECT_EQ(rows[3].row[1].to_int().value(), 44);
|
||||
EXPECT_EQ(rows[4].row[1].to_int().value(), 47);
|
||||
EXPECT_EQ(result.size(), 5u);
|
||||
EXPECT_EQ(result[0].row[1].to_int().value(), 40);
|
||||
EXPECT_EQ(result[1].row[1].to_int().value(), 41);
|
||||
EXPECT_EQ(result[2].row[1].to_int().value(), 42);
|
||||
EXPECT_EQ(result[3].row[1].to_int().value(), 44);
|
||||
EXPECT_EQ(result[4].row[1].to_int().value(), 47);
|
||||
|
||||
result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable ORDER BY TextColumn;");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.has_results());
|
||||
rows = result.results();
|
||||
EXPECT_EQ(rows.size(), 5u);
|
||||
EXPECT_EQ(rows[0].row[0].to_string(), "Test_1");
|
||||
EXPECT_EQ(rows[1].row[0].to_string(), "Test_2");
|
||||
EXPECT_EQ(rows[2].row[0].to_string(), "Test_3");
|
||||
EXPECT_EQ(rows[3].row[0].to_string(), "Test_4");
|
||||
EXPECT_EQ(rows[4].row[0].to_string(), "Test_5");
|
||||
EXPECT_EQ(result.size(), 5u);
|
||||
EXPECT_EQ(result[0].row[0].to_string(), "Test_1");
|
||||
EXPECT_EQ(result[1].row[0].to_string(), "Test_2");
|
||||
EXPECT_EQ(result[2].row[0].to_string(), "Test_3");
|
||||
EXPECT_EQ(result[3].row[0].to_string(), "Test_4");
|
||||
EXPECT_EQ(result[4].row[0].to_string(), "Test_5");
|
||||
}
|
||||
|
||||
TEST_CASE(select_with_regexp)
|
||||
|
@ -475,34 +437,25 @@ TEST_CASE(select_with_regexp)
|
|||
"( 'Test[4]', 45 ), "
|
||||
"( 'Test+5', 46 ), "
|
||||
"( 'Another-Test_6', 47 );");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.inserted() == 6);
|
||||
EXPECT(result.size() == 6);
|
||||
|
||||
// Simple match
|
||||
result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn REGEXP 'Test\\+1';");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.has_results());
|
||||
EXPECT_EQ(result.results().size(), 1u);
|
||||
EXPECT_EQ(result.size(), 1u);
|
||||
|
||||
// Match all
|
||||
result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn REGEXP '.*';");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.has_results());
|
||||
EXPECT_EQ(result.results().size(), 6u);
|
||||
EXPECT_EQ(result.size(), 6u);
|
||||
|
||||
// Match with wildcards
|
||||
result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn REGEXP '^Test.+';");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.has_results());
|
||||
EXPECT_EQ(result.results().size(), 4u);
|
||||
EXPECT_EQ(result.size(), 4u);
|
||||
|
||||
// Match with case insensitive basic Latin and case sensitive Swedish ö
|
||||
// FIXME: If LibRegex is changed to support case insensitive matches of Unicode characters
|
||||
// This test should be updated and changed to match 'PRÖV'.
|
||||
result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn REGEXP 'PRöV.*';");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.has_results());
|
||||
EXPECT_EQ(result.results().size(), 1u);
|
||||
EXPECT_EQ(result.size(), 1u);
|
||||
}
|
||||
|
||||
TEST_CASE(handle_regexp_errors)
|
||||
|
@ -514,13 +467,11 @@ TEST_CASE(handle_regexp_errors)
|
|||
auto result = execute(database,
|
||||
"INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES "
|
||||
"( 'Test', 0 );");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.inserted() == 1);
|
||||
EXPECT(result.size() == 1);
|
||||
|
||||
// Malformed regex, unmatched square bracket
|
||||
result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn REGEXP 'Test\\+[0-9.*';");
|
||||
EXPECT(result.is_error());
|
||||
EXPECT(!result.has_results());
|
||||
auto select_result = try_execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn REGEXP 'Test\\+[0-9.*';");
|
||||
EXPECT(select_result.is_error());
|
||||
}
|
||||
|
||||
TEST_CASE(select_with_order_two_columns)
|
||||
|
@ -536,24 +487,20 @@ TEST_CASE(select_with_order_two_columns)
|
|||
"( 'Test_1', 47 ), "
|
||||
"( 'Test_2', 40 ), "
|
||||
"( 'Test_4', 41 );");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.inserted() == 5);
|
||||
EXPECT(result.size() == 5);
|
||||
|
||||
result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable ORDER BY TextColumn, IntColumn;");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.has_results());
|
||||
auto rows = result.results();
|
||||
EXPECT_EQ(rows.size(), 5u);
|
||||
EXPECT_EQ(rows[0].row[0].to_string(), "Test_1");
|
||||
EXPECT_EQ(rows[0].row[1].to_int().value(), 47);
|
||||
EXPECT_EQ(rows[1].row[0].to_string(), "Test_2");
|
||||
EXPECT_EQ(rows[1].row[1].to_int().value(), 40);
|
||||
EXPECT_EQ(rows[2].row[0].to_string(), "Test_2");
|
||||
EXPECT_EQ(rows[2].row[1].to_int().value(), 42);
|
||||
EXPECT_EQ(rows[3].row[0].to_string(), "Test_4");
|
||||
EXPECT_EQ(rows[3].row[1].to_int().value(), 41);
|
||||
EXPECT_EQ(rows[4].row[0].to_string(), "Test_5");
|
||||
EXPECT_EQ(rows[4].row[1].to_int().value(), 44);
|
||||
EXPECT_EQ(result.size(), 5u);
|
||||
EXPECT_EQ(result[0].row[0].to_string(), "Test_1");
|
||||
EXPECT_EQ(result[0].row[1].to_int().value(), 47);
|
||||
EXPECT_EQ(result[1].row[0].to_string(), "Test_2");
|
||||
EXPECT_EQ(result[1].row[1].to_int().value(), 40);
|
||||
EXPECT_EQ(result[2].row[0].to_string(), "Test_2");
|
||||
EXPECT_EQ(result[2].row[1].to_int().value(), 42);
|
||||
EXPECT_EQ(result[3].row[0].to_string(), "Test_4");
|
||||
EXPECT_EQ(result[3].row[1].to_int().value(), 41);
|
||||
EXPECT_EQ(result[4].row[0].to_string(), "Test_5");
|
||||
EXPECT_EQ(result[4].row[1].to_int().value(), 44);
|
||||
}
|
||||
|
||||
TEST_CASE(select_with_order_by_column_not_in_result)
|
||||
|
@ -569,19 +516,15 @@ TEST_CASE(select_with_order_by_column_not_in_result)
|
|||
"( 'Test_1', 47 ), "
|
||||
"( 'Test_3', 40 ), "
|
||||
"( 'Test_4', 41 );");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.inserted() == 5);
|
||||
EXPECT(result.size() == 5);
|
||||
|
||||
result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable ORDER BY IntColumn;");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.has_results());
|
||||
auto rows = result.results();
|
||||
EXPECT_EQ(rows.size(), 5u);
|
||||
EXPECT_EQ(rows[0].row[0].to_string(), "Test_3");
|
||||
EXPECT_EQ(rows[1].row[0].to_string(), "Test_4");
|
||||
EXPECT_EQ(rows[2].row[0].to_string(), "Test_2");
|
||||
EXPECT_EQ(rows[3].row[0].to_string(), "Test_5");
|
||||
EXPECT_EQ(rows[4].row[0].to_string(), "Test_1");
|
||||
EXPECT_EQ(result.size(), 5u);
|
||||
EXPECT_EQ(result[0].row[0].to_string(), "Test_3");
|
||||
EXPECT_EQ(result[1].row[0].to_string(), "Test_4");
|
||||
EXPECT_EQ(result[2].row[0].to_string(), "Test_2");
|
||||
EXPECT_EQ(result[3].row[0].to_string(), "Test_5");
|
||||
EXPECT_EQ(result[4].row[0].to_string(), "Test_1");
|
||||
}
|
||||
|
||||
TEST_CASE(select_with_limit)
|
||||
|
@ -593,13 +536,10 @@ TEST_CASE(select_with_limit)
|
|||
for (auto count = 0; count < 100; count++) {
|
||||
auto result = execute(database,
|
||||
String::formatted("INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test_{}', {} );", count, count));
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.inserted() == 1);
|
||||
EXPECT(result.size() == 1);
|
||||
}
|
||||
auto result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable LIMIT 10;");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.has_results());
|
||||
auto rows = result.results();
|
||||
auto rows = result;
|
||||
EXPECT_EQ(rows.size(), 10u);
|
||||
}
|
||||
|
||||
|
@ -612,14 +552,10 @@ TEST_CASE(select_with_limit_and_offset)
|
|||
for (auto count = 0; count < 100; count++) {
|
||||
auto result = execute(database,
|
||||
String::formatted("INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test_{}', {} );", count, count));
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.inserted() == 1);
|
||||
EXPECT(result.size() == 1);
|
||||
}
|
||||
auto result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable LIMIT 10 OFFSET 10;");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.has_results());
|
||||
auto rows = result.results();
|
||||
EXPECT_EQ(rows.size(), 10u);
|
||||
EXPECT_EQ(result.size(), 10u);
|
||||
}
|
||||
|
||||
TEST_CASE(select_with_order_limit_and_offset)
|
||||
|
@ -631,24 +567,20 @@ TEST_CASE(select_with_order_limit_and_offset)
|
|||
for (auto count = 0; count < 100; count++) {
|
||||
auto result = execute(database,
|
||||
String::formatted("INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test_{}', {} );", count, count));
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.inserted() == 1);
|
||||
EXPECT(result.size() == 1);
|
||||
}
|
||||
auto result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable ORDER BY IntColumn LIMIT 10 OFFSET 10;");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.has_results());
|
||||
auto rows = result.results();
|
||||
EXPECT_EQ(rows.size(), 10u);
|
||||
EXPECT_EQ(rows[0].row[1].to_int().value(), 10);
|
||||
EXPECT_EQ(rows[1].row[1].to_int().value(), 11);
|
||||
EXPECT_EQ(rows[2].row[1].to_int().value(), 12);
|
||||
EXPECT_EQ(rows[3].row[1].to_int().value(), 13);
|
||||
EXPECT_EQ(rows[4].row[1].to_int().value(), 14);
|
||||
EXPECT_EQ(rows[5].row[1].to_int().value(), 15);
|
||||
EXPECT_EQ(rows[6].row[1].to_int().value(), 16);
|
||||
EXPECT_EQ(rows[7].row[1].to_int().value(), 17);
|
||||
EXPECT_EQ(rows[8].row[1].to_int().value(), 18);
|
||||
EXPECT_EQ(rows[9].row[1].to_int().value(), 19);
|
||||
EXPECT_EQ(result.size(), 10u);
|
||||
EXPECT_EQ(result[0].row[1].to_int().value(), 10);
|
||||
EXPECT_EQ(result[1].row[1].to_int().value(), 11);
|
||||
EXPECT_EQ(result[2].row[1].to_int().value(), 12);
|
||||
EXPECT_EQ(result[3].row[1].to_int().value(), 13);
|
||||
EXPECT_EQ(result[4].row[1].to_int().value(), 14);
|
||||
EXPECT_EQ(result[5].row[1].to_int().value(), 15);
|
||||
EXPECT_EQ(result[6].row[1].to_int().value(), 16);
|
||||
EXPECT_EQ(result[7].row[1].to_int().value(), 17);
|
||||
EXPECT_EQ(result[8].row[1].to_int().value(), 18);
|
||||
EXPECT_EQ(result[9].row[1].to_int().value(), 19);
|
||||
}
|
||||
|
||||
TEST_CASE(select_with_limit_out_of_bounds)
|
||||
|
@ -660,14 +592,10 @@ TEST_CASE(select_with_limit_out_of_bounds)
|
|||
for (auto count = 0; count < 100; count++) {
|
||||
auto result = execute(database,
|
||||
String::formatted("INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test_{}', {} );", count, count));
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.inserted() == 1);
|
||||
EXPECT(result.size() == 1);
|
||||
}
|
||||
auto result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable LIMIT 500;");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.has_results());
|
||||
auto rows = result.results();
|
||||
EXPECT_EQ(rows.size(), 100u);
|
||||
EXPECT_EQ(result.size(), 100u);
|
||||
}
|
||||
|
||||
TEST_CASE(select_with_offset_out_of_bounds)
|
||||
|
@ -679,14 +607,10 @@ TEST_CASE(select_with_offset_out_of_bounds)
|
|||
for (auto count = 0; count < 100; count++) {
|
||||
auto result = execute(database,
|
||||
String::formatted("INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test_{}', {} );", count, count));
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.inserted() == 1);
|
||||
EXPECT(result.size() == 1);
|
||||
}
|
||||
auto result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable LIMIT 10 OFFSET 200;");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.has_results());
|
||||
auto rows = result.results();
|
||||
EXPECT_EQ(rows.size(), 0u);
|
||||
EXPECT_EQ(result.size(), 0u);
|
||||
}
|
||||
|
||||
TEST_CASE(describe_table)
|
||||
|
@ -696,18 +620,13 @@ TEST_CASE(describe_table)
|
|||
EXPECT(!database->open().is_error());
|
||||
create_table(database);
|
||||
auto result = execute(database, "DESCRIBE TABLE TestSchema.TestTable;");
|
||||
EXPECT(!result.is_error());
|
||||
EXPECT(result.has_results());
|
||||
EXPECT_EQ(result.results().size(), 2u);
|
||||
EXPECT_EQ(result.size(), 2u);
|
||||
|
||||
auto rows = result.results();
|
||||
auto& row1 = rows[0];
|
||||
EXPECT_EQ(row1.row[0].to_string(), "TEXTCOLUMN");
|
||||
EXPECT_EQ(row1.row[1].to_string(), "text");
|
||||
EXPECT_EQ(result[0].row[0].to_string(), "TEXTCOLUMN");
|
||||
EXPECT_EQ(result[0].row[1].to_string(), "text");
|
||||
|
||||
auto& row2 = rows[1];
|
||||
EXPECT_EQ(row2.row[0].to_string(), "INTCOLUMN");
|
||||
EXPECT_EQ(row2.row[1].to_string(), "int");
|
||||
EXPECT_EQ(result[1].row[0].to_string(), "INTCOLUMN");
|
||||
EXPECT_EQ(result[1].row[1].to_string(), "int");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
#include <LibSQL/AST/Token.h>
|
||||
#include <LibSQL/Forward.h>
|
||||
#include <LibSQL/Result.h>
|
||||
#include <LibSQL/ResultSet.h>
|
||||
#include <LibSQL/Type.h>
|
||||
|
||||
namespace SQL::AST {
|
||||
|
@ -725,11 +726,11 @@ private:
|
|||
|
||||
class Statement : public ASTNode {
|
||||
public:
|
||||
Result execute(AK::NonnullRefPtr<Database> database) const;
|
||||
ResultOr<ResultSet> execute(AK::NonnullRefPtr<Database> database) const;
|
||||
|
||||
virtual Result execute(ExecutionContext&) const
|
||||
virtual ResultOr<ResultSet> execute(ExecutionContext&) const
|
||||
{
|
||||
return { SQLCommand::Unknown, SQLErrorCode::NotYetImplemented };
|
||||
return Result { SQLCommand::Unknown, SQLErrorCode::NotYetImplemented };
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -747,7 +748,7 @@ public:
|
|||
const String& schema_name() const { return m_schema_name; }
|
||||
bool is_error_if_schema_exists() const { return m_is_error_if_schema_exists; }
|
||||
|
||||
Result execute(ExecutionContext&) const override;
|
||||
ResultOr<ResultSet> execute(ExecutionContext&) const override;
|
||||
|
||||
private:
|
||||
String m_schema_name;
|
||||
|
@ -786,7 +787,7 @@ public:
|
|||
bool is_temporary() const { return m_is_temporary; }
|
||||
bool is_error_if_table_exists() const { return m_is_error_if_table_exists; }
|
||||
|
||||
Result execute(ExecutionContext&) const override;
|
||||
ResultOr<ResultSet> execute(ExecutionContext&) const override;
|
||||
|
||||
private:
|
||||
String m_schema_name;
|
||||
|
@ -949,7 +950,7 @@ public:
|
|||
bool has_selection() const { return !m_select_statement.is_null(); }
|
||||
const RefPtr<Select>& select_statement() const { return m_select_statement; }
|
||||
|
||||
virtual Result execute(ExecutionContext&) const override;
|
||||
virtual ResultOr<ResultSet> execute(ExecutionContext&) const override;
|
||||
|
||||
private:
|
||||
RefPtr<CommonTableExpressionList> m_common_table_expression_list;
|
||||
|
@ -1042,7 +1043,7 @@ public:
|
|||
const RefPtr<GroupByClause>& group_by_clause() const { return m_group_by_clause; }
|
||||
const NonnullRefPtrVector<OrderingTerm>& ordering_term_list() const { return m_ordering_term_list; }
|
||||
const RefPtr<LimitClause>& limit_clause() const { return m_limit_clause; }
|
||||
Result execute(ExecutionContext&) const override;
|
||||
ResultOr<ResultSet> execute(ExecutionContext&) const override;
|
||||
|
||||
private:
|
||||
RefPtr<CommonTableExpressionList> m_common_table_expression_list;
|
||||
|
@ -1063,7 +1064,7 @@ public:
|
|||
}
|
||||
|
||||
NonnullRefPtr<QualifiedTableName> qualified_table_name() const { return m_qualified_table_name; }
|
||||
Result execute(ExecutionContext&) const override;
|
||||
ResultOr<ResultSet> execute(ExecutionContext&) const override;
|
||||
|
||||
private:
|
||||
NonnullRefPtr<QualifiedTableName> m_qualified_table_name;
|
||||
|
|
|
@ -10,20 +10,20 @@
|
|||
|
||||
namespace SQL::AST {
|
||||
|
||||
Result CreateSchema::execute(ExecutionContext& context) const
|
||||
ResultOr<ResultSet> CreateSchema::execute(ExecutionContext& context) const
|
||||
{
|
||||
auto schema_def = TRY(context.database->get_schema(m_schema_name));
|
||||
|
||||
if (schema_def) {
|
||||
if (m_is_error_if_schema_exists)
|
||||
return { SQLCommand::Create, SQLErrorCode::SchemaExists, m_schema_name };
|
||||
return { SQLCommand::Create };
|
||||
return Result { SQLCommand::Create, SQLErrorCode::SchemaExists, m_schema_name };
|
||||
return ResultSet { SQLCommand::Create };
|
||||
}
|
||||
|
||||
schema_def = SchemaDef::construct(m_schema_name);
|
||||
TRY(context.database->add_schema(*schema_def));
|
||||
|
||||
return { SQLCommand::Create, 0, 1 };
|
||||
return ResultSet { SQLCommand::Create };
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -9,19 +9,19 @@
|
|||
|
||||
namespace SQL::AST {
|
||||
|
||||
Result CreateTable::execute(ExecutionContext& context) const
|
||||
ResultOr<ResultSet> CreateTable::execute(ExecutionContext& context) const
|
||||
{
|
||||
auto schema_name = m_schema_name.is_empty() ? String { "default"sv } : m_schema_name;
|
||||
|
||||
auto schema_def = TRY(context.database->get_schema(schema_name));
|
||||
if (!schema_def)
|
||||
return { SQLCommand::Create, SQLErrorCode::SchemaDoesNotExist, schema_name };
|
||||
return Result { SQLCommand::Create, SQLErrorCode::SchemaDoesNotExist, schema_name };
|
||||
|
||||
auto table_def = TRY(context.database->get_table(schema_name, m_table_name));
|
||||
if (table_def) {
|
||||
if (m_is_error_if_table_exists)
|
||||
return { SQLCommand::Create, SQLErrorCode::TableExists, m_table_name };
|
||||
return { SQLCommand::Create };
|
||||
return Result { SQLCommand::Create, SQLErrorCode::TableExists, m_table_name };
|
||||
return ResultSet { SQLCommand::Create };
|
||||
}
|
||||
|
||||
table_def = TableDef::construct(schema_def, m_table_name);
|
||||
|
@ -36,13 +36,13 @@ Result CreateTable::execute(ExecutionContext& context) const
|
|||
else if (column.type_name()->name().is_one_of("FLOAT"sv, "NUMBER"sv))
|
||||
type = SQLType::Float;
|
||||
else
|
||||
return { SQLCommand::Create, SQLErrorCode::InvalidType, column.type_name()->name() };
|
||||
return Result { SQLCommand::Create, SQLErrorCode::InvalidType, column.type_name()->name() };
|
||||
|
||||
table_def->append_column(column.name(), type);
|
||||
}
|
||||
|
||||
TRY(context.database->add_table(*table_def));
|
||||
return { SQLCommand::Create, 0, 1 };
|
||||
return ResultSet { SQLCommand::Create };
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,11 +7,12 @@
|
|||
#include <LibSQL/AST/AST.h>
|
||||
#include <LibSQL/Database.h>
|
||||
#include <LibSQL/Meta.h>
|
||||
#include <LibSQL/ResultSet.h>
|
||||
#include <LibSQL/Row.h>
|
||||
|
||||
namespace SQL::AST {
|
||||
|
||||
Result DescribeTable::execute(ExecutionContext& context) const
|
||||
ResultOr<ResultSet> DescribeTable::execute(ExecutionContext& context) const
|
||||
{
|
||||
auto schema_name = m_qualified_table_name->schema_name();
|
||||
auto table_name = m_qualified_table_name->table_name();
|
||||
|
@ -20,19 +21,21 @@ Result DescribeTable::execute(ExecutionContext& context) const
|
|||
if (!table_def) {
|
||||
if (schema_name.is_empty())
|
||||
schema_name = "default"sv;
|
||||
return { SQLCommand::Describe, SQLErrorCode::TableDoesNotExist, String::formatted("{}.{}", schema_name, table_name) };
|
||||
return Result { SQLCommand::Describe, SQLErrorCode::TableDoesNotExist, String::formatted("{}.{}", schema_name, table_name) };
|
||||
}
|
||||
|
||||
auto describe_table_def = MUST(context.database->get_table("master"sv, "internal_describe_table"sv));
|
||||
auto descriptor = describe_table_def->to_tuple_descriptor();
|
||||
|
||||
Result result { SQLCommand::Describe };
|
||||
ResultSet result { SQLCommand::Describe };
|
||||
TRY(result.try_ensure_capacity(table_def->columns().size()));
|
||||
|
||||
for (auto& column : table_def->columns()) {
|
||||
Tuple tuple(descriptor);
|
||||
tuple[0] = column.name();
|
||||
tuple[1] = SQLType_name(column.type());
|
||||
result.insert(tuple, Tuple {});
|
||||
|
||||
result.insert_row(tuple, Tuple {});
|
||||
}
|
||||
|
||||
return result;
|
||||
|
|
|
@ -21,23 +21,23 @@ static bool does_value_data_type_match(SQLType expected, SQLType actual)
|
|||
return expected == actual;
|
||||
}
|
||||
|
||||
Result Insert::execute(ExecutionContext& context) const
|
||||
ResultOr<ResultSet> Insert::execute(ExecutionContext& context) const
|
||||
{
|
||||
auto table_def = TRY(context.database->get_table(m_schema_name, m_table_name));
|
||||
|
||||
if (!table_def) {
|
||||
auto schema_name = m_schema_name.is_empty() ? String("default"sv) : m_schema_name;
|
||||
return { SQLCommand::Insert, SQLErrorCode::TableDoesNotExist, String::formatted("{}.{}", schema_name, m_table_name) };
|
||||
return Result { SQLCommand::Insert, SQLErrorCode::TableDoesNotExist, String::formatted("{}.{}", schema_name, m_table_name) };
|
||||
}
|
||||
|
||||
Row row(table_def);
|
||||
for (auto& column : m_column_names) {
|
||||
if (!row.has(column))
|
||||
return { SQLCommand::Insert, SQLErrorCode::ColumnDoesNotExist, column };
|
||||
return Result { SQLCommand::Insert, SQLErrorCode::ColumnDoesNotExist, column };
|
||||
}
|
||||
|
||||
Vector<Row> inserted_rows;
|
||||
TRY(inserted_rows.try_ensure_capacity(m_chained_expressions.size()));
|
||||
ResultSet result { SQLCommand::Insert };
|
||||
TRY(result.try_ensure_capacity(m_chained_expressions.size()));
|
||||
|
||||
context.result = Result { SQLCommand::Insert };
|
||||
|
||||
|
@ -55,7 +55,7 @@ Result Insert::execute(ExecutionContext& context) const
|
|||
auto values = row_value.to_vector().value();
|
||||
|
||||
if (m_column_names.is_empty() && values.size() != row.size())
|
||||
return { SQLCommand::Insert, SQLErrorCode::InvalidNumberOfValues, String::empty() };
|
||||
return Result { SQLCommand::Insert, SQLErrorCode::InvalidNumberOfValues, String::empty() };
|
||||
|
||||
for (auto ix = 0u; ix < values.size(); ix++) {
|
||||
auto input_value_type = values[ix].type();
|
||||
|
@ -65,18 +65,16 @@ Result Insert::execute(ExecutionContext& context) const
|
|||
auto element_type = tuple_descriptor[element_index].type;
|
||||
|
||||
if (!does_value_data_type_match(element_type, input_value_type))
|
||||
return { SQLCommand::Insert, SQLErrorCode::InvalidValueType, table_def->columns()[element_index].name() };
|
||||
return Result { SQLCommand::Insert, SQLErrorCode::InvalidValueType, table_def->columns()[element_index].name() };
|
||||
|
||||
row[element_index] = values[ix];
|
||||
}
|
||||
|
||||
inserted_rows.append(row);
|
||||
TRY(context.database->insert(row));
|
||||
result.insert_row(row, {});
|
||||
}
|
||||
|
||||
for (auto& inserted_row : inserted_rows)
|
||||
TRY(context.database->insert(inserted_row));
|
||||
|
||||
return { SQLCommand::Insert, 0, m_chained_expressions.size() };
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -8,12 +8,11 @@
|
|||
#include <LibSQL/AST/AST.h>
|
||||
#include <LibSQL/Database.h>
|
||||
#include <LibSQL/Meta.h>
|
||||
#include <LibSQL/ResultSet.h>
|
||||
#include <LibSQL/Row.h>
|
||||
|
||||
namespace SQL::AST {
|
||||
|
||||
Result Select::execute(ExecutionContext& context) const
|
||||
ResultOr<ResultSet> Select::execute(ExecutionContext& context) const
|
||||
{
|
||||
NonnullRefPtrVector<ResultColumn> columns;
|
||||
|
||||
|
@ -22,11 +21,11 @@ Result Select::execute(ExecutionContext& context) const
|
|||
|
||||
for (auto& table_descriptor : table_or_subquery_list()) {
|
||||
if (!table_descriptor.is_table())
|
||||
return { SQLCommand::Select, SQLErrorCode::NotYetImplemented, "Sub-selects are not yet implemented"sv };
|
||||
return Result { SQLCommand::Select, SQLErrorCode::NotYetImplemented, "Sub-selects are not yet implemented"sv };
|
||||
|
||||
auto table_def = TRY(context.database->get_table(table_descriptor.schema_name(), table_descriptor.table_name()));
|
||||
if (!table_def)
|
||||
return { SQLCommand::Select, SQLErrorCode::TableDoesNotExist, table_descriptor.table_name() };
|
||||
return Result { SQLCommand::Select, SQLErrorCode::TableDoesNotExist, table_descriptor.table_name() };
|
||||
|
||||
if (result_column_list.size() == 1 && result_column_list[0].type() == ResultType::All) {
|
||||
for (auto& col : table_def->columns()) {
|
||||
|
@ -42,7 +41,7 @@ Result Select::execute(ExecutionContext& context) const
|
|||
for (auto& col : result_column_list) {
|
||||
if (col.type() == ResultType::All) {
|
||||
// FIXME can have '*' for example in conjunction with computed columns
|
||||
return { SQLCommand::Select, SQLErrorCode::SyntaxError, "*"sv };
|
||||
return Result { SQLCommand::Select, SQLErrorCode::SyntaxError, "*"sv };
|
||||
}
|
||||
|
||||
columns.append(col);
|
||||
|
@ -50,6 +49,7 @@ Result Select::execute(ExecutionContext& context) const
|
|||
}
|
||||
|
||||
context.result = Result { SQLCommand::Select };
|
||||
ResultSet result { SQLCommand::Select };
|
||||
|
||||
auto descriptor = adopt_ref(*new TupleDescriptor);
|
||||
Tuple tuple(descriptor);
|
||||
|
@ -60,7 +60,7 @@ Result Select::execute(ExecutionContext& context) const
|
|||
|
||||
for (auto& table_descriptor : table_or_subquery_list()) {
|
||||
if (!table_descriptor.is_table())
|
||||
return { SQLCommand::Select, SQLErrorCode::NotYetImplemented, "Sub-selects are not yet implemented"sv };
|
||||
return Result { SQLCommand::Select, SQLErrorCode::NotYetImplemented, "Sub-selects are not yet implemented"sv };
|
||||
|
||||
auto table_def = TRY(context.database->get_table(table_descriptor.schema_name(), table_descriptor.table_name()));
|
||||
if (table_def->num_columns() == 0)
|
||||
|
@ -119,7 +119,7 @@ Result Select::execute(ExecutionContext& context) const
|
|||
}
|
||||
}
|
||||
|
||||
context.result->insert(tuple, sort_key);
|
||||
result.insert_row(tuple, sort_key);
|
||||
}
|
||||
|
||||
if (m_limit_clause != nullptr) {
|
||||
|
@ -130,7 +130,7 @@ Result Select::execute(ExecutionContext& context) const
|
|||
if (!limit.is_null()) {
|
||||
auto limit_value_maybe = limit.to_u32();
|
||||
if (!limit_value_maybe.has_value())
|
||||
return { SQLCommand::Select, SQLErrorCode::SyntaxError, "LIMIT clause must evaluate to an integer value"sv };
|
||||
return Result { SQLCommand::Select, SQLErrorCode::SyntaxError, "LIMIT clause must evaluate to an integer value"sv };
|
||||
|
||||
limit_value = limit_value_maybe.value();
|
||||
}
|
||||
|
@ -140,16 +140,16 @@ Result Select::execute(ExecutionContext& context) const
|
|||
if (!offset.is_null()) {
|
||||
auto offset_value_maybe = offset.to_u32();
|
||||
if (!offset_value_maybe.has_value())
|
||||
return { SQLCommand::Select, SQLErrorCode::SyntaxError, "OFFSET clause must evaluate to an integer value"sv };
|
||||
return Result { SQLCommand::Select, SQLErrorCode::SyntaxError, "OFFSET clause must evaluate to an integer value"sv };
|
||||
|
||||
offset_value = offset_value_maybe.value();
|
||||
}
|
||||
}
|
||||
|
||||
context.result->limit(offset_value, limit_value);
|
||||
result.limit(offset_value, limit_value);
|
||||
}
|
||||
|
||||
return context.result.release_value();
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
|
||||
namespace SQL::AST {
|
||||
|
||||
Result Statement::execute(AK::NonnullRefPtr<Database> database) const
|
||||
ResultOr<ResultSet> Statement::execute(AK::NonnullRefPtr<Database> database) const
|
||||
{
|
||||
ExecutionContext context { move(database), {}, this, nullptr };
|
||||
return execute(context);
|
||||
|
|
|
@ -23,6 +23,7 @@ class Key;
|
|||
class KeyPartDef;
|
||||
class Relation;
|
||||
class Result;
|
||||
class ResultSet;
|
||||
class Row;
|
||||
class SchemaDef;
|
||||
class Serializer;
|
||||
|
|
|
@ -9,30 +9,6 @@
|
|||
|
||||
namespace SQL {
|
||||
|
||||
void Result::insert(Tuple const& row, Tuple const& sort_key)
|
||||
{
|
||||
if (!m_result_set.has_value())
|
||||
m_result_set = ResultSet {};
|
||||
m_result_set->insert_row(row, sort_key);
|
||||
}
|
||||
|
||||
void Result::limit(size_t offset, size_t limit)
|
||||
{
|
||||
VERIFY(has_results());
|
||||
|
||||
if (offset > 0) {
|
||||
if (offset > m_result_set->size()) {
|
||||
m_result_set->clear();
|
||||
return;
|
||||
}
|
||||
|
||||
m_result_set->remove(0, offset);
|
||||
}
|
||||
|
||||
if (m_result_set->size() > limit)
|
||||
m_result_set->remove(limit, m_result_set->size() - limit);
|
||||
}
|
||||
|
||||
String Result::error_string() const
|
||||
{
|
||||
VERIFY(is_error());
|
||||
|
|
|
@ -9,8 +9,6 @@
|
|||
|
||||
#include <AK/Error.h>
|
||||
#include <AK/Noncopyable.h>
|
||||
#include <LibSQL/ResultSet.h>
|
||||
#include <LibSQL/Tuple.h>
|
||||
#include <LibSQL/Type.h>
|
||||
|
||||
namespace SQL {
|
||||
|
@ -75,11 +73,8 @@ enum class SQLErrorCode {
|
|||
|
||||
class [[nodiscard]] Result {
|
||||
public:
|
||||
ALWAYS_INLINE Result(SQLCommand command, size_t update_count = 0, size_t insert_count = 0, size_t delete_count = 0)
|
||||
ALWAYS_INLINE Result(SQLCommand command)
|
||||
: m_command(command)
|
||||
, m_update_count(update_count)
|
||||
, m_insert_count(insert_count)
|
||||
, m_delete_count(delete_count)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -109,19 +104,9 @@ public:
|
|||
SQLErrorCode error() const { return m_error; }
|
||||
String error_string() const;
|
||||
|
||||
void insert(Tuple const& row, Tuple const& sort_key);
|
||||
void limit(size_t offset, size_t limit);
|
||||
|
||||
bool has_results() const { return m_result_set.has_value(); }
|
||||
ResultSet const& results() const { return m_result_set.value(); }
|
||||
|
||||
size_t updated() const { return m_update_count; }
|
||||
size_t inserted() const { return m_insert_count; }
|
||||
size_t deleted() const { return m_delete_count; }
|
||||
|
||||
// These are for compatibility with the TRY() macro in AK.
|
||||
[[nodiscard]] bool is_error() const { return m_error != SQLErrorCode::NoError; }
|
||||
[[nodiscard]] ResultSet release_value() { return m_result_set.release_value(); }
|
||||
[[nodiscard]] Result release_value() { return move(*this); }
|
||||
Result release_error()
|
||||
{
|
||||
VERIFY(is_error());
|
||||
|
@ -138,11 +123,9 @@ private:
|
|||
|
||||
SQLErrorCode m_error { SQLErrorCode::NoError };
|
||||
Optional<String> m_error_message {};
|
||||
|
||||
Optional<ResultSet> m_result_set {};
|
||||
size_t m_update_count { 0 };
|
||||
size_t m_insert_count { 0 };
|
||||
size_t m_delete_count { 0 };
|
||||
};
|
||||
|
||||
template<typename ValueType>
|
||||
using ResultOr = ErrorOr<ValueType, Result>;
|
||||
|
||||
}
|
||||
|
|
|
@ -35,4 +35,19 @@ void ResultSet::insert_row(Tuple const& row, Tuple const& sort_key)
|
|||
insert(ix, ResultRow { row, sort_key });
|
||||
}
|
||||
|
||||
void ResultSet::limit(size_t offset, size_t limit)
|
||||
{
|
||||
if (offset > 0) {
|
||||
if (offset > size()) {
|
||||
clear();
|
||||
return;
|
||||
}
|
||||
|
||||
remove(0, offset);
|
||||
}
|
||||
|
||||
if (size() > limit)
|
||||
remove(limit, size() - limit);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/Vector.h>
|
||||
#include <LibSQL/Result.h>
|
||||
#include <LibSQL/Tuple.h>
|
||||
#include <LibSQL/Type.h>
|
||||
|
||||
|
@ -19,11 +20,20 @@ struct ResultRow {
|
|||
|
||||
class ResultSet : public Vector<ResultRow> {
|
||||
public:
|
||||
ResultSet() = default;
|
||||
ALWAYS_INLINE ResultSet(SQLCommand command)
|
||||
: m_command(command)
|
||||
{
|
||||
}
|
||||
|
||||
SQLCommand command() const { return m_command; }
|
||||
|
||||
void insert_row(Tuple const& row, Tuple const& sort_key);
|
||||
void limit(size_t offset, size_t limit);
|
||||
|
||||
private:
|
||||
size_t binary_search(Tuple const& sort_key, size_t low, size_t high);
|
||||
|
||||
SQLCommand m_command { SQLCommand::Unknown };
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -33,9 +33,9 @@ SQLStatement::SQLStatement(DatabaseConnection& connection, String sql)
|
|||
s_statements.set(m_statement_id, *this);
|
||||
}
|
||||
|
||||
void SQLStatement::report_error()
|
||||
void SQLStatement::report_error(SQL::Result result)
|
||||
{
|
||||
dbgln_if(SQLSERVER_DEBUG, "SQLStatement::report_error(statement_id {}, error {}", statement_id(), m_result->error_string());
|
||||
dbgln_if(SQLSERVER_DEBUG, "SQLStatement::report_error(statement_id {}, error {}", statement_id(), result.error_string());
|
||||
|
||||
auto client_connection = ClientConnection::client_connection_for(connection()->client_id());
|
||||
|
||||
|
@ -43,7 +43,7 @@ void SQLStatement::report_error()
|
|||
remove_from_parent();
|
||||
|
||||
if (client_connection)
|
||||
client_connection->async_execution_error(statement_id(), (int)m_result->error(), m_result->error_string());
|
||||
client_connection->async_execution_error(statement_id(), (int)result.error(), result.error_string());
|
||||
else
|
||||
warnln("Cannot return execution error. Client disconnected");
|
||||
|
||||
|
@ -61,17 +61,17 @@ void SQLStatement::execute()
|
|||
}
|
||||
|
||||
deferred_invoke([this]() mutable {
|
||||
m_result = parse();
|
||||
if (m_result.has_value()) {
|
||||
report_error();
|
||||
auto parse_result = parse();
|
||||
if (parse_result.is_error()) {
|
||||
report_error(parse_result.release_error());
|
||||
return;
|
||||
}
|
||||
|
||||
VERIFY(!connection()->database().is_null());
|
||||
|
||||
m_result = m_statement->execute(connection()->database().release_nonnull());
|
||||
if (m_result->is_error()) {
|
||||
report_error();
|
||||
auto execution_result = m_statement->execute(connection()->database().release_nonnull());
|
||||
if (execution_result.is_error()) {
|
||||
report_error(execution_result.release_error());
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -80,15 +80,20 @@ void SQLStatement::execute()
|
|||
warnln("Cannot return statement execution results. Client disconnected");
|
||||
return;
|
||||
}
|
||||
client_connection->async_execution_success(statement_id(), m_result->has_results(), m_result->updated(), m_result->inserted(), m_result->deleted());
|
||||
if (m_result->has_results()) {
|
||||
|
||||
m_result = execution_result.release_value();
|
||||
|
||||
if (should_send_result_rows()) {
|
||||
client_connection->async_execution_success(statement_id(), true, 0, 0, 0);
|
||||
m_index = 0;
|
||||
next();
|
||||
} else {
|
||||
client_connection->async_execution_success(statement_id(), false, 0, m_result->size(), 0);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Optional<SQL::Result> SQLStatement::parse()
|
||||
SQL::ResultOr<void> SQLStatement::parse()
|
||||
{
|
||||
auto parser = SQL::AST::Parser(SQL::AST::Lexer(m_sql));
|
||||
m_statement = parser.next_statement();
|
||||
|
@ -98,16 +103,32 @@ Optional<SQL::Result> SQLStatement::parse()
|
|||
return {};
|
||||
}
|
||||
|
||||
bool SQLStatement::should_send_result_rows() const
|
||||
{
|
||||
VERIFY(m_result.has_value());
|
||||
|
||||
if (m_result->is_empty())
|
||||
return false;
|
||||
|
||||
switch (m_result->command()) {
|
||||
case SQL::SQLCommand::Describe:
|
||||
case SQL::SQLCommand::Select:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void SQLStatement::next()
|
||||
{
|
||||
VERIFY(m_result->has_results());
|
||||
VERIFY(!m_result->is_empty());
|
||||
auto client_connection = ClientConnection::client_connection_for(connection()->client_id());
|
||||
if (!client_connection) {
|
||||
warnln("Cannot yield next result. Client disconnected");
|
||||
return;
|
||||
}
|
||||
if (m_index < m_result->results().size()) {
|
||||
auto& tuple = m_result->results()[m_index++].row;
|
||||
if (m_index < m_result->size()) {
|
||||
auto& tuple = m_result->at(m_index++).row;
|
||||
client_connection->async_next_result(statement_id(), tuple.to_string_vector());
|
||||
deferred_invoke([this]() {
|
||||
next();
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
#include <LibCore/Object.h>
|
||||
#include <LibSQL/AST/AST.h>
|
||||
#include <LibSQL/Result.h>
|
||||
#include <LibSQL/ResultSet.h>
|
||||
#include <SQLServer/DatabaseConnection.h>
|
||||
#include <SQLServer/Forward.h>
|
||||
|
||||
|
@ -30,15 +31,16 @@ public:
|
|||
|
||||
private:
|
||||
SQLStatement(DatabaseConnection&, String sql);
|
||||
Optional<SQL::Result> parse();
|
||||
SQL::ResultOr<void> parse();
|
||||
bool should_send_result_rows() const;
|
||||
void next();
|
||||
void report_error();
|
||||
void report_error(SQL::Result);
|
||||
|
||||
int m_statement_id;
|
||||
String m_sql;
|
||||
size_t m_index { 0 };
|
||||
RefPtr<SQL::AST::Statement> m_statement { nullptr };
|
||||
Optional<SQL::Result> m_result {};
|
||||
Optional<SQL::ResultSet> m_result {};
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue