godb

package module
v2.4.2 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2023 License: BSD-2-Clause Imports: 10 Imported by: 3

README

GODB

The database wrapper for manage postgres db

Support DB drivers

Init connection example

import _ "github.com/lib/pq"

connectionConfig := godb.PostgresConnectionConfig{...}

dbo, err := godb.DBO{
    Options: godb.Options{
        Debug:  true,
        QueryProcessor: godb.PreparePositionalArgsQuery,
        Logger: App.GetLogger(),
    },
    Connection: &connectionConfig,
}.Init()

Transaction functions

func StartTransaction() *godb.SqlTx {
	tx, err := dbo.Begin()
	if err != nil {
		panic(err)
		return nil
	}
	return tx
}

func EndTransaction(q *godb.SqlTx, e porterr.IError) {
	var err error
	if e != nil {
		err = q.Rollback()
	} else {
		err = q.Commit()
	}
	if err != nil {
		panic(err)
	}
	return
}

// usage

tx := StartTransaction()
defer func() { EndTransaction(tx, e) }()

If you find this project useful or want to support the author, you can send tokens to any of these wallets
  • Bitcoin: bc1qgx5c3n7q26qv0tngculjz0g78u6mzavy2vg3tf
  • Ethereum: 0x62812cb089E0df31347ca32A1610019537bbFe0D
  • Dogecoin: DET7fbNzZftp4sGRrBehfVRoi97RiPKajV

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsTableExists added in v2.4.0

func IsTableExists(q Queryer, table, schema string) bool

IsTableExists check if table exists

func PreparePositionalArgsQuery added in v2.4.0

func PreparePositionalArgsQuery(query string) string

PreparePositionalArgsQuery Position argument

Types

type Connection

type Connection interface {
	// String Get prepared URI
	String() string
	// GetDbType return database type
	GetDbType() string
	// GetMaxConnection return maximum connection
	GetMaxConnection() int
	// GetConnMaxLifetime return maximum lifetime
	GetConnMaxLifetime() int
	// GetMaxIdleConns return maximum idle connections count
	GetMaxIdleConns() int
}

Connection Database Object Connection Interface

type ConnectionConfig

type ConnectionConfig struct {
	// Database host
	Host string `yaml:"host"`
	// Database port
	Port int `yaml:"port"`
	// Database name
	Name string `yaml:"name"`
	// Database user
	User string `yaml:"user"`
	// Database password
	Password string `yaml:"password"`
	// Maximum connections
	MaxConnections int `yaml:"maxConnections"`
	// Maximum idle connections count
	MaxIdleConnections int `yaml:"maxIdleConnections"`
	// Connection idle lifetime
	ConnectionIdleLifetime int `yaml:"connectionIdleLifetime"`
}

ConnectionConfig Client connection config

func (*ConnectionConfig) GetConnMaxLifetime

func (cc *ConnectionConfig) GetConnMaxLifetime() int

GetConnMaxLifetime Connection idle lifetime

func (*ConnectionConfig) GetMaxConnection

func (cc *ConnectionConfig) GetMaxConnection() int

GetMaxConnection Get Max Connection

func (*ConnectionConfig) GetMaxIdleConns

func (cc *ConnectionConfig) GetMaxIdleConns() int

GetMaxIdleConns Connection max idle connections

type DBO

type DBO struct {
	*sql.DB
	Options
	Connection Connection
}

DBO Main Database Object

func (*DBO) Begin

func (dbo *DBO) Begin() (*SqlTx, error)

Begin transaction

func (*DBO) ConnType added in v2.4.0

func (dbo *DBO) ConnType() string

ConnType get connection type

func (*DBO) Exec

func (dbo *DBO) Exec(query string, args ...interface{}) (sql.Result, error)

Exec SQL run query

func (DBO) Init

func (dbo DBO) Init() (*DBO, error)

Init Database Object

func (*DBO) Prepare

func (dbo *DBO) Prepare(query string) (*SqlStmt, error)

Prepare statement

func (*DBO) Query

func (dbo *DBO) Query(query string, args ...interface{}) (*sql.Rows, error)

Query SQL exec query

func (*DBO) QueryRow

func (dbo *DBO) QueryRow(query string, args ...interface{}) *sql.Row

QueryRow SQL query row

type IConnType added in v2.4.0

type IConnType interface {
	// ConnType return connection type
	ConnType() string
}

IConnType interface helps to get connection type

type IOptions added in v2.4.0

type IOptions interface {
	// GetLogger return logger
	GetLogger() gocli.Logger
}

IOptions interface helps to get logger

type Options

type Options struct {
	// Debug mode shows logs
	Debug bool
	// Logger
	Logger gocli.Logger

	// Query preprocessing
	QueryProcessor func(query string) string
	// TTL for transaction
	TransactionTTL time.Duration `yaml:"transactionTTL"`
	// contains filtered or unexported fields
}

Options database object options

func (*Options) GetLogger added in v2.4.0

func (o *Options) GetLogger() gocli.Logger

GetLogger return logger

type PostgresConnectionConfig

type PostgresConnectionConfig struct {
	ConnectionConfig `yaml:",inline"`
	// SSL mode
	SSLMode string `yaml:"sslMode"`
	// Use with pgpool
	BinaryParameters bool `yaml:"binaryParameters"`
}

PostgresConnectionConfig Postgres connection config

func (*PostgresConnectionConfig) GetDbType

func (pcc *PostgresConnectionConfig) GetDbType() string

GetDbType Get database type

func (*PostgresConnectionConfig) String

func (pcc *PostgresConnectionConfig) String() string

To string

type Queryer

type Queryer interface {
	// Exec query
	Exec(query string, args ...interface{}) (sql.Result, error)
	// Prepare statement
	Prepare(query string) (*SqlStmt, error)
	// Query rows
	Query(query string, args ...interface{}) (*sql.Rows, error)
	// QueryRow single row
	QueryRow(query string, args ...interface{}) *sql.Row
}

Queryer interface

type SqlStmt

type SqlStmt struct {
	*sql.Stmt
	Options
	// contains filtered or unexported fields
}

SqlStmt Statement object

func (*SqlStmt) Exec

func (st *SqlStmt) Exec(args ...interface{}) (sql.Result, error)

Exec Stmt Exec

func (*SqlStmt) Query

func (st *SqlStmt) Query(args ...interface{}) (*sql.Rows, error)

Query Stmt Query

func (*SqlStmt) QueryRow

func (st *SqlStmt) QueryRow(args ...interface{}) *sql.Row

QueryRow Stmt Query Row

type SqlTx

type SqlTx struct {
	*sql.Tx
	Options

	Connection Connection
	// contains filtered or unexported fields
}

SqlTx Transaction object

func (*SqlTx) Commit

func (tx *SqlTx) Commit() error

Commit transaction

func (*SqlTx) ConnType added in v2.4.0

func (tx *SqlTx) ConnType() string

ConnType get connection type

func (*SqlTx) Exec

func (tx *SqlTx) Exec(query string, args ...interface{}) (sql.Result, error)

Exec Transaction

func (*SqlTx) Prepare

func (tx *SqlTx) Prepare(query string) (*SqlStmt, error)

Prepare Stmt

func (*SqlTx) Query

func (tx *SqlTx) Query(query string, args ...interface{}) (*sql.Rows, error)

Query Transaction

func (*SqlTx) QueryRow

func (tx *SqlTx) QueryRow(query string, args ...interface{}) *sql.Row

QueryRow Query Row transaction

func (*SqlTx) Rollback

func (tx *SqlTx) Rollback() error

Rollback transaction

func (*SqlTx) Stmt

func (tx *SqlTx) Stmt(stmt *SqlStmt) *SqlStmt

Stmt Get Stmt

type Transaction

type Transaction struct {
	// Time to live in unix timestampt
	// 0 - no TTL for transaction
	TTL int
	// contains filtered or unexported fields
}

Transaction params

type TransactionId

type TransactionId string

TransactionId transaction identifier

func GenTransactionId

func GenTransactionId() TransactionId

GenTransactionId Generate transaction id

type TransactionPool

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

TransactionPool transaction pool

func NewTransactionPool

func NewTransactionPool() *TransactionPool

NewTransactionPool Create transaction pool

func (*TransactionPool) Count

func (p *TransactionPool) Count() int

Count transaction count

func (*TransactionPool) Get

func (p *TransactionPool) Get(id TransactionId) *SqlTx

Get transaction if exists

func (*TransactionPool) Reset

func (p *TransactionPool) Reset() *TransactionPool

Reset pool

func (*TransactionPool) Set

Set transaction

func (*TransactionPool) UnSet

UnSet transaction

Jump to

Keyboard shortcuts

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