Documentation
¶
Overview ¶
Package adapter provides database adapter implementations for different SQL dialects.
Index ¶
- Variables
- func ScanRow(rows *sql.Rows) (map[string]interface{}, error)
- func ScanRows(rows *sql.Rows) ([]map[string]interface{}, error)
- type Adapter
- type BaseAdapter
- func (a *BaseAdapter) Begin(ctx context.Context) (Tx, error)
- func (a *BaseAdapter) BeginTx(ctx context.Context, opts *sql.TxOptions) (Tx, error)
- func (a *BaseAdapter) Close() error
- func (a *BaseAdapter) DB() *sql.DB
- func (a *BaseAdapter) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (a *BaseAdapter) HealthCheck(ctx context.Context) error
- func (a *BaseAdapter) Ping(ctx context.Context) error
- func (a *BaseAdapter) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
- func (a *BaseAdapter) QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row
- type ColumnInfo
- type Config
- type MySQLAdapter
- func (a *MySQLAdapter) AnalyzeTable(ctx context.Context, table string) error
- func (a *MySQLAdapter) DialectName() string
- func (a *MySQLAdapter) DriverName() string
- func (a *MySQLAdapter) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (a *MySQLAdapter) GetDatabaseSize(ctx context.Context) (int64, error)
- func (a *MySQLAdapter) GetProcessList(ctx context.Context) ([]map[string]interface{}, error)
- func (a *MySQLAdapter) GetTableColumns(ctx context.Context, table string) ([]ColumnInfo, error)
- func (a *MySQLAdapter) GetTableSize(ctx context.Context, table string) (int64, error)
- func (a *MySQLAdapter) GetVariables(ctx context.Context, like string) (map[string]string, error)
- func (a *MySQLAdapter) GetVersion(ctx context.Context) (string, error)
- func (a *MySQLAdapter) KillProcess(ctx context.Context, processID int64) error
- func (a *MySQLAdapter) LastInsertID(ctx context.Context, table, idColumn string) (int64, error)
- func (a *MySQLAdapter) LoadDataInfile(ctx context.Context, table, filepath string, columns []string) error
- func (a *MySQLAdapter) Open(ctx context.Context) error
- func (a *MySQLAdapter) OptimizeTable(ctx context.Context, table string) error
- func (a *MySQLAdapter) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
- func (a *MySQLAdapter) QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row
- func (a *MySQLAdapter) SetSessionVariable(ctx context.Context, name, value string) error
- func (a *MySQLAdapter) TableExists(ctx context.Context, table string) (bool, error)
- type PostgresAdapter
- func (a *PostgresAdapter) CopyFrom(ctx context.Context, table string, columns []string, values [][]interface{}) (int64, error)
- func (a *PostgresAdapter) CreateSchema(ctx context.Context, schema string) error
- func (a *PostgresAdapter) DialectName() string
- func (a *PostgresAdapter) DriverName() string
- func (a *PostgresAdapter) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (a *PostgresAdapter) GetActiveConnections(ctx context.Context) (int, error)
- func (a *PostgresAdapter) GetDatabaseSize(ctx context.Context) (int64, error)
- func (a *PostgresAdapter) GetTableColumns(ctx context.Context, table string) ([]ColumnInfo, error)
- func (a *PostgresAdapter) GetTableSize(ctx context.Context, table string) (int64, error)
- func (a *PostgresAdapter) LastInsertID(ctx context.Context, table, idColumn string) (int64, error)
- func (a *PostgresAdapter) Listen(ctx context.Context, channel string) error
- func (a *PostgresAdapter) Notify(ctx context.Context, channel, payload string) error
- func (a *PostgresAdapter) Open(ctx context.Context) error
- func (a *PostgresAdapter) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
- func (a *PostgresAdapter) QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row
- func (a *PostgresAdapter) TableExists(ctx context.Context, table string) (bool, error)
- func (a *PostgresAdapter) Vacuum(ctx context.Context, table string) error
- type SQLiteAdapter
- func (a *SQLiteAdapter) Analyze(ctx context.Context) error
- func (a *SQLiteAdapter) CreateTable(ctx context.Context, ddl string) error
- func (a *SQLiteAdapter) DialectName() string
- func (a *SQLiteAdapter) DriverName() string
- func (a *SQLiteAdapter) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (a *SQLiteAdapter) GetTableColumns(ctx context.Context, table string) ([]ColumnInfo, error)
- func (a *SQLiteAdapter) LastInsertID(ctx context.Context, table, idColumn string) (int64, error)
- func (a *SQLiteAdapter) Open(ctx context.Context) error
- func (a *SQLiteAdapter) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
- func (a *SQLiteAdapter) QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row
- func (a *SQLiteAdapter) TableExists(ctx context.Context, table string) (bool, error)
- func (a *SQLiteAdapter) Vacuum(ctx context.Context) error
- type Tx
Constants ¶
This section is empty.
Variables ¶
var ErrMySQLNotAvailable = errors.New("MySQL adapter not available: build with mysql tag")
ErrMySQLNotAvailable is returned when MySQL driver is not compiled in.
var ErrPostgresNotAvailable = errors.New("PostgreSQL adapter not available: build with postgres tag")
ErrPostgresNotAvailable is returned when PostgreSQL driver is not compiled in.
var ErrSQLiteNotAvailable = errors.New("SQLite adapter not available: build with CGO and sqlite tag")
ErrSQLiteNotAvailable is returned when SQLite is not compiled in.
Functions ¶
Types ¶
type Adapter ¶
type Adapter interface {
// Connection management
Open(ctx context.Context) error
Close() error
Ping(ctx context.Context) error
// Query execution
Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row
Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
// Transaction support
Begin(ctx context.Context) (Tx, error)
BeginTx(ctx context.Context, opts *sql.TxOptions) (Tx, error)
// Metadata
DialectName() string
DriverName() string
// Database-specific operations
LastInsertID(ctx context.Context, table, idColumn string) (int64, error)
// Health check
HealthCheck(ctx context.Context) error
}
Adapter defines the interface for database operations.
type BaseAdapter ¶
type BaseAdapter struct {
// contains filtered or unexported fields
}
BaseAdapter provides common functionality for all adapters.
func (*BaseAdapter) Begin ¶
func (a *BaseAdapter) Begin(ctx context.Context) (Tx, error)
Begin starts a transaction with default options.
func (*BaseAdapter) Close ¶
func (a *BaseAdapter) Close() error
Close closes the database connection.
func (*BaseAdapter) DB ¶
func (a *BaseAdapter) DB() *sql.DB
DB returns the underlying database connection.
func (*BaseAdapter) Exec ¶
func (a *BaseAdapter) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
Exec executes a query that doesn't return rows.
func (*BaseAdapter) HealthCheck ¶
func (a *BaseAdapter) HealthCheck(ctx context.Context) error
HealthCheck performs a basic health check.
func (*BaseAdapter) Ping ¶
func (a *BaseAdapter) Ping(ctx context.Context) error
Ping verifies the database connection.
type ColumnInfo ¶
type ColumnInfo struct {
Name string
DataType string
IsNullable bool
IsPrimaryKey bool
DefaultValue string
}
ColumnInfo holds metadata about a database column.
type Config ¶
type Config struct {
Host string
Port int
Database string
Username string
Password string
SSLMode string
MaxOpenConns int
MaxIdleConns int
ConnMaxLifetime time.Duration
ConnMaxIdleTime time.Duration
// SQLite specific
FilePath string
InMemory bool
// Additional options as key-value pairs
Options map[string]string
}
Config holds common adapter configuration.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a config with sensible defaults.
type MySQLAdapter ¶
type MySQLAdapter struct {
BaseAdapter
}
MySQLAdapter implements Adapter for MySQL databases. This is a stub implementation when the mysql build tag is not set.
func NewMySQLAdapter ¶
func NewMySQLAdapter(config Config) *MySQLAdapter
NewMySQLAdapter creates a new MySQL adapter.
func (*MySQLAdapter) AnalyzeTable ¶
func (a *MySQLAdapter) AnalyzeTable(ctx context.Context, table string) error
AnalyzeTable returns an error as MySQL is not available.
func (*MySQLAdapter) DialectName ¶
func (a *MySQLAdapter) DialectName() string
DialectName returns the dialect name.
func (*MySQLAdapter) DriverName ¶
func (a *MySQLAdapter) DriverName() string
DriverName returns the driver name.
func (*MySQLAdapter) Exec ¶
func (a *MySQLAdapter) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
Exec stub
func (*MySQLAdapter) GetDatabaseSize ¶
func (a *MySQLAdapter) GetDatabaseSize(ctx context.Context) (int64, error)
GetDatabaseSize returns an error as MySQL is not available.
func (*MySQLAdapter) GetProcessList ¶
func (a *MySQLAdapter) GetProcessList(ctx context.Context) ([]map[string]interface{}, error)
GetProcessList returns an error as MySQL is not available.
func (*MySQLAdapter) GetTableColumns ¶
func (a *MySQLAdapter) GetTableColumns(ctx context.Context, table string) ([]ColumnInfo, error)
GetTableColumns returns an error as MySQL is not available.
func (*MySQLAdapter) GetTableSize ¶
GetTableSize returns an error as MySQL is not available.
func (*MySQLAdapter) GetVariables ¶
GetVariables returns an error as MySQL is not available.
func (*MySQLAdapter) GetVersion ¶
func (a *MySQLAdapter) GetVersion(ctx context.Context) (string, error)
GetVersion returns an error as MySQL is not available.
func (*MySQLAdapter) KillProcess ¶
func (a *MySQLAdapter) KillProcess(ctx context.Context, processID int64) error
KillProcess returns an error as MySQL is not available.
func (*MySQLAdapter) LastInsertID ¶
LastInsertID returns an error as MySQL is not available.
func (*MySQLAdapter) LoadDataInfile ¶
func (a *MySQLAdapter) LoadDataInfile(ctx context.Context, table, filepath string, columns []string) error
LoadDataInfile returns an error as MySQL is not available.
func (*MySQLAdapter) Open ¶
func (a *MySQLAdapter) Open(ctx context.Context) error
Open returns an error as MySQL is not available.
func (*MySQLAdapter) OptimizeTable ¶
func (a *MySQLAdapter) OptimizeTable(ctx context.Context, table string) error
OptimizeTable returns an error as MySQL is not available.
func (*MySQLAdapter) Query ¶
func (a *MySQLAdapter) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
Query stub
func (*MySQLAdapter) SetSessionVariable ¶
func (a *MySQLAdapter) SetSessionVariable(ctx context.Context, name, value string) error
SetSessionVariable returns an error as MySQL is not available.
func (*MySQLAdapter) TableExists ¶
TableExists returns an error as MySQL is not available.
type PostgresAdapter ¶
type PostgresAdapter struct {
BaseAdapter
}
PostgresAdapter implements Adapter for PostgreSQL databases. This is a stub implementation when the postgres build tag is not set.
func NewPostgresAdapter ¶
func NewPostgresAdapter(config Config) *PostgresAdapter
NewPostgresAdapter creates a new PostgreSQL adapter.
func (*PostgresAdapter) CopyFrom ¶
func (a *PostgresAdapter) CopyFrom(ctx context.Context, table string, columns []string, values [][]interface{}) (int64, error)
CopyFrom returns an error as PostgreSQL is not available.
func (*PostgresAdapter) CreateSchema ¶
func (a *PostgresAdapter) CreateSchema(ctx context.Context, schema string) error
CreateSchema returns an error as PostgreSQL is not available.
func (*PostgresAdapter) DialectName ¶
func (a *PostgresAdapter) DialectName() string
DialectName returns the dialect name.
func (*PostgresAdapter) DriverName ¶
func (a *PostgresAdapter) DriverName() string
DriverName returns the driver name.
func (*PostgresAdapter) Exec ¶
func (a *PostgresAdapter) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
Exec stub
func (*PostgresAdapter) GetActiveConnections ¶
func (a *PostgresAdapter) GetActiveConnections(ctx context.Context) (int, error)
GetActiveConnections returns an error as PostgreSQL is not available.
func (*PostgresAdapter) GetDatabaseSize ¶
func (a *PostgresAdapter) GetDatabaseSize(ctx context.Context) (int64, error)
GetDatabaseSize returns an error as PostgreSQL is not available.
func (*PostgresAdapter) GetTableColumns ¶
func (a *PostgresAdapter) GetTableColumns(ctx context.Context, table string) ([]ColumnInfo, error)
GetTableColumns returns an error as PostgreSQL is not available.
func (*PostgresAdapter) GetTableSize ¶
GetTableSize returns an error as PostgreSQL is not available.
func (*PostgresAdapter) LastInsertID ¶
LastInsertID returns an error as PostgreSQL is not available.
func (*PostgresAdapter) Listen ¶
func (a *PostgresAdapter) Listen(ctx context.Context, channel string) error
Listen returns an error as PostgreSQL is not available.
func (*PostgresAdapter) Notify ¶
func (a *PostgresAdapter) Notify(ctx context.Context, channel, payload string) error
Notify returns an error as PostgreSQL is not available.
func (*PostgresAdapter) Open ¶
func (a *PostgresAdapter) Open(ctx context.Context) error
Open returns an error as PostgreSQL is not available.
func (*PostgresAdapter) Query ¶
func (a *PostgresAdapter) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
Query stub
func (*PostgresAdapter) TableExists ¶
TableExists returns an error as PostgreSQL is not available.
type SQLiteAdapter ¶
type SQLiteAdapter struct {
BaseAdapter
}
SQLiteAdapter implements Adapter for SQLite databases. This is a stub implementation when CGO is not available.
func NewSQLiteAdapter ¶
func NewSQLiteAdapter(config Config) *SQLiteAdapter
NewSQLiteAdapter creates a new SQLite adapter.
func NewSQLiteFile ¶
func NewSQLiteFile(path string) *SQLiteAdapter
NewSQLiteFile creates a SQLite adapter for a file database.
func NewSQLiteMemory ¶
func NewSQLiteMemory() *SQLiteAdapter
NewSQLiteMemory creates an in-memory SQLite adapter for testing.
func (*SQLiteAdapter) Analyze ¶
func (a *SQLiteAdapter) Analyze(ctx context.Context) error
Analyze returns an error as SQLite is not available.
func (*SQLiteAdapter) CreateTable ¶
func (a *SQLiteAdapter) CreateTable(ctx context.Context, ddl string) error
CreateTable returns an error as SQLite is not available.
func (*SQLiteAdapter) DialectName ¶
func (a *SQLiteAdapter) DialectName() string
DialectName returns the dialect name.
func (*SQLiteAdapter) DriverName ¶
func (a *SQLiteAdapter) DriverName() string
DriverName returns the driver name.
func (*SQLiteAdapter) Exec ¶
func (a *SQLiteAdapter) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
Exec stub
func (*SQLiteAdapter) GetTableColumns ¶
func (a *SQLiteAdapter) GetTableColumns(ctx context.Context, table string) ([]ColumnInfo, error)
GetTableColumns returns an error as SQLite is not available.
func (*SQLiteAdapter) LastInsertID ¶
LastInsertID returns an error as SQLite is not available.
func (*SQLiteAdapter) Open ¶
func (a *SQLiteAdapter) Open(ctx context.Context) error
Open returns an error as SQLite is not available.
func (*SQLiteAdapter) Query ¶
func (a *SQLiteAdapter) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
Query stub
func (*SQLiteAdapter) TableExists ¶
TableExists returns an error as SQLite is not available.
type Tx ¶
type Tx interface {
Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row
Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
Commit() error
Rollback() error
}
Tx represents a database transaction.