github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/templates/main/16_update.go.tpl (about) 1 {{- if .Table.IsView -}} 2 {{- else -}} 3 {{- $alias := .Aliases.Table .Table.Name -}} 4 {{- $schemaTable := .Table.Name | .SchemaTable}} 5 {{if .AddGlobal -}} 6 // UpdateG a single {{$alias.UpSingular}} record using the global executor. 7 // See Update for more documentation. 8 func (o *{{$alias.UpSingular}}) UpdateG({{if not .NoContext}}ctx context.Context, {{end -}} columns boil.Columns) {{if .NoRowsAffected}}error{{else}}(int64, error){{end -}} { 9 return o.Update({{if .NoContext}}boil.GetDB(){{else}}ctx, boil.GetContextDB(){{end}}, columns) 10 } 11 12 {{end -}} 13 14 {{if .AddPanic -}} 15 // UpdateP uses an executor to update the {{$alias.UpSingular}}, and panics on error. 16 // See Update for more documentation. 17 func (o *{{$alias.UpSingular}}) UpdateP({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}, columns boil.Columns) {{if not .NoRowsAffected}}int64{{end -}} { 18 {{if not .NoRowsAffected}}rowsAff, {{end}}err := o.Update({{if not .NoContext}}ctx, {{end -}} exec, columns) 19 if err != nil { 20 panic(boil.WrapErr(err)) 21 } 22 {{- if not .NoRowsAffected}} 23 24 return rowsAff 25 {{end -}} 26 } 27 28 {{end -}} 29 30 {{if and .AddGlobal .AddPanic -}} 31 // UpdateGP a single {{$alias.UpSingular}} record using the global executor. Panics on error. 32 // See Update for more documentation. 33 func (o *{{$alias.UpSingular}}) UpdateGP({{if not .NoContext}}ctx context.Context, {{end -}} columns boil.Columns) {{if not .NoRowsAffected}}int64{{end -}} { 34 {{if not .NoRowsAffected}}rowsAff, {{end}}err := o.Update({{if .NoContext}}boil.GetDB(){{else}}ctx, boil.GetContextDB(){{end}}, columns) 35 if err != nil { 36 panic(boil.WrapErr(err)) 37 } 38 {{- if not .NoRowsAffected}} 39 40 return rowsAff 41 {{end -}} 42 } 43 44 {{end -}} 45 46 // Update uses an executor to update the {{$alias.UpSingular}}. 47 // See boil.Columns.UpdateColumnSet documentation to understand column list inference for updates. 48 // Update does not automatically update the record in case of default values. Use .Reload() to refresh the records. 49 func (o *{{$alias.UpSingular}}) Update({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}, columns boil.Columns) {{if .NoRowsAffected}}error{{else}}(int64, error){{end -}} { 50 {{- template "timestamp_update_helper" . -}} 51 52 var err error 53 {{if not .NoHooks -}} 54 if err = o.doBeforeUpdateHooks({{if not .NoContext}}ctx, {{end -}} exec); err != nil { 55 return {{if not .NoRowsAffected}}0, {{end -}} err 56 } 57 {{end -}} 58 59 key := makeCacheKey(columns, nil) 60 {{$alias.DownSingular}}UpdateCacheMut.RLock() 61 cache, cached := {{$alias.DownSingular}}UpdateCache[key] 62 {{$alias.DownSingular}}UpdateCacheMut.RUnlock() 63 64 if !cached { 65 wl := columns.UpdateColumnSet( 66 {{$alias.DownSingular}}AllColumns, 67 {{$alias.DownSingular}}PrimaryKeyColumns, 68 ) 69 {{- if filterColumnsByAuto true .Table.Columns }} 70 wl = strmangle.SetComplement(wl, {{$alias.DownSingular}}GeneratedColumns) 71 {{end}} 72 {{if not .NoAutoTimestamps}} 73 if !columns.IsWhitelist() { 74 wl = strmangle.SetComplement(wl, []string{"created_at"}) 75 } 76 {{end -}} 77 if len(wl) == 0 { 78 return {{if not .NoRowsAffected}}0, {{end -}} errors.New("{{.PkgName}}: unable to update {{.Table.Name}}, could not build whitelist") 79 } 80 81 cache.query = fmt.Sprintf("UPDATE {{$schemaTable}} SET %s WHERE %s", 82 strmangle.SetParamNames("{{.LQ}}", "{{.RQ}}", {{if .Dialect.UseIndexPlaceholders}}1{{else}}0{{end}}, wl), 83 strmangle.WhereClause("{{.LQ}}", "{{.RQ}}", {{if .Dialect.UseIndexPlaceholders}}len(wl)+1{{else}}0{{end}}, {{$alias.DownSingular}}PrimaryKeyColumns), 84 ) 85 cache.valueMapping, err = queries.BindMapping({{$alias.DownSingular}}Type, {{$alias.DownSingular}}Mapping, append(wl, {{$alias.DownSingular}}PrimaryKeyColumns...)) 86 if err != nil { 87 return {{if not .NoRowsAffected}}0, {{end -}} err 88 } 89 } 90 91 values := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(o)), cache.valueMapping) 92 93 {{if .NoContext -}} 94 if boil.DebugMode { 95 fmt.Fprintln(boil.DebugWriter, cache.query) 96 fmt.Fprintln(boil.DebugWriter, values) 97 } 98 {{else -}} 99 if boil.IsDebug(ctx) { 100 writer := boil.DebugWriterFrom(ctx) 101 fmt.Fprintln(writer, cache.query) 102 fmt.Fprintln(writer, values) 103 } 104 {{end -}} 105 106 {{if .NoRowsAffected -}} 107 {{if .NoContext -}} 108 _, err = exec.Exec(cache.query, values...) 109 {{else -}} 110 _, err = exec.ExecContext(ctx, cache.query, values...) 111 {{end -}} 112 {{else -}} 113 var result sql.Result 114 {{if .NoContext -}} 115 result, err = exec.Exec(cache.query, values...) 116 {{else -}} 117 result, err = exec.ExecContext(ctx, cache.query, values...) 118 {{end -}} 119 {{end -}} 120 if err != nil { 121 return {{if not .NoRowsAffected}}0, {{end -}} errors.Wrap(err, "{{.PkgName}}: unable to update {{.Table.Name}} row") 122 } 123 124 {{if not .NoRowsAffected -}} 125 rowsAff, err := result.RowsAffected() 126 if err != nil { 127 return 0, errors.Wrap(err, "{{.PkgName}}: failed to get rows affected by update for {{.Table.Name}}") 128 } 129 130 {{end -}} 131 132 if !cached { 133 {{$alias.DownSingular}}UpdateCacheMut.Lock() 134 {{$alias.DownSingular}}UpdateCache[key] = cache 135 {{$alias.DownSingular}}UpdateCacheMut.Unlock() 136 } 137 138 {{if not .NoHooks -}} 139 return {{if not .NoRowsAffected}}rowsAff, {{end -}} o.doAfterUpdateHooks({{if not .NoContext}}ctx, {{end -}} exec) 140 {{- else -}} 141 return {{if not .NoRowsAffected}}rowsAff, {{end -}} nil 142 {{- end}} 143 } 144 145 {{if .AddPanic -}} 146 // UpdateAllP updates all rows with matching column names, and panics on error. 147 func (q {{$alias.DownSingular}}Query) UpdateAllP({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}, cols M) {{if not .NoRowsAffected}}int64{{end -}} { 148 {{if not .NoRowsAffected}}rowsAff, {{end -}} err := q.UpdateAll({{if not .NoContext}}ctx, {{end -}} exec, cols) 149 if err != nil { 150 panic(boil.WrapErr(err)) 151 } 152 {{- if not .NoRowsAffected}} 153 154 return rowsAff 155 {{end -}} 156 } 157 158 {{end -}} 159 160 161 {{if .AddGlobal -}} 162 // UpdateAllG updates all rows with the specified column values. 163 func (q {{$alias.DownSingular}}Query) UpdateAllG({{if not .NoContext}}ctx context.Context, {{end -}} cols M) {{if .NoRowsAffected}}error{{else}}(int64, error){{end -}} { 164 return q.UpdateAll({{if .NoContext}}boil.GetDB(){{else}}ctx, boil.GetContextDB(){{end}}, cols) 165 } 166 167 {{end -}} 168 169 170 {{if and .AddGlobal .AddPanic -}} 171 // UpdateAllGP updates all rows with the specified column values, and panics on error. 172 func (q {{$alias.DownSingular}}Query) UpdateAllGP({{if not .NoContext}}ctx context.Context, {{end -}} cols M) {{if not .NoRowsAffected}}int64{{end -}} { 173 {{if not .NoRowsAffected}}rowsAff, {{end -}} err := q.UpdateAll({{if .NoContext}}boil.GetDB(){{else}}ctx, boil.GetContextDB(){{end}}, cols) 174 if err != nil { 175 panic(boil.WrapErr(err)) 176 } 177 {{- if not .NoRowsAffected}} 178 179 return rowsAff 180 {{end -}} 181 } 182 183 {{end -}} 184 185 186 // UpdateAll updates all rows with the specified column values. 187 func (q {{$alias.DownSingular}}Query) UpdateAll({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}, cols M) {{if .NoRowsAffected}}error{{else}}(int64, error){{end -}} { 188 queries.SetUpdate(q.Query, cols) 189 190 {{if .NoRowsAffected -}} 191 {{if .NoContext -}} 192 _, err := q.Query.Exec(exec) 193 {{else -}} 194 _, err := q.Query.ExecContext(ctx, exec) 195 {{end -}} 196 {{else -}} 197 {{if .NoContext -}} 198 result, err := q.Query.Exec(exec) 199 {{else -}} 200 result, err := q.Query.ExecContext(ctx, exec) 201 {{end -}} 202 {{end -}} 203 if err != nil { 204 return {{if not .NoRowsAffected}}0, {{end -}} errors.Wrap(err, "{{.PkgName}}: unable to update all for {{.Table.Name}}") 205 } 206 207 {{if not .NoRowsAffected -}} 208 rowsAff, err := result.RowsAffected() 209 if err != nil { 210 return 0, errors.Wrap(err, "{{.PkgName}}: unable to retrieve rows affected for {{.Table.Name}}") 211 } 212 213 {{end -}} 214 215 return {{if not .NoRowsAffected}}rowsAff, {{end -}} nil 216 } 217 218 {{if .AddGlobal -}} 219 // UpdateAllG updates all rows with the specified column values. 220 func (o {{$alias.UpSingular}}Slice) UpdateAllG({{if not .NoContext}}ctx context.Context, {{end -}} cols M) {{if .NoRowsAffected}}error{{else}}(int64, error){{end -}} { 221 return o.UpdateAll({{if .NoContext}}boil.GetDB(){{else}}ctx, boil.GetContextDB(){{end}}, cols) 222 } 223 224 {{end -}} 225 226 {{if and .AddGlobal .AddPanic -}} 227 // UpdateAllGP updates all rows with the specified column values, and panics on error. 228 func (o {{$alias.UpSingular}}Slice) UpdateAllGP({{if not .NoContext}}ctx context.Context, {{end -}} cols M) {{if not .NoRowsAffected}}int64{{end -}} { 229 {{if not .NoRowsAffected}}rowsAff, {{end -}} err := o.UpdateAll({{if .NoContext}}boil.GetDB(){{else}}ctx, boil.GetContextDB(){{end}}, cols) 230 if err != nil { 231 panic(boil.WrapErr(err)) 232 } 233 {{- if not .NoRowsAffected}} 234 235 return rowsAff 236 {{end -}} 237 } 238 239 {{end -}} 240 241 {{if .AddPanic -}} 242 // UpdateAllP updates all rows with the specified column values, and panics on error. 243 func (o {{$alias.UpSingular}}Slice) UpdateAllP({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}, cols M) {{if not .NoRowsAffected}}int64{{end -}} { 244 {{if not .NoRowsAffected}}rowsAff, {{end -}} err := o.UpdateAll({{if not .NoContext}}ctx, {{end -}} exec, cols) 245 if err != nil { 246 panic(boil.WrapErr(err)) 247 } 248 {{- if not .NoRowsAffected}} 249 250 return rowsAff 251 {{end -}} 252 } 253 254 {{end -}} 255 256 // UpdateAll updates all rows with the specified column values, using an executor. 257 func (o {{$alias.UpSingular}}Slice) UpdateAll({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}, cols M) {{if .NoRowsAffected}}error{{else}}(int64, error){{end -}} { 258 ln := int64(len(o)) 259 if ln == 0 { 260 return {{if not .NoRowsAffected}}0, {{end -}} nil 261 } 262 263 if len(cols) == 0 { 264 return {{if not .NoRowsAffected}}0, {{end -}} errors.New("{{.PkgName}}: update all requires at least one column argument") 265 } 266 267 colNames := make([]string, len(cols)) 268 args := make([]interface{}, len(cols)) 269 270 i := 0 271 for name, value := range cols { 272 colNames[i] = name 273 args[i] = value 274 i++ 275 } 276 277 // Append all of the primary key values for each column 278 for _, obj := range o { 279 pkeyArgs := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(obj)), {{$alias.DownSingular}}PrimaryKeyMapping) 280 args = append(args, pkeyArgs...) 281 } 282 283 sql := fmt.Sprintf("UPDATE {{$schemaTable}} SET %s WHERE %s", 284 strmangle.SetParamNames("{{.LQ}}", "{{.RQ}}", {{if .Dialect.UseIndexPlaceholders}}1{{else}}0{{end}}, colNames), 285 strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), {{if .Dialect.UseIndexPlaceholders}}len(colNames)+1{{else}}0{{end}}, {{$alias.DownSingular}}PrimaryKeyColumns, len(o))) 286 287 {{if .NoContext -}} 288 if boil.DebugMode { 289 fmt.Fprintln(boil.DebugWriter, sql) 290 fmt.Fprintln(boil.DebugWriter, args...) 291 } 292 {{else -}} 293 if boil.IsDebug(ctx) { 294 writer := boil.DebugWriterFrom(ctx) 295 fmt.Fprintln(writer, sql) 296 fmt.Fprintln(writer, args...) 297 } 298 {{end -}} 299 300 {{if .NoRowsAffected -}} 301 {{if .NoContext -}} 302 _, err := exec.Exec(sql, args...) 303 {{else -}} 304 _, err := exec.ExecContext(ctx, sql, args...) 305 {{end -}} 306 {{else -}} 307 {{if .NoContext -}} 308 result, err := exec.Exec(sql, args...) 309 {{else -}} 310 result, err := exec.ExecContext(ctx, sql, args...) 311 {{end -}} 312 {{end -}} 313 if err != nil { 314 return {{if not .NoRowsAffected}}0, {{end -}} errors.Wrap(err, "{{.PkgName}}: unable to update all in {{$alias.DownSingular}} slice") 315 } 316 317 {{if not .NoRowsAffected -}} 318 rowsAff, err := result.RowsAffected() 319 if err != nil { 320 return 0, errors.Wrap(err, "{{.PkgName}}: unable to retrieve rows affected all in update all {{$alias.DownSingular}}") 321 } 322 {{end -}} 323 324 return {{if not .NoRowsAffected}}rowsAff, {{end -}} nil 325 } 326 327 {{- end -}}