base

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package base provides common functionality for Queen database drivers.

This package contains shared code to reduce duplication across different database drivers (PostgreSQL, MySQL, SQLite, ClickHouse, CockroachDB).

The base package provides:

  • Transaction management (Exec)
  • Connection lifecycle (Close)
  • Common migration operations (GetApplied, Record, Remove)
  • SQL identifier quoting strategies
  • Placeholder formatting strategies

drivers/base/quote.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseTimeISO8601

func ParseTimeISO8601(src interface{}) (time.Time, error)

ParseTimeISO8601 parses time from ISO8601 string format. Used by SQLite which stores timestamps as TEXT.

SQLite default format: "YYYY-MM-DD HH:MM:SS"

func PlaceholderDollar

func PlaceholderDollar(n int) string

PlaceholderDollar creates placeholders in the format $1, $2, $3... Used by PostgreSQL and CockroachDB.

func PlaceholderQuestion

func PlaceholderQuestion(n int) string

PlaceholderQuestion creates placeholders in the format ?, ?, ?... Used by MySQL, SQLite, and ClickHouse.

func QuoteBackticks

func QuoteBackticks(name string) string

QuoteBackticks is a convenience wrapper for QuoteIdentifier with Backtick.

Used by MySQL.

Examples:

QuoteBackticks("users")      -> `users`
QuoteBackticks("my`table")   -> `my``table`

func QuoteDoubleQuotes

func QuoteDoubleQuotes(name string) string

QuoteDoubleQuotes is a convenience wrapper for QuoteIdentifier with DoubleQuote.

Used by PostgreSQL, SQLite, ClickHouse, and CockroachDB.

Examples:

QuoteDoubleQuotes("users")       -> "users"
QuoteDoubleQuotes(`my"table`)   -> "my""table"

func QuoteIdentifier

func QuoteIdentifier(name string, quoteChar QuoteChar) string

QuoteIdentifier escapes and wraps a SQL identifier using the provided quote character.

It prevents SQL injection by doubling any existing quote characters inside the identifier.

Supported quote characters:

  • DoubleQuote (") for PostgreSQL, SQLite, ClickHouse, CockroachDB
  • Backtick (`) for MySQL

Examples:

QuoteIdentifier("users", DoubleQuote)     -> "users"
QuoteIdentifier(`my"table`, DoubleQuote) -> "my""table"
QuoteIdentifier("users", Backtick)        -> `users`

Use this function whenever constructing SQL queries with dynamic table or column names.

Types

type Config

type Config struct {
	// Placeholder generates SQL placeholders for the n-th argument (1-based).
	// PostgreSQL/CockroachDB: func(n int) string { return fmt.Sprintf("$%d", n) }
	// MySQL/SQLite/ClickHouse: func(n int) string { return "?" }
	Placeholder func(n int) string

	// QuoteIdentifier escapes SQL identifiers (table names, column names).
	// PostgreSQL/SQLite/ClickHouse/CockroachDB: double quotes
	// MySQL: backticks
	QuoteIdentifier func(name string) string

	// ParseTime parses time from query result (optional).
	// Most drivers: nil (use standard scanning)
	// SQLite: parses from ISO8601 string
	ParseTime func(src interface{}) (time.Time, error)
}

Config contains configuration for the base driver. Each concrete driver provides these strategies to customize behavior.

type Driver

type Driver struct {
	DB        *sql.DB
	TableName string
	Config    Config
	// contains filtered or unexported fields
}

Driver provides base implementation of common queen.Driver methods.

Concrete drivers should embed this type and provide:

  • Init() - database-specific schema creation
  • Lock()/Unlock() - database-specific locking mechanisms

Note: This base driver does not manage database connections explicitly. Some databases (e.g. MySQL with GET_LOCK) require a dedicated connection for locking. In such cases, the concrete driver (mysql.Driver) is responsible for managing its own *sql.Conn for lock lifetime.

func (*Driver) Close

func (d *Driver) Close() error

Close closes the database connection.

Identical implementation for all database drivers.

func (*Driver) Exec

func (d *Driver) Exec(ctx context.Context, fn func(*sql.Tx) error) error

Exec executes a function within a transaction.

If the function returns an error, the transaction is rolled back. Otherwise, the transaction is committed.

This provides ACID guarantees for migration execution. Identical implementation for all database drivers.

func (*Driver) GetApplied

func (d *Driver) GetApplied(ctx context.Context) ([]queen.Applied, error)

GetApplied returns all applied migrations sorted by applied_at in ascending order.

Uses the QuoteIdentifier strategy for SQL identifier escaping. Uses the optional ParseTime strategy for SQLite compatibility.

func (*Driver) Record

func (d *Driver) Record(ctx context.Context, m *queen.Migration) error

Record marks a migration as applied in the database.

Uses Placeholder and QuoteIdentifier strategies to generate database-specific SQL queries.

func (*Driver) Remove

func (d *Driver) Remove(ctx context.Context, version string) error

Remove removes a migration record from the database (for rollback).

Uses Placeholder and QuoteIdentifier strategies to generate database-specific SQL queries.

type QuoteChar

type QuoteChar string

QuoteChar represents a SQL identifier quote character.

const (
	DoubleQuote QuoteChar = `"` // PostgreSQL, SQLite, ClickHouse, CockroachDB
	Backtick    QuoteChar = "`" // MySQL
)

Jump to

Keyboard shortcuts

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