Skip to content

Instantly share code, notes, and snippets.

@keisisqrl
Last active June 17, 2024 20:18
Show Gist options
  • Save keisisqrl/50ccccd353e3ebf293a6fd09102e06aa to your computer and use it in GitHub Desktop.
Save keisisqrl/50ccccd353e3ebf293a6fd09102e06aa to your computer and use it in GitHub Desktop.
uuidgen for sqlite
/* uuidgen.sql
*
* creates a view uuidgen(uuid TEXT) which generates v4 UUIDs on demand
* generated per RFC 4122
*
* it's not a stored procedure exactly but subqueries are allowed in
* any expression
**/
CREATE VIEW uuidgen AS
SELECT printf('%x-%x-%x-%x-%x',
random() & 0xffffffff,
random() & 0xffff,
random() & 0x0fff | 0x4000, -- version 4, random uuid
random() & 0x3fff | 0x8000, -- variant 1, RFC 4122 uuid
random() & 0xffffffffffff)
AS uuid;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment