Documentation ¶
Index ¶
- Constants
- Variables
- func AnalyzeValuesFromBuckets(valueString string, cols []*model.ColumnInfo) ([]string, error)
- func CloseDB(db *sql.DB) error
- func ColumnName(column string) string
- func DecodeTimeInBucket(packedStr string) (string, error)
- func DeleteRows(ctx context.Context, db DBExecutor, schemaName string, tableName string, ...) error
- func ExecSQLWithRetry(ctx context.Context, db DBExecutor, sql string, args ...any) (err error)
- func ExecuteSQLs(ctx context.Context, db DBExecutor, sqls []string, args [][]any) error
- func FindAllColumnWithIndex(tableInfo *model.TableInfo) []*model.ColumnInfo
- func FindAllIndex(tableInfo *model.TableInfo) []*model.IndexInfo
- func FindColumnByName(cols []*model.ColumnInfo, name string) *model.ColumnInfo
- func FindSuitableColumnWithIndex(ctx context.Context, db QueryExecutor, schemaName string, ...) (*model.ColumnInfo, error)
- func FormatTimeZoneOffset(offset time.Duration) string
- func GetBucketsInfo(ctx context.Context, db QueryExecutor, schema, table string, ...) (map[string][]Bucket, error)
- func GetCRC32Checksum(ctx context.Context, db QueryExecutor, schemaName, tableName string, ...) (int64, error)
- func GetCreateTableSQL(ctx context.Context, db QueryExecutor, schemaName string, tableName string) (string, error)
- func GetDBVersion(ctx context.Context, db QueryExecutor) (string, error)
- func GetMinMaxValue(ctx context.Context, db QueryExecutor, schema, table, column string, ...) (minStr string, maxStr string, err error)
- func GetParserForDB(ctx context.Context, db QueryExecutor) (*parser.Parser, error)
- func GetRandomValues(ctx context.Context, db QueryExecutor, schemaName, table, column string, ...) ([]string, error)
- func GetRowCount(ctx context.Context, db QueryExecutor, schemaName string, tableName string, ...) (int64, error)
- func GetSQLMode(ctx context.Context, db QueryExecutor) (tmysql.SQLMode, error)
- func GetSchemas(ctx context.Context, db QueryExecutor) ([]string, error)
- func GetSessionVariable(ctx context.Context, db QueryExecutor, variable string) (value string, err error)
- func GetTables(ctx context.Context, db QueryExecutor, schemaName string) (tables []string, err error)
- func GetTidbLatestTSO(ctx context.Context, db QueryExecutor) (int64, error)
- func GetTimeZoneOffset(ctx context.Context, db QueryExecutor) (time.Duration, error)
- func GetViews(ctx context.Context, db QueryExecutor, schemaName string) (tables []string, err error)
- func IsFloatType(tp byte) bool
- func IsNumberType(tp byte) bool
- func IsRetryableError(err error) bool
- func IsTiDB(ctx context.Context, db QueryExecutor) (bool, error)
- func IsTimeTypeAndNeedDecode(tp byte) bool
- func OpenDB(cfg DBConfig, vars map[string]string) (*sql.DB, error)
- func ReplacePlaceholder(str string, args []string) string
- func ScanRow(rows *sql.Rows) (map[string]*ColumnData, error)
- func ScanRowsToInterfaces(rows *sql.Rows) ([][]any, error)
- func SelectUniqueOrderKey(tbInfo *model.TableInfo) ([]string, []*model.ColumnInfo)
- func ShowBinlogFormat(ctx context.Context, db QueryExecutor) (value string, err error)
- func ShowBinlogRowImage(ctx context.Context, db QueryExecutor) (value string, err error)
- func ShowGrants(ctx context.Context, db QueryExecutor, user, host string) ([]string, error)
- func ShowLogBin(ctx context.Context, db QueryExecutor) (value string, err error)
- func ShowMySQLVariable(ctx context.Context, db QueryExecutor, variable string) (value string, err error)
- func ShowServerID(ctx context.Context, db QueryExecutor) (serverID uint64, err error)
- func ShowVersion(ctx context.Context, db QueryExecutor) (value string, err error)
- func TableName(schema, table string) string
- type Bucket
- type ColumnData
- type DBConfig
- type DBExecutor
- type IndexInfo
- type QueryExecutor
Constants ¶
const ( // DefaultRetryTime is the default retry time to execute sql DefaultRetryTime = 10 // DefaultTimeout is the default timeout for execute sql DefaultTimeout time.Duration = 10 * time.Second // SlowLogThreshold defines the duration to log debug log of sql when exec time greater than SlowLogThreshold = 200 * time.Millisecond // DefaultDeleteRowsNum is the default rows num for delete one time DefaultDeleteRowsNum int64 = 100000 )
Variables ¶
var ( // ErrVersionNotFound means can't get the database's version ErrVersionNotFound = errors.New("can't get the database's version") // ErrNoData means no data in table ErrNoData = errors.New("no data found in table") )
var ( // Retryable1105Msgs list the error messages of some retryable error with `1105` code (`ErrUnknown`). Retryable1105Msgs = []string{ "Information schema is out of date", "Information schema is changed", } )
Functions ¶
func AnalyzeValuesFromBuckets ¶
func AnalyzeValuesFromBuckets(valueString string, cols []*model.ColumnInfo) ([]string, error)
AnalyzeValuesFromBuckets analyze upperBound or lowerBound to string for each column. upperBound and lowerBound are looks like '(123, abc)' for multiple fields, or '123' for one field.
func DecodeTimeInBucket ¶
DecodeTimeInBucket decodes Time from a packed uint64 value.
func DeleteRows ¶
func DeleteRows(ctx context.Context, db DBExecutor, schemaName string, tableName string, where string, args []any) error
DeleteRows delete rows in several times. Only can delete less than 300,000 one time in TiDB.
func ExecSQLWithRetry ¶
ExecSQLWithRetry executes sql with retry
func ExecuteSQLs ¶
ExecuteSQLs executes some sqls in one transaction
func FindAllColumnWithIndex ¶
func FindAllColumnWithIndex(tableInfo *model.TableInfo) []*model.ColumnInfo
FindAllColumnWithIndex returns columns with index, order is pk, uk and normal index.
func FindAllIndex ¶
FindAllIndex returns all index, order is pk, uk, and normal index.
func FindColumnByName ¶
func FindColumnByName(cols []*model.ColumnInfo, name string) *model.ColumnInfo
FindColumnByName finds column by name.
func FindSuitableColumnWithIndex ¶
func FindSuitableColumnWithIndex(ctx context.Context, db QueryExecutor, schemaName string, tableInfo *model.TableInfo) (*model.ColumnInfo, error)
FindSuitableColumnWithIndex returns first column of a suitable index. The priority is * primary key * unique key * normal index which has max cardinality
func FormatTimeZoneOffset ¶
FormatTimeZoneOffset is to format offset of timezone.
func GetBucketsInfo ¶
func GetBucketsInfo(ctx context.Context, db QueryExecutor, schema, table string, tableInfo *model.TableInfo) (map[string][]Bucket, error)
GetBucketsInfo SHOW STATS_BUCKETS in TiDB.
func GetCRC32Checksum ¶
func GetCRC32Checksum(ctx context.Context, db QueryExecutor, schemaName, tableName string, tbInfo *model.TableInfo, limitRange string, args []any) (int64, error)
GetCRC32Checksum returns checksum code of some data by given condition
func GetCreateTableSQL ¶
func GetCreateTableSQL(ctx context.Context, db QueryExecutor, schemaName string, tableName string) (string, error)
GetCreateTableSQL returns the create table statement.
func GetDBVersion ¶
func GetDBVersion(ctx context.Context, db QueryExecutor) (string, error)
GetDBVersion returns the database's version
func GetMinMaxValue ¶
func GetMinMaxValue(ctx context.Context, db QueryExecutor, schema, table, column string, limitRange string, limitArgs []any, collation string) (minStr string, maxStr string, err error)
GetMinMaxValue return min and max value of given column by specified limitRange condition.
func GetParserForDB ¶
GetParserForDB discovers ANSI_QUOTES in db's session variables and returns a proper parser
func GetRandomValues ¶
func GetRandomValues(ctx context.Context, db QueryExecutor, schemaName, table, column string, num int, limitRange string, limitArgs []any, collation string) ([]string, error)
GetRandomValues returns some random value. Tips: limitArgs is the value in limitRange.
func GetRowCount ¶
func GetRowCount(ctx context.Context, db QueryExecutor, schemaName string, tableName string, where string, args []any) (int64, error)
GetRowCount returns row count of the table. if not specify where condition, return total row count of the table.
func GetSQLMode ¶
GetSQLMode returns sql_mode.
func GetSchemas ¶
func GetSchemas(ctx context.Context, db QueryExecutor) ([]string, error)
GetSchemas returns name of all schemas
func GetSessionVariable ¶
func GetSessionVariable(ctx context.Context, db QueryExecutor, variable string) (value string, err error)
GetSessionVariable gets server's session variable, although argument is QueryExecutor, (session) system variables may be set through DSN
func GetTables ¶
func GetTables(ctx context.Context, db QueryExecutor, schemaName string) (tables []string, err error)
GetTables returns name of all tables in the specified schema
func GetTidbLatestTSO ¶
func GetTidbLatestTSO(ctx context.Context, db QueryExecutor) (int64, error)
GetTidbLatestTSO returns tidb's current TSO.
func GetTimeZoneOffset ¶
GetTimeZoneOffset is to get offset of timezone.
func GetViews ¶
func GetViews(ctx context.Context, db QueryExecutor, schemaName string) (tables []string, err error)
GetViews returns names of all views in the specified schema
func IsRetryableError ¶
IsRetryableError checks whether the SQL statement can be retry directly when encountering this error. NOTE: this should be compatible with different TiDB versions. some errors are only retryable in some special cases, then we mark it as un-retryable: - errno.ErrTiKVServerTimeout - errno.ErrTableLocked
some errors are un-retryable: - errno.ErrQueryInterrupted
some errors are unknown: - errno.ErrRegionUnavailable
func IsTiDB ¶
func IsTiDB(ctx context.Context, db QueryExecutor) (bool, error)
IsTiDB returns true if this database is tidb
func IsTimeTypeAndNeedDecode ¶
IsTimeTypeAndNeedDecode returns true if tp is time type and encoded in tidb buckets.
func ReplacePlaceholder ¶
ReplacePlaceholder will use args to replace '?', used for log. tips: make sure the num of "?" is same with len(args)
func ScanRow ¶
func ScanRow(rows *sql.Rows) (map[string]*ColumnData, error)
ScanRow scans rows into a map.
func ScanRowsToInterfaces ¶
ScanRowsToInterfaces scans rows to interface array.
func SelectUniqueOrderKey ¶
func SelectUniqueOrderKey(tbInfo *model.TableInfo) ([]string, []*model.ColumnInfo)
SelectUniqueOrderKey returns some columns for order by condition.
func ShowBinlogFormat ¶
func ShowBinlogFormat(ctx context.Context, db QueryExecutor) (value string, err error)
ShowBinlogFormat queries variable 'binlog_format' and returns its value.
func ShowBinlogRowImage ¶
func ShowBinlogRowImage(ctx context.Context, db QueryExecutor) (value string, err error)
ShowBinlogRowImage queries variable 'binlog_row_image' and returns its values.
func ShowGrants ¶
ShowGrants queries privileges for a mysql user. For mysql 8.0, if user has granted roles, ShowGrants also extract privilege from roles.
func ShowLogBin ¶
func ShowLogBin(ctx context.Context, db QueryExecutor) (value string, err error)
ShowLogBin queries variable 'log_bin' and returns its value.
func ShowMySQLVariable ¶
func ShowMySQLVariable(ctx context.Context, db QueryExecutor, variable string) (value string, err error)
ShowMySQLVariable queries MySQL variable and returns its value.
func ShowServerID ¶
func ShowServerID(ctx context.Context, db QueryExecutor) (serverID uint64, err error)
ShowServerID queries variable 'server_id' and returns its value.
func ShowVersion ¶
func ShowVersion(ctx context.Context, db QueryExecutor) (value string, err error)
ShowVersion queries variable 'version' and returns its value.
Types ¶
type ColumnData ¶
ColumnData saves column's data.
type DBConfig ¶
type DBConfig struct { Host string `toml:"host" json:"host"` User string `toml:"user" json:"user"` Password string `toml:"password" json:"-"` Schema string `toml:"schema" json:"schema"` Snapshot string `toml:"snapshot" json:"snapshot"` Port int `toml:"port" json:"port"` }
DBConfig is database configuration.
func GetDBConfigFromEnv ¶
GetDBConfigFromEnv returns DBConfig from environment
type DBExecutor ¶
type DBExecutor interface { QueryExecutor BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error) ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) }
DBExecutor is a interface for execute read and write statements from a database.
in generate the implement should be *sql.DB or *sql.Conn
type IndexInfo ¶
type IndexInfo struct { Table string KeyName string ColumnName string SeqInIndex int Cardinality int NoneUnique bool }
IndexInfo contains information of table index.
type QueryExecutor ¶
type QueryExecutor interface { QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error) QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row }
QueryExecutor is a interface for execute Query from a database.
in generate the implement should be *sql.DB or *sql.Conn
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package dbutiltest is a package for some common used methods for db related testing.
|
Package dbutiltest is a package for some common used methods for db related testing. |