sqlx

package module
v0.0.0-...-21e2f5b Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2021 License: MIT Imports: 36 Imported by: 4

README

sqlx

Travis CI Software License GoDoc Coverage Status goreport

more about golang db sql

数据库连接池增强

  1. MaxOpenConns:10
  2. MaxIdleConns:0
  3. ConnMaxLifetime:10s

MySQL增强

  1. 自动增强连接属性: charset=utf8mb4&parseTime=true&loc=Local&timeout=10s&writeTimeout=10s&readTimeout=10s
  2. 增强GormDB建表选项: db.Set("gorm:table_options", "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4")
  3. MySQLDump

Utilities

  1. ExecSQL
  2. SplitSqls

Resources

  1. Interceptors for database/sql
  2. Generate type safe Go from SQL sqlc.dev
  3. Golang SQL Database Layer for Layered Architecture. fs02.github.io/rel
  4. dbq Zero boilerplate database operations for Go

Documentation

Index

Constants

View Source
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
)
View Source
const CreateTableTmpl = `` /* 258-byte string literal not displayed */

CreateTableTmpl defines the const string for the table template.

View Source
const MySQLDumpVersion = "0.2.2"

MySQLDumpVersion defines the version of mysqldump.

View Source
const TableDataTmplEnd = `;

/*!40000 ALTER TABLE {{ .Name }} ENABLE KEYS */;
UNLOCK TABLES;

`

TableDataTmplEnd defines the template for the table data ending.

View Source
const TableDataTmplStart = `` /* 158-byte string literal not displayed */

TableDataTmplStart defines the template for the table data starting.

Variables

View Source
var (
	LimitType = reflect.TypeOf((*Limit)(nil)).Elem()
	CountType = reflect.TypeOf((*Count)(nil)).Elem()
)
View Source
var (
	// ErrTagSyntax ...
	ErrTagSyntax = errors.New("bad syntax for struct tag pair")
	// ErrTagKeySyntax ...
	ErrTagKeySyntax = errors.New("bad syntax for struct tag key")
	// ErrTagValueSyntax ...
	ErrTagValueSyntax = errors.New("bad syntax for struct tag value")
)

Functions

func CompatibleMySQLDs

func CompatibleMySQLDs(s string) string

CompatibleMySQLDs make mysql datasource be compatible with raw, mysql or gossh host format.

func ContainsFold

func ContainsFold(a, b string) bool

ContainsFold tell if a contains b in case-insensitively.

func ConvertSQLLines

func ConvertSQLLines(lines []string) []string

ConvertSQLLines converts the inline comments to line comments and merge the uncomment lines together. nolint:funlen

func CreateDao

func CreateDao(dao interface{}, createDaoOpts ...CreateDaoOpter) error

CreateDao fulfils the dao (should be pointer).

func FirstWord

func FirstWord(s string) string

FirstWord returns the first word of the SQL statement s.

func FixPkgName

func FixPkgName(pkgName string) string

FixPkgName fixes the package name to all lower case with letters and digits kept.

func IfElse

func IfElse(ifCondition bool, ifValue, elseValue string) string

IfElse if else ...

func ImplSQLScanner

func ImplSQLScanner(t reflect.Type) bool

ImplSQLScanner tells t whether it implements sql.Scanner interface.

func IsIPv6

func IsIPv6(str string) bool

IsIPv6 tests if the str is an IPv6 format.

func IsQuerySQL

func IsQuerySQL(sql string) (string, bool)

IsQuerySQL tests a sql is a query or not.

func LookupDriverName

func LookupDriverName(driver driver.Driver) string

LookupDriverName get driverName from the driver instance. The database/sql API doesn't provide a way to get the registry name for a driver from the driver type. from https://github.com/golang/go/issues/12600

func MapValueOrDefault

func MapValueOrDefault(m map[string]string, key, defaultValue string) string

MapValueOrDefault returns the value associated to the key, or return defaultValue when value does not exits or it is empty.

func MySQLBindAddress

func MySQLBindAddress(mysqlNet, bindAddress string, defaultDialer *net.Dialer) error

MySQLBindAddress bind client address.

func MySQLDump

func MySQLDump(db *sql.DB, writer io.Writer, specifiesTables ...string) error

MySQLDump creates a MYSQL Dump based on the options supplied through the dumper.

func ParseDotTag

func ParseDotTag(line, prefix, mainTag string) (map[string]string, string)

ParseDotTag parses the tag like name:value age:34 adult to map returns the map and main tag's value.

func ResolveIP

func ResolveIP(address string) (net.IP, error)

ResolveIP resolves the address to IP.

func SetConnectionPool

func SetConnectionPool(db *sql.DB) *sql.DB

SetConnectionPool 设置连接池常见属性.

func SplitSqls

func SplitSqls(sqls string, separate rune) []string

