dialect_mysql.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. // Copyright 2015 The Xorm Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package xorm
  5. import (
  6. "crypto/tls"
  7. "errors"
  8. "fmt"
  9. "regexp"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "github.com/go-xorm/core"
  14. )
  15. var (
  16. mysqlReservedWords = map[string]bool{
  17. "ADD": true,
  18. "ALL": true,
  19. "ALTER": true,
  20. "ANALYZE": true,
  21. "AND": true,
  22. "AS": true,
  23. "ASC": true,
  24. "ASENSITIVE": true,
  25. "BEFORE": true,
  26. "BETWEEN": true,
  27. "BIGINT": true,
  28. "BINARY": true,
  29. "BLOB": true,
  30. "BOTH": true,
  31. "BY": true,
  32. "CALL": true,
  33. "CASCADE": true,
  34. "CASE": true,
  35. "CHANGE": true,
  36. "CHAR": true,
  37. "CHARACTER": true,
  38. "CHECK": true,
  39. "COLLATE": true,
  40. "COLUMN": true,
  41. "CONDITION": true,
  42. "CONNECTION": true,
  43. "CONSTRAINT": true,
  44. "CONTINUE": true,
  45. "CONVERT": true,
  46. "CREATE": true,
  47. "CROSS": true,
  48. "CURRENT_DATE": true,
  49. "CURRENT_TIME": true,
  50. "CURRENT_TIMESTAMP": true,
  51. "CURRENT_USER": true,
  52. "CURSOR": true,
  53. "DATABASE": true,
  54. "DATABASES": true,
  55. "DAY_HOUR": true,
  56. "DAY_MICROSECOND": true,
  57. "DAY_MINUTE": true,
  58. "DAY_SECOND": true,
  59. "DEC": true,
  60. "DECIMAL": true,
  61. "DECLARE": true,
  62. "DEFAULT": true,
  63. "DELAYED": true,
  64. "DELETE": true,
  65. "DESC": true,
  66. "DESCRIBE": true,
  67. "DETERMINISTIC": true,
  68. "DISTINCT": true,
  69. "DISTINCTROW": true,
  70. "DIV": true,
  71. "DOUBLE": true,
  72. "DROP": true,
  73. "DUAL": true,
  74. "EACH": true,
  75. "ELSE": true,
  76. "ELSEIF": true,
  77. "ENCLOSED": true,
  78. "ESCAPED": true,
  79. "EXISTS": true,
  80. "EXIT": true,
  81. "EXPLAIN": true,
  82. "FALSE": true,
  83. "FETCH": true,
  84. "FLOAT": true,
  85. "FLOAT4": true,
  86. "FLOAT8": true,
  87. "FOR": true,
  88. "FORCE": true,
  89. "FOREIGN": true,
  90. "FROM": true,
  91. "FULLTEXT": true,
  92. "GOTO": true,
  93. "GRANT": true,
  94. "GROUP": true,
  95. "HAVING": true,
  96. "HIGH_PRIORITY": true,
  97. "HOUR_MICROSECOND": true,
  98. "HOUR_MINUTE": true,
  99. "HOUR_SECOND": true,
  100. "IF": true,
  101. "IGNORE": true,
  102. "IN": true, "INDEX": true,
  103. "INFILE": true, "INNER": true, "INOUT": true,
  104. "INSENSITIVE": true, "INSERT": true, "INT": true,
  105. "INT1": true, "INT2": true, "INT3": true,
  106. "INT4": true, "INT8": true, "INTEGER": true,
  107. "INTERVAL": true, "INTO": true, "IS": true,
  108. "ITERATE": true, "JOIN": true, "KEY": true,
  109. "KEYS": true, "KILL": true, "LABEL": true,
  110. "LEADING": true, "LEAVE": true, "LEFT": true,
  111. "LIKE": true, "LIMIT": true, "LINEAR": true,
  112. "LINES": true, "LOAD": true, "LOCALTIME": true,
  113. "LOCALTIMESTAMP": true, "LOCK": true, "LONG": true,
  114. "LONGBLOB": true, "LONGTEXT": true, "LOOP": true,
  115. "LOW_PRIORITY": true, "MATCH": true, "MEDIUMBLOB": true,
  116. "MEDIUMINT": true, "MEDIUMTEXT": true, "MIDDLEINT": true,
  117. "MINUTE_MICROSECOND": true, "MINUTE_SECOND": true, "MOD": true,
  118. "MODIFIES": true, "NATURAL": true, "NOT": true,
  119. "NO_WRITE_TO_BINLOG": true, "NULL": true, "NUMERIC": true,
  120. "ON OPTIMIZE": true, "OPTION": true,
  121. "OPTIONALLY": true, "OR": true, "ORDER": true,
  122. "OUT": true, "OUTER": true, "OUTFILE": true,
  123. "PRECISION": true, "PRIMARY": true, "PROCEDURE": true,
  124. "PURGE": true, "RAID0": true, "RANGE": true,
  125. "READ": true, "READS": true, "REAL": true,
  126. "REFERENCES": true, "REGEXP": true, "RELEASE": true,
  127. "RENAME": true, "REPEAT": true, "REPLACE": true,
  128. "REQUIRE": true, "RESTRICT": true, "RETURN": true,
  129. "REVOKE": true, "RIGHT": true, "RLIKE": true,
  130. "SCHEMA": true, "SCHEMAS": true, "SECOND_MICROSECOND": true,
  131. "SELECT": true, "SENSITIVE": true, "SEPARATOR": true,
  132. "SET": true, "SHOW": true, "SMALLINT": true,
  133. "SPATIAL": true, "SPECIFIC": true, "SQL": true,
  134. "SQLEXCEPTION": true, "SQLSTATE": true, "SQLWARNING": true,
  135. "SQL_BIG_RESULT": true, "SQL_CALC_FOUND_ROWS": true, "SQL_SMALL_RESULT": true,
  136. "SSL": true, "STARTING": true, "STRAIGHT_JOIN": true,
  137. "TABLE": true, "TERMINATED": true, "THEN": true,
  138. "TINYBLOB": true, "TINYINT": true, "TINYTEXT": true,
  139. "TO": true, "TRAILING": true, "TRIGGER": true,
  140. "TRUE": true, "UNDO": true, "UNION": true,
  141. "UNIQUE": true, "UNLOCK": true, "UNSIGNED": true,
  142. "UPDATE": true, "USAGE": true, "USE": true,
  143. "USING": true, "UTC_DATE": true, "UTC_TIME": true,
  144. "UTC_TIMESTAMP": true, "VALUES": true, "VARBINARY": true,
  145. "VARCHAR": true,
  146. "VARCHARACTER": true,
  147. "VARYING": true,
  148. "WHEN": true,
  149. "WHERE": true,
  150. "WHILE": true,
  151. "WITH": true,
  152. "WRITE": true,
  153. "X509": true,
  154. "XOR": true,
  155. "YEAR_MONTH": true,
  156. "ZEROFILL": true,
  157. }
  158. )
  159. type mysql struct {
  160. core.Base
  161. net string
  162. addr string
  163. params map[string]string
  164. loc *time.Location
  165. timeout time.Duration
  166. tls *tls.Config
  167. allowAllFiles bool
  168. allowOldPasswords bool
  169. clientFoundRows bool
  170. }
  171. func (db *mysql) Init(d *core.DB, uri *core.Uri, drivername, dataSourceName string) error {
  172. return db.Base.Init(d, db, uri, drivername, dataSourceName)
  173. }
  174. func (db *mysql) SqlType(c *core.Column) string {
  175. var res string
  176. switch t := c.SQLType.Name; t {
  177. case core.Bool:
  178. res = core.TinyInt
  179. c.Length = 1
  180. case core.Serial:
  181. c.IsAutoIncrement = true
  182. c.IsPrimaryKey = true
  183. c.Nullable = false
  184. res = core.Int
  185. case core.BigSerial:
  186. c.IsAutoIncrement = true
  187. c.IsPrimaryKey = true
  188. c.Nullable = false
  189. res = core.BigInt
  190. case core.Bytea:
  191. res = core.Blob
  192. case core.TimeStampz:
  193. res = core.Char
  194. c.Length = 64
  195. case core.Enum: //mysql enum
  196. res = core.Enum
  197. res += "("
  198. opts := ""
  199. for v := range c.EnumOptions {
  200. opts += fmt.Sprintf(",'%v'", v)
  201. }
  202. res += strings.TrimLeft(opts, ",")
  203. res += ")"
  204. case core.Set: //mysql set
  205. res = core.Set
  206. res += "("
  207. opts := ""
  208. for v := range c.SetOptions {
  209. opts += fmt.Sprintf(",'%v'", v)
  210. }
  211. res += strings.TrimLeft(opts, ",")
  212. res += ")"
  213. case core.NVarchar:
  214. res = core.Varchar
  215. case core.Uuid:
  216. res = core.Varchar
  217. c.Length = 40
  218. case core.Json:
  219. res = core.Text
  220. default:
  221. res = t
  222. }
  223. hasLen1 := (c.Length > 0)
  224. hasLen2 := (c.Length2 > 0)
  225. if res == core.BigInt && !hasLen1 && !hasLen2 {
  226. c.Length = 20
  227. hasLen1 = true
  228. }
  229. if hasLen2 {
  230. res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")"
  231. } else if hasLen1 {
  232. res += "(" + strconv.Itoa(c.Length) + ")"
  233. }
  234. return res
  235. }
  236. func (db *mysql) SupportInsertMany() bool {
  237. return true
  238. }
  239. func (db *mysql) IsReserved(name string) bool {
  240. _, ok := mysqlReservedWords[name]
  241. return ok
  242. }
  243. func (db *mysql) Quote(name string) string {
  244. return "`" + name + "`"
  245. }
  246. func (db *mysql) QuoteStr() string {
  247. return "`"
  248. }
  249. func (db *mysql) SupportEngine() bool {
  250. return true
  251. }
  252. func (db *mysql) AutoIncrStr() string {
  253. return "AUTO_INCREMENT"
  254. }
  255. func (db *mysql) SupportCharset() bool {
  256. return true
  257. }
  258. func (db *mysql) IndexOnTable() bool {
  259. return true
  260. }
  261. func (db *mysql) IndexCheckSql(tableName, idxName string) (string, []interface{}) {
  262. args := []interface{}{db.DbName, tableName, idxName}
  263. sql := "SELECT `INDEX_NAME` FROM `INFORMATION_SCHEMA`.`STATISTICS`"
  264. sql += " WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? AND `INDEX_NAME`=?"
  265. return sql, args
  266. }
  267. /*func (db *mysql) ColumnCheckSql(tableName, colName string) (string, []interface{}) {
  268. args := []interface{}{db.DbName, tableName, colName}
  269. sql := "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? AND `COLUMN_NAME` = ?"
  270. return sql, args
  271. }*/
  272. func (db *mysql) TableCheckSql(tableName string) (string, []interface{}) {
  273. args := []interface{}{db.DbName, tableName}
  274. sql := "SELECT `TABLE_NAME` from `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`=? and `TABLE_NAME`=?"
  275. return sql, args
  276. }
  277. func (db *mysql) GetColumns(tableName string) ([]string, map[string]*core.Column, error) {
  278. args := []interface{}{db.DbName, tableName}
  279. s := "SELECT `COLUMN_NAME`, `IS_NULLABLE`, `COLUMN_DEFAULT`, `COLUMN_TYPE`," +
  280. " `COLUMN_KEY`, `EXTRA` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ?"
  281. db.LogSQL(s, args)
  282. rows, err := db.DB().Query(s, args...)
  283. if err != nil {
  284. return nil, nil, err
  285. }
  286. defer rows.Close()
  287. cols := make(map[string]*core.Column)
  288. colSeq := make([]string, 0)
  289. for rows.Next() {
  290. col := new(core.Column)
  291. col.Indexes = make(map[string]int)
  292. var columnName, isNullable, colType, colKey, extra string
  293. var colDefault *string
  294. err = rows.Scan(&columnName, &isNullable, &colDefault, &colType, &colKey, &extra)
  295. if err != nil {
  296. return nil, nil, err
  297. }
  298. col.Name = strings.Trim(columnName, "` ")
  299. if "YES" == isNullable {
  300. col.Nullable = true
  301. }
  302. if colDefault != nil {
  303. col.Default = *colDefault
  304. if col.Default == "" {
  305. col.DefaultIsEmpty = true
  306. }
  307. }
  308. cts := strings.Split(colType, "(")
  309. colName := cts[0]
  310. colType = strings.ToUpper(colName)
  311. var len1, len2 int
  312. if len(cts) == 2 {
  313. idx := strings.Index(cts[1], ")")
  314. if colType == core.Enum && cts[1][0] == '\'' { //enum
  315. options := strings.Split(cts[1][0:idx], ",")
  316. col.EnumOptions = make(map[string]int)
  317. for k, v := range options {
  318. v = strings.TrimSpace(v)
  319. v = strings.Trim(v, "'")
  320. col.EnumOptions[v] = k
  321. }
  322. } else if colType == core.Set && cts[1][0] == '\'' {
  323. options := strings.Split(cts[1][0:idx], ",")
  324. col.SetOptions = make(map[string]int)
  325. for k, v := range options {
  326. v = strings.TrimSpace(v)
  327. v = strings.Trim(v, "'")
  328. col.SetOptions[v] = k
  329. }
  330. } else {
  331. lens := strings.Split(cts[1][0:idx], ",")
  332. len1, err = strconv.Atoi(strings.TrimSpace(lens[0]))
  333. if err != nil {
  334. return nil, nil, err
  335. }
  336. if len(lens) == 2 {
  337. len2, err = strconv.Atoi(lens[1])
  338. if err != nil {
  339. return nil, nil, err
  340. }
  341. }
  342. }
  343. }
  344. if colType == "FLOAT UNSIGNED" {
  345. colType = "FLOAT"
  346. }
  347. col.Length = len1
  348. col.Length2 = len2
  349. if _, ok := core.SqlTypes[colType]; ok {
  350. col.SQLType = core.SQLType{Name: colType, DefaultLength: len1, DefaultLength2: len2}
  351. } else {
  352. return nil, nil, fmt.Errorf("Unknown colType %v", colType)
  353. }
  354. if colKey == "PRI" {
  355. col.IsPrimaryKey = true
  356. }
  357. if colKey == "UNI" {
  358. //col.is
  359. }
  360. if extra == "auto_increment" {
  361. col.IsAutoIncrement = true
  362. }
  363. if col.SQLType.IsText() || col.SQLType.IsTime() {
  364. if col.Default != "" {
  365. col.Default = "'" + col.Default + "'"
  366. } else {
  367. if col.DefaultIsEmpty {
  368. col.Default = "''"
  369. }
  370. }
  371. }
  372. cols[col.Name] = col
  373. colSeq = append(colSeq, col.Name)
  374. }
  375. return colSeq, cols, nil
  376. }
  377. func (db *mysql) GetTables() ([]*core.Table, error) {
  378. args := []interface{}{db.DbName}
  379. s := "SELECT `TABLE_NAME`, `ENGINE`, `TABLE_ROWS`, `AUTO_INCREMENT` from " +
  380. "`INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`=? AND (`ENGINE`='MyISAM' OR `ENGINE` = 'InnoDB' OR `ENGINE` = 'TokuDB')"
  381. db.LogSQL(s, args)
  382. rows, err := db.DB().Query(s, args...)
  383. if err != nil {
  384. return nil, err
  385. }
  386. defer rows.Close()
  387. tables := make([]*core.Table, 0)
  388. for rows.Next() {
  389. table := core.NewEmptyTable()
  390. var name, engine, tableRows string
  391. var autoIncr *string
  392. err = rows.Scan(&name, &engine, &tableRows, &autoIncr)
  393. if err != nil {
  394. return nil, err
  395. }
  396. table.Name = name
  397. table.StoreEngine = engine
  398. tables = append(tables, table)
  399. }
  400. return tables, nil
  401. }
  402. func (db *mysql) GetIndexes(tableName string) (map[string]*core.Index, error) {
  403. args := []interface{}{db.DbName, tableName}
  404. s := "SELECT `INDEX_NAME`, `NON_UNIQUE`, `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`STATISTICS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ?"
  405. db.LogSQL(s, args)
  406. rows, err := db.DB().Query(s, args...)
  407. if err != nil {
  408. return nil, err
  409. }
  410. defer rows.Close()
  411. indexes := make(map[string]*core.Index, 0)
  412. for rows.Next() {
  413. var indexType int
  414. var indexName, colName, nonUnique string
  415. err = rows.Scan(&indexName, &nonUnique, &colName)
  416. if err != nil {
  417. return nil, err
  418. }
  419. if indexName == "PRIMARY" {
  420. continue
  421. }
  422. if "YES" == nonUnique || nonUnique == "1" {
  423. indexType = core.IndexType
  424. } else {
  425. indexType = core.UniqueType
  426. }
  427. colName = strings.Trim(colName, "` ")
  428. var isRegular bool
  429. if strings.HasPrefix(indexName, "IDX_"+tableName) || strings.HasPrefix(indexName, "UQE_"+tableName) {
  430. indexName = indexName[5+len(tableName):]
  431. isRegular = true
  432. }
  433. var index *core.Index
  434. var ok bool
  435. if index, ok = indexes[indexName]; !ok {
  436. index = new(core.Index)
  437. index.IsRegular = isRegular
  438. index.Type = indexType
  439. index.Name = indexName
  440. indexes[indexName] = index
  441. }
  442. index.AddColumn(colName)
  443. }
  444. return indexes, nil
  445. }
  446. func (db *mysql) Filters() []core.Filter {
  447. return []core.Filter{&core.IdFilter{}}
  448. }
  449. type mymysqlDriver struct {
  450. }
  451. func (p *mymysqlDriver) Parse(driverName, dataSourceName string) (*core.Uri, error) {
  452. db := &core.Uri{DbType: core.MYSQL}
  453. pd := strings.SplitN(dataSourceName, "*", 2)
  454. if len(pd) == 2 {
  455. // Parse protocol part of URI
  456. p := strings.SplitN(pd[0], ":", 2)
  457. if len(p) != 2 {
  458. return nil, errors.New("Wrong protocol part of URI")
  459. }
  460. db.Proto = p[0]
  461. options := strings.Split(p[1], ",")
  462. db.Raddr = options[0]
  463. for _, o := range options[1:] {
  464. kv := strings.SplitN(o, "=", 2)
  465. var k, v string
  466. if len(kv) == 2 {
  467. k, v = kv[0], kv[1]
  468. } else {
  469. k, v = o, "true"
  470. }
  471. switch k {
  472. case "laddr":
  473. db.Laddr = v
  474. case "timeout":
  475. to, err := time.ParseDuration(v)
  476. if err != nil {
  477. return nil, err
  478. }
  479. db.Timeout = to
  480. default:
  481. return nil, errors.New("Unknown option: " + k)
  482. }
  483. }
  484. // Remove protocol part
  485. pd = pd[1:]
  486. }
  487. // Parse database part of URI
  488. dup := strings.SplitN(pd[0], "/", 3)
  489. if len(dup) != 3 {
  490. return nil, errors.New("Wrong database part of URI")
  491. }
  492. db.DbName = dup[0]
  493. db.User = dup[1]
  494. db.Passwd = dup[2]
  495. return db, nil
  496. }
  497. type mysqlDriver struct {
  498. }
  499. func (p *mysqlDriver) Parse(driverName, dataSourceName string) (*core.Uri, error) {
  500. dsnPattern := regexp.MustCompile(
  501. `^(?:(?P<user>.*?)(?::(?P<passwd>.*))?@)?` + // [user[:password]@]
  502. `(?:(?P<net>[^\(]*)(?:\((?P<addr>[^\)]*)\))?)?` + // [net[(addr)]]
  503. `\/(?P<dbname>.*?)` + // /dbname
  504. `(?:\?(?P<params>[^\?]*))?$`) // [?param1=value1&paramN=valueN]
  505. matches := dsnPattern.FindStringSubmatch(dataSourceName)
  506. //tlsConfigRegister := make(map[string]*tls.Config)
  507. names := dsnPattern.SubexpNames()
  508. uri := &core.Uri{DbType: core.MYSQL}
  509. for i, match := range matches {
  510. switch names[i] {
  511. case "dbname":
  512. uri.DbName = match
  513. case "params":
  514. if len(match) > 0 {
  515. kvs := strings.Split(match, "&")
  516. for _, kv := range kvs {
  517. splits := strings.Split(kv, "=")
  518. if len(splits) == 2 {
  519. switch splits[0] {
  520. case "charset":
  521. uri.Charset = splits[1]
  522. }
  523. }
  524. }
  525. }
  526. }
  527. }
  528. return uri, nil
  529. }