Documentation ¶
Index ¶
- Constants
- Variables
- func CompatibleMySQLDs(s string) string
- func ConvertSQLLines(lines []string) []string
- func CreateDao(dao interface{}, createDaoOpts ...CreateDaoOpter) error
- func DetectDriverName(driverName, dataSourceName string) string
- func DriverName(d driver.Driver) string
- func GetQueryRows(dest interface{}) int
- func ImplSQLScanner(t reflect.Type) bool
- func IsIPv6(str string) bool
- func IsQuerySQL(query string) (string, bool)
- func LogSqlResult(lastResult sql.Result)
- func LogSqlResultDesc(desc string, lastResult sql.Result)
- func ParseDotTag(line, prefix, mainTag string) (map[string]string, string)
- func RegisterDriverName(d driver.Driver, driverName string)
- func ScanMapRow(rows *sql.Rows, columns []string) (map[string]string, error)
- func ScanRowValues(rows *sql.Rows) ([]interface{}, error)
- func ScanSliceRow(rows *sql.Rows, columns []string) ([]string, error)
- func ScanStringRow(rows *sql.Rows, columns []string) ([]string, error)
- func SplitSqls(sqls string, separate rune) []string
- func TrimSQL(s, delimiter string) string
- func Vars(keys ...interface{}) []interface{}
- func VarsStr(keys ...string) []interface{}
- func WithVars(vars ...interface{}) []interface{}
- type BeginTx
- type Col1Scanner
- type ContextKey
- type Count
- type CreateDaoOpt
- type CreateDaoOptFn
- type CreateDaoOpter
- func WithCtx(ctx context.Context) CreateDaoOpter
- func WithDB(db *sql.DB) CreateDaoOpter
- func WithError(err *error) CreateDaoOpter
- func WithLimit(maxRows int) CreateDaoOpter
- func WithRowScanInterceptor(interceptor RowScanInterceptor) CreateDaoOpter
- func WithSQLFile(sqlFile string) CreateDaoOpter
- func WithSQLStr(s string) CreateDaoOpter
- type CustomDriverValueConvertFn
- type CustomDriverValueConverter
- type DBGetter
- type DBRaw
- type DBTypeAware
- type DaoLog
- type DaoLogger
- type Dot
- type DotItem
- type DotSQL
- type DotScanner
- type ExecFn
- type ExecOption
- type Executable
- type FieldPart
- type FieldParts
- type IfCondition
- type IfPart
- type IfSQLPartParser
- type Limit
- type LiteralPart
- type MapScanner
- type MultiPart
- type NullAny
- type PostProcessingSQLPart
- type QueryArgs
- type QueryFn
- type QueryOption
- type QueryOptionFn
- type QueryOptionFns
- type Queryable
- type Result
- type RowScanInterceptor
- type RowScanInterceptorFn
- type RowScanner
- type RowScannerInit
- type SQL
- func (s *SQL) And(cond string, args ...interface{}) *SQL
- func (s *SQL) AndIf(ok bool, cond string, args ...interface{}) *SQL
- func (s *SQL) Append(sub string, args ...interface{}) *SQL
- func (s *SQL) AppendIf(ok bool, sub string, args ...interface{}) *SQL
- func (s SQL) CreateCount() (*SQL, error)
- func (s SQL) Query(db SqxDB, result interface{}, optionFns ...QueryOptionFn) error
- func (s SQL) QueryAsMap(db SqxDB, optionFns ...QueryOptionFn) (map[string]string, error)
- func (s SQL) QueryAsMaps(db SqxDB, optionFns ...QueryOptionFn) ([]map[string]string, error)
- func (s SQL) QueryAsNumber(db SqxDB) (int64, error)
- func (s SQL) QueryAsRow(db SqxDB, optionFns ...QueryOptionFn) ([]string, error)
- func (s SQL) QueryAsRows(db SqxDB, optionFns ...QueryOptionFn) ([][]string, error)
- func (s SQL) QueryAsString(db SqxDB) (string, error)
- func (s SQL) QueryRaw(db SqxDB, optionFns ...QueryOptionFn) error
- func (s SQL) Update(db SqxDB) (int64, error)
- func (s SQL) UpdateRaw(db SqxDB) (sql.Result, error)
- func (s *SQL) WithConvertOptions(convertOptions []sqlparser.ConvertOption) *SQL
- func (s *SQL) WithTimeout(timeout time.Duration) *SQL
- func (s *SQL) WithVars(vars ...interface{}) *SQL
- type SQLExec
- type SQLExecContext
- type SQLParsed
- type SQLPart
- type SQLPartParser
- type ScanRowFn
- type Sqx
- func (s *Sqx) Close() error
- func (s *Sqx) DoExec(arg *QueryArgs) (int64, error)
- func (s *Sqx) DoExecRaw(arg *QueryArgs) (sql.Result, error)
- func (s *Sqx) DoQuery(arg *QueryArgs) error
- func (s *Sqx) Exec(query string, args ...interface{}) (sql.Result, error)
- func (s *Sqx) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (s *Sqx) Get(dest interface{}, query string, args ...interface{}) error
- func (s Sqx) GetDBType() sqlparser.DBType
- func (s *Sqx) GetDesc(desc string, dest interface{}, query string, args ...interface{}) error
- func (s *Sqx) Query(query string, args ...interface{}) (*sql.Rows, error)
- func (s *Sqx) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
- func (s *Sqx) Select(dest interface{}, query string, args ...interface{}) error
- func (s *Sqx) SelectDesc(desc string, dest interface{}, query string, args ...interface{}) error
- func (s *Sqx) Tx(f func(sqx *Sqx) error) error
- func (s *Sqx) TxContext(ctx context.Context, f func(sqx *Sqx) error) error
- func (s *Sqx) Upsert(insertQuery, updateQuery string, args ...interface{}) (ur UpsertResult, err error)
- type SqxDB
- type StdDB
- type StringRowScanner
- type StructField
- type StructValue
- type UpsertResult
Constants ¶
const ( // ByNone means no bind params. ByNone bindBy = iota // ByAuto means auto seq for bind params. ByAuto // BySeq means specific seq for bind params. BySeq // ByName means named bind params. ByName )
Variables ¶
var ( LimitType = reflect.TypeOf((*Limit)(nil)).Elem() CountType = reflect.TypeOf((*Count)(nil)).Elem() )
var ( TypeInt64 = reflect.TypeOf(int64(0)) TypeUint64 = reflect.TypeOf(uint64(0)) TypeFloat64 = reflect.TypeOf(float64(0)) )
var CustomDriverValueConverters = map[reflect.Type]CustomDriverValueConverter{}
var DB *sql.DB
DB is the global sql.DB for convenience.
var ErrConditionKind = errors.New("condition kind should be struct or its pointer")
ErrConditionKind tells that the condition kind should be struct or its pointer
var ErrNotSelect = errors.New("not a select query statement")
ErrNotSelect shows an error that the query is not a select statement.
var ErrNotSupported = errors.New("sqx: Unsupported result type")
Functions ¶
func CompatibleMySQLDs ¶
CompatibleMySQLDs make mysql datasource be compatible with raw, mysql or gossh host format.
func ConvertSQLLines ¶
ConvertSQLLines converts the inline comments to line comments and merge to uncomment lines together.
func CreateDao ¶
func CreateDao(dao interface{}, createDaoOpts ...CreateDaoOpter) error
CreateDao fulfils the dao (should be pointer).
func DetectDriverName ¶
DetectDriverName detects the driver name for database source name.
func DriverName ¶
DriverName returns the driver name for the current db.
func GetQueryRows ¶
func GetQueryRows(dest interface{}) int
func ImplSQLScanner ¶
ImplSQLScanner tells t whether it implements sql.Scanner interface.
func IsQuerySQL ¶
IsQuerySQL tests a sql is a query or not.
func LogSqlResult ¶
func LogSqlResultDesc ¶
func ParseDotTag ¶
ParseDotTag parses the tag like name:value age:34 adult to map returns the map and main tag's value.
func RegisterDriverName ¶
RegisterDriverName register the driver name for the current db.
func ScanRowValues ¶
Types ¶
type Col1Scanner ¶
type CreateDaoOpt ¶
type CreateDaoOpt struct { Error *error Ctx context.Context QueryMaxRows int `default:"-1"` RowScanInterceptor RowScanInterceptor DotSQL func(name string) (SQLPart, error) Logger DaoLogger ErrSetter func(err error) DBGetter DBGetter }
CreateDaoOpt defines the options for CreateDao.
type CreateDaoOptFn ¶
type CreateDaoOptFn func(*CreateDaoOpt)
CreateDaoOptFn defines the func prototype to option applying.
func (CreateDaoOptFn) ApplyCreateOpt ¶
func (c CreateDaoOptFn) ApplyCreateOpt(opt *CreateDaoOpt)
ApplyCreateOpt applies the option.
type CreateDaoOpter ¶
type CreateDaoOpter interface {
ApplyCreateOpt(*CreateDaoOpt)
}
CreateDaoOpter defines the option pattern interface for CreateDaoOpt.
func WithCtx ¶
func WithCtx(ctx context.Context) CreateDaoOpter
WithCtx specifies the context.Context to sdb execution processes.
func WithError ¶
func WithError(err *error) CreateDaoOpter
WithError specifies the err pointer to receive error.
func WithLimit ¶
func WithLimit(maxRows int) CreateDaoOpter
WithLimit specifies the max rows to be fetched when execute query.
func WithRowScanInterceptor ¶
func WithRowScanInterceptor(interceptor RowScanInterceptor) CreateDaoOpter
WithRowScanInterceptor specifies the RowScanInterceptor after a row fetched.
func WithSQLFile ¶
func WithSQLFile(sqlFile string) CreateDaoOpter
WithSQLFile imports SQL queries from the file.
func WithSQLStr ¶
func WithSQLStr(s string) CreateDaoOpter
WithSQLStr imports SQL queries from the string.
type CustomDriverValueConvertFn ¶
type CustomDriverValueConvertFn func(value interface{}) (interface{}, error)
func (CustomDriverValueConvertFn) Convert ¶
func (fn CustomDriverValueConvertFn) Convert(value interface{}) (interface{}, error)
type CustomDriverValueConverter ¶
type CustomDriverValueConverter interface {
Convert(value interface{}) (interface{}, error)
}
type DBTypeAware ¶
type DaoLog ¶
type DaoLog struct{}
DaoLog implements the interface for dao logging with standard log.
type DaoLogger ¶
type DaoLogger interface { // LogError logs the error LogError(err error) // LogStart logs the sql before the sql execution LogStart(id, sql string, vars interface{}) }
DaoLogger is the interface for dao logging.
type DotItem ¶
DotItem tells the item details.
func (DotItem) DynamicSQL ¶
DynamicSQL returns the dynamic SQL.
type DotSQL ¶
DotSQL is the set of SQL statements.
func DotSQLLoad ¶
DotSQLLoad imports sql queries from any io.Reader.
func DotSQLLoadFile ¶
DotSQLLoadFile imports SQL queries from the file.
func DotSQLLoadString ¶
DotSQLLoadString imports SQL queries from the string.
type DotScanner ¶
type DotScanner struct {
// contains filtered or unexported fields
}
DotScanner scans the SQL statements from .sql files.
type ExecOption ¶
type Executable ¶
type FieldParts ¶
type FieldParts struct {
// contains filtered or unexported fields
}
func (*FieldParts) AddFieldSqlPart ¶
func (p *FieldParts) AddFieldSqlPart(part string, varVal []interface{}, joinedSep bool)
type IfCondition ¶
IfCondition defines a single condition that makes up a conditions-set for IfPart/SwitchPart.
type IfPart ¶
type IfPart struct { Conditions []IfCondition Else SQLPart }
IfPart is the part that has the format of if ... else if ... else ... end.
func (*IfPart) AddCondition ¶
AddCondition adds a condition to the IfPart.
type IfSQLPartParser ¶
IfSQLPartParser defines the Parser of IfPart.
func MakeIfSQLPartParser ¶
func MakeIfSQLPartParser(condition string) *IfSQLPartParser
MakeIfSQLPartParser makes a IfSQLPartParser.
type LiteralPart ¶
type LiteralPart struct {
Literal string
}
LiteralPart define literal SQL part that no eval required.
func (*LiteralPart) Compile ¶
func (p *LiteralPart) Compile() error
Compile compiles the condition int advance.
type MapScanner ¶
func (*MapScanner) Data0 ¶
func (s *MapScanner) Data0() map[string]string
type MultiPart ¶
type MultiPart struct {
Parts []SQLPart
}
MultiPart is the multi SQLParts.
func MakeLiteralMultiPart ¶
MakeLiteralMultiPart makes a MultiPart.
type NullAny ¶
NullAny represents any that may be null. NullAny implements the Scanner interface so it can be used as a scan destination.
func (*NullAny) Scan ¶
Scan assigns a value from a database driver.
The src value will be of one of the following types:
int64 float64 bool []byte string time.Time nil - for NULL values
An error should be returned if the value cannot be stored without loss of information.
Reference types such as []byte are only valid until the next call to Scan and should not be retained. Their underlying memory is owned by the driver. If retention is necessary, copy their values before the next call to Scan.
type PostProcessingSQLPart ¶
PostProcessingSQLPart defines the SQLPart for post-processing like delimiter trimming.
func (*PostProcessingSQLPart) Compile ¶
func (p *PostProcessingSQLPart) Compile() error
Compile compiles the condition int advance.
func (*PostProcessingSQLPart) Eval ¶
func (p *PostProcessingSQLPart) Eval(env map[string]interface{}) (string, error)
Eval evaluated the dynamic sql with env.
func (*PostProcessingSQLPart) Raw ¶
func (p *PostProcessingSQLPart) Raw() string
Raw returns the raw content.
type QueryArgs ¶
type QueryOption ¶
type QueryOption struct { MaxRows int TagNames []string Scanner RowScanner LowerColumnNames bool ConvertOptionOptions []sqlparser.ConvertOption }
QueryOption defines the query options.
type QueryOptionFn ¶
type QueryOptionFn func(o *QueryOption)
QueryOptionFn define the prototype function to set QueryOption.
func WithLowerColumnNames ¶
func WithLowerColumnNames(v bool) QueryOptionFn
WithLowerColumnNames set the LowerColumnNames of 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 Result ¶
type Result struct { Error error CostTime time.Duration Headers []string Rows [][]string RowsAffected int64 LastInsertID int64 IsQuerySQL bool FirstKey string }
Result defines the result structure of sql execution.
type RowScanInterceptor ¶
RowScanInterceptor defines the interceptor after a row scanning.
type RowScanInterceptorFn ¶
RowScanInterceptorFn defines the interceptor function after a row scanning.
type RowScanner ¶
type RowScannerInit ¶
type RowScannerInit interface {
InitRowScanner(columns []string)
}
type SQL ¶
type SQL struct { Name string Q string AppendQ string Vars []interface{} Ctx context.Context NoLog bool Timeout time.Duration Limit int ConvertOptions []sqlparser.ConvertOption // contains filtered or unexported fields }
SQL is a structure for query and vars.
func (SQL) CreateCount ¶
CreateCount creates a count query sql.
func (SQL) Query ¶
func (s SQL) Query(db SqxDB, result interface{}, optionFns ...QueryOptionFn) error
Query queries return with result.
func (SQL) QueryAsMap ¶
QueryAsMap query a single row as a map return.
func (SQL) QueryAsMaps ¶
QueryAsMaps query rows as map slice.
func (SQL) QueryAsNumber ¶
QueryAsNumber executes a query which only returns number like count(*) sql.
func (SQL) QueryAsRow ¶
func (s SQL) QueryAsRow(db SqxDB, optionFns ...QueryOptionFn) ([]string, error)
QueryAsRow query a single row as a string slice return.
func (SQL) QueryAsRows ¶
func (s SQL) QueryAsRows(db SqxDB, optionFns ...QueryOptionFn) ([][]string, error)
QueryAsRows query rows as [][]string.
func (SQL) QueryAsString ¶
QueryAsString executes a query which only returns number like count(*) sql.
func (SQL) QueryRaw ¶
func (s SQL) QueryRaw(db SqxDB, optionFns ...QueryOptionFn) error
QueryRaw query rows for customized row scanner.
func (*SQL) WithConvertOptions ¶
func (s *SQL) WithConvertOptions(convertOptions []sqlparser.ConvertOption) *SQL
WithConvertOptions set SQL conversion options.
func (*SQL) WithTimeout ¶
WithTimeout set sql execution timeout
type SQLExec ¶
type SQLExec interface { Exec(query string, args ...interface{}) (sql.Result, error) Query(query string, args ...interface{}) (*sql.Rows, error) }
SQLExec wraps Exec method.
type SQLExecContext ¶
type SQLParsed ¶
type SQLParsed struct { ID string SQL SQLPart BindBy bindBy Vars []string MaxSeq int IsQuery bool RawStmt string // contains filtered or unexported fields }
SQLParsed is the structure of the parsed SQL.
type SQLPart ¶
type SQLPart interface { // Compile compiles the condition int advance. Compile() error // Eval evaluates the SQL part to a real SQL. Eval(m map[string]interface{}) (string, error) // Raw returns the raw content. Raw() string }
SQLPart defines the dynamic SQL part.
func MakeLiteralPart ¶
MakeLiteralPart makes a MakeLiteralPart.
type SQLPartParser ¶
type SQLPartParser interface { // Parse parses the lines to SQLPart. Parse(lines []string) (partLines int, part SQLPart, err error) }
SQLPartParser defines the parser of SQLPart.
func CreateParser ¶
func CreateParser(word string, l string) SQLPartParser
CreateParser creates a SQLPartParser. If no parser found, nil returned.
type Sqx ¶
func (*Sqx) ExecContext ¶
func (*Sqx) QueryContext ¶
func (*Sqx) SelectDesc ¶
type SqxDB ¶
type SqxDB interface { SQLExec SQLExecContext }
type StdDB ¶
type StdDB struct {
// contains filtered or unexported fields
}
StdDB is the wrapper for sql.DBGetter.
type StringRowScanner ¶
func (*StringRowScanner) Data0 ¶
func (r *StringRowScanner) Data0() []string
type StructField ¶
type StructField struct { Parent *StructValue Field reflect.Value Index int StructField reflect.StructField Type reflect.Type Name string Tag reflect.StructTag Kind reflect.Kind PkgPath string }
StructField represents the information of a struct's field.
func (StructField) GetTag ¶
func (f StructField) GetTag(name string) string
GetTag returns the value associated with key in the tag string. If there is no such key in the tag, Get returns the empty string.
type StructValue ¶
type StructValue struct { StructSelf reflect.Value NumField int FieldTypes []reflect.StructField }
StructValue represents the structure for a struct.
func MakeStructValue ¶
func MakeStructValue(structSelf reflect.Value) *StructValue
MakeStructValue makes a StructValue by a struct's value.
func (*StructValue) FieldByIndex ¶
func (s *StructValue) FieldByIndex(index int) StructField
FieldByIndex return the StructField at index.
func (*StructValue) FieldByName ¶
func (s *StructValue) FieldByName(name string) (StructField, bool)
FieldByName returns the StructField which has the name.
func (*StructValue) FieldIndexByName ¶
func (s *StructValue) FieldIndexByName(name string) int
FieldIndexByName return's the index of field by its name. If the field is not found, FieldIndexByName returns -1.
type UpsertResult ¶
type UpsertResult int
const ( UpsertError UpsertResult = iota UpsertInserted UpsertUpdated )
func Upsert ¶
func Upsert(executable Executable, insertQuery, updateQuery string, args ...interface{}) (ur UpsertResult, err error)
func UpsertContext ¶
func UpsertContext(ctx context.Context, executable Executable, insertQuery, updateQuery string, args ...interface{}) (ur UpsertResult, err error)