mirror of
https://github.com/grafana/grafana.git
synced 2025-02-12 08:35:43 -06:00
88 lines
1.6 KiB
Go
88 lines
1.6 KiB
Go
package migrations
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
)
|
|
|
|
type Mysql struct {
|
|
BaseDialect
|
|
}
|
|
|
|
func NewMysqlDialect() *Mysql {
|
|
d := Mysql{}
|
|
d.BaseDialect.dialect = &d
|
|
d.BaseDialect.driverName = MYSQL
|
|
return &d
|
|
}
|
|
|
|
func (db *Mysql) Quote(name string) string {
|
|
return "`" + name + "`"
|
|
}
|
|
|
|
func (db *Mysql) QuoteStr() string {
|
|
return "`"
|
|
}
|
|
|
|
func (db *Mysql) AutoIncrStr() string {
|
|
return "AUTO_INCREMENT"
|
|
}
|
|
|
|
func (db *Mysql) SqlType(c *Column) string {
|
|
var res string
|
|
switch c.Type {
|
|
case DB_Bool:
|
|
res = DB_TinyInt
|
|
c.Length = 1
|
|
case DB_Serial:
|
|
c.IsAutoIncrement = true
|
|
c.IsPrimaryKey = true
|
|
c.Nullable = false
|
|
res = DB_Int
|
|
case DB_BigSerial:
|
|
c.IsAutoIncrement = true
|
|
c.IsPrimaryKey = true
|
|
c.Nullable = false
|
|
res = DB_BigInt
|
|
case DB_Bytea:
|
|
res = DB_Blob
|
|
case DB_TimeStampz:
|
|
res = DB_Char
|
|
c.Length = 64
|
|
case DB_NVarchar:
|
|
res = DB_Varchar
|
|
default:
|
|
res = c.Type
|
|
}
|
|
|
|
var hasLen1 bool = (c.Length > 0)
|
|
var hasLen2 bool = (c.Length2 > 0)
|
|
|
|
if res == DB_BigInt && !hasLen1 && !hasLen2 {
|
|
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) ToDBTypeSql(columnType ColumnType, length int) string {
|
|
switch columnType {
|
|
case DB_TYPE_STRING:
|
|
return fmt.Sprintf("NVARCHAR(%d)", length)
|
|
}
|
|
|
|
panic("Unsupported db type")
|
|
}
|
|
|
|
func (db *Mysql) TableCheckSql(tableName string) (string, []interface{}) {
|
|
args := []interface{}{"grafana", tableName}
|
|
sql := "SELECT `TABLE_NAME` from `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_SCHEMA`=? and `TABLE_NAME`=?"
|
|
return sql, args
|
|
}
|