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 ¶
- func ParseTimeISO8601(src interface{}) (time.Time, error)
- func PlaceholderDollar(n int) string
- func PlaceholderQuestion(n int) string
- func QuoteBackticks(name string) string
- func QuoteDoubleQuotes(name string) string
- func QuoteIdentifier(name string, quoteChar QuoteChar) string
- type Config
- type Driver
- func (d *Driver) Close() error
- func (d *Driver) Exec(ctx context.Context, fn func(*sql.Tx) error) error
- func (d *Driver) GetApplied(ctx context.Context) ([]queen.Applied, error)
- func (d *Driver) Record(ctx context.Context, m *queen.Migration) error
- func (d *Driver) Remove(ctx context.Context, version string) error
- type QuoteChar
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ParseTimeISO8601 ¶
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 ¶
PlaceholderDollar creates placeholders in the format $1, $2, $3... Used by PostgreSQL and CockroachDB.
func PlaceholderQuestion ¶
PlaceholderQuestion creates placeholders in the format ?, ?, ?... Used by MySQL, SQLite, and ClickHouse.
func QuoteBackticks ¶
QuoteBackticks is a convenience wrapper for QuoteIdentifier with Backtick.
Used by MySQL.
Examples:
QuoteBackticks("users") -> `users`
QuoteBackticks("my`table") -> `my``table`
func QuoteDoubleQuotes ¶
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 ¶
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 ¶
Close closes the database connection.
Identical implementation for all database drivers.
func (*Driver) Exec ¶
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 ¶
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.