فهرست منبع

LibSQL: Add a helper to convert a SQL::Value to a UnixDateTime

Support for constructing a Value from a UnixDateTime was added in commit
effcd080ca802515ddce8392c70df512b509f57c.

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.
Timothy Flynn 1 سال پیش
والد
کامیت
cd0e07f6a4
3فایلهای تغییر یافته به همراه44 افزوده شده و 0 حذف شده
  1. 34 0
      Tests/LibSQL/TestSqlValueAndTuple.cpp
  2. 9 0
      Userland/Libraries/LibSQL/Value.cpp
  3. 1 0
      Userland/Libraries/LibSQL/Value.h

+ 34 - 0
Tests/LibSQL/TestSqlValueAndTuple.cpp

@@ -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);

+ 9 - 0
Userland/Libraries/LibSQL/Value.cpp

@@ -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))

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

@@ -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>