Documentation
¶
Overview ¶
Package mysql storage is designed to give lazy load singleton access to mysql connections it doesn't provide any cluster nor balancing support, assuming it is handled in lower level infra, i.e. proxy, cluster etc.
Index ¶
- Constants
- Variables
- func Init() error
- func Log(logMode bool, query string, args ...interface{})
- func New() error
- func Shutdown() (err error)
- type Config
- type Conn
- type LogWriter
- type Logger
- type SQL
- func (sql *SQL) Begin() (*Tx, error)
- func (sql *SQL) Exec(query string, args ...interface{}) (sql.Result, error)
- func (sql *SQL) Get(dest interface{}, query string, args ...interface{}) error
- func (sql *SQL) MustBegin() *Tx
- func (sql *SQL) Query(query string, args ...interface{}) (*sql.Rows, error)
- func (sql *SQL) QueryRow(query string, args ...interface{}) *sql.Row
- func (sql *SQL) QueryRowx(query string, args ...interface{}) *sqlx.Row
- func (sql *SQL) Queryx(query string, args ...interface{}) (*sqlx.Rows, error)
- func (sql *SQL) Select(dest interface{}, query string, args ...interface{}) error
- func (sql *SQL) WithContext(ctx context.Context) *SQL
- type SQLError
- type Tx
- func (db *Tx) Exec(query string, args ...interface{}) (sql.Result, error)
- func (db *Tx) Get(dest interface{}, query string, args ...interface{}) error
- func (db *Tx) Query(query string, args ...interface{}) (*sql.Rows, error)
- func (db *Tx) QueryRow(query string, args ...interface{}) *sql.Row
- func (db *Tx) QueryRowx(query string, args ...interface{}) *sqlx.Row
- func (db *Tx) Queryx(query string, args ...interface{}) (*sqlx.Rows, error)
- func (db *Tx) Select(dest interface{}, query string, args ...interface{}) error
Constants ¶
View Source
const ( ErrorCredential uint32 = 100 << iota ErrorConnection )
Variables ¶
View Source
var LogFormatter = func(values ...interface{}) (messages []interface{}) { if len(values) > 1 { var ( sql string formattedValues []string level = values[0] currentTime = "\n\033[33m[" + NowFunc().Format("2006-01-02 15:04:05") + "]\033[0m" source = fmt.Sprintf("\033[35m(%v)\033[0m", values[1]) ) messages = []interface{}{source, currentTime} if level != "sql" { messages = append(messages, "\033[31;1m") messages = append(messages, values[2:]...) messages = append(messages, "\033[0m") return } for _, value := range values[3].([]interface{}) { indirectValue := reflect.Indirect(reflect.ValueOf(value)) value = indirectValue.Interface() if !indirectValue.IsValid() { formattedValues = append(formattedValues, "NULL") } switch value.(type) { case time.Time: formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value.(time.Time).Format("2006-01-02 15:04:05"))) case []byte: if str := string(value.([]byte)); isPrintable(str) { formattedValues = append(formattedValues, fmt.Sprintf("'%v'", str)) } else { formattedValues = append(formattedValues, "'<binary>'") } case driver.Valuer: if value, err := value.(driver.Valuer).Value(); err == nil && value != nil { formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value)) } else { formattedValues = append(formattedValues, "NULL") } default: formattedValues = append(formattedValues, fmt.Sprintf("'%v'", value)) } } if numericPlaceHolderRegexp.MatchString(values[2].(string)) { sql = values[3].(string) for index, value := range formattedValues { placeholder := fmt.Sprintf(`\$%d([^\d]|$)`, index+1) sql = regexp.MustCompile(placeholder).ReplaceAllString(sql, value+"$1") } } else { formattedValuesLength := len(formattedValues) for index, value := range sqlRegexp.Split(values[2].(string), -1) { sql += value if index < formattedValuesLength { sql += formattedValues[index] } } } messages = append(messages, sql) } return }
LogFormatter : mysql log formatter
View Source
var NowFunc = func() time.Time { return time.Now() }
NowFunc : time func
Functions ¶
Types ¶
type Config ¶
type Config struct { User string Password string Address string DB string LogMode bool MaxOpen int MaxIdle int MaxLifetime int }
Config struct
type LogWriter ¶
type LogWriter interface {
Println(v ...interface{})
}
LogWriter log writer interface
type SQL ¶
SQL struct
func Read ¶
func Read() *SQL
Read retrieve MySQL established connection client (sqlx) and panic if error
func Write ¶
func Write() *SQL
Write retrieve MySQL established connection client (sqlx) and panic if error
type SQLError ¶
type SQLError struct { Errors uint32 // contains filtered or unexported fields }
func NewSQLError ¶
type Tx ¶
Tx struct
Click to show internal directories.
Click to hide internal directories.