Skip to content

uuid

Package uuid provides RFC 4122 UUID SQL functions backed by github.com/google/uuid.

  • uuid([version [, …]]) → TEXT Generate a UUID. Defaults to v4 when called with no args. Version selector covers v1 (time-based), v4 (random), v6 (reordered time-based), v7 (Unix epoch time-based), v3 / v5 (name-based MD5 / SHA-1, requires namespace + data), and v2 (DCE Security, requires domain + id — see below).
  • gen_random_uuid() → TEXT: shorthand for uuid() = uuid(4).
  • uuid_str(u) → TEXT: parse a UUID (TEXT or 16-byte BLOB) into the canonical 8-4-4-4-12 hex string.
  • uuid_blob(u) → BLOB: parse a UUID into its 16-byte BLOB form.
  • uuid_extract_version(u) → INTEGER: the RFC 4122 version field.
  • uuid_extract_timestamp(u) → INTEGER: Unix seconds for v1/v6/v7 UUIDs; NULL otherwise.
  • uuid_extract_domain(u) → TEXT: the DCE domain (person/group/org) of a v2 UUID; NULL otherwise.
  • uuid_extract_id(u) → INTEGER: the local identifier (UID/GID) of a v2 UUID; NULL otherwise.

For v3 / v5 (name-based) UUIDs, the namespace may be any of:

  • a TEXT UUID literal (e.g. ‘6ba7b810-9dad-11d1-80b4-00c04fd430c8’)
  • a 16-byte BLOB
  • one of the well-known namespace shortcuts: “dns”, “url”, “oid”, “x500” (case-insensitive)

For v2 (DCE Security), call uuid(2, domain, id): domain is a name (person/group/org, case-insensitive) or its numeric code (0/1/2), and id is the local identifier (a POSIX UID/GID for the person/group domains). The id is supplied explicitly rather than read from the process, so generation stays portable and deterministic.

Ported from ncruces/ext/uuid.

import (
sqlite "gosqlite.org"
"gosqlite.org/ext/uuid"
)
if err := uuid.Register(conn); err != nil { ... }
row := db.QueryRow(`SELECT uuid(4)`) // random v4

For pool-wide install via gosqlite.org.Driver.ConnectHook, blank-import the auto sub-package:

import _ "gosqlite.org/ext/uuid/auto"

Full API: pkg.go.dev/gosqlite.org/ext/uuid