From 7ea54db43062a15615735eda2b23c214cc7a0d1a Mon Sep 17 00:00:00 2001 From: Jan de Visser Date: Tue, 2 Nov 2021 16:39:00 -0400 Subject: [PATCH] LibSQL: Add 'schema' and 'table' to TupleElementDescriptor These are needed to distinguish columns from different tables with the same column name in one and the same (joined) Tuple. Not quite happy yet with this API; I think some sort of hierarchical structure would be better but we'll burn that bridge when we get there :^) --- Tests/LibSQL/TestSqlBtreeIndex.cpp | 2 +- Tests/LibSQL/TestSqlHashIndex.cpp | 4 +-- Tests/LibSQL/TestSqlValueAndTuple.cpp | 36 ++++++++++----------- Userland/Libraries/LibSQL/Meta.cpp | 4 +-- Userland/Libraries/LibSQL/TupleDescriptor.h | 2 ++ Userland/Libraries/LibSQL/Value.cpp | 2 +- Userland/Libraries/LibSQL/Value.h | 2 +- 7 files changed, 27 insertions(+), 25 deletions(-) diff --git a/Tests/LibSQL/TestSqlBtreeIndex.cpp b/Tests/LibSQL/TestSqlBtreeIndex.cpp index 9f09560ce60..92d63cf6e78 100644 --- a/Tests/LibSQL/TestSqlBtreeIndex.cpp +++ b/Tests/LibSQL/TestSqlBtreeIndex.cpp @@ -127,7 +127,7 @@ void insert_into_and_scan_btree(int); NonnullRefPtr setup_btree(SQL::Serializer& serializer) { NonnullRefPtr tuple_descriptor = adopt_ref(*new SQL::TupleDescriptor); - tuple_descriptor->append({ "key_value", SQL::SQLType::Integer, SQL::Order::Ascending }); + tuple_descriptor->append({ "schema", "table", "key_value", SQL::SQLType::Integer, SQL::Order::Ascending }); auto root_pointer = serializer.heap().user_value(0); if (!root_pointer) { diff --git a/Tests/LibSQL/TestSqlHashIndex.cpp b/Tests/LibSQL/TestSqlHashIndex.cpp index d379f973033..57f862a1d33 100644 --- a/Tests/LibSQL/TestSqlHashIndex.cpp +++ b/Tests/LibSQL/TestSqlHashIndex.cpp @@ -124,8 +124,8 @@ void insert_into_and_scan_hash_index(int); NonnullRefPtr setup_hash_index(SQL::Serializer& serializer) { NonnullRefPtr tuple_descriptor = adopt_ref(*new SQL::TupleDescriptor); - tuple_descriptor->append({ "key_value", SQL::SQLType::Integer, SQL::Order::Ascending }); - tuple_descriptor->append({ "text_value", SQL::SQLType::Text, SQL::Order::Ascending }); + tuple_descriptor->append({ "schema", "table", "key_value", SQL::SQLType::Integer, SQL::Order::Ascending }); + tuple_descriptor->append({ "schema", "table", "text_value", SQL::SQLType::Text, SQL::Order::Ascending }); auto directory_pointer = serializer.heap().user_value(0); if (!directory_pointer) { diff --git a/Tests/LibSQL/TestSqlValueAndTuple.cpp b/Tests/LibSQL/TestSqlValueAndTuple.cpp index 59ebde4b724..23e6f2f0fc2 100644 --- a/Tests/LibSQL/TestSqlValueAndTuple.cpp +++ b/Tests/LibSQL/TestSqlValueAndTuple.cpp @@ -286,8 +286,8 @@ TEST_CASE(serialize_boolean_value) TEST_CASE(tuple_value) { NonnullRefPtr descriptor = adopt_ref(*new SQL::TupleDescriptor); - descriptor->append({ "col1", SQL::SQLType::Text, SQL::Order::Ascending }); - descriptor->append({ "col2", SQL::SQLType::Integer, SQL::Order::Descending }); + descriptor->append({ "schema", "table", "col1", SQL::SQLType::Text, SQL::Order::Ascending }); + descriptor->append({ "schema", "table", "col2", SQL::SQLType::Integer, SQL::Order::Descending }); auto v = SQL::Value::create_tuple(descriptor); Vector values; @@ -303,8 +303,8 @@ TEST_CASE(tuple_value) TEST_CASE(copy_tuple_value) { NonnullRefPtr descriptor = adopt_ref(*new SQL::TupleDescriptor); - descriptor->append({ "col1", SQL::SQLType::Text, SQL::Order::Ascending }); - descriptor->append({ "col2", SQL::SQLType::Integer, SQL::Order::Descending }); + descriptor->append({ "schema", "table", "col1", SQL::SQLType::Text, SQL::Order::Ascending }); + descriptor->append({ "schema", "table", "col2", SQL::SQLType::Integer, SQL::Order::Descending }); auto v = SQL::Value::create_tuple(descriptor); Vector values; @@ -321,7 +321,7 @@ TEST_CASE(copy_tuple_value) TEST_CASE(tuple_value_wrong_type) { NonnullRefPtr descriptor = adopt_ref(*new SQL::TupleDescriptor); - descriptor->append({ "col1", SQL::SQLType::Text, SQL::Order::Ascending }); + descriptor->append({ "schema", "table", "col1", SQL::SQLType::Text, SQL::Order::Ascending }); auto v = SQL::Value::create_tuple(descriptor); Vector values; @@ -333,7 +333,7 @@ TEST_CASE(tuple_value_wrong_type) TEST_CASE(tuple_value_too_many_values) { NonnullRefPtr descriptor = adopt_ref(*new SQL::TupleDescriptor); - descriptor->append({ "col1", SQL::SQLType::Text, SQL::Order::Ascending }); + descriptor->append({ "schema", "table", "col1", SQL::SQLType::Text, SQL::Order::Ascending }); auto v = SQL::Value::create_tuple(descriptor); Vector values; @@ -346,8 +346,8 @@ TEST_CASE(tuple_value_too_many_values) TEST_CASE(tuple_value_not_enough_values) { NonnullRefPtr descriptor = adopt_ref(*new SQL::TupleDescriptor); - descriptor->append({ "col1", SQL::SQLType::Text, SQL::Order::Ascending }); - descriptor->append({ "col2", SQL::SQLType::Integer, SQL::Order::Ascending }); + descriptor->append({ "schema", "table", "col1", SQL::SQLType::Text, SQL::Order::Ascending }); + descriptor->append({ "schema", "table", "col2", SQL::SQLType::Integer, SQL::Order::Ascending }); auto v = SQL::Value::create_tuple(descriptor); Vector values; @@ -365,8 +365,8 @@ TEST_CASE(tuple_value_not_enough_values) TEST_CASE(serialize_tuple_value) { NonnullRefPtr descriptor = adopt_ref(*new SQL::TupleDescriptor); - descriptor->append({ "col1", SQL::SQLType::Text, SQL::Order::Ascending }); - descriptor->append({ "col2", SQL::SQLType::Integer, SQL::Order::Descending }); + descriptor->append({ "schema", "table", "col1", SQL::SQLType::Text, SQL::Order::Ascending }); + descriptor->append({ "schema", "table", "col2", SQL::SQLType::Integer, SQL::Order::Descending }); auto v = SQL::Value::create_tuple(descriptor); Vector values; @@ -477,8 +477,8 @@ TEST_CASE(order_int_values) TEST_CASE(tuple) { NonnullRefPtr descriptor = adopt_ref(*new SQL::TupleDescriptor); - descriptor->append({ "col1", SQL::SQLType::Text, SQL::Order::Ascending }); - descriptor->append({ "col2", SQL::SQLType::Integer, SQL::Order::Descending }); + descriptor->append({ "schema", "table", "col1", SQL::SQLType::Text, SQL::Order::Ascending }); + descriptor->append({ "schema", "table", "col2", SQL::SQLType::Integer, SQL::Order::Descending }); SQL::Tuple tuple(descriptor); tuple["col1"] = "Test"; @@ -490,8 +490,8 @@ TEST_CASE(tuple) TEST_CASE(serialize_tuple) { NonnullRefPtr descriptor = adopt_ref(*new SQL::TupleDescriptor); - descriptor->append({ "col1", SQL::SQLType::Text, SQL::Order::Ascending }); - descriptor->append({ "col2", SQL::SQLType::Integer, SQL::Order::Descending }); + descriptor->append({ "schema", "table", "col1", SQL::SQLType::Text, SQL::Order::Ascending }); + descriptor->append({ "schema", "table", "col2", SQL::SQLType::Integer, SQL::Order::Descending }); SQL::Tuple tuple(descriptor); tuple["col1"] = "Test"; @@ -512,8 +512,8 @@ TEST_CASE(serialize_tuple) TEST_CASE(copy_tuple) { NonnullRefPtr descriptor = adopt_ref(*new SQL::TupleDescriptor); - descriptor->append({ "col1", SQL::SQLType::Text, SQL::Order::Ascending }); - descriptor->append({ "col2", SQL::SQLType::Integer, SQL::Order::Descending }); + descriptor->append({ "schema", "table", "col1", SQL::SQLType::Text, SQL::Order::Ascending }); + descriptor->append({ "schema", "table", "col2", SQL::SQLType::Integer, SQL::Order::Descending }); SQL::Tuple tuple(descriptor); tuple["col1"] = "Test"; @@ -530,8 +530,8 @@ TEST_CASE(copy_tuple) TEST_CASE(compare_tuples) { NonnullRefPtr descriptor = adopt_ref(*new SQL::TupleDescriptor); - descriptor->append({ "col1", SQL::SQLType::Text, SQL::Order::Ascending }); - descriptor->append({ "col2", SQL::SQLType::Integer, SQL::Order::Descending }); + descriptor->append({ "schema", "table", "col1", SQL::SQLType::Text, SQL::Order::Ascending }); + descriptor->append({ "schema", "table", "col2", SQL::SQLType::Integer, SQL::Order::Descending }); SQL::Tuple tuple1(descriptor); tuple1["col1"] = "Test"; diff --git a/Userland/Libraries/LibSQL/Meta.cpp b/Userland/Libraries/LibSQL/Meta.cpp index 65859f0e76b..5dab68e598b 100644 --- a/Userland/Libraries/LibSQL/Meta.cpp +++ b/Userland/Libraries/LibSQL/Meta.cpp @@ -118,7 +118,7 @@ NonnullRefPtr IndexDef::to_tuple_descriptor() const { NonnullRefPtr ret = adopt_ref(*new TupleDescriptor); for (auto& part : m_key_definition) { - ret->append({ part.name(), part.type(), part.sort_order() }); + ret->append({ "", "", part.name(), part.type(), part.sort_order() }); } return ret; } @@ -161,7 +161,7 @@ NonnullRefPtr TableDef::to_tuple_descriptor() const { NonnullRefPtr ret = adopt_ref(*new TupleDescriptor); for (auto& part : m_columns) { - ret->append({ part.name(), part.type(), Order::Ascending }); + ret->append({ parent()->name(), name(), part.name(), part.type(), Order::Ascending }); } return ret; } diff --git a/Userland/Libraries/LibSQL/TupleDescriptor.h b/Userland/Libraries/LibSQL/TupleDescriptor.h index a4ca1cc6b81..734bea69da5 100644 --- a/Userland/Libraries/LibSQL/TupleDescriptor.h +++ b/Userland/Libraries/LibSQL/TupleDescriptor.h @@ -13,6 +13,8 @@ namespace SQL { struct TupleElementDescriptor { + String schema { "" }; + String table { "" }; String name { "" }; SQLType type { SQLType::Text }; Order order { Order::Ascending }; diff --git a/Userland/Libraries/LibSQL/Value.cpp b/Userland/Libraries/LibSQL/Value.cpp index d5fea9d48e1..6eb6bdd6b8e 100644 --- a/Userland/Libraries/LibSQL/Value.cpp +++ b/Userland/Libraries/LibSQL/Value.cpp @@ -1014,7 +1014,7 @@ void TupleImpl::infer_descriptor() void TupleImpl::extend_descriptor(Value const& value) { VERIFY(m_descriptor_inferred); - m_descriptor->empend("", value.type(), Order::Ascending); + m_descriptor->empend("", "", "", value.type(), Order::Ascending); } bool TupleImpl::validate_before_assignment(Vector const& values) diff --git a/Userland/Libraries/LibSQL/Value.h b/Userland/Libraries/LibSQL/Value.h index 28ec19eb03f..54f1df22d87 100644 --- a/Userland/Libraries/LibSQL/Value.h +++ b/Userland/Libraries/LibSQL/Value.h @@ -130,7 +130,7 @@ public: [[nodiscard]] TupleElementDescriptor descriptor() const { - return { "", type(), Order::Ascending }; + return { "", "", "", type(), Order::Ascending }; } static Value const& null();