Преглед на файлове

LibSQL: Allow expressions and column names in SELECT ... FROM

Up to now the only ``SELECT`` statement that worked was ``SELECT *
FROM <table>``. This commit allows a column list consisting of
column names and expressions in addition to ``*``. ``WHERE``
still doesn't work though.
Jan de Visser преди 3 години
родител
ревизия
fe50598a03

+ 2 - 1
Userland/Libraries/LibSQL/AST/AST.h

@@ -298,7 +298,7 @@ private:
 struct ExecutionContext {
 struct ExecutionContext {
     NonnullRefPtr<Database> database;
     NonnullRefPtr<Database> database;
     RefPtr<SQLResult> result { nullptr };
     RefPtr<SQLResult> result { nullptr };
-    Tuple current_row {};
+    Tuple* current_row { nullptr };
 };
 };
 
 
 class Expression : public ASTNode {
 class Expression : public ASTNode {
@@ -429,6 +429,7 @@ public:
     const String& schema_name() const { return m_schema_name; }
     const String& schema_name() const { return m_schema_name; }
     const String& table_name() const { return m_table_name; }
     const String& table_name() const { return m_table_name; }
     const String& column_name() const { return m_column_name; }
     const String& column_name() const { return m_column_name; }
+    virtual Value evaluate(ExecutionContext&) const override;
 
 
 private:
 private:
     String m_schema_name;
     String m_schema_name;

+ 13 - 0
Userland/Libraries/LibSQL/AST/Expression.cpp

@@ -87,4 +87,17 @@ Value UnaryOperatorExpression::evaluate(ExecutionContext& context) const
     VERIFY_NOT_REACHED();
     VERIFY_NOT_REACHED();
 }
 }
 
 
+Value ColumnNameExpression::evaluate(ExecutionContext& context) const
+{
+    auto& descriptor = *context.current_row->descriptor();
+    VERIFY(context.current_row->size() == descriptor.size());
+    for (auto ix = 0u; ix < context.current_row->size(); ix++) {
+        auto& column_descriptor = descriptor[ix];
+        if (column_descriptor.name == column_name())
+            return { (*context.current_row)[ix] };
+    }
+    // TODO: Error handling.
+    VERIFY_NOT_REACHED();
+}
+
 }
 }

+ 26 - 12
Userland/Libraries/LibSQL/AST/Select.cpp

@@ -14,22 +14,36 @@ namespace SQL::AST {
 RefPtr<SQLResult> Select::execute(ExecutionContext& context) const
 RefPtr<SQLResult> Select::execute(ExecutionContext& context) const
 {
 {
     if (table_or_subquery_list().size() == 1 && table_or_subquery_list()[0].is_table()) {
     if (table_or_subquery_list().size() == 1 && table_or_subquery_list()[0].is_table()) {
+        auto table = context.database->get_table(table_or_subquery_list()[0].schema_name(), table_or_subquery_list()[0].table_name());
+        if (!table) {
+            return SQLResult::construct(SQL::SQLCommand::Select, SQL::SQLErrorCode::TableDoesNotExist, table_or_subquery_list()[0].table_name());
+        }
+
+        NonnullRefPtrVector<ResultColumn> columns;
         if (result_column_list().size() == 1 && result_column_list()[0].type() == ResultType::All) {
         if (result_column_list().size() == 1 && result_column_list()[0].type() == ResultType::All) {
-            auto table = context.database->get_table(table_or_subquery_list()[0].schema_name(), table_or_subquery_list()[0].table_name());
-            if (!table) {
-                return SQLResult::construct(SQL::SQLCommand::Select, SQL::SQLErrorCode::TableDoesNotExist, table_or_subquery_list()[0].table_name());
+            for (auto& col : table->columns()) {
+                columns.append(
+                    create_ast_node<ResultColumn>(
+                        create_ast_node<ColumnNameExpression>(table->parent()->name(), table->name(), col.name()),
+                        ""));
             }
             }
-            NonnullRefPtr<TupleDescriptor> descriptor = table->to_tuple_descriptor();
-            context.result = SQLResult::construct();
-            for (auto& row : context.database->select_all(*table)) {
-                Tuple tuple(descriptor);
-                for (auto ix = 0u; ix < descriptor->size(); ix++) {
-                    tuple[ix] = row[ix];
-                }
-                context.result->append(tuple);
+        } else {
+            for (auto& col : result_column_list()) {
+                columns.append(col);
+            }
+        }
+        context.result = SQLResult::construct();
+        AK::NonnullRefPtr<TupleDescriptor> descriptor = AK::adopt_ref(*new TupleDescriptor);
+        for (auto& row : context.database->select_all(*table)) {
+            context.current_row = &row;
+            Tuple tuple(descriptor);
+            for (auto& col : columns) {
+                auto value = col.expression()->evaluate(context);
+                tuple.append(value);
             }
             }
-            return context.result;
+            context.result->append(tuple);
         }
         }
+        return context.result;
     }
     }
     return SQLResult::construct();
     return SQLResult::construct();
 }
 }

+ 4 - 1
Userland/Libraries/LibSQL/Tuple.cpp

@@ -119,7 +119,10 @@ Value& Tuple::operator[](String const& name)
 
 
 void Tuple::append(const Value& value)
 void Tuple::append(const Value& value)
 {
 {
-    VERIFY(m_descriptor->size() == 0);
+    VERIFY(descriptor()->size() >= size());
+    if (descriptor()->size() == size()) {
+        descriptor()->append(value.descriptor());
+    }
     m_data.append(value);
     m_data.append(value);
 }
 }
 
 

+ 1 - 1
Userland/Libraries/LibSQL/Tuple.h

@@ -62,7 +62,7 @@ public:
 
 
     [[nodiscard]] size_t size() const { return m_data.size(); }
     [[nodiscard]] size_t size() const { return m_data.size(); }
     [[nodiscard]] virtual size_t length() const;
     [[nodiscard]] virtual size_t length() const;
-    void clear() { m_descriptor->clear(); }
+    void clear() { m_data.clear(); }
     [[nodiscard]] NonnullRefPtr<TupleDescriptor> descriptor() const { return m_descriptor; }
     [[nodiscard]] NonnullRefPtr<TupleDescriptor> descriptor() const { return m_descriptor; }
     [[nodiscard]] int compare(Tuple const&) const;
     [[nodiscard]] int compare(Tuple const&) const;
     [[nodiscard]] int match(Tuple const&) const;
     [[nodiscard]] int match(Tuple const&) const;

+ 14 - 0
Userland/Libraries/LibSQL/TupleDescriptor.h

@@ -36,6 +36,11 @@ struct TupleElementDescriptor {
     {
     {
         return (sizeof(u32) + name.length()) + 2 * sizeof(u8);
         return (sizeof(u32) + name.length()) + 2 * sizeof(u8);
     }
     }
+
+    String to_string() const
+    {
+        return String::formatted("  name: {} type: {} order: {}", name, SQLType_name(type), Order_name(order));
+    }
 };
 };
 
 
 class TupleDescriptor
 class TupleDescriptor
