
The implemtation of the Value class was based on lambda member variables implementing type-dependent behaviour. This was done to ensure that Values can be used as stack-only objects; the simplest alternative, virtual methods, forces them onto the heap. The problem with the the lambda approach is that it bloats the Values (which are supposed to be lightweight objects) quite considerably, because every object contains more than a dozen function pointers. The solution to address both problems (we want Values to be able to live on the stack and be as lightweight as possible) chosen here is to encapsulate type-dependent behaviour and state in an implementation class, and let the Value be an AK::Variant of those implementation classes. All methods of Value are now basically straight delegates to the implementation object using the Variant::visit method. One issue complicating matters is the addition of two aggregate types, Tuple and Array, which each contain a Vector of Values. At this point Tuples and Arrays (and potential future aggregate types) can't contain these aggregate types. This is limiting and needs to be addressed. Another area that needs attention is the nomenclature of things; it's a bit of a tangle of 'ValueBlahBlah' and 'ImplBlahBlah'. It makes sense right now I think but admit we probably can do better. Other things included here: - Added the Boolean and Null types (and Tuple and Array, see above). - to_string now always succeeds and returns a String instead of an Optional. This had some impact on other sources. - Added a lot of tests. - Started moving the serialization mechanism more towards where I want it to be, i.e. a 'DataSerializer' object which just takes serialization and deserialization requests and knows for example how to store long strings out-of-line. One last remark: There is obviously a naming clash between the Tuple class and the Tuple Value type. This is intentional; I plan to make the Tuple class a subclass of Value (and hence Key and Row as well).
176 lines
4.3 KiB
C++
176 lines
4.3 KiB
C++
/*
|
|
* Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <AK/ScopeGuard.h>
|
|
#include <LibSQL/BTree.h>
|
|
#include <LibSQL/Database.h>
|
|
#include <LibSQL/Heap.h>
|
|
#include <LibSQL/Meta.h>
|
|
#include <LibSQL/Row.h>
|
|
#include <LibSQL/Value.h>
|
|
#include <LibTest/TestCase.h>
|
|
|
|
NonnullRefPtr<SQL::SchemaDef> setup_schema(SQL::Database&);
|
|
NonnullRefPtr<SQL::SchemaDef> setup_table(SQL::Database&);
|
|
void insert_into_table(SQL::Database&, int);
|
|
void verify_table_contents(SQL::Database&, int);
|
|
void insert_and_verify(int);
|
|
|
|
NonnullRefPtr<SQL::SchemaDef> setup_schema(SQL::Database& db)
|
|
{
|
|
auto schema = SQL::SchemaDef::construct("TestSchema");
|
|
db.add_schema(schema);
|
|
return schema;
|
|
}
|
|
|
|
NonnullRefPtr<SQL::SchemaDef> setup_table(SQL::Database& db)
|
|
{
|
|
auto schema = setup_schema(db);
|
|
auto table = SQL::TableDef::construct(schema, "TestTable");
|
|
db.add_table(table);
|
|
table->append_column("TextColumn", SQL::SQLType::Text);
|
|
table->append_column("IntColumn", SQL::SQLType::Integer);
|
|
EXPECT_EQ(table->num_columns(), 2u);
|
|
db.add_table(table);
|
|
return table;
|
|
}
|
|
|
|
void insert_into_table(SQL::Database& db, int count)
|
|
{
|
|
auto table = db.get_table("TestSchema", "TestTable");
|
|
EXPECT(table);
|
|
|
|
for (int ix = 0; ix < count; ix++) {
|
|
SQL::Row row(*table);
|
|
StringBuilder builder;
|
|
builder.appendff("Test{}", ix);
|
|
|
|
row["TextColumn"] = builder.build();
|
|
row["IntColumn"] = ix;
|
|
EXPECT(db.insert(row));
|
|
}
|
|
}
|
|
|
|
void verify_table_contents(SQL::Database& db, int expected_count)
|
|
{
|
|
auto table = db.get_table("TestSchema", "TestTable");
|
|
EXPECT(table);
|
|
|
|
int sum = 0;
|
|
int count = 0;
|
|
for (auto& row : db.select_all(*table)) {
|
|
StringBuilder builder;
|
|
builder.appendff("Test{}", row["IntColumn"].to_int().value());
|
|
EXPECT_EQ(row["TextColumn"].to_string(), builder.build());
|
|
count++;
|
|
sum += row["IntColumn"].to_int().value();
|
|
}
|
|
EXPECT_EQ(count, expected_count);
|
|
EXPECT_EQ(sum, (expected_count * (expected_count - 1)) / 2);
|
|
}
|
|
|
|
void insert_and_verify(int count)
|
|
{
|
|
ScopeGuard guard([]() { unlink("/tmp/test.db"); });
|
|
{
|
|
auto db = SQL::Database::construct("/tmp/test.db");
|
|
setup_table(db);
|
|
db->commit();
|
|
}
|
|
{
|
|
auto db = SQL::Database::construct("/tmp/test.db");
|
|
insert_into_table(db, count);
|
|
db->commit();
|
|
}
|
|
{
|
|
auto db = SQL::Database::construct("/tmp/test.db");
|
|
verify_table_contents(db, count);
|
|
}
|
|
}
|
|
|
|
TEST_CASE(create_heap)
|
|
{
|
|
ScopeGuard guard([]() { unlink("/tmp/test.db"); });
|
|
auto heap = SQL::Heap::construct("/tmp/test.db");
|
|
EXPECT_EQ(heap->version(), 0x00000001u);
|
|
}
|
|
|
|
TEST_CASE(create_database)
|
|
{
|
|
ScopeGuard guard([]() { unlink("/tmp/test.db"); });
|
|
auto db = SQL::Database::construct("/tmp/test.db");
|
|
db->commit();
|
|
}
|
|
|
|
TEST_CASE(add_schema_to_database)
|
|
{
|
|
ScopeGuard guard([]() { unlink("/tmp/test.db"); });
|
|
auto db = SQL::Database::construct("/tmp/test.db");
|
|
setup_schema(db);
|
|
db->commit();
|
|
}
|
|
|
|
TEST_CASE(get_schema_from_database)
|
|
{
|
|
ScopeGuard guard([]() { unlink("/tmp/test.db"); });
|
|
{
|
|
auto db = SQL::Database::construct("/tmp/test.db");
|
|
setup_schema(db);
|
|
db->commit();
|
|
}
|
|
{
|
|
auto db = SQL::Database::construct("/tmp/test.db");
|
|
auto schema = db->get_schema("TestSchema");
|
|
EXPECT(schema);
|
|
}
|
|
}
|
|
|
|
TEST_CASE(add_table_to_database)
|
|
{
|
|
ScopeGuard guard([]() { unlink("/tmp/test.db"); });
|
|
auto db = SQL::Database::construct("/tmp/test.db");
|
|
setup_table(db);
|
|
db->commit();
|
|
}
|
|
|
|
TEST_CASE(get_table_from_database)
|
|
{
|
|
ScopeGuard guard([]() { unlink("/tmp/test.db"); });
|
|
{
|
|
auto db = SQL::Database::construct("/tmp/test.db");
|
|
setup_table(db);
|
|
db->commit();
|
|
}
|
|
{
|
|
auto db = SQL::Database::construct("/tmp/test.db");
|
|
auto table = db->get_table("TestSchema", "TestTable");
|
|
EXPECT(table);
|
|
EXPECT_EQ(table->name(), "TestTable");
|
|
EXPECT_EQ(table->num_columns(), 2u);
|
|
}
|
|
}
|
|
|
|
TEST_CASE(insert_one_into_and_select_from_table)
|
|
{
|
|
insert_and_verify(1);
|
|
}
|
|
|
|
TEST_CASE(insert_two_into_table)
|
|
{
|
|
insert_and_verify(2);
|
|
}
|
|
|
|
TEST_CASE(insert_10_into_table)
|
|
{
|
|
insert_and_verify(10);
|
|
}
|
|
|
|
TEST_CASE(insert_100_into_table)
|
|
{
|
|
insert_and_verify(100);
|
|
}
|