github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/templates/main/19_reload.go.tpl (about) 1 {{- if .Table.IsView -}} 2 {{- else -}} 3 {{- $alias := .Aliases.Table .Table.Name -}} 4 {{- $schemaTable := .Table.Name | .SchemaTable -}} 5 {{- $canSoftDelete := .Table.CanSoftDelete $.AutoColumns.Deleted }} 6 {{if .AddGlobal -}} 7 // ReloadG refetches the object from the database using the primary keys. 8 func (o *{{$alias.UpSingular}}) ReloadG({{if not .NoContext}}ctx context.Context{{end}}) error { 9 if o == nil { 10 return errors.New("{{.PkgName}}: no {{$alias.UpSingular}} provided for reload") 11 } 12 13 return o.Reload({{if .NoContext}}boil.GetDB(){{else}}ctx, boil.GetContextDB(){{end}}) 14 } 15 16 {{end -}} 17 18 {{if .AddPanic -}} 19 // ReloadP refetches the object from the database with an executor. Panics on error. 20 func (o *{{$alias.UpSingular}}) ReloadP({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}) { 21 if err := o.Reload({{if not .NoContext}}ctx, {{end -}} exec); err != nil { 22 panic(boil.WrapErr(err)) 23 } 24 } 25 26 {{end -}} 27 28 {{if and .AddGlobal .AddPanic -}} 29 // ReloadGP refetches the object from the database and panics on error. 30 func (o *{{$alias.UpSingular}}) ReloadGP({{if not .NoContext}}ctx context.Context{{end}}) { 31 if err := o.Reload({{if .NoContext}}boil.GetDB(){{else}}ctx, boil.GetContextDB(){{end}}); err != nil { 32 panic(boil.WrapErr(err)) 33 } 34 } 35 36 {{end -}} 37 38 // Reload refetches the object from the database 39 // using the primary keys with an executor. 40 func (o *{{$alias.UpSingular}}) Reload({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}) error { 41 ret, err := Find{{$alias.UpSingular}}({{if not .NoContext}}ctx, {{end -}} exec, {{.Table.PKey.Columns | stringMap (aliasCols $alias) | prefixStringSlice "o." | join ", "}}) 42 if err != nil { 43 return err 44 } 45 46 *o = *ret 47 return nil 48 } 49 50 {{if .AddGlobal -}} 51 // ReloadAllG refetches every row with matching primary key column values 52 // and overwrites the original object slice with the newly updated slice. 53 func (o *{{$alias.UpSingular}}Slice) ReloadAllG({{if not .NoContext}}ctx context.Context{{end}}) error { 54 if o == nil { 55 return errors.New("{{.PkgName}}: empty {{$alias.UpSingular}}Slice provided for reload all") 56 } 57 58 return o.ReloadAll({{if .NoContext}}boil.GetDB(){{else}}ctx, boil.GetContextDB(){{end}}) 59 } 60 61 {{end -}} 62 63 {{if .AddPanic -}} 64 // ReloadAllP refetches every row with matching primary key column values 65 // and overwrites the original object slice with the newly updated slice. 66 // Panics on error. 67 func (o *{{$alias.UpSingular}}Slice) ReloadAllP({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}) { 68 if err := o.ReloadAll({{if not .NoContext}}ctx, {{end -}} exec); err != nil { 69 panic(boil.WrapErr(err)) 70 } 71 } 72 73 {{end -}} 74 75 {{if and .AddGlobal .AddPanic -}} 76 // ReloadAllGP refetches every row with matching primary key column values 77 // and overwrites the original object slice with the newly updated slice. 78 // Panics on error. 79 func (o *{{$alias.UpSingular}}Slice) ReloadAllGP({{if not .NoContext}}ctx context.Context{{end}}) { 80 if err := o.ReloadAll({{if .NoContext}}boil.GetDB(){{else}}ctx, boil.GetContextDB(){{end}}); err != nil { 81 panic(boil.WrapErr(err)) 82 } 83 } 84 85 {{end -}} 86 87 // ReloadAll refetches every row with matching primary key column values 88 // and overwrites the original object slice with the newly updated slice. 89 func (o *{{$alias.UpSingular}}Slice) ReloadAll({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}) error { 90 if o == nil || len(*o) == 0 { 91 return nil 92 } 93 94 slice := {{$alias.UpSingular}}Slice{} 95 var args []interface{} 96 for _, obj := range *o { 97 pkeyArgs := queries.ValuesFromMapping(reflect.Indirect(reflect.ValueOf(obj)), {{$alias.DownSingular}}PrimaryKeyMapping) 98 args = append(args, pkeyArgs...) 99 } 100 101 sql := "SELECT {{$schemaTable}}.* FROM {{$schemaTable}} WHERE " + 102 strmangle.WhereClauseRepeated(string(dialect.LQ), string(dialect.RQ), {{if .Dialect.UseIndexPlaceholders}}1{{else}}0{{end}}, {{$alias.DownSingular}}PrimaryKeyColumns, len(*o)){{if and .AddSoftDeletes $canSoftDelete}} + 103 "and {{or $.AutoColumns.Deleted "deleted_at" | $.Quotes}} is null" 104 {{- end}} 105 106 q := queries.Raw(sql, args...) 107 108 err := q.Bind({{if .NoContext}}nil{{else}}ctx{{end}}, exec, &slice) 109 if err != nil { 110 return errors.Wrap(err, "{{.PkgName}}: unable to reload all in {{$alias.UpSingular}}Slice") 111 } 112 113 *o = slice 114 115 return nil 116 } 117 118 {{- end -}}