2016-11-04 06:16:01 +08:00
|
|
|
// Copyright 2015 The Xorm Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
package dialects
|
2016-11-04 06:16:01 +08:00
|
|
|
|
|
|
|
import (
|
2020-03-22 23:12:55 +08:00
|
|
|
"context"
|
2016-11-04 06:16:01 +08:00
|
|
|
"crypto/tls"
|
2017-04-07 09:47:25 +08:00
|
|
|
"errors"
|
2016-11-04 06:16:01 +08:00
|
|
|
"fmt"
|
2017-04-07 09:47:25 +08:00
|
|
|
"regexp"
|
2016-11-04 06:16:01 +08:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
"xorm.io/xorm/core"
|
|
|
|
"xorm.io/xorm/schemas"
|
2016-11-04 06:16:01 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
mysqlReservedWords = map[string]bool{
|
|
|
|
"ADD": true,
|
|
|
|
"ALL": true,
|
|
|
|
"ALTER": true,
|
|
|
|
"ANALYZE": true,
|
|
|
|
"AND": true,
|
|
|
|
"AS": true,
|
|
|
|
"ASC": true,
|
|
|
|
"ASENSITIVE": true,
|
|
|
|
"BEFORE": true,
|
|
|
|
"BETWEEN": true,
|
|
|
|
"BIGINT": true,
|
|
|
|
"BINARY": true,
|
|
|
|
"BLOB": true,
|
|
|
|
"BOTH": true,
|
|
|
|
"BY": true,
|
|
|
|
"CALL": true,
|
|
|
|
"CASCADE": true,
|
|
|
|
"CASE": true,
|
|
|
|
"CHANGE": true,
|
|
|
|
"CHAR": true,
|
|
|
|
"CHARACTER": true,
|
|
|
|
"CHECK": true,
|
|
|
|
"COLLATE": true,
|
|
|
|
"COLUMN": true,
|
|
|
|
"CONDITION": true,
|
|
|
|
"CONNECTION": true,
|
|
|
|
"CONSTRAINT": true,
|
|
|
|
"CONTINUE": true,
|
|
|
|
"CONVERT": true,
|
|
|
|
"CREATE": true,
|
|
|
|
"CROSS": true,
|
|
|
|
"CURRENT_DATE": true,
|
|
|
|
"CURRENT_TIME": true,
|
|
|
|
"CURRENT_TIMESTAMP": true,
|
|
|
|
"CURRENT_USER": true,
|
|
|
|
"CURSOR": true,
|
|
|
|
"DATABASE": true,
|
|
|
|
"DATABASES": true,
|
|
|
|
"DAY_HOUR": true,
|
|
|
|
"DAY_MICROSECOND": true,
|
|
|
|
"DAY_MINUTE": true,
|
|
|
|
"DAY_SECOND": true,
|
|
|
|
"DEC": true,
|
|
|
|
"DECIMAL": true,
|
|
|
|
"DECLARE": true,
|
|
|
|
"DEFAULT": true,
|
|
|
|
"DELAYED": true,
|
|
|
|
"DELETE": true,
|
|
|
|
"DESC": true,
|
|
|
|
"DESCRIBE": true,
|
|
|
|
"DETERMINISTIC": true,
|
|
|
|
"DISTINCT": true,
|
|
|
|
"DISTINCTROW": true,
|
|
|
|
"DIV": true,
|
|
|
|
"DOUBLE": true,
|
|
|
|
"DROP": true,
|
|
|
|
"DUAL": true,
|
|
|
|
"EACH": true,
|
|
|
|
"ELSE": true,
|
|
|
|
"ELSEIF": true,
|
|
|
|
"ENCLOSED": true,
|
|
|
|
"ESCAPED": true,
|
|
|
|
"EXISTS": true,
|
|
|
|
"EXIT": true,
|
|
|
|
"EXPLAIN": true,
|
|
|
|
"FALSE": true,
|
|
|
|
"FETCH": true,
|
|
|
|
"FLOAT": true,
|
|
|
|
"FLOAT4": true,
|
|
|
|
"FLOAT8": true,
|
|
|
|
"FOR": true,
|
|
|
|
"FORCE": true,
|
|
|
|
"FOREIGN": true,
|
|
|
|
"FROM": true,
|
|
|
|
"FULLTEXT": true,
|
|
|
|
"GOTO": true,
|
|
|
|
"GRANT": true,
|
|
|
|
"GROUP": true,
|
|
|
|
"HAVING": true,
|
|
|
|
"HIGH_PRIORITY": true,
|
|
|
|
"HOUR_MICROSECOND": true,
|
|
|
|
"HOUR_MINUTE": true,
|
|
|
|
"HOUR_SECOND": true,
|
|
|
|
"IF": true,
|
|
|
|
"IGNORE": true,
|
|
|
|
"IN": true, "INDEX": true,
|
|
|
|
"INFILE": true, "INNER": true, "INOUT": true,
|
|
|
|
"INSENSITIVE": true, "INSERT": true, "INT": true,
|
|
|
|
"INT1": true, "INT2": true, "INT3": true,
|
|
|
|
"INT4": true, "INT8": true, "INTEGER": true,
|
|
|
|
"INTERVAL": true, "INTO": true, "IS": true,
|
|
|
|
"ITERATE": true, "JOIN": true, "KEY": true,
|
|
|
|
"KEYS": true, "KILL": true, "LABEL": true,
|
|
|
|
"LEADING": true, "LEAVE": true, "LEFT": true,
|
|
|
|
"LIKE": true, "LIMIT": true, "LINEAR": true,
|
|
|
|
"LINES": true, "LOAD": true, "LOCALTIME": true,
|
|
|
|
"LOCALTIMESTAMP": true, "LOCK": true, "LONG": true,
|
|
|
|
"LONGBLOB": true, "LONGTEXT": true, "LOOP": true,
|
|
|
|
"LOW_PRIORITY": true, "MATCH": true, "MEDIUMBLOB": true,
|
|
|
|
"MEDIUMINT": true, "MEDIUMTEXT": true, "MIDDLEINT": true,
|
|
|
|
"MINUTE_MICROSECOND": true, "MINUTE_SECOND": true, "MOD": true,
|
|
|
|
"MODIFIES": true, "NATURAL": true, "NOT": true,
|
|
|
|
"NO_WRITE_TO_BINLOG": true, "NULL": true, "NUMERIC": true,
|
|
|
|
"ON OPTIMIZE": true, "OPTION": true,
|
|
|
|
"OPTIONALLY": true, "OR": true, "ORDER": true,
|
|
|
|
"OUT": true, "OUTER": true, "OUTFILE": true,
|
|
|
|
"PRECISION": true, "PRIMARY": true, "PROCEDURE": true,
|
|
|
|
"PURGE": true, "RAID0": true, "RANGE": true,
|
|
|
|
"READ": true, "READS": true, "REAL": true,
|
|
|
|
"REFERENCES": true, "REGEXP": true, "RELEASE": true,
|
|
|
|
"RENAME": true, "REPEAT": true, "REPLACE": true,
|
|
|
|
"REQUIRE": true, "RESTRICT": true, "RETURN": true,
|
|
|
|
"REVOKE": true, "RIGHT": true, "RLIKE": true,
|
|
|
|
"SCHEMA": true, "SCHEMAS": true, "SECOND_MICROSECOND": true,
|
|
|
|
"SELECT": true, "SENSITIVE": true, "SEPARATOR": true,
|
|
|
|
"SET": true, "SHOW": true, "SMALLINT": true,
|
|
|
|
"SPATIAL": true, "SPECIFIC": true, "SQL": true,
|
|
|
|
"SQLEXCEPTION": true, "SQLSTATE": true, "SQLWARNING": true,
|
|
|
|
"SQL_BIG_RESULT": true, "SQL_CALC_FOUND_ROWS": true, "SQL_SMALL_RESULT": true,
|
|
|
|
"SSL": true, "STARTING": true, "STRAIGHT_JOIN": true,
|
|
|
|
"TABLE": true, "TERMINATED": true, "THEN": true,
|
|
|
|
"TINYBLOB": true, "TINYINT": true, "TINYTEXT": true,
|
|
|
|
"TO": true, "TRAILING": true, "TRIGGER": true,
|
|
|
|
"TRUE": true, "UNDO": true, "UNION": true,
|
|
|
|
"UNIQUE": true, "UNLOCK": true, "UNSIGNED": true,
|
|
|
|
"UPDATE": true, "USAGE": true, "USE": true,
|
|
|
|
"USING": true, "UTC_DATE": true, "UTC_TIME": true,
|
|
|
|
"UTC_TIMESTAMP": true, "VALUES": true, "VARBINARY": true,
|
|
|
|
"VARCHAR": true,
|
|
|
|
"VARCHARACTER": true,
|
|
|
|
"VARYING": true,
|
|
|
|
"WHEN": true,
|
|
|
|
"WHERE": true,
|
|
|
|
"WHILE": true,
|
|
|
|
"WITH": true,
|
|
|
|
"WRITE": true,
|
|
|
|
"X509": true,
|
|
|
|
"XOR": true,
|
|
|
|
"YEAR_MONTH": true,
|
|
|
|
"ZEROFILL": true,
|
|
|
|
}
|
2020-03-22 23:12:55 +08:00
|
|
|
|
|
|
|
mysqlQuoter = schemas.Quoter{'`', '`', schemas.AlwaysReserve}
|
2016-11-04 06:16:01 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type mysql struct {
|
2020-03-22 23:12:55 +08:00
|
|
|
Base
|
2016-11-04 06:16:01 +08:00
|
|
|
net string
|
|
|
|
addr string
|
|
|
|
params map[string]string
|
|
|
|
loc *time.Location
|
|
|
|
timeout time.Duration
|
|
|
|
tls *tls.Config
|
|
|
|
allowAllFiles bool
|
|
|
|
allowOldPasswords bool
|
|
|
|
clientFoundRows bool
|
2018-07-20 10:10:17 +08:00
|
|
|
rowFormat string
|
2016-11-04 06:16:01 +08:00
|
|
|
}
|
|
|
|
|
2020-03-25 22:32:23 +08:00
|
|
|
func (db *mysql) Init(uri *URI) error {
|
2020-03-22 23:12:55 +08:00
|
|
|
db.quoter = mysqlQuoter
|
2020-03-25 22:32:23 +08:00
|
|
|
return db.Base.Init(db, uri)
|
2016-11-04 06:16:01 +08:00
|
|
|
}
|
|
|
|
|
2018-07-20 10:10:17 +08:00
|
|
|
func (db *mysql) SetParams(params map[string]string) {
|
|
|
|
rowFormat, ok := params["rowFormat"]
|
|
|
|
if ok {
|
|
|
|
var t = strings.ToUpper(rowFormat)
|
|
|
|
switch t {
|
|
|
|
case "COMPACT":
|
|
|
|
fallthrough
|
|
|
|
case "REDUNDANT":
|
|
|
|
fallthrough
|
|
|
|
case "DYNAMIC":
|
|
|
|
fallthrough
|
|
|
|
case "COMPRESSED":
|
|
|
|
db.rowFormat = t
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
func (db *mysql) SQLType(c *schemas.Column) string {
|
2016-11-04 06:16:01 +08:00
|
|
|
var res string
|
|
|
|
switch t := c.SQLType.Name; t {
|
2020-03-22 23:12:55 +08:00
|
|
|
case schemas.Bool:
|
|
|
|
res = schemas.TinyInt
|
2016-11-04 06:16:01 +08:00
|
|
|
c.Length = 1
|
2020-03-22 23:12:55 +08:00
|
|
|
case schemas.Serial:
|
2016-11-04 06:16:01 +08:00
|
|
|
c.IsAutoIncrement = true
|
|
|
|
c.IsPrimaryKey = true
|
|
|
|
c.Nullable = false
|
2020-03-22 23:12:55 +08:00
|
|
|
res = schemas.Int
|
|
|
|
case schemas.BigSerial:
|
2016-11-04 06:16:01 +08:00
|
|
|
c.IsAutoIncrement = true
|
|
|
|
c.IsPrimaryKey = true
|
|
|
|
c.Nullable = false
|
2020-03-22 23:12:55 +08:00
|
|
|
res = schemas.BigInt
|
|
|
|
case schemas.Bytea:
|
|
|
|
res = schemas.Blob
|
|
|
|
case schemas.TimeStampz:
|
|
|
|
res = schemas.Char
|
2016-11-04 06:16:01 +08:00
|
|
|
c.Length = 64
|
2020-03-22 23:12:55 +08:00
|
|
|
case schemas.Enum: // mysql enum
|
|
|
|
res = schemas.Enum
|
2016-11-04 06:16:01 +08:00
|
|
|
res += "("
|
|
|
|
opts := ""
|
2016-11-12 00:40:21 +08:00
|
|
|
for v := range c.EnumOptions {
|
2016-11-04 06:16:01 +08:00
|
|
|
opts += fmt.Sprintf(",'%v'", v)
|
|
|
|
}
|
|
|
|
res += strings.TrimLeft(opts, ",")
|
|
|
|
res += ")"
|
2020-03-22 23:12:55 +08:00
|
|
|
case schemas.Set: // mysql set
|
|
|
|
res = schemas.Set
|
2016-11-04 06:16:01 +08:00
|
|
|
res += "("
|
|
|
|
opts := ""
|
2016-11-12 00:40:21 +08:00
|
|
|
for v := range c.SetOptions {
|
2016-11-04 06:16:01 +08:00
|
|
|
opts += fmt.Sprintf(",'%v'", v)
|
|
|
|
}
|
|
|
|
res += strings.TrimLeft(opts, ",")
|
|
|
|
res += ")"
|
2020-03-22 23:12:55 +08:00
|
|
|
case schemas.NVarchar:
|
|
|
|
res = schemas.Varchar
|
|
|
|
case schemas.Uuid:
|
|
|
|
res = schemas.Varchar
|
2016-11-04 06:16:01 +08:00
|
|
|
c.Length = 40
|
2020-03-22 23:12:55 +08:00
|
|
|
case schemas.Json:
|
|
|
|
res = schemas.Text
|
2016-11-04 06:16:01 +08:00
|
|
|
default:
|
|
|
|
res = t
|
|
|
|
}
|
|
|
|
|
2016-11-12 00:40:21 +08:00
|
|
|
hasLen1 := (c.Length > 0)
|
|
|
|
hasLen2 := (c.Length2 > 0)
|
2016-11-04 06:16:01 +08:00
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
if res == schemas.BigInt && !hasLen1 && !hasLen2 {
|
2016-11-04 06:16:01 +08:00
|
|
|
c.Length = 20
|
|
|
|
hasLen1 = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasLen2 {
|
|
|
|
res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")"
|
|
|
|
} else if hasLen1 {
|
|
|
|
res += "(" + strconv.Itoa(c.Length) + ")"
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *mysql) IsReserved(name string) bool {
|
2020-03-22 23:12:55 +08:00
|
|
|
_, ok := mysqlReservedWords[strings.ToUpper(name)]
|
2016-11-04 06:16:01 +08:00
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *mysql) AutoIncrStr() string {
|
|
|
|
return "AUTO_INCREMENT"
|
|
|
|
}
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
func (db *mysql) IndexCheckSQL(tableName, idxName string) (string, []interface{}) {
|
|
|
|
args := []interface{}{db.uri.DBName, tableName, idxName}
|
2016-11-04 06:16:01 +08:00
|
|
|
sql := "SELECT `INDEX_NAME` FROM `INFORMATION_SCHEMA`.`STATISTICS`"
|
|
|
|
sql += " WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? AND `INDEX_NAME`=?"
|
|
|
|
return sql, args
|
|
|
|
}
|
|
|
|
|
2020-03-25 22:32:23 +08:00
|
|
|
func (db *mysql) IsTableExist(queryer core.Queryer, ctx context.Context, tableName string) (bool, error) {
|
2016-11-04 06:16:01 +08:00
|
|
|
sql := "SELECT `TABLE_NAME` from `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`=? and `TABLE_NAME`=?"
|
2020-03-25 22:32:23 +08:00
|
|
|
return db.HasRecords(queryer, ctx, sql, db.uri.DBName, tableName)
|
2016-11-04 06:16:01 +08:00
|
|
|
}
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
func (db *mysql) AddColumnSQL(tableName string, col *schemas.Column) string {
|
|
|
|
quoter := db.dialect.Quoter()
|
|
|
|
sql := fmt.Sprintf("ALTER TABLE %v ADD %v", quoter.Quote(tableName),
|
|
|
|
db.String(col))
|
|
|
|
if len(col.Comment) > 0 {
|
|
|
|
sql += " COMMENT '" + col.Comment + "'"
|
|
|
|
}
|
|
|
|
return sql
|
|
|
|
}
|
|
|
|
|
2020-03-25 22:32:23 +08:00
|
|
|
func (db *mysql) GetColumns(queryer core.Queryer, ctx context.Context, tableName string) ([]string, map[string]*schemas.Column, error) {
|
2020-03-22 23:12:55 +08:00
|
|
|
args := []interface{}{db.uri.DBName, tableName}
|
2016-11-04 06:16:01 +08:00
|
|
|
s := "SELECT `COLUMN_NAME`, `IS_NULLABLE`, `COLUMN_DEFAULT`, `COLUMN_TYPE`," +
|
2017-08-22 19:39:52 +08:00
|
|
|
" `COLUMN_KEY`, `EXTRA`,`COLUMN_COMMENT` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ?"
|
2016-11-04 06:16:01 +08:00
|
|
|
|
2020-03-25 22:32:23 +08:00
|
|
|
rows, err := queryer.QueryContext(ctx, s, args...)
|
2016-11-04 06:16:01 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
cols := make(map[string]*schemas.Column)
|
2016-11-04 06:16:01 +08:00
|
|
|
colSeq := make([]string, 0)
|
|
|
|
for rows.Next() {
|
2020-03-22 23:12:55 +08:00
|
|
|
col := new(schemas.Column)
|
2016-11-04 06:16:01 +08:00
|
|
|
col.Indexes = make(map[string]int)
|
|
|
|
|
2017-08-22 19:39:52 +08:00
|
|
|
var columnName, isNullable, colType, colKey, extra, comment string
|
2016-11-04 06:16:01 +08:00
|
|
|
var colDefault *string
|
2017-08-22 19:39:52 +08:00
|
|
|
err = rows.Scan(&columnName, &isNullable, &colDefault, &colType, &colKey, &extra, &comment)
|
2016-11-04 06:16:01 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
col.Name = strings.Trim(columnName, "` ")
|
2017-08-22 19:39:52 +08:00
|
|
|
col.Comment = comment
|
2016-11-04 06:16:01 +08:00
|
|
|
if "YES" == isNullable {
|
|
|
|
col.Nullable = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if colDefault != nil {
|
|
|
|
col.Default = *colDefault
|
2019-10-03 04:47:20 +08:00
|
|
|
col.DefaultIsEmpty = false
|
|
|
|
} else {
|
|
|
|
col.DefaultIsEmpty = true
|
2016-11-04 06:16:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
cts := strings.Split(colType, "(")
|
|
|
|
colName := cts[0]
|
|
|
|
colType = strings.ToUpper(colName)
|
|
|
|
var len1, len2 int
|
|
|
|
if len(cts) == 2 {
|
|
|
|
idx := strings.Index(cts[1], ")")
|
2020-03-22 23:12:55 +08:00
|
|
|
if colType == schemas.Enum && cts[1][0] == '\'' { // enum
|
2016-11-04 06:16:01 +08:00
|
|
|
options := strings.Split(cts[1][0:idx], ",")
|
|
|
|
col.EnumOptions = make(map[string]int)
|
|
|
|
for k, v := range options {
|
|
|
|
v = strings.TrimSpace(v)
|
|
|
|
v = strings.Trim(v, "'")
|
|
|
|
col.EnumOptions[v] = k
|
|
|
|
}
|
2020-03-22 23:12:55 +08:00
|
|
|
} else if colType == schemas.Set && cts[1][0] == '\'' {
|
2016-11-04 06:16:01 +08:00
|
|
|
options := strings.Split(cts[1][0:idx], ",")
|
|
|
|
col.SetOptions = make(map[string]int)
|
|
|
|
for k, v := range options {
|
|
|
|
v = strings.TrimSpace(v)
|
|
|
|
v = strings.Trim(v, "'")
|
|
|
|
col.SetOptions[v] = k
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lens := strings.Split(cts[1][0:idx], ",")
|
|
|
|
len1, err = strconv.Atoi(strings.TrimSpace(lens[0]))
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
if len(lens) == 2 {
|
|
|
|
len2, err = strconv.Atoi(lens[1])
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if colType == "FLOAT UNSIGNED" {
|
|
|
|
colType = "FLOAT"
|
|
|
|
}
|
2019-06-23 23:22:43 +08:00
|
|
|
if colType == "DOUBLE UNSIGNED" {
|
|
|
|
colType = "DOUBLE"
|
|
|
|
}
|
2016-11-04 06:16:01 +08:00
|
|
|
col.Length = len1
|
|
|
|
col.Length2 = len2
|
2020-03-22 23:12:55 +08:00
|
|
|
if _, ok := schemas.SqlTypes[colType]; ok {
|
|
|
|
col.SQLType = schemas.SQLType{Name: colType, DefaultLength: len1, DefaultLength2: len2}
|
2016-11-04 06:16:01 +08:00
|
|
|
} else {
|
2016-11-12 00:40:21 +08:00
|
|
|
return nil, nil, fmt.Errorf("Unknown colType %v", colType)
|
2016-11-04 06:16:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if colKey == "PRI" {
|
|
|
|
col.IsPrimaryKey = true
|
|
|
|
}
|
|
|
|
if colKey == "UNI" {
|
2019-08-27 10:17:23 +08:00
|
|
|
// col.is
|
2016-11-04 06:16:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if extra == "auto_increment" {
|
|
|
|
col.IsAutoIncrement = true
|
|
|
|
}
|
|
|
|
|
2019-10-03 04:47:20 +08:00
|
|
|
if !col.DefaultIsEmpty {
|
|
|
|
if col.SQLType.IsText() {
|
|
|
|
col.Default = "'" + col.Default + "'"
|
|
|
|
} else if col.SQLType.IsTime() && col.Default != "CURRENT_TIMESTAMP" {
|
2016-11-04 06:16:01 +08:00
|
|
|
col.Default = "'" + col.Default + "'"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cols[col.Name] = col
|
|
|
|
colSeq = append(colSeq, col.Name)
|
|
|
|
}
|
|
|
|
return colSeq, cols, nil
|
|
|
|
}
|
|
|
|
|
2020-03-25 22:32:23 +08:00
|
|
|
func (db *mysql) GetTables(queryer core.Queryer, ctx context.Context) ([]*schemas.Table, error) {
|
2020-03-22 23:12:55 +08:00
|
|
|
args := []interface{}{db.uri.DBName}
|
|
|
|
s := "SELECT `TABLE_NAME`, `ENGINE`, `AUTO_INCREMENT`, `TABLE_COMMENT` from " +
|
2016-11-04 06:16:01 +08:00
|
|
|
"`INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`=? AND (`ENGINE`='MyISAM' OR `ENGINE` = 'InnoDB' OR `ENGINE` = 'TokuDB')"
|
|
|
|
|
2020-03-25 22:32:23 +08:00
|
|
|
rows, err := queryer.QueryContext(ctx, s, args...)
|
2016-11-04 06:16:01 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
tables := make([]*schemas.Table, 0)
|
2016-11-04 06:16:01 +08:00
|
|
|
for rows.Next() {
|
2020-03-22 23:12:55 +08:00
|
|
|
table := schemas.NewEmptyTable()
|
|
|
|
var name, engine string
|
|
|
|
var autoIncr, comment *string
|
|
|
|
err = rows.Scan(&name, &engine, &autoIncr, &comment)
|
2016-11-04 06:16:01 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
table.Name = name
|
2020-03-22 23:12:55 +08:00
|
|
|
if comment != nil {
|
|
|
|
table.Comment = *comment
|
|
|
|
}
|
2016-11-04 06:16:01 +08:00
|
|
|
table.StoreEngine = engine
|
|
|
|
tables = append(tables, table)
|
|
|
|
}
|
|
|
|
return tables, nil
|
|
|
|
}
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
func (db *mysql) SetQuotePolicy(quotePolicy QuotePolicy) {
|
|
|
|
switch quotePolicy {
|
|
|
|
case QuotePolicyNone:
|
|
|
|
var q = mysqlQuoter
|
|
|
|
q.IsReserved = schemas.AlwaysNoReserve
|
|
|
|
db.quoter = q
|
|
|
|
case QuotePolicyReserved:
|
|
|
|
var q = mysqlQuoter
|
|
|
|
q.IsReserved = db.IsReserved
|
|
|
|
db.quoter = q
|
|
|
|
case QuotePolicyAlways:
|
|
|
|
fallthrough
|
|
|
|
default:
|
|
|
|
db.quoter = mysqlQuoter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 22:32:23 +08:00
|
|
|
func (db *mysql) GetIndexes(queryer core.Queryer, ctx context.Context, tableName string) (map[string]*schemas.Index, error) {
|
2020-03-22 23:12:55 +08:00
|
|
|
args := []interface{}{db.uri.DBName, tableName}
|
2016-11-04 06:16:01 +08:00
|
|
|
s := "SELECT `INDEX_NAME`, `NON_UNIQUE`, `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`STATISTICS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ?"
|
|
|
|
|
2020-03-25 22:32:23 +08:00
|
|
|
rows, err := queryer.QueryContext(ctx, s, args...)
|
2016-11-04 06:16:01 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
indexes := make(map[string]*schemas.Index, 0)
|
2016-11-04 06:16:01 +08:00
|
|
|
for rows.Next() {
|
|
|
|
var indexType int
|
|
|
|
var indexName, colName, nonUnique string
|
|
|
|
err = rows.Scan(&indexName, &nonUnique, &colName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if indexName == "PRIMARY" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if "YES" == nonUnique || nonUnique == "1" {
|
2020-03-22 23:12:55 +08:00
|
|
|
indexType = schemas.IndexType
|
2016-11-04 06:16:01 +08:00
|
|
|
} else {
|
2020-03-22 23:12:55 +08:00
|
|
|
indexType = schemas.UniqueType
|
2016-11-04 06:16:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
colName = strings.Trim(colName, "` ")
|
|
|
|
var isRegular bool
|
|
|
|
if strings.HasPrefix(indexName, "IDX_"+tableName) || strings.HasPrefix(indexName, "UQE_"+tableName) {
|
2017-01-23 17:11:18 +08:00
|
|
|
indexName = indexName[5+len(tableName):]
|
2016-11-04 06:16:01 +08:00
|
|
|
isRegular = true
|
|
|
|
}
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
var index *schemas.Index
|
2016-11-04 06:16:01 +08:00
|
|
|
var ok bool
|
|
|
|
if index, ok = indexes[indexName]; !ok {
|
2020-03-22 23:12:55 +08:00
|
|
|
index = new(schemas.Index)
|
2016-11-04 06:16:01 +08:00
|
|
|
index.IsRegular = isRegular
|
|
|
|
index.Type = indexType
|
|
|
|
index.Name = indexName
|
|
|
|
indexes[indexName] = index
|
|
|
|
}
|
|
|
|
index.AddColumn(colName)
|
|
|
|
}
|
|
|
|
return indexes, nil
|
|
|
|
}
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
func (db *mysql) CreateTableSQL(table *schemas.Table, tableName string) ([]string, bool) {
|
|
|
|
var sql = "CREATE TABLE IF NOT EXISTS "
|
2018-07-20 10:10:17 +08:00
|
|
|
if tableName == "" {
|
|
|
|
tableName = table.Name
|
|
|
|
}
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
quoter := db.Quoter()
|
|
|
|
|
|
|
|
sql += quoter.Quote(tableName)
|
2018-07-20 10:10:17 +08:00
|
|
|
sql += " ("
|
|
|
|
|
|
|
|
if len(table.ColumnsSeq()) > 0 {
|
|
|
|
pkList := table.PrimaryKeys
|
|
|
|
|
|
|
|
for _, colName := range table.ColumnsSeq() {
|
|
|
|
col := table.GetColumn(colName)
|
|
|
|
if col.IsPrimaryKey && len(pkList) == 1 {
|
2020-03-22 23:12:55 +08:00
|
|
|
sql += db.String(col)
|
2018-07-20 10:10:17 +08:00
|
|
|
} else {
|
2020-03-22 23:12:55 +08:00
|
|
|
sql += db.StringNoPk(col)
|
2018-07-20 10:10:17 +08:00
|
|
|
}
|
|
|
|
sql = strings.TrimSpace(sql)
|
|
|
|
if len(col.Comment) > 0 {
|
|
|
|
sql += " COMMENT '" + col.Comment + "'"
|
|
|
|
}
|
|
|
|
sql += ", "
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(pkList) > 1 {
|
|
|
|
sql += "PRIMARY KEY ( "
|
2020-03-22 23:12:55 +08:00
|
|
|
sql += quoter.Join(pkList, ",")
|
2018-07-20 10:10:17 +08:00
|
|
|
sql += " ), "
|
|
|
|
}
|
|
|
|
|
|
|
|
sql = sql[:len(sql)-2]
|
|
|
|
}
|
|
|
|
sql += ")"
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
if table.StoreEngine != "" {
|
|
|
|
sql += " ENGINE=" + table.StoreEngine
|
2018-07-20 10:10:17 +08:00
|
|
|
}
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
var charset = table.Charset
|
2018-07-20 10:10:17 +08:00
|
|
|
if len(charset) == 0 {
|
|
|
|
charset = db.URI().Charset
|
2019-08-27 10:17:23 +08:00
|
|
|
}
|
2018-12-12 09:01:41 +08:00
|
|
|
if len(charset) != 0 {
|
2018-07-20 10:10:17 +08:00
|
|
|
sql += " DEFAULT CHARSET " + charset
|
|
|
|
}
|
|
|
|
|
|
|
|
if db.rowFormat != "" {
|
|
|
|
sql += " ROW_FORMAT=" + db.rowFormat
|
|
|
|
}
|
2020-03-22 23:12:55 +08:00
|
|
|
return []string{sql}, true
|
2018-07-20 10:10:17 +08:00
|
|
|
}
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
func (db *mysql) Filters() []Filter {
|
|
|
|
return []Filter{}
|
2016-11-04 06:16:01 +08:00
|
|
|
}
|
2017-04-07 09:47:25 +08:00
|
|
|
|
|
|
|
type mymysqlDriver struct {
|
|
|
|
}
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
func (p *mymysqlDriver) Parse(driverName, dataSourceName string) (*URI, error) {
|
|
|
|
uri := &URI{DBType: schemas.MYSQL}
|
2017-04-07 09:47:25 +08:00
|
|
|
|
|
|
|
pd := strings.SplitN(dataSourceName, "*", 2)
|
|
|
|
if len(pd) == 2 {
|
|
|
|
// Parse protocol part of URI
|
|
|
|
p := strings.SplitN(pd[0], ":", 2)
|
|
|
|
if len(p) != 2 {
|
|
|
|
return nil, errors.New("Wrong protocol part of URI")
|
|
|
|
}
|
2020-03-22 23:12:55 +08:00
|
|
|
uri.Proto = p[0]
|
2017-04-07 09:47:25 +08:00
|
|
|
options := strings.Split(p[1], ",")
|
2020-03-22 23:12:55 +08:00
|
|
|
uri.Raddr = options[0]
|
2017-04-07 09:47:25 +08:00
|
|
|
for _, o := range options[1:] {
|
|
|
|
kv := strings.SplitN(o, "=", 2)
|
|
|
|
var k, v string
|
|
|
|
if len(kv) == 2 {
|
|
|
|
k, v = kv[0], kv[1]
|
|
|
|
} else {
|
|
|
|
k, v = o, "true"
|
|
|
|
}
|
|
|
|
switch k {
|
|
|
|
case "laddr":
|
2020-03-22 23:12:55 +08:00
|
|
|
uri.Laddr = v
|
2017-04-07 09:47:25 +08:00
|
|
|
case "timeout":
|
|
|
|
to, err := time.ParseDuration(v)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-03-22 23:12:55 +08:00
|
|
|
uri.Timeout = to
|
2017-04-07 09:47:25 +08:00
|
|
|
default:
|
|
|
|
return nil, errors.New("Unknown option: " + k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Remove protocol part
|
|
|
|
pd = pd[1:]
|
|
|
|
}
|
|
|
|
// Parse database part of URI
|
|
|
|
dup := strings.SplitN(pd[0], "/", 3)
|
|
|
|
if len(dup) != 3 {
|
|
|
|
return nil, errors.New("Wrong database part of URI")
|
|
|
|
}
|
2020-03-22 23:12:55 +08:00
|
|
|
uri.DBName = dup[0]
|
|
|
|
uri.User = dup[1]
|
|
|
|
uri.Passwd = dup[2]
|
2017-04-07 09:47:25 +08:00
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
return uri, nil
|
2017-04-07 09:47:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type mysqlDriver struct {
|
|
|
|
}
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
func (p *mysqlDriver) Parse(driverName, dataSourceName string) (*URI, error) {
|
2017-04-07 09:47:25 +08:00
|
|
|
dsnPattern := regexp.MustCompile(
|
|
|
|
`^(?:(?P<user>.*?)(?::(?P<passwd>.*))?@)?` + // [user[:password]@]
|
|
|
|
`(?:(?P<net>[^\(]*)(?:\((?P<addr>[^\)]*)\))?)?` + // [net[(addr)]]
|
|
|
|
`\/(?P<dbname>.*?)` + // /dbname
|
|
|
|
`(?:\?(?P<params>[^\?]*))?$`) // [?param1=value1¶mN=valueN]
|
|
|
|
matches := dsnPattern.FindStringSubmatch(dataSourceName)
|
2019-08-27 10:17:23 +08:00
|
|
|
// tlsConfigRegister := make(map[string]*tls.Config)
|
2017-04-07 09:47:25 +08:00
|
|
|
names := dsnPattern.SubexpNames()
|
|
|
|
|
2020-03-22 23:12:55 +08:00
|
|
|
uri := &URI{DBType: schemas.MYSQL}
|
2017-04-07 09:47:25 +08:00
|
|
|
|
|
|
|
for i, match := range matches {
|
|
|
|
switch names[i] {
|
|
|
|
case "dbname":
|
2020-03-22 23:12:55 +08:00
|
|
|
uri.DBName = match
|
2017-04-07 09:47:25 +08:00
|
|
|
case "params":
|
|
|
|
if len(match) > 0 {
|
|
|
|
kvs := strings.Split(match, "&")
|
|
|
|
for _, kv := range kvs {
|
|
|
|
splits := strings.Split(kv, "=")
|
|
|
|
if len(splits) == 2 {
|
|
|
|
switch splits[0] {
|
|
|
|
case "charset":
|
|
|
|
uri.Charset = splits[1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return uri, nil
|
|
|
|
}
|