You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

582 lines
15 KiB

  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`,`COLUMN_COMMENT` 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, comment string
  293. var colDefault *string
  294. err = rows.Scan(&columnName, &isNullable, &colDefault, &colType, &colKey, &extra, &comment)
  295. if err != nil {
  296. return nil, nil, err
  297. }
  298. col.Name = strings.Trim(columnName, "` ")
  299. col.Comment = comment
  300. if "YES" == isNullable {
  301. col.Nullable = true
  302. }
  303. if colDefault != nil {
  304. col.Default = *colDefault
  305. if col.Default == "" {
  306. col.DefaultIsEmpty = true
  307. }
  308. }
  309. cts := strings.Split(colType, "(")
  310. colName := cts[0]
  311. colType = strings.ToUpper(colName)
  312. var len1, len2 int
  313. if len(cts) == 2 {
  314. idx := strings.Index(cts[1], ")")
  315. if colType == core.Enum && cts[1][0] == '\'' { //enum
  316. options := strings.Split(cts[1][0:idx], ",")
  317. col.EnumOptions = make(map[string]int)
  318. for k, v := range options {
  319. v = strings.TrimSpace(v)
  320. v = strings.Trim(v, "'")
  321. col.EnumOptions[v] = k
  322. }
  323. } else if colType == core.Set && cts[1][0] == '\'' {
  324. options := strings.Split(cts[1][0:idx], ",")
  325. col.SetOptions = make(map[string]int)
  326. for k, v := range options {
  327. v = strings.TrimSpace(v)
  328. v = strings.Trim(v, "'")
  329. col.SetOptions[v] = k
  330. }
  331. } else {
  332. lens := strings.Split(cts[1][0:idx], ",")
  333. len1, err = strconv.Atoi(strings.TrimSpace(lens[0]))
  334. if err != nil {
  335. return nil, nil, err
  336. }
  337. if len(lens) == 2 {
  338. len2, err = strconv.Atoi(lens[1])
  339. if err != nil {
  340. return nil, nil, err
  341. }
  342. }
  343. }
  344. }
  345. if colType == "FLOAT UNSIGNED" {
  346. colType = "FLOAT"
  347. }
  348. col.Length = len1
  349. col.Length2 = len2
  350. if _, ok := core.SqlTypes[colType]; ok {
  351. col.SQLType = core.SQLType{Name: colType, DefaultLength: len1, DefaultLength2: len2}
  352. } else {
  353. return nil, nil, fmt.Errorf("Unknown colType %v", colType)
  354. }
  355. if colKey == "PRI" {
  356. col.IsPrimaryKey = true
  357. }
  358. if colKey == "UNI" {
  359. //col.is
  360. }
  361. if extra == "auto_increment" {
  362. col.IsAutoIncrement = true
  363. }
  364. if col.SQLType.IsText() || col.SQLType.IsTime() {
  365. if col.Default != "" {
  366. col.Default = "'" + col.Default + "'"
  367. } else {
  368. if col.DefaultIsEmpty {
  369. col.Default = "''"
  370. }
  371. }
  372. }
  373. cols[col.Name] = col
  374. colSeq = append(colSeq, col.Name)
  375. }
  376. return colSeq, cols, nil
  377. }
  378. func (db *mysql) GetTables() ([]*core.Table, error) {
  379. args := []interface{}{db.DbName}
  380. s := "SELECT `TABLE_NAME`, `ENGINE`, `TABLE_ROWS`, `AUTO_INCREMENT`, `TABLE_COMMENT` from " +
  381. "`INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`=? AND (`ENGINE`='MyISAM' OR `ENGINE` = 'InnoDB' OR `ENGINE` = 'TokuDB')"
  382. db.LogSQL(s, args)
  383. rows, err := db.DB().Query(s, args...)
  384. if err != nil {
  385. return nil, err
  386. }
  387. defer rows.Close()
  388. tables := make([]*core.Table, 0)
  389. for rows.Next() {
  390. table := core.NewEmptyTable()
  391. var name, engine, tableRows, comment string
  392. var autoIncr *string
  393. err = rows.Scan(&name, &engine, &tableRows, &autoIncr, &comment)
  394. if err != nil {
  395. return nil, err
  396. }
  397. table.Name = name
  398. table.Comment = comment
  399. table.StoreEngine = engine
  400. tables = append(tables, table)
  401. }
  402. return tables, nil
  403. }
  404. func (db *mysql) GetIndexes(tableName string) (map[string]*core.Index, error) {
  405. args := []interface{}{db.DbName, tableName}
  406. s := "SELECT `INDEX_NAME`, `NON_UNIQUE`, `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`STATISTICS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ?"
  407. db.LogSQL(s, args)
  408. rows, err := db.DB().Query(s, args...)
  409. if err != nil {
  410. return nil, err
  411. }
  412. defer rows.Close()
  413. indexes := make(map[string]*core.Index, 0)
  414. for rows.Next() {
  415. var indexType int
  416. var indexName, colName, nonUnique string
  417. err = rows.Scan(&indexName, &nonUnique, &colName)
  418. if err != nil {
  419. return nil, err
  420. }
  421. if indexName == "PRIMARY" {
  422. continue
  423. }
  424. if "YES" == nonUnique || nonUnique == "1" {
  425. indexType = core.IndexType
  426. } else {
  427. indexType = core.UniqueType
  428. }
  429. colName = strings.Trim(colName, "` ")
  430. var isRegular bool
  431. if strings.HasPrefix(indexName, "IDX_"+tableName) || strings.HasPrefix(indexName, "UQE_"+tableName) {
  432. indexName = indexName[5+len(tableName):]
  433. isRegular = true
  434. }
  435. var index *core.Index
  436. var ok bool
  437. if index, ok = indexes[indexName]; !ok {
  438. index = new(core.Index)
  439. index.IsRegular = isRegular
  440. index.Type = indexType
  441. index.Name = indexName
  442. indexes[indexName] = index
  443. }
  444. index.AddColumn(colName)
  445. }
  446. return indexes, nil
  447. }
  448. func (db *mysql) Filters() []core.Filter {
  449. return []core.Filter{&core.IdFilter{}}
  450. }
  451. type mymysqlDriver struct {
  452. }
  453. func (p *mymysqlDriver) Parse(driverName, dataSourceName string) (*core.Uri, error) {
  454. db := &core.Uri{DbType: core.MYSQL}
  455. pd := strings.SplitN(dataSourceName, "*", 2)
  456. if len(pd) == 2 {
  457. // Parse protocol part of URI
  458. p := strings.SplitN(pd[0], ":", 2)
  459. if len(p) != 2 {
  460. return nil, errors.New("Wrong protocol part of URI")
  461. }
  462. db.Proto = p[0]
  463. options := strings.Split(p[1], ",")
  464. db.Raddr = options[0]
  465. for _, o := range options[1:] {
  466. kv := strings.SplitN(o, "=", 2)
  467. var k, v string
  468. if len(kv) == 2 {
  469. k, v = kv[0], kv[1]
  470. } else {
  471. k, v = o, "true"
  472. }
  473. switch k {
  474. case "laddr":
  475. db.Laddr = v
  476. case "timeout":
  477. to, err := time.ParseDuration(v)
  478. if err != nil {
  479. return nil, err
  480. }
  481. db.Timeout = to
  482. default:
  483. return nil, errors.New("Unknown option: " + k)
  484. }
  485. }
  486. // Remove protocol part
  487. pd = pd[1:]
  488. }
  489. // Parse database part of URI
  490. dup := strings.SplitN(pd[0], "/", 3)
  491. if len(dup) != 3 {
  492. return nil, errors.New("Wrong database part of URI")
  493. }
  494. db.DbName = dup[0]
  495. db.User = dup[1]
  496. db.Passwd = dup[2]
  497. return db, nil
  498. }
  499. type mysqlDriver struct {
  500. }
  501. func (p *mysqlDriver) Parse(driverName, dataSourceName string) (*core.Uri, error) {
  502. dsnPattern := regexp.MustCompile(
  503. `^(?:(?P<user>.*?)(?::(?P<passwd>.*))?@)?` + // [user[:password]@]
  504. `(?:(?P<net>[^\(]*)(?:\((?P<addr>[^\)]*)\))?)?` + // [net[(addr)]]
  505. `\/(?P<dbname>.*?)` + // /dbname
  506. `(?:\?(?P<params>[^\?]*))?$`) // [?param1=value1&paramN=valueN]
  507. matches := dsnPattern.FindStringSubmatch(dataSourceName)
  508. //tlsConfigRegister := make(map[string]*tls.Config)
  509. names := dsnPattern.SubexpNames()
  510. uri := &core.Uri{DbType: core.MYSQL}
  511. for i, match := range matches {
  512. switch names[i] {
  513. case "dbname":
  514. uri.DbName = match
  515. case "params":
  516. if len(match) > 0 {
  517. kvs := strings.Split(match, "&")
  518. for _, kv := range kvs {
  519. splits := strings.Split(kv, "=")
  520. if len(splits) == 2 {
  521. switch splits[0] {
  522. case "charset":
  523. uri.Charset = splits[1]
  524. }
  525. }
  526. }
  527. }
  528. }
  529. }
  530. return uri, nil
  531. }