func beforeQuery(scope *gorm.Scope) { if isLocalizable(scope) { quotedTableName := scope.QuotedTableName() quotedPrimaryKey := scope.Quote(scope.PrimaryKey()) locale, isLocale := getLocale(scope) switch mode, _ := scope.DB().Get("l10n:mode"); mode { case "unscoped": case "global": scope.Search.Where(fmt.Sprintf("%v.language_code = ?", quotedTableName), Global) case "locale": scope.Search.Where(fmt.Sprintf("%v.language_code = ?", quotedTableName), locale) case "reverse": if !scope.Search.Unscoped && scope.Fields()["deleted_at"] != nil { scope.Search.Where(fmt.Sprintf("(%v NOT IN (SELECT DISTINCT(%v) FROM %v t2 WHERE t2.language_code = ? AND t2.deleted_at IS NULL) AND language_code = ?)", quotedPrimaryKey, quotedPrimaryKey, quotedTableName), locale, Global) } else { scope.Search.Where(fmt.Sprintf("(%v NOT IN (SELECT DISTINCT(%v) FROM %v t2 WHERE t2.language_code = ?) AND language_code = ?)", quotedPrimaryKey, quotedPrimaryKey, quotedTableName), locale, Global) } default: if isLocale { if !scope.Search.Unscoped && scope.Fields()["deleted_at"] != nil { scope.Search.Where(fmt.Sprintf("((%v NOT IN (SELECT DISTINCT(%v) FROM %v t2 WHERE t2.language_code = ? AND t2.deleted_at IS NULL) AND language_code = ?) OR language_code = ?) AND deleted_at IS NULL", quotedPrimaryKey, quotedPrimaryKey, quotedTableName), locale, Global, locale) } else { scope.Search.Where(fmt.Sprintf("(%v NOT IN (SELECT DISTINCT(%v) FROM %v t2 WHERE t2.language_code = ?) AND language_code = ?) OR (language_code = ?)", quotedPrimaryKey, quotedPrimaryKey, quotedTableName), locale, Global, locale) } } else { scope.Search.Where(fmt.Sprintf("%v.language_code = ?", quotedTableName), Global) } } } }
func syncDeleteFromProductionToDraft(scope *gorm.Scope) { if !scope.HasError() { if ok, clone := isProductionModeAndNewScope(scope); ok { scope.DB().Callback().Delete().Get("gorm:delete")(clone) } } }
func validate(scope *gorm.Scope) { if _, ok := scope.Get("gorm:update_column"); !ok { if result, ok := scope.DB().Get(skipValidations); !(ok && result.(bool)) { scope.CallMethodWithErrorCheck("Validate") } } }
func reorderPositions(scope *gorm.Scope) { if !scope.HasError() { if _, ok := scope.Value.(sortingInterface); ok { table := scope.TableName() var additionalSQL []string var additionalValues []interface{} // with l10n if locale, ok := scope.DB().Get("l10n:locale"); ok && locale.(string) != "" { additionalSQL = append(additionalSQL, "language_code = ?") additionalValues = append(additionalValues, locale) } additionalValues = append(additionalValues, additionalValues...) // with soft delete if scope.HasColumn("DeletedAt") { additionalSQL = append(additionalSQL, "deleted_at IS NULL") } var sql = fmt.Sprintf("UPDATE %v SET position = (SELECT COUNT(pos) + 1 FROM (SELECT DISTINCT(position) AS pos FROM %v WHERE %v) AS t2 WHERE t2.pos < %v.position) WHERE %v", table, table, strings.Join(additionalSQL, " AND "), table, strings.Join(additionalSQL, " AND ")) if scope.NewDB().Exec(sql, additionalValues...).Error == nil { // Create Publish Event createPublishEvent(scope.DB(), scope.Value) } } } }
func getLocale(scope *gorm.Scope) (locale string, ok bool) { if str, ok := scope.DB().Get("l10n:locale"); ok { if locale, ok := str.(string); ok { return locale, (locale != Global) && (locale != "") } } return Global, false }
func validate(scope *gorm.Scope) { db := scope.DB() if _, ok := db.Get(settingKey); !ok { db.InstantSet(settingKey, map[string][]string{}) } if result, ok := db.Get(skipValidations); !(ok && result.(bool)) { scope.CallMethodWithErrorCheck("Validate") } }
func afterUpdate(scope *gorm.Scope) { if !scope.HasError() { if isLocalizable(scope) { if locale, ok := getLocale(scope); ok { if scope.DB().RowsAffected == 0 && !scope.PrimaryKeyZero() { //is locale and nothing updated var count int var query = fmt.Sprintf("%v.language_code = ? AND %v.%v = ?", scope.QuotedTableName(), scope.QuotedTableName(), scope.PrimaryKey()) // if enabled soft delete, delete soft deleted records if scope.HasColumn("DeletedAt") { scope.NewDB().Unscoped().Where("deleted_at is not null").Where(query, locale, scope.PrimaryKeyValue()).Delete(scope.Value) } // if no localized records exist, localize it if scope.NewDB().Table(scope.TableName()).Where(query, locale, scope.PrimaryKeyValue()).Count(&count); count == 0 { scope.DB().Create(scope.Value) } } } else if syncColumns := syncColumns(scope); len(syncColumns) > 0 { // is global if mode, _ := scope.DB().Get("l10n:mode"); mode != "unscoped" { if scope.DB().RowsAffected > 0 { var primaryField = scope.PrimaryField() var syncAttrs = map[string]interface{}{} if updateAttrs, ok := scope.InstanceGet("gorm:update_attrs"); ok { for key, value := range updateAttrs.(map[string]interface{}) { for _, syncColumn := range syncColumns { if syncColumn == key { syncAttrs[syncColumn] = value break } } } } else { var fields = scope.Fields() for _, syncColumn := range syncColumns { if field, ok := fields[syncColumn]; ok && field.IsNormal { syncAttrs[syncColumn] = field.Field.Interface() } } } if len(syncAttrs) > 0 { db := scope.DB().Model(reflect.New(utils.ModelType(scope.Value)).Interface()).Set("l10n:mode", "unscoped").Where("language_code <> ?", Global) if !primaryField.IsBlank { db = db.Where(fmt.Sprintf("%v = ?", primaryField.DBName), primaryField.Field.Interface()) } scope.Err(db.UpdateColumns(syncAttrs).Error) } } } } } } }
func initalizePosition(scope *gorm.Scope) { if !scope.HasError() { if position, ok := scope.Value.(sortingInterface); ok { if pos, err := strconv.Atoi(fmt.Sprintf("%v", scope.PrimaryKeyValue())); err == nil { if scope.DB().UpdateColumn("position", pos).Error == nil { position.SetPosition(pos) } } } } }
func isProductionModeAndNewScope(scope *gorm.Scope) (isProduction bool, clone *gorm.Scope) { if !IsDraftMode(scope.DB()) { if _, ok := scope.InstanceGet("publish:supported_model"); ok { table := OriginalTableName(scope.TableName()) clone := scope.New(scope.Value) clone.Search.Table(table) return true, clone } } return false, nil }
func syncUpdateFromProductionToDraft(scope *gorm.Scope) { if !scope.HasError() { if ok, clone := isProductionModeAndNewScope(scope); ok { if updateAttrs, ok := scope.InstanceGet("gorm:update_attrs"); ok { table := OriginalTableName(scope.TableName()) clone.Search = scope.Search clone.Search.Table(table) clone.InstanceSet("gorm:update_attrs", updateAttrs) } scope.DB().Callback().Update().Get("gorm:update")(clone) } } }
func assignCreatedBy(scope *gorm.Scope) { if isAuditable(scope) { if user, ok := scope.DB().Get("audited:current_user"); ok { var currentUser string if primaryField := scope.New(user).PrimaryField(); primaryField != nil { currentUser = fmt.Sprintf("%v", primaryField.Field.Interface()) } else { currentUser = fmt.Sprintf("%v", user) } scope.SetColumn("CreatedBy", currentUser) } } }
func beforeUpdate(scope *gorm.Scope) { if isLocalizable(scope) { locale, isLocale := getLocale(scope) switch mode, _ := scope.DB().Get("l10n:mode"); mode { case "unscoped": default: scope.Search.Where("language_code = ?", locale) setLocale(scope, locale) } if isLocale { scope.Search.Omit(syncColumns(scope)...) } } }
func deleteScope(scope *gorm.Scope) { if !scope.HasError() { _, supportedModel := scope.InstanceGet("publish:supported_model") if supportedModel && IsDraftMode(scope.DB()) { scope.Raw( fmt.Sprintf("UPDATE %v SET deleted_at=%v, publish_status=%v %v", scope.QuotedTableName(), scope.AddToVars(gorm.NowFunc()), scope.AddToVars(DIRTY), scope.CombinedConditionSql(), )) scope.Exec() } else { gorm.Delete(scope) } } }
func assignUpdatedBy(scope *gorm.Scope) { if isAuditable(scope) { if user, ok := scope.DB().Get("audited:current_user"); ok { var currentUser string if primaryField := scope.New(user).PrimaryField(); primaryField != nil { currentUser = fmt.Sprintf("%v", primaryField.Field.Interface()) } else { currentUser = fmt.Sprintf("%v", user) } if attrs, ok := scope.InstanceGet("gorm:update_attrs"); ok { updateAttrs := attrs.(map[string]interface{}) updateAttrs["updated_by"] = currentUser scope.InstanceSet("gorm:update_attrs", updateAttrs) } else { scope.SetColumn("UpdatedBy", currentUser) } } } }
func afterUpdate(scope *gorm.Scope) { if !scope.HasError() { if isLocalizable(scope) { if locale, ok := getLocale(scope); ok { if scope.DB().RowsAffected == 0 && !scope.PrimaryKeyZero() { //is locale and nothing updated var count int var query = fmt.Sprintf("%v.language_code = ? AND %v.%v = ?", scope.QuotedTableName(), scope.QuotedTableName(), scope.PrimaryKey()) if scope.NewDB().Table(scope.TableName()).Where(query, locale, scope.PrimaryKeyValue()).Count(&count); count == 0 { scope.DB().Create(scope.Value) } } } else if syncColumns := syncColumns(scope); len(syncColumns) > 0 { // is global if mode, _ := scope.DB().Get("l10n:mode"); mode != "unscoped" { if scope.DB().RowsAffected > 0 { primaryKey := scope.PrimaryKeyValue() if updateAttrs, ok := scope.InstanceGet("gorm:update_attrs"); ok { var syncAttrs = map[string]interface{}{} for key, value := range updateAttrs.(map[string]interface{}) { for _, syncColumn := range syncColumns { if syncColumn == key { syncAttrs[syncColumn] = value break } } } if len(syncAttrs) > 0 { scope.DB().Model(scope.Value).Set("l10n:mode", "unscoped").Where("language_code <> ?", Global).UpdateColumns(syncAttrs) } } else { scope.NewDB().Set("l10n:mode", "unscoped").Where(fmt.Sprintf("%v = ?", scope.PrimaryKey()), primaryKey).Select(syncColumns).Save(scope.Value) } } } } } } }
func getCurrentUser(scope *gorm.Scope) (string, bool) { var user interface{} var hasUser bool user, hasUser = scope.DB().Get("qor:current_user") if !hasUser { user, hasUser = scope.DB().Get("audited:current_user") } if hasUser { var currentUser string if primaryField := scope.New(user).PrimaryField(); primaryField != nil { currentUser = fmt.Sprintf("%v", primaryField.Field.Interface()) } else { currentUser = fmt.Sprintf("%v", user) } return currentUser, true } return "", false }
func validate(scope *gorm.Scope) { if _, ok := scope.Get("gorm:update_column"); !ok { if result, ok := scope.DB().Get(skipValidations); !(ok && result.(bool)) { if !scope.HasError() { scope.CallMethod("Validate") resource := scope.IndirectValue().Interface() _, validatorErrors := govalidator.ValidateStruct(resource) if validatorErrors != nil { if errors, ok := validatorErrors.(govalidator.Errors); ok { for _, err := range flatValidatorErrors(errors) { scope.DB().AddError(formattedError(err, resource)) } } else { scope.DB().AddError(validatorErrors) } } } } } }