Documentation
¶
Index ¶
- Variables
- type Config
- type Connection
- func (c *Connection) BeginTx(ctx context.Context) (*sql.Tx, error)
- func (c *Connection) Close() error
- func (c *Connection) DB() *sql.DB
- func (c *Connection) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (c *Connection) Ping(ctx context.Context) error
- func (c *Connection) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
- func (c *Connection) QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row
- func (c *Connection) Stats() sql.DBStats
- type Database
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // ErrNotFound is returned when a record is not found ErrNotFound = errors.New("record not found") // ErrDuplicateKey is returned when trying to insert a duplicate key ErrDuplicateKey = errors.New("duplicate key") // ErrInvalidConfig is returned when database configuration is invalid ErrInvalidConfig = errors.New("invalid database configuration") // ErrConnectionFailed is returned when database connection fails ErrConnectionFailed = errors.New("database connection failed") // ErrTransactionFailed is returned when transaction fails ErrTransactionFailed = errors.New("transaction failed") // ErrMigrationFailed is returned when migration fails ErrMigrationFailed = errors.New("migration failed") )
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// Driver name: sqlite3, postgres, mysql
Driver string
// Connection string or DSN
DSN string
// Maximum number of open connections
MaxOpenConns int
// Maximum number of idle connections
MaxIdleConns int
// Maximum lifetime of a connection
ConnMaxLifetime time.Duration
// Maximum idle time of a connection
ConnMaxIdleTime time.Duration
}
Config represents common database configuration
func DefaultConfig ¶
DefaultConfig returns a Config with sensible defaults
type Connection ¶
type Connection struct {
// contains filtered or unexported fields
}
Connection implements the Database interface wrapping *sql.DB
func (*Connection) Exec ¶
func (c *Connection) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
Exec executes a query without returning rows
func (*Connection) Ping ¶
func (c *Connection) Ping(ctx context.Context) error
Ping checks if the database connection is alive
func (*Connection) Query ¶
func (c *Connection) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
Query executes a query that returns rows
func (*Connection) Stats ¶
func (c *Connection) Stats() sql.DBStats
Stats returns database statistics
type Database ¶
type Database interface {
// DB returns the underlying *sql.DB for direct access when needed
DB() *sql.DB
// Ping checks if the database connection is alive
Ping(ctx context.Context) error
// Close closes the database connection
Close() error
// BeginTx starts a new transaction
BeginTx(ctx context.Context) (*sql.Tx, error)
// Exec executes a query without returning rows
Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
// Query executes a query that returns rows
Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
// QueryRow executes a query that returns at most one row
QueryRow(ctx context.Context, query string, args ...interface{}) *sql.Row
// Stats returns database statistics
Stats() sql.DBStats
}
Database represents a generic database connection interface
Click to show internal directories.
Click to hide internal directories.