db

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: AGPL-3.0, AGPL-3.0-or-later Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package db provides database abstraction via a Dialect interface. Implementations for MySQL, LibSQL, and PostgreSQL live in separate files. The Dialect is selected at startup via the KORA_DB_TYPE env var.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AgentTablesLibSQL

func AgentTablesLibSQL() []string

func AgentTablesMySQL

func AgentTablesMySQL() []string

AgentTables returns tenant-scoped runtime tables for generic agent manifests and durable run/handoff records. JSON remains flexible while the ownership and timestamps stay queryable.

func AgentTablesPostgres

func AgentTablesPostgres() []string

func BoolParam

func BoolParam(dialect QueryDialect, value bool) any

BoolParam returns the bound representation used by Kora's boolean-like SQL columns. PostgreSQL's system tables store several of these flags as SMALLINT for compatibility with existing schemas; MySQL and LibSQL accept bool.

func ExtensibilityTablesLibSQL

func ExtensibilityTablesLibSQL() []string

ExtensibilityTablesLibSQL returns LibSQL-compatible extensibility DDL.

func ExtensibilityTablesMySQL

func ExtensibilityTablesMySQL() []string

ExtensibilityTablesMySQL returns MySQL-specific extensibility DDL.

func ExtensibilityTablesPostgres

func ExtensibilityTablesPostgres() []string

ExtensibilityTablesPostgres returns PostgreSQL-specific system DDL.

func IdentityTablesLibSQL

func IdentityTablesLibSQL() []string

IdentityTablesLibSQL returns the LibSQL DDL for sign-in providers and identity links.

func IdentityTablesMySQL

func IdentityTablesMySQL() []string

IdentityTablesMySQL returns the MySQL DDL for sign-in providers and identity links.

func IdentityTablesPostgres

func IdentityTablesPostgres() []string

IdentityTablesPostgres returns the PostgreSQL DDL for sign-in providers and identity links.

func KernelTablesLibSQL

func KernelTablesLibSQL() []string

KernelTablesLibSQL returns the LibSQL-compatible kernel DDL.

func KernelTablesMySQL

func KernelTablesMySQL() []string

KernelTablesMySQL returns the MySQL DDL for the operation kernel system tables (KERNEL-006, KERNEL-007): the idempotency receipt store (RFC §8.2) and the unified operation audit ledger. Both are written inside the same SQL transaction as the business mutation so that a commit implies exactly one receipt, one audit row, and the outbox events for that operation. New scoped receipts store stable tenant identity in the legacy "site" column.

func KernelTablesPostgres

func KernelTablesPostgres() []string

KernelTablesPostgres returns the PostgreSQL-compatible kernel DDL. PostgreSQL is the reference dialect for these tables.

func OutboxTablesLibSQL

func OutboxTablesLibSQL() []string

OutboxTablesLibSQL returns the LibSQL-compatible outbox DDL.

func OutboxTablesMySQL

func OutboxTablesMySQL() []string

OutboxTablesMySQL returns the MySQL DDL for the transactional outbox (RFC §8.1). The outbox table is written in the same SQL transaction as the business write so that no event can be lost between commit and publish.

func OutboxTablesPostgres

func OutboxTablesPostgres() []string

OutboxTablesPostgres returns the PostgreSQL-compatible outbox DDL.

func PaymentTablesLibSQL

func PaymentTablesLibSQL() []string

PaymentTablesLibSQL returns the LibSQL-compatible payment intent DDL.

func PaymentTablesMySQL

func PaymentTablesMySQL() []string

PaymentTablesMySQL returns the MySQL DDL for the engine-owned payment intent store. Idempotency keys are unique per site, and a provider's request id is unique per (site, provider) so asynchronous confirmations resolve to exactly one intent.

func PaymentTablesPostgres

func PaymentTablesPostgres() []string

PaymentTablesPostgres returns the PostgreSQL payment intent DDL.

func Rebind

