database

package
v1.7.6 Latest Latest
Warning

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

Go to latest
Published: Jul 2, 2023 License: CC0-1.0 Imports: 18 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateAddOnQuery added in v0.5.19

func GenerateAddOnQuery(ctx context.Context, reqData *TableRequest) (string, []interface{}, error)

Types

type BaseColumn

type BaseColumn[T string | int | int64] struct {
	Id T `json:"id" db:"id"`
	TimeCol
}

type CUDConstructData

type CUDConstructData struct {
	Cols       []string      `json:"cols"`
	Values     []interface{} `json:"values"`
	ColsInsert string
	BulkValues string
	BulkQuery  string
	Action     string
	TableName  string
}

func (*CUDConstructData) SetValues added in v0.0.3

func (req *CUDConstructData) SetValues(value interface{})

type CUDResponse

type CUDResponse struct {
	Status       bool  `json:"status"`
	RowsAffected int64 `json:"rows_affected"`
	LastInsertID int64 `json:"last_insert_id"`
	// contains filtered or unexported fields
}

func (*CUDResponse) GetGeneratedQuery added in v1.5.9

func (e *CUDResponse) GetGeneratedQuery() map[string][]interface{}

GetGeneratedQuery - return query + params with format map[<query>]<params>

type Follower

type Follower interface {
	// Get from follower database
	Get(dest interface{}, query string, args ...interface{}) error

	// Select from follower database
	Select(dest interface{}, query string, args ...interface{}) error

	// Query from follower database
	Query(query string, args ...interface{}) (*sql.Rows, error)

	// QueryRow executes QueryRow against follower DB
	QueryRow(query string, args ...interface{}) *sql.Row

	// NamedQuery do named query on follower DB
	NamedQuery(query string, arg interface{}) (*sqlx.Rows, error)

	// GetContext from sql database
	GetContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error

	// SelectContext from sql database
	SelectContext(ctx context.Context, dest interface{}, query string, args ...interface{}) error

	// QueryContext from sql database
	QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

	// QueryRowContext from sql database
	QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row

	// QueryxContext queries the database and returns an *sqlx.Rows. Any placeholder parameters are replaced with supplied args.
	QueryxContext(ctx context.Context, query string, args ...interface{}) (*sqlx.Rows, error)

	// QueryRowxContext queries the database and returns an *sqlx.Row. Any placeholder parameters are replaced with supplied args.
	QueryRowxContext(ctx context.Context, query string, args ...interface{}) *sqlx.Row

	// NamedQueryContext do named query on follower DB
	NamedQueryContext(ctx context.Context, query string, arg interface{}) (*sqlx.Rows, error)
}

Follower defines operation that will be executed to follower DB

type Master

type Master interface {
	Exec(query string, args ...interface{}) (sql.Result, error)

	// ExecContext use master database to exec query
	ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

	// Begin transaction on master DB
	Begin() (*sql.Tx, error)

	// BeginTx begins transaction on master DB
	BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)

	// Rebind a query from the default bindtype (QUESTION) to the target bindtype.
	Rebind(sql string) string

	// NamedExec do named exec on master DB
	NamedExec(query string, arg interface{}) (sql.Result, error)

	// NamedExecContext do named exec on master DB
	NamedExecContext(ctx context.Context, query string, arg interface{}) (sql.Result, error)

	// BindNamed do BindNamed on master DB
	BindNamed(query string, arg interface{}) (string, []interface{}, error)
}

type QueryOpts

type QueryOpts struct {
	BaseQuery         string
	Conditions        func(ctx context.Context)
	ExcludeColumns    string
	Columns           string
	OptionalTableName string // for view name
	SelectRequest     *TableRequest
	CUDRequest        *CUDConstructData
	Result            interface{}
	IsList            bool
	Trx               *sql.Tx
	// contains filtered or unexported fields
}

func (*QueryOpts) GetGeneratedQuery added in v1.5.9

func (e *QueryOpts) GetGeneratedQuery() map[string][]interface{}

GetGeneratedQuery - return query + params with format map[<query>]<params>

