database

package
v0.0.0-...-90ae8ee Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrEmptyDriver       = errors.New("database driver is empty")
	ErrEmptyDSN          = errors.New("database DSN is empty")
	ErrUnsupportedDriver = errors.New("unsupported database driver")
	ErrInvalidIdentifier = errors.New("invalid SQL identifier")
)

Functions

This section is empty.

Types

type BaseEngine

type BaseEngine[E any, K any] struct {
	// contains filtered or unexported fields
}

func NewBaseEngine

func NewBaseEngine[E any, K any](
	e *DatabaseEngine,
	spec EngineSpec[E, K],
) *BaseEngine[E, K]

func (*BaseEngine[E, K]) ConflictColumns

func (b *BaseEngine[E, K]) ConflictColumns() []string

func (*BaseEngine[E, K]) Count

func (b *BaseEngine[E, K]) Count() (int, error)

func (*BaseEngine[E, K]) Delete

func (b *BaseEngine[E, K]) Delete(key K) error

func (*BaseEngine[E, K]) DeleteAll

func (b *BaseEngine[E, K]) DeleteAll() error

func (*BaseEngine[E, K]) Find

func (b *BaseEngine[E, K]) Find(key K) (*E, error)

func (*BaseEngine[E, K]) FromRow

func (b *BaseEngine[E, K]) FromRow(*sql.Rows) (E, error)

func (*BaseEngine[E, K]) IntPrimaryKey

func (b *BaseEngine[E, K]) IntPrimaryKey() *string

func (*BaseEngine[E, K]) IsEmptyFast

func (b *BaseEngine[E, K]) IsEmptyFast() (bool, error)

func (*BaseEngine[E, K]) KeyArgs

func (b *BaseEngine[E, K]) KeyArgs(K) []any

func (*BaseEngine[E, K]) KeyColumns

func (b *BaseEngine[E, K]) KeyColumns() []string

func (*BaseEngine[E, K]) Load

func (b *BaseEngine[E, K]) Load(
	mode PaginationMode,
	limit int,
	offset int,
	lastID *int,
) ([]E, error)

func (*BaseEngine[E, K]) Save

func (b *BaseEngine[E, K]) Save(entity E, mode InserType) error

func (*BaseEngine[E, K]) SaveBatch

func (b *BaseEngine[E, K]) SaveBatch(
	entities []E,
	mode InserType,
) error

func (*BaseEngine[E, K]) Table

func (b *BaseEngine[E, K]) Table() string

⚠️ DI-OVERRIDE OLEH ENGINE SPESIFIK

func (*BaseEngine[E, K]) Upsert

func (b *BaseEngine[E, K]) Upsert(
	entity E,
	updateColumns []string,
) error

func (*BaseEngine[E, K]) UpsertBatch

func (b *BaseEngine[E, K]) UpsertBatch(
	entities []E,
	updateColumns []string,
) error

type Config

type Config struct {
	Driver Driver
	DSN    string

	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxLifetime time.Duration

	AutoMigrate bool
	Migrations  []string

	EnableMaintenance bool
}

type DBMaintenanceScheduler

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

func NewDBMaintenanceScheduler

func NewDBMaintenanceScheduler(e *DatabaseEngine) *DBMaintenanceScheduler

func (*DBMaintenanceScheduler) Start

func (s *DBMaintenanceScheduler) Start()

func (*DBMaintenanceScheduler) Stop

func (s *DBMaintenanceScheduler) Stop()

type DatabaseEngine

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

func Open

func Open(config Config) (*DatabaseEngine, error)

func Start

func Start(fileName string, sqlFiles []string, table []string) (*DatabaseEngine, error)

func StartAtPath

func StartAtPath(path string, sqlFiles []string, table []string) (*DatabaseEngine, error)

func (*DatabaseEngine) Close

func (e *DatabaseEngine) Close() error

func (*DatabaseEngine) DB

func (e *DatabaseEngine) DB() *sql.DB

func (*DatabaseEngine) Driver

func (e *DatabaseEngine) Driver() Driver

func (*DatabaseEngine) Migrate

