mirror of
https://github.com/aljazceru/turso.git
synced 2026-02-02 23:04:23 +01:00
ext/uuid: Convert uuid4() to external function
This commit is contained in:
@@ -30,3 +30,8 @@ impl ExtFunc {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init(db: &mut crate::Database) {
|
||||
#[cfg(feature = "uuid")]
|
||||
uuid::init(db);
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use super::ExtFunc;
|
||||
use crate::{
|
||||
types::{LimboText, OwnedValue},
|
||||
LimboError,
|
||||
Database, LimboError,
|
||||
};
|
||||
use std::rc::Rc;
|
||||
use uuid::{ContextV7, Timestamp, Uuid};
|
||||
@@ -9,7 +9,6 @@ use uuid::{ContextV7, Timestamp, Uuid};
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
pub enum UuidFunc {
|
||||
Uuid4Str,
|
||||
Uuid4,
|
||||
Uuid7,
|
||||
Uuid7TS,
|
||||
UuidStr,
|
||||
@@ -20,7 +19,6 @@ impl UuidFunc {
|
||||
pub fn resolve_function(name: &str, num_args: usize) -> Option<ExtFunc> {
|
||||
match name {
|
||||
"uuid4_str" => Some(ExtFunc::Uuid(Self::Uuid4Str)),
|
||||
"uuid4" => Some(ExtFunc::Uuid(Self::Uuid4)),
|
||||
"uuid7" if num_args < 2 => Some(ExtFunc::Uuid(Self::Uuid7)),
|
||||
"uuid_str" if num_args == 1 => Some(ExtFunc::Uuid(Self::UuidStr)),
|
||||
"uuid_blob" if num_args == 1 => Some(ExtFunc::Uuid(Self::UuidBlob)),
|
||||
@@ -36,7 +34,6 @@ impl std::fmt::Display for UuidFunc {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Uuid4Str => write!(f, "uuid4_str"),
|
||||
Self::Uuid4 => write!(f, "uuid4"),
|
||||
Self::Uuid7 => write!(f, "uuid7"),
|
||||
Self::Uuid7TS => write!(f, "uuid7_timestamp_ms"),
|
||||
Self::UuidStr => write!(f, "uuid_str"),
|
||||
@@ -47,9 +44,6 @@ impl std::fmt::Display for UuidFunc {
|
||||
|
||||
pub fn exec_uuid(var: &UuidFunc, sec: Option<&OwnedValue>) -> crate::Result<OwnedValue> {
|
||||
match var {
|
||||
UuidFunc::Uuid4 => Ok(OwnedValue::Blob(Rc::new(
|
||||
Uuid::new_v4().into_bytes().to_vec(),
|
||||
))),
|
||||
UuidFunc::Uuid4Str => Ok(OwnedValue::Text(LimboText::new(Rc::new(
|
||||
Uuid::new_v4().to_string(),
|
||||
)))),
|
||||
@@ -71,6 +65,12 @@ pub fn exec_uuid(var: &UuidFunc, sec: Option<&OwnedValue>) -> crate::Result<Owne
|
||||
}
|
||||
}
|
||||
|
||||
pub fn exec_uuid4() -> crate::Result<OwnedValue> {
|
||||
Ok(OwnedValue::Blob(Rc::new(
|
||||
Uuid::new_v4().into_bytes().to_vec(),
|
||||
)))
|
||||
}
|
||||
|
||||
pub fn exec_uuidstr(reg: &OwnedValue) -> crate::Result<OwnedValue> {
|
||||
match reg {
|
||||
OwnedValue::Blob(blob) => {
|
||||
@@ -136,6 +136,10 @@ fn uuid_to_unix(uuid: &[u8; 16]) -> u64 {
|
||||
| (uuid[5] as u64)
|
||||
}
|
||||
|
||||
pub fn init(db: &mut Database) {
|
||||
db.define_scalar_function("uuid4", |_args| exec_uuid4());
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[cfg(feature = "uuid")]
|
||||
pub mod test {
|
||||
@@ -143,10 +147,9 @@ pub mod test {
|
||||
use crate::types::OwnedValue;
|
||||
#[test]
|
||||
fn test_exec_uuid_v4blob() {
|
||||
use super::exec_uuid;
|
||||
use super::exec_uuid4;
|
||||
use uuid::Uuid;
|
||||
let func = UuidFunc::Uuid4;
|
||||
let owned_val = exec_uuid(&func, None);
|
||||
let owned_val = exec_uuid4();
|
||||
match owned_val {
|
||||
Ok(OwnedValue::Blob(blob)) => {
|
||||
assert_eq!(blob.len(), 16);
|
||||
@@ -303,11 +306,10 @@ pub mod test {
|
||||
|
||||
#[test]
|
||||
fn test_exec_uuid_v4_blob_to_str() {
|
||||
use super::{exec_uuid, exec_uuidstr, UuidFunc};
|
||||
use super::{exec_uuid4, exec_uuidstr};
|
||||
use uuid::Uuid;
|
||||
// convert a v4 blob to a string
|
||||
let owned_val =
|
||||
exec_uuidstr(&exec_uuid(&UuidFunc::Uuid4, None).expect("uuid v7 blob to generate"));
|
||||
let owned_val = exec_uuidstr(&exec_uuid4().expect("uuid v7 blob to generate"));
|
||||
match owned_val {
|
||||
Ok(OwnedValue::Text(v4str)) => {
|
||||
assert_eq!(v4str.value.len(), 36);
|
||||
|
||||
@@ -124,14 +124,16 @@ impl Database {
|
||||
let header = db_header;
|
||||
let schema = Rc::new(RefCell::new(Schema::new()));
|
||||
let syms = Rc::new(RefCell::new(SymbolTable::new()));
|
||||
let db = Arc::new(Database {
|
||||
let mut db = Database {
|
||||
pager: pager.clone(),
|
||||
schema: schema.clone(),
|
||||
header: header.clone(),
|
||||
_shared_page_cache: _shared_page_cache.clone(),
|
||||
_shared_wal: shared_wal.clone(),
|
||||
syms,
|
||||
});
|
||||
};
|
||||
ext::init(&mut db);
|
||||
let db = Arc::new(db);
|
||||
let conn = Rc::new(Connection {
|
||||
db: db.clone(),
|
||||
pager: pager,
|
||||
|
||||
@@ -1766,7 +1766,7 @@ pub fn translate_expr(
|
||||
});
|
||||
Ok(target_register)
|
||||
}
|
||||
UuidFunc::Uuid4 | UuidFunc::Uuid4Str => {
|
||||
UuidFunc::Uuid4Str => {
|
||||
if args.is_some() {
|
||||
crate::bail_parse_error!(
|
||||
"{} function with arguments",
|
||||
|
||||
@@ -2183,7 +2183,7 @@ impl Program {
|
||||
crate::function::Func::Extension(extfn) => match extfn {
|
||||
#[cfg(feature = "uuid")]
|
||||
ExtFunc::Uuid(uuidfn) => match uuidfn {
|
||||
UuidFunc::Uuid4 | UuidFunc::Uuid4Str => {
|
||||
UuidFunc::Uuid4Str => {
|
||||
state.registers[*dest] = exec_uuid(uuidfn, None)?
|
||||
}
|
||||
UuidFunc::Uuid7 => match arg_count {
|
||||
|
||||
Reference in New Issue
Block a user