# README
splmysql
(This is beta) MySQL wrapper library to split single UPDATE query into many tiny transaction queries.
How to use
Create Runner instance
Create runner instance with connection information.
sr, err := splmysql.NewByOptions("DB Name", "HostName", 3306, "USER", "PASSWORD", "CharacterSet")
if err != nil {
return err
}
defer sr.Close()
You can use my.cnf.
sr, err := splmysql.NewByConf("DB Name", "path-to-my.cnf")
if err != nil {
return err
}
defer sr.Close()
NewByOptions
and NewByConf
do not establish any connections to the database.
They create connection information only.
Execute query
Create session object to get table information and test connection parameters. It's ok, execute query.
// Create session. Runner connect to DB and get table information.
// If connection information has invalid parameters, error returns.
sessionData, err := sr.NewSession(sql)
if err != nil {
return err
}
// Let's execute parallel
retrySessionData, err := sr.RunParallel(sessionData, numberOfParallel)
RunParallel()
returns Session object retrySessionData
to retry failed queries.
Fallback
If NewSession()
returns NoUsableColumnError
, you can run SimpleUpdate()
as fallback.
This method does not split query. Be careful to use Galera Cluster environment.
sess, err := sr.NewSession(sql)
if err != nil {
e := reflect.ValueOf(err).Elem()
switch {
case e.Type() == reflect.TypeOf(splmysql.NoUsableColumnError{}):
_, err := sr.SimpleUpdate(sql)
if err != nil {
return err
}
default:
return err
}
}
Logging
splmysql
is using Logrus logger.
You can overide it your own Logrus logger before call NewSession()
.
logger := logrus.New()
logger.Formatter = &logrus.TextFormatter{
FullTimestamp: true,
}
logger.Out = os.Stdout
// Overide Logger
sr.Logger = logger
# Functions
NewByConf makes DB connection with my.cnf and returns Runner object.
NewByOptions makes DB connection with options and returns Runner object.
NewInvalidUpdateQueryError create InvalidUpdateQueryError.
NewNoUsableColumnError create NoUsableColumnError.
NewResult returns struct of New Result.
# Constants
DefaultSplitRange is lower than 131072 (default limit value in Galera Cluster's 'wsrep_max_ws_rows').
NoUsableColumnErrorCode is the exit code of NoUsableColumnError.
LogDebugLevel is loglevel to log debug information level messages.
LogDefaultLevel is this library default level, use LogSuppressLevel.
LogErrorLevel is loglevel to log error level messages.
LogInfoLevel is loglevel to log information level messages.
LogSuppressLevel is loglevel of no logger messages.
LogTraceLevel is loglevel to log trace information level messages.
LogWarnLevel is loglevel to log warning and error level message.
NoUsableColumnErrorCode is the exit code of NoUsableColumnError.
# Variables
ErrorInterface is generic interface of splmysql errors.
# Structs
InvalidUpdateQueryError is the error that splmysql cannot treat.
NoUsableColumnError is the error that it found no usable column for split update in the table.
Result is struct of SQL execution result.
Runner includes splmysql global variables.
Session is a data of splmysql parallel execution.
SplError is generic struct of splmysql errors.
Transaction is single transaction data, equals to single SQL.