Skip to content

Commit 3c31ade

Browse files
committed
Initial commit
0 parents  commit 3c31ade

File tree

7 files changed

+124
-0
lines changed

7 files changed

+124
-0
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uuidv7.so

Diff for: LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Craig Pastro
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Diff for: Makefile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.PHONY: build
2+
build:
3+
go build -buildmode=c-shared -o uuidv7.so .
4+
5+
.PHONY: lint
6+
lint:
7+
golangci-lint run

Diff for: README.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# sqlite-uuidv7
2+
3+
An SQLite extension for generating
4+
[UUIDv7s](https://datatracker.ietf.org/doc/html/draft-peabody-dispatch-new-uuid-format).
5+
It wraps https://github.com/gofrs/uuid.
6+
7+
## Usage
8+
9+
You will need a version of SQLite that allows loading of extensions. If you are
10+
on a Mac, you can `brew install sqlite`.
11+
12+
```text
13+
$ make
14+
$ sqlite3
15+
> .load uuidv7.so
16+
> select uuid_generate_v7();
17+
018898f3-3067-703b-8292-e2def527c6b4
18+
> sqlite> select quote(uuid_generate_v7_bytes());
19+
X'018898F33745703BA7618E0C33AECF3E'
20+
> .quit
21+
```
22+
23+
## Tests
24+
25+
Yes, I should add tests.

Diff for: go.mod

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module github.com/craigpastro/sqlite-uuidv7
2+
3+
go 1.20
4+
5+
require (
6+
github.com/gofrs/uuid v4.4.0+incompatible
7+
go.riyazali.net/sqlite v0.0.0-20230320080028-80a51d3944c0
8+
)
9+
10+
require github.com/mattn/go-pointer v0.0.1 // indirect

Diff for: go.sum

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
2+
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
3+
github.com/mattn/go-pointer v0.0.1 h1:n+XhsuGeVO6MEAp7xyEukFINEa+Quek5psIR/ylA6o0=
4+
github.com/mattn/go-pointer v0.0.1/go.mod h1:2zXcozF6qYGgmsG+SeTZz3oAbFLdD3OWqnUbNvJZAlc=
5+
github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg=
6+
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
7+
go.riyazali.net/sqlite v0.0.0-20230320080028-80a51d3944c0 h1:59rDFi9pMMud3hjl4DEWIiZdx8kR4LpAIVaWEnpOn6s=
8+
go.riyazali.net/sqlite v0.0.0-20230320080028-80a51d3944c0/go.mod h1:UVocl0mLwS0QKUKa5mI6lppmBjvQnUEkFjFfoWqFWQU=

Diff for: main.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/gofrs/uuid"
7+
"go.riyazali.net/sqlite"
8+
)
9+
10+
type v7String struct{}
11+
12+
func (*v7String) Args() int { return 0 }
13+
func (*v7String) Deterministic() bool { return true }
14+
func (*v7String) Apply(ctx *sqlite.Context, values ...sqlite.Value) {
15+
u, err := uuid.NewV7()
16+
if err != nil {
17+
ctx.ResultError(fmt.Errorf("error generating uuidv7: %w", err))
18+
return
19+
}
20+
21+
ctx.ResultText(u.String())
22+
}
23+
24+
type v7Byte struct{}
25+
26+
func (*v7Byte) Args() int { return 0 }
27+
func (*v7Byte) Deterministic() bool { return true }
28+
func (*v7Byte) Apply(ctx *sqlite.Context, values ...sqlite.Value) {
29+
u, err := uuid.NewV7()
30+
if err != nil {
31+
ctx.ResultError(fmt.Errorf("error generating uuidv7: %w", err))
32+
return
33+
}
34+
35+
ctx.ResultBlob(u.Bytes())
36+
}
37+
38+
func init() {
39+
sqlite.Register(func(api *sqlite.ExtensionApi) (sqlite.ErrorCode, error) {
40+
if err := api.CreateFunction("uuid_generate_v7", &v7String{}); err != nil {
41+
return sqlite.SQLITE_ERROR, err
42+
}
43+
44+
if err := api.CreateFunction("uuid_generate_v7_bytes", &v7Byte{}); err != nil {
45+
return sqlite.SQLITE_ERROR, err
46+
}
47+
48+
return sqlite.SQLITE_OK, nil
49+
})
50+
}
51+
52+
func main() {}

0 commit comments

Comments
 (0)