Add SQL rand::ulid() function

Related to #1353
This commit is contained in:
Tobie Morgan Hitchcock 2023-01-17 09:02:01 +00:00
parent 3a1294029e
commit 30953bc2a3
5 changed files with 14 additions and 7 deletions

View file

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

View file

@ -162,6 +162,7 @@ pub fn synchronous(ctx: &Context<'_>, name: &str, args: Vec<Value>) -> Result<Va
"rand::int" => 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,

View file

@ -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<Value, Error> {
Ok(rand::random::<f64>().into())
@ -120,6 +121,10 @@ pub fn time((range,): (Option<(i64, i64)>,)) -> Result<Value, Error> {
Ok(Datetime::from(i).into())
}
pub fn ulid(_: ()) -> Result<Value, Error> {
Ok(Ulid::new().to_string().into())
}
pub fn uuid(_: ()) -> Result<Value, Error> {
Ok(Uuid::new().into())
}

View file

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

View file

@ -155,12 +155,6 @@ impl From<Uuid> for Value {
}
}
impl From<uuid::Uuid> for Value {
fn from(v: uuid::Uuid) -> Self {
Value::Uuid(Uuid(v))
}
}
impl From<Param> for Value {
fn from(v: Param) -> Self {
Value::Param(v)
@ -401,6 +395,12 @@ impl From<Operation> for Value {
}
}
impl From<uuid::Uuid> for Value {
fn from(v: uuid::Uuid) -> Self {
Value::Uuid(Uuid(v))
}
}
impl From<Vec<&str>> for Value {
fn from(v: Vec<&str>) -> Self {
Value::Array(Array::from(v))