MCPcopy Create free account
hub / github.com/pydio/cells / ApplyOrderedUpdates

Method ApplyOrderedUpdates

common/storage/sql/postgres.go:74–127  ·  view source on GitHub ↗
(db *gorm.DB, tableName string, sets []OrderedUpdate, wheres []sql.NamedArg)

Source from the content-addressed store, hash-verified

72}
73
74func (p *postgresHelper) ApplyOrderedUpdates(db *gorm.DB, tableName string, sets []OrderedUpdate, wheres []sql.NamedArg) (int64, error) {
75
76 tx1 := db.Exec("CREATE EXTENSION IF NOT EXISTS pgcrypto;")
77 if tx1.Error != nil {
78 return 0, tx1.Error
79 }
80
81 var namedSets []string
82 var namedWheres []string
83 var args []interface{}
84
85 for _, w := range wheres {
86 namedWheres = append(namedWheres, "@"+w.Name)
87 args = append(args, w)
88 }
89
90 var assigns []string
91 assigns = append(assigns, "uuid")
92 var hasName bool
93 for _, u := range sets {
94 if u.Key == "hash" {
95 u.Value = gorm.Expr(p.Hash("uv.new_mpath1", "uv.new_mpath2", "uv.new_mpath3", "uv.new_mpath4"))
96 namedSets = append(namedSets, fmt.Sprintf("%s=@%s", u.Key, u.Key))
97 } else if u.Key == "hash2" {
98 cName := "name"
99 if hasName {
100 cName = "uv.new_name"
101 }
102 u.Value = gorm.Expr(p.HashParent(cName, "uv.new_level", "uv.new_mpath1", "uv.new_mpath2", "uv.new_mpath3", "uv.new_mpath4"))
103 namedSets = append(namedSets, fmt.Sprintf("%s=@%s", u.Key, u.Key))
104 } else {
105 if u.Key == "name" {
106 hasName = true
107 }
108 switch u.Value.(type) {
109 case int8, int16:
110 assigns = append(assigns, fmt.Sprintf("CAST(@%s AS SMALLINT) AS new_%s", u.Key, u.Key))
111 case int, int32, int64:
112 assigns = append(assigns, fmt.Sprintf("CAST(@%s AS INT) AS new_%s", u.Key, u.Key))
113 case bool:
114 assigns = append(assigns, fmt.Sprintf("CAST(@%s AS BOOL) AS new_%s", u.Key, u.Key))
115 default:
116 assigns = append(assigns, fmt.Sprintf("@%s AS new_%s", u.Key, u.Key))
117 }
118 namedSets = append(namedSets, fmt.Sprintf("\"%s\"=uv.new_%s", u.Key, u.Key))
119 }
120 args = append(args, sql.Named(u.Key, u.Value))
121 }
122
123 q := fmt.Sprintf("WITH updated_values AS ( SELECT %s FROM \"%s\" WHERE %s )", strings.Join(assigns, ", "), tableName, strings.Join(namedWheres, " AND "))
124 q += " " + fmt.Sprintf("UPDATE \"%s\" SET %s FROM updated_values AS uv WHERE \"%s\".uuid=uv.uuid", tableName, strings.Join(namedSets, ", "), tableName)
125 tx := db.Exec(q, args...)
126 return tx.RowsAffected, tx.Error
127}
128
129const (
130 PostgreDriver = "postgres"

Callers

nothing calls this directly

Calls 4

HashMethod · 0.95
HashParentMethod · 0.95
NamedMethod · 0.65
JoinMethod · 0.45

Tested by

no test coverage detected