From 30953bc2a3de9f90b78014779cdde9040a872fa4 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Tue, 17 Jan 2023 09:02:01 +0000 Subject: [PATCH] Add SQL `rand::ulid()` function Related to #1353 --- lib/Cargo.toml | 2 +- lib/src/fnc/mod.rs | 1 + lib/src/fnc/rand.rs | 5 +++++ lib/src/sql/function.rs | 1 + lib/src/sql/value/value.rs | 12 ++++++------ 5 files changed, 14 insertions(+), 7 deletions(-) diff --git a/lib/Cargo.toml b/lib/Cargo.toml index 4766873d..f2d4f9db 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -96,6 +96,7 @@ tikv = { version = "0.1.0", package = "tikv-client", optional = true } tokio-stream = { version = "0.1.11", optional = true } tokio-util = { version = "0.7.4", optional = true, features = ["compat"] } trice = "0.2.0" +ulid = { version = "1.0.0", features = ["serde"] } url = "2.3.1" [dependencies.js] @@ -116,7 +117,6 @@ features = [ [dev-dependencies] time = { version = "0.3.17", features = ["serde"] } tokio = { version = "1.24.1", features = ["macros", "rt", "rt-multi-thread"] } -ulid = { version = "1.0.0", features = ["serde"] } [target.'cfg(target_arch = "wasm32")'.dependencies] pharos = "0.5.3" diff --git a/lib/src/fnc/mod.rs b/lib/src/fnc/mod.rs index aceb261f..a82c558d 100644 --- a/lib/src/fnc/mod.rs +++ b/lib/src/fnc/mod.rs @@ -162,6 +162,7 @@ pub fn synchronous(ctx: &Context<'_>, name: &str, args: Vec) -> Result rand::int, "rand::string" => rand::string, "rand::time" => rand::time, + "rand::ulid" => rand::ulid, "rand::uuid::v4" => rand::uuid::v4, "rand::uuid::v7" => rand::uuid::v7, "rand::uuid" => rand::uuid, diff --git a/lib/src/fnc/rand.rs b/lib/src/fnc/rand.rs index c7af2171..76dc6624 100644 --- a/lib/src/fnc/rand.rs +++ b/lib/src/fnc/rand.rs @@ -7,6 +7,7 @@ use nanoid::nanoid; use rand::distributions::{Alphanumeric, DistString}; use rand::prelude::IteratorRandom; use rand::Rng; +use ulid::Ulid; pub fn rand(_: ()) -> Result { Ok(rand::random::().into()) @@ -120,6 +121,10 @@ pub fn time((range,): (Option<(i64, i64)>,)) -> Result { Ok(Datetime::from(i).into()) } +pub fn ulid(_: ()) -> Result { + Ok(Ulid::new().to_string().into()) +} + pub fn uuid(_: ()) -> Result { Ok(Uuid::new().into()) } diff --git a/lib/src/sql/function.rs b/lib/src/sql/function.rs index d79d8deb..d6ed2bbe 100644 --- a/lib/src/sql/function.rs +++ b/lib/src/sql/function.rs @@ -393,6 +393,7 @@ fn function_rand(i: &str) -> IResult<&str, &str> { tag("rand::int"), tag("rand::string"), tag("rand::time"), + tag("rand::ulid"), tag("rand::uuid::v4"), tag("rand::uuid::v7"), tag("rand::uuid"), diff --git a/lib/src/sql/value/value.rs b/lib/src/sql/value/value.rs index 7143e63e..a773c10d 100644 --- a/lib/src/sql/value/value.rs +++ b/lib/src/sql/value/value.rs @@ -155,12 +155,6 @@ impl From for Value { } } -impl From for Value { - fn from(v: uuid::Uuid) -> Self { - Value::Uuid(Uuid(v)) - } -} - impl From for Value { fn from(v: Param) -> Self { Value::Param(v) @@ -401,6 +395,12 @@ impl From for Value { } } +impl From for Value { + fn from(v: uuid::Uuid) -> Self { + Value::Uuid(Uuid(v)) + } +} + impl From> for Value { fn from(v: Vec<&str>) -> Self { Value::Array(Array::from(v))