SplitSqls splits sqls by separate.

func TrimSQL

func TrimSQL(s, delimiter string) string

TrimSQL trims the delimiter from the string s.

func ViperMySQLBindAddress

func ViperMySQLBindAddress() error

ViperMySQLBindAddress bind client address by viper flag bindAddress.

Types

type Count

type Count int64

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 WithDB

func WithDB(db *sql.DB) CreateDaoOpter

WithDB imports a db.

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 WithLogger

func WithLogger(logger DaoLogger) CreateDaoOpter

WithLogger specifies dao logger.

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 DBGetter

type DBGetter interface{ GetDB() *sql.DB }

DBGetter is the interface to get a sql.DBGetter.

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 DaoLoggerNoop

type DaoLoggerNoop struct{}

DaoLoggerNoop implements the interface for dao logging with NOOP.

func (*DaoLoggerNoop) LogError

func (d *DaoLoggerNoop) LogError(err error)

LogError logs the error.

func (*DaoLoggerNoop) LogStart

func (d *DaoLoggerNoop) LogStart(id, sql string, vars interface{})

LogStart logs the sql before the sql execution.

type DaoLogrus

type DaoLogrus struct{}

DaoLogrus implements the interface for dao logging with logrus.

func (*DaoLogrus) LogError

func (d *DaoLogrus) LogError(err error)

LogError logs the error.

func (*DaoLogrus) LogStart

func (d *DaoLogrus) LogStart(id, sql string, vars interface{})

LogStart logs the sql before the sql execution.

type DotSQL

type DotSQL struct {
	Sqls map[string]DotSQLItem
}

DotSQL is the set of SQL statements.

func DotSQLLoad

func DotSQLLoad(r io.Reader) (*DotSQL, error)

DotSQLLoad imports sql queries from any io.Reader.

func DotSQLLoadFile

func DotSQLLoadFile(sqlFile string) (*DotSQL, error)

DotSQLLoadFile imports SQL queries from the file.

func DotSQLLoadString

func DotSQLLoadString(s string) (*DotSQL, error)

DotSQLLoadString imports SQL queries from the string.

func (DotSQL) Raw

func (d DotSQL) Raw(name string) (SQLPart, error)

Raw returns the query, everything after the --name tag.

type DotSQLItem

type DotSQLItem struct {
	Content []string
	Name    string
	Attrs   map[string]string
}

DotSQLItem tells the SQL details.

func (DotSQLItem) DynamicSQL

func (d DotSQLItem) DynamicSQL() (SQLPart, error)

DynamicSQL returns the dynamic SQL.

func (DotSQLItem) RawSQL

func (d DotSQLItem) RawSQL() string

RawSQL returns the raw SQL.

type DotSQLScanner

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

DotSQLScanner scans the SQL statements from .sql files.

func (*DotSQLScanner) Run

func (s *DotSQLScanner) Run(io *bufio.Scanner) map[string]DotSQLItem

Run runs the scanner.

type ExecOption

type ExecOption struct {
	MaxRows     int
	NullReplace string
	BlobReplace string
}

type ExecResult

type ExecResult struct {
	Error        error
	CostTime     time.Duration
	Headers      []string
	Rows         [][]string
	RowsAffected int64
	LastInsertID int64
	IsQuerySQL   bool
	FirstKey     string
}

ExecResult defines the result structure of sql execution.

func ExecSQL

func ExecSQL(db SQLExec, sqlStr string, option ExecOption) ExecResult

ExecSQL executes a SQL.

type FieldPart

type FieldPart struct {
	PartSQL        string
	BindVal        []interface{}
	PartSQLPlTimes int
	JoinedSep      bool
}

func (FieldPart) VarMarks

func (p FieldPart) VarMarks() []string

func (FieldPart) Vars

func (p FieldPart) Vars() []interface{}

type FieldParts

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

func (*FieldParts) AddFieldSqlPart

func (p *FieldParts) AddFieldSqlPart(part string, varVal []interface{}, joinedSep bool)

type GetDBFn

type GetDBFn func() *sql.DB

GetDBFn is the function type to get a sql.DBGetter.

func (GetDBFn) GetDB

func (f GetDBFn) GetDB() *sql.DB

GetDB returns a sql.DBGetter.

type IfCondition

type IfCondition struct {
	Expr         string
	CompiledExpr *vm.Program
	Part         SQLPart
}

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 MakeIfPart

func MakeIfPart() *IfPart

MakeIfPart makes a new IfPart.

func (*IfPart) AddCondition

func (p *IfPart) AddCondition(conditionExpr string, part SQLPart)

AddCondition adds a condition to the IfPart.

func (*IfPart) AddElse

func (p *IfPart) AddElse(part SQLPart)

AddElse adds a else part to the IfPart.

func (*IfPart) Compile

func (p *IfPart) Compile() (err error)

