LibSQL: Add a helper to convert a SQL::Value to a UnixDateTime
Support for constructing a Value from a UnixDateTime was added in commit
effcd080ca
.
That constructor just stores the value as the number of milliseconds
since epoch. There's no way for outside users to know this, so this adds
a helper to retrieve the value as a UnixDateTime and let SQL::Value be
the source of truth for how the value is encoded/decoded.
This commit is contained in:
parent
1205d39fc3
commit
cd0e07f6a4
Notes:
sideshowbarker
2024-07-16 17:05:37 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/cd0e07f6a4 Pull-request: https://github.com/SerenityOS/serenity/pull/22690
3 changed files with 44 additions and 0 deletions
|
@ -7,6 +7,7 @@
|
|||
|
||||
#include <unistd.h>
|
||||
|
||||
#include <AK/Time.h>
|
||||
#include <LibSQL/Meta.h>
|
||||
#include <LibSQL/Row.h>
|
||||
#include <LibSQL/Tuple.h>
|
||||
|
@ -471,6 +472,39 @@ TEST_CASE(serialize_boolean_value)
|
|||
EXPECT_EQ(v, v2);
|
||||
}
|
||||
|
||||
TEST_CASE(unix_date_time_value)
|
||||
{
|
||||
auto now = UnixDateTime::now();
|
||||
{
|
||||
SQL::Value value(now);
|
||||
EXPECT_EQ(value.type(), SQL::SQLType::Integer);
|
||||
|
||||
auto result = value.to_unix_date_time();
|
||||
VERIFY(result.has_value());
|
||||
EXPECT_EQ(result->milliseconds_since_epoch(), now.milliseconds_since_epoch());
|
||||
}
|
||||
{
|
||||
auto now_plus_10s = now + Duration::from_seconds(10);
|
||||
|
||||
SQL::Value value(now_plus_10s);
|
||||
EXPECT_EQ(value.type(), SQL::SQLType::Integer);
|
||||
|
||||
auto result = value.to_unix_date_time();
|
||||
VERIFY(result.has_value());
|
||||
EXPECT_EQ(result->milliseconds_since_epoch(), now_plus_10s.milliseconds_since_epoch());
|
||||
}
|
||||
{
|
||||
auto now_minus_10s = now - Duration::from_seconds(10);
|
||||
|
||||
SQL::Value value(now_minus_10s);
|
||||
EXPECT_EQ(value.type(), SQL::SQLType::Integer);
|
||||
|
||||
auto result = value.to_unix_date_time();
|
||||
VERIFY(result.has_value());
|
||||
EXPECT_EQ(result->milliseconds_since_epoch(), now_minus_10s.milliseconds_since_epoch());
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE(tuple_value)
|
||||
{
|
||||
NonnullRefPtr<SQL::TupleDescriptor> descriptor = adopt_ref(*new SQL::TupleDescriptor);
|
||||
|
|
|
@ -277,6 +277,15 @@ Optional<bool> Value::to_bool() const
|
|||
});
|
||||
}
|
||||
|
||||
Optional<UnixDateTime> Value::to_unix_date_time() const
|
||||
{
|
||||
auto time = to_int<i64>();
|
||||
if (!time.has_value())
|
||||
return {};
|
||||
|
||||
return UnixDateTime::from_milliseconds_since_epoch(*time);
|
||||
}
|
||||
|
||||
Optional<Vector<Value>> Value::to_vector() const
|
||||
{
|
||||
if (is_null() || (type() != SQLType::Tuple))
|
||||
|
|
|
@ -77,6 +77,7 @@ public:
|
|||
[[nodiscard]] ByteString to_byte_string() const;
|
||||
[[nodiscard]] Optional<double> to_double() const;
|
||||
[[nodiscard]] Optional<bool> to_bool() const;
|
||||
[[nodiscard]] Optional<UnixDateTime> to_unix_date_time() const;
|
||||
[[nodiscard]] Optional<Vector<Value>> to_vector() const;
|
||||
|
||||
template<Integer T>
|
||||
|
|
Loading…
Add table
Reference in a new issue