sqlz

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2022 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// AllMigrations means apply/revert all migrations.
	AllMigrations = -1
)

Variables

View Source
var (
	// DefaultPragmas provides a default, sane set of PRAGMAs for opening SQLite databases.
	DefaultPragmas = map[string]interface{}{
		"auto_vacuum":              2,
		"cache_size":               -2000,
		"foreign_keys":             1,
		"ignore_check_constraints": 0,
		"locking_mode":             "normal",
		"synchronous":              2,
		"writable_schema":          0,
	}
)

Functions

func ApplyMigrations

func ApplyMigrations(db *sql.DB, migrations *ConfigMigrations, max int) (int, error)

ApplyMigrations applies the given migrations.

func GetDB

func GetDB(ctx context.Context) *sql.DB

GetDB extracts the SQLite from context as *sql.DB, panics if not found.

func Initializer

func Initializer(ctx context.Context) (injectz.Injector, injectz.Releaser)

Initializer is a SQLite initializer.

func NewConfigSingletonInjector

func NewConfigSingletonInjector(cfg *Config) injectz.Injector

NewConfigSingletonInjector always inject the given *Config.

func RevertMigrations

func RevertMigrations(db *sql.DB, migrations *ConfigMigrations, max int) (int, error)

RevertMigrations reverts the given migrations.

Types

type Config

type Config struct {
	DBSpec          *ConfigDBSpec     `json:"dbSpec" validate:"required"`
	ApplyMigrations *ConfigMigrations `json:"-"`
}

Config describes the configuration for SQLite.

func GetConfig

func GetConfig(ctx context.Context) *Config

GetConfig extracts the *Config from context, panics if not found.

func (*Config) Validate

func (c *Config) Validate() error

Validate implements the vz.Validator interface.

type ConfigDBSpec

type ConfigDBSpec struct {
	FilePath string                 `json:"filePath" validate:"required"`
	Pragmas  map[string]interface{} `json:"pragmas" validate:"required"`
}

ConfigDBSpec describes part of the configuration for SQLite.

func NewConfigDBSpec

func NewConfigDBSpec(filePath string, pragmas map[string]interface{}) *ConfigDBSpec

NewConfigDBSpec initializes a new *ConfigDBSpec.

func NewDefaultConfigDBSpec

func NewDefaultConfigDBSpec(filePath string) *ConfigDBSpec

NewDefaultConfigDBSpec initializes a new *ConfigDBSpec with the default PRAGMAs.

func (*ConfigDBSpec) ToOpen

func (c *ConfigDBSpec) ToOpen() string

ToOpen converts the *ConfigDBSpec to a string that can be passed to sql.Open().

type ConfigMigrations

type ConfigMigrations struct {
	EmbedFS                embed.FS `json:"-" validate:"required"`
	EmbedMigrationsDirPath string   `json:"embedMigrationsDirPath" validate:"required"`
}

ConfigMigrations describes part of the configuration for SQLite.

func NewConfigMigrations

func NewConfigMigrations(embedFS embed.FS, embedMigrationsDirPath string) *ConfigMigrations

NewConfigMigrations initializes a new *ConfigMigrations.

type ContextSQLite

type ContextSQLite interface {
	Exec(query string, args ...interface{}) (sql.Result, error)
	Query(query string, args ...interface{}) (*sql.Rows, error)
	QueryRow(query string, args ...interface{}) *sql.Row
}

ContextSQLite describes a SQLite with a cached context.

func GetCtx

func GetCtx(ctx context.Context) ContextSQLite

GetCtx extracts the SQLite from context and wraps it as ContextSQLite, panics if not found.

type SQLite

type SQLite interface {
	Exec(query string, args ...interface{}) (sql.Result, error)
	Query(query string, args ...interface{}) (*sql.Rows, error)
	QueryRow(query string, args ...interface{}) *sql.Row
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
}

SQLite describes the sqlite module (a subset of *sql.DB).

func Get

func Get(ctx context.Context) SQLite

Get extracts the SQLite from context, panics if not found.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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