Compile compile the condition int advance.

func (*IfPart) Eval

func (p *IfPart) Eval(env map[string]interface{}) (string, error)

Eval evaluates the SQL part to a real SQL.

func (*IfPart) Raw

func (p *IfPart) Raw() string

Raw returns the raw content.

type IfSQLPartParser

type IfSQLPartParser struct {
	Condition string
	Else      string
}

IfSQLPartParser defines the Parser of IfPart.

func MakeIfSQLPartParser

func MakeIfSQLPartParser(condition string) *IfSQLPartParser

MakeIfSQLPartParser makes a IfSQLPartParser.

func (*IfSQLPartParser) Parse

func (p *IfSQLPartParser) Parse(lines []string) (partLines int, part SQLPart, err error)

Parse parses the lines to SQLPart.

type Limit

type Limit struct {
	Offset int64
	Length int64
}

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 compile the condition int advance.

func (*LiteralPart) Eval

func (p *LiteralPart) Eval(map[string]interface{}) (string, error)

Eval evaluates the SQL part to a real SQL.

func (*LiteralPart) Raw

func (p *LiteralPart) Raw() string

Raw returns the raw content.

type More

type More interface {
	// Matches 是否匹配当前实现
	Matches() bool
	// EnhanceURI 增强URI
	EnhanceURI(dbURI string) string
	// EnhanceGormDB 增强GormDB
	EnhanceGormDB(db *gorm.DB) *gorm.DB
}

More 为各个数据库增强的接口.

type MultiPart

type MultiPart struct {
	Parts []SQLPart
}

MultiPart is the multi SQLParts.

func MakeLiteralMultiPart

func MakeLiteralMultiPart(l string) *MultiPart

MakeLiteralMultiPart makes a MultiPart.

func MakeMultiPart

func MakeMultiPart() *MultiPart

MakeMultiPart makes MultiPart.

func (*MultiPart) AddPart

func (p *MultiPart) AddPart(part SQLPart)

AddPart adds a part to the current MultiPart.

func (*MultiPart) Compile

func (p *MultiPart) Compile() error

Compile compile the condition int advance.

func (*MultiPart) Eval

func (p *MultiPart) Eval(env map[string]interface{}) (string, error)

Eval evaluates the SQL part to a real SQL.

func (*MultiPart) Raw

func (p *MultiPart) Raw() string

Raw returns the raw content.

type MySQLDumper

type MySQLDumper struct {
	Sdb *sql.DB
}

MySQLDumper is the structure of dumping.

func (*MySQLDumper) CreateTable

func (m *MySQLDumper) CreateTable(ct, ds, de *template.Template, writer io.Writer, name string) error

CreateTable createa a table.

func (*MySQLDumper) CreateTableSQL

func (m *MySQLDumper) CreateTableSQL(name string) (string, error)

CreateTableSQL creates a SQL statement to create a table.

func (*MySQLDumper) CreateTableValues

func (m *MySQLDumper) CreateTableValues(ds, de *template.Template, writer io.Writer, name string) error

CreateTableValues ... nolint:funlen

func (*MySQLDumper) GetServerVersion

func (m *MySQLDumper) GetServerVersion() (string, error)

GetServerVersion get the server version.

func (*MySQLDumper) GetTables

func (m *MySQLDumper) GetTables() ([]string, error)

GetTables returns tables.

type MySQLMore

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

MySQLMore MySQL增强器.

func NewMySQLMore

func NewMySQLMore(dbDriver string) *MySQLMore

NewMySQLMore 新建MySQL增强器.

func (*MySQLMore) EnhanceGormDB

func (m *MySQLMore) EnhanceGormDB(db *gorm.DB) *gorm.DB

EnhanceGormDB 增强GormDB.

func (*MySQLMore) EnhanceURI

func (m *MySQLMore) EnhanceURI(dbURI string) string

EnhanceURI 增强URI.

func (*MySQLMore) Matches

func (m *MySQLMore) Matches() bool

Matches 是否匹配当前实现.

type NullAny

type NullAny struct {
	Type reflect.Type
	Val  reflect.Value
}

NullAny represents any that may be null. NullAny implements the Scanner interface so it can be used as a scan destination.

func (*NullAny) Scan

func (n *NullAny) Scan(value interface{}) error

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

type PostProcessingSQLPart struct {
	Part  SQLPart
	Attrs map[string]string
}

PostProcessingSQLPart defines the SQLPart for post-processing like delimiter trimming.

func (*PostProcessingSQLPart) Compile

func (p *PostProcessingSQLPart) Compile() error

Compile compile 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 QueryReplacer

type QueryReplacer interface {
	ReplacerQuery(query string) (string, error)
}
var (
	// DB is the global sql.DB for convenience.
	DB *sql.DB
	// SQLReplacer is the global SQLReplacer.
	SQLReplacer QueryReplacer
)

