Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
craigpangea committed Jun 8, 2023
0 parents commit 3c31ade
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
uuidv7.so
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Craig Pastro

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.PHONY: build
build:
go build -buildmode=c-shared -o uuidv7.so .

.PHONY: lint
lint:
golangci-lint run
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# sqlite-uuidv7

An SQLite extension for generating
[UUIDv7s](https://datatracker.ietf.org/doc/html/draft-peabody-dispatch-new-uuid-format).
It wraps https://github.com/gofrs/uuid.

## Usage

You will need a version of SQLite that allows loading of extensions. If you are
on a Mac, you can `brew install sqlite`.

```text
$ make
$ sqlite3
> .load uuidv7.so
> select uuid_generate_v7();
018898f3-3067-703b-8292-e2def527c6b4
> sqlite> select quote(uuid_generate_v7_bytes());
X'018898F33745703BA7618E0C33AECF3E'
> .quit
```

## Tests

Yes, I should add tests.
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/craigpastro/sqlite-uuidv7

go 1.20

require (
github.com/gofrs/uuid v4.4.0+incompatible
go.riyazali.net/sqlite v0.0.0-20230320080028-80a51d3944c0
)

require github.com/mattn/go-pointer v0.0.1 // indirect
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/mattn/go-pointer v0.0.1 h1:n+XhsuGeVO6MEAp7xyEukFINEa+Quek5psIR/ylA6o0=
github.com/mattn/go-pointer v0.0.1/go.mod h1:2zXcozF6qYGgmsG+SeTZz3oAbFLdD3OWqnUbNvJZAlc=
github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
go.riyazali.net/sqlite v0.0.0-20230320080028-80a51d3944c0 h1:59rDFi9pMMud3hjl4DEWIiZdx8kR4LpAIVaWEnpOn6s=
go.riyazali.net/sqlite v0.0.0-20230320080028-80a51d3944c0/go.mod h1:UVocl0mLwS0QKUKa5mI6lppmBjvQnUEkFjFfoWqFWQU=
52 changes: 52 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package main

import (
"fmt"

"github.com/gofrs/uuid"
"go.riyazali.net/sqlite"
)

type v7String struct{}

func (*v7String) Args() int { return 0 }
func (*v7String) Deterministic() bool { return true }
func (*v7String) Apply(ctx *sqlite.Context, values ...sqlite.Value) {
u, err := uuid.NewV7()
if err != nil {
ctx.ResultError(fmt.Errorf("error generating uuidv7: %w", err))
return
}

ctx.ResultText(u.String())
}

type v7Byte struct{}

func (*v7Byte) Args() int { return 0 }
func (*v7Byte) Deterministic() bool { return true }
func (*v7Byte) Apply(ctx *sqlite.Context, values ...sqlite.Value) {
u, err := uuid.NewV7()
if err != nil {
ctx.ResultError(fmt.Errorf("error generating uuidv7: %w", err))
return
}

ctx.ResultBlob(u.Bytes())
}

func init() {
sqlite.Register(func(api *sqlite.ExtensionApi) (sqlite.ErrorCode, error) {
if err := api.CreateFunction("uuid_generate_v7", &v7String{}); err != nil {
return sqlite.SQLITE_ERROR, err
}

if err := api.CreateFunction("uuid_generate_v7_bytes", &v7Byte{}); err != nil {
return sqlite.SQLITE_ERROR, err
}

return sqlite.SQLITE_OK, nil
})
}

func main() {}

0 comments on commit 3c31ade

Please sign in to comment.