mirror of
https://github.com/SerenityOS/serenity
synced 2024-10-15 12:23:15 +00:00
LibSQL: Allow constructing SQL values from a String
LibSQL still uses ByteString internally for storage, but to help outside application port to String, add helpers to construct values from String and to convert a value to a String.
This commit is contained in:
parent
c4820838bf
commit
30fae8fa1d
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
|
* Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
|
||||||
* Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
|
* Copyright (c) 2022-2024, Tim Flynn <trflynn89@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* 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)
|
Value::Value(ByteString value)
|
||||||
: m_type(SQLType::Text)
|
: m_type(SQLType::Text)
|
||||||
, m_value(move(value))
|
, m_value(move(value))
|
||||||
|
@ -214,6 +219,27 @@ bool Value::is_int() const
|
||||||
return m_value.has_value() && (m_value->has<i64>() || m_value->has<u64>());
|
return m_value.has_value() && (m_value->has<i64>() || m_value->has<u64>());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ErrorOr<String> 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
|
ByteString Value::to_byte_string() const
|
||||||
{
|
{
|
||||||
if (is_null())
|
if (is_null())
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
|
* Copyright (c) 2021, Jan de Visser <jan@de-visser.net>
|
||||||
* Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
|
* Copyright (c) 2022-2024, Tim Flynn <trflynn89@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -11,6 +11,7 @@
|
||||||
#include <AK/Checked.h>
|
#include <AK/Checked.h>
|
||||||
#include <AK/Format.h>
|
#include <AK/Format.h>
|
||||||
#include <AK/Optional.h>
|
#include <AK/Optional.h>
|
||||||
|
#include <AK/String.h>
|
||||||
#include <AK/StringView.h>
|
#include <AK/StringView.h>
|
||||||
#include <AK/Variant.h>
|
#include <AK/Variant.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
|
@ -39,6 +40,7 @@ class Value {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit Value(SQLType sql_type = SQLType::Null);
|
explicit Value(SQLType sql_type = SQLType::Null);
|
||||||
|
explicit Value(String);
|
||||||
explicit Value(ByteString);
|
explicit Value(ByteString);
|
||||||
explicit Value(double);
|
explicit Value(double);
|
||||||
Value(Value const&);
|
Value(Value const&);
|
||||||
|
@ -74,6 +76,7 @@ public:
|
||||||
return *m_value;
|
return *m_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] ErrorOr<String> to_string() const;
|
||||||
[[nodiscard]] ByteString to_byte_string() const;
|
[[nodiscard]] ByteString to_byte_string() const;
|
||||||
[[nodiscard]] Optional<double> to_double() const;
|
[[nodiscard]] Optional<double> to_double() const;
|
||||||
[[nodiscard]] Optional<bool> to_bool() const;
|
[[nodiscard]] Optional<bool> to_bool() const;
|
||||||
|
|
Loading…
Reference in a new issue