nolint:gochecknoglobals

type QueryReplacerFn

type QueryReplacerFn func(query string) (string, error)

func (QueryReplacerFn) ReplacerQuery

func (d QueryReplacerFn) ReplacerQuery(query string) (string, error)

type RowScanInterceptor

type RowScanInterceptor interface {
	After(rowIndex int, v ...interface{}) (bool, error)
}

RowScanInterceptor defines the interceptor after a row scanning.

type RowScanInterceptorFn

type RowScanInterceptorFn func(rowIndex int, v ...interface{}) (bool, error)

RowScanInterceptorFn defines the interceptor function after a row scanning.

func (RowScanInterceptorFn) After

func (r RowScanInterceptorFn) After(rowIndex int, v ...interface{}) (bool, error)

After is revoked after after a row scanning.

type SQL

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

	CountQuery string
	CuntVars   []interface{}
}

type SQLExec

type SQLExec interface {
	// Exec execute query.
	Exec(query string, args ...interface{}) (sql.Result, error)
	Query(query string, args ...interface{}) (*sql.Rows, error)
}

SQLExec wraps Exec method.

type SQLMore

type SQLMore struct {

	// DbDriver 原始驱动名称
	Driver string
	// EnhancedDbURI 增强后的URI
	EnhancedURI string
	// contains filtered or unexported fields
}

SQLMore SQL增强结构体.

func NewSQLMore

func NewSQLMore(dbDriver, dbURI string) *SQLMore

NewSQLMore 创建SQL增强器.

func (*SQLMore) GormOpen

func (s *SQLMore) GormOpen() *gorm.DB

GormOpen 确保打开新的Gorm数据库连接池对象.

func (*SQLMore) GormOpenE

func (s *SQLMore) GormOpenE() (*gorm.DB, error)

GormOpenE 打开新的Gorm数据库连接池对象.

func (*SQLMore) Open

func (s *SQLMore) Open() *sql.DB

Open 确保打开新的数据库连接池对象.

func (*SQLMore) OpenE

func (s *SQLMore) OpenE() (*sql.DB, error)

OpenE 打开新的数据库连接池对象.

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.

func ParseSQL

func ParseSQL(name, stmt string) (*SQLParsed, error)

ParseSQL parses the sql.

type SQLPart

type SQLPart interface {
	// Compile compile 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

func MakeLiteralPart(s string) SQLPart

MakeLiteralPart makes a MakeLiteralPart.

func ParseDynamicSQL

func ParseDynamicSQL(lines []string, terminators ...string) (int, SQLPart, error)

ParseDynamicSQL parses the dynamic sqls to structured SQLPart.

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 StdDB

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

StdDB is the wrapper for sql.DBGetter.

func MakeDB

func MakeDB(db *sql.DB) *StdDB

MakeDB makes a new StdDB from sql.DBGetter.

func (StdDB) GetDB

func (f StdDB) GetDB() *sql.DB

GetDB returns a sql.DBGetter.

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.

func (StructField) GetTagOr

func (f StructField) GetTagOr(tagName string, defaultValue string) string

GetTagOr returns the tag's value of the field or defaultValue when tag is empty.

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 Tag

type Tag struct {
	// Key is the tag key, such as json, xml, etc..
	// i.e: `json:"foo,omitempty". Here key is: "json"
	Key string

	// Name is a part of the value
	// i.e: `json:"foo,omitempty". Here value is: "foo,omitempty"
	Value string
}

Tag defines a single struct's string literal tag.

func (Tag) GoString

func (t Tag) GoString() string

GoString implements the fmt.GoStringer interface.

func (Tag) String

func (t Tag) String() string

String reassembles the tag into a valid tag field representation.

type Tags

type Tags map[string]Tag

Tags represent a set of tags from a single struct field.

func ParseTags

func ParseTags(tag string) (Tags, error)

ParseTags parses a single struct field tag and returns the set of tags. nolint:funlen,gocognit

func (Tags) Get

func (t Tags) Get(key string) string

Get returns the tag associated with the given key. If the key is present in the tag the value (which may be empty) is returned. Otherwise the returned value will be the empty string. The ok return value reports whether the tag exists or not (which the return value is nil).

func (Tags) GetOrDefault

func (t Tags) GetOrDefault(key, defaultValue string) string

GetOrDefault gets the value associated to the key or defaultValue is return when value is empty.

func (Tags) Keys

func (t Tags) Keys() []string

Keys returns a sorted slice of tag keys.

func (Tags) Map

func (t Tags) Map() map[string]string

Map convert tags to map[string]string.

func (Tags) String

func (t Tags) String() string

String reassembles the tags into a valid literal tag field representation.

Directories

Path Synopsis
cmd
sqlxx
nolint:errcheck
nolint:errcheck

Jump to

Keyboard shortcuts

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