rename scope.quote to scope.Quote

This commit is contained in:
Jinzhu 2014-01-28 16:22:41 +08:00
parent 4c368be254
commit f1d990e92a
5 changed files with 20 additions and 20 deletions

View File

@ -27,7 +27,7 @@ func Create(scope *Scope) {
for _, field := range scope.Fields() { for _, field := range scope.Fields() {
if field.DBName != scope.PrimaryKey() && len(field.SqlTag) > 0 && !field.IsIgnored { if field.DBName != scope.PrimaryKey() && len(field.SqlTag) > 0 && !field.IsIgnored {
columns = append(columns, scope.quote(field.DBName)) columns = append(columns, scope.Quote(field.DBName))
sqls = append(sqls, scope.AddToVars(field.Value)) sqls = append(sqls, scope.AddToVars(field.Value))
} }
} }

View File

@ -49,12 +49,12 @@ func Update(scope *Scope) {
updateAttrs, ok := scope.Get("gorm:update_attrs") updateAttrs, ok := scope.Get("gorm:update_attrs")
if ok { if ok {
for key, value := range updateAttrs.(map[string]interface{}) { for key, value := range updateAttrs.(map[string]interface{}) {
sqls = append(sqls, fmt.Sprintf("%v = %v", scope.quote(key), scope.AddToVars(value))) sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(key), scope.AddToVars(value)))
} }
} else { } else {
for _, field := range scope.Fields() { for _, field := range scope.Fields() {
if field.DBName != scope.PrimaryKey() && len(field.SqlTag) > 0 && !field.IsIgnored { if field.DBName != scope.PrimaryKey() && len(field.SqlTag) > 0 && !field.IsIgnored {
sqls = append(sqls, fmt.Sprintf("%v = %v", scope.quote(field.DBName), scope.AddToVars(field.Value))) sqls = append(sqls, fmt.Sprintf("%v = %v", scope.Quote(field.DBName), scope.AddToVars(field.Value)))
} }
} }
} }

View File

@ -460,7 +460,7 @@ func (scope *Scope) related(value interface{}, foreignKeys ...string) *Scope {
if foreignValue, ok := scope.FieldByName(foreignKey); ok { if foreignValue, ok := scope.FieldByName(foreignKey); ok {
return toScope.inlineCondition(foreignValue).callCallbacks(scope.db.parent.callback.queries) return toScope.inlineCondition(foreignValue).callCallbacks(scope.db.parent.callback.queries)
} else if toScope.HasColumn(foreignKey) { } else if toScope.HasColumn(foreignKey) {
sql := fmt.Sprintf("%v = ?", scope.quote(toSnake(foreignKey))) sql := fmt.Sprintf("%v = ?", scope.Quote(toSnake(foreignKey)))
return toScope.inlineCondition(sql, scope.PrimaryKeyValue()).callCallbacks(scope.db.parent.callback.queries) return toScope.inlineCondition(sql, scope.PrimaryKeyValue()).callCallbacks(scope.db.parent.callback.queries)
} }
} }

View File