type SQL

type SQL struct {
	Master
	Follower
	// contains filtered or unexported fields
}

func Connect

func Connect(cfg *SQLs) (*SQL, error)

func (*SQL) GetFollower

func (this *SQL) GetFollower() *sqlx.DB

func (*SQL) GetMaster

func (this *SQL) GetMaster() *sqlx.DB

func (*SQL) Read

func (this *SQL) Read(ctx context.Context, opts *QueryOpts, additionalParams ...interface{}) error

func (*SQL) Write

func (this *SQL) Write(ctx context.Context, opts *QueryOpts, isSoftDelete ...bool) (*CUDResponse, error)

type SQLConfig

type SQLConfig struct {
	Username        string `yaml:"username"`
	Password        string `yaml:"password"`
	Host            string `yaml:"host"`
	Port            string `yaml:"port"`
	Engine          string `yaml:"engine"`
	DBName          string `yaml:"db_name"`
	Timeout         int    `yaml:"timeout"`
	MaxConnLifetime int    `yaml:"max_conn_lifetime"`
	MaxIdleTime     int    `yaml:"max_idle_time"`
	MaxOpenConn     int    `yaml:"max_open_conn"`
	MaxIdleConn     int    `yaml:"max_idle_conn"`
	ConnString      string `yaml:"conn_string"`
}

type SQLs added in v0.1.3

type SQLs struct {
	Master             SQLConfig `yaml:"master"`
	Follower           SQLConfig `yaml:"follower"`
	Timeout            int       `yaml:"timeout"`
	SlowQueryThreshold float64   `yaml:"slow_query_threshold"`
}

type SelectResponse

type SelectResponse struct {
	RequestParam  interface{} `json:"request_param"`
	Data          interface{} `json:"data"`
	TotalData     int64       `json:"total_data"`
	TotalFiltered int64       `json:"total_filtered"`
}

type TableRequest

type TableRequest struct {
	Keyword               string        `json:"keyword,omitempty" schema:"keyword"`
	SearchColsStr         string        `json:"search_cols,omitempty" schema:"search_cols"`
	SearchCols            []string      `json:"-"`
	Page                  int           `json:"page,omitempty" schema:"page"`
	Limit                 int           `json:"limit,omitempty" schema:"limit"`
	OrderBy               string        `json:"order_by,omitempty" schema:"order_by"`
	GroupBy               string        `json:"group_by,omitempty,omitempty" schema:"group_by"`
	CreatedStart          string        `json:"date_start,omitempty" schema:"date_start"`
	CreatedEnd            string        `json:"date_end,omitempty" schema:"date_end"`
	InitiateWhere         []string      `json:"-"` // will be defined manually at each of usecase services
	InitiateWhereValues   []interface{} `json:"-"` // will be defined manually at each of usecase services
	IncludeDeleted        bool          `json:"-"`
	NotContainsDeletedCol bool          `json:"-"`
	MainTableAlias        string        `json:"-"`
	IsDeleted             string        `json:"is_deleted,omitempty" schema:"is_deleted"`
}

func (*TableRequest) SetWhereCondition added in v0.0.3

func (req *TableRequest) SetWhereCondition(condition string, value ...interface{})

type TimeCol

type TimeCol struct {
	CreatedAt string      `json:"created_at" db:"created_at"`
	UpdatedAt null.String `json:"updated_at" db:"updated_at"`
	DeletedAt null.String `json:"deleted_at" db:"deleted_at"`
}

type Transaction added in v0.0.4

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

func NewTransaction added in v0.0.4

func NewTransaction(db *sqlx.DB) *Transaction

func (*Transaction) Begin added in v0.0.4

func (t *Transaction) Begin() (*sql.Tx, error)

func (*Transaction) Finish added in v0.0.4

func (t *Transaction) Finish(tx *sql.Tx, errTransaction error)

type Transactions added in v0.0.4

type Transactions interface {
	Begin() (*sql.Tx, error)
	Finish(tx *sql.Tx, errTransaction error)
}

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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