Skip to content

Commit

Permalink
quote table name
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed May 24, 2014
1 parent 2f8b184 commit 38a06bf
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions callback_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ func Create(scope *Scope) {

if len(columns) == 0 {
scope.Raw(fmt.Sprintf("INSERT INTO %v DEFAULT VALUES %v",
scope.TableName(),
scope.Quote(scope.TableName()),
scope.Dialect().ReturningStr(scope.PrimaryKey()),
))
} else {
scope.Raw(fmt.Sprintf(
"INSERT INTO %v (%v) VALUES (%v) %v",
scope.TableName(),
scope.Quote(scope.TableName()),
strings.Join(columns, ","),
strings.Join(sqls, ","),
scope.Dialect().ReturningStr(scope.PrimaryKey()),
Expand Down
4 changes: 2 additions & 2 deletions callback_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ func Delete(scope *Scope) {
if !scope.Search.Unscope && scope.HasColumn("DeletedAt") {
scope.Raw(
fmt.Sprintf("UPDATE %v SET deleted_at=%v %v",
scope.TableName(),
scope.Quote(scope.TableName()),
scope.AddToVars(time.Now()),
scope.CombinedConditionSql(),
))
} else {
scope.Raw(fmt.Sprintf("DELETE FROM %v %v", scope.TableName(), scope.CombinedConditionSql()))
scope.Raw(fmt.Sprintf("DELETE FROM %v %v", scope.Quote(scope.TableName()), scope.CombinedConditionSql()))
}

scope.Exec()
Expand Down
2 changes: 1 addition & 1 deletion callback_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func Update(scope *Scope) {

scope.Raw(fmt.Sprintf(
"UPDATE %v SET %v %v",
scope.TableName(),
scope.Quote(scope.TableName()),
strings.Join(sqls, ", "),
scope.CombinedConditionSql(),
))
Expand Down
16 changes: 8 additions & 8 deletions scope_private.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func (scope *Scope) prepareQuerySql() {
if scope.Search.Raw {
scope.Raw(strings.TrimLeft(scope.CombinedConditionSql(), "WHERE "))
} 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.Quote(scope.TableName()), scope.CombinedConditionSql()))
}
return
}
Expand Down Expand Up @@ -414,21 +414,21 @@ func (scope *Scope) createTable() *Scope {
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.Quote(scope.TableName()), strings.Join(sqls, ","))).Exec()
return scope
}

func (scope *Scope) dropTable() *Scope {
scope.Raw(fmt.Sprintf("DROP TABLE %v", scope.TableName())).Exec()
scope.Raw(fmt.Sprintf("DROP TABLE %v", scope.Quote(scope.TableName()))).Exec()
return scope
}

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.Quote(scope.TableName()), scope.Quote(column), typ)).Exec()
}

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.Quote(scope.TableName()), scope.Quote(column))).Exec()
}

func (scope *Scope) addIndex(column string, names ...string) {
Expand All @@ -439,11 +439,11 @@ func (scope *Scope) addIndex(column string, names ...string) {
indexName = fmt.Sprintf("index_%v_on_%v", scope.TableName(), column)
}

scope.Raw(fmt.Sprintf("CREATE INDEX %v ON %v(%v);", indexName, scope.TableName(), scope.Quote(column))).Exec()
scope.Raw(fmt.Sprintf("CREATE INDEX %v ON %v(%v);", indexName, scope.Quote(scope.TableName()), scope.Quote(column))).Exec()
}

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.Quote(scope.TableName()))).Exec()
}

func (scope *Scope) autoMigrate() *Scope {
Expand All @@ -453,7 +453,7 @@ func (scope *Scope) autoMigrate() *Scope {
for _, field := range scope.Fields() {
if !scope.Dialect().HasColumn(scope, scope.TableName(), field.DBName) {
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.Quote(scope.TableName()), field.DBName, field.SqlTag)).Exec()
}
}
}
Expand Down

0 comments on commit 38a06bf

Please sign in to comment.