@@ -84,6 +89,15 @@ public:
         return len;
         return len;
     }
     }
 
 
+    String to_string() const
+    {
+        Vector<String> elements;
+        for (auto& element : *this) {
+            elements.append(element.to_string());
+        }
+        return String::formatted("[\n{}\n]", String::join("\n", elements));
+    }
+
     using Vector<TupleElementDescriptor>::operator==;
     using Vector<TupleElementDescriptor>::operator==;
 };
 };
 
 

+ 22 - 2
Userland/Libraries/LibSQL/Type.h

@@ -55,11 +55,31 @@ inline static size_t size_of(SQLType t)
     }
     }
 }
 }
 
 
+#define ENUMERATE_ORDERS(S) \
+    S(Ascending)            \
+    S(Descending)
+
 enum class Order {
 enum class Order {
-    Ascending,
-    Descending,
+#undef __ENUMERATE_ORDER
+#define __ENUMERATE_ORDER(order) order,
+    ENUMERATE_ORDERS(__ENUMERATE_ORDER)
+#undef __ENUMERATE_ORDER
 };
 };
 
 
+inline static String Order_name(Order order)
+{
+    switch (order) {
+#undef __ENUMERATE_ORDER
+#define __ENUMERATE_ORDER(order) \
+    case Order::order:           \
+        return #order;
+        ENUMERATE_ORDERS(__ENUMERATE_ORDER)
+#undef __ENUMERATE_ORDER
+    default:
+        VERIFY_NOT_REACHED();
+    }
+}
+
 enum class Nulls {
 enum class Nulls {
     First,
     First,
     Last,
     Last,

+ 5 - 0
Userland/Libraries/LibSQL/Value.h

@@ -116,6 +116,11 @@ public:
     bool operator>(Value const&) const;
     bool operator>(Value const&) const;
     bool operator>=(Value const&) const;
     bool operator>=(Value const&) const;
 
 
+    [[nodiscard]] TupleElementDescriptor descriptor() const
+    {
+        return { "", type(), Order::Ascending };
+    }
+
     static Value const& null();
     static Value const& null();
     static Value create_tuple(NonnullRefPtr<TupleDescriptor> const&);
     static Value create_tuple(NonnullRefPtr<TupleDescriptor> const&);
     static Value create_array(SQLType element_type, Optional<size_t> const& max_size = {});
     static Value create_array(SQLType element_type, Optional<size_t> const& max_size = {});