Documentation ¶
Overview ¶
Package sql provides functionalities to interact with SQL databases using the database/sql package.This package includes a wrapper around sql.DB and sql.Tx to provide additional features such as query logging, metrics recording, and error handling.
Package sql is a generated GoMock package.
Index ¶
- func DeleteByQuery(dialect, tableName, field string) string
- func InsertQuery(dialect, tableName string, fieldNames []string, values []interface{}, ...) (string, error)
- func NewSQLMocks(t *testing.T) (*DB, sqlmock.Sqlmock, *MockMetrics)
- func NewSQLMocksWithConfig(t *testing.T, config *DBConfig) (*DB, sqlmock.Sqlmock, *MockMetrics)
- func SelectByQuery(dialect, tableName, field string) string
- func SelectQuery(dialect, tableName string) string
- func ToSnakeCase(str string) string
- func UpdateByQuery(dialect, tableName string, fieldNames []string, field string) string
- type BindVarType
- type DB
- func (d *DB) Begin() (*Tx, error)
- func (d *DB) Close() error
- func (d *DB) Dialect() string
- func (d *DB) Exec(query string, args ...interface{}) (sql.Result, error)
- func (d *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (d *DB) HealthCheck() *datasource.Health
- func (d *DB) Prepare(query string) (*sql.Stmt, error)
- func (d *DB) Query(query string, args ...interface{}) (*sql.Rows, error)
- func (d *DB) QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
- func (d *DB) QueryRow(query string, args ...interface{}) *sql.Row
- func (d *DB) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
- func (d *DB) Select(ctx context.Context, data interface{}, query string, args ...interface{})
- type DBConfig
- type DBStats
- type FieldConstraints
- type Log
- type Metrics
- type MockMetrics
- type MockMetricsMockRecorder
- type Tx
- func (t *Tx) Commit() error
- func (t *Tx) Exec(query string, args ...interface{}) (sql.Result, error)
- func (t *Tx) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (t *Tx) Prepare(query string) (*sql.Stmt, error)
- func (t *Tx) Query(query string, args ...interface{}) (*sql.Rows, error)
- func (t *Tx) QueryRow(query string, args ...interface{}) *sql.Row
- func (t *Tx) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
- func (t *Tx) Rollback() error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DeleteByQuery ¶
func InsertQuery ¶
func NewSQLMocks ¶
func NewSQLMocks(t *testing.T) (*DB, sqlmock.Sqlmock, *MockMetrics)
func NewSQLMocksWithConfig ¶
func NewSQLMocksWithConfig(t *testing.T, config *DBConfig) (*DB, sqlmock.Sqlmock, *MockMetrics)
func SelectByQuery ¶
func SelectQuery ¶
func ToSnakeCase ¶
Types ¶
type BindVarType ¶
type BindVarType uint
BindVarType represents different type of bindvars in SQL queries.
const ( UNKNOWN BindVarType = iota + 1 QUESTION DOLLAR )
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 (*DB) ExecContext ¶
func (*DB) HealthCheck ¶
func (d *DB) HealthCheck() *datasource.Health
func (*DB) QueryContext ¶
func (*DB) QueryRowContext ¶
func (*DB) Select ¶
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:
Get multiple rows with only one column ids := make([]int, 0) db.Select(ctx, &ids, "select id from users")
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)
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 { Dialect string HostName string User string Password string Port string Database string SSLMode string MaxIdleConn int MaxOpenConn int }
DBConfig has those members which are necessary variables while connecting to database.
type DBStats ¶
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 FieldConstraints ¶
type Log ¶
type Log struct { Type string `json:"type"` Query string `json:"query"` Duration int64 `json:"duration"` Args []interface{} `json:"args,omitempty"` }
func (*Log) PrettyPrint ¶
type MockMetrics ¶
type MockMetrics struct {
// contains filtered or unexported fields
}
MockMetrics is a mock of Metrics interface.
func NewMockMetrics ¶
func NewMockMetrics(ctrl *gomock.Controller) *MockMetrics
NewMockMetrics creates a new mock instance.
func (*MockMetrics) EXPECT ¶
func (m *MockMetrics) EXPECT() *MockMetricsMockRecorder
EXPECT returns an object that allows the caller to indicate expected use.
func (*MockMetrics) RecordHistogram ¶
func (m *MockMetrics) RecordHistogram(ctx context.Context, name string, value float64, labels ...string)
RecordHistogram mocks base method.
type MockMetricsMockRecorder ¶
type MockMetricsMockRecorder struct {
// contains filtered or unexported fields
}
MockMetricsMockRecorder is the mock recorder for MockMetrics.
func (*MockMetricsMockRecorder) RecordHistogram ¶
func (mr *MockMetricsMockRecorder) RecordHistogram(ctx, name, value any, labels ...any) *gomock.Call
RecordHistogram indicates an expected call of RecordHistogram.