LibSQL+SQLServer: Bare bones INSERT and SELECT statements
This patch provides very basic, bare bones implementations of the
INSERT and SELECT statements. They are *very* limited:
- The only variant of the INSERT statement that currently works is
SELECT INTO schema.table (column1, column2, ....) VALUES
(value11, value21, ...), (value12, value22, ...), ...
where the values are literals.
- The SELECT statement is even more limited, and is only provided to
allow verification of the INSERT statement. The only form implemented
is: SELECT * FROM schema.table
These statements required a bit of change in the Statement::execute
API. Originally execute only received a Database object as parameter.
This is not enough; we now pass an ExecutionContext object which
contains the Database, the current result set, and the last Tuple read
from the database. This object will undoubtedly evolve over time.
This API change dragged SQLServer::SQLStatement into the patch.
Another API addition is Expression::evaluate. This method is,
unsurprisingly, used to evaluate expressions, like the values in the
INSERT statement.
Finally, a new test file is added: TestSqlStatementExecution, which
tests the currently implemented statements. As the number and flavour of
implemented statements grows, this test file will probably have to be
restructured.
2021-07-19 19:48:46 -04:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
|
2021-09-19 21:35:16 +02:00
|
|
|
* Copyright (c) 2021, Mahmoud Mandour <ma.mandourr@gmail.com>
|
LibSQL+SQLServer: Bare bones INSERT and SELECT statements
This patch provides very basic, bare bones implementations of the
INSERT and SELECT statements. They are *very* limited:
- The only variant of the INSERT statement that currently works is
SELECT INTO schema.table (column1, column2, ....) VALUES
(value11, value21, ...), (value12, value22, ...), ...
where the values are literals.
- The SELECT statement is even more limited, and is only provided to
allow verification of the INSERT statement. The only form implemented
is: SELECT * FROM schema.table
These statements required a bit of change in the Statement::execute
API. Originally execute only received a Database object as parameter.
This is not enough; we now pass an ExecutionContext object which
contains the Database, the current result set, and the last Tuple read
from the database. This object will undoubtedly evolve over time.
This API change dragged SQLServer::SQLStatement into the patch.
Another API addition is Expression::evaluate. This method is,
unsurprisingly, used to evaluate expressions, like the values in the
INSERT statement.
Finally, a new test file is added: TestSqlStatementExecution, which
tests the currently implemented statements. As the number and flavour of
implemented statements grows, this test file will probably have to be
restructured.
2021-07-19 19:48:46 -04:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
2022-02-11 10:23:20 -05:00
|
|
|
#include <AK/QuickSort.h>
|
LibSQL+SQLServer: Bare bones INSERT and SELECT statements
This patch provides very basic, bare bones implementations of the
INSERT and SELECT statements. They are *very* limited:
- The only variant of the INSERT statement that currently works is
SELECT INTO schema.table (column1, column2, ....) VALUES
(value11, value21, ...), (value12, value22, ...), ...
where the values are literals.
- The SELECT statement is even more limited, and is only provided to
allow verification of the INSERT statement. The only form implemented
is: SELECT * FROM schema.table
These statements required a bit of change in the Statement::execute
API. Originally execute only received a Database object as parameter.
This is not enough; we now pass an ExecutionContext object which
contains the Database, the current result set, and the last Tuple read
from the database. This object will undoubtedly evolve over time.
This API change dragged SQLServer::SQLStatement into the patch.
Another API addition is Expression::evaluate. This method is,
unsurprisingly, used to evaluate expressions, like the values in the
INSERT statement.
Finally, a new test file is added: TestSqlStatementExecution, which
tests the currently implemented statements. As the number and flavour of
implemented statements grows, this test file will probably have to be
restructured.
2021-07-19 19:48:46 -04:00
|
|
|
#include <AK/ScopeGuard.h>
|
|
|
|
|
#include <LibSQL/AST/Parser.h>
|
|
|
|
|
#include <LibSQL/Database.h>
|
2022-02-09 16:02:49 -05:00
|
|
|
#include <LibSQL/Result.h>
|
2022-02-10 14:43:00 -05:00
|
|
|
#include <LibSQL/ResultSet.h>
|
LibSQL+SQLServer: Bare bones INSERT and SELECT statements
This patch provides very basic, bare bones implementations of the
INSERT and SELECT statements. They are *very* limited:
- The only variant of the INSERT statement that currently works is
SELECT INTO schema.table (column1, column2, ....) VALUES
(value11, value21, ...), (value12, value22, ...), ...
where the values are literals.
- The SELECT statement is even more limited, and is only provided to
allow verification of the INSERT statement. The only form implemented
is: SELECT * FROM schema.table
These statements required a bit of change in the Statement::execute
API. Originally execute only received a Database object as parameter.
This is not enough; we now pass an ExecutionContext object which
contains the Database, the current result set, and the last Tuple read
from the database. This object will undoubtedly evolve over time.
This API change dragged SQLServer::SQLStatement into the patch.
Another API addition is Expression::evaluate. This method is,
unsurprisingly, used to evaluate expressions, like the values in the
INSERT statement.
Finally, a new test file is added: TestSqlStatementExecution, which
tests the currently implemented statements. As the number and flavour of
implemented statements grows, this test file will probably have to be
restructured.
2021-07-19 19:48:46 -04:00
|
|
|
#include <LibSQL/Row.h>
|
|
|
|
|
#include <LibSQL/Value.h>
|
|
|
|
|
#include <LibTest/TestCase.h>
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
constexpr char const* db_name = "/tmp/test.db";
|
LibSQL+SQLServer: Bare bones INSERT and SELECT statements
This patch provides very basic, bare bones implementations of the
INSERT and SELECT statements. They are *very* limited:
- The only variant of the INSERT statement that currently works is
SELECT INTO schema.table (column1, column2, ....) VALUES
(value11, value21, ...), (value12, value22, ...), ...
where the values are literals.
- The SELECT statement is even more limited, and is only provided to
allow verification of the INSERT statement. The only form implemented
is: SELECT * FROM schema.table
These statements required a bit of change in the Statement::execute
API. Originally execute only received a Database object as parameter.
This is not enough; we now pass an ExecutionContext object which
contains the Database, the current result set, and the last Tuple read
from the database. This object will undoubtedly evolve over time.
This API change dragged SQLServer::SQLStatement into the patch.
Another API addition is Expression::evaluate. This method is,
unsurprisingly, used to evaluate expressions, like the values in the
INSERT statement.
Finally, a new test file is added: TestSqlStatementExecution, which
tests the currently implemented statements. As the number and flavour of
implemented statements grows, this test file will probably have to be
restructured.
2021-07-19 19:48:46 -04:00
|
|
|
|
2022-02-10 14:43:00 -05:00
|
|
|
SQL::ResultOr<SQL::ResultSet> try_execute(NonnullRefPtr<SQL::Database> database, String const& sql)
|
LibSQL+SQLServer: Bare bones INSERT and SELECT statements
This patch provides very basic, bare bones implementations of the
INSERT and SELECT statements. They are *very* limited:
- The only variant of the INSERT statement that currently works is
SELECT INTO schema.table (column1, column2, ....) VALUES
(value11, value21, ...), (value12, value22, ...), ...
where the values are literals.
- The SELECT statement is even more limited, and is only provided to
allow verification of the INSERT statement. The only form implemented
is: SELECT * FROM schema.table
These statements required a bit of change in the Statement::execute
API. Originally execute only received a Database object as parameter.
This is not enough; we now pass an ExecutionContext object which
contains the Database, the current result set, and the last Tuple read
from the database. This object will undoubtedly evolve over time.
This API change dragged SQLServer::SQLStatement into the patch.
Another API addition is Expression::evaluate. This method is,
unsurprisingly, used to evaluate expressions, like the values in the
INSERT statement.
Finally, a new test file is added: TestSqlStatementExecution, which
tests the currently implemented statements. As the number and flavour of
implemented statements grows, this test file will probably have to be
restructured.
2021-07-19 19:48:46 -04:00
|
|
|
{
|
|
|
|
|
auto parser = SQL::AST::Parser(SQL::AST::Lexer(sql));
|
|
|
|
|
auto statement = parser.next_statement();
|
|
|
|
|
EXPECT(!parser.has_errors());
|
2021-10-23 10:47:12 -04:00
|
|
|
if (parser.has_errors())
|
2021-09-09 17:06:15 +02:00
|
|
|
outln("{}", parser.errors()[0].to_string());
|
2022-02-10 14:43:00 -05:00
|
|
|
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();
|
LibSQL+SQLServer: Bare bones INSERT and SELECT statements
This patch provides very basic, bare bones implementations of the
INSERT and SELECT statements. They are *very* limited:
- The only variant of the INSERT statement that currently works is
SELECT INTO schema.table (column1, column2, ....) VALUES
(value11, value21, ...), (value12, value22, ...), ...
where the values are literals.
- The SELECT statement is even more limited, and is only provided to
allow verification of the INSERT statement. The only form implemented
is: SELECT * FROM schema.table
These statements required a bit of change in the Statement::execute
API. Originally execute only received a Database object as parameter.
This is not enough; we now pass an ExecutionContext object which
contains the Database, the current result set, and the last Tuple read
from the database. This object will undoubtedly evolve over time.
This API change dragged SQLServer::SQLStatement into the patch.
Another API addition is Expression::evaluate. This method is,
unsurprisingly, used to evaluate expressions, like the values in the
INSERT statement.
Finally, a new test file is added: TestSqlStatementExecution, which
tests the currently implemented statements. As the number and flavour of
implemented statements grows, this test file will probably have to be
restructured.
2021-07-19 19:48:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void create_schema(NonnullRefPtr<SQL::Database> database)
|
|
|
|
|
{
|
|
|
|
|
auto result = execute(database, "CREATE SCHEMA TestSchema;");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT_EQ(result.command(), SQL::SQLCommand::Create);
|
LibSQL+SQLServer: Bare bones INSERT and SELECT statements
This patch provides very basic, bare bones implementations of the
INSERT and SELECT statements. They are *very* limited:
- The only variant of the INSERT statement that currently works is
SELECT INTO schema.table (column1, column2, ....) VALUES
(value11, value21, ...), (value12, value22, ...), ...
where the values are literals.
- The SELECT statement is even more limited, and is only provided to
allow verification of the INSERT statement. The only form implemented
is: SELECT * FROM schema.table
These statements required a bit of change in the Statement::execute
API. Originally execute only received a Database object as parameter.
This is not enough; we now pass an ExecutionContext object which
contains the Database, the current result set, and the last Tuple read
from the database. This object will undoubtedly evolve over time.
This API change dragged SQLServer::SQLStatement into the patch.
Another API addition is Expression::evaluate. This method is,
unsurprisingly, used to evaluate expressions, like the values in the
INSERT statement.
Finally, a new test file is added: TestSqlStatementExecution, which
tests the currently implemented statements. As the number and flavour of
implemented statements grows, this test file will probably have to be
restructured.
2021-07-19 19:48:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void create_table(NonnullRefPtr<SQL::Database> database)
|
|
|
|
|
{
|
|
|
|
|
create_schema(database);
|
|
|
|
|
auto result = execute(database, "CREATE TABLE TestSchema.TestTable ( TextColumn text, IntColumn integer );");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT_EQ(result.command(), SQL::SQLCommand::Create);
|
LibSQL+SQLServer: Bare bones INSERT and SELECT statements
This patch provides very basic, bare bones implementations of the
INSERT and SELECT statements. They are *very* limited:
- The only variant of the INSERT statement that currently works is
SELECT INTO schema.table (column1, column2, ....) VALUES
(value11, value21, ...), (value12, value22, ...), ...
where the values are literals.
- The SELECT statement is even more limited, and is only provided to
allow verification of the INSERT statement. The only form implemented
is: SELECT * FROM schema.table
These statements required a bit of change in the Statement::execute
API. Originally execute only received a Database object as parameter.
This is not enough; we now pass an ExecutionContext object which
contains the Database, the current result set, and the last Tuple read
from the database. This object will undoubtedly evolve over time.
This API change dragged SQLServer::SQLStatement into the patch.
Another API addition is Expression::evaluate. This method is,
unsurprisingly, used to evaluate expressions, like the values in the
INSERT statement.
Finally, a new test file is added: TestSqlStatementExecution, which
tests the currently implemented statements. As the number and flavour of
implemented statements grows, this test file will probably have to be
restructured.
2021-07-19 19:48:46 -04:00
|
|
|
}
|
|
|
|
|
|
2021-11-02 16:49:54 -04:00
|
|
|
void create_two_tables(NonnullRefPtr<SQL::Database> database)
|
|
|
|
|
{
|
|
|
|
|
create_schema(database);
|
|
|
|
|
auto result = execute(database, "CREATE TABLE TestSchema.TestTable1 ( TextColumn1 text, IntColumn integer );");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT_EQ(result.command(), SQL::SQLCommand::Create);
|
2021-11-02 16:49:54 -04:00
|
|
|
result = execute(database, "CREATE TABLE TestSchema.TestTable2 ( TextColumn2 text, IntColumn integer );");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT_EQ(result.command(), SQL::SQLCommand::Create);
|
2021-11-02 16:49:54 -04:00
|
|
|
}
|
|
|
|
|
|
LibSQL+SQLServer: Bare bones INSERT and SELECT statements
This patch provides very basic, bare bones implementations of the
INSERT and SELECT statements. They are *very* limited:
- The only variant of the INSERT statement that currently works is
SELECT INTO schema.table (column1, column2, ....) VALUES
(value11, value21, ...), (value12, value22, ...), ...
where the values are literals.
- The SELECT statement is even more limited, and is only provided to
allow verification of the INSERT statement. The only form implemented
is: SELECT * FROM schema.table
These statements required a bit of change in the Statement::execute
API. Originally execute only received a Database object as parameter.
This is not enough; we now pass an ExecutionContext object which
contains the Database, the current result set, and the last Tuple read
from the database. This object will undoubtedly evolve over time.
This API change dragged SQLServer::SQLStatement into the patch.
Another API addition is Expression::evaluate. This method is,
unsurprisingly, used to evaluate expressions, like the values in the
INSERT statement.
Finally, a new test file is added: TestSqlStatementExecution, which
tests the currently implemented statements. As the number and flavour of
implemented statements grows, this test file will probably have to be
restructured.
2021-07-19 19:48:46 -04:00
|
|
|
TEST_CASE(create_schema)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
2021-11-05 19:05:59 -04:00
|
|
|
EXPECT(!database->open().is_error());
|
LibSQL+SQLServer: Bare bones INSERT and SELECT statements
This patch provides very basic, bare bones implementations of the
INSERT and SELECT statements. They are *very* limited:
- The only variant of the INSERT statement that currently works is
SELECT INTO schema.table (column1, column2, ....) VALUES
(value11, value21, ...), (value12, value22, ...), ...
where the values are literals.
- The SELECT statement is even more limited, and is only provided to
allow verification of the INSERT statement. The only form implemented
is: SELECT * FROM schema.table
These statements required a bit of change in the Statement::execute
API. Originally execute only received a Database object as parameter.
This is not enough; we now pass an ExecutionContext object which
contains the Database, the current result set, and the last Tuple read
from the database. This object will undoubtedly evolve over time.
This API change dragged SQLServer::SQLStatement into the patch.
Another API addition is Expression::evaluate. This method is,
unsurprisingly, used to evaluate expressions, like the values in the
INSERT statement.
Finally, a new test file is added: TestSqlStatementExecution, which
tests the currently implemented statements. As the number and flavour of
implemented statements grows, this test file will probably have to be
restructured.
2021-07-19 19:48:46 -04:00
|
|
|
create_schema(database);
|
2021-11-05 19:05:59 -04:00
|
|
|
auto schema_or_error = database->get_schema("TESTSCHEMA");
|
|
|
|
|
EXPECT(!schema_or_error.is_error());
|
LibSQL+SQLServer: Bare bones INSERT and SELECT statements
This patch provides very basic, bare bones implementations of the
INSERT and SELECT statements. They are *very* limited:
- The only variant of the INSERT statement that currently works is
SELECT INTO schema.table (column1, column2, ....) VALUES
(value11, value21, ...), (value12, value22, ...), ...
where the values are literals.
- The SELECT statement is even more limited, and is only provided to
allow verification of the INSERT statement. The only form implemented
is: SELECT * FROM schema.table
These statements required a bit of change in the Statement::execute
API. Originally execute only received a Database object as parameter.
This is not enough; we now pass an ExecutionContext object which
contains the Database, the current result set, and the last Tuple read
from the database. This object will undoubtedly evolve over time.
This API change dragged SQLServer::SQLStatement into the patch.
Another API addition is Expression::evaluate. This method is,
unsurprisingly, used to evaluate expressions, like the values in the
INSERT statement.
Finally, a new test file is added: TestSqlStatementExecution, which
tests the currently implemented statements. As the number and flavour of
implemented statements grows, this test file will probably have to be
restructured.
2021-07-19 19:48:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE(create_table)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
2021-11-05 19:05:59 -04:00
|
|
|
EXPECT(!database->open().is_error());
|
LibSQL+SQLServer: Bare bones INSERT and SELECT statements
This patch provides very basic, bare bones implementations of the
INSERT and SELECT statements. They are *very* limited:
- The only variant of the INSERT statement that currently works is
SELECT INTO schema.table (column1, column2, ....) VALUES
(value11, value21, ...), (value12, value22, ...), ...
where the values are literals.
- The SELECT statement is even more limited, and is only provided to
allow verification of the INSERT statement. The only form implemented
is: SELECT * FROM schema.table
These statements required a bit of change in the Statement::execute
API. Originally execute only received a Database object as parameter.
This is not enough; we now pass an ExecutionContext object which
contains the Database, the current result set, and the last Tuple read
from the database. This object will undoubtedly evolve over time.
This API change dragged SQLServer::SQLStatement into the patch.
Another API addition is Expression::evaluate. This method is,
unsurprisingly, used to evaluate expressions, like the values in the
INSERT statement.
Finally, a new test file is added: TestSqlStatementExecution, which
tests the currently implemented statements. As the number and flavour of
implemented statements grows, this test file will probably have to be
restructured.
2021-07-19 19:48:46 -04:00
|
|
|
create_table(database);
|
2021-11-05 19:05:59 -04:00
|
|
|
auto table_or_error = database->get_table("TESTSCHEMA", "TESTTABLE");
|
|
|
|
|
EXPECT(!table_or_error.is_error());
|
LibSQL+SQLServer: Bare bones INSERT and SELECT statements
This patch provides very basic, bare bones implementations of the
INSERT and SELECT statements. They are *very* limited:
- The only variant of the INSERT statement that currently works is
SELECT INTO schema.table (column1, column2, ....) VALUES
(value11, value21, ...), (value12, value22, ...), ...
where the values are literals.
- The SELECT statement is even more limited, and is only provided to
allow verification of the INSERT statement. The only form implemented
is: SELECT * FROM schema.table
These statements required a bit of change in the Statement::execute
API. Originally execute only received a Database object as parameter.
This is not enough; we now pass an ExecutionContext object which
contains the Database, the current result set, and the last Tuple read
from the database. This object will undoubtedly evolve over time.
This API change dragged SQLServer::SQLStatement into the patch.
Another API addition is Expression::evaluate. This method is,
unsurprisingly, used to evaluate expressions, like the values in the
INSERT statement.
Finally, a new test file is added: TestSqlStatementExecution, which
tests the currently implemented statements. As the number and flavour of
implemented statements grows, this test file will probably have to be
restructured.
2021-07-19 19:48:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE(insert_into_table)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
2021-11-05 19:05:59 -04:00
|
|
|
EXPECT(!database->open().is_error());
|
LibSQL+SQLServer: Bare bones INSERT and SELECT statements
This patch provides very basic, bare bones implementations of the
INSERT and SELECT statements. They are *very* limited:
- The only variant of the INSERT statement that currently works is
SELECT INTO schema.table (column1, column2, ....) VALUES
(value11, value21, ...), (value12, value22, ...), ...
where the values are literals.
- The SELECT statement is even more limited, and is only provided to
allow verification of the INSERT statement. The only form implemented
is: SELECT * FROM schema.table
These statements required a bit of change in the Statement::execute
API. Originally execute only received a Database object as parameter.
This is not enough; we now pass an ExecutionContext object which
contains the Database, the current result set, and the last Tuple read
from the database. This object will undoubtedly evolve over time.
This API change dragged SQLServer::SQLStatement into the patch.
Another API addition is Expression::evaluate. This method is,
unsurprisingly, used to evaluate expressions, like the values in the
INSERT statement.
Finally, a new test file is added: TestSqlStatementExecution, which
tests the currently implemented statements. As the number and flavour of
implemented statements grows, this test file will probably have to be
restructured.
2021-07-19 19:48:46 -04:00
|
|
|
create_table(database);
|
|
|
|
|
auto result = execute(database, "INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test', 42 );");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT(result.size() == 1);
|
LibSQL+SQLServer: Bare bones INSERT and SELECT statements
This patch provides very basic, bare bones implementations of the
INSERT and SELECT statements. They are *very* limited:
- The only variant of the INSERT statement that currently works is
SELECT INTO schema.table (column1, column2, ....) VALUES
(value11, value21, ...), (value12, value22, ...), ...
where the values are literals.
- The SELECT statement is even more limited, and is only provided to
allow verification of the INSERT statement. The only form implemented
is: SELECT * FROM schema.table
These statements required a bit of change in the Statement::execute
API. Originally execute only received a Database object as parameter.
This is not enough; we now pass an ExecutionContext object which
contains the Database, the current result set, and the last Tuple read
from the database. This object will undoubtedly evolve over time.
This API change dragged SQLServer::SQLStatement into the patch.
Another API addition is Expression::evaluate. This method is,
unsurprisingly, used to evaluate expressions, like the values in the
INSERT statement.
Finally, a new test file is added: TestSqlStatementExecution, which
tests the currently implemented statements. As the number and flavour of
implemented statements grows, this test file will probably have to be
restructured.
2021-07-19 19:48:46 -04:00
|
|
|
|
2022-11-29 08:47:22 -05:00
|
|
|
auto table = MUST(database->get_table("TESTSCHEMA", "TESTTABLE"));
|
LibSQL+SQLServer: Bare bones INSERT and SELECT statements
This patch provides very basic, bare bones implementations of the
INSERT and SELECT statements. They are *very* limited:
- The only variant of the INSERT statement that currently works is
SELECT INTO schema.table (column1, column2, ....) VALUES
(value11, value21, ...), (value12, value22, ...), ...
where the values are literals.
- The SELECT statement is even more limited, and is only provided to
allow verification of the INSERT statement. The only form implemented
is: SELECT * FROM schema.table
These statements required a bit of change in the Statement::execute
API. Originally execute only received a Database object as parameter.
This is not enough; we now pass an ExecutionContext object which
contains the Database, the current result set, and the last Tuple read
from the database. This object will undoubtedly evolve over time.
This API change dragged SQLServer::SQLStatement into the patch.
Another API addition is Expression::evaluate. This method is,
unsurprisingly, used to evaluate expressions, like the values in the
INSERT statement.
Finally, a new test file is added: TestSqlStatementExecution, which
tests the currently implemented statements. As the number and flavour of
implemented statements grows, this test file will probably have to be
restructured.
2021-07-19 19:48:46 -04:00
|
|
|
|
|
|
|
|
int count = 0;
|
2021-11-05 19:05:59 -04:00
|
|
|
auto rows_or_error = database->select_all(*table);
|
|
|
|
|
EXPECT(!rows_or_error.is_error());
|
|
|
|
|
for (auto& row : rows_or_error.value()) {
|
LibSQL+SQLServer: Bare bones INSERT and SELECT statements
This patch provides very basic, bare bones implementations of the
INSERT and SELECT statements. They are *very* limited:
- The only variant of the INSERT statement that currently works is
SELECT INTO schema.table (column1, column2, ....) VALUES
(value11, value21, ...), (value12, value22, ...), ...
where the values are literals.
- The SELECT statement is even more limited, and is only provided to
allow verification of the INSERT statement. The only form implemented
is: SELECT * FROM schema.table
These statements required a bit of change in the Statement::execute
API. Originally execute only received a Database object as parameter.
This is not enough; we now pass an ExecutionContext object which
contains the Database, the current result set, and the last Tuple read
from the database. This object will undoubtedly evolve over time.
This API change dragged SQLServer::SQLStatement into the patch.
Another API addition is Expression::evaluate. This method is,
unsurprisingly, used to evaluate expressions, like the values in the
INSERT statement.
Finally, a new test file is added: TestSqlStatementExecution, which
tests the currently implemented statements. As the number and flavour of
implemented statements grows, this test file will probably have to be
restructured.
2021-07-19 19:48:46 -04:00
|
|
|
EXPECT_EQ(row["TEXTCOLUMN"].to_string(), "Test");
|
|
|
|
|
EXPECT_EQ(row["INTCOLUMN"].to_int().value(), 42);
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
EXPECT_EQ(count, 1);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-17 20:48:54 +02:00
|
|
|
TEST_CASE(insert_into_table_wrong_data_types)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
2021-11-05 19:05:59 -04:00
|
|
|
EXPECT(!database->open().is_error());
|
2021-09-17 20:48:54 +02:00
|
|
|
create_table(database);
|
2022-02-10 14:43:00 -05:00
|
|
|
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);
|
2021-09-17 20:48:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE(insert_into_table_multiple_tuples_wrong_data_types)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
2021-11-05 19:05:59 -04:00
|
|
|
EXPECT(!database->open().is_error());
|
2021-09-17 20:48:54 +02:00
|
|
|
create_table(database);
|
2022-02-10 14:43:00 -05:00
|
|
|
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);
|
2021-09-17 20:48:54 +02:00
|
|
|
}
|
|
|
|
|
|
2021-09-17 20:49:49 +02:00
|
|
|
TEST_CASE(insert_wrong_number_of_values)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
2021-11-05 19:05:59 -04:00
|
|
|
EXPECT(!database->open().is_error());
|
2021-09-17 20:49:49 +02:00
|
|
|
create_table(database);
|
2022-02-10 14:43:00 -05:00
|
|
|
auto result = try_execute(database, "INSERT INTO TestSchema.TestTable VALUES ( 42 );");
|
|
|
|
|
EXPECT(result.is_error());
|
|
|
|
|
EXPECT(result.release_error().error() == SQL::SQLErrorCode::InvalidNumberOfValues);
|
2021-09-17 20:49:49 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-13 17:30:29 -05:00
|
|
|
TEST_CASE(insert_identifier_as_value)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
|
|
|
|
EXPECT(!database->open().is_error());
|
|
|
|
|
create_table(database);
|
2022-02-10 14:43:00 -05:00
|
|
|
auto result = try_execute(database, "INSERT INTO TestSchema.TestTable VALUES ( identifier, 42 );");
|
|
|
|
|
EXPECT(result.is_error());
|
|
|
|
|
EXPECT(result.release_error().error() == SQL::SQLErrorCode::SyntaxError);
|
2021-11-13 17:30:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE(insert_quoted_identifier_as_value)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
|
|
|
|
EXPECT(!database->open().is_error());
|
|
|
|
|
create_table(database);
|
2022-02-10 14:43:00 -05:00
|
|
|
auto result = try_execute(database, "INSERT INTO TestSchema.TestTable VALUES ( \"QuotedIdentifier\", 42 );");
|
|
|
|
|
EXPECT(result.is_error());
|
|
|
|
|
EXPECT(result.release_error().error() == SQL::SQLErrorCode::SyntaxError);
|
2021-11-13 17:30:29 -05:00
|
|
|
}
|
|
|
|
|
|
2021-09-17 23:29:28 +02:00
|
|
|
TEST_CASE(insert_without_column_names)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
2021-11-05 19:05:59 -04:00
|
|
|
EXPECT(!database->open().is_error());
|
2021-09-17 23:29:28 +02:00
|
|
|
create_table(database);
|
|
|
|
|
auto result = execute(database, "INSERT INTO TestSchema.TestTable VALUES ('Test_1', 42), ('Test_2', 43);");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT(result.size() == 2);
|
2021-09-17 23:29:28 +02:00
|
|
|
|
2022-11-29 08:47:22 -05:00
|
|
|
auto table = MUST(database->get_table("TESTSCHEMA", "TESTTABLE"));
|
|
|
|
|
auto rows_or_error = database->select_all(*table);
|
2021-11-05 19:05:59 -04:00
|
|
|
EXPECT(!rows_or_error.is_error());
|
|
|
|
|
EXPECT_EQ(rows_or_error.value().size(), 2u);
|
2021-09-17 23:29:28 +02:00
|
|
|
}
|
|
|
|
|
|
2022-02-09 17:10:08 -05:00
|
|
|
TEST_CASE(select_from_empty_table)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
|
|
|
|
EXPECT(!database->open().is_error());
|
|
|
|
|
create_table(database);
|
|
|
|
|
auto result = execute(database, "SELECT * FROM TestSchema.TestTable;");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT(result.is_empty());
|
2022-02-09 17:10:08 -05:00
|
|
|
}
|
|
|
|
|
|
LibSQL+SQLServer: Bare bones INSERT and SELECT statements
This patch provides very basic, bare bones implementations of the
INSERT and SELECT statements. They are *very* limited:
- The only variant of the INSERT statement that currently works is
SELECT INTO schema.table (column1, column2, ....) VALUES
(value11, value21, ...), (value12, value22, ...), ...
where the values are literals.
- The SELECT statement is even more limited, and is only provided to
allow verification of the INSERT statement. The only form implemented
is: SELECT * FROM schema.table
These statements required a bit of change in the Statement::execute
API. Originally execute only received a Database object as parameter.
This is not enough; we now pass an ExecutionContext object which
contains the Database, the current result set, and the last Tuple read
from the database. This object will undoubtedly evolve over time.
This API change dragged SQLServer::SQLStatement into the patch.
Another API addition is Expression::evaluate. This method is,
unsurprisingly, used to evaluate expressions, like the values in the
INSERT statement.
Finally, a new test file is added: TestSqlStatementExecution, which
tests the currently implemented statements. As the number and flavour of
implemented statements grows, this test file will probably have to be
restructured.
2021-07-19 19:48:46 -04:00
|
|
|
TEST_CASE(select_from_table)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
2021-11-05 19:05:59 -04:00
|
|
|
EXPECT(!database->open().is_error());
|
LibSQL+SQLServer: Bare bones INSERT and SELECT statements
This patch provides very basic, bare bones implementations of the
INSERT and SELECT statements. They are *very* limited:
- The only variant of the INSERT statement that currently works is
SELECT INTO schema.table (column1, column2, ....) VALUES
(value11, value21, ...), (value12, value22, ...), ...
where the values are literals.
- The SELECT statement is even more limited, and is only provided to
allow verification of the INSERT statement. The only form implemented
is: SELECT * FROM schema.table
These statements required a bit of change in the Statement::execute
API. Originally execute only received a Database object as parameter.
This is not enough; we now pass an ExecutionContext object which
contains the Database, the current result set, and the last Tuple read
from the database. This object will undoubtedly evolve over time.
This API change dragged SQLServer::SQLStatement into the patch.
Another API addition is Expression::evaluate. This method is,
unsurprisingly, used to evaluate expressions, like the values in the
INSERT statement.
Finally, a new test file is added: TestSqlStatementExecution, which
tests the currently implemented statements. As the number and flavour of
implemented statements grows, this test file will probably have to be
restructured.
2021-07-19 19:48:46 -04:00
|
|
|
create_table(database);
|
2021-11-02 16:49:54 -04:00
|
|
|
auto result = execute(database,
|
|
|
|
|
"INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES "
|
|
|
|
|
"( 'Test_1', 42 ), "
|
|
|
|
|
"( 'Test_2', 43 ), "
|
|
|
|
|
"( 'Test_3', 44 ), "
|
|
|
|
|
"( 'Test_4', 45 ), "
|
|
|
|
|
"( 'Test_5', 46 );");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT(result.size() == 5);
|
LibSQL+SQLServer: Bare bones INSERT and SELECT statements
This patch provides very basic, bare bones implementations of the
INSERT and SELECT statements. They are *very* limited:
- The only variant of the INSERT statement that currently works is
SELECT INTO schema.table (column1, column2, ....) VALUES
(value11, value21, ...), (value12, value22, ...), ...
where the values are literals.
- The SELECT statement is even more limited, and is only provided to
allow verification of the INSERT statement. The only form implemented
is: SELECT * FROM schema.table
These statements required a bit of change in the Statement::execute
API. Originally execute only received a Database object as parameter.
This is not enough; we now pass an ExecutionContext object which
contains the Database, the current result set, and the last Tuple read
from the database. This object will undoubtedly evolve over time.
This API change dragged SQLServer::SQLStatement into the patch.
Another API addition is Expression::evaluate. This method is,
unsurprisingly, used to evaluate expressions, like the values in the
INSERT statement.
Finally, a new test file is added: TestSqlStatementExecution, which
tests the currently implemented statements. As the number and flavour of
implemented statements grows, this test file will probably have to be
restructured.
2021-07-19 19:48:46 -04:00
|
|
|
result = execute(database, "SELECT * FROM TestSchema.TestTable;");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT_EQ(result.size(), 5u);
|
LibSQL+SQLServer: Bare bones INSERT and SELECT statements
This patch provides very basic, bare bones implementations of the
INSERT and SELECT statements. They are *very* limited:
- The only variant of the INSERT statement that currently works is
SELECT INTO schema.table (column1, column2, ....) VALUES
(value11, value21, ...), (value12, value22, ...), ...
where the values are literals.
- The SELECT statement is even more limited, and is only provided to
allow verification of the INSERT statement. The only form implemented
is: SELECT * FROM schema.table
These statements required a bit of change in the Statement::execute
API. Originally execute only received a Database object as parameter.
This is not enough; we now pass an ExecutionContext object which
contains the Database, the current result set, and the last Tuple read
from the database. This object will undoubtedly evolve over time.
This API change dragged SQLServer::SQLStatement into the patch.
Another API addition is Expression::evaluate. This method is,
unsurprisingly, used to evaluate expressions, like the values in the
INSERT statement.
Finally, a new test file is added: TestSqlStatementExecution, which
tests the currently implemented statements. As the number and flavour of
implemented statements grows, this test file will probably have to be
restructured.
2021-07-19 19:48:46 -04:00
|
|
|
}
|
|
|
|
|
|
2021-10-23 10:47:12 -04:00
|
|
|
TEST_CASE(select_with_column_names)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
2021-11-05 19:05:59 -04:00
|
|
|
EXPECT(!database->open().is_error());
|
2021-10-23 10:47:12 -04:00
|
|
|
create_table(database);
|
2021-11-02 16:49:54 -04:00
|
|
|
auto result = execute(database,
|
|
|
|
|
"INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES "
|
|
|
|
|
"( 'Test_1', 42 ), "
|
|
|
|
|
"( 'Test_2', 43 ), "
|
|
|
|
|
"( 'Test_3', 44 ), "
|
|
|
|
|
"( 'Test_4', 45 ), "
|
|
|
|
|
"( 'Test_5', 46 );");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT(result.size() == 5);
|
2021-10-23 10:47:12 -04:00
|
|
|
result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable;");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT_EQ(result.size(), 5u);
|
|
|
|
|
EXPECT_EQ(result[0].row.size(), 1u);
|
2021-10-23 10:47:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE(select_with_nonexisting_column_name)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
2021-11-05 19:05:59 -04:00
|
|
|
EXPECT(!database->open().is_error());
|
2021-10-23 10:47:12 -04:00
|
|
|
create_table(database);
|
|
|
|
|
auto result = execute(database,
|
|
|
|
|
"INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES "
|
|
|
|
|
"( 'Test_1', 42 ), "
|
|
|
|
|
"( 'Test_2', 43 ), "
|
|
|
|
|
"( 'Test_3', 44 ), "
|
|
|
|
|
"( 'Test_4', 45 ), "
|
|
|
|
|
"( 'Test_5', 46 );");
|
2022-02-10 14:43:00 -05:00
|
|
|
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);
|
2021-10-23 10:47:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE(select_with_where)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
2021-11-05 19:05:59 -04:00
|
|
|
EXPECT(!database->open().is_error());
|
2021-10-23 10:47:12 -04:00
|
|
|
create_table(database);
|
|
|
|
|
auto result = execute(database,
|
|
|
|
|
"INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES "
|
|
|
|
|
"( 'Test_1', 42 ), "
|
|
|
|
|
"( 'Test_2', 43 ), "
|
|
|
|
|
"( 'Test_3', 44 ), "
|
|
|
|
|
"( 'Test_4', 45 ), "
|
|
|
|
|
"( 'Test_5', 46 );");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT(result.size() == 5);
|
2021-10-23 10:47:12 -04:00
|
|
|
result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable WHERE IntColumn > 44;");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT_EQ(result.size(), 2u);
|
|
|
|
|
for (auto& row : result) {
|
2022-01-12 09:49:43 -05:00
|
|
|
EXPECT(row.row[1].to_int().value() > 44);
|
2021-10-23 10:47:12 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-02 16:49:54 -04:00
|
|
|
TEST_CASE(select_cross_join)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
2021-11-05 19:05:59 -04:00
|
|
|
EXPECT(!database->open().is_error());
|
2021-11-02 16:49:54 -04:00
|
|
|
create_two_tables(database);
|
|
|
|
|
auto result = execute(database,
|
|
|
|
|
"INSERT INTO TestSchema.TestTable1 ( TextColumn1, IntColumn ) VALUES "
|
|
|
|
|
"( 'Test_1', 42 ), "
|
|
|
|
|
"( 'Test_2', 43 ), "
|
|
|
|
|
"( 'Test_3', 44 ), "
|
|
|
|
|
"( 'Test_4', 45 ), "
|
|
|
|
|
"( 'Test_5', 46 );");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT(result.size() == 5);
|
2021-11-02 16:49:54 -04:00
|
|
|
result = execute(database,
|
|
|
|
|
"INSERT INTO TestSchema.TestTable2 ( TextColumn2, IntColumn ) VALUES "
|
|
|
|
|
"( 'Test_10', 40 ), "
|
|
|
|
|
"( 'Test_11', 41 ), "
|
|
|
|
|
"( 'Test_12', 42 ), "
|
|
|
|
|
"( 'Test_13', 47 ), "
|
|
|
|
|
"( 'Test_14', 48 );");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT(result.size() == 5);
|
2021-11-02 16:49:54 -04:00
|
|
|
result = execute(database, "SELECT * FROM TestSchema.TestTable1, TestSchema.TestTable2;");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT_EQ(result.size(), 25u);
|
|
|
|
|
for (auto& row : result) {
|
2022-01-12 09:49:43 -05:00
|
|
|
EXPECT(row.row.size() == 4);
|
|
|
|
|
EXPECT(row.row[1].to_int().value() >= 42);
|
|
|
|
|
EXPECT(row.row[1].to_int().value() <= 46);
|
|
|
|
|
EXPECT(row.row[3].to_int().value() >= 40);
|
|
|
|
|
EXPECT(row.row[3].to_int().value() <= 48);
|
2021-11-02 16:49:54 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE(select_inner_join)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
2021-11-05 19:05:59 -04:00
|
|
|
EXPECT(!database->open().is_error());
|
2021-11-02 16:49:54 -04:00
|
|
|
create_two_tables(database);
|
|
|
|
|
auto result = execute(database,
|
|
|
|
|
"INSERT INTO TestSchema.TestTable1 ( TextColumn1, IntColumn ) VALUES "
|
|
|
|
|
"( 'Test_1', 42 ), "
|
|
|
|
|
"( 'Test_2', 43 ), "
|
|
|
|
|
"( 'Test_3', 44 ), "
|
|
|
|
|
"( 'Test_4', 45 ), "
|
|
|
|
|
"( 'Test_5', 46 );");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT(result.size() == 5);
|
2021-11-02 16:49:54 -04:00
|
|
|
result = execute(database,
|
|
|
|
|
"INSERT INTO TestSchema.TestTable2 ( TextColumn2, IntColumn ) VALUES "
|
|
|
|
|
"( 'Test_10', 40 ), "
|
|
|
|
|
"( 'Test_11', 41 ), "
|
|
|
|
|
"( 'Test_12', 42 ), "
|
|
|
|
|
"( 'Test_13', 47 ), "
|
|
|
|
|
"( 'Test_14', 48 );");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT(result.size() == 5);
|
2021-11-02 16:49:54 -04:00
|
|
|
result = execute(database,
|
|
|
|
|
"SELECT TestTable1.IntColumn, TextColumn1, TextColumn2 "
|
|
|
|
|
"FROM TestSchema.TestTable1, TestSchema.TestTable2 "
|
|
|
|
|
"WHERE TestTable1.IntColumn = TestTable2.IntColumn;");
|
2022-02-10 14:43:00 -05:00
|
|
|
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");
|
2021-11-02 16:49:54 -04:00
|
|
|
}
|
|
|
|
|
|
2021-12-29 11:47:29 -03:00
|
|
|
TEST_CASE(select_with_like)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
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 ), "
|
|
|
|
|
"( 'Test+2', 43 ), "
|
|
|
|
|
"( 'Test+3', 44 ), "
|
|
|
|
|
"( 'Test+4', 45 ), "
|
|
|
|
|
"( 'Test+5', 46 ), "
|
|
|
|
|
"( 'Another+Test_6', 47 );");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT(result.size() == 6);
|
2021-12-29 11:47:29 -03:00
|
|
|
|
|
|
|
|
// Simple match
|
|
|
|
|
result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn LIKE 'Test+1';");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT_EQ(result.size(), 1u);
|
2021-12-29 11:47:29 -03:00
|
|
|
|
|
|
|
|
// Use % to match most rows
|
|
|
|
|
result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn LIKE 'T%';");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT_EQ(result.size(), 5u);
|
2021-12-29 11:47:29 -03:00
|
|
|
|
|
|
|
|
// Same as above but invert the match
|
|
|
|
|
result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn NOT LIKE 'T%';");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT_EQ(result.size(), 1u);
|
2021-12-29 11:47:29 -03:00
|
|
|
|
|
|
|
|
// Use _ and % to match all rows
|
|
|
|
|
result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn LIKE '%e_t%';");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT_EQ(result.size(), 6u);
|
2021-12-29 11:47:29 -03:00
|
|
|
|
|
|
|
|
// 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 '^';");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT_EQ(result.size(), 1u);
|
2021-12-29 11:47:29 -03:00
|
|
|
|
|
|
|
|
// 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 '_';");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT_EQ(result.size(), 1u);
|
2021-12-29 11:47:29 -03:00
|
|
|
|
|
|
|
|
// (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 ':';");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT_EQ(result.size(), 5u);
|
2021-12-29 11:47:29 -03:00
|
|
|
|
|
|
|
|
// Make sure we error out if the ESCAPE is empty
|
2022-02-10 14:43:00 -05:00
|
|
|
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);
|
2021-12-29 11:47:29 -03:00
|
|
|
|
|
|
|
|
// Make sure we error out if the ESCAPE has more than a single character
|
2022-02-10 14:43:00 -05:00
|
|
|
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);
|
2021-12-29 11:47:29 -03:00
|
|
|
}
|
|
|
|
|
|
2022-01-12 09:49:43 -05:00
|
|
|
TEST_CASE(select_with_order)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
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_5', 44 ), "
|
|
|
|
|
"( 'Test_2', 42 ), "
|
|
|
|
|
"( 'Test_1', 47 ), "
|
|
|
|
|
"( 'Test_3', 40 ), "
|
|
|
|
|
"( 'Test_4', 41 );");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT(result.size() == 5);
|
2022-01-12 09:49:43 -05:00
|
|
|
|
|
|
|
|
result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable ORDER BY IntColumn;");
|
2022-02-10 14:43:00 -05:00
|
|
|
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);
|
2022-01-12 09:49:43 -05:00
|
|
|
|
|
|
|
|
result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable ORDER BY TextColumn;");
|
2022-02-10 14:43:00 -05:00
|
|
|
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");
|
2022-01-12 09:49:43 -05:00
|
|
|
}
|
|
|
|
|
|
2022-01-22 15:43:30 +01:00
|
|
|
TEST_CASE(select_with_regexp)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
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 ), "
|
|
|
|
|
"( 'Pröv+2', 43 ), "
|
|
|
|
|
"( 'Test(3)', 44 ), "
|
|
|
|
|
"( 'Test[4]', 45 ), "
|
|
|
|
|
"( 'Test+5', 46 ), "
|
|
|
|
|
"( 'Another-Test_6', 47 );");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT(result.size() == 6);
|
2022-01-22 15:43:30 +01:00
|
|
|
|
|
|
|
|
// Simple match
|
|
|
|
|
result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn REGEXP 'Test\\+1';");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT_EQ(result.size(), 1u);
|
2022-01-22 15:43:30 +01:00
|
|
|
|
|
|
|
|
// Match all
|
|
|
|
|
result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn REGEXP '.*';");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT_EQ(result.size(), 6u);
|
2022-01-22 15:43:30 +01:00
|
|
|
|
|
|
|
|
// Match with wildcards
|
|
|
|
|
result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn REGEXP '^Test.+';");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT_EQ(result.size(), 4u);
|
2022-01-22 15:43:30 +01:00
|
|
|
|
|
|
|
|
// 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.*';");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT_EQ(result.size(), 1u);
|
2022-01-22 15:43:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE(handle_regexp_errors)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
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', 0 );");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT(result.size() == 1);
|
2022-01-22 15:43:30 +01:00
|
|
|
|
|
|
|
|
// Malformed regex, unmatched square bracket
|
2022-02-10 14:43:00 -05:00
|
|
|
auto select_result = try_execute(database, "SELECT TextColumn FROM TestSchema.TestTable WHERE TextColumn REGEXP 'Test\\+[0-9.*';");
|
|
|
|
|
EXPECT(select_result.is_error());
|
2022-01-22 15:43:30 +01:00
|
|
|
}
|
|
|
|
|
|
2022-01-12 09:49:43 -05:00
|
|
|
TEST_CASE(select_with_order_two_columns)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
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_5', 44 ), "
|
|
|
|
|
"( 'Test_2', 42 ), "
|
|
|
|
|
"( 'Test_1', 47 ), "
|
|
|
|
|
"( 'Test_2', 40 ), "
|
|
|
|
|
"( 'Test_4', 41 );");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT(result.size() == 5);
|
2022-01-12 09:49:43 -05:00
|
|
|
|
|
|
|
|
result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable ORDER BY TextColumn, IntColumn;");
|
2022-02-10 14:43:00 -05:00
|
|
|
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);
|
2022-01-12 09:49:43 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE(select_with_order_by_column_not_in_result)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
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_5', 44 ), "
|
|
|
|
|
"( 'Test_2', 42 ), "
|
|
|
|
|
"( 'Test_1', 47 ), "
|
|
|
|
|
"( 'Test_3', 40 ), "
|
|
|
|
|
"( 'Test_4', 41 );");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT(result.size() == 5);
|
2022-01-12 09:49:43 -05:00
|
|
|
|
|
|
|
|
result = execute(database, "SELECT TextColumn FROM TestSchema.TestTable ORDER BY IntColumn;");
|
2022-02-10 14:43:00 -05:00
|
|
|
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");
|
2022-01-12 09:49:43 -05:00
|
|
|
}
|
|
|
|
|
|
2022-01-12 11:41:58 -05:00
|
|
|
TEST_CASE(select_with_limit)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
|
|
|
|
EXPECT(!database->open().is_error());
|
|
|
|
|
create_table(database);
|
|
|
|
|
for (auto count = 0; count < 100; count++) {
|
|
|
|
|
auto result = execute(database,
|
|
|
|
|
String::formatted("INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test_{}', {} );", count, count));
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT(result.size() == 1);
|
2022-01-12 11:41:58 -05:00
|
|
|
}
|
|
|
|
|
auto result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable LIMIT 10;");
|
2022-02-10 14:43:00 -05:00
|
|
|
auto rows = result;
|
2022-01-12 11:41:58 -05:00
|
|
|
EXPECT_EQ(rows.size(), 10u);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE(select_with_limit_and_offset)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
|
|
|
|
EXPECT(!database->open().is_error());
|
|
|
|
|
create_table(database);
|
|
|
|
|
for (auto count = 0; count < 100; count++) {
|
|
|
|
|
auto result = execute(database,
|
|
|
|
|
String::formatted("INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test_{}', {} );", count, count));
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT(result.size() == 1);
|
2022-01-12 11:41:58 -05:00
|
|
|
}
|
|
|
|
|
auto result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable LIMIT 10 OFFSET 10;");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT_EQ(result.size(), 10u);
|
2022-01-12 11:41:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE(select_with_order_limit_and_offset)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
|
|
|
|
EXPECT(!database->open().is_error());
|
|
|
|
|
create_table(database);
|
|
|
|
|
for (auto count = 0; count < 100; count++) {
|
|
|
|
|
auto result = execute(database,
|
|
|
|
|
String::formatted("INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test_{}', {} );", count, count));
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT(result.size() == 1);
|
2022-01-12 11:41:58 -05:00
|
|
|
}
|
|
|
|
|
auto result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable ORDER BY IntColumn LIMIT 10 OFFSET 10;");
|
2022-02-10 14:43:00 -05:00
|
|
|
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);
|
2022-01-12 11:41:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE(select_with_limit_out_of_bounds)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
|
|
|
|
EXPECT(!database->open().is_error());
|
|
|
|
|
create_table(database);
|
|
|
|
|
for (auto count = 0; count < 100; count++) {
|
|
|
|
|
auto result = execute(database,
|
|
|
|
|
String::formatted("INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test_{}', {} );", count, count));
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT(result.size() == 1);
|
2022-01-12 11:41:58 -05:00
|
|
|
}
|
|
|
|
|
auto result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable LIMIT 500;");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT_EQ(result.size(), 100u);
|
2022-01-12 11:41:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE(select_with_offset_out_of_bounds)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
|
|
|
|
EXPECT(!database->open().is_error());
|
|
|
|
|
create_table(database);
|
|
|
|
|
for (auto count = 0; count < 100; count++) {
|
|
|
|
|
auto result = execute(database,
|
|
|
|
|
String::formatted("INSERT INTO TestSchema.TestTable ( TextColumn, IntColumn ) VALUES ( 'Test_{}', {} );", count, count));
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT(result.size() == 1);
|
2022-01-12 11:41:58 -05:00
|
|
|
}
|
|
|
|
|
auto result = execute(database, "SELECT TextColumn, IntColumn FROM TestSchema.TestTable LIMIT 10 OFFSET 200;");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT_EQ(result.size(), 0u);
|
2022-01-12 11:41:58 -05:00
|
|
|
}
|
|
|
|
|
|
2021-09-19 21:35:16 +02:00
|
|
|
TEST_CASE(describe_table)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
|
|
|
|
EXPECT(!database->open().is_error());
|
|
|
|
|
create_table(database);
|
|
|
|
|
auto result = execute(database, "DESCRIBE TABLE TestSchema.TestTable;");
|
2022-02-10 14:43:00 -05:00
|
|
|
EXPECT_EQ(result.size(), 2u);
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(result[0].row[0].to_string(), "TEXTCOLUMN");
|
|
|
|
|
EXPECT_EQ(result[0].row[1].to_string(), "text");
|
|
|
|
|
|
|
|
|
|
EXPECT_EQ(result[1].row[0].to_string(), "INTCOLUMN");
|
|
|
|
|
EXPECT_EQ(result[1].row[1].to_string(), "int");
|
2021-09-19 21:35:16 +02:00
|
|
|
}
|
|
|
|
|
|
2022-02-11 10:23:20 -05:00
|
|
|
TEST_CASE(binary_operator_execution)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
|
|
|
|
EXPECT(!database->open().is_error());
|
|
|
|
|
create_table(database);
|
|
|
|
|
|
|
|
|
|
for (auto count = 0; count < 10; ++count) {
|
|
|
|
|
auto result = execute(database, String::formatted("INSERT INTO TestSchema.TestTable VALUES ( 'T{}', {} );", count, count));
|
|
|
|
|
EXPECT_EQ(result.size(), 1u);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto compare_result = [](SQL::ResultSet const& result, Vector<int> const& expected) {
|
|
|
|
|
EXPECT_EQ(result.command(), SQL::SQLCommand::Select);
|
|
|
|
|
EXPECT_EQ(result.size(), expected.size());
|
|
|
|
|
|
|
|
|
|
Vector<int> result_values;
|
|
|
|
|
result_values.ensure_capacity(result.size());
|
|
|
|
|
|
|
|
|
|
for (size_t i = 0; i < result.size(); ++i) {
|
|
|
|
|
auto const& result_row = result.at(i).row;
|
|
|
|
|
EXPECT_EQ(result_row.size(), 1u);
|
|
|
|
|
|
|
|
|
|
auto result_column = result_row[0].to_int();
|
|
|
|
|
result_values.append(result_column.value());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
quick_sort(result_values);
|
|
|
|
|
EXPECT_EQ(result_values, expected);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn + 1) < 5);");
|
|
|
|
|
compare_result(result, { 0, 1, 2, 3 });
|
|
|
|
|
|
|
|
|
|
result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn + 1) <= 5);");
|
|
|
|
|
compare_result(result, { 0, 1, 2, 3, 4 });
|
|
|
|
|
|
|
|
|
|
result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn - 1) > 4);");
|
|
|
|
|
compare_result(result, { 6, 7, 8, 9 });
|
|
|
|
|
|
|
|
|
|
result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn - 1) >= 4);");
|
|
|
|
|
compare_result(result, { 5, 6, 7, 8, 9 });
|
|
|
|
|
|
|
|
|
|
result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn * 2) < 10);");
|
|
|
|
|
compare_result(result, { 0, 1, 2, 3, 4 });
|
|
|
|
|
|
|
|
|
|
result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn * 2) <= 10);");
|
|
|
|
|
compare_result(result, { 0, 1, 2, 3, 4, 5 });
|
|
|
|
|
|
|
|
|
|
result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn / 3) > 2);");
|
|
|
|
|
compare_result(result, { 7, 8, 9 });
|
|
|
|
|
|
|
|
|
|
result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn / 3) >= 2);");
|
|
|
|
|
compare_result(result, { 6, 7, 8, 9 });
|
|
|
|
|
|
|
|
|
|
result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn % 2) = 0);");
|
|
|
|
|
compare_result(result, { 0, 2, 4, 6, 8 });
|
|
|
|
|
|
|
|
|
|
result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn % 2) = 1);");
|
|
|
|
|
compare_result(result, { 1, 3, 5, 7, 9 });
|
|
|
|
|
|
|
|
|
|
result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((1 << IntColumn) <= 32);");
|
|
|
|
|
compare_result(result, { 0, 1, 2, 3, 4, 5 });
|
|
|
|
|
|
|
|
|
|
result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((1024 >> IntColumn) >= 32);");
|
|
|
|
|
compare_result(result, { 0, 1, 2, 3, 4, 5 });
|
|
|
|
|
|
|
|
|
|
result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn | 1) != IntColumn);");
|
|
|
|
|
compare_result(result, { 0, 2, 4, 6, 8 });
|
|
|
|
|
|
|
|
|
|
result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable WHERE ((IntColumn & 1) = 1);");
|
|
|
|
|
compare_result(result, { 1, 3, 5, 7, 9 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE(binary_operator_failure)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
|
|
|
|
EXPECT(!database->open().is_error());
|
|
|
|
|
create_table(database);
|
|
|
|
|
|
|
|
|
|
for (auto count = 0; count < 10; ++count) {
|
|
|
|
|
auto result = execute(database, String::formatted("INSERT INTO TestSchema.TestTable VALUES ( 'T{}', {} );", count, count));
|
|
|
|
|
EXPECT_EQ(result.size(), 1u);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto expect_failure = [](auto result, auto op) {
|
|
|
|
|
EXPECT(result.is_error());
|
|
|
|
|
|
|
|
|
|
auto error = result.release_error();
|
|
|
|
|
EXPECT_EQ(error.error(), SQL::SQLErrorCode::NumericOperatorTypeMismatch);
|
|
|
|
|
|
|
|
|
|
auto message = String::formatted("NumericOperatorTypeMismatch: Cannot apply '{}' operator to non-numeric operands", op);
|
|
|
|
|
EXPECT_EQ(error.error_string(), message);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
auto result = try_execute(database, "SELECT * FROM TestSchema.TestTable WHERE ((IntColumn + TextColumn) < 5);");
|
|
|
|
|
expect_failure(move(result), '+');
|
|
|
|
|
|
|
|
|
|
result = try_execute(database, "SELECT * FROM TestSchema.TestTable WHERE ((IntColumn - TextColumn) < 5);");
|
|
|
|
|
expect_failure(move(result), '-');
|
|
|
|
|
|
|
|
|
|
result = try_execute(database, "SELECT * FROM TestSchema.TestTable WHERE ((IntColumn * TextColumn) < 5);");
|
|
|
|
|
expect_failure(move(result), '*');
|
|
|
|
|
|
|
|
|
|
result = try_execute(database, "SELECT * FROM TestSchema.TestTable WHERE ((IntColumn / TextColumn) < 5);");
|
|
|
|
|
expect_failure(move(result), '/');
|
|
|
|
|
|
|
|
|
|
result = try_execute(database, "SELECT * FROM TestSchema.TestTable WHERE ((IntColumn % TextColumn) < 5);");
|
|
|
|
|
expect_failure(move(result), '%');
|
|
|
|
|
|
|
|
|
|
result = try_execute(database, "SELECT * FROM TestSchema.TestTable WHERE ((IntColumn << TextColumn) < 5);");
|
|
|
|
|
expect_failure(move(result), "<<"sv);
|
|
|
|
|
|
|
|
|
|
result = try_execute(database, "SELECT * FROM TestSchema.TestTable WHERE ((IntColumn >> TextColumn) < 5);");
|
|
|
|
|
expect_failure(move(result), ">>"sv);
|
|
|
|
|
|
|
|
|
|
result = try_execute(database, "SELECT * FROM TestSchema.TestTable WHERE ((IntColumn | TextColumn) < 5);");
|
|
|
|
|
expect_failure(move(result), '|');
|
|
|
|
|
|
|
|
|
|
result = try_execute(database, "SELECT * FROM TestSchema.TestTable WHERE ((IntColumn & TextColumn) < 5);");
|
|
|
|
|
expect_failure(move(result), '&');
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-26 01:17:43 +01:00
|
|
|
TEST_CASE(describe_large_table_after_persist)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
{
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
|
|
|
|
EXPECT(!database->open().is_error());
|
|
|
|
|
|
|
|
|
|
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 );");
|
|
|
|
|
EXPECT_EQ(result.command(), SQL::SQLCommand::Create);
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
|
|
|
|
EXPECT(!database->open().is_error());
|
|
|
|
|
|
|
|
|
|
auto result = execute(database, "DESCRIBE TABLE Cookies;");
|
|
|
|
|
EXPECT_EQ(result.size(), 12u);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-28 07:49:03 -05:00
|
|
|
TEST_CASE(delete_single_row)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
{
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
|
|
|
|
EXPECT(!database->open().is_error());
|
|
|
|
|
|
|
|
|
|
create_table(database);
|
|
|
|
|
for (auto count = 0; count < 10; ++count) {
|
|
|
|
|
auto result = execute(database, String::formatted("INSERT INTO TestSchema.TestTable VALUES ( 'T{}', {} );", count, count));
|
|
|
|
|
EXPECT_EQ(result.size(), 1u);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto result = execute(database, "SELECT * FROM TestSchema.TestTable;");
|
|
|
|
|
EXPECT_EQ(result.size(), 10u);
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
|
|
|
|
EXPECT(!database->open().is_error());
|
|
|
|
|
|
|
|
|
|
execute(database, "DELETE FROM TestSchema.TestTable WHERE (IntColumn = 4);");
|
|
|
|
|
|
|
|
|
|
auto result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable ORDER BY IntColumn;");
|
|
|
|
|
EXPECT_EQ(result.size(), 9u);
|
|
|
|
|
|
|
|
|
|
for (auto i = 0u; i < 4; ++i)
|
|
|
|
|
EXPECT_EQ(result[i].row[0], i);
|
|
|
|
|
for (auto i = 5u; i < 9; ++i)
|
|
|
|
|
EXPECT_EQ(result[i].row[0], i + 1);
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
|
|
|
|
EXPECT(!database->open().is_error());
|
|
|
|
|
|
|
|
|
|
auto result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable ORDER BY IntColumn;");
|
|
|
|
|
EXPECT_EQ(result.size(), 9u);
|
|
|
|
|
|
|
|
|
|
for (auto i = 0u; i < 4; ++i)
|
|
|
|
|
EXPECT_EQ(result[i].row[0], i);
|
|
|
|
|
for (auto i = 5u; i < 9; ++i)
|
|
|
|
|
EXPECT_EQ(result[i].row[0], i + 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE(delete_multiple_rows)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
{
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
|
|
|
|
EXPECT(!database->open().is_error());
|
|
|
|
|
|
|
|
|
|
create_table(database);
|
|
|
|
|
for (auto count = 0; count < 10; ++count) {
|
|
|
|
|
auto result = execute(database, String::formatted("INSERT INTO TestSchema.TestTable VALUES ( 'T{}', {} );", count, count));
|
|
|
|
|
EXPECT_EQ(result.size(), 1u);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto result = execute(database, "SELECT * FROM TestSchema.TestTable;");
|
|
|
|
|
EXPECT_EQ(result.size(), 10u);
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
|
|
|
|
EXPECT(!database->open().is_error());
|
|
|
|
|
|
|
|
|
|
execute(database, "DELETE FROM TestSchema.TestTable WHERE (IntColumn >= 4);");
|
|
|
|
|
|
|
|
|
|
auto result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable ORDER BY IntColumn;");
|
|
|
|
|
EXPECT_EQ(result.size(), 4u);
|
|
|
|
|
|
|
|
|
|
for (auto i = 0u; i < result.size(); ++i)
|
|
|
|
|
EXPECT_EQ(result[i].row[0], i);
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
|
|
|
|
EXPECT(!database->open().is_error());
|
|
|
|
|
|
|
|
|
|
auto result = execute(database, "SELECT IntColumn FROM TestSchema.TestTable ORDER BY IntColumn;");
|
|
|
|
|
EXPECT_EQ(result.size(), 4u);
|
|
|
|
|
|
|
|
|
|
for (auto i = 0u; i < result.size(); ++i)
|
|
|
|
|
EXPECT_EQ(result[i].row[0], i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TEST_CASE(delete_all_rows)
|
|
|
|
|
{
|
|
|
|
|
ScopeGuard guard([]() { unlink(db_name); });
|
|
|
|
|
{
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
|
|
|
|
EXPECT(!database->open().is_error());
|
|
|
|
|
|
|
|
|
|
create_table(database);
|
|
|
|
|
for (auto count = 0; count < 10; ++count) {
|
|
|
|
|
auto result = execute(database, String::formatted("INSERT INTO TestSchema.TestTable VALUES ( 'T{}', {} );", count, count));
|
|
|
|
|
EXPECT_EQ(result.size(), 1u);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto result = execute(database, "SELECT * FROM TestSchema.TestTable;");
|
|
|
|
|
EXPECT_EQ(result.size(), 10u);
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
|
|
|
|
EXPECT(!database->open().is_error());
|
|
|
|
|
|
|
|
|
|
execute(database, "DELETE FROM TestSchema.TestTable;");
|
|
|
|
|
|
|
|
|
|
auto result = execute(database, "SELECT * FROM TestSchema.TestTable;");
|
|
|
|
|
EXPECT(result.is_empty());
|
|
|
|
|
}
|
|
|
|
|
{
|
|
|
|
|
auto database = SQL::Database::construct(db_name);
|
|
|
|
|
EXPECT(!database->open().is_error());
|
|
|
|
|
|
|
|
|
|
auto result = execute(database, "SELECT * FROM TestSchema.TestTable;");
|
|
|
|
|
EXPECT(result.is_empty());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
LibSQL+SQLServer: Bare bones INSERT and SELECT statements
This patch provides very basic, bare bones implementations of the
INSERT and SELECT statements. They are *very* limited:
- The only variant of the INSERT statement that currently works is
SELECT INTO schema.table (column1, column2, ....) VALUES
(value11, value21, ...), (value12, value22, ...), ...
where the values are literals.
- The SELECT statement is even more limited, and is only provided to
allow verification of the INSERT statement. The only form implemented
is: SELECT * FROM schema.table
These statements required a bit of change in the Statement::execute
API. Originally execute only received a Database object as parameter.
This is not enough; we now pass an ExecutionContext object which
contains the Database, the current result set, and the last Tuple read
from the database. This object will undoubtedly evolve over time.
This API change dragged SQLServer::SQLStatement into the patch.
Another API addition is Expression::evaluate. This method is,
unsurprisingly, used to evaluate expressions, like the values in the
INSERT statement.
Finally, a new test file is added: TestSqlStatementExecution, which
tests the currently implemented statements. As the number and flavour of
implemented statements grows, this test file will probably have to be
restructured.
2021-07-19 19:48:46 -04:00
|
|
|
}
|