func Rebind(d QueryDialect, query string) string

Rebind converts a dialect-neutral '?'-placeholder query into the placeholder style required by the underlying driver. MySQL and LibSQL use '?' natively; PostgreSQL (lib/pq) requires ordered '$n' markers.

This is the transitional bridge for packages that compose raw SQL while DB-001 migrates the ORM to dialect.Placeholder() everywhere. Quoted string literals containing '?' are preserved.

Types

type DBConfig

type DBConfig struct {
	Type     string // "mysql", "libsql", "postgres"
	Host     string
	Port     int
	Name     string
	User     string
	Password string
	// LibSQL-specific: URL for remote connection (e.g., "libsql://db.turso.io")
	URL string
	// Additional parameters appended to the DSN.
	Params map[string]string
}

DBConfig holds the connection parameters for any database. It replaces the MySQL-specific DSN construction in site/site.go.

type Dialect

type Dialect interface {
	SchemaDialect
	QueryDialect
	ErrorDialect

	// DriverName returns the database/sql driver name (e.g., "mysql", "libsql").
	DriverName() string

	// Open opens a database connection using the given config.
	Open(cfg DBConfig) (*sql.DB, error)

	// LoadSchema introspects the live database schema for comparison with
	// the doctype registry (used by schema migration).
	LoadSchema(db *sql.DB, dbName string) (*LiveSchema, error)

	// QuoteIdent quotes an identifier (table or column name) for safe embedding in SQL.
	QuoteIdent(name string) string

	// TableSuffix returns the table options suffix (ENGINE/CHARSET for MySQL, empty for LibSQL).
	TableSuffix() string

	// SystemTableSQL returns the CREATE TABLE IF NOT EXISTS DDL for all _kora_*
	// system tables. Each dialect provides its own version.
	SystemTableSQL() []string

	// ExecuteBatch runs multiple DDL statements as a single atomic operation.
	// For LibSQL: joins statements with semicolons for a single HTTP round-trip.
	// For MySQL: wraps in a transaction.
	// For embedded SQLite: wraps in a transaction.
	ExecuteBatch(db *sql.DB, statements []string) error
}

Dialect abstracts database-specific SQL generation, schema introspection, and error parsing. It composes SchemaDialect, QueryDialect, and ErrorDialect. Implementations are in dialect_*.go files.

func MySQL

func MySQL() Dialect

