db

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2022 License: MIT Imports: 8 Imported by: 17

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CloseAll

func CloseAll() []exception.Exception

CloseAll initiated mysql connection

func GetInstance

func GetInstance(instanceName string) (*sql.DB, exception.Exception)

GetInstance that already fabricated before as an sql.DB

Types

type Adapter

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

An Adapter for golang sql

func (*Adapter) Eject added in v1.3.0

func (a *Adapter) Eject() *sql.DB

Eject sql.DB out of db adapter

func (*Adapter) ExecContext

func (a *Adapter) ExecContext(ktx kontext.Context, queryKey, query string, args ...interface{}) (Result, exception.Exception)

ExecContext wrap sql ExecContext function

func (*Adapter) Ping

func (a *Adapter) Ping(ktx kontext.Context) exception.Exception

Ping wrap sql Ping function

func (*Adapter) QueryContext

func (a *Adapter) QueryContext(ktx kontext.Context, queryKey, query string, args ...interface{}) (Rows, exception.Exception)

QueryContext wrap sql QueryContext function

func (*Adapter) QueryRowContext

func (a *Adapter) QueryRowContext(ktx kontext.Context, queryKey, query string, args ...interface{}) Row

QueryRowContext wrap sql QueryRowContext function

func (*Adapter) Transaction

func (a *Adapter) Transaction(ktx kontext.Context, transactionKey string, f func(tx TX) exception.Exception) exception.Exception

Transaction wrap mysql transaction into a bit of simpler way

type Config

type Config struct {
	Username string
	Password string
	Host     string

	// If port is empty it will return 3306 instead
	Port string

	Name string
	// contains filtered or unexported fields
}

Config carry all database config in a single struct

type DB

type DB interface {
	Ping(ktx kontext.Context) exception.Exception
	Transactionable
	TX
	Eject() *sql.DB
}

DB is database interface wrapper for *sql.DB

func Adapt

func Adapt(db *sql.DB) DB

Adapt adapting golang sql.DB

func FabricateMySQL

func FabricateMySQL(instanceName string, config Config, opts ...Option) (DB, exception.Exception)

FabricateMySQL will fabricate mysql connection and wrap it into SQL interfaces

type Option

type Option func(*Config)

Option when fabricating connection

func WithConnMaxLifetime

func WithConnMaxLifetime(connMaxLifetime time.Duration) Option

WithConnMaxLifetime fabricate connection with max connection lifetime

func WithMaxIdleConn

func WithMaxIdleConn(maxIdleConn int) Option

WithMaxIdleConn fabricate connection with max idle connection

func WithMaxOpenConn

func WithMaxOpenConn(maxOpenConn int) Option

WithMaxOpenConn fabricate connection with max open connection

type Result

type Result interface {
	// LastInsertId returns the integer generated by the database
	// in response to a command. Typically this will be from an
	// "auto increment" column when inserting a new row. Not all
	// databases support this feature, and the syntax of such
	// statements varies.
	LastInsertId() (int64, exception.Exception)

	// RowsAffected returns the number of rows affected by an
	// update, insert, or delete. Not every database or database
	// driver may support this.
	RowsAffected() (int64, exception.Exception)
}

Result summarizes an executed SQL command.

func AdaptResult

func AdaptResult(result sql.Result) Result

AdaptResult wrap default sql result

type ResultAdapter

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

ResultAdapter wrap sql result

func (*ResultAdapter) LastInsertId

func (r *ResultAdapter) LastInsertId() (int64, exception.Exception)

LastInsertId returns the integer generated by the database in response to a command. Typically this will be from an "auto increment" column when inserting a new row. Not all databases support this feature, and the syntax of such statements varies.

func (*ResultAdapter) RowsAffected

func (r *ResultAdapter) RowsAffected() (int64, exception.Exception)

RowsAffected returns the number of rows affected by an update, insert, or delete. Not every database or database driver may support this.

type Row

type Row interface {
	Scan(dest ...interface{}) exception.Exception
}

Row single result of database query

func AdaptRow

func AdaptRow(sqlrow *sql.Row) Row

AdaptRow wrap provider row

type RowAdapter

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

RowAdapter wrap single sql row

func (*RowAdapter) Scan

func (r *RowAdapter) Scan(dest ...interface{}) exception.Exception

Scan warp default row scan function

type Rows

type Rows interface {
	Close() exception.Exception
	Columns() ([]string, exception.Exception)
	Err() exception.Exception
	Next() bool
	NextResultSet() bool
	Scan(dest ...interface{}) exception.Exception
}

Rows multiple result of database query

func AdaptRows

func AdaptRows(rows *sql.Rows) Rows

AdaptRows adapting sql.Rows into adapter.Rows

type RowsAdapter

type RowsAdapter struct {
	*sql.Rows
}

RowsAdapter wrap default sql.Rows struct

func (*RowsAdapter) Close

func (r *RowsAdapter) Close() exception.Exception

Close rows

func (*RowsAdapter) Columns

func (r *RowsAdapter) Columns() ([]string, exception.Exception)

Columns return rows column

func (*RowsAdapter) Err

func (r *RowsAdapter) Err() exception.Exception

Err return rows error

func (*RowsAdapter) Scan

func (r *RowsAdapter) Scan(dest ...interface{}) exception.Exception

Scan row

type TX

type TX interface {
	ExecContext(ctx kontext.Context, queryKey, query string, args ...interface{}) (Result, exception.Exception)
	QueryContext(ctx kontext.Context, queryKey, query string, args ...interface{}) (Rows, exception.Exception)
	QueryRowContext(ctx kontext.Context, queryKey, query string, args ...interface{}) Row
}

TX is database transaction

type TXAdapter

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

A TXAdapter adapater for golang sql

func AdaptTXAdapter

func AdaptTXAdapter(tx *sql.Tx) *TXAdapter

AdaptTXAdapter do adapting mysql transaction

func (*TXAdapter) ExecContext

func (t *TXAdapter) ExecContext(ctx kontext.Context, queryKey, query string, args ...interface{}) (Result, exception.Exception)

ExecContext wrap sql ExecContext function

func (*TXAdapter) QueryContext

func (t *TXAdapter) QueryContext(ctx kontext.Context, queryKey, query string, args ...interface{}) (Rows, exception.Exception)

QueryContext wrap sql QueryContext function

func (*TXAdapter) QueryRowContext

func (t *TXAdapter) QueryRowContext(ctx kontext.Context, queryKey, query string, args ...interface{}) Row

QueryRowContext wrap sql QueryRowContext function

type Transactionable

type Transactionable interface {
	Transaction(ctx kontext.Context, transactionKey string, f func(tx TX) exception.Exception) exception.Exception
}

Transactionable is wrapper to create transaction process

Directories

Path Synopsis
Package mock is a generated GoMock package.
Package mock is a generated GoMock package.

Jump to

Keyboard shortcuts

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