database

package
v1.28.0 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// SchemaKey is the context key for database schema
	SchemaKey contextKey = "db_schema"
)

Variables

This section is empty.

Functions

func BuildQualifiedTableName

func BuildQualifiedTableName(schema, tableName string) (string, error)

BuildQualifiedTableName builds a safe schema.table_name string Validates both schema and table name, then properly quotes them

func GetSchema

func GetSchema(ctx context.Context) string

GetSchema extracts the schema from context Returns empty string if not found (will use default schema)

func IsAllowedSortColumn

func IsAllowedSortColumn(column string, allowedColumns []string) bool

IsAllowedSortColumn checks if a column name is in the allowed whitelist This prevents ORDER BY injection attacks

func MustGetSchema

func MustGetSchema(ctx context.Context) string

MustGetSchema extracts the schema from context Panics if not found - use in handlers after auth middleware

func SanitizeIdentifier

func SanitizeIdentifier(identifier string) string

SanitizeIdentifier safely quotes PostgreSQL identifiers to prevent SQL injection This function properly escapes quotes within the identifier

func ValidateOrderDirection

func ValidateOrderDirection(direction string) error

ValidateOrderDirection validates SQL ORDER BY direction Only allows ASC and DESC (case-insensitive)

func ValidateSchemaName

func ValidateSchemaName(schema string) error

ValidateSchemaName validates schema name format according to PostgreSQL identifier rules Returns error if schema name is invalid or contains potentially dangerous characters

func ValidateTableName

func ValidateTableName(tableName string) error

ValidateTableName validates table name format Uses same rules as schema names for consistency

func WithSchema

func WithSchema(ctx context.Context, schema string) context.Context

WithSchema returns a new context with the schema value

Types

type Config

type Config struct {
	Host            string
	Port            string
	Username        string
	Password        string
	Database        string
	SSLMode         string
	MaxIdleConns    int
	MaxOpenConns    int
	ConnMaxLifetime int // in seconds
	LogMode         bool
}

Config holds database configuration

func DefaultConfig

func DefaultConfig() *Config

DefaultConfig returns default database configuration

type Database

type Database interface {
	// DB returns the underlying *gorm.DB instance
	DB() *gorm.DB

	// Close closes the database connection
	Close() error

	// Ping checks if the database is reachable
	Ping(ctx context.Context) error

	// AutoMigrate runs auto migration for given models
	AutoMigrate(dst ...interface{}) error

	// Transaction executes a function within a transaction
	Transaction(ctx context.Context, fn func(*gorm.DB) error) error
}

Database defines the interface for database operations This abstraction allows for easy mocking and testing

func NewMock

func NewMock() (Database, error)

NewMock creates a new mock database using SQLite in-memory (CGO-free) Uses modernc.org/sqlite driver (Pure Go implementation)

func NewMockWithDB

func NewMockWithDB(db *gorm.DB) Database

NewMockWithDB creates a mock database with a custom *gorm.DB

type MockDB

type MockDB struct {
	// contains filtered or unexported fields
}

MockDB is a mock database implementation for testing Uses SQLite (Pure Go, CGO-free) for in-memory testing

func (*MockDB) AutoMigrate

func (m *MockDB) AutoMigrate(dst ...interface{}) error

AutoMigrate runs auto migration for given models

func (*MockDB) Close

func (m *MockDB) Close() error

Close closes the database connection

func (*MockDB) DB

func (m *MockDB) DB() *gorm.DB

DB returns the underlying *gorm.DB instance

func (*MockDB) Ping

func (m *MockDB) Ping(ctx context.Context) error

Ping checks if the database is reachable

func (*MockDB) SetCloseFunc

func (m *MockDB) SetCloseFunc(fn func() error)

SetCloseFunc sets a custom close function for testing

func (*MockDB) SetPingFunc

func (m *MockDB) SetPingFunc(fn func(ctx context.Context) error)

SetPingFunc sets a custom ping function for testing

func (*MockDB) Transaction

func (m *MockDB) Transaction(ctx context.Context, fn func(*gorm.DB) error) error

Transaction executes a function within a transaction

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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