@ -10,12 +10,12 @@ import (
"strings" "strings"
) )
func (scope *Scope) quote(str string) string { func (scope *Scope) Quote(str string) string {
return scope.Dialect().Quote(str) return scope.Dialect().Quote(str)
} }
func (scope *Scope) primaryCondiation(value interface{}) string { func (scope *Scope) primaryCondiation(value interface{}) string {
return fmt.Sprintf("(%v = %v)", scope.quote(scope.PrimaryKey()), value) return fmt.Sprintf("(%v = %v)", scope.Quote(scope.PrimaryKey()), value)
} }
func (scope *Scope) buildWhereCondition(clause map[string]interface{}) (str string) { func (scope *Scope) buildWhereCondition(clause map[string]interface{}) (str string) {
@ -33,19 +33,19 @@ func (scope *Scope) buildWhereCondition(clause map[string]interface{}) (str stri
case sql.NullInt64: case sql.NullInt64:
return scope.primaryCondiation(scope.AddToVars(value.Int64)) return scope.primaryCondiation(scope.AddToVars(value.Int64))
case []int64, []int, []int32, []string: case []int64, []int, []int32, []string:
str = fmt.Sprintf("(%v in (?))", scope.quote(scope.PrimaryKey())) str = fmt.Sprintf("(%v in (?))", scope.Quote(scope.PrimaryKey()))
clause["args"] = []interface{}{value} clause["args"] = []interface{}{value}
case map[string]interface{}: case map[string]interface{}:
var sqls []string var sqls []string
for key, value := range value { for key, value := range value {
sqls = append(sqls, fmt.Sprintf("(%v = %v)", scope.quote(key), scope.AddToVars(value))) sqls = append(sqls, fmt.Sprintf("(%v = %v)", scope.Quote(key), scope.AddToVars(value)))
} }
return strings.Join(sqls, " AND ") return strings.Join(sqls, " AND ")
case interface{}: case interface{}:
var sqls []string var sqls []string
for _, field := range scope.New(value).Fields() { for _, field := range scope.New(value).Fields() {
if !field.IsBlank { if !field.IsBlank {
sqls = append(sqls, fmt.Sprintf("(%v = %v)", scope.quote(field.DBName), scope.AddToVars(field.Value))) sqls = append(sqls, fmt.Sprintf("(%v = %v)", scope.Quote(field.DBName), scope.AddToVars(field.Value)))
} }
} }
return strings.Join(sqls, " AND ") return strings.Join(sqls, " AND ")
@ -79,19 +79,19 @@ func (scope *Scope) buildNotCondition(clause map[string]interface{}) (str string
case string: case string:
if regexp.MustCompile("^\\s*\\d+\\s*$").MatchString(value) { if regexp.MustCompile("^\\s*\\d+\\s*$").MatchString(value) {
id, _ := strconv.Atoi(value) id, _ := strconv.Atoi(value)
return fmt.Sprintf("(%v <> %v)", scope.quote(scope.PrimaryKey()), id) return fmt.Sprintf("(%v <> %v)", scope.Quote(scope.PrimaryKey()), id)
} else if regexp.MustCompile("(?i) (=|<>|>|<|LIKE|IS) ").MatchString(value) { } else if regexp.MustCompile("(?i) (=|<>|>|<|LIKE|IS) ").MatchString(value) {
str = fmt.Sprintf(" NOT (%v) ", value) str = fmt.Sprintf(" NOT (%v) ", value)
not_equal_sql = fmt.Sprintf("NOT (%v)", value) not_equal_sql = fmt.Sprintf("NOT (%v)", value)
} else { } else {
str = fmt.Sprintf("(%v NOT IN (?))", scope.quote(value)) str = fmt.Sprintf("(%v NOT IN (?))", scope.Quote(value))
not_equal_sql = fmt.Sprintf("(%v <> ?)", scope.quote(value)) not_equal_sql = fmt.Sprintf("(%v <> ?)", scope.Quote(value))
} }
case int, int64, int32: case int, int64, int32:
return fmt.Sprintf("(%v <> %v)", scope.quote(scope.PrimaryKey()), value) return fmt.Sprintf("(%v <> %v)", scope.Quote(scope.PrimaryKey()), value)
case []int64, []int, []int32, []string: case []int64, []int, []int32, []string:
if reflect.ValueOf(value).Len() > 0 { if reflect.ValueOf(value).Len() > 0 {
str = fmt.Sprintf("(%v not in (?))", scope.quote(scope.PrimaryKey())) str = fmt.Sprintf("(%v not in (?))", scope.Quote(scope.PrimaryKey()))
clause["args"] = []interface{}{value} clause["args"] = []interface{}{value}
} else { } else {
return "" return ""
@ -99,14 +99,14 @@ func (scope *Scope) buildNotCondition(clause map[string]interface{}) (str string
case map[string]interface{}: case map[string]interface{}:
var sqls []string var sqls []string
for key, value := range value { for key, value := range value {
sqls = append(sqls, fmt.Sprintf("(%v <> %v)", scope.quote(key), scope.AddToVars(value))) sqls = append(sqls, fmt.Sprintf("(%v <> %v)", scope.Quote(key), scope.AddToVars(value)))
} }
return strings.Join(sqls, " AND ") return strings.Join(sqls, " AND ")
case interface{}: case interface{}:
var sqls []string var sqls []string
for _, field := range scope.New(value).Fields() { for _, field := range scope.New(value).Fields() {
if !field.IsBlank { if !field.IsBlank {
sqls = append(sqls, fmt.Sprintf("(%v <> %v)", scope.quote(field.DBName), scope.AddToVars(field.Value))) sqls = append(sqls, fmt.Sprintf("(%v <> %v)", scope.Quote(field.DBName), scope.AddToVars(field.Value)))
} }
} }
return strings.Join(sqls, " AND ") return strings.Join(sqls, " AND ")

View File

@ -9,7 +9,7 @@ func (scope *Scope) createTable() *Scope {
var sqls []string var sqls []string
for _, field := range scope.Fields() { for _, field := range scope.Fields() {
if !field.IsIgnored && len(field.SqlTag) > 0 { if !field.IsIgnored && len(field.SqlTag) > 0 {
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.TableName(), strings.Join(sqls, ","))).Exec()
@ -22,11 +22,11 @@ func (scope *Scope) dropTable() *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.TableName(), 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.TableName(), scope.Quote(column))).Exec()
} }
func (scope *Scope) addIndex(column string, names ...string) { func (scope *Scope) addIndex(column string, names ...string) {
@ -37,7 +37,7 @@ func (scope *Scope) addIndex(column string, names ...string) {
indexName = fmt.Sprintf("index_%v_on_%v", scope.TableName(), column) 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.TableName(), scope.Quote(column))).Exec()
} }
func (scope *Scope) removeIndex(indexName string) { func (scope *Scope) removeIndex(indexName string) {