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>
|
|
|
|
|
|
|
|
#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 {
|
|
|
|
|
|
|
|
constexpr const char* db_name = "/tmp/test.db";
|
|
|
|
|
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());
|
|
|
|
EXPECT(schema_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
|
|
|
}
|
|
|
|
|
|
|
|
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());
|
|
|
|
EXPECT(table_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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2021-11-05 19:05:59 -04:00
|
|
|
auto table_or_error = database->get_table("TESTSCHEMA", "TESTTABLE");
|
|
|
|
EXPECT(!table_or_error.is_error());
|
|
|
|
auto table = table_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
|
|
|
|
|
|
|
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
|
|
|
|
2021-11-05 19:05:59 -04:00
|
|
|
auto table_or_error = database->get_table("TESTSCHEMA", "TESTTABLE");
|
|
|
|
EXPECT(!table_or_error.is_error());
|
|
|
|
auto rows_or_error = database->select_all(*(table_or_error.value()));
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|