[Feature] Add support for database views.
[Feature] Add support to manually set primary keys for destination structure fields.
This commit is contained in:
parent
5b08a1d240
commit
b88519bfd4
18 changed files with 462 additions and 128 deletions
|
|
@ -771,7 +771,7 @@ func (s *scanContext) getGroupKeyInfo(structType reflect.Type, parentField *refl
|
|||
if len(subType.indexes) != 0 || len(subType.subTypes) != 0 {
|
||||
ret.subTypes = append(ret.subTypes, subType)
|
||||
}
|
||||
} else if isPrimaryKey(field) {
|
||||
} else if isPrimaryKey(field, parentField) {
|
||||
index := s.typeToColumnIndex(newTypeName, fieldName)
|
||||
|
||||
if index < 0 {
|
||||
|
|
@ -837,13 +837,45 @@ func (s *scanContext) rowElemValuePtr(index int) reflect.Value {
|
|||
return newElem
|
||||
}
|
||||
|
||||
func isPrimaryKey(field reflect.StructField) bool {
|
||||
func isPrimaryKey(field reflect.StructField, parentField *reflect.StructField) bool {
|
||||
|
||||
if hasOverwrite, isPrimaryKey := primaryKeyOvewrite(field.Name, parentField); hasOverwrite {
|
||||
return isPrimaryKey
|
||||
}
|
||||
|
||||
sqlTag := field.Tag.Get("sql")
|
||||
|
||||
return sqlTag == "primary_key"
|
||||
}
|
||||
|
||||
func primaryKeyOvewrite(columnName string, parentField *reflect.StructField) (hasOverwrite, primaryKey bool) {
|
||||
if parentField == nil {
|
||||
return
|
||||
}
|
||||
|
||||
sqlTag := parentField.Tag.Get("sql")
|
||||
|
||||
if !strings.HasPrefix(sqlTag, "primary_key") {
|
||||
return
|
||||
}
|
||||
|
||||
parts := strings.Split(sqlTag, "=")
|
||||
|
||||
if len(parts) < 2 {
|
||||
return
|
||||
}
|
||||
|
||||
primaryKeyColumns := strings.Split(parts[1], ",")
|
||||
|
||||
for _, primaryKeyCol := range primaryKeyColumns {
|
||||
if toCommonIdentifier(columnName) == toCommonIdentifier(primaryKeyCol) {
|
||||
return true, true
|
||||
}
|
||||
}
|
||||
|
||||
return true, false
|
||||
}
|
||||
|
||||
func indirectType(reflectType reflect.Type) reflect.Type {
|
||||
if reflectType.Kind() != reflect.Ptr {
|
||||
return reflectType
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue