2021-06-17 14:12:46 -04:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
|
2021-12-06 15:30:38 +02:00
|
|
|
* Copyright (c) 2021, Mahmoud Mandour <ma.mandourr@gmail.com>
|
2021-06-17 14:12:46 -04:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
2021-06-17 14:12:46 -04:00
|
|
|
#include <AK/RefPtr.h>
|
|
|
|
|
|
|
|
|
|
#include <LibSQL/BTree.h>
|
|
|
|
|
#include <LibSQL/Database.h>
|
|
|
|
|
#include <LibSQL/Heap.h>
|
|
|
|
|
#include <LibSQL/Meta.h>
|
|
|
|
|
#include <LibSQL/Row.h>
|
|
|
|
|
#include <LibSQL/Tuple.h>
|
|
|
|
|
|
|
|
|
|
namespace SQL {
|
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
Database::Database(DeprecatedString name)
|
2021-11-05 19:05:59 -04:00
|
|
|
: m_heap(Heap::construct(move(name)))
|
2021-08-18 20:50:13 -04:00
|
|
|
, m_serializer(m_heap)
|
2021-06-17 14:12:46 -04:00
|
|
|
{
|
2021-11-05 19:05:59 -04:00
|
|
|
}
|
|
|
|
|
|
2022-11-29 08:24:15 -05:00
|
|
|
ResultOr<void> Database::open()
|
2021-11-05 19:05:59 -04:00
|
|
|
{
|
|
|
|
|
TRY(m_heap->open());
|
2022-11-29 08:24:15 -05:00
|
|
|
|
2021-11-05 19:05:59 -04:00
|
|
|
m_schemas = BTree::construct(m_serializer, SchemaDef::index_def()->to_tuple_descriptor(), m_heap->schemas_root());
|
2021-06-17 14:12:46 -04:00
|
|
|
m_schemas->on_new_root = [&]() {
|
|
|
|
|
m_heap->set_schemas_root(m_schemas->root());
|
|
|
|
|
};
|
2021-11-05 19:05:59 -04:00
|
|
|
|
|
|
|
|
m_tables = BTree::construct(m_serializer, TableDef::index_def()->to_tuple_descriptor(), m_heap->tables_root());
|
2021-06-17 14:12:46 -04:00
|
|
|
m_tables->on_new_root = [&]() {
|
|
|
|
|
m_heap->set_tables_root(m_tables->root());
|
|
|
|
|
};
|
2021-11-05 19:05:59 -04:00
|
|
|
|
|
|
|
|
m_table_columns = BTree::construct(m_serializer, ColumnDef::index_def()->to_tuple_descriptor(), m_heap->table_columns_root());
|
2021-06-17 14:12:46 -04:00
|
|
|
m_table_columns->on_new_root = [&]() {
|
|
|
|
|
m_heap->set_table_columns_root(m_table_columns->root());
|
|
|
|
|
};
|
2021-11-05 19:05:59 -04:00
|
|
|
|
|
|
|
|
m_open = true;
|
2021-12-06 15:30:38 +02:00
|
|
|
|
2022-11-29 08:24:15 -05:00
|
|
|
auto ensure_schema_exists = [&](auto schema_name) -> ResultOr<NonnullRefPtr<SchemaDef>> {
|
|
|
|
|
if (auto result = get_schema(schema_name); result.is_error()) {
|
|
|
|
|
if (result.error().error() != SQLErrorCode::SchemaDoesNotExist)
|
|
|
|
|
return result.release_error();
|
|
|
|
|
|
|
|
|
|
auto schema_def = SchemaDef::construct(schema_name);
|
|
|
|
|
TRY(add_schema(*schema_def));
|
|
|
|
|
return schema_def;
|
|
|
|
|
} else {
|
|
|
|
|
return result.release_value();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
(void)TRY(ensure_schema_exists("default"sv));
|
|
|
|
|
auto master_schema = TRY(ensure_schema_exists("master"sv));
|
2021-12-06 15:30:38 +02:00
|
|
|
|
2022-11-29 08:47:22 -05:00
|
|
|
if (auto result = get_table("master"sv, "internal_describe_table"sv); result.is_error()) {
|
|
|
|
|
if (result.error().error() != SQLErrorCode::TableDoesNotExist)
|
|
|
|
|
return result.release_error();
|
|
|
|
|
|
|
|
|
|
auto internal_describe_table = TableDef::construct(master_schema, "internal_describe_table");
|
|
|
|
|
internal_describe_table->append_column("Name", SQLType::Text);
|
|
|
|
|
internal_describe_table->append_column("Type", SQLType::Text);
|
|
|
|
|
TRY(add_table(*internal_describe_table));
|
2021-12-06 15:30:38 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-05 19:05:59 -04:00
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-27 09:06:13 -04:00
|
|
|
Database::~Database() = default;
|
2021-11-05 19:05:59 -04:00
|
|
|
|
|
|
|
|
ErrorOr<void> Database::commit()
|
|
|
|
|
{
|
|
|
|
|
VERIFY(is_open());
|
|
|
|
|
TRY(m_heap->flush());
|
|
|
|
|
return {};
|
2021-06-17 14:12:46 -04:00
|
|
|
}
|
|
|
|
|
|
2022-11-29 08:24:15 -05:00
|
|
|
ResultOr<void> Database::add_schema(SchemaDef const& schema)
|
2021-06-17 14:12:46 -04:00
|
|
|
{
|
2021-11-05 19:05:59 -04:00
|
|
|
VERIFY(is_open());
|
2022-11-29 08:24:15 -05:00
|
|
|
|
|
|
|
|
if (!m_schemas->insert(schema.key()))
|
|
|
|
|
return Result { SQLCommand::Unknown, SQLErrorCode::SchemaExists, schema.name() };
|
2021-11-05 19:05:59 -04:00
|
|
|
return {};
|
2021-06-17 14:12:46 -04:00
|
|
|
}
|
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
Key Database::get_schema_key(DeprecatedString const& schema_name)
|
2021-06-17 14:12:46 -04:00
|
|
|
{
|
|
|
|
|
auto key = SchemaDef::make_key();
|
|
|
|
|
key["schema_name"] = schema_name;
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
ResultOr<NonnullRefPtr<SchemaDef>> Database::get_schema(DeprecatedString const& schema)
|
2021-06-17 14:12:46 -04:00
|
|
|
{
|
2021-11-05 19:05:59 -04:00
|
|
|
VERIFY(is_open());
|
2022-11-29 08:24:15 -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
|
|
|
auto schema_name = schema;
|
2022-11-29 08:24:15 -05:00
|
|
|
if (schema.is_empty())
|
|
|
|
|
schema_name = "default"sv;
|
|
|
|
|
|
2021-06-17 14:12:46 -04:00
|
|
|
Key key = get_schema_key(schema_name);
|
2022-11-29 08:24:15 -05:00
|
|
|
if (auto it = m_schema_cache.find(key.hash()); it != m_schema_cache.end())
|
|
|
|
|
return it->value;
|
|
|
|
|
|
2021-06-17 14:12:46 -04:00
|
|
|
auto schema_iterator = m_schemas->find(key);
|
2022-11-29 08:24:15 -05:00
|
|
|
if (schema_iterator.is_end() || (*schema_iterator != key))
|
|
|
|
|
return Result { SQLCommand::Unknown, SQLErrorCode::SchemaDoesNotExist, schema_name };
|
|
|
|
|
|
|
|
|
|
auto schema_def = SchemaDef::construct(*schema_iterator);
|
|
|
|
|
m_schema_cache.set(key.hash(), schema_def);
|
|
|
|
|
return schema_def;
|
2021-06-17 14:12:46 -04:00
|
|
|
}
|
|
|
|
|
|
2022-11-29 08:47:22 -05:00
|
|
|
ResultOr<void> Database::add_table(TableDef& table)
|
2021-06-17 14:12:46 -04:00
|
|
|
{
|
2021-11-05 19:05:59 -04:00
|
|
|
VERIFY(is_open());
|
2022-11-29 08:47:22 -05:00
|
|
|
|
|
|
|
|
if (!m_tables->insert(table.key()))
|
|
|
|
|
return Result { SQLCommand::Unknown, SQLErrorCode::TableExists, table.name() };
|
|
|
|
|
|
2021-06-17 14:12:46 -04:00
|
|
|
for (auto& column : table.columns()) {
|
2022-11-29 08:47:22 -05:00
|
|
|
if (!m_table_columns->insert(column.key()))
|
|
|
|
|
VERIFY_NOT_REACHED();
|
2021-06-17 14:12:46 -04:00
|
|
|
}
|
2022-11-29 08:47:22 -05:00
|
|
|
|
2021-11-05 19:05:59 -04:00
|
|
|
return {};
|
2021-06-17 14:12:46 -04:00
|
|
|
}
|
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
Key Database::get_table_key(DeprecatedString const& schema_name, DeprecatedString const& table_name)
|
2021-06-17 14:12:46 -04:00
|
|
|
{
|
|
|
|
|
auto key = TableDef::make_key(get_schema_key(schema_name));
|
|
|
|
|
key["table_name"] = table_name;
|
|
|
|
|
return key;
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
ResultOr<NonnullRefPtr<TableDef>> Database::get_table(DeprecatedString const& schema, DeprecatedString const& name)
|
2021-06-17 14:12:46 -04:00
|
|
|
{
|
2021-11-05 19:05:59 -04:00
|
|
|
VERIFY(is_open());
|
2022-11-29 08:47:22 -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
|
|
|
auto schema_name = schema;
|
2022-11-29 08:47:22 -05:00
|
|
|
if (schema.is_empty())
|
|
|
|
|
schema_name = "default"sv;
|
|
|
|
|
|
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
|
|
|
Key key = get_table_key(schema_name, name);
|
2022-11-29 08:47:22 -05:00
|
|
|
if (auto it = m_table_cache.find(key.hash()); it != m_table_cache.end())
|
|
|
|
|
return it->value;
|
|
|
|
|
|
2021-06-17 14:12:46 -04:00
|
|
|
auto table_iterator = m_tables->find(key);
|
2022-11-29 08:47:22 -05:00
|
|
|
if (table_iterator.is_end() || (*table_iterator != key))
|
2022-12-04 18:02:33 +00:00
|
|
|
return Result { SQLCommand::Unknown, SQLErrorCode::TableDoesNotExist, DeprecatedString::formatted("{}.{}", schema_name, name) };
|
2022-11-29 08:47:22 -05:00
|
|
|
|
2021-11-05 19:05:59 -04:00
|
|
|
auto schema_def = TRY(get_schema(schema));
|
2022-11-29 08:47:22 -05:00
|
|
|
auto table_def = TableDef::construct(schema_def, name);
|
|
|
|
|
table_def->set_pointer((*table_iterator).pointer());
|
|
|
|
|
m_table_cache.set(key.hash(), table_def);
|
|
|
|
|
|
|
|
|
|
auto table_hash = table_def->hash();
|
|
|
|
|
auto column_key = ColumnDef::make_key(table_def);
|
LibSQL: Support 64-bit integer values and handle overflow errors
Currently, integers are stored in LibSQL as 32-bit signed integers, even
if the provided type is unsigned. This resulted in a series of unchecked
unsigned-to-signed conversions, and prevented storing 64-bit values.
Further, mathematical operations were performed without similar checks,
and without checking for overflow.
This changes SQL::Value to behave like SQLite for INTEGER types. In
SQLite, the INTEGER type does not imply a size or signedness of the
underlying type. Instead, SQLite determines on-the-fly what type is
needed as values are created and updated.
To do so, the SQL::Value variant can now hold an i64 or u64 integer. If
a specific type is requested, invalid conversions are now explictly an
error (e.g. converting a stored -1 to a u64 will fail). When binary
mathematical operations are performed, we now try to coerce the RHS
value to a type that works with the LHS value, failing the operation if
that isn't possible. Any overflow or invalid operation (e.g. bitshifting
a 64-bit value by more than 64 bytes) is an error.
2022-12-11 11:44:11 -05:00
|
|
|
for (auto it = m_table_columns->find(column_key); !it.is_end() && ((*it)["table_hash"].to_int<u32>() == table_hash); ++it)
|
2022-11-29 08:47:22 -05:00
|
|
|
table_def->append_column(*it);
|
|
|
|
|
|
|
|
|
|
return table_def;
|
2021-06-17 14:12:46 -04:00
|
|
|
}
|
|
|
|
|
|
2023-02-19 23:22:46 +01:00
|
|
|
ErrorOr<Vector<Row>> Database::select_all(TableDef& table)
|
2021-06-17 14:12:46 -04:00
|
|
|
{
|
|
|
|
|
VERIFY(m_table_cache.get(table.key().hash()).has_value());
|
|
|
|
|
Vector<Row> ret;
|
|
|
|
|
for (auto pointer = table.pointer(); pointer; pointer = ret.last().next_pointer()) {
|
2021-08-18 20:50:13 -04:00
|
|
|
ret.append(m_serializer.deserialize_block<Row>(pointer, table, pointer));
|
2021-06-17 14:12:46 -04:00
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-19 23:22:46 +01:00
|
|
|
ErrorOr<Vector<Row>> Database::match(TableDef& table, Key const& key)
|
2021-06-17 14:12:46 -04:00
|
|
|
{
|
|
|
|
|
VERIFY(m_table_cache.get(table.key().hash()).has_value());
|
|
|
|
|
Vector<Row> ret;
|
|
|
|
|
|
|
|
|
|
// TODO Match key against indexes defined on table. If found,
|
|
|
|
|
// use the index instead of scanning the table.
|
|
|
|
|
for (auto pointer = table.pointer(); pointer;) {
|
2021-08-18 20:50:13 -04:00
|
|
|
auto row = m_serializer.deserialize_block<Row>(pointer, table, pointer);
|
2021-06-17 14:12:46 -04:00
|
|
|
if (row.match(key))
|
|
|
|
|
ret.append(row);
|
|
|
|
|
pointer = ret.last().next_pointer();
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-05 19:05:59 -04:00
|
|
|
ErrorOr<void> Database::insert(Row& row)
|
2021-06-17 14:12:46 -04:00
|
|
|
{
|
2022-11-29 08:52:09 -05:00
|
|
|
VERIFY(m_table_cache.get(row.table().key().hash()).has_value());
|
2021-11-05 19:05:59 -04:00
|
|
|
// TODO Check constraints
|
|
|
|
|
|
2021-06-17 14:12:46 -04:00
|
|
|
row.set_pointer(m_heap->new_record_pointer());
|
2022-11-29 08:52:09 -05:00
|
|
|
row.set_next_pointer(row.table().pointer());
|
2021-11-05 19:05:59 -04:00
|
|
|
TRY(update(row));
|
2021-06-17 14:12:46 -04:00
|
|
|
|
|
|
|
|
// TODO update indexes defined on table.
|
|
|
|
|
|
2022-11-29 08:52:09 -05:00
|
|
|
auto table_key = row.table().key();
|
2021-06-17 14:12:46 -04:00
|
|
|
table_key.set_pointer(row.pointer());
|
|
|
|
|
VERIFY(m_tables->update_key_pointer(table_key));
|
2022-11-29 08:52:09 -05:00
|
|
|
row.table().set_pointer(row.pointer());
|
2021-11-05 19:05:59 -04:00
|
|
|
return {};
|
2021-06-17 14:12:46 -04:00
|
|
|
}
|
|
|
|
|
|
2022-11-28 07:49:03 -05:00
|
|
|
ErrorOr<void> Database::remove(Row& row)
|
|
|
|
|
{
|
|
|
|
|
auto& table = row.table();
|
|
|
|
|
VERIFY(m_table_cache.get(table.key().hash()).has_value());
|
|
|
|
|
|
|
|
|
|
if (table.pointer() == row.pointer()) {
|
|
|
|
|
auto table_key = table.key();
|
|
|
|
|
table_key.set_pointer(row.next_pointer());
|
|
|
|
|
m_tables->update_key_pointer(table_key);
|
|
|
|
|
|
|
|
|
|
table.set_pointer(row.next_pointer());
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (auto pointer = table.pointer(); pointer;) {
|
|
|
|
|
auto current = m_serializer.deserialize_block<Row>(pointer, table, pointer);
|
|
|
|
|
|
|
|
|
|
if (current.next_pointer() == row.pointer()) {
|
|
|
|
|
current.set_next_pointer(row.next_pointer());
|
|
|
|
|
TRY(update(current));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pointer = current.next_pointer();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-05 19:05:59 -04:00
|
|
|
ErrorOr<void> Database::update(Row& tuple)
|
2021-06-17 14:12:46 -04:00
|
|
|
{
|
2022-11-29 08:52:09 -05:00
|
|
|
VERIFY(m_table_cache.get(tuple.table().key().hash()).has_value());
|
2021-11-05 19:05:59 -04:00
|
|
|
// TODO Check constraints
|
2021-08-18 20:50:13 -04:00
|
|
|
m_serializer.reset();
|
2022-11-26 01:17:43 +01:00
|
|
|
m_serializer.serialize_and_write<Tuple>(tuple);
|
2021-06-17 14:12:46 -04:00
|
|
|
|
|
|
|
|
// TODO update indexes defined on table.
|
2021-11-05 19:05:59 -04:00
|
|
|
return {};
|
2021-06-17 14:12:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|