Documentation
¶
Index ¶
- Constants
- func BuildQualifiedTableName(schema, tableName string) (string, error)
- func GetSchema(ctx context.Context) string
- func IsAllowedSortColumn(column string, allowedColumns []string) bool
- func MustGetSchema(ctx context.Context) string
- func SanitizeIdentifier(identifier string) string
- func ValidateOrderDirection(direction string) error
- func ValidateSchemaName(schema string) error
- func ValidateTableName(tableName string) error
- func WithSchema(ctx context.Context, schema string) context.Context
- type Config
- type Database
- type MockDB
- func (m *MockDB) AutoMigrate(dst ...interface{}) error
- func (m *MockDB) Close() error
- func (m *MockDB) DB() *gorm.DB
- func (m *MockDB) Ping(ctx context.Context) error
- func (m *MockDB) SetCloseFunc(fn func() error)
- func (m *MockDB) SetPingFunc(fn func(ctx context.Context) error)
- func (m *MockDB) Transaction(ctx context.Context, fn func(*gorm.DB) error) error
Constants ¶
const (
// SchemaKey is the context key for database schema
SchemaKey contextKey = "db_schema"
)
Variables ¶
This section is empty.
Functions ¶
func BuildQualifiedTableName ¶
BuildQualifiedTableName builds a safe schema.table_name string Validates both schema and table name, then properly quotes them
func GetSchema ¶
GetSchema extracts the schema from context Returns empty string if not found (will use default schema)
func IsAllowedSortColumn ¶
IsAllowedSortColumn checks if a column name is in the allowed whitelist This prevents ORDER BY injection attacks
func MustGetSchema ¶
MustGetSchema extracts the schema from context Panics if not found - use in handlers after auth middleware
func SanitizeIdentifier ¶
SanitizeIdentifier safely quotes PostgreSQL identifiers to prevent SQL injection This function properly escapes quotes within the identifier
func ValidateOrderDirection ¶
ValidateOrderDirection validates SQL ORDER BY direction Only allows ASC and DESC (case-insensitive)
func ValidateSchemaName ¶
ValidateSchemaName validates schema name format according to PostgreSQL identifier rules Returns error if schema name is invalid or contains potentially dangerous characters
func ValidateTableName ¶
ValidateTableName validates table name format Uses same rules as schema names for consistency
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 ¶
NewMock creates a new mock database using SQLite in-memory (CGO-free) Uses modernc.org/sqlite driver (Pure Go implementation)
func NewMockWithDB ¶
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 ¶
AutoMigrate runs auto migration for given models
func (*MockDB) SetCloseFunc ¶
SetCloseFunc sets a custom close function for testing
func (*MockDB) SetPingFunc ¶
SetPingFunc sets a custom ping function for testing