godb

package module
v2.1.3 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2023 License: BSD-2-Clause Imports: 11 Imported by: 0

README

GODB

The database wrapper for manage postgres db

Init connection example

import _ "github.com/lib/pq"

connectionConfig := godb.PostgresConnectionConfig{...}

dbo, err := godb.DBO{
    Options: godb.Options{
        Debug:  true,
        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) }()

Migration


migrations := &godb.Migration{
    RegistryPath:  "user/project",
    MigrationPath: "user/project/migrations",
    RegistryXPath: "app.Registry",
    DBO:           app.GetDB(),
    Registry:      make(godb.MigrationRegistry),
    Config:        app.GetConfig().Db.ConnectionConfig,
}

// Init schema migration
err := migrations.InitMigration("schema")
if err != nil {
    panic(err)
}

// Execute schema migrations
err = migrations.Upgrade("schema")
if err != nil {
    panic(err)
}
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

This section is empty.

Types

type Connection

type Connection interface {
	String() string
	GetDbType() string
	GetMaxConnection() int
	GetConnMaxLifetime() int
	GetMaxIdleConns() int
}

Database Object Connection Interface

type ConnectionConfig

type ConnectionConfig struct {
	Host                   string
	Port                   int
	Name                   string
	User                   string
	Password               string
	MaxConnections         int `yaml:"maxConnections"`
	MaxIdleConnections     int `yaml:"maxIdleConnections"`
	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
}

Main Database Object

func (*DBO) Begin

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

Begin transaction

func (*DBO) BeginContext

func (dbo *DBO) BeginContext(ctx context.Context) (*SqlTx, error)

BeginContext transaction

func (*DBO) Exec

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

Exec SQL run query

func (*DBO) ExecContext

func (dbo *DBO) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

ExecContext 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) PrepareContext

func (dbo *DBO) PrepareContext(ctx context.Context, query string) (*SqlStmt, error)

PrepareContext statement

func (*DBO) Query

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

Query SQL exec query

func (*DBO) QueryContext

func (dbo *DBO) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

QueryContext SQL exec query

func (*DBO) QueryRow

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

QueryRow SQL query row

func (*DBO) QueryRowContext

func (dbo *DBO) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row

QueryRowContext SQL query row

type ILogger added in v2.1.3

type ILogger interface {
	Output(callDepth int, message string) error

	Printf(format string, v ...interface{})
	Print(v ...interface{})
	Println(v ...interface{})

	Fatal(v ...interface{})
	Fatalf(format string, v ...interface{})
	Fatalln(v ...interface{})

	Panic(v ...interface{})
	Panicf(format string, v ...interface{})
	Panicln(v ...interface{})
}

type IMigrationFile

type IMigrationFile interface {
	Up(tx *SqlTx) error
	Down(tx *SqlTx) error
	GetVersion() string
}

IMigrationFile migration file interface

type Migration

type Migration struct {
	MigrationPath string
	DBO           *DBO
	Config        ConnectionConfig
	Registry      MigrationRegistry
	RegistryPath  string
	RegistryXPath string
}

Migration struct

func (*Migration) CreateMigrationFile

func (m *Migration) CreateMigrationFile(class string, name string) error

CreateMigrationFile Create migration file

func (*Migration) Downgrade

func (m *Migration) Downgrade(class string, version string) error

Downgrade

func (*Migration) GetTemplate

func (m *Migration) GetTemplate() *template.Template

func (*Migration) InitMigration

func (m *Migration) InitMigration(class string) error

InitMigration Init migration

func (*Migration) Upgrade

func (m *Migration) Upgrade(class string) error

Upgrade

type MigrationRegistry

type MigrationRegistry map[string][]IMigrationFile

MigrationRegistry migration registry

type Options

type Options struct {
	// Debug mode shows logs
	Debug bool
	// Logger
	Logger ILogger

	// TTL for transaction
	TransactionTTL time.Duration `yaml:"transactionTTL"`
	// contains filtered or unexported fields
}

Options database object options

type PostgresConnectionConfig

type PostgresConnectionConfig struct {
	ConnectionConfig `yaml:",inline"`
	SSLMode          string `yaml:"sslMode"`
	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 string, args ...interface{}) (sql.Result, error)
	Prepare(query string) (*SqlStmt, error)
	Query(query string, args ...interface{}) (*sql.Rows, error)
	QueryRow(query string, args ...interface{}) *sql.Row
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
	QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
}

Queryer interface

type SqlStmt

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

Stmt

func (*SqlStmt) Exec

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

Exec Stmt Exec

func (*SqlStmt) ExecContext

func (st *SqlStmt) ExecContext(ctx context.Context, args ...interface{}) (sql.Result, error)

ExecContext Stmt Exec

func (*SqlStmt) Query

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

Query Stmt Query

func (*SqlStmt) QueryContext

func (st *SqlStmt) QueryContext(ctx context.Context, args ...interface{}) (*sql.Rows, error)

QueryContext Stmt Query

func (*SqlStmt) QueryRow

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

QueryRow Stmt Query Row

func (*SqlStmt) QueryRowContext

func (st *SqlStmt) QueryRowContext(ctx context.Context, args ...interface{}) *sql.Row

QueryRowContext Stmt Query Row

type SqlTx

type SqlTx struct {
	*sql.Tx
	Options
	// contains filtered or unexported fields
}

Transaction

func (*SqlTx) Commit

func (tx *SqlTx) Commit() error

Commit transaction

func (*SqlTx) Exec

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

Exec Transaction

func (*SqlTx) ExecContext

func (tx *SqlTx) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

ExecContext Transaction

func (*SqlTx) Prepare

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

Prepare Stmt

func (*SqlTx) PrepareContext

func (tx *SqlTx) PrepareContext(ctx context.Context, query string) (*SqlStmt, error)

PrepareContext Stmt

func (*SqlTx) Query

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

Query Transaction

func (*SqlTx) QueryContext

func (tx *SqlTx) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

QueryContext Transaction

func (*SqlTx) QueryRow

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

QueryRow Query Row transaction

func (*SqlTx) QueryRowContext

func (tx *SqlTx) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row

QueryRowContext 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

func (*SqlTx) StmtContext

func (tx *SqlTx) StmtContext(ctx context.Context, stmt *SqlStmt) *SqlStmt

StmtContext 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