sqx

package
v0.0.0-...-2e9c582 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2021 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	TypeInt64   = reflect.TypeOf(int64(0))
	TypeUint64  = reflect.TypeOf(uint64(0))
	TypeFloat64 = reflect.TypeOf(float64(0))
)
View Source
var ErrConditionKind = errors.New("condition kind should be struct or its pointer")

ErrConditionKind tells that the condition kind should be struct or its pointer

View Source
var ErrNotSelect = errors.New("not a select query statement")

ErrNotSelect shows an error that the query is not a select statement.

Functions

func WithVars

func WithVars(vars ...interface{}) []interface{}

WithVars replace vars.

Types

type QueryOption

type QueryOption struct {
	MaxRows  int
	TagNames []string
	Scanner  RowScanner
}

QueryOption defines the query options.

type QueryOptionFn

type QueryOptionFn func(o *QueryOption)

QueryOptionFn define the prototype function to set QueryOption.

func WithMaxRows

func WithMaxRows(maxRows int) QueryOptionFn

WithMaxRows set the max rows of QueryOption.

func WithOptions

func WithOptions(v *QueryOption) QueryOptionFn

WithOptions apply the query option directly.

func WithRowScanner

func WithRowScanner(v RowScanner) QueryOptionFn

WithRowScanner set row scanner for the query result.

func WithScanRow

func WithScanRow(v ScanRowFn) QueryOptionFn

WithScanRow set row scanner for the query result.

func WithTagNames

func WithTagNames(tagNames ...string) QueryOptionFn

WithTagNames set the tagNames for mapping struct fields to query columns.

type QueryOptionFns

type QueryOptionFns []QueryOptionFn

QueryOptionFns is the slice of QueryOptionFn.

func (QueryOptionFns) Options

func (q QueryOptionFns) Options() *QueryOption

type RowScanner

type RowScanner interface {
	ScanRow(rows *sql.Rows, rowIndex int, columns []string) (bool, error)
}

type SQL

type SQL struct {
	Query string
	Vars  []interface{}
}

SQL is a structure for query and vars.

func CreateSQL

func CreateSQL(base string, cond interface{}) (*SQL, error)

CreateSQL creates a composite SQL on base and condition cond.

func NewSQL

func NewSQL(query string, vars ...interface{}) *SQL

NewSQL create s SQL object.

func (*SQL) Append

func (s *SQL) Append(sub string, args ...interface{}) *SQL

Append apppends sub statement to the query.

func (SQL) CreateCount

func (s SQL) CreateCount() (*SQL, error)

CreateCount creates a count query sql.

func (SQL) QueryAsBeans

func (s SQL) QueryAsBeans(db *sql.DB, result interface{}, optionFns ...QueryOptionFn) error

QueryAsBeans query return with result.

func (SQL) QueryAsMap

func (s SQL) QueryAsMap(db *sql.DB, optionFns ...QueryOptionFn) (map[string]string, error)

QueryAsMap query a single row as a map return.

func (SQL) QueryAsMaps

func (s SQL) QueryAsMaps(db *sql.DB, optionFns ...QueryOptionFn) ([]map[string]string, error)

QueryAsMaps query rows as map slice.

func (SQL) QueryAsNumber

func (s SQL) QueryAsNumber(db *sql.DB) (int64, error)

QueryAsNumber executes a query which only returns number like count(*) sql.

func (SQL) QueryAsRow

func (s SQL) QueryAsRow(db *sql.DB, optionFns ...QueryOptionFn) ([]string, error)

QueryAsRow query a single row as a string slice return.

func (SQL) QueryAsRows

func (s SQL) QueryAsRows(db *sql.DB, optionFns ...QueryOptionFn) ([][]string, error)

QueryAsRows query rows as [][]string.

func (SQL) QueryAsString

func (s SQL) QueryAsString(db *sql.DB) (string, error)

QueryAsNumber executes a query which only returns number like count(*) sql.

func (SQL) QueryRaw

func (s SQL) QueryRaw(db *sql.DB, optionFns ...QueryOptionFn) error

QueryRaw query rows for customized row scanner.

func (SQL) Update

func (s SQL) Update(db *sql.DB) (int64, error)

Update executes an update/delete query and returns rows affected.

func (*SQL) WithVars

func (s *SQL) WithVars(vars ...interface{}) *SQL

WithVars replace vars.

type ScanRowFn

type ScanRowFn func(rows *sql.Rows, rowIndex int, columns []string) (bool, error)

func (ScanRowFn) ScanRow

func (s ScanRowFn) ScanRow(rows *sql.Rows, rowIndex int, columns []string) (bool, error)

Jump to

Keyboard shortcuts

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