db

package
v2.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 19, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package db defines a database-agnostic Store abstraction for the Leaf framework. Concrete drivers live in sub-packages (db/sqlite, db/postgres) and all expose the same Store interface, so business code can switch the underlying database without any change.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dialect

type Dialect int

Dialect identifies the SQL flavor of the underlying driver. It is used to rewrite portable queries (written with "?" placeholders) into the syntax that the target database expects.

const (
	// DialectSQLite targets SQLite (placeholders are "?").
	DialectSQLite Dialect = iota
	// DialectPostgres targets PostgreSQL (placeholders are "$1", "$2", ...).
	DialectPostgres
)

type Store

type Store interface {
	// NextSeq atomically increments and returns the next value of the named
	// sequence, creating it (starting at 1) on first use.
	NextSeq(ctx context.Context, name string) (int64, error)

	// Get returns the value stored under key. ok reports whether the key
	// exists.
	Get(ctx context.Context, key string) (value []byte, ok bool, err error)
	// Set inserts or updates the value stored under key.
	Set(ctx context.Context, key string, value []byte) error
	// Del removes key. Removing a missing key is not an error.
	Del(ctx context.Context, key string) error

	// Exec runs a raw statement. Placeholders use "?" and are rewritten for
	// the active dialect.
	Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
	// Query runs a raw query. Placeholders use "?" and are rewritten for the
	// active dialect.
	Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)
	// QueryRow runs a raw single-row query. Placeholders use "?" and are
	// rewritten for the active dialect.
	QueryRow(ctx context.Context, query string, args ...any) *sql.Row

	// IsDup reports whether err is a unique-constraint (duplicate key)
	// violation.
	IsDup(err error) bool

	// DB exposes the underlying *sql.DB for advanced use.
	DB() *sql.DB
	// Close releases all resources held by the store.
	Close() error
}

Store is the database-agnostic interface that every Leaf database driver implements. The KV and sequence helpers cover the common game-server needs (auto-increment player ids, simple persistence), while Exec/Query provide a raw escape hatch for arbitrary SQL.

Every method is goroutine safe.

func OpenSQL

func OpenSQL(ctx context.Context, sqlDB *sql.DB, dialect Dialect, isDup func(error) bool) (Store, error)

OpenSQL builds a Store on top of an already-open *sql.DB, provisioning the internal bookkeeping tables (_leaf_seq, _leaf_kv). Driver packages call this after registering and opening their database/sql driver.

Directories

Path Synopsis
Package postgres provides a PostgreSQL-backed db.Store implementation for Leaf, built on the jackc/pgx/v5 driver (PostgreSQL 18 compatible) through its database/sql-compatible stdlib adapter.
Package postgres provides a PostgreSQL-backed db.Store implementation for Leaf, built on the jackc/pgx/v5 driver (PostgreSQL 18 compatible) through its database/sql-compatible stdlib adapter.
Package sqlite provides a SQLite-backed db.Store implementation for Leaf, built on the pure-Go (CGO-free) modernc.org/sqlite driver.
Package sqlite provides a SQLite-backed db.Store implementation for Leaf, built on the pure-Go (CGO-free) modernc.org/sqlite driver.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL