Esempio n. 1
0
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)
			}
		}
	}
}
Esempio n. 2
0
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)
						}
					}
				}
			}
		}
	}
}
Esempio n. 3
0
func (item *QueryItem) getQuery(scope *gorm.Scope) (string, error) {
	_, exists := allowedOps()[item.Op]
	if !exists {
		str := fmt.Sprintf("Invalid SQL operator: %s", item.Op)
		return "", errors.New(str)
	}

	if !scope.HasColumn(item.Key) {
		str := fmt.Sprintf("Column does not exist: %s", item.Key)
		return "", errors.New(str)
	}

	var query string

	if item.Op == "in" {
		query = fmt.Sprintf("%s %s (?)", item.Key, item.Op)
	} else {
		query = fmt.Sprintf("%s %s ?", item.Key, item.Op)
	}

	return query, nil
}