add ks-iam and ks-apigateway

Signed-off-by: hongming <talonwan@yunify.com>
This commit is contained in:
hongming
2019-03-08 11:09:05 +08:00
parent f579e97f6b
commit b59c244ca2
715 changed files with 108638 additions and 23446 deletions

View File

@@ -63,12 +63,12 @@ func (scope *Scope) SQLDB() SQLCommon {
// Dialect get dialect
func (scope *Scope) Dialect() Dialect {
return scope.db.parent.dialect
return scope.db.dialect
}
// Quote used to quote string to escape them for database
func (scope *Scope) Quote(str string) string {
if strings.Index(str, ".") != -1 {
if strings.Contains(str, ".") {
newStrs := []string{}
for _, str := range strings.Split(str, ".") {
newStrs = append(newStrs, scope.Dialect().Quote(str))
@@ -134,7 +134,7 @@ func (scope *Scope) Fields() []*Field {
// FieldByName find `gorm.Field` with field name or db name
func (scope *Scope) FieldByName(name string) (field *Field, ok bool) {
var (
dbName = ToDBName(name)
dbName = ToColumnName(name)
mostMatchedField *Field
)
@@ -330,7 +330,7 @@ func (scope *Scope) TableName() string {
// QuotedTableName return quoted table name
func (scope *Scope) QuotedTableName() (name string) {
if scope.Search != nil && len(scope.Search.tableName) > 0 {
if strings.Index(scope.Search.tableName, " ") != -1 {
if strings.Contains(scope.Search.tableName, " ") {
return scope.Search.tableName
}
return scope.Quote(scope.Search.tableName)
@@ -486,8 +486,10 @@ func (scope *Scope) scan(rows *sql.Rows, columns []string, fields []*Field) {
values[index] = &ignored
selectFields = fields
offset := 0
if idx, ok := selectedColumnsMap[column]; ok {
selectFields = selectFields[idx+1:]
offset = idx + 1
selectFields = selectFields[offset:]
}
for fieldIndex, field := range selectFields {
@@ -501,7 +503,7 @@ func (scope *Scope) scan(rows *sql.Rows, columns []string, fields []*Field) {
resetFields[index] = field
}
selectedColumnsMap[column] = fieldIndex
selectedColumnsMap[column] = offset + fieldIndex
if field.IsNormal {
break
@@ -586,10 +588,10 @@ func (scope *Scope) buildCondition(clause map[string]interface{}, include bool)
scope.Err(fmt.Errorf("invalid query condition: %v", value))
return
}
scopeQuotedTableName := newScope.QuotedTableName()
for _, field := range newScope.Fields() {
if !field.IsIgnored && !field.IsBlank {
sqls = append(sqls, fmt.Sprintf("(%v.%v %s %v)", quotedTableName, scope.Quote(field.DBName), equalSQL, scope.AddToVars(field.Field.Interface())))
sqls = append(sqls, fmt.Sprintf("(%v.%v %s %v)", scopeQuotedTableName, scope.Quote(field.DBName), equalSQL, scope.AddToVars(field.Field.Interface())))
}
}
return strings.Join(sqls, " AND ")
@@ -692,12 +694,12 @@ func (scope *Scope) buildSelectQuery(clause map[string]interface{}) (str string)
buff := bytes.NewBuffer([]byte{})
i := 0
for pos := range str {
for pos, char := range str {
if str[pos] == '?' {
buff.WriteString(replacements[i])
i++
} else {
buff.WriteByte(str[pos])
buff.WriteRune(char)
}
}
@@ -853,6 +855,14 @@ func (scope *Scope) inlineCondition(values ...interface{}) *Scope {
}
func (scope *Scope) callCallbacks(funcs []*func(s *Scope)) *Scope {
defer func() {
if err := recover(); err != nil {
if db, ok := scope.db.db.(sqlTx); ok {
db.Rollback()
}
panic(err)
}
}()
for _, f := range funcs {
(*f)(scope)
if scope.skipLeft {
@@ -880,7 +890,7 @@ func convertInterfaceToMap(values interface{}, withIgnoredField bool) map[string
switch reflectValue.Kind() {
case reflect.Map:
for _, key := range reflectValue.MapKeys() {
attrs[ToDBName(key.Interface().(string))] = reflectValue.MapIndex(key).Interface()
attrs[ToColumnName(key.Interface().(string))] = reflectValue.MapIndex(key).Interface()
}
default:
for _, field := range (&Scope{Value: values}).Fields() {
@@ -907,7 +917,7 @@ func (scope *Scope) updatedAttrsWithValues(value interface{}) (results map[strin
results[field.DBName] = value
} else {
err := field.Set(value)
if field.IsNormal {
if field.IsNormal && !field.IsIgnored {
hasUpdate = true
if err == ErrUnaddressable {
results[field.DBName] = value
@@ -1113,8 +1123,8 @@ func (scope *Scope) createJoinTable(field *StructField) {
if field, ok := scope.FieldByName(fieldName); ok {
foreignKeyStruct := field.clone()
foreignKeyStruct.IsPrimaryKey = false
foreignKeyStruct.TagSettings["IS_JOINTABLE_FOREIGNKEY"] = "true"
delete(foreignKeyStruct.TagSettings, "AUTO_INCREMENT")
foreignKeyStruct.TagSettingsSet("IS_JOINTABLE_FOREIGNKEY", "true")
foreignKeyStruct.TagSettingsDelete("AUTO_INCREMENT")
sqlTypes = append(sqlTypes, scope.Quote(relationship.ForeignDBNames[idx])+" "+scope.Dialect().DataTypeOf(foreignKeyStruct))
primaryKeys = append(primaryKeys, scope.Quote(relationship.ForeignDBNames[idx]))
}
@@ -1124,8 +1134,8 @@ func (scope *Scope) createJoinTable(field *StructField) {
if field, ok := toScope.FieldByName(fieldName); ok {
foreignKeyStruct := field.clone()
foreignKeyStruct.IsPrimaryKey = false
foreignKeyStruct.TagSettings["IS_JOINTABLE_FOREIGNKEY"] = "true"
delete(foreignKeyStruct.TagSettings, "AUTO_INCREMENT")
foreignKeyStruct.TagSettingsSet("IS_JOINTABLE_FOREIGNKEY", "true")
foreignKeyStruct.TagSettingsDelete("AUTO_INCREMENT")
sqlTypes = append(sqlTypes, scope.Quote(relationship.AssociationForeignDBNames[idx])+" "+scope.Dialect().DataTypeOf(foreignKeyStruct))
primaryKeys = append(primaryKeys, scope.Quote(relationship.AssociationForeignDBNames[idx]))
}
@@ -1215,12 +1225,18 @@ func (scope *Scope) addForeignKey(field string, dest string, onDelete string, on
}
func (scope *Scope) removeForeignKey(field string, dest string) {
keyName := scope.Dialect().BuildKeyName(scope.TableName(), field, dest)
keyName := scope.Dialect().BuildKeyName(scope.TableName(), field, dest, "foreign")
if !scope.Dialect().HasForeignKey(scope.TableName(), keyName) {
return
}
var query = `ALTER TABLE %s DROP CONSTRAINT %s;`
var mysql mysql
var query string
if scope.Dialect().GetName() == mysql.GetName() {
query = `ALTER TABLE %s DROP FOREIGN KEY %s;`
} else {
query = `ALTER TABLE %s DROP CONSTRAINT %s;`
}
scope.Raw(fmt.Sprintf(query, scope.QuotedTableName(), scope.quoteIfPossible(keyName))).Exec()
}
@@ -1254,7 +1270,7 @@ func (scope *Scope) autoIndex() *Scope {
var uniqueIndexes = map[string][]string{}
for _, field := range scope.GetStructFields() {
if name, ok := field.TagSettings["INDEX"]; ok {
if name, ok := field.TagSettingsGet("INDEX"); ok {
names := strings.Split(name, ",")
for _, name := range names {
@@ -1265,7 +1281,7 @@ func (scope *Scope) autoIndex() *Scope {
}
}
if name, ok := field.TagSettings["UNIQUE_INDEX"]; ok {
if name, ok := field.TagSettingsGet("UNIQUE_INDEX"); ok {
names := strings.Split(name, ",")
for _, name := range names {