Add method QuotedTableName for Scope
This commit is contained in:
parent
2fff8a7fac
commit
0cba662be0
@ -35,13 +35,13 @@ func Create(scope *Scope) {
|
|||||||
|
|
||||||
if len(columns) == 0 {
|
if len(columns) == 0 {
|
||||||
scope.Raw(fmt.Sprintf("INSERT INTO %v DEFAULT VALUES %v",
|
scope.Raw(fmt.Sprintf("INSERT INTO %v DEFAULT VALUES %v",
|
||||||
scope.TableName(),
|
scope.QuotedTableName(),
|
||||||
scope.Dialect().ReturningStr(scope.PrimaryKey()),
|
scope.Dialect().ReturningStr(scope.PrimaryKey()),
|
||||||
))
|
))
|
||||||
} else {
|
} else {
|
||||||
scope.Raw(fmt.Sprintf(
|
scope.Raw(fmt.Sprintf(
|
||||||
"INSERT INTO %v (%v) VALUES (%v) %v",
|
"INSERT INTO %v (%v) VALUES (%v) %v",
|
||||||
scope.TableName(),
|
scope.QuotedTableName(),
|
||||||
strings.Join(columns, ","),
|
strings.Join(columns, ","),
|
||||||
strings.Join(sqls, ","),
|
strings.Join(sqls, ","),
|
||||||
scope.Dialect().ReturningStr(scope.PrimaryKey()),
|
scope.Dialect().ReturningStr(scope.PrimaryKey()),
|
||||||
|
@ -14,12 +14,12 @@ func Delete(scope *Scope) {
|
|||||||
if !scope.Search.Unscope && scope.HasColumn("DeletedAt") {
|
if !scope.Search.Unscope && scope.HasColumn("DeletedAt") {
|
||||||
scope.Raw(
|
scope.Raw(
|
||||||
fmt.Sprintf("UPDATE %v SET deleted_at=%v %v",
|
fmt.Sprintf("UPDATE %v SET deleted_at=%v %v",
|
||||||
scope.TableName(),
|
scope.QuotedTableName(),
|
||||||
scope.AddToVars(time.Now()),
|
scope.AddToVars(time.Now()),
|
||||||
scope.CombinedConditionSql(),
|
scope.CombinedConditionSql(),
|
||||||
))
|
))
|
||||||
} else {
|
} else {
|
||||||
scope.Raw(fmt.Sprintf("DELETE FROM %v %v", scope.TableName(), scope.CombinedConditionSql()))
|
scope.Raw(fmt.Sprintf("DELETE FROM %v %v", scope.QuotedTableName(), scope.CombinedConditionSql()))
|
||||||
}
|
}
|
||||||
|
|
||||||
scope.Exec()
|
scope.Exec()
|
||||||
|
@ -59,7 +59,7 @@ func Update(scope *Scope) {
|
|||||||
|
|
||||||
scope.Raw(fmt.Sprintf(
|
scope.Raw(fmt.Sprintf(
|
||||||
"UPDATE %v SET %v %v",
|
"UPDATE %v SET %v %v",
|
||||||
scope.TableName(),
|
scope.QuotedTableName(),
|
||||||
strings.Join(sqls, ", "),
|
strings.Join(sqls, ", "),
|
||||||
scope.CombinedConditionSql(),
|
scope.CombinedConditionSql(),
|
||||||
))
|
))
|
||||||
|
8
scope.go
8
scope.go
@ -214,6 +214,14 @@ func (scope *Scope) TableName() string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (scope *Scope) QuotedTableName() string {
|
||||||
|
if scope.Search != nil && len(scope.Search.TableName) > 0 {
|
||||||
|
return scope.Search.TableName
|
||||||
|
} else {
|
||||||
|
return scope.Quote(scope.TableName())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// CombinedConditionSql get combined condition sql
|
// CombinedConditionSql get combined condition sql
|
||||||
func (scope *Scope) CombinedConditionSql() string {
|
func (scope *Scope) CombinedConditionSql() string {
|
||||||
return scope.joinsSql() + scope.whereSql() + scope.groupSql() +
|
return scope.joinsSql() + scope.whereSql() + scope.groupSql() +
|
||||||
|
@ -237,7 +237,7 @@ func (scope *Scope) prepareQuerySql() {
|
|||||||
if scope.Search.Raw {
|
if scope.Search.Raw {
|
||||||
scope.Raw(strings.TrimLeft(scope.CombinedConditionSql(), "WHERE "))
|
scope.Raw(strings.TrimLeft(scope.CombinedConditionSql(), "WHERE "))
|
||||||
} else {
|
} else {
|
||||||
scope.Raw(fmt.Sprintf("SELECT %v FROM %v %v", scope.selectSql(), scope.TableName(), scope.CombinedConditionSql()))
|
scope.Raw(fmt.Sprintf("SELECT %v FROM %v %v", scope.selectSql(), scope.QuotedTableName(), scope.CombinedConditionSql()))
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -414,21 +414,21 @@ func (scope *Scope) createTable() *Scope {
|
|||||||
sqls = append(sqls, scope.Quote(field.DBName)+" "+field.SqlTag)
|
sqls = append(sqls, scope.Quote(field.DBName)+" "+field.SqlTag)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
scope.Raw(fmt.Sprintf("CREATE TABLE %v (%v)", scope.TableName(), strings.Join(sqls, ","))).Exec()
|
scope.Raw(fmt.Sprintf("CREATE TABLE %v (%v)", scope.QuotedTableName(), strings.Join(sqls, ","))).Exec()
|
||||||
return scope
|
return scope
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) dropTable() *Scope {
|
func (scope *Scope) dropTable() *Scope {
|
||||||
scope.Raw(fmt.Sprintf("DROP TABLE %v", scope.TableName())).Exec()
|
scope.Raw(fmt.Sprintf("DROP TABLE %v", scope.QuotedTableName())).Exec()
|
||||||
return scope
|
return scope
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) modifyColumn(column string, typ string) {
|
func (scope *Scope) modifyColumn(column string, typ string) {
|
||||||
scope.Raw(fmt.Sprintf("ALTER TABLE %v MODIFY %v %v", scope.TableName(), scope.Quote(column), typ)).Exec()
|
scope.Raw(fmt.Sprintf("ALTER TABLE %v MODIFY %v %v", scope.QuotedTableName(), scope.Quote(column), typ)).Exec()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) dropColumn(column string) {
|
func (scope *Scope) dropColumn(column string) {
|
||||||
scope.Raw(fmt.Sprintf("ALTER TABLE %v DROP COLUMN %v", scope.TableName(), scope.Quote(column))).Exec()
|
scope.Raw(fmt.Sprintf("ALTER TABLE %v DROP COLUMN %v", scope.QuotedTableName(), scope.Quote(column))).Exec()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) addIndex(unique bool, indexName string, column ...string) {
|
func (scope *Scope) addIndex(unique bool, indexName string, column ...string) {
|
||||||
@ -446,7 +446,7 @@ func (scope *Scope) addIndex(unique bool, indexName string, column ...string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) removeIndex(indexName string) {
|
func (scope *Scope) removeIndex(indexName string) {
|
||||||
scope.Raw(fmt.Sprintf("DROP INDEX %v ON %v", indexName, scope.TableName())).Exec()
|
scope.Raw(fmt.Sprintf("DROP INDEX %v ON %v", indexName, scope.QuotedTableName())).Exec()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (scope *Scope) autoMigrate() *Scope {
|
func (scope *Scope) autoMigrate() *Scope {
|
||||||
@ -456,7 +456,7 @@ func (scope *Scope) autoMigrate() *Scope {
|
|||||||
for _, field := range scope.Fields() {
|
for _, field := range scope.Fields() {
|
||||||
if !scope.Dialect().HasColumn(scope, scope.TableName(), field.DBName) {
|
if !scope.Dialect().HasColumn(scope, scope.TableName(), field.DBName) {
|
||||||
if len(field.SqlTag) > 0 && !field.IsIgnored {
|
if len(field.SqlTag) > 0 && !field.IsIgnored {
|
||||||
scope.Raw(fmt.Sprintf("ALTER TABLE %v ADD %v %v;", scope.TableName(), field.DBName, field.SqlTag)).Exec()
|
scope.Raw(fmt.Sprintf("ALTER TABLE %v ADD %v %v;", scope.QuotedTableName(), field.DBName, field.SqlTag)).Exec()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user