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.

dialect_mysql.go 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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. "xorm.io/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. rowFormat string
  171. }
  172. func (db *mysql) Init(d *core.DB, uri *core.Uri, drivername, dataSourceName string) error {
  173. return db.Base.Init(d, db, uri, drivername, dataSourceName)
  174. }
  175. func (db *mysql) SetParams(params map[string]string) {
  176. rowFormat, ok := params["rowFormat"]
  177. if ok {
  178. var t = strings.ToUpper(rowFormat)
  179. switch t {
  180. case "COMPACT":
  181. fallthrough
  182. case "REDUNDANT":
  183. fallthrough
  184. case "DYNAMIC":
  185. fallthrough
  186. case "COMPRESSED":
  187. db.rowFormat = t
  188. break
  189. default:
  190. break
  191. }
  192. }
  193. }
  194. func (db *mysql) SqlType(c *core.Column) string {
  195. var res string
  196. switch t := c.SQLType.Name; t {
  197. case core.Bool:
  198. res = core.TinyInt
  199. c.Length = 1
  200. case core.Serial:
  201. c.IsAutoIncrement = true
  202. c.IsPrimaryKey = true
  203. c.Nullable = false
  204. res = core.Int
  205. case core.BigSerial:
  206. c.IsAutoIncrement = true
  207. c.IsPrimaryKey = true
  208. c.Nullable = false
  209. res = core.BigInt
  210. case core.Bytea:
  211. res = core.Blob
  212. case core.TimeStampz:
  213. res = core.Char
  214. c.Length = 64
  215. case core.Enum: //mysql enum
  216. res = core.Enum
  217. res += "("
  218. opts := ""
  219. for v := range c.EnumOptions {
  220. opts += fmt.Sprintf(",'%v'", v)
  221. }
  222. res += strings.TrimLeft(opts, ",")
  223. res += ")"
  224. case core.Set: //mysql set
  225. res = core.Set
  226. res += "("
  227. opts := ""
  228. for v := range c.SetOptions {
  229. opts += fmt.Sprintf(",'%v'", v)
  230. }
  231. res += strings.TrimLeft(opts, ",")
  232. res += ")"
  233. case core.NVarchar:
  234. res = core.Varchar
  235. case core.Uuid:
  236. res = core.Varchar
  237. c.Length = 40
  238. case core.Json:
  239. res = core.Text
  240. default:
  241. res = t
  242. }
  243. hasLen1 := (c.Length > 0)
  244. hasLen2 := (c.Length2 > 0)
  245. if res == core.BigInt && !hasLen1 && !hasLen2 {
  246. c.Length = 20
  247. hasLen1 = true
  248. }
  249. if hasLen2 {
  250. res += "(" + strconv.Itoa(c.Length) + "," + strconv.Itoa(c.Length2) + ")"
  251. } else if hasLen1 {
  252. res += "(" + strconv.Itoa(c.Length) + ")"
  253. }
  254. return res
  255. }
  256. func (db *mysql) SupportInsertMany() bool {
  257. return true
  258. }
  259. func (db *mysql) IsReserved(name string) bool {
  260. _, ok := mysqlReservedWords[name]
  261. return ok
  262. }
  263. func (db *mysql) Quote(name string) string {
  264. return "`" + name + "`"
  265. }
  266. func (db *mysql) QuoteStr() string {
  267. return "`"
  268. }
  269. func (db *mysql) SupportEngine() bool {
  270. return true
  271. }
  272. func (db *mysql) AutoIncrStr() string {
  273. return "AUTO_INCREMENT"
  274. }
  275. func (db *mysql) SupportCharset() bool {
  276. return true
  277. }
  278. func (db *mysql) IndexOnTable() bool {
  279. return true
  280. }
  281. func (db *mysql) IndexCheckSql(tableName, idxName string) (string, []interface{}) {
  282. args := []interface{}{db.DbName, tableName, idxName}
  283. sql := "SELECT `INDEX_NAME` FROM `INFORMATION_SCHEMA`.`STATISTICS`"
  284. sql += " WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? AND `INDEX_NAME`=?"
  285. return sql, args
  286. }
  287. /*func (db *mysql) ColumnCheckSql(tableName, colName string) (string, []interface{}) {
  288. args := []interface{}{db.DbName, tableName, colName}
  289. sql := "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ? AND `COLUMN_NAME` = ?"
  290. return sql, args
  291. }*/
  292. func (db *mysql) TableCheckSql(tableName string) (string, []interface{}) {
  293. args := []interface{}{db.DbName, tableName}
  294. sql := "SELECT `TABLE_NAME` from `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`=? and `TABLE_NAME`=?"
  295. return sql, args
  296. }
  297. func (db *mysql) GetColumns(tableName string) ([]string, map[string]*core.Column, error) {
  298. args := []interface{}{db.DbName, tableName}
  299. s := "SELECT `COLUMN_NAME`, `IS_NULLABLE`, `COLUMN_DEFAULT`, `COLUMN_TYPE`," +
  300. " `COLUMN_KEY`, `EXTRA`,`COLUMN_COMMENT` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ?"
  301. db.LogSQL(s, args)
  302. rows, err := db.DB().Query(s, args...)
  303. if err != nil {
  304. return nil, nil, err
  305. }
  306. defer rows.Close()
  307. cols := make(map[string]*core.Column)
  308. colSeq := make([]string, 0)
  309. for rows.Next() {
  310. col := new(core.Column)
  311. col.Indexes = make(map[string]int)
  312. var columnName, isNullable, colType, colKey, extra, comment string
  313. var colDefault *string
  314. err = rows.Scan(&columnName, &isNullable, &colDefault, &colType, &colKey, &extra, &comment)
  315. if err != nil {
  316. return nil, nil, err
  317. }
  318. col.Name = strings.Trim(columnName, "` ")
  319. col.Comment = comment
  320. if "YES" == isNullable {
  321. col.Nullable = true
  322. }
  323. if colDefault != nil {
  324. col.Default = *colDefault
  325. if col.Default == "" {
  326. col.DefaultIsEmpty = true
  327. }
  328. }
  329. cts := strings.Split(colType, "(")
  330. colName := cts[0]
  331. colType = strings.ToUpper(colName)
  332. var len1, len2 int
  333. if len(cts) == 2 {
  334. idx := strings.Index(cts[1], ")")
  335. if colType == core.Enum && cts[1][0] == '\'' { //enum
  336. options := strings.Split(cts[1][0:idx], ",")
  337. col.EnumOptions = make(map[string]int)
  338. for k, v := range options {
  339. v = strings.TrimSpace(v)
  340. v = strings.Trim(v, "'")
  341. col.EnumOptions[v] = k
  342. }
  343. } else if colType == core.Set && cts[1][0] == '\'' {
  344. options := strings.Split(cts[1][0:idx], ",")
  345. col.SetOptions = make(map[string]int)
  346. for k, v := range options {
  347. v = strings.TrimSpace(v)
  348. v = strings.Trim(v, "'")
  349. col.SetOptions[v] = k
  350. }
  351. } else {
  352. lens := strings.Split(cts[1][0:idx], ",")
  353. len1, err = strconv.Atoi(strings.TrimSpace(lens[0]))
  354. if err != nil {
  355. return nil, nil, err
  356. }
  357. if len(lens) == 2 {
  358. len2, err = strconv.Atoi(lens[1])
  359. if err != nil {
  360. return nil, nil, err
  361. }
  362. }
  363. }
  364. }
  365. if colType == "FLOAT UNSIGNED" {
  366. colType = "FLOAT"
  367. }
  368. if colType == "DOUBLE UNSIGNED" {
  369. colType = "DOUBLE"
  370. }
  371. col.Length = len1
  372. col.Length2 = len2
  373. if _, ok := core.SqlTypes[colType]; ok {
  374. col.SQLType = core.SQLType{Name: colType, DefaultLength: len1, DefaultLength2: len2}
  375. } else {
  376. return nil, nil, fmt.Errorf("Unknown colType %v", colType)
  377. }
  378. if colKey == "PRI" {
  379. col.IsPrimaryKey = true
  380. }
  381. if colKey == "UNI" {
  382. //col.is
  383. }
  384. if extra == "auto_increment" {
  385. col.IsAutoIncrement = true
  386. }
  387. if col.SQLType.IsText() || col.SQLType.IsTime() {
  388. if col.Default != "" {
  389. col.Default = "'" + col.Default + "'"
  390. } else {
  391. if col.DefaultIsEmpty {
  392. col.Default = "''"
  393. }
  394. }
  395. }
  396. cols[col.Name] = col
  397. colSeq = append(colSeq, col.Name)
  398. }
  399. return colSeq, cols, nil
  400. }
  401. func (db *mysql) GetTables() ([]*core.Table, error) {
  402. args := []interface{}{db.DbName}
  403. s := "SELECT `TABLE_NAME`, `ENGINE`, `TABLE_ROWS`, `AUTO_INCREMENT`, `TABLE_COMMENT` from " +
  404. "`INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`=? AND (`ENGINE`='MyISAM' OR `ENGINE` = 'InnoDB' OR `ENGINE` = 'TokuDB')"
  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. tables := make([]*core.Table, 0)
  412. for rows.Next() {
  413. table := core.NewEmptyTable()
  414. var name, engine, tableRows, comment string
  415. var autoIncr *string
  416. err = rows.Scan(&name, &engine, &tableRows, &autoIncr, &comment)
  417. if err != nil {
  418. return nil, err
  419. }
  420. table.Name = name
  421. table.Comment = comment
  422. table.StoreEngine = engine
  423. tables = append(tables, table)
  424. }
  425. return tables, nil
  426. }
  427. func (db *mysql) GetIndexes(tableName string) (map[string]*core.Index, error) {
  428. args := []interface{}{db.DbName, tableName}
  429. s := "SELECT `INDEX_NAME`, `NON_UNIQUE`, `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`STATISTICS` WHERE `TABLE_SCHEMA` = ? AND `TABLE_NAME` = ?"
  430. db.LogSQL(s, args)
  431. rows, err := db.DB().Query(s, args...)
  432. if err != nil {
  433. return nil, err
  434. }
  435. defer rows.Close()
  436. indexes := make(map[string]*core.Index, 0)
  437. for rows.Next() {
  438. var indexType int
  439. var indexName, colName, nonUnique string
  440. err = rows.Scan(&indexName, &nonUnique, &colName)
  441. if err != nil {
  442. return nil, err
  443. }
  444. if indexName == "PRIMARY" {
  445. continue
  446. }
  447. if "YES" == nonUnique || nonUnique == "1" {
  448. indexType = core.IndexType
  449. } else {
  450. indexType = core.UniqueType
  451. }
  452. colName = strings.Trim(colName, "` ")
  453. var isRegular bool
  454. if strings.HasPrefix(indexName, "IDX_"+tableName) || strings.HasPrefix(indexName, "UQE_"+tableName) {
  455. indexName = indexName[5+len(tableName):]
  456. isRegular = true
  457. }
  458. var index *core.Index
  459. var ok bool
  460. if index, ok = indexes[indexName]; !ok {
  461. index = new(core.Index)
  462. index.IsRegular = isRegular
  463. index.Type = indexType
  464. index.Name = indexName
  465. indexes[indexName] = index
  466. }
  467. index.AddColumn(colName)
  468. }
  469. return indexes, nil
  470. }
  471. func (db *mysql) CreateTableSql(table *core.Table, tableName, storeEngine, charset string) string {
  472. var sql string
  473. sql = "CREATE TABLE IF NOT EXISTS "
  474. if tableName == "" {
  475. tableName = table.Name
  476. }
  477. sql += db.Quote(tableName)
  478. sql += " ("
  479. if len(table.ColumnsSeq()) > 0 {
  480. pkList := table.PrimaryKeys
  481. for _, colName := range table.ColumnsSeq() {
  482. col := table.GetColumn(colName)
  483. if col.IsPrimaryKey && len(pkList) == 1 {
  484. sql += col.String(db)
  485. } else {
  486. sql += col.StringNoPk(db)
  487. }
  488. sql = strings.TrimSpace(sql)
  489. if len(col.Comment) > 0 {
  490. sql += " COMMENT '" + col.Comment + "'"
  491. }
  492. sql += ", "
  493. }
  494. if len(pkList) > 1 {
  495. sql += "PRIMARY KEY ( "
  496. sql += db.Quote(strings.Join(pkList, db.Quote(",")))
  497. sql += " ), "
  498. }
  499. sql = sql[:len(sql)-2]
  500. }
  501. sql += ")"
  502. if storeEngine != "" {
  503. sql += " ENGINE=" + storeEngine
  504. }
  505. if len(charset) == 0 {
  506. charset = db.URI().Charset
  507. }
  508. if len(charset) != 0 {
  509. sql += " DEFAULT CHARSET " + charset
  510. }
  511. if db.rowFormat != "" {
  512. sql += " ROW_FORMAT=" + db.rowFormat
  513. }
  514. return sql
  515. }
  516. func (db *mysql) Filters() []core.Filter {
  517. return []core.Filter{&core.IdFilter{}}
  518. }
  519. type mymysqlDriver struct {
  520. }
  521. func (p *mymysqlDriver) Parse(driverName, dataSourceName string) (*core.Uri, error) {
  522. db := &core.Uri{DbType: core.MYSQL}
  523. pd := strings.SplitN(dataSourceName, "*", 2)
  524. if len(pd) == 2 {
  525. // Parse protocol part of URI
  526. p := strings.SplitN(pd[0], ":", 2)
  527. if len(p) != 2 {
  528. return nil, errors.New("Wrong protocol part of URI")
  529. }
  530. db.Proto = p[0]
  531. options := strings.Split(p[1], ",")
  532. db.Raddr = options[0]
  533. for _, o := range options[1:] {
  534. kv := strings.SplitN(o, "=", 2)
  535. var k, v string
  536. if len(kv) == 2 {
  537. k, v = kv[0], kv[1]
  538. } else {
  539. k, v = o, "true"
  540. }
  541. switch k {
  542. case "laddr":
  543. db.Laddr = v
  544. case "timeout":
  545. to, err := time.ParseDuration(v)
  546. if err != nil {
  547. return nil, err
  548. }
  549. db.Timeout = to
  550. default:
  551. return nil, errors.New("Unknown option: " + k)
  552. }
  553. }
  554. // Remove protocol part
  555. pd = pd[1:]
  556. }
  557. // Parse database part of URI
  558. dup := strings.SplitN(pd[0], "/", 3)
  559. if len(dup) != 3 {
  560. return nil, errors.New("Wrong database part of URI")
  561. }
  562. db.DbName = dup[0]
  563. db.User = dup[1]
  564. db.Passwd = dup[2]
  565. return db, nil
  566. }
  567. type mysqlDriver struct {
  568. }
  569. func (p *mysqlDriver) Parse(driverName, dataSourceName string) (*core.Uri, error) {
  570. dsnPattern := regexp.MustCompile(
  571. `^(?:(?P<user>.*?)(?::(?P<passwd>.*))?@)?` + // [user[:password]@]
  572. `(?:(?P<net>[^\(]*)(?:\((?P<addr>[^\)]*)\))?)?` + // [net[(addr)]]
  573. `\/(?P<dbname>.*?)` + // /dbname
  574. `(?:\?(?P<params>[^\?]*))?$`) // [?param1=value1&paramN=valueN]
  575. matches := dsnPattern.FindStringSubmatch(dataSourceName)
  576. //tlsConfigRegister := make(map[string]*tls.Config)
  577. names := dsnPattern.SubexpNames()
  578. uri := &core.Uri{DbType: core.MYSQL}
  579. for i, match := range matches {
  580. switch names[i] {
  581. case "dbname":
  582. uri.DbName = match
  583. case "params":
  584. if len(match) > 0 {
  585. kvs := strings.Split(match, "&")
  586. for _, kv := range kvs {
  587. splits := strings.Split(kv, "=")
  588. if len(splits) == 2 {
  589. switch splits[0] {
  590. case "charset":
  591. uri.Charset = splits[1]
  592. }
  593. }
  594. }
  595. }
  596. }
  597. }
  598. return uri, nil
  599. }