db

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2026 License: AGPL-3.0, AGPL-3.0-or-later Imports: 6 Imported by: 0

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

This section is empty.

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 {
	// 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)

	// DDL generation.
	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

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

	// SystemColumnDDL returns DDL fragments for the 7 standard system columns
	// (name, owner, creation, modified, modified_by, doc_status, idx).
	SystemColumnDDL() []string

	// ChildColumnDDL returns DDL fragments for child-table system columns
	// (parent, parentfield, parenttype).
	ChildColumnDDL() []string

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

	// ColumnType returns the DDL column type for a field.
	ColumnType(f *doctype.Field) string

	// NowTimestamp returns the SQL expression for the current timestamp.
	NowTimestamp() string

	// ParseError converts a database error into a doctype.ValidationError if recognized.
	// Returns nil if the error is not a constraint violation.
	ParseError(err error, dt *doctype.DocType) *doctype.ValidationError

	// Placeholder returns the parameter placeholder for the nth argument (1-indexed).
	// MySQL/LibSQL use "?". PostgreSQL uses "$1", "$2", etc.
	Placeholder(n int) string

	// UpsertClause generates an upsert suffix for INSERT statements.
	// MySQL: ON DUPLICATE KEY UPDATE col = VALUES(col), ...
	// SQLite: ON CONFLICT(cols) DO UPDATE SET col = excluded.col, ...
	UpsertClause(conflictCols []string, updateCols []string) string

	// InsertOrIgnorePrefix returns the prefix for an idempotent INSERT.
	// MySQL: "INSERT IGNORE" — SQLite: "INSERT OR IGNORE"
	InsertOrIgnorePrefix() string

	// NameGenQuery returns SQL to find the next numeric suffix for document naming.
	// MySQL uses SUBSTRING_INDEX + CAST AS UNSIGNED.
	// SQLite uses SUBSTR + INSTR + CAST AS INTEGER.
	NameGenQuery(tableName, prefix string) string

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

Dialect abstracts database-specific SQL generation, schema introspection, and error parsing. 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 (future)

type LibSQLDialect

type LibSQLDialect struct{}

LibSQLDialect implements Dialect for LibSQL (Turso's SQLite fork). LibSQL is deployed as a managed database in Dokploy — Kora 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) 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) 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) InsertOrIgnorePrefix

func (d *LibSQLDialect) InsertOrIgnorePrefix() string

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) 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

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) 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) InsertOrIgnorePrefix

func (d *MySQLDialect) InsertOrIgnorePrefix() string

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

Jump to

Keyboard shortcuts

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