adapter

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2026 License: GPL-3.0 Imports: 5 Imported by: 0

Documentation

Overview

Package adapter provides database adapter implementations for different SQL dialects.

Index

Constants

This section is empty.

Variables

View Source
var ErrMySQLNotAvailable = errors.New("MySQL adapter not available: build with mysql tag")

ErrMySQLNotAvailable is returned when MySQL driver is not compiled in.

View Source
var ErrPostgresNotAvailable = errors.New("PostgreSQL adapter not available: build with postgres tag")

ErrPostgresNotAvailable is returned when PostgreSQL driver is not compiled in.

View Source
var ErrSQLiteNotAvailable = errors.New("SQLite adapter not available: build with CGO and sqlite tag")

ErrSQLiteNotAvailable is returned when SQLite is not compiled in.

Functions

func ScanRow

func ScanRow(rows *sql.Rows) (map[string]interface{}, error)

ScanRow is a helper to scan a single row into a map.

func ScanRows

func ScanRows(rows *sql.Rows) ([]map[string]interface{}, error)

ScanRows is a helper to scan all rows into a slice of maps.

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

func (a *BaseAdapter) BeginTx(ctx context.Context, opts *sql.TxOptions) (Tx, error)

BeginTx starts a transaction with the given 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.

func (*BaseAdapter) Query

func (a *BaseAdapter) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

Query executes a query that returns rows.

func (*BaseAdapter) QueryRow

func (a *BaseAdapter) QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row

QueryRow executes a query that returns a single row.

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

func (a *MySQLAdapter) GetTableSize(ctx context.Context, table string) (int64, error)

GetTableSize returns an error as MySQL is not available.

func (*MySQLAdapter) GetVariables

func (a *MySQLAdapter) GetVariables(ctx context.Context, like string) (map[string]string, error)

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

func (a *MySQLAdapter) LastInsertID(ctx context.Context, table, idColumn string) (int64, error)

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

func (a *MySQLAdapter) QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row

QueryRow 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

func (a *MySQLAdapter) TableExists(ctx context.Context, table string) (bool, error)

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

func (a *PostgresAdapter) GetTableSize(ctx context.Context, table string) (int64, error)

GetTableSize returns an error as PostgreSQL is not available.

func (*PostgresAdapter) LastInsertID

func (a *PostgresAdapter) LastInsertID(ctx context.Context, table, idColumn string) (int64, error)

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

func (a *PostgresAdapter) QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row

QueryRow stub

func (*PostgresAdapter) TableExists

func (a *PostgresAdapter) TableExists(ctx context.Context, table string) (bool, error)

TableExists returns an error as PostgreSQL is not available.

func (*PostgresAdapter) Vacuum

func (a *PostgresAdapter) Vacuum(ctx context.Context, table string) error

Vacuum 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

func (a *SQLiteAdapter) LastInsertID(ctx context.Context, table, idColumn string) (int64, error)

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

func (a *SQLiteAdapter) QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row

QueryRow stub

func (*SQLiteAdapter) TableExists

func (a *SQLiteAdapter) TableExists(ctx context.Context, table string) (bool, error)

TableExists returns an error as SQLite is not available.

func (*SQLiteAdapter) Vacuum

func (a *SQLiteAdapter) Vacuum(ctx context.Context) error

Vacuum 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.

Jump to

Keyboard shortcuts

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