github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/templates/main/03_finishers.go.tpl (about) 1 {{- $alias := .Aliases.Table .Table.Name}} 2 3 {{if .AddGlobal -}} 4 // OneG returns a single {{$alias.DownSingular}} record from the query using the global executor. 5 func (q {{$alias.DownSingular}}Query) OneG({{if not .NoContext}}ctx context.Context{{end}}) (*{{$alias.UpSingular}}, error) { 6 return q.One({{if .NoContext}}boil.GetDB(){{else}}ctx, boil.GetContextDB(){{end -}}) 7 } 8 9 {{end -}} 10 11 {{if and .AddGlobal .AddPanic -}} 12 // OneGP returns a single {{$alias.DownSingular}} record from the query using the global executor, and panics on error. 13 func (q {{$alias.DownSingular}}Query) OneGP({{if not .NoContext}}ctx context.Context{{end}}) *{{$alias.UpSingular}} { 14 o, err := q.One({{if .NoContext}}boil.GetDB(){{else}}ctx, boil.GetContextDB(){{end -}}) 15 if err != nil { 16 panic(boil.WrapErr(err)) 17 } 18 19 return o 20 } 21 22 {{end -}} 23 24 {{if .AddPanic -}} 25 // OneP returns a single {{$alias.DownSingular}} record from the query, and panics on error. 26 func (q {{$alias.DownSingular}}Query) OneP({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}) (*{{$alias.UpSingular}}) { 27 o, err := q.One({{if not .NoContext}}ctx, {{end -}} exec) 28 if err != nil { 29 panic(boil.WrapErr(err)) 30 } 31 32 return o 33 } 34 35 {{end -}} 36 37 // One returns a single {{$alias.DownSingular}} record from the query. 38 func (q {{$alias.DownSingular}}Query) One({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}) (*{{$alias.UpSingular}}, error) { 39 o := &{{$alias.UpSingular}}{} 40 41 queries.SetLimit(q.Query, 1) 42 43 err := q.Bind({{if .NoContext}}nil{{else}}ctx{{end}}, exec, o) 44 if err != nil { 45 {{if not .AlwaysWrapErrors -}} 46 if errors.Is(err, sql.ErrNoRows) { 47 return nil, sql.ErrNoRows 48 } 49 {{end -}} 50 return nil, errors.Wrap(err, "{{.PkgName}}: failed to execute a one query for {{.Table.Name}}") 51 } 52 53 {{if not .NoHooks -}} 54 if err := o.doAfterSelectHooks({{if not .NoContext}}ctx, {{end -}} exec); err != nil { 55 return o, err 56 } 57 {{- end}} 58 59 return o, nil 60 } 61 62 {{if .AddGlobal -}} 63 // AllG returns all {{$alias.UpSingular}} records from the query using the global executor. 64 func (q {{$alias.DownSingular}}Query) AllG({{if not .NoContext}}ctx context.Context{{end}}) ({{$alias.UpSingular}}Slice, error) { 65 return q.All({{if .NoContext}}boil.GetDB(){{else}}ctx, boil.GetContextDB(){{end -}}) 66 } 67 68 {{end -}} 69 70 {{if and .AddGlobal .AddPanic -}} 71 // AllGP returns all {{$alias.UpSingular}} records from the query using the global executor, and panics on error. 72 func (q {{$alias.DownSingular}}Query) AllGP({{if not .NoContext}}ctx context.Context{{end}}) {{$alias.UpSingular}}Slice { 73 o, err := q.All({{if .NoContext}}boil.GetDB(){{else}}ctx, boil.GetContextDB(){{end -}}) 74 if err != nil { 75 panic(boil.WrapErr(err)) 76 } 77 78 return o 79 } 80 81 {{end -}} 82 83 {{if .AddPanic -}} 84 // AllP returns all {{$alias.UpSingular}} records from the query, and panics on error. 85 func (q {{$alias.DownSingular}}Query) AllP({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}) {{$alias.UpSingular}}Slice { 86 o, err := q.All({{if not .NoContext}}ctx, {{end -}} exec) 87 if err != nil { 88 panic(boil.WrapErr(err)) 89 } 90 91 return o 92 } 93 94 {{end -}} 95 96 // All returns all {{$alias.UpSingular}} records from the query. 97 func (q {{$alias.DownSingular}}Query) All({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}) ({{$alias.UpSingular}}Slice, error) { 98 var o []*{{$alias.UpSingular}} 99 100 err := q.Bind({{if .NoContext}}nil{{else}}ctx{{end}}, exec, &o) 101 if err != nil { 102 return nil, errors.Wrap(err, "{{.PkgName}}: failed to assign all query results to {{$alias.UpSingular}} slice") 103 } 104 105 {{if not .NoHooks -}} 106 if len({{$alias.DownSingular}}AfterSelectHooks) != 0 { 107 for _, obj := range o { 108 if err := obj.doAfterSelectHooks({{if not .NoContext}}ctx, {{end -}} exec); err != nil { 109 return o, err 110 } 111 } 112 } 113 {{- end}} 114 115 return o, nil 116 } 117 118 {{if .AddGlobal -}} 119 // CountG returns the count of all {{$alias.UpSingular}} records in the query using the global executor 120 func (q {{$alias.DownSingular}}Query) CountG({{if not .NoContext}}ctx context.Context{{end}}) (int64, error) { 121 return q.Count({{if .NoContext}}boil.GetDB(){{else}}ctx, boil.GetContextDB(){{end -}}) 122 } 123 124 {{end -}} 125 126 {{if and .AddGlobal .AddPanic -}} 127 // CountGP returns the count of all {{$alias.UpSingular}} records in the query using the global executor, and panics on error. 128 func (q {{$alias.DownSingular}}Query) CountGP({{if not .NoContext}}ctx context.Context{{end}}) int64 { 129 c, err := q.Count({{if .NoContext}}boil.GetDB(){{else}}ctx, boil.GetContextDB(){{end -}}) 130 if err != nil { 131 panic(boil.WrapErr(err)) 132 } 133 134 return c 135 } 136 137 {{end -}} 138 139 {{if .AddPanic -}} 140 // CountP returns the count of all {{$alias.UpSingular}} records in the query, and panics on error. 141 func (q {{$alias.DownSingular}}Query) CountP({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}) int64 { 142 c, err := q.Count({{if not .NoContext}}ctx, {{end -}} exec) 143 if err != nil { 144 panic(boil.WrapErr(err)) 145 } 146 147 return c 148 } 149 150 {{end -}} 151 152 // Count returns the count of all {{$alias.UpSingular}} records in the query. 153 func (q {{$alias.DownSingular}}Query) Count({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}) (int64, error) { 154 var count int64 155 156 queries.SetSelect(q.Query, nil) 157 queries.SetCount(q.Query) 158 159 {{if .NoContext -}} 160 err := q.Query.QueryRow(exec).Scan(&count) 161 {{else -}} 162 err := q.Query.QueryRowContext(ctx, exec).Scan(&count) 163 {{end -}} 164 if err != nil { 165 return 0, errors.Wrap(err, "{{.PkgName}}: failed to count {{.Table.Name}} rows") 166 } 167 168 return count, nil 169 } 170 171 {{if .AddGlobal -}} 172 // ExistsG checks if the row exists in the table using the global executor. 173 func (q {{$alias.DownSingular}}Query) ExistsG({{if not .NoContext}}ctx context.Context{{end}}) (bool, error) { 174 return q.Exists({{if .NoContext}}boil.GetDB(){{else}}ctx, boil.GetContextDB(){{end -}}) 175 } 176 177 {{end -}} 178 179 {{if and .AddGlobal .AddPanic -}} 180 // ExistsGP checks if the row exists in the table using the global executor, and panics on error. 181 func (q {{$alias.DownSingular}}Query) ExistsGP({{if not .NoContext}}ctx context.Context{{end}}) bool { 182 e, err := q.Exists({{if .NoContext}}boil.GetDB(){{else}}ctx, boil.GetContextDB(){{end -}}) 183 if err != nil { 184 panic(boil.WrapErr(err)) 185 } 186 187 return e 188 } 189 190 {{end -}} 191 192 {{if .AddPanic -}} 193 // ExistsP checks if the row exists in the table, and panics on error. 194 func (q {{$alias.DownSingular}}Query) ExistsP({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}) bool { 195 e, err := q.Exists({{if not .NoContext}}ctx, {{end -}} exec) 196 if err != nil { 197 panic(boil.WrapErr(err)) 198 } 199 200 return e 201 } 202 203 {{end -}} 204 205 // Exists checks if the row exists in the table. 206 func (q {{$alias.DownSingular}}Query) Exists({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}) (bool, error) { 207 var count int64 208 209 queries.SetSelect(q.Query, nil) 210 queries.SetCount(q.Query) 211 queries.SetLimit(q.Query, 1) 212 213 {{if .NoContext -}} 214 err := q.Query.QueryRow(exec).Scan(&count) 215 {{else -}} 216 err := q.Query.QueryRowContext(ctx, exec).Scan(&count) 217 {{end -}} 218 if err != nil { 219 return false, errors.Wrap(err, "{{.PkgName}}: failed to check if {{.Table.Name}} exists") 220 } 221 222 return count > 0, nil 223 }