sql

package
v0.0.0-...-29e199f Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package sql defines the SQL database management utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterDialect

func RegisterDialect(name string, dialect Dialect)

Types

type Connection

type Connection interface {
	// GetDB returns the underlying SQL database connection.
	GetDB() *sqlx.DB
	// GetErrorTranslator returns the error translator associated with the connection.
	GetErrorTranslator() ErrorTranslator
	// GetMigrator returns the database migrator associated with the connection.
	GetMigrator() Migrator
	// GetDialectName returns the name of the SQL dialect used by the connection.
	GetDialectName() string
}

Connection represents a SQL database connection.

type ConnectionData

type ConnectionData struct {
	ConnectionString string
	Driver           string
}

type ConnectionFw

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

ConnectionFw represents a database connection framework.

func NewConnectionFw

func NewConnectionFw(options ConnectionFwOptions) (*ConnectionFw, error)

NewConnectionFw returns a new ConnectionFw with the specified ConnectionFwOptions.

func (ConnectionFw) GetDB

func (c ConnectionFw) GetDB() *sqlx.DB

func (ConnectionFw) GetDialectName

func (c ConnectionFw) GetDialectName() string

func (ConnectionFw) GetErrorTranslator

func (c ConnectionFw) GetErrorTranslator() ErrorTranslator

func (ConnectionFw) GetMigrator

func (c ConnectionFw) GetMigrator() Migrator

type ConnectionFwOptions

type ConnectionFwOptions struct {
	// Postgres defines the PostgresSQL connection information
	Postgres *PostgresInfo `mapstructure:"postgres"`
	// SQLite defines the SQLite connection information
	SQLite *SQLiteInfo
}

ConnectionFwOptions defines the options to configure a Connection

type ContextKey

type ContextKey string

type Dialect

type Dialect interface {
	Opener
	Migrator
	ErrorTranslator
}

func GetDialect

func GetDialect(s string) (Dialect, bool)

type ErrorTranslator

type ErrorTranslator interface {
	// TranslateError translates an SQL error.
	TranslateError(ctx context.Context, originalErr error, defaultError error) error
}

ErrorTranslator translates SQL errors for better context.

type Fw

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

Fw persistence framework for Storage data: - Allows transactional executions - Translates specific database errors - Allow templated statements

func NewPersistenceFw

func NewPersistenceFw(opts FwOptions) (*Fw, error)

NewPersistenceFw creates a PersistenceFw instance with the given options

func (*Fw) AddConfig

func (p *Fw) AddConfig(config persistence.StorageConfig) error

AddConfig registers SQL statements

func (*Fw) BeginTransaction

func (p *Fw) BeginTransaction(ctx context.Context) (context.Context, error)

BeginTransaction starts a Storage operation that is transactional. It returns an error if it fails

func (*Fw) CommitTransaction

func (p *Fw) CommitTransaction(ctx context.Context) error

CommitTransaction confirms a Storage operation that is transactional. It returns an error if it fails

func (*Fw) ExecuteStmt

func (p *Fw) ExecuteStmt(ctx context.Context, stmtID string, args interface{}) error

ExecuteStmt executes statement with arguments provided. It returns an error if it fails

func (*Fw) ExecuteStmtWithStorageResult

func (p *Fw) ExecuteStmtWithStorageResult(ctx context.Context, stmtID string, args interface{}) (*persistence.ExecuteStmtWithStorageResultOutput, error)

ExecuteStmtWithStorageResult executes statement with arguments provided. It returns information on stmt or an error if it fails

func (*Fw) GetTransaction

func (p *Fw) GetTransaction(ctx context.Context) (any, error)

GetTransaction returns the transaction from context (in case of success).

func (*Fw) QueryAll

func (p *Fw) QueryAll(ctx context.Context, stmtID string, args interface{}, dst interface{}) error

QueryAll obtains all element from statement with arguments provided. It returns an error if it fails

func (*Fw) RollbackTransaction

func (p *Fw) RollbackTransaction(ctx context.Context) error

RollbackTransaction rollbacks to previous state a Storage operation that is transactional. It returns an error if it fails

func (*Fw) SetSQLClientParameters

func (p *Fw) SetSQLClientParameters(parameters SQLClientParameters)

type FwOptions

type FwOptions struct {
	Connection          Connection
	SQLClientParameters SQLClientParameters
}

FwOptions options to configure the PersistenceFw

type GetVersionOutput

type GetVersionOutput struct {
	MigrationVersion
}

type MigrationVersion

type MigrationVersion struct {
	Version     int
	Dirty       bool
	Description string
}

type Migrator

type Migrator interface {
	// OpenConnection opens a connection to the database.
	OpenConnection(ctx context.Context) error
	// InitMigration initializes the migration process.
	InitMigration(ctx context.Context, migrationsTablePrefix *string) error
	// GetMigrationVersion retrieves the current migration version.
	GetMigrationVersion(ctx context.Context) (*GetVersionOutput, error)
	// SetMigrationVersion sets the migration version.
	SetMigrationVersion(ctx context.Context, input SetVersionInput) error
	// RunMigration runs a migration script.
	RunMigration(ctx context.Context, migration string) error
	// CloseConnection closes the connection to the database.
	CloseConnection(ctx context.Context) error
}

Migrator manages database migrations.

type Opener

type Opener interface {
	// GetConnectionData retrieves connection data from the provided configuration.
	GetConnectionData(config interface{}) (*ConnectionData, error)
	// Connect establishes a database connection using the provided connection data.
	Connect(connectionData ConnectionData) (*sqlx.DB, error)
}

Opener defines functionality to establish a database connection.

type PostgresInfo

type PostgresInfo struct {
	Host     string `mapstructure:"host" valid:"required~hosts is mandatory in Postgres Connection config"`
	Port     int    `mapstructure:"port" valid:"required~port is mandatory in Postgres Connection config"`
	Scheme   string `mapstructure:"scheme" valid:"required~scheme is mandatory in Postgres Connection config"`
	Username string `mapstructure:"username" valid:"required~username is mandatory in Postgres Connection config"`
	Password string `mapstructure:"password" valid:"required~password is mandatory in Postgres Connection config"`
	SSLMode  string `mapstructure:"sslMode" valid:"required~sslMode is mandatory in Postgres Connection config"`
	Database string `mapstructure:"database" valid:"required~database is mandatory in Postgres Connection config"`
}

PostgresInfo contains information required to establish a connection to a PostgreSQL database.

type SQLClientParameters

type SQLClientParameters struct {
	MaxIdleConnections    *int
	MaxOpenConnections    *int
	MaxConnectionLifetime *int
}

SQLClientParameters defines parameters that will be passed to database/sql DB to configure client connections settings

type SQLiteInfo

type SQLiteInfo struct {
	// ConnectionString to connect to sqlite databases
	ConnectionString string `valid:"required~connectionString is mandatory in SQLite Connection config"`
}

SQLiteInfo contains information required to establish a connection to a SQLite database.

type SetVersionInput

type SetVersionInput struct {
	MigrationVersion
}

Directories

Path Synopsis
Package postgres defines the PostgresSQL functionalities.
Package postgres defines the PostgresSQL functionalities.
Package sqlite defines the SQLite database functionality to operate with.
Package sqlite defines the SQLite database functionality to operate with.

Jump to

Keyboard shortcuts

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