Documentation ¶
Index ¶
- Constants
- Variables
- func CompatibleMySQLDs(s string) string
- func ContainsFold(a, b string) bool
- func ConvertSQLLines(lines []string) []string
- func CreateDao(dao interface{}, createDaoOpts ...CreateDaoOpter) error
- func FirstWord(s string) string
- func FixPkgName(pkgName string) string
- func IfElse(ifCondition bool, ifValue, elseValue string) string
- func ImplSQLScanner(t reflect.Type) bool
- func IsIPv6(str string) bool
- func IsQuerySQL(sql string) (string, bool)
- func LookupDriverName(driver driver.Driver) string
- func MapValueOrDefault(m map[string]string, key, defaultValue string) string
- func MySQLBindAddress(mysqlNet, bindAddress string, defaultDialer *net.Dialer) error
- func MySQLDump(db *sql.DB, writer io.Writer, specifiesTables ...string) error
- func ParseDotTag(line, prefix, mainTag string) (map[string]string, string)
- func ResolveIP(address string) (net.IP, error)
- func SetConnectionPool(db *sql.DB) *sql.DB
- func SplitSqls(sqls string, separate rune) []string
- func TrimSQL(s, delimiter string) string
- func ViperMySQLBindAddress() error
- 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 WithLogger(logger DaoLogger) CreateDaoOpter
- func WithRowScanInterceptor(interceptor RowScanInterceptor) CreateDaoOpter
- func WithSQLFile(sqlFile string) CreateDaoOpter
- func WithSQLStr(s string) CreateDaoOpter
- type DBGetter
- type DaoLogger
- type DaoLoggerNoop
- type DaoLogrus
- type DotSQL
- type DotSQLItem
- type DotSQLScanner
- type ExecOption
- type ExecResult
- type FieldPart
- type FieldParts
- type GetDBFn
- type IfCondition
- type IfPart
- type IfSQLPartParser
- type Limit
- type LiteralPart
- type More
- type MultiPart
- type MySQLDumper
- func (m *MySQLDumper) CreateTable(ct, ds, de *template.Template, writer io.Writer, name string) error
- func (m *MySQLDumper) CreateTableSQL(name string) (string, error)
- func (m *MySQLDumper) CreateTableValues(ds, de *template.Template, writer io.Writer, name string) error
- func (m *MySQLDumper) GetServerVersion() (string, error)
- func (m *MySQLDumper) GetTables() ([]string, error)
- type MySQLMore
- type NullAny
- type PostProcessingSQLPart
- type QueryReplacer
- type QueryReplacerFn
- type RowScanInterceptor
- type RowScanInterceptorFn
- type SQL
- type SQLExec
- type SQLMore
- type SQLParsed
- type SQLPart
- type SQLPartParser
- type StdDB
- type StructField
- type StructValue
- type Tag
- type Tags
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 )
const CreateTableTmpl = `` /* 258-byte string literal not displayed */
CreateTableTmpl defines the const string for the table template.
const MySQLDumpVersion = "0.2.2"
MySQLDumpVersion defines the version of mysqldump.
const TableDataTmplEnd = `;
/*!40000 ALTER TABLE {{ .Name }} ENABLE KEYS */;
UNLOCK TABLES;
`
TableDataTmplEnd defines the template for the table data ending.
const TableDataTmplStart = `` /* 158-byte string literal not displayed */
TableDataTmplStart defines the template for the table data starting.
Variables ¶
Functions ¶
func CompatibleMySQLDs ¶
CompatibleMySQLDs make mysql datasource be compatible with raw, mysql or gossh host format.
func ContainsFold ¶
ContainsFold tell if a contains b in case-insensitively.
func ConvertSQLLines ¶
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 FixPkgName ¶
FixPkgName fixes the package name to all lower case with letters and digits kept.
func ImplSQLScanner ¶
ImplSQLScanner tells t whether it implements sql.Scanner interface.
func IsQuerySQL ¶
IsQuerySQL tests a sql is a query or not.
func LookupDriverName ¶
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 ¶
MapValueOrDefault returns the value associated to the key, or return defaultValue when value does not exits or it is empty.
func MySQLBindAddress ¶
MySQLBindAddress bind client address.
func ParseDotTag ¶
ParseDotTag parses the tag like name:value age:34 adult to map returns the map and main tag's value.
func ViperMySQLBindAddress ¶
func ViperMySQLBindAddress() error
ViperMySQLBindAddress bind client address by viper flag bindAddress.
Types ¶
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 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.
type DotSQL ¶
type DotSQL struct {
Sqls map[string]DotSQLItem
}
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 DotSQLItem ¶
DotSQLItem tells the SQL details.
func (DotSQLItem) DynamicSQL ¶
func (d DotSQLItem) DynamicSQL() (SQLPart, error)
DynamicSQL returns the dynamic 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 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 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 compile the condition int advance.
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 ¶
MakeLiteralMultiPart makes a MultiPart.
type MySQLDumper ¶
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 (*MySQLMore) EnhanceGormDB ¶
EnhanceGormDB 增强GormDB.
func (*MySQLMore) EnhanceURI ¶
EnhanceURI 增强URI.
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 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 ¶
var ( // DB is the global sql.DB for convenience. DB *sql.DB // SQLReplacer is the global SQLReplacer. SQLReplacer QueryReplacer )
nolint:gochecknoglobals
type QueryReplacerFn ¶
func (QueryReplacerFn) ReplacerQuery ¶
func (d QueryReplacerFn) ReplacerQuery(query string) (string, error)
type RowScanInterceptor ¶
RowScanInterceptor defines the interceptor after a row scanning.
type RowScanInterceptorFn ¶
RowScanInterceptorFn defines the interceptor function after a row scanning.
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增强结构体.
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 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 ¶
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 StdDB ¶
type StdDB struct {
// contains filtered or unexported fields
}
StdDB is the wrapper for 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.
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.
type Tags ¶
Tags represent a set of tags from a single struct field.
func ParseTags ¶
ParseTags parses a single struct field tag and returns the set of tags. nolint:funlen,gocognit
func (Tags) Get ¶
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 ¶
GetOrDefault gets the value associated to the key or defaultValue is return when value is empty.