sql

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 19, 2024 License: Apache-2.0 Imports: 10 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ToSnakeCase

func ToSnakeCase(str string) string

Types

type DB

type DB struct {
	// contains unexported or private fields
	*sql.DB
	// contains filtered or unexported fields
}

DB is a wrapper around sql.DB which provides some more features.

func NewSQL added in v0.2.0

func NewSQL(configs config.Config, logger datasource.Logger, metrics Metrics) *DB

func (*DB) Begin

func (d *DB) Begin() (*Tx, error)

func (*DB) Exec

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

func (*DB) HealthCheck

func (d *DB) HealthCheck() *datasource.Health

func (*DB) Prepare

func (d *DB) Prepare(query string) (*sql.Stmt, error)

func (*DB) Query

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

func (*DB) QueryRow

func (d *DB) QueryRow(query string, args ...interface{}) *sql.Row

func (*DB) QueryRowContext

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

func (*DB) Select

func (d *DB) Select(ctx context.Context, data interface{}, query string, args ...interface{})

Select runs a query with args and binds the result of the query to the data. data should ba a point to a slice, struct or any type. Slice will return multiple objects whereas struct will return a single object.

Example Usages:

  1. Get multiple rows with only one column ids := make([]int, 0) db.Select(ctx, &ids, "select id from users")

  2. Get a single object from database type user struct { Name string ID int Image string } u := user{} db.Select(ctx, &u, "select * from users where id=?", 1)

  3. Get array of objects from multiple rows type user struct { Name string ID int Image string `db:"image_url"` } users := []user{} db.Select(ctx, &users, "select * from users")

type DBConfig

type DBConfig struct {
	HostName string
	User     string
	Password string
	Port     string
	Database string
}

DBConfig has those members which are necessary variables while connecting to database.

type DBStats added in v0.2.0

type DBStats struct {
	MaxOpenConnections int `json:"maxOpenConnections"` // Maximum number of open connections to the database.

	// Pool Status
	OpenConnections int `json:"openConnections"` // The number of established connections both in use and idle.
	InUse           int `json:"inUse"`           // The number of connections currently in use.
	Idle            int `json:"idle"`            // The number of idle connections.

	// Counters
	WaitCount         int64         `json:"waitCount"`         // The total number of connections waited for.
	WaitDuration      time.Duration `json:"waitDuration"`      // The total time blocked waiting for a new connection.
	MaxIdleClosed     int64         `json:"maxIdleClosed"`     // The total number of connections closed due to SetMaxIdleConns.
	MaxIdleTimeClosed int64         `json:"maxIdleTimeClosed"` // The total number of connections closed due to SetConnMaxIdleTime.
	MaxLifetimeClosed int64         `json:"maxLifetimeClosed"` // The total number of connections closed due to SetConnMaxLifetime.
}

type Log

type Log struct {
	Type     string        `json:"type"`
	Query    string        `json:"query"`
	Duration int64         `json:"duration"`
	Args     []interface{} `json:"args,omitempty"`
}

type Metrics added in v0.3.0

type Metrics interface {
	IncrementCounter(ctx context.Context, name string, labels ...string)
	DeltaUpDownCounter(ctx context.Context, name string, value float64, labels ...string)
	RecordHistogram(ctx context.Context, name string, value float64, labels ...string)
	SetGauge(name string, value float64)
}

type Tx

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

func (*Tx) Commit

func (t *Tx) Commit() error

func (*Tx) Exec

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

func (*Tx) Prepare

func (t *Tx) Prepare(query string) (*sql.Stmt, error)

func (*Tx) Query

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

func (*Tx) QueryRow

func (t *Tx) QueryRow(query string, args ...interface{}) *sql.Row

func (*Tx) QueryRowContext

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

func (*Tx) Rollback

func (t *Tx) Rollback() error

Jump to

Keyboard shortcuts

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