lightning-terminal/db/sqlc/db_custom.go
Elle Mouton d3db19e2cc
db: SQL boilerplate code
This commit adds the boilerplate code we will need in order to start
using SQL backed stores (namely sqlite and postgres) for our DB backend.

NOTE that this has been copied from the taproot-assets repo.
2025-01-29 16:37:20 +02:00

46 lines
1.2 KiB
Go

package sqlc
// BackendType is an enum that represents the type of database backend we're
// using.
type BackendType uint8
const (
// BackendTypeUnknown indicates we're using an unknown backend.
BackendTypeUnknown BackendType = iota
// BackendTypeSqlite indicates we're using a SQLite backend.
BackendTypeSqlite
// BackendTypePostgres indicates we're using a Postgres backend.
BackendTypePostgres
)
// wrappedTX is a wrapper around a DBTX that also stores the database backend
// type.
type wrappedTX struct {
DBTX
backendType BackendType
}
// Backend returns the type of database backend we're using.
func (q *Queries) Backend() BackendType {
wtx, ok := q.db.(*wrappedTX)
if !ok {
// Shouldn't happen unless a new database backend type is added
// but not initialized correctly.
return BackendTypeUnknown
}
return wtx.backendType
}
// NewSqlite creates a new Queries instance for a SQLite database.
func NewSqlite(db DBTX) *Queries {
return &Queries{db: &wrappedTX{db, BackendTypeSqlite}}
}
// NewPostgres creates a new Queries instance for a Postgres database.
func NewPostgres(db DBTX) *Queries {
return &Queries{db: &wrappedTX{db, BackendTypePostgres}}
}