diff --git a/core/ext/mod.rs b/core/ext/mod.rs index 2140957d..cea65a98 100644 --- a/core/ext/mod.rs +++ b/core/ext/mod.rs @@ -30,3 +30,8 @@ impl ExtFunc { } } } + +pub fn init(db: &mut crate::Database) { + #[cfg(feature = "uuid")] + uuid::init(db); +} diff --git a/core/ext/uuid.rs b/core/ext/uuid.rs index d3225e8e..92fdd831 100644 --- a/core/ext/uuid.rs +++ b/core/ext/uuid.rs @@ -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 { 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 { 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 crate::Result { + Ok(OwnedValue::Blob(Rc::new( + Uuid::new_v4().into_bytes().to_vec(), + ))) +} + pub fn exec_uuidstr(reg: &OwnedValue) -> crate::Result { 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); diff --git a/core/lib.rs b/core/lib.rs index 321954e8..5ef48e74 100644 --- a/core/lib.rs +++ b/core/lib.rs @@ -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, diff --git a/core/translate/expr.rs b/core/translate/expr.rs index 5032e827..6b12c24e 100644 --- a/core/translate/expr.rs +++ b/core/translate/expr.rs @@ -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", diff --git a/core/vdbe/mod.rs b/core/vdbe/mod.rs index 470b4858..20b5e39c 100644 --- a/core/vdbe/mod.rs +++ b/core/vdbe/mod.rs @@ -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 {