MySQL returns the MySQL dialect (for callers that don't have DBType info).

func Resolve

func Resolve(dbType string) Dialect

Resolve returns the Dialect for the given database type. Default is "mysql" for backward compatibility.

KORA_DB_TYPE=mysql    → MySQL dialect (default)
KORA_DB_TYPE=libsql   → LibSQL dialect
KORA_DB_TYPE=postgres → PostgreSQL dialect

type ErrorDialect

type ErrorDialect interface {
	ParseError(err error, dt *doctype.DocType) *doctype.ValidationError
	// IsWriteConflict identifies database-native optimistic/serialization
	// conflicts so callers can preserve a stable contract without inspecting
	// driver error strings outside the database boundary.
	IsWriteConflict(err error) bool
}

ErrorDialect is the error-parsing subset.

type LibSQLDialect

type LibSQLDialect struct{}

LibSQLDialect implements Dialect for LibSQL (Turso's SQLite fork). LibSQL can be deployed as a managed database — Kigae connects to it remotely via HTTP. This adapter handles SQL dialect differences: PRAGMA instead of INFORMATION_SCHEMA, SQLite types, and constraint errors.

func (*LibSQLDialect) AddColumn

func (d *LibSQLDialect) AddColumn(tableName string, f *doctype.Field) string

func (*LibSQLDialect) AllocateNameNumber

func (d *LibSQLDialect) AllocateNameNumber(tx *sql.Tx, doctypeName, prefix, tableName string) (int64, error)

func (*LibSQLDialect) AllocateSeriesNumber

func (d *LibSQLDialect) AllocateSeriesNumber(tx *sql.Tx, doctypeName, key string, seed int64) (int64, error)

func (*LibSQLDialect) AlterColumn

func (d *LibSQLDialect) AlterColumn(tableName string, f *doctype.Field) string

func (*LibSQLDialect) ChildColumnDDL

func (d *LibSQLDialect) ChildColumnDDL() []string

func (*LibSQLDialect) ColumnType

func (d *LibSQLDialect) ColumnType(f *doctype.Field) string

func (*LibSQLDialect) CreateIndex

func (d *LibSQLDialect) CreateIndex(tableName, fieldName string, unique bool) string

func (*LibSQLDialect) CreateSearchIndex

func (d *LibSQLDialect) CreateSearchIndex(tableName, fieldName string) string

func (*LibSQLDialect) CreateTable

func (d *LibSQLDialect) CreateTable(dt *doctype.DocType) []string

func (*LibSQLDialect) DriverName

func (d *LibSQLDialect) DriverName() string

func (*LibSQLDialect) DropColumn

func (d *LibSQLDialect) DropColumn(tableName, columnName string) string

func (*LibSQLDialect) DropIndex

func (d *LibSQLDialect) DropIndex(tableName, indexName string) string

func (*LibSQLDialect) ExecuteBatch

func (d *LibSQLDialect) ExecuteBatch(db *sql.DB, statements []string) error

ExecuteBatch runs multiple DDL statements in a single batch for LibSQL. Statements are joined with semicolons and sent as one HTTP request, which executes atomically in an implicit transaction on the Turso server. This avoids the O(n) HTTP round-trip overhead of individual Exec calls. If the batch fails with an idempotent error (e.g. "already exists"), it falls back to sequential execution with idempotent error skipping.

func (*LibSQLDialect) InsertOrIgnorePrefix

func (d *LibSQLDialect) InsertOrIgnorePrefix() string

func (*LibSQLDialect) IsWriteConflict

func (d *LibSQLDialect) IsWriteConflict(err error) bool

func (*LibSQLDialect) LoadSchema

func (d *LibSQLDialect) LoadSchema(db *sql.DB, _ string) (*LiveSchema, error)

func (*LibSQLDialect) NameGenQuery

func (d *LibSQLDialect) NameGenQuery(tableName, prefix string) string

func (*LibSQLDialect) NowTimestamp

func (d *LibSQLDialect) NowTimestamp() string

func (*LibSQLDialect) Open

func (d *LibSQLDialect) Open(cfg DBConfig) (*sql.DB, error)

func (*LibSQLDialect) ParseError

func (d *LibSQLDialect) ParseError(err error, dt *doctype.DocType) *doctype.ValidationError

func (*LibSQLDialect) Placeholder

func (d *LibSQLDialect) Placeholder(n int) string

func (*LibSQLDialect) QuoteIdent

func (d *LibSQLDialect) QuoteIdent(name string) string

func (*LibSQLDialect) RenameColumn

func (d *LibSQLDialect) RenameColumn(tableName, oldName, newName string) string

func (*LibSQLDialect) SearchIndexName

func (d *LibSQLDialect) SearchIndexName(tableName, fieldName string) string

func (*LibSQLDialect) SystemColumnDDL

func (d *LibSQLDialect) SystemColumnDDL() []string

func (*LibSQLDialect) SystemTableSQL

func (d *LibSQLDialect) SystemTableSQL() []string

func (*LibSQLDialect) TableSuffix

func (d *LibSQLDialect) TableSuffix() string

func (*LibSQLDialect) UpsertClause

func (d *LibSQLDialect) UpsertClause(conflictCols []string, updateCols []string) string

func (*LibSQLDialect) UpsertIncrement

func (d *LibSQLDialect) UpsertIncrement(conflictCols []string, incrementCols []string) string

func (*LibSQLDialect) UpsertIncrementForTable

func (d *LibSQLDialect) UpsertIncrementForTable(_ string, conflictCols []string, incrementCols []string) string

type LiveColumn

type LiveColumn struct {
	Name         string
	Type         string // raw column type as reported by the DB
	IsNullable   bool
	DefaultValue string
}

LiveColumn represents a column discovered from the live database.

type LiveIndex

type LiveIndex struct {
	Name     string
	Columns  []string
	IsUnique bool
}

LiveIndex represents an index discovered from the live database.

type LiveSchema

type LiveSchema struct {
	Tables map[string]*LiveTable
}

LiveSchema represents the current state of the database as discovered by the Dialect's LoadSchema method.

type LiveTable

type LiveTable struct {
	Name    string
	Columns []LiveColumn
	Indexes []LiveIndex
}

LiveTable represents a database table discovered at runtime.

type MySQLDialect

type MySQLDialect struct{}

MySQLDialect implements Dialect for MySQL 8.0.

func (*MySQLDialect) AddColumn

func (d *MySQLDialect) AddColumn(tableName string, f *doctype.Field) string

func (*MySQLDialect) AllocateNameNumber

func (d *MySQLDialect) AllocateNameNumber(tx *sql.Tx, doctypeName, prefix, tableName string) (int64, error)

func (*MySQLDialect) AllocateSeriesNumber

func (d *MySQLDialect) AllocateSeriesNumber(tx *sql.Tx, doctypeName, key string, seed int64) (int64, error)

func (*MySQLDialect) AlterColumn

func (d *MySQLDialect) AlterColumn(tableName string, f *doctype.Field) string

func (*MySQLDialect) ChildColumnDDL

func (d *MySQLDialect) ChildColumnDDL() []string

func (*MySQLDialect) ColumnType

func (d *MySQLDialect) ColumnType(f *doctype.Field) string

func (*MySQLDialect) CreateIndex

func (d *MySQLDialect) CreateIndex(tableName, fieldName string, unique bool) string

func (*MySQLDialect) CreateTable

func (d *MySQLDialect) CreateTable(dt *doctype.DocType) []string

func (*MySQLDialect) DriverName

func (d *MySQLDialect) DriverName() string

func (*MySQLDialect) DropColumn

func (d *MySQLDialect) DropColumn(tableName, columnName string) string

func (*MySQLDialect) DropIndex

func (d *MySQLDialect) DropIndex(tableName, indexName string) string

func (*MySQLDialect) ExecuteBatch

func (d *MySQLDialect) ExecuteBatch(db *sql.DB, statements []string) error

ExecuteBatch runs multiple DDL statements atomically inside a transaction.

func (*MySQLDialect) InsertOrIgnorePrefix

func (d *MySQLDialect) InsertOrIgnorePrefix() string

func (*MySQLDialect) IsWriteConflict

func (d *MySQLDialect) IsWriteConflict(err error) bool

func (*MySQLDialect) LoadSchema

func (d *MySQLDialect) LoadSchema(db *sql.DB, dbName string) (*LiveSchema, error)

func (*MySQLDialect) NameGenQuery

func (d *MySQLDialect) NameGenQuery(tableName, prefix string) string

func (*MySQLDialect) NowTimestamp

func (d *MySQLDialect) NowTimestamp() string

func (*MySQLDialect) Open

func (d *MySQLDialect) Open(cfg DBConfig) (*sql.DB, error)

func (*MySQLDialect) ParseError

func (d *MySQLDialect) ParseError(err error, dt *doctype.DocType) *doctype.ValidationError

func (*MySQLDialect) Placeholder

func (d *MySQLDialect) Placeholder(n int) string

func (*MySQLDialect) QuoteIdent

func (d *MySQLDialect) QuoteIdent(name string) string

func (*MySQLDialect) RenameColumn

func (d *MySQLDialect) RenameColumn(tableName, oldName, newName string) string

func (*MySQLDialect) SystemColumnDDL

func (d *MySQLDialect) SystemColumnDDL() []string

func (*MySQLDialect) SystemTableSQL

func (d *MySQLDialect) SystemTableSQL() []string

func (*MySQLDialect) TableSuffix

func (d *MySQLDialect) TableSuffix() string

func (*MySQLDialect) UpsertClause

func (d *MySQLDialect) UpsertClause(conflictCols []string, updateCols []string) string

func (*MySQLDialect) UpsertIncrement

func (d *MySQLDialect) UpsertIncrement(conflictCols []string, incrementCols []string) string

func (*MySQLDialect) UpsertIncrementForTable

func (d *MySQLDialect) UpsertIncrementForTable(_ string, conflictCols []string, incrementCols []string) string

type PostgresDialect

type PostgresDialect struct{}

PostgresDialect implements Dialect for PostgreSQL. It is a proof-of-concept that adding a new dialect to Kigae requires only a single file implementing the Dialect interface.

func (*PostgresDialect) AddColumn

func (d *PostgresDialect) AddColumn(tableName string, f *doctype.Field) string

func (*PostgresDialect) AllocateNameNumber

func (d *PostgresDialect) AllocateNameNumber(tx *sql.Tx, doctypeName, prefix, tableName string) (int64, error)

func (*PostgresDialect) AllocateSeriesNumber

func (d *PostgresDialect) AllocateSeriesNumber(tx *sql.Tx, doctypeName, key string, seed int64) (int64, error)

func (*PostgresDialect) AlterColumn

func (d *PostgresDialect) AlterColumn(tableName string, f *doctype.Field) string

func (*PostgresDialect) ChildColumnDDL

func (d *PostgresDialect) ChildColumnDDL() []string

func (*PostgresDialect) ColumnType

func (d *PostgresDialect) ColumnType(f *doctype.Field) string

func (*PostgresDialect) CreateIndex

func (d *PostgresDialect) CreateIndex(tableName, fieldName string, unique bool) string

func (*PostgresDialect) CreateSearchIndex

func (d *PostgresDialect) CreateSearchIndex(tableName, fieldName string) string

CreateSearchIndex creates a B-tree operator-class index suitable for the left-anchored LIKE predicates used by quick search. This keeps PostgreSQL from falling back to a table scan for configured picker fields.

func (*PostgresDialect) CreateTable

func (d *PostgresDialect) CreateTable(dt *doctype.DocType) []string

func (*PostgresDialect) DriverName

func (d *PostgresDialect) DriverName() string

func (*PostgresDialect) DropColumn

func (d *PostgresDialect) DropColumn(tableName, columnName string) string

func (*PostgresDialect) DropIndex

func (d *PostgresDialect) DropIndex(tableName, indexName string) string

func (*PostgresDialect) ExecuteBatch

func (d *PostgresDialect) ExecuteBatch(db *sql.DB, statements []string) error

ExecuteBatch runs multiple DDL statements atomically inside a transaction.

func (*PostgresDialect) InsertOrIgnorePrefix

func (d *PostgresDialect) InsertOrIgnorePrefix() string

func (*PostgresDialect) IsWriteConflict

func (d *PostgresDialect) IsWriteConflict(err error) bool

func (*PostgresDialect) LoadSchema

func (d *PostgresDialect) LoadSchema(db *sql.DB, _ string) (*LiveSchema, error)

func (*PostgresDialect) NameGenQuery

func (d *PostgresDialect) NameGenQuery(tableName, prefix string) string

func (*PostgresDialect) NowTimestamp

func (d *PostgresDialect) NowTimestamp() string

func (*PostgresDialect) Open

func (d *PostgresDialect) Open(cfg DBConfig) (*sql.DB, error)

func (*PostgresDialect) ParseError

func (d *PostgresDialect) ParseError(err error, dt *doctype.DocType) *doctype.ValidationError

func (*PostgresDialect) Placeholder

func (d *PostgresDialect) Placeholder(n int) string

func (*PostgresDialect) QuoteIdent

func (d *PostgresDialect) QuoteIdent(name string) string

func (*PostgresDialect) RenameColumn

func (d *PostgresDialect) RenameColumn(tableName, oldName, newName string) string

func (*PostgresDialect) SearchIndexName

func (d *PostgresDialect) SearchIndexName(tableName, fieldName string) string

func (*PostgresDialect) SystemColumnDDL

func (d *PostgresDialect) SystemColumnDDL() []string

func (*PostgresDialect) SystemTableSQL

func (d *PostgresDialect) SystemTableSQL() []string

func (*PostgresDialect) TableSuffix

func (d *PostgresDialect) TableSuffix() string

func (*PostgresDialect) UpsertClause

func (d *PostgresDialect) UpsertClause(conflictCols []string, updateCols []string) string

func (*PostgresDialect) UpsertIncrement

func (d *PostgresDialect) UpsertIncrement(conflictCols []string, incrementCols []string) string

func (*PostgresDialect) UpsertIncrementForTable

func (d *PostgresDialect) UpsertIncrementForTable(tableName string, conflictCols []string, incrementCols []string) string

type QueryDialect

type QueryDialect interface {
	UpsertClause(conflictCols []string, updateCols []string) string
	UpsertIncrement(conflictCols []string, incrementCols []string) string
	InsertOrIgnorePrefix() string
	AllocateNameNumber(tx *sql.Tx, doctypeName, prefix, tableName string) (int64, error)
	// AllocateSeriesNumber atomically increments the counter for one naming
	// series key, starting after seed when the key is new. All values are bound.
	AllocateSeriesNumber(tx *sql.Tx, doctypeName, key string, seed int64) (int64, error)
	NameGenQuery(tableName, prefix string) string
	Placeholder(n int) string
	NowTimestamp() string
}

QueryDialect is the DML subset (insert/update/select helpers).

type Queryer

type Queryer interface {
	Exec(query string, args ...any) (sql.Result, error)
	Query(query string, args ...any) (*sql.Rows, error)
	QueryRow(query string, args ...any) *sql.Row
}

Queryer is satisfied by *sql.DB and *sql.Tx. It provides Exec, Query, and QueryRow for use by packages that need to run SQL against either a database or a transaction.

type SchemaDialect

type SchemaDialect interface {
	CreateTable(dt *doctype.DocType) []string
	AddColumn(tableName string, f *doctype.Field) string
	AlterColumn(tableName string, f *doctype.Field) string
	RenameColumn(tableName, oldName, newName string) string
	CreateIndex(tableName, fieldName string, unique bool) string
	DropIndex(tableName, indexName string) string
	DropColumn(tableName, columnName string) string
	ColumnType(f *doctype.Field) string
	SystemColumnDDL() []string
	ChildColumnDDL() []string
	QuoteIdent(name string) string
}

SchemaDialect is the DDL subset of Dialect (table/column/index management).

type SearchIndexDialect

type SearchIndexDialect interface {
	CreateSearchIndex(tableName, fieldName string) string
	SearchIndexName(tableName, fieldName string) string
}

SearchIndexDialect may provide a non-unique index compatible with the dialect's prefix-search predicates. Unique constraints remain separate and keep their existing comparison rules.

type TableQualifiedIncrementDialect

type TableQualifiedIncrementDialect interface {
	UpsertIncrementForTable(tableName string, conflictCols []string, incrementCols []string) string
}

TableQualifiedIncrementDialect provides an incrementing upsert expression that can qualify the existing target row. PostgreSQL needs this when the inserted and existing rows both expose the incremented column name.

type Timestamp

type Timestamp struct{ time.Time }

Timestamp scans native SQL timestamp values and the UTC text values returned by SQLite/LibSQL timestamp expressions.

func (*Timestamp) Scan

func (stamp *Timestamp) Scan(value any) error

Jump to

Keyboard shortcuts

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