driver

package
v0.38.0 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2026 License: AGPL-3.0 Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const MaxArrayRows = 100000

MaxArrayRows limits the number of rows that can be populated in a single Populate call to prevent unbounded memory/CPU consumption.

View Source
const MaxCSVRows = 100000

MaxCSVRows limits the number of data rows that can be loaded from a single CSV file to prevent unbounded memory/CPU consumption. This mirrors the Array driver's MaxArrayRows limit.

Variables

View Source
var PlaceholderFuncs = map[string]PlaceholderFunc{
	"mysql":     mysqlPlaceholder,
	"oracle":    oraclePlaceholder,
	"postgres":  postgresPlaceholder,
	"sqlite":    sqlitePlaceholder,
	"sqlserver": sqlserverPlaceholder,
	"turso":     sqlitePlaceholder,
	"array":     sqlitePlaceholder,
}

PlaceholderFuncs contains placeholder functions for different dialects.

Functions

This section is empty.

Types

type Array added in v0.27.0

type Array struct {
	*SQLite
	// contains filtered or unexported fields
}

Array implements the Driver interface for array-backed storage using SQLite.

func NewArray added in v0.27.0

func NewArray() *Array

NewArray creates a new Array driver.

func (*Array) Cleanup added in v0.27.0

func (a *Array) Cleanup(db *sql.DB)

Cleanup removes all cached entries for the given *sql.DB from the populated and locks maps. This should be called when the connection is closed to prevent unbounded memory growth in long-running services.

func (*Array) Dialect added in v0.27.0

func (a *Array) Dialect() string

Dialect returns the dialect name.

func (*Array) Populate added in v0.27.0

func (a *Array) Populate(ctx context.Context, db *sql.DB, source contractsorm.ArraySource) error

Populate populates the database with rows from the given ArraySource.

type CSVDB added in v0.36.0

type CSVDB struct {
	*SQLite
}

CSVDB implements the Driver interface for CSV-directory-backed storage. It embeds *SQLite for all standard Driver methods and overrides Open to scan a directory of CSV files and populate an in-memory SQLite database.

The directory path is passed as the DSN (from ConnectionConfig.Database). Each .csv file in the directory becomes a table named after the filename (without the .csv extension). The first row of each CSV is the header.

The driver is stateless — all state lives in the in-memory SQLite database.

func NewCSVDB added in v0.36.0

func NewCSVDB() *CSVDB

NewCSVDB creates a new CSVDB driver.

func (*CSVDB) Dialect added in v0.36.0

func (c *CSVDB) Dialect() string

Dialect returns "sqlite" so the query builder generates SQLite-compatible SQL and uses SQLite placeholders. The query builder's isSQLite() check returns true, and no ArraySource Model() hook fires (tables are already populated at Open time).

func (*CSVDB) Open added in v0.36.0

func (c *CSVDB) Open(dirPath string) (*sql.DB, error)

Open opens an in-memory SQLite database, scans the directory at dirPath for .csv files, and populates one table per file. The dirPath is the DSN, which comes from ConnectionConfig.Database via BuildDSN.

If dirPath is empty or ":memory:", no directory is scanned and an empty in-memory SQLite database is returned (useful for testing).

type Driver

type Driver interface {
	// Open opens a connection to the database.
	Open(dsn string) (*sql.DB, error)
	// Close closes the database connection.
	Close(db *sql.DB) error
	// Ping checks if the database connection is alive.
	Ping(ctx context.Context, db *sql.DB) error
	// BeginTx starts a transaction with the given options.
	BeginTx(ctx context.Context, db *sql.DB, opts *sql.TxOptions) (*sql.Tx, error)
	// Placeholder returns the placeholder format for the driver.
	Placeholder(n int) string
	// Dialect returns the dialect name (mysql, postgres, sqlite, sqlserver, turso).
	Dialect() string
}

Driver defines the interface for database driver implementations.

type JSONDB added in v0.37.0

type JSONDB struct {
	*SQLite
}

JSONDB implements the Driver interface for JSON-directory-backed storage. It embeds *SQLite for all standard Driver methods and overrides Open to scan a directory of JSON, JSONL, or NDJSON files and populate an in-memory SQLite database.

The directory path is passed as the DSN (from ConnectionConfig.Database). Each .json, .jsonl, or .ndjson file becomes a table named after the filename (without the extension). Object keys define the columns.

The driver is stateless — all state lives in the in-memory SQLite database.

func NewJSONDB added in v0.37.0

func NewJSONDB() *JSONDB

NewJSONDB creates a new JSONDB driver.

func (*JSONDB) Dialect added in v0.37.0

func (j *JSONDB) Dialect() string

Dialect returns "sqlite" so the query builder generates SQLite-compatible SQL and uses SQLite placeholders.

func (*JSONDB) Open added in v0.37.0

func (j *JSONDB) Open(dirPath string) (*sql.DB, error)

Open opens an in-memory SQLite database, scans the directory at dirPath for .json, .jsonl, and .ndjson files, and populates one table per file. The dirPath is the DSN, which comes from ConnectionConfig.Database via BuildDSN.

If dirPath is empty or ":memory:", no directory is scanned and an empty in-memory SQLite database is returned (useful for testing).

type MySQL

type MySQL struct{}

MySQL implements the Driver interface for MySQL databases.

func NewMySQL

func NewMySQL() *MySQL

NewMySQL creates a new MySQL driver.

func (*MySQL) BeginTx

func (m *MySQL) BeginTx(ctx context.Context, db *sql.DB, opts *sql.TxOptions) (*sql.Tx, error)

BeginTx starts a MySQL transaction with the given options.

func (*MySQL) Close

func (m *MySQL) Close(db *sql.DB) error

Close closes the MySQL database connection.

func (*MySQL) Dialect

func (m *MySQL) Dialect() string

Dialect returns the dialect name.

func (*MySQL) Open

func (m *MySQL) Open(dsn string) (*sql.DB, error)

Open opens a connection to the MySQL database.

func (*MySQL) Ping

func (m *MySQL) Ping(ctx context.Context, db *sql.DB) error

Ping checks if the MySQL database connection is alive.

func (*MySQL) Placeholder

func (m *MySQL) Placeholder(n int) string

Placeholder returns MySQL-style placeholders (?).

type Oracle added in v0.7.0

type Oracle struct{}

Oracle implements the Driver interface for Oracle databases.

func NewOracle added in v0.7.0

func NewOracle() *Oracle

NewOracle creates a new Oracle driver.

func (*Oracle) BeginTx added in v0.7.0

func (o *Oracle) BeginTx(ctx context.Context, db *sql.DB, opts *sql.TxOptions) (*sql.Tx, error)

BeginTx starts an Oracle transaction with the given options.

func (*Oracle) Close added in v0.7.0

func (o *Oracle) Close(db *sql.DB) error

Close closes the Oracle database connection.

func (*Oracle) Dialect added in v0.7.0

func (o *Oracle) Dialect() string

Dialect returns the dialect name.

func (*Oracle) Open added in v0.7.0

func (o *Oracle) Open(dsn string) (*sql.DB, error)

Open opens a connection to the Oracle database.

func (*Oracle) Ping added in v0.7.0

func (o *Oracle) Ping(ctx context.Context, db *sql.DB) error

Ping checks if the Oracle database connection is alive.

func (*Oracle) Placeholder added in v0.7.0

func (o *Oracle) Placeholder(n int) string

Placeholder returns Oracle-style placeholders (:1, :2, :3).

type PlaceholderFunc

type PlaceholderFunc func(n int) string

PlaceholderFunc is a function that generates placeholders for a given index.

func GetPlaceholderFunc

func GetPlaceholderFunc(dialect string) PlaceholderFunc

GetPlaceholderFunc returns the placeholder function for the given dialect.

type PostgreSQL

type PostgreSQL struct{}

PostgreSQL implements the Driver interface for PostgreSQL databases.

func NewPostgreSQL

func NewPostgreSQL() *PostgreSQL

NewPostgreSQL creates a new PostgreSQL driver.

func (*PostgreSQL) BeginTx

func (p *PostgreSQL) BeginTx(ctx context.Context, db *sql.DB, opts *sql.TxOptions) (*sql.Tx, error)

BeginTx starts a PostgreSQL transaction with the given options.

func (*PostgreSQL) Close

func (p *PostgreSQL) Close(db *sql.DB) error

Close closes the PostgreSQL database connection.

func (*PostgreSQL) Dialect

func (p *PostgreSQL) Dialect() string

Dialect returns the dialect name.

func (*PostgreSQL) Open

func (p *PostgreSQL) Open(dsn string) (*sql.DB, error)

Open opens a connection to the PostgreSQL database.

func (*PostgreSQL) Ping

func (p *PostgreSQL) Ping(ctx context.Context, db *sql.DB) error

Ping checks if the PostgreSQL database connection is alive.

func (*PostgreSQL) Placeholder

func (p *PostgreSQL) Placeholder(n int) string

Placeholder returns PostgreSQL-style placeholders ($1, $2, $3).

type SQLServer

type SQLServer struct{}

SQLServer implements the Driver interface for SQL Server databases.

func NewSQLServer

func NewSQLServer() *SQLServer

NewSQLServer creates a new SQL Server driver.

func (*SQLServer) BeginTx

func (s *SQLServer) BeginTx(ctx context.Context, db *sql.DB, opts *sql.TxOptions) (*sql.Tx, error)

BeginTx starts a SQL Server transaction with the given options.

func (*SQLServer) Close

func (s *SQLServer) Close(db *sql.DB) error

Close closes the SQL Server database connection.

func (*SQLServer) Dialect

func (s *SQLServer) Dialect() string

Dialect returns the dialect name.

func (*SQLServer) Open

func (s *SQLServer) Open(dsn string) (*sql.DB, error)

Open opens a connection to the SQL Server database.

func (*SQLServer) Ping

func (s *SQLServer) Ping(ctx context.Context, db *sql.DB) error

Ping checks if the SQL Server database connection is alive.

func (*SQLServer) Placeholder

func (s *SQLServer) Placeholder(n int) string

Placeholder returns SQL Server-style placeholders (@p1, @p2, @p3).

type SQLite

type SQLite struct{}

SQLite implements the Driver interface for SQLite databases.

func NewSQLite

func NewSQLite() *SQLite

NewSQLite creates a new SQLite driver.

func (*SQLite) BeginTx

func (s *SQLite) BeginTx(ctx context.Context, db *sql.DB, opts *sql.TxOptions) (*sql.Tx, error)

BeginTx starts a SQLite transaction with the given options.

func (*SQLite) Close

func (s *SQLite) Close(db *sql.DB) error

Close closes the SQLite database connection.

func (*SQLite) Dialect

func (s *SQLite) Dialect() string

Dialect returns the dialect name.

func (*SQLite) Open

func (s *SQLite) Open(dsn string) (*sql.DB, error)

Open opens a connection to the SQLite database.

func (*SQLite) Ping

func (s *SQLite) Ping(ctx context.Context, db *sql.DB) error

Ping checks if the SQLite database connection is alive.

func (*SQLite) Placeholder

func (s *SQLite) Placeholder(n int) string

Placeholder returns SQLite-style placeholders (?).

type Turso

type Turso struct{}

Turso implements the Driver interface for Turso databases.

func NewTurso

func NewTurso() *Turso

NewTurso creates a new Turso driver.

func (*Turso) BeginTx

func (t *Turso) BeginTx(ctx context.Context, db *sql.DB, opts *sql.TxOptions) (*sql.Tx, error)

BeginTx starts a Turso transaction with the given options.

func (*Turso) Close

func (t *Turso) Close(db *sql.DB) error

Close closes the Turso database connection.

func (*Turso) Dialect

func (t *Turso) Dialect() string

Dialect returns the dialect name.

func (*Turso) Open

func (t *Turso) Open(dsn string) (*sql.DB, error)

Open opens a connection to the Turso database.

func (*Turso) Ping

func (t *Turso) Ping(ctx context.Context, db *sql.DB) error

Ping checks if the Turso database connection is alive.

func (*Turso) Placeholder

func (t *Turso) Placeholder(n int) string

Placeholder returns SQLite-style placeholders (?) since Turso uses SQLite.

type XMLDB added in v0.37.0

type XMLDB struct {
	*SQLite
}

XMLDB implements the Driver interface for XML-directory-backed storage. It embeds *SQLite for all standard Driver methods and overrides Open to scan a directory of XML files and populate an in-memory SQLite database.

The directory path is passed as the DSN (from ConnectionConfig.Database). Each .xml file in the directory becomes a table named after the filename (without the .xml extension). The root element is the container; each direct child element is a row. Attributes and leaf sub-elements define the columns.

The driver is stateless — all state lives in the in-memory SQLite database.

func NewXMLDB added in v0.37.0

func NewXMLDB() *XMLDB

NewXMLDB creates a new XMLDB driver.

func (*XMLDB) Dialect added in v0.37.0

func (x *XMLDB) Dialect() string

Dialect returns "sqlite" so the query builder generates SQLite-compatible SQL and uses SQLite placeholders.

func (*XMLDB) Open added in v0.37.0

func (x *XMLDB) Open(dirPath string) (*sql.DB, error)

Open opens an in-memory SQLite database, scans the directory at dirPath for .xml files, and populates one table per file. The dirPath is the DSN, which comes from ConnectionConfig.Database via BuildDSN.

If dirPath is empty or ":memory:", no directory is scanned and an empty in-memory SQLite database is returned (useful for testing).

Jump to

Keyboard shortcuts

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