Skip to content

Commit

Permalink
Fix #99
Browse files Browse the repository at this point in the history
  • Loading branch information
ncruces committed Jul 3, 2024
1 parent 72f8ad0 commit ad06862
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
87 changes: 87 additions & 0 deletions ext/uuid/uuid.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package uuid

import (
"fmt"

"github.com/google/uuid"
"github.com/ncruces/go-sqlite3"
)

func Register(db *sqlite3.Conn) {
flags := sqlite3.DETERMINISTIC | sqlite3.INNOCUOUS
db.CreateFunction("uuid", 0, sqlite3.INNOCUOUS, generate)
db.CreateFunction("uuid", 1, sqlite3.INNOCUOUS, generate)
db.CreateFunction("uuid_str", 1, flags, toString)
db.CreateFunction("uuid_blob", 1, flags, toBLOB)
}

func generate(ctx sqlite3.Context, arg ...sqlite3.Value) {
var (
version int
err error
u uuid.UUID
)

if len(arg) > 0 {
version = arg[0].Int()
}

switch version {
case 0, 4:
u, err = uuid.NewRandom()
case 1:
u, err = uuid.NewUUID()
case 6:
u, err = uuid.NewV6()
case 7:
u, err = uuid.NewV7()
default:
err = fmt.Errorf("invalid version: %d", version)
}

if err != nil {
ctx.ResultError(fmt.Errorf("uuid: %w", err))
} else {
ctx.ResultText(u.String())
}
}

func fromValue(arg sqlite3.Value) (u uuid.UUID, err error) {
switch t := arg.Type(); t {
case sqlite3.TEXT:
u, err = uuid.ParseBytes(arg.RawText())
if err != nil {
err = fmt.Errorf("uuid: %w", err)
}

case sqlite3.BLOB:
blob := arg.RawBlob()
if len := len(blob); len != 16 {
err = fmt.Errorf("uuid: invalid BLOB length: %d", len)
} else {
copy(u[:], blob)
}

default:
err = fmt.Errorf("uuid: invalid type: %v", t)
}
return u, err
}

func toBLOB(ctx sqlite3.Context, arg ...sqlite3.Value) {
u, err := fromValue(arg[0])
if err != nil {
ctx.ResultError(err)
} else {
ctx.ResultBlob(u[:])
}
}

func toString(ctx sqlite3.Context, arg ...sqlite3.Value) {
u, err := fromValue(arg[0])
if err != nil {
ctx.ResultError(err)
} else {
ctx.ResultText(u.String())
}
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ require (
lukechampine.com/adiantum v1.1.1
)

require github.com/google/uuid v1.6.0

retract v0.4.0 // tagged from the wrong branch
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/dchest/siphash v1.2.3 h1:QXwFc8cFOR2dSa/gE6o/HokBMWtLUaNDVd+22aKHeEA=
github.com/dchest/siphash v1.2.3/go.mod h1:0NvQU092bT0ipiFN++/rXm69QG9tVxLAlQHIXMPAkHc=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/ncruces/julianday v1.0.0 h1:fH0OKwa7NWvniGQtxdJRxAgkBMolni2BjDHaWTxqt7M=
github.com/ncruces/julianday v1.0.0/go.mod h1:Dusn2KvZrrovOMJuOt0TNXL6tB7U2E8kvza5fFc9G7g=
github.com/ncruces/sort v0.1.2 h1:zKQ9CA4fpHPF6xsUhRTfi5EEryspuBpe/QA4VWQOV1U=
Expand Down

0 comments on commit ad06862

Please sign in to comment.