Chore: Fix SQL related Go variable naming (#28887)
* Chore: Fix variable naming Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
package migrator
|
||||
|
||||
type MigrationCondition interface {
|
||||
Sql(dialect Dialect) (string, []interface{})
|
||||
SQL(dialect Dialect) (string, []interface{})
|
||||
IsFulfilled(results []map[string][]byte) bool
|
||||
}
|
||||
|
||||
@@ -23,8 +23,8 @@ type IfIndexExistsCondition struct {
|
||||
IndexName string
|
||||
}
|
||||
|
||||
func (c *IfIndexExistsCondition) Sql(dialect Dialect) (string, []interface{}) {
|
||||
return dialect.IndexCheckSql(c.TableName, c.IndexName)
|
||||
func (c *IfIndexExistsCondition) SQL(dialect Dialect) (string, []interface{}) {
|
||||
return dialect.IndexCheckSQL(c.TableName, c.IndexName)
|
||||
}
|
||||
|
||||
type IfIndexNotExistsCondition struct {
|
||||
@@ -33,8 +33,8 @@ type IfIndexNotExistsCondition struct {
|
||||
IndexName string
|
||||
}
|
||||
|
||||
func (c *IfIndexNotExistsCondition) Sql(dialect Dialect) (string, []interface{}) {
|
||||
return dialect.IndexCheckSql(c.TableName, c.IndexName)
|
||||
func (c *IfIndexNotExistsCondition) SQL(dialect Dialect) (string, []interface{}) {
|
||||
return dialect.IndexCheckSQL(c.TableName, c.IndexName)
|
||||
}
|
||||
|
||||
type IfColumnNotExistsCondition struct {
|
||||
@@ -43,6 +43,6 @@ type IfColumnNotExistsCondition struct {
|
||||
ColumnName string
|
||||
}
|
||||
|
||||
func (c *IfColumnNotExistsCondition) Sql(dialect Dialect) (string, []interface{}) {
|
||||
return dialect.ColumnCheckSql(c.TableName, c.ColumnName)
|
||||
func (c *IfColumnNotExistsCondition) SQL(dialect Dialect) (string, []interface{}) {
|
||||
return dialect.ColumnCheckSQL(c.TableName, c.ColumnName)
|
||||
}
|
||||
|
||||
@@ -15,25 +15,25 @@ type Dialect interface {
|
||||
OrStr() string
|
||||
EqStr() string
|
||||
ShowCreateNull() bool
|
||||
SqlType(col *Column) string
|
||||
SQLType(col *Column) string
|
||||
SupportEngine() bool
|
||||
LikeStr() string
|
||||
Default(col *Column) string
|
||||
BooleanStr(bool) string
|
||||
DateTimeFunc(string) string
|
||||
|
||||
CreateIndexSql(tableName string, index *Index) string
|
||||
CreateTableSql(table *Table) string
|
||||
AddColumnSql(tableName string, col *Column) string
|
||||
CreateIndexSQL(tableName string, index *Index) string
|
||||
CreateTableSQL(table *Table) string
|
||||
AddColumnSQL(tableName string, col *Column) string
|
||||
CopyTableData(sourceTable string, targetTable string, sourceCols []string, targetCols []string) string
|
||||
DropTable(tableName string) string
|
||||
DropIndexSql(tableName string, index *Index) string
|
||||
DropIndexSQL(tableName string, index *Index) string
|
||||
|
||||
RenameTable(oldName string, newName string) string
|
||||
UpdateTableSql(tableName string, columns []*Column) string
|
||||
UpdateTableSQL(tableName string, columns []*Column) string
|
||||
|
||||
IndexCheckSql(tableName, indexName string) (string, []interface{})
|
||||
ColumnCheckSql(tableName, columnName string) (string, []interface{})
|
||||
IndexCheckSQL(tableName, indexName string) (string, []interface{})
|
||||
ColumnCheckSQL(tableName, columnName string) (string, []interface{})
|
||||
|
||||
ColString(*Column) string
|
||||
ColStringNoPk(*Column) string
|
||||
@@ -46,7 +46,7 @@ type Dialect interface {
|
||||
|
||||
CleanDB() error
|
||||
TruncateDBTables() error
|
||||
NoOpSql() string
|
||||
NoOpSQL() string
|
||||
|
||||
IsUniqueConstraintViolation(err error) bool
|
||||
ErrorMessage(err error) string
|
||||
@@ -56,12 +56,12 @@ type Dialect interface {
|
||||
type dialectFunc func(*xorm.Engine) Dialect
|
||||
|
||||
var supportedDialects = map[string]dialectFunc{
|
||||
MYSQL: NewMysqlDialect,
|
||||
SQLITE: NewSqlite3Dialect,
|
||||
POSTGRES: NewPostgresDialect,
|
||||
MYSQL + "WithHooks": NewMysqlDialect,
|
||||
SQLITE + "WithHooks": NewSqlite3Dialect,
|
||||
POSTGRES + "WithHooks": NewPostgresDialect,
|
||||
MySQL: NewMysqlDialect,
|
||||
SQLite: NewSQLite3Dialect,
|
||||
Postgres: NewPostgresDialect,
|
||||
MySQL + "WithHooks": NewMysqlDialect,
|
||||
SQLite + "WithHooks": NewSQLite3Dialect,
|
||||
Postgres + "WithHooks": NewPostgresDialect,
|
||||
}
|
||||
|
||||
func NewDialect(engine *xorm.Engine) Dialect {
|
||||
@@ -111,7 +111,7 @@ func (b *BaseDialect) DateTimeFunc(value string) string {
|
||||
return value
|
||||
}
|
||||
|
||||
func (b *BaseDialect) CreateTableSql(table *Table) string {
|
||||
func (b *BaseDialect) CreateTableSQL(table *Table) string {
|
||||
sql := "CREATE TABLE IF NOT EXISTS "
|
||||
sql += b.dialect.Quote(table.Name) + " (\n"
|
||||
|
||||
@@ -145,11 +145,11 @@ func (b *BaseDialect) CreateTableSql(table *Table) string {
|
||||
return sql
|
||||
}
|
||||
|
||||
func (b *BaseDialect) AddColumnSql(tableName string, col *Column) string {
|
||||
func (b *BaseDialect) AddColumnSQL(tableName string, col *Column) string {
|
||||
return fmt.Sprintf("alter table %s ADD COLUMN %s", b.dialect.Quote(tableName), col.StringNoPk(b.dialect))
|
||||
}
|
||||
|
||||
func (b *BaseDialect) CreateIndexSql(tableName string, index *Index) string {
|
||||
func (b *BaseDialect) CreateIndexSQL(tableName string, index *Index) string {
|
||||
quote := b.dialect.Quote
|
||||
var unique string
|
||||
if index.Type == UniqueIndex {
|
||||
@@ -167,20 +167,20 @@ func (b *BaseDialect) CreateIndexSql(tableName string, index *Index) string {
|
||||
}
|
||||
|
||||
func (b *BaseDialect) QuoteColList(cols []string) string {
|
||||
var sourceColsSql = ""
|
||||
var sourceColsSQL = ""
|
||||
for _, col := range cols {
|
||||
sourceColsSql += b.dialect.Quote(col)
|
||||
sourceColsSql += "\n, "
|
||||
sourceColsSQL += b.dialect.Quote(col)
|
||||
sourceColsSQL += "\n, "
|
||||
}
|
||||
return strings.TrimSuffix(sourceColsSql, "\n, ")
|
||||
return strings.TrimSuffix(sourceColsSQL, "\n, ")
|
||||
}
|
||||
|
||||
func (b *BaseDialect) CopyTableData(sourceTable string, targetTable string, sourceCols []string, targetCols []string) string {
|
||||
sourceColsSql := b.QuoteColList(sourceCols)
|
||||
targetColsSql := b.QuoteColList(targetCols)
|
||||
sourceColsSQL := b.QuoteColList(sourceCols)
|
||||
targetColsSQL := b.QuoteColList(targetCols)
|
||||
|
||||
quote := b.dialect.Quote
|
||||
return fmt.Sprintf("INSERT INTO %s (%s) SELECT %s FROM %s", quote(targetTable), targetColsSql, sourceColsSql, quote(sourceTable))
|
||||
return fmt.Sprintf("INSERT INTO %s (%s) SELECT %s FROM %s", quote(targetTable), targetColsSQL, sourceColsSQL, quote(sourceTable))
|
||||
}
|
||||
|
||||
func (b *BaseDialect) DropTable(tableName string) string {
|
||||
@@ -193,24 +193,24 @@ func (b *BaseDialect) RenameTable(oldName string, newName string) string {
|
||||
return fmt.Sprintf("ALTER TABLE %s RENAME TO %s", quote(oldName), quote(newName))
|
||||
}
|
||||
|
||||
func (b *BaseDialect) ColumnCheckSql(tableName, columnName string) (string, []interface{}) {
|
||||
func (b *BaseDialect) ColumnCheckSQL(tableName, columnName string) (string, []interface{}) {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func (b *BaseDialect) DropIndexSql(tableName string, index *Index) string {
|
||||
func (b *BaseDialect) DropIndexSQL(tableName string, index *Index) string {
|
||||
quote := b.dialect.Quote
|
||||
name := index.XName(tableName)
|
||||
return fmt.Sprintf("DROP INDEX %v ON %s", quote(name), quote(tableName))
|
||||
}
|
||||
|
||||
func (b *BaseDialect) UpdateTableSql(tableName string, columns []*Column) string {
|
||||
func (b *BaseDialect) UpdateTableSQL(tableName string, columns []*Column) string {
|
||||
return "-- NOT REQUIRED"
|
||||
}
|
||||
|
||||
func (b *BaseDialect) ColString(col *Column) string {
|
||||
sql := b.dialect.Quote(col.Name) + " "
|
||||
|
||||
sql += b.dialect.SqlType(col) + " "
|
||||
sql += b.dialect.SQLType(col) + " "
|
||||
|
||||
if col.IsPrimaryKey {
|
||||
sql += "PRIMARY KEY "
|
||||
@@ -237,7 +237,7 @@ func (b *BaseDialect) ColString(col *Column) string {
|
||||
func (b *BaseDialect) ColStringNoPk(col *Column) string {
|
||||
sql := b.dialect.Quote(col.Name) + " "
|
||||
|
||||
sql += b.dialect.SqlType(col) + " "
|
||||
sql += b.dialect.SQLType(col) + " "
|
||||
|
||||
if b.dialect.ShowCreateNull() {
|
||||
if col.Nullable {
|
||||
@@ -274,7 +274,7 @@ func (b *BaseDialect) CleanDB() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *BaseDialect) NoOpSql() string {
|
||||
func (b *BaseDialect) NoOpSQL() string {
|
||||
return "SELECT 0;"
|
||||
}
|
||||
|
||||
|
||||
@@ -21,21 +21,21 @@ func (m *MigrationBase) GetCondition() MigrationCondition {
|
||||
return m.Condition
|
||||
}
|
||||
|
||||
type RawSqlMigration struct {
|
||||
type RawSQLMigration struct {
|
||||
MigrationBase
|
||||
|
||||
sql map[string]string
|
||||
}
|
||||
|
||||
func NewRawSqlMigration(sql string) *RawSqlMigration {
|
||||
m := &RawSqlMigration{}
|
||||
func NewRawSQLMigration(sql string) *RawSQLMigration {
|
||||
m := &RawSQLMigration{}
|
||||
if sql != "" {
|
||||
m.Default(sql)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *RawSqlMigration) Sql(dialect Dialect) string {
|
||||
func (m *RawSQLMigration) SQL(dialect Dialect) string {
|
||||
if m.sql != nil {
|
||||
if val := m.sql[dialect.DriverName()]; val != "" {
|
||||
return val
|
||||
@@ -46,10 +46,10 @@ func (m *RawSqlMigration) Sql(dialect Dialect) string {
|
||||
}
|
||||
}
|
||||
|
||||
return dialect.NoOpSql()
|
||||
return dialect.NoOpSQL()
|
||||
}
|
||||
|
||||
func (m *RawSqlMigration) Set(dialect string, sql string) *RawSqlMigration {
|
||||
func (m *RawSQLMigration) Set(dialect string, sql string) *RawSQLMigration {
|
||||
if m.sql == nil {
|
||||
m.sql = make(map[string]string)
|
||||
}
|
||||
@@ -58,23 +58,23 @@ func (m *RawSqlMigration) Set(dialect string, sql string) *RawSqlMigration {
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *RawSqlMigration) Default(sql string) *RawSqlMigration {
|
||||
func (m *RawSQLMigration) Default(sql string) *RawSQLMigration {
|
||||
return m.Set("default", sql)
|
||||
}
|
||||
|
||||
func (m *RawSqlMigration) Sqlite(sql string) *RawSqlMigration {
|
||||
return m.Set(SQLITE, sql)
|
||||
func (m *RawSQLMigration) SQLite(sql string) *RawSQLMigration {
|
||||
return m.Set(SQLite, sql)
|
||||
}
|
||||
|
||||
func (m *RawSqlMigration) Mysql(sql string) *RawSqlMigration {
|
||||
return m.Set(MYSQL, sql)
|
||||
func (m *RawSQLMigration) Mysql(sql string) *RawSQLMigration {
|
||||
return m.Set(MySQL, sql)
|
||||
}
|
||||
|
||||
func (m *RawSqlMigration) Postgres(sql string) *RawSqlMigration {
|
||||
return m.Set(POSTGRES, sql)
|
||||
func (m *RawSQLMigration) Postgres(sql string) *RawSQLMigration {
|
||||
return m.Set(Postgres, sql)
|
||||
}
|
||||
|
||||
func (m *RawSqlMigration) Mssql(sql string) *RawSqlMigration {
|
||||
func (m *RawSQLMigration) Mssql(sql string) *RawSQLMigration {
|
||||
return m.Set(MSSQL, sql)
|
||||
}
|
||||
|
||||
@@ -100,8 +100,8 @@ func (m *AddColumnMigration) Column(col *Column) *AddColumnMigration {
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *AddColumnMigration) Sql(dialect Dialect) string {
|
||||
return dialect.AddColumnSql(m.tableName, m.column)
|
||||
func (m *AddColumnMigration) SQL(dialect Dialect) string {
|
||||
return dialect.AddColumnSQL(m.tableName, m.column)
|
||||
}
|
||||
|
||||
type AddIndexMigration struct {
|
||||
@@ -121,8 +121,8 @@ func (m *AddIndexMigration) Table(tableName string) *AddIndexMigration {
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *AddIndexMigration) Sql(dialect Dialect) string {
|
||||
return dialect.CreateIndexSql(m.tableName, m.index)
|
||||
func (m *AddIndexMigration) SQL(dialect Dialect) string {
|
||||
return dialect.CreateIndexSQL(m.tableName, m.index)
|
||||
}
|
||||
|
||||
type DropIndexMigration struct {
|
||||
@@ -137,11 +137,11 @@ func NewDropIndexMigration(table Table, index *Index) *DropIndexMigration {
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *DropIndexMigration) Sql(dialect Dialect) string {
|
||||
func (m *DropIndexMigration) SQL(dialect Dialect) string {
|
||||
if m.index.Name == "" {
|
||||
m.index.Name = strings.Join(m.index.Cols, "_")
|
||||
}
|
||||
return dialect.DropIndexSql(m.tableName, m.index)
|
||||
return dialect.DropIndexSQL(m.tableName, m.index)
|
||||
}
|
||||
|
||||
type AddTableMigration struct {
|
||||
@@ -158,8 +158,8 @@ func NewAddTableMigration(table Table) *AddTableMigration {
|
||||
return &AddTableMigration{table: table}
|
||||
}
|
||||
|
||||
func (m *AddTableMigration) Sql(d Dialect) string {
|
||||
return d.CreateTableSql(&m.table)
|
||||
func (m *AddTableMigration) SQL(d Dialect) string {
|
||||
return d.CreateTableSQL(&m.table)
|
||||
}
|
||||
|
||||
type DropTableMigration struct {
|
||||
@@ -171,7 +171,7 @@ func NewDropTableMigration(tableName string) *DropTableMigration {
|
||||
return &DropTableMigration{tableName: tableName}
|
||||
}
|
||||
|
||||
func (m *DropTableMigration) Sql(d Dialect) string {
|
||||
func (m *DropTableMigration) SQL(d Dialect) string {
|
||||
return d.DropTable(m.tableName)
|
||||
}
|
||||
|
||||
@@ -191,7 +191,7 @@ func (m *RenameTableMigration) Rename(oldName string, newName string) *RenameTab
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *RenameTableMigration) Sql(d Dialect) string {
|
||||
func (m *RenameTableMigration) SQL(d Dialect) string {
|
||||
return d.RenameTable(m.oldName, m.newName)
|
||||
}
|
||||
|
||||
@@ -213,7 +213,7 @@ func NewCopyTableDataMigration(targetTable string, sourceTable string, colMap ma
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *CopyTableDataMigration) Sql(d Dialect) string {
|
||||
func (m *CopyTableDataMigration) SQL(d Dialect) string {
|
||||
return d.CopyTableData(m.sourceTable, m.targetTable, m.sourceCols, m.targetCols)
|
||||
}
|
||||
|
||||
@@ -227,6 +227,6 @@ func NewTableCharsetMigration(tableName string, columns []*Column) *TableCharset
|
||||
return &TableCharsetMigration{tableName: tableName, columns: columns}
|
||||
}
|
||||
|
||||
func (m *TableCharsetMigration) Sql(d Dialect) string {
|
||||
return d.UpdateTableSql(m.tableName, m.columns)
|
||||
func (m *TableCharsetMigration) SQL(d Dialect) string {
|
||||
return d.UpdateTableSQL(m.tableName, m.columns)
|
||||
}
|
||||
|
||||
@@ -20,8 +20,8 @@ type Migrator struct {
|
||||
|
||||
type MigrationLog struct {
|
||||
Id int64
|
||||
MigrationId string
|
||||
Sql string
|
||||
MigrationID string `xorm:"migration_id"`
|
||||
SQL string `xorm:"sql"`
|
||||
Success bool
|
||||
Error string
|
||||
Timestamp time.Time
|
||||
@@ -65,7 +65,7 @@ func (mg *Migrator) GetMigrationLog() (map[string]MigrationLog, error) {
|
||||
if !logItem.Success {
|
||||
continue
|
||||
}
|
||||
logMap[logItem.MigrationId] = logItem
|
||||
logMap[logItem.MigrationID] = logItem
|
||||
}
|
||||
|
||||
return logMap, nil
|
||||
@@ -87,11 +87,11 @@ func (mg *Migrator) Start() error {
|
||||
continue
|
||||
}
|
||||
|
||||
sql := m.Sql(mg.Dialect)
|
||||
sql := m.SQL(mg.Dialect)
|
||||
|
||||
record := MigrationLog{
|
||||
MigrationId: m.Id(),
|
||||
Sql: sql,
|
||||
MigrationID: m.Id(),
|
||||
SQL: sql,
|
||||
Timestamp: time.Now(),
|
||||
}
|
||||
|
||||
@@ -122,7 +122,7 @@ func (mg *Migrator) exec(m Migration, sess *xorm.Session) error {
|
||||
|
||||
condition := m.GetCondition()
|
||||
if condition != nil {
|
||||
sql, args := condition.Sql(mg.Dialect)
|
||||
sql, args := condition.SQL(mg.Dialect)
|
||||
|
||||
if sql != "" {
|
||||
mg.Logger.Debug("Executing migration condition sql", "id", m.Id(), "sql", sql, "args", args)
|
||||
@@ -144,7 +144,7 @@ func (mg *Migrator) exec(m Migration, sess *xorm.Session) error {
|
||||
mg.Logger.Debug("Executing code migration", "id", m.Id())
|
||||
err = codeMigration.Exec(sess, mg)
|
||||
} else {
|
||||
sql := m.Sql(mg.Dialect)
|
||||
sql := m.SQL(mg.Dialect)
|
||||
mg.Logger.Debug("Executing sql migration", "id", m.Id(), "sql", sql)
|
||||
_, err = sess.Exec(sql)
|
||||
}
|
||||
|
||||
@@ -11,38 +11,38 @@ import (
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
type Mysql struct {
|
||||
type MySQLDialect struct {
|
||||
BaseDialect
|
||||
}
|
||||
|
||||
func NewMysqlDialect(engine *xorm.Engine) Dialect {
|
||||
d := Mysql{}
|
||||
d := MySQLDialect{}
|
||||
d.BaseDialect.dialect = &d
|
||||
d.BaseDialect.engine = engine
|
||||
d.BaseDialect.driverName = MYSQL
|
||||
d.BaseDialect.driverName = MySQL
|
||||
return &d
|
||||
}
|
||||
|
||||
func (db *Mysql) SupportEngine() bool {
|
||||
func (db *MySQLDialect) SupportEngine() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (db *Mysql) Quote(name string) string {
|
||||
func (db *MySQLDialect) Quote(name string) string {
|
||||
return "`" + name + "`"
|
||||
}
|
||||
|
||||
func (db *Mysql) AutoIncrStr() string {
|
||||
func (db *MySQLDialect) AutoIncrStr() string {
|
||||
return "AUTO_INCREMENT"
|
||||
}
|
||||
|
||||
func (db *Mysql) BooleanStr(value bool) string {
|
||||
func (db *MySQLDialect) BooleanStr(value bool) string {
|
||||
if value {
|
||||
return "1"
|
||||
}
|
||||
return "0"
|
||||
}
|
||||
|
||||
func (db *Mysql) SqlType(c *Column) string {
|
||||
func (db *MySQLDialect) SQLType(c *Column) string {
|
||||
var res string
|
||||
switch c.Type {
|
||||
case DB_Bool:
|
||||
@@ -91,7 +91,7 @@ func (db *Mysql) SqlType(c *Column) string {
|
||||
return res
|
||||
}
|
||||
|
||||
func (db *Mysql) UpdateTableSql(tableName string, columns []*Column) string {
|
||||
func (db *MySQLDialect) UpdateTableSQL(tableName string, columns []*Column) string {
|
||||
var statements = []string{}
|
||||
|
||||
statements = append(statements, "DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci")
|
||||
@@ -103,19 +103,19 @@ func (db *Mysql) UpdateTableSql(tableName string, columns []*Column) string {
|
||||
return "ALTER TABLE " + db.Quote(tableName) + " " + strings.Join(statements, ", ") + ";"
|
||||
}
|
||||
|
||||
func (db *Mysql) IndexCheckSql(tableName, indexName string) (string, []interface{}) {
|
||||
func (db *MySQLDialect) IndexCheckSQL(tableName, indexName string) (string, []interface{}) {
|
||||
args := []interface{}{tableName, indexName}
|
||||
sql := "SELECT 1 FROM " + db.Quote("INFORMATION_SCHEMA") + "." + db.Quote("STATISTICS") + " WHERE " + db.Quote("TABLE_SCHEMA") + " = DATABASE() AND " + db.Quote("TABLE_NAME") + "=? AND " + db.Quote("INDEX_NAME") + "=?"
|
||||
return sql, args
|
||||
}
|
||||
|
||||
func (db *Mysql) ColumnCheckSql(tableName, columnName string) (string, []interface{}) {
|
||||
func (db *MySQLDialect) ColumnCheckSQL(tableName, columnName string) (string, []interface{}) {
|
||||
args := []interface{}{tableName, columnName}
|
||||
sql := "SELECT 1 FROM " + db.Quote("INFORMATION_SCHEMA") + "." + db.Quote("COLUMNS") + " WHERE " + db.Quote("TABLE_SCHEMA") + " = DATABASE() AND " + db.Quote("TABLE_NAME") + "=? AND " + db.Quote("COLUMN_NAME") + "=?"
|
||||
return sql, args
|
||||
}
|
||||
|
||||
func (db *Mysql) CleanDB() error {
|
||||
func (db *MySQLDialect) CleanDB() error {
|
||||
tables, err := db.engine.DBMetas()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -140,7 +140,7 @@ func (db *Mysql) CleanDB() error {
|
||||
|
||||
// TruncateDBTables truncates all the tables.
|
||||
// A special case is the dashboard_acl table where we keep the default permissions.
|
||||
func (db *Mysql) TruncateDBTables() error {
|
||||
func (db *MySQLDialect) TruncateDBTables() error {
|
||||
tables, err := db.engine.DBMetas()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -168,7 +168,7 @@ func (db *Mysql) TruncateDBTables() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *Mysql) isThisError(err error, errcode uint16) bool {
|
||||
func (db *MySQLDialect) isThisError(err error, errcode uint16) bool {
|
||||
if driverErr, ok := err.(*mysql.MySQLError); ok {
|
||||
if driverErr.Number == errcode {
|
||||
return true
|
||||
@@ -178,17 +178,17 @@ func (db *Mysql) isThisError(err error, errcode uint16) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (db *Mysql) IsUniqueConstraintViolation(err error) bool {
|
||||
func (db *MySQLDialect) IsUniqueConstraintViolation(err error) bool {
|
||||
return db.isThisError(err, mysqlerr.ER_DUP_ENTRY)
|
||||
}
|
||||
|
||||
func (db *Mysql) ErrorMessage(err error) string {
|
||||
func (db *MySQLDialect) ErrorMessage(err error) string {
|
||||
if driverErr, ok := err.(*mysql.MySQLError); ok {
|
||||
return driverErr.Message
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (db *Mysql) IsDeadlock(err error) bool {
|
||||
func (db *MySQLDialect) IsDeadlock(err error) bool {
|
||||
return db.isThisError(err, mysqlerr.ER_LOCK_DEADLOCK)
|
||||
}
|
||||
|
||||
@@ -11,39 +11,39 @@ import (
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
type Postgres struct {
|
||||
type PostgresDialect struct {
|
||||
BaseDialect
|
||||
}
|
||||
|
||||
func NewPostgresDialect(engine *xorm.Engine) Dialect {
|
||||
d := Postgres{}
|
||||
d := PostgresDialect{}
|
||||
d.BaseDialect.dialect = &d
|
||||
d.BaseDialect.engine = engine
|
||||
d.BaseDialect.driverName = POSTGRES
|
||||
d.BaseDialect.driverName = Postgres
|
||||
return &d
|
||||
}
|
||||
|
||||
func (db *Postgres) SupportEngine() bool {
|
||||
func (db *PostgresDialect) SupportEngine() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (db *Postgres) Quote(name string) string {
|
||||
func (db *PostgresDialect) Quote(name string) string {
|
||||
return "\"" + name + "\""
|
||||
}
|
||||
|
||||
func (db *Postgres) LikeStr() string {
|
||||
func (db *PostgresDialect) LikeStr() string {
|
||||
return "ILIKE"
|
||||
}
|
||||
|
||||
func (db *Postgres) AutoIncrStr() string {
|
||||
func (db *PostgresDialect) AutoIncrStr() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (db *Postgres) BooleanStr(value bool) string {
|
||||
func (db *PostgresDialect) BooleanStr(value bool) string {
|
||||
return strconv.FormatBool(value)
|
||||
}
|
||||
|
||||
func (db *Postgres) Default(col *Column) string {
|
||||
func (db *PostgresDialect) Default(col *Column) string {
|
||||
if col.Type == DB_Bool {
|
||||
if col.Default == "0" {
|
||||
return "FALSE"
|
||||
@@ -53,7 +53,7 @@ func (db *Postgres) Default(col *Column) string {
|
||||
return col.Default
|
||||
}
|
||||
|
||||
func (db *Postgres) SqlType(c *Column) string {
|
||||
func (db *PostgresDialect) SQLType(c *Column) string {
|
||||
var res string
|
||||
switch t := c.Type; t {
|
||||
case DB_TinyInt:
|
||||
@@ -103,29 +103,29 @@ func (db *Postgres) SqlType(c *Column) string {
|
||||
return res
|
||||
}
|
||||
|
||||
func (db *Postgres) IndexCheckSql(tableName, indexName string) (string, []interface{}) {
|
||||
func (db *PostgresDialect) IndexCheckSQL(tableName, indexName string) (string, []interface{}) {
|
||||
args := []interface{}{tableName, indexName}
|
||||
sql := "SELECT 1 FROM " + db.Quote("pg_indexes") + " WHERE" + db.Quote("tablename") + "=? AND " + db.Quote("indexname") + "=?"
|
||||
return sql, args
|
||||
}
|
||||
|
||||
func (db *Postgres) DropIndexSql(tableName string, index *Index) string {
|
||||
func (db *PostgresDialect) DropIndexSQL(tableName string, index *Index) string {
|
||||
quote := db.Quote
|
||||
idxName := index.XName(tableName)
|
||||
return fmt.Sprintf("DROP INDEX %v CASCADE", quote(idxName))
|
||||
}
|
||||
|
||||
func (db *Postgres) UpdateTableSql(tableName string, columns []*Column) string {
|
||||
func (db *PostgresDialect) UpdateTableSQL(tableName string, columns []*Column) string {
|
||||
var statements = []string{}
|
||||
|
||||
for _, col := range columns {
|
||||
statements = append(statements, "ALTER "+db.Quote(col.Name)+" TYPE "+db.SqlType(col))
|
||||
statements = append(statements, "ALTER "+db.Quote(col.Name)+" TYPE "+db.SQLType(col))
|
||||
}
|
||||
|
||||
return "ALTER TABLE " + db.Quote(tableName) + " " + strings.Join(statements, ", ") + ";"
|
||||
}
|
||||
|
||||
func (db *Postgres) CleanDB() error {
|
||||
func (db *PostgresDialect) CleanDB() error {
|
||||
sess := db.engine.NewSession()
|
||||
defer sess.Close()
|
||||
|
||||
@@ -142,7 +142,7 @@ func (db *Postgres) CleanDB() error {
|
||||
|
||||
// TruncateDBTables truncates all the tables.
|
||||
// A special case is the dashboard_acl table where we keep the default permissions.
|
||||
func (db *Postgres) TruncateDBTables() error {
|
||||
func (db *PostgresDialect) TruncateDBTables() error {
|
||||
sess := db.engine.NewSession()
|
||||
defer sess.Close()
|
||||
|
||||
@@ -171,7 +171,7 @@ func (db *Postgres) TruncateDBTables() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *Postgres) isThisError(err error, errcode string) bool {
|
||||
func (db *PostgresDialect) isThisError(err error, errcode string) bool {
|
||||
if driverErr, ok := err.(*pq.Error); ok {
|
||||
if string(driverErr.Code) == errcode {
|
||||
return true
|
||||
@@ -181,26 +181,26 @@ func (db *Postgres) isThisError(err error, errcode string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (db *Postgres) ErrorMessage(err error) string {
|
||||
func (db *PostgresDialect) ErrorMessage(err error) string {
|
||||
if driverErr, ok := err.(*pq.Error); ok {
|
||||
return driverErr.Message
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (db *Postgres) isUndefinedTable(err error) bool {
|
||||
func (db *PostgresDialect) isUndefinedTable(err error) bool {
|
||||
return db.isThisError(err, "42P01")
|
||||
}
|
||||
|
||||
func (db *Postgres) IsUniqueConstraintViolation(err error) bool {
|
||||
func (db *PostgresDialect) IsUniqueConstraintViolation(err error) bool {
|
||||
return db.isThisError(err, "23505")
|
||||
}
|
||||
|
||||
func (db *Postgres) IsDeadlock(err error) bool {
|
||||
func (db *PostgresDialect) IsDeadlock(err error) bool {
|
||||
return db.isThisError(err, "40P01")
|
||||
}
|
||||
|
||||
func (db *Postgres) PostInsertId(table string, sess *xorm.Session) error {
|
||||
func (db *PostgresDialect) PostInsertId(table string, sess *xorm.Session) error {
|
||||
if table != "org" {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -8,42 +8,42 @@ import (
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
type Sqlite3 struct {
|
||||
type SQLite3 struct {
|
||||
BaseDialect
|
||||
}
|
||||
|
||||
func NewSqlite3Dialect(engine *xorm.Engine) Dialect {
|
||||
d := Sqlite3{}
|
||||
func NewSQLite3Dialect(engine *xorm.Engine) Dialect {
|
||||
d := SQLite3{}
|
||||
d.BaseDialect.dialect = &d
|
||||
d.BaseDialect.engine = engine
|
||||
d.BaseDialect.driverName = SQLITE
|
||||
d.BaseDialect.driverName = SQLite
|
||||
return &d
|
||||
}
|
||||
|
||||
func (db *Sqlite3) SupportEngine() bool {
|
||||
func (db *SQLite3) SupportEngine() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (db *Sqlite3) Quote(name string) string {
|
||||
func (db *SQLite3) Quote(name string) string {
|
||||
return "`" + name + "`"
|
||||
}
|
||||
|
||||
func (db *Sqlite3) AutoIncrStr() string {
|
||||
func (db *SQLite3) AutoIncrStr() string {
|
||||
return "AUTOINCREMENT"
|
||||
}
|
||||
|
||||
func (db *Sqlite3) BooleanStr(value bool) string {
|
||||
func (db *SQLite3) BooleanStr(value bool) string {
|
||||
if value {
|
||||
return "1"
|
||||
}
|
||||
return "0"
|
||||
}
|
||||
|
||||
func (db *Sqlite3) DateTimeFunc(value string) string {
|
||||
func (db *SQLite3) DateTimeFunc(value string) string {
|
||||
return "datetime(" + value + ")"
|
||||
}
|
||||
|
||||
func (db *Sqlite3) SqlType(c *Column) string {
|
||||
func (db *SQLite3) SQLType(c *Column) string {
|
||||
switch c.Type {
|
||||
case DB_Date, DB_DateTime, DB_TimeStamp, DB_Time:
|
||||
return DB_DateTime
|
||||
@@ -69,26 +69,26 @@ func (db *Sqlite3) SqlType(c *Column) string {
|
||||
}
|
||||
}
|
||||
|
||||
func (db *Sqlite3) IndexCheckSql(tableName, indexName string) (string, []interface{}) {
|
||||
func (db *SQLite3) IndexCheckSQL(tableName, indexName string) (string, []interface{}) {
|
||||
args := []interface{}{tableName, indexName}
|
||||
sql := "SELECT 1 FROM " + db.Quote("sqlite_master") + " WHERE " + db.Quote("type") + "='index' AND " + db.Quote("tbl_name") + "=? AND " + db.Quote("name") + "=?"
|
||||
return sql, args
|
||||
}
|
||||
|
||||
func (db *Sqlite3) DropIndexSql(tableName string, index *Index) string {
|
||||
func (db *SQLite3) DropIndexSQL(tableName string, index *Index) string {
|
||||
quote := db.Quote
|
||||
// var unique string
|
||||
idxName := index.XName(tableName)
|
||||
return fmt.Sprintf("DROP INDEX %v", quote(idxName))
|
||||
}
|
||||
|
||||
func (db *Sqlite3) CleanDB() error {
|
||||
func (db *SQLite3) CleanDB() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// TruncateDBTables deletes all data from all the tables and resets the sequences.
|
||||
// A special case is the dashboard_acl table where we keep the default permissions.
|
||||
func (db *Sqlite3) TruncateDBTables() error {
|
||||
func (db *SQLite3) TruncateDBTables() error {
|
||||
tables, err := db.engine.DBMetas()
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -119,7 +119,7 @@ func (db *Sqlite3) TruncateDBTables() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (db *Sqlite3) isThisError(err error, errcode int) bool {
|
||||
func (db *SQLite3) isThisError(err error, errcode int) bool {
|
||||
if driverErr, ok := err.(sqlite3.Error); ok {
|
||||
if int(driverErr.ExtendedCode) == errcode {
|
||||
return true
|
||||
@@ -129,17 +129,17 @@ func (db *Sqlite3) isThisError(err error, errcode int) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (db *Sqlite3) ErrorMessage(err error) string {
|
||||
func (db *SQLite3) ErrorMessage(err error) string {
|
||||
if driverErr, ok := err.(sqlite3.Error); ok {
|
||||
return driverErr.Error()
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (db *Sqlite3) IsUniqueConstraintViolation(err error) bool {
|
||||
func (db *SQLite3) IsUniqueConstraintViolation(err error) bool {
|
||||
return db.isThisError(err, int(sqlite3.ErrConstraintUnique))
|
||||
}
|
||||
|
||||
func (db *Sqlite3) IsDeadlock(err error) bool {
|
||||
func (db *SQLite3) IsDeadlock(err error) bool {
|
||||
return false // No deadlock
|
||||
}
|
||||
|
||||
@@ -8,14 +8,14 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
POSTGRES = "postgres"
|
||||
SQLITE = "sqlite3"
|
||||
MYSQL = "mysql"
|
||||
Postgres = "postgres"
|
||||
SQLite = "sqlite3"
|
||||
MySQL = "mysql"
|
||||
MSSQL = "mssql"
|
||||
)
|
||||
|
||||
type Migration interface {
|
||||
Sql(dialect Dialect) string
|
||||
SQL(dialect Dialect) string
|
||||
Id() string
|
||||
SetId(string)
|
||||
GetCondition() MigrationCondition
|
||||
|
||||
Reference in New Issue
Block a user