Skip to content

Commit

Permalink
refactor: renamed v7 function to uuid_v7 and uuid_v7_blob
Browse files Browse the repository at this point in the history
BREAKING CHANGE:
  • Loading branch information
woile committed Feb 18, 2024
1 parent 2fe26a0 commit 0753dbf
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 69 deletions.
24 changes: 14 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ CREATE TABLE events(
);

INSERT INTO events(event_id, name) VALUES (uuid_blob(), 'up');
INSERT INTO events(event_id, name) VALUES (uuid_blob(7), 'down');
INSERT INTO events(event_id, name) VALUES (uuid_v7_blob(), 'down');
INSERT INTO events(event_id, name) VALUES (uuid_as_blob('018d9887-42cd-7115-b1ca-18227ac211b4'), 'down');

SELECT uuid_from_blob(event_id), name
Expand All @@ -46,19 +46,23 @@ SELECT event_id name FROM events_as_str;
## Functions

```sql
--- create a uuid as BLOB
--- create a UUIDv4 as BLOB
uuid_blob()
--- create a uuidv7 as BLOB
uuid_blob(7)

--- create a uuid as TEXT
--- create a UUIDv7 as BLOB (recommended)
uuid_v7_blob()

--- create a UUIDv4 as TEXT
uuid()
--- create a uuidv7 as TEXT
uuid(7)

--- convert TEXT uuid to BLOB
--- create a UUIDv7 as TEXT
uuid_v7()

--- convert TEXT UUID to BLOB
uuid_as_blob('018d9887-42cd-7115-b1ca-18227ac211b4')

--- convert uuid BLOB to TEXT
--- convert UUID BLOB to TEXT
uuid_from_blob(column_name)
```
```

Thanks to the amazing work by [asg017](https://github.com/asg017) who built an amazing [sqlite-ecosystem](https://github.com/asg017/sqlite-ecosystem)
99 changes: 40 additions & 59 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,83 +2,62 @@ use sqlite_loadable::prelude::*;

use sqlite_loadable::{api, define_scalar_function, Result};

/// Create a UUID
///
/// When no parameters are given, a random UUIDv4 is created.
/// You can also specify a version number to create a UUIDv7.
/// Create a TEXT UUIDv4
///
/// # Example
///
/// ```sql
/// SELECT uuid(); -- 4
/// SELECT uuid(7); -- 7
/// SELECT uuid(); -- v4
/// ```
pub fn uuid(context: *mut sqlite3_context, values: &[*mut sqlite3_value]) -> Result<()> {
let version = {
let v = values.get(0);
if v.is_none() {
4
} else {
api::value_int(v.unwrap())
}
};

match version {
4 => {
let val = uuid::Uuid::new_v4().to_string();
api::result_text(context, val)?;
}
7 => {
let val = uuid::Uuid::now_v7().to_string();
api::result_text(context, val)?;
}
_ => {
let val = uuid::Uuid::new_v4().to_string();
api::result_text(context, val)?;
}
}
pub fn uuid(context: *mut sqlite3_context, _values: &[*mut sqlite3_value]) -> Result<()> {
let val = uuid::Uuid::new_v4().to_string();
api::result_text(context, val)?;
Ok(())
}

/// Create a blob UUID
/// Create a TEXT UUIDv7
///
/// When no parameters are given, a random UUIDv4 is created.
/// You can also specify a version number to create a UUIDv7.
/// # Example
///
/// ```sql
/// SELECT uuid_v7(); -- v7
/// ```
pub fn uuid_v7(context: *mut sqlite3_context, _values: &[*mut sqlite3_value]) -> Result<()> {
let val = uuid::Uuid::now_v7().to_string();
api::result_text(context, val)?;
Ok(())
}

/// Create a blob UUIDv4
///
/// ## Example
///
/// ```sql
/// SELECT uuid_blob(); -- 4
/// SELECT uuid_blob(7); -- 7
/// ```
pub fn uuid_blob(context: *mut sqlite3_context, values: &[*mut sqlite3_value]) -> Result<()> {
let version = {
let v = values.get(0);
if v.is_none() {
4
} else {
api::value_int(v.unwrap())
}
};

match version {
4 => {
let val = uuid::Uuid::new_v4();
api::result_blob(context, val.as_bytes());
}
7 => {
let val = uuid::Uuid::now_v7();
pub fn uuid_blob(context: *mut sqlite3_context, _values: &[*mut sqlite3_value]) -> Result<()> {
let val = uuid::Uuid::new_v4();
api::result_blob(context, val.as_bytes());
}
_ => {
let val = uuid::Uuid::new_v4();
api::result_blob(context, val.as_bytes());
}
}
Ok(())
}

/// Create a blob UUIDv7
///
/// ## Example
///
/// ```sql
/// SELECT uuid_v7_blob(); -- v7
/// ```
pub fn uuid_v7_blob(context: *mut sqlite3_context, _values: &[*mut sqlite3_value]) -> Result<()> {
let val = uuid::Uuid::now_v7();
api::result_blob(context, val.as_bytes());
Ok(())
}

/// Convert a blob to a string representation of a UUID
///
/// It doesn't matter if it's a v4 or v7 UUID, it will be converted to a string
///
/// ## Example
///
/// ```sql
Expand All @@ -93,6 +72,8 @@ pub fn uuid_from_blob(context: *mut sqlite3_context, values: &[*mut sqlite3_valu

/// Convert a string representation of a UUID to blob
///
/// It doesn't matter if it's a v4 or v7 UUID, it will be converted to a string
///
/// ## Example
///
/// ```sql
Expand All @@ -111,9 +92,9 @@ pub fn uuid_as_blob(context: *mut sqlite3_context, values: &[*mut sqlite3_value]
#[sqlite_entrypoint]
pub fn sqlite3_sqliteuuid_init(db: *mut sqlite3) -> Result<()> {
define_scalar_function(db, "uuid", 0, uuid, FunctionFlags::UTF8)?;
define_scalar_function(db, "uuid", 1, uuid, FunctionFlags::UTF8)?;
define_scalar_function(db, "uuid_v7", 0, uuid_v7, FunctionFlags::UTF8)?;
define_scalar_function(db, "uuid_blob", 0, uuid_blob, FunctionFlags::UTF8)?;
define_scalar_function(db, "uuid_blob", 1, uuid_blob, FunctionFlags::UTF8)?;
define_scalar_function(db, "uuid_v7_blob", 0, uuid_v7_blob, FunctionFlags::UTF8)?;
define_scalar_function(
db,
"uuid_from_blob",
Expand Down

0 comments on commit 0753dbf

Please sign in to comment.