database

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DEFAULT_TIMEOUT_SECONDS   int           = 5
	DEFAULT_MAX_OPEN_CONNS    int           = 10
	DEFAULT_MAX_IDLE_CONNS    int           = 10
	DEFAULT_CONN_MAX_LIFETIME time.Duration = 5 * time.Minute
)

Variables

This section is empty.

Functions

func GetSqlError

func GetSqlError(err error) error

func ManageUpdateError

func ManageUpdateError(callerName string, result sql.Result, err error) error

Manages the possible error coming for an update execution and returns the database error if exists. If not, it checks if any row has been affected and returns err if nothing has been updated.

  params

	'callerName' is the name of the caller function, it is used in log message
	'result' is the sql.Result returned by Exec function
	'err' is the error returned by Exec function

Types

type Connection

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

type DbConfig

type DbConfig struct {
	Driver DbDriver
	User   string
	Pass   string
	Host   string
	Port   int
	DBName string

	TimeoutSeconds  int // connection timeout
	MaxOpenConns    int
	MaxIdleConns    int
	ConnMaxLifetime time.Duration
}

DbConfig is the generic configuration for all SQL engines.

type DbDriver

type DbDriver string
const (
	DB_DRIVER_MYSQL    DbDriver = "mysql"
	DB_DRIVER_POSTGRES DbDriver = "postgres"
)

type DbPool

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

func CreateDbPool

func CreateDbPool(cfg DbConfig) (*DbPool, error)

CreateDbPool creates a connection pool for the given database configuration.

The given database configuration is used to create a connection pool. The connection pool is created with the following settings: - Max open connections: cfg.MaxOpenConns (default: 10) - Max idle connections: cfg.MaxIdleConns (default: 10) - Connection max lifetime: cfg.ConnMaxLifetime (default: 5 minutes) - Connection timeout: cfg.TimeoutSeconds (default: 5 seconds)

The connection pool is created by calling sql.Open() with the database configuration.

If the connection pool cannot be created, an error is returned.

The returned error is of type *errors.CommonsError and contains the error code "ERROR_OPENING_DATABASE" and the error message returned by sql.Open().

func (*DbPool) BeginEnhacedTx

func (d *DbPool) BeginEnhacedTx(ctx context.Context) (*EnhacedTx, error)

func (*DbPool) BeginTx

func (d *DbPool) BeginTx(ctx context.Context) (*sql.Tx, error)

func (*DbPool) Close

func (d *DbPool) Close()

func (*DbPool) GetConnection

func (d *DbPool) GetConnection() *sql.DB

type EnhacedTx

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

func (*EnhacedTx) Commit

func (v *EnhacedTx) Commit() error

Commit commits the transaction

func (*EnhacedTx) Exec

func (v *EnhacedTx) Exec(query string, args ...any) (sql.Result, error)

Exec executes a query that doesn't return rows. For example: an INSERT and UPDATE.

func (*EnhacedTx) ForceClose

func (v *EnhacedTx) ForceClose() error

ForceClose closes and aborts the transaction

func (*EnhacedTx) IsActive

func (v *EnhacedTx) IsActive() bool

IsOpen returns true if the transaction is still active

func (*EnhacedTx) Query

func (v *EnhacedTx) Query(query string, args ...any) (*sql.Rows, error)

Query executes a query that returns rows, typically a SELECT.

func (*EnhacedTx) QueryRow

func (v *EnhacedTx) QueryRow(query string, args ...any) *sql.Row

QueryRow executes a query that is expected to return at most one row. QueryRow always returns a non-nil value. Errors are deferred until [Row]'s Scan method is called.

func (*EnhacedTx) Rollback

func (v *EnhacedTx) Rollback() error

Rollback aborts the transaction

type Executor

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

Jump to

Keyboard shortcuts

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