func (e *DatabaseEngine) Migrate(ctx context.Context, migrations []string) error

func (*DatabaseEngine) Ping

func (e *DatabaseEngine) Ping(ctx context.Context) error

type Dialect

type Dialect interface {
	DriverName() string
	Placeholder(index int) string
	Placeholders(count int, startIndex int) string

	QuoteIdent(name string) string

	Insert(table string, cols []string) string
	InsertIgnore(table string, cols []string, conflictCols []string) string
	InsertReplace(table string, cols []string, conflictCols []string) string
	Upsert(table string, cols []string, conflictCols []string, updateCols []string) string

	Init(db *sql.DB) error
	SupportsMaintenance() bool
}

type Driver

type Driver string
const (
	DriverSQLite   Driver = "sqlite"
	DriverPostgres Driver = "postgres"
)

type EngineSpec

type EngineSpec[E any, K any] interface {
	Table() string

	// WRITE
	Columns() []string
	Values(E) []any
	ConflictColumns() []string

	// READ
	FromRow(*sql.Rows) (E, error)

	// KEY
	KeyColumns() []string
	KeyArgs(K) []any
	IntPrimaryKey() *string
}

type InserType

type InserType string
const (
	InsertReplace InserType = "REPLACE"
	InsertIgnore  InserType = "IGNORE"
	ModeUpsert    InserType = "UPSERT"
)

type PaginationMode

type PaginationMode int
const (
	PaginationOffset PaginationMode = iota
	PaginationKeyset
)

type PostgresDialect

type PostgresDialect struct{}

func (PostgresDialect) DriverName

func (PostgresDialect) DriverName() string

func (PostgresDialect) Init

func (PostgresDialect) Init(*sql.DB) error

func (PostgresDialect) Insert

func (d PostgresDialect) Insert(table string, cols []string) string

func (PostgresDialect) InsertIgnore

func (d PostgresDialect) InsertIgnore(table string, cols []string, conflictCols []string) string

func (PostgresDialect) InsertReplace

func (d PostgresDialect) InsertReplace(table string, cols []string, conflictCols []string) string

func (PostgresDialect) Placeholder

func (PostgresDialect) Placeholder(index int) string

func (PostgresDialect) Placeholders

func (d PostgresDialect) Placeholders(count int, startIndex int) string

func (PostgresDialect) QuoteIdent

func (PostgresDialect) QuoteIdent(name string) string

func (PostgresDialect) SupportsMaintenance

func (PostgresDialect) SupportsMaintenance() bool

func (PostgresDialect) Upsert

func (d PostgresDialect) Upsert(table string, cols []string, conflictCols []string, updateCols []string) string

type SQLiteDialect

type SQLiteDialect struct{}

func (SQLiteDialect) DriverName

func (SQLiteDialect) DriverName() string

func (SQLiteDialect) Init

func (SQLiteDialect) Init(db *sql.DB) error

func (SQLiteDialect) Insert

func (d SQLiteDialect) Insert(table string, cols []string) string

func (SQLiteDialect) InsertIgnore

func (d SQLiteDialect) InsertIgnore(table string, cols []string, _ []string) string

func (SQLiteDialect) InsertReplace

func (d SQLiteDialect) InsertReplace(table string, cols []string, _ []string) string

func (SQLiteDialect) Placeholder

func (SQLiteDialect) Placeholder(int) string

func (SQLiteDialect) Placeholders

func (d SQLiteDialect) Placeholders(count int, startIndex int) string

func (SQLiteDialect) QuoteIdent

func (SQLiteDialect) QuoteIdent(name string) string

func (SQLiteDialect) SupportsMaintenance

func (SQLiteDialect) SupportsMaintenance() bool

func (SQLiteDialect) Upsert

func (d SQLiteDialect) Upsert(table string, cols []string, conflictCols []string, updateCols []string) string

Directories

Path Synopsis
Package postgres contains reusable PostgreSQL helpers built on pgx/v5.
Package postgres contains reusable PostgreSQL helpers built on pgx/v5.

Jump to

Keyboard shortcuts

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