Skip to content

Include UUID extension #1331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
zepatrik opened this issue Mar 29, 2025 · 1 comment
Open

Include UUID extension #1331

zepatrik opened this issue Mar 29, 2025 · 1 comment

Comments

@zepatrik
Copy link

zepatrik commented Mar 29, 2025

There is a very handy extension for generating UUIDs in sqlite: https://sqlite.org/src/file/ext/misc/uuid.c
It would be nice to include it into go-sqlite3 as a loadable extension.
It's actually very small and shouldn't add too much overhead, so it could also be loaded per default.

I did not really understand how the upgrade tool pulls in extensions, or how I would add the extension there.

As an alternative, one can create a new instance of the driver with a go implementation. I successfully made that work, but it was basically impossible to use with other libraries like ORMs due to the self-initialization nature of the go SQL drivers.

@dimmerz92
Copy link

This would be a great addition imo.

For those wanting uuid functionality in the meantime, you can register it with the driver something like this (this specific one is in conjunction with sqlc):

import (
        "database/sql"
        "log"
        "os"

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

type Database struct {
        Conn    *sql.DB
        Queries *Queries
}

// opens the `DB_STRING` sqlite database.
func (db *Database) Connect() {
        // register the sqlite driver with a uuid function
        sql.Register("sqlite3_uuid", &sqlite3.SQLiteDriver{
                ConnectHook: func(sc *sqlite3.SQLiteConn) error {
                        return sc.RegisterFunc("uuid", func() string {
                                return uuid.New().String()
                        }, true)
                },
        })

        // open the sqlite connection
        conn, err := sql.Open("sqlite3_uuid", os.Getenv("DB_STRING"))
        if err != nil {
                log.Fatalf("failed to open sqlite database: %v", err)
        }

        db.Conn = conn
        db.Queries = New(db.Conn)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants