diff --git a/Userland/Libraries/LibSQL/Value.cpp b/Userland/Libraries/LibSQL/Value.cpp index 83db129778..c5fea63fa6 100644 --- a/Userland/Libraries/LibSQL/Value.cpp +++ b/Userland/Libraries/LibSQL/Value.cpp @@ -1,6 +1,6 @@ /* * Copyright (c) 2021, Jan de Visser - * Copyright (c) 2022, Tim Flynn + * Copyright (c) 2022-2024, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ @@ -97,6 +97,11 @@ Value::Value(SQLType type) { } +Value::Value(String value) + : Value(value.to_byte_string()) +{ +} + Value::Value(ByteString value) : m_type(SQLType::Text) , m_value(move(value)) @@ -214,6 +219,27 @@ bool Value::is_int() const return m_value.has_value() && (m_value->has() || m_value->has()); } +ErrorOr Value::to_string() const +{ + if (is_null()) + return String::from_utf8("(null)"sv); + + return m_value->visit( + [](ByteString const& value) { return String::from_byte_string(value); }, + [](Integer auto value) { return String::number(value); }, + [](double value) { return String::number(value); }, + [](bool value) { return String::from_utf8(value ? "true"sv : "false"sv); }, + [](TupleValue const& value) { + StringBuilder builder; + + builder.append('('); + builder.join(',', value.values); + builder.append(')'); + + return builder.to_string(); + }); +} + ByteString Value::to_byte_string() const { if (is_null()) diff --git a/Userland/Libraries/LibSQL/Value.h b/Userland/Libraries/LibSQL/Value.h index 54654447b1..fd6b739a5b 100644 --- a/Userland/Libraries/LibSQL/Value.h +++ b/Userland/Libraries/LibSQL/Value.h @@ -1,6 +1,6 @@ /* * Copyright (c) 2021, Jan de Visser - * Copyright (c) 2022, Tim Flynn + * Copyright (c) 2022-2024, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -39,6 +40,7 @@ class Value { public: explicit Value(SQLType sql_type = SQLType::Null); + explicit Value(String); explicit Value(ByteString); explicit Value(double); Value(Value const&); @@ -74,6 +76,7 @@ public: return *m_value; } + [[nodiscard]] ErrorOr to_string() const; [[nodiscard]] ByteString to_byte_string() const; [[nodiscard]] Optional to_double() const; [[nodiscard]] Optional to_bool() const;