
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).
201 lines
5.2 KiB
C++
201 lines
5.2 KiB
C++
/*
|
|
* Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <LibSQL/Key.h>
|
|
#include <LibSQL/Meta.h>
|
|
#include <LibSQL/Type.h>
|
|
|
|
namespace SQL {
|
|
|
|
SchemaDef::SchemaDef(String name)
|
|
: Relation(move(name))
|
|
{
|
|
}
|
|
|
|
SchemaDef::SchemaDef(Key const& key)
|
|
: Relation(key["schema_name"].to_string())
|
|
{
|
|
}
|
|
|
|
Key SchemaDef::key() const
|
|
{
|
|
auto key = Key(index_def()->to_tuple_descriptor());
|
|
key["schema_name"] = name();
|
|
key.set_pointer(pointer());
|
|
return key;
|
|
}
|
|
|
|
Key SchemaDef::make_key()
|
|
{
|
|
return Key(index_def());
|
|
}
|
|
|
|
NonnullRefPtr<IndexDef> SchemaDef::index_def()
|
|
{
|
|
NonnullRefPtr<IndexDef> s_index_def = IndexDef::construct("$schema", true, 0);
|
|
if (!s_index_def->size()) {
|
|
s_index_def->append_column("schema_name", SQLType::Text, Order::Ascending);
|
|
}
|
|
return s_index_def;
|
|
}
|
|
|
|
ColumnDef::ColumnDef(Relation* parent, size_t column_number, String name, SQLType sql_type)
|
|
: Relation(move(name), parent)
|
|
, m_index(column_number)
|
|
, m_type(sql_type)
|
|
{
|
|
}
|
|
|
|
Key ColumnDef::key() const
|
|
{
|
|
auto key = Key(index_def());
|
|
key["table_hash"] = parent_relation()->hash();
|
|
key["column_number"] = (int)column_number();
|
|
key["column_name"] = name();
|
|
key["column_type"] = (int)type();
|
|
return key;
|
|
}
|
|
|
|
Key ColumnDef::make_key(TableDef const& table_def)
|
|
{
|
|
Key key(index_def());
|
|
key["table_hash"] = table_def.key().hash();
|
|
return key;
|
|
}
|
|
|
|
NonnullRefPtr<IndexDef> ColumnDef::index_def()
|
|
{
|
|
NonnullRefPtr<IndexDef> s_index_def = IndexDef::construct("$column", true, 0);
|
|
if (!s_index_def->size()) {
|
|
s_index_def->append_column("table_hash", SQLType::Integer, Order::Ascending);
|
|
s_index_def->append_column("column_number", SQLType::Integer, Order::Ascending);
|
|
s_index_def->append_column("column_name", SQLType::Text, Order::Ascending);
|
|
s_index_def->append_column("column_type", SQLType::Integer, Order::Ascending);
|
|
}
|
|
return s_index_def;
|
|
}
|
|
|
|
KeyPartDef::KeyPartDef(IndexDef* index, String name, SQLType sql_type, Order sort_order)
|
|
: ColumnDef(index, index->size(), move(name), sql_type)
|
|
, m_sort_order(sort_order)
|
|
{
|
|
}
|
|
|
|
IndexDef::IndexDef(TableDef* table, String name, bool unique, u32 pointer)
|
|
: Relation(move(name), pointer, table)
|
|
, m_key_definition()
|
|
, m_unique(unique)
|
|
{
|
|
}
|
|
|
|
IndexDef::IndexDef(String name, bool unique, u32 pointer)
|
|
: IndexDef(nullptr, move(name), unique, pointer)
|
|
{
|
|
}
|
|
|
|
void IndexDef::append_column(String name, SQLType sql_type, Order sort_order)
|
|
{
|
|
auto part = KeyPartDef::construct(this, move(name), sql_type, sort_order);
|
|
m_key_definition.append(part);
|
|
}
|
|
|
|
NonnullRefPtr<TupleDescriptor> IndexDef::to_tuple_descriptor() const
|
|
{
|
|
NonnullRefPtr<TupleDescriptor> ret = adopt_ref(*new TupleDescriptor);
|
|
for (auto& part : m_key_definition) {
|
|
ret->append({ part.name(), part.type(), part.sort_order() });
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
Key IndexDef::key() const
|
|
{
|
|
auto key = Key(index_def()->to_tuple_descriptor());
|
|
key["table_hash"] = parent_relation()->key().hash();
|
|
key["index_name"] = name();
|
|
key["unique"] = unique() ? 1 : 0;
|
|
return key;
|
|
}
|
|
|
|
Key IndexDef::make_key(TableDef const& table_def)
|
|
{
|
|
Key key(index_def());
|
|
key["table_hash"] = table_def.key().hash();
|
|
return key;
|
|
}
|
|
|
|
NonnullRefPtr<IndexDef> IndexDef::index_def()
|
|
{
|
|
NonnullRefPtr<IndexDef> s_index_def = IndexDef::construct("$index", true, 0);
|
|
if (!s_index_def->size()) {
|
|
s_index_def->append_column("table_hash", SQLType::Integer, Order::Ascending);
|
|
s_index_def->append_column("index_name", SQLType::Text, Order::Ascending);
|
|
s_index_def->append_column("unique", SQLType::Integer, Order::Ascending);
|
|
}
|
|
return s_index_def;
|
|
}
|
|
|
|
TableDef::TableDef(SchemaDef* schema, String name)
|
|
: Relation(move(name), schema)
|
|
, m_columns()
|
|
, m_indexes()
|
|
{
|
|
}
|
|
|
|
NonnullRefPtr<TupleDescriptor> TableDef::to_tuple_descriptor() const
|
|
{
|
|
NonnullRefPtr<TupleDescriptor> ret = adopt_ref(*new TupleDescriptor);
|
|
for (auto& part : m_columns) {
|
|
ret->append({ part.name(), part.type(), Order::Ascending });
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
Key TableDef::key() const
|
|
{
|
|
auto key = Key(index_def()->to_tuple_descriptor());
|
|
key["schema_hash"] = parent_relation()->key().hash();
|
|
key["table_name"] = name();
|
|
key.set_pointer(pointer());
|
|
return key;
|
|
}
|
|
|
|
void TableDef::append_column(String name, SQLType sql_type)
|
|
{
|
|
auto column = ColumnDef::construct(this, num_columns(), move(name), sql_type);
|
|
m_columns.append(column);
|
|
}
|
|
|
|
void TableDef::append_column(Key const& column)
|
|
{
|
|
append_column(
|
|
(String)column["column_name"],
|
|
(SQLType)((int)column["column_type"]));
|
|
}
|
|
|
|
Key TableDef::make_key(SchemaDef const& schema_def)
|
|
{
|
|
return TableDef::make_key(schema_def.key());
|
|
}
|
|
|
|
Key TableDef::make_key(Key const& schema_key)
|
|
{
|
|
Key key(index_def());
|
|
key["schema_hash"] = schema_key.hash();
|
|
return key;
|
|
}
|
|
|
|
NonnullRefPtr<IndexDef> TableDef::index_def()
|
|
{
|
|
NonnullRefPtr<IndexDef> s_index_def = IndexDef::construct("$table", true, 0);
|
|
if (!s_index_def->size()) {
|
|
s_index_def->append_column("schema_hash", SQLType::Integer, Order::Ascending);
|
|
s_index_def->append_column("table_name", SQLType::Text, Order::Ascending);
|
|
}
|
|
return s_index_def;
|
|
}
|
|
|
|
}
|