2013-06-04 08:56:59 +00:00
|
|
|
package xorm
|
|
|
|
|
2013-08-08 16:03:33 +00:00
|
|
|
import (
|
2013-09-28 15:14:42 +00:00
|
|
|
"crypto/tls"
|
|
|
|
//"fmt"
|
|
|
|
"regexp"
|
2013-08-08 16:03:33 +00:00
|
|
|
"strconv"
|
2013-09-28 15:14:42 +00:00
|
|
|
//"strings"
|
|
|
|
"time"
|
2013-08-08 16:03:33 +00:00
|
|
|
)
|
2013-06-04 08:56:59 +00:00
|
|
|
|
|
|
|
type mysql struct {
|
2013-09-28 15:14:42 +00:00
|
|
|
user string
|
|
|
|
passwd string
|
|
|
|
net string
|
|
|
|
addr string
|
|
|
|
dbname string
|
|
|
|
params map[string]string
|
|
|
|
loc *time.Location
|
|
|
|
timeout time.Duration
|
|
|
|
tls *tls.Config
|
|
|
|
allowAllFiles bool
|
|
|
|
allowOldPasswords bool
|
|
|
|
clientFoundRows bool
|
|
|
|
}
|
|
|
|
|
|
|
|
/*func readBool(input string) (value bool, valid bool) {
|
|
|
|
switch input {
|
|
|
|
case "1", "true", "TRUE", "True":
|
|
|
|
return true, true
|
|
|
|
case "0", "false", "FALSE", "False":
|
|
|
|
return false, true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not a valid bool value
|
|
|
|
return
|
|
|
|
}*/
|
|
|
|
|
|
|
|
func (cfg *mysql) parseDSN(dsn string) (err error) {
|
|
|
|
//cfg.params = make(map[string]string)
|
|
|
|
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(dsn)
|
|
|
|
//tlsConfigRegister := make(map[string]*tls.Config)
|
|
|
|
names := dsnPattern.SubexpNames()
|
|
|
|
|
|
|
|
for i, match := range matches {
|
|
|
|
switch names[i] {
|
|
|
|
case "dbname":
|
|
|
|
cfg.dbname = match
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *mysql) Init(uri string) error {
|
|
|
|
return db.parseDSN(uri)
|
2013-06-04 08:56:59 +00:00
|
|
|
}
|
|
|
|
|
2013-07-03 03:49:29 +00:00
|
|
|
func (db *mysql) SqlType(c *Column) string {
|
2013-08-08 05:24:38 +00:00
|
|
|
var res string
|
2013-08-08 16:03:33 +00:00
|
|
|
switch t := c.SQLType.Name; t {
|
2013-08-08 05:24:38 +00:00
|
|
|
case Bool:
|
2013-08-08 16:03:33 +00:00
|
|
|
res = TinyInt
|
2013-08-08 05:24:38 +00:00
|
|
|
case Serial:
|
|
|
|
c.IsAutoIncrement = true
|
2013-08-08 16:03:33 +00:00
|
|
|
c.IsPrimaryKey = true
|
|
|
|
c.Nullable = false
|
|
|
|
res = Int
|
2013-08-08 05:24:38 +00:00
|
|
|
case BigSerial:
|
|
|
|
c.IsAutoIncrement = true
|
2013-08-08 16:03:33 +00:00
|
|
|
c.IsPrimaryKey = true
|
|
|
|
c.Nullable = false
|
|
|
|
res = BigInt
|
2013-08-08 05:24:38 +00:00
|
|
|
case Bytea:
|
2013-08-08 16:03:33 +00:00
|
|
|
res = Blob
|
2013-06-04 08:56:59 +00:00
|
|
|
default:
|
2013-08-08 16:03:33 +00:00
|
|
|
res = t
|
2013-06-04 08:56:59 +00:00
|
|
|
}
|
2013-08-08 05:24:38 +00:00
|
|
|
|
|
|
|
var hasLen1 bool = (c.Length > 0)
|
|
|
|
var hasLen2 bool = (c.Length2 > 0)
|
|
|
|
if hasLen1 {
|
|
|
|
res += "(" + strconv.Itoa(c.Length) + ")"
|
|
|
|
} else if hasLen2 {
|
|
|
|
res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")"
|
|
|
|
}
|
|
|
|
return res
|
2013-06-04 08:56:59 +00:00
|
|
|
}
|
2013-07-03 03:49:29 +00:00
|
|
|
|
|
|
|
func (db *mysql) SupportInsertMany() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2013-08-08 05:24:38 +00:00
|
|
|
func (db *mysql) QuoteStr() string {
|
2013-07-03 03:49:29 +00:00
|
|
|
return "`"
|
|
|
|
}
|
|
|
|
|
2013-08-08 05:24:38 +00:00
|
|
|
func (db *mysql) SupportEngine() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2013-09-28 15:14:42 +00:00
|
|
|
func (db *mysql) AutoIncrStr() string {
|
|
|
|
return "AUTO_INCREMENT"
|
|
|
|
}
|
|
|
|
|
2013-08-08 05:24:38 +00:00
|
|
|
func (db *mysql) SupportCharset() bool {
|
|
|
|
return true
|
|
|
|
}
|
2013-09-26 07:19:39 +00:00
|
|
|
|
|
|
|
func (db *mysql) IndexOnTable() bool {
|
|
|
|
return true
|
|
|
|
}
|
2013-09-28 15:14:42 +00:00
|
|
|
|
|
|
|
func (db *mysql) IndexCheckSql(tableName, idxName string) (string, []interface{}) {
|
|
|
|
args := []interface{}{db.dbname, tableName, idxName}
|
|
|
|
sql := "SELECT `INDEX_NAME` FROM `INFORMATION_SCHEMA`.`STATISTICS`"
|
|
|
|
sql += " WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? AND `INDEX_NAME`=?"
|
|
|
|
return sql, args
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *mysql) ColumnCheckSql(tableName, colName string) (string, []interface{}) {
|
|
|
|
args := []interface{}{db.dbname, tableName, colName}
|
|
|
|
sql := "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? AND `COLUMN_NAME` = ?"
|
|
|
|
return sql, args
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *mysql) TableCheckSql(tableName string) (string, []interface{}) {
|
|
|
|
args := []interface{}{db.dbname, tableName}
|
|
|
|
sql := "SELECT `TABLE_NAME` from `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`=? and `TABLE_NAME`=?"
|
|
|
|
return sql, args
|
|
|
|
}
|