1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
|
{{/*
Copyright 2019-present Facebook Inc. All rights reserved.
This source code is licensed under the Apache 2.0 license found
in the LICENSE file in the root directory of this source tree.
*/}}
{{ define "query" }}
{{ $pkg := base $.Config.Package }}
{{ template "header" $ }}
{{ template "import" $ }}
import (
{{- range $path := $.SiblingImports }}
"{{ $path }}"
{{- end }}
)
{{ $builder := $.QueryName }}
{{ $receiver := receiver $builder }}
// {{ $builder }} is the builder for querying {{ $.Name }} entities.
type {{ $builder }} struct {
config
limit *int
offset *int
order []OrderFunc
fields []string
predicates []predicate.{{ $.Name }}
{{- with $.Edges }}
// eager-loading edges.
{{- range $e := . }}
{{ $e.EagerLoadField }} *{{ $e.Type.QueryName }}
{{- end }}
{{- end }}
{{- /* Additional fields to add to the builder. */}}
{{- $tmpl := printf "dialect/%s/query/fields" $.Storage }}
{{- if hasTemplate $tmpl }}
{{- xtemplate $tmpl . }}
{{- end }}
// intermediate query (i.e. traversal path).
{{ $.Storage }} {{ $.Storage.Builder }}
path func(context.Context) ({{ $.Storage.Builder }}, error)
}
// Where adds a new predicate for the {{ $builder }} builder.
func ({{ $receiver }} *{{ $builder }}) Where(ps ...predicate.{{ $.Name }}) *{{ $builder }} {
{{ $receiver}}.predicates = append({{ $receiver }}.predicates, ps...)
return {{ $receiver }}
}
// Limit adds a limit step to the query.
func ({{ $receiver }} *{{ $builder }}) Limit(limit int) *{{ $builder }} {
{{ $receiver }}.limit = &limit
return {{ $receiver }}
}
// Offset adds an offset step to the query.
func ({{ $receiver }} *{{ $builder }}) Offset(offset int) *{{ $builder }} {
{{ $receiver }}.offset = &offset
return {{ $receiver }}
}
// Order adds an order step to the query.
func ({{ $receiver }} *{{ $builder }}) Order(o ...OrderFunc) *{{ $builder }} {
{{ $receiver }}.order = append({{ $receiver }}.order, o...)
return {{ $receiver }}
}
{{/* this code has similarity with edge queries in client.tmpl */}}
{{ range $e := $.Edges }}
{{ $edge_builder := print (pascal $e.Type.Name) "Query" }}
// Query{{ pascal $e.Name }} chains the current query on the "{{ $e.Name }}" edge.
func ({{ $receiver }} *{{ $builder }}) Query{{ pascal $e.Name }}() *{{ $edge_builder }} {
query := &{{ $edge_builder }}{config: {{ $receiver }}.config}
query.path = func(ctx context.Context) (fromU {{ $.Storage.Builder }}, err error) {
if err := {{ $receiver }}.prepareQuery(ctx); err != nil {
return nil, err
}
{{- with extend $ "Receiver" $receiver "Edge" $e "Ident" "fromU" -}}
{{ $tmpl := printf "dialect/%s/query/path" $.Storage }}
{{- xtemplate $tmpl . }}
{{- end -}}
return fromU, nil
}
return query
}
{{ end }}
// First returns the first {{ $.Name }} entity from the query.
// Returns a *NotFoundError when no {{ $.Name }} was found.
func ({{ $receiver }} *{{ $builder }}) First(ctx context.Context) (*{{ $.Name }}, error) {
nodes, err := {{ $receiver }}.Limit(1).All(ctx)
if err != nil {
return nil, err
}
if len(nodes) == 0 {
return nil, &NotFoundError{ {{ $.Package }}.Label}
}
return nodes[0], nil
}
// FirstX is like First, but panics if an error occurs.
func ({{ $receiver }} *{{ $builder }}) FirstX(ctx context.Context) *{{ $.Name }} {
node, err := {{ $receiver }}.First(ctx)
if err != nil && !IsNotFound(err) {
panic(err)
}
return node
}
// FirstID returns the first {{ $.Name }} ID from the query.
// Returns a *NotFoundError when no {{ $.Name }} ID was found.
func ({{ $receiver }} *{{ $builder }}) FirstID(ctx context.Context) (id {{ $.ID.Type }}, err error) {
var ids []{{ $.ID.Type }}
if ids, err = {{ $receiver }}.Limit(1).IDs(ctx); err != nil {
return
}
if len(ids) == 0 {
err = &NotFoundError{ {{ $.Package }}.Label}
return
}
return ids[0], nil
}
// FirstIDX is like FirstID, but panics if an error occurs.
func ({{ $receiver }} *{{ $builder }}) FirstIDX(ctx context.Context) {{ $.ID.Type }} {
id, err := {{ $receiver }}.FirstID(ctx)
if err != nil && !IsNotFound(err) {
panic(err)
}
return id
}
// Only returns a single {{ $.Name }} entity found by the query, ensuring it only returns one.
// Returns a *NotSingularError when exactly one {{ $.Name }} entity is not found.
// Returns a *NotFoundError when no {{ $.Name }} entities are found.
func ({{ $receiver }} *{{ $builder }}) Only(ctx context.Context) (*{{ $.Name }}, error) {
nodes, err := {{ $receiver }}.Limit(2).All(ctx)
if err != nil {
return nil, err
}
switch len(nodes) {
case 1:
return nodes[0], nil
case 0:
return nil, &NotFoundError{ {{ $.Package }}.Label}
default:
return nil, &NotSingularError{ {{ $.Package }}.Label}
}
}
// OnlyX is like Only, but panics if an error occurs.
func ({{ $receiver }} *{{ $builder }}) OnlyX(ctx context.Context) *{{ $.Name }} {
node, err := {{ $receiver }}.Only(ctx)
if err != nil {
panic(err)
}
return node
}
// OnlyID is like Only, but returns the only {{ $.Name }} ID in the query.
// Returns a *NotSingularError when exactly one {{ $.Name }} ID is not found.
// Returns a *NotFoundError when no entities are found.
func ({{ $receiver }} *{{ $builder }}) OnlyID(ctx context.Context) (id {{ $.ID.Type }}, err error) {
var ids []{{ $.ID.Type }}
if ids, err = {{ $receiver }}.Limit(2).IDs(ctx); err != nil {
return
}
switch len(ids) {
case 1:
id = ids[0]
case 0:
err = &NotFoundError{ {{ $.Package }}.Label}
default:
err = &NotSingularError{ {{ $.Package }}.Label}
}
return
}
// OnlyIDX is like OnlyID, but panics if an error occurs.
func ({{ $receiver }} *{{ $builder }}) OnlyIDX(ctx context.Context) {{ $.ID.Type }} {
id, err := {{ $receiver }}.OnlyID(ctx)
if err != nil {
panic(err)
}
return id
}
// All executes the query and returns a list of {{ plural $.Name }}.
func ({{ $receiver }} *{{ $builder }}) All(ctx context.Context) ([]*{{ $.Name }}, error) {
if err := {{ $receiver }}.prepareQuery(ctx); err != nil {
return nil, err
}
return {{ $receiver }}.{{ $.Storage }}All(ctx)
}
// AllX is like All, but panics if an error occurs.
func ({{ $receiver }} *{{ $builder }}) AllX(ctx context.Context) []*{{ $.Name }} {
nodes, err := {{ $receiver }}.All(ctx)
if err != nil {
panic(err)
}
return nodes
}
// IDs executes the query and returns a list of {{ $.Name }} IDs.
func ({{ $receiver }} *{{ $builder }}) IDs(ctx context.Context) ([]{{ $.ID.Type }}, error) {
var ids []{{ $.ID.Type }}
if err := {{ $receiver }}.Select({{ $.Package }}.FieldID).Scan(ctx, &ids); err != nil {
return nil, err
}
return ids, nil
}
// IDsX is like IDs, but panics if an error occurs.
func ({{ $receiver }} *{{ $builder }}) IDsX(ctx context.Context) []{{ $.ID.Type }} {
ids, err := {{ $receiver }}.IDs(ctx)
if err != nil {
panic(err)
}
return ids
}
// Count returns the count of the given query.
func ({{ $receiver }} *{{ $builder }}) Count(ctx context.Context) (int, error) {
if err := {{ $receiver }}.prepareQuery(ctx); err != nil {
return 0, err
}
return {{ $receiver }}.{{ $.Storage }}Count(ctx)
}
// CountX is like Count, but panics if an error occurs.
func ({{ $receiver }} *{{ $builder }}) CountX(ctx context.Context) int {
count, err := {{ $receiver }}.Count(ctx)
if err != nil {
panic(err)
}
return count
}
// Exist returns true if the query has elements in the graph.
func ({{ $receiver }} *{{ $builder }}) Exist(ctx context.Context) (bool, error) {
if err := {{ $receiver }}.prepareQuery(ctx); err != nil {
return false, err
}
return {{ $receiver }}.{{ $.Storage }}Exist(ctx)
}
// ExistX is like Exist, but panics if an error occurs.
func ({{ $receiver }} *{{ $builder }}) ExistX(ctx context.Context) bool {
exist, err := {{ $receiver }}.Exist(ctx)
if err != nil {
panic(err)
}
return exist
}
// Clone returns a duplicate of the {{ $builder }} builder, including all associated steps. It can be
// used to prepare common query builders and use them differently after the clone is made.
func ({{ $receiver }} *{{ $builder }}) Clone() *{{ $builder }} {
if {{ $receiver }} == nil {
return nil
}
return &{{ $builder }}{
config: {{ $receiver }}.config,
limit: {{ $receiver }}.limit,
offset: {{ $receiver }}.offset,
order: append([]OrderFunc{}, {{ $receiver }}.order...),
predicates: append([]predicate.{{ $.Name }}{}, {{ $receiver }}.predicates...),
{{- range $e := $.Edges }}
{{ $e.EagerLoadField }}: {{ $receiver }}.{{ $e.EagerLoadField }}.Clone(),
{{- end }}
// clone intermediate query.
{{ $.Storage }}: {{ $receiver }}.{{ $.Storage }}.Clone(),
path: {{ $receiver }}.path,
}
}
{{- range $e := $.Edges }}
{{ $ebuilder := $e.Type.QueryName }}
// With{{ pascal $e.Name }} tells the query-builder to eager-load the nodes that are connected to
// the "{{ $e.Name }}" edge. The optional arguments are used to configure the query builder of the edge.
func ({{ $receiver }} *{{ $builder }}) With{{ pascal $e.Name }}(opts ...func(*{{ $ebuilder }})) *{{ $builder }} {
query := &{{ $ebuilder }}{config: {{ $receiver }}.config}
for _, opt := range opts {
opt(query)
}
{{ $receiver }}.{{ $e.EagerLoadField }} = query
return {{ $receiver }}
}
{{- end }}
{{ $groupBuilder := pascal $.Name | printf "%sGroupBy" }}
// GroupBy is used to group vertices by one or more fields/columns.
// It is often used with aggregate functions, like: {{ join (keys aggregate) ", " }}.
{{- with len $.Fields }}
{{- $f := index $.Fields 0 }}
//
// Example:
//
// var v []struct {
// {{ $f.StructField }} {{ $f.Type }} `{{ $f.StructTag }}`
// Count int `json:"count,omitempty"`
// }
//
// client.{{ pascal $.Name }}.Query().
// GroupBy({{ $.Package }}.{{ $f.Constant }}).
// Aggregate({{ $pkg }}.Count()).
// Scan(ctx, &v)
//
{{- end }}
func ({{ $receiver }} *{{ $builder }}) GroupBy(field string, fields ...string) *{{ $groupBuilder }} {
group := &{{ $groupBuilder }}{config: {{ $receiver }}.config}
group.fields = append([]string{field}, fields...)
group.path = func(ctx context.Context) (prev {{ $.Storage.Builder }}, err error) {
if err := {{ $receiver }}.prepareQuery(ctx); err != nil {
return nil, err
}
return {{ $receiver }}.{{ $.Storage }}Query(), nil
}
return group
}
{{ $selectBuilder := pascal $.Name | printf "%sSelect" }}
// Select allows the selection one or more fields/columns for the given query,
// instead of selecting all fields in the entity.
{{- with len $.Fields }}
{{- $f := index $.Fields 0 }}
//
// Example:
//
// var v []struct {
// {{ $f.StructField }} {{ $f.Type }} `{{ $f.StructTag }}`
// }
//
// client.{{ pascal $.Name }}.Query().
// Select({{ $.Package }}.{{ $f.Constant }}).
// Scan(ctx, &v)
//
{{- end }}
func ({{ $receiver }} *{{ $builder }}) Select(field string, fields ...string) *{{ $selectBuilder }} {
{{ $receiver }}.fields = append([]string{field}, fields...)
return &{{ $selectBuilder }}{ {{ $builder }}: {{ $receiver }} }
}
func ({{ $receiver }} *{{ $builder }}) prepareQuery(ctx context.Context) error {
{{- /* Optional prepare checks per dialect. */}}
{{- $tmpl = printf "dialect/%s/query/preparecheck" $.Storage }}
{{- if hasTemplate $tmpl }}
{{- with extend $ "Receiver" $receiver "Package" $pkg }}
{{- xtemplate $tmpl . }}
{{- end }}
{{- end }}
if {{ $receiver }}.path != nil {
prev, err := {{ $receiver }}.path(ctx)
if err != nil {
return err
}
{{ $receiver }}.{{ $.Storage }} = prev
}
{{- if $.NumPolicy }}
if {{ $.Package }}.Policy == nil {
return errors.New("{{ $pkg }}: uninitialized {{ $.Package }}.Policy (forgotten import {{ $pkg }}/runtime?)")
}
if err := {{ $.Package }}.Policy.EvalQuery(ctx, {{ $receiver }}); err != nil {
return err
}
{{- end }}
return nil
}
{{ with extend $ "Builder" $builder "Package" $pkg }}
{{ $tmpl := printf "dialect/%s/query" $.Storage }}
{{ xtemplate $tmpl . }}
{{ end }}
{{/* groupby builder */}}
{{ $groupReceiver := receiver $groupBuilder }}
// {{ $groupBuilder }} is the group-by builder for {{ $.Name }} entities.
type {{ $groupBuilder }} struct {
config
fields []string
fns []AggregateFunc
// intermediate query (i.e. traversal path).
{{ $.Storage }} {{ $.Storage.Builder }}
path func(context.Context) ({{ $.Storage.Builder }}, error)
}
// Aggregate adds the given aggregation functions to the group-by query.
func ({{ $groupReceiver }} *{{ $groupBuilder }}) Aggregate(fns ...AggregateFunc) *{{ $groupBuilder }} {
{{ $groupReceiver }}.fns = append({{ $groupReceiver }}.fns, fns...)
return {{ $groupReceiver }}
}
// Scan applies the group-by query and scans the result into the given value.
func ({{ $groupReceiver }} *{{ $groupBuilder }}) Scan(ctx context.Context, v interface{}) error {
query, err := {{ $groupReceiver }}.path(ctx)
if err != nil {
return err
}
{{ $groupReceiver }}.{{ $.Storage }} = query
return {{ $groupReceiver }}.{{ $.Storage }}Scan(ctx, v)
}
// ScanX is like Scan, but panics if an error occurs.
func ({{ $groupReceiver }} *{{ $groupBuilder }}) ScanX(ctx context.Context, v interface{}) {
if err := {{ $groupReceiver }}.Scan(ctx, v); err != nil {
panic(err)
}
}
{{ range $t := primitives }}
{{ $plural := pascal $t | plural }}
// {{ $plural }} returns list of {{ plural $t }} from group-by.
// It is only allowed when executing a group-by query with one field.
func ({{ $groupReceiver }} *{{ $groupBuilder }}) {{ $plural }}(ctx context.Context) ([]{{ $t }}, error) {
if len({{ $groupReceiver }}.fields) > 1 {
return nil, errors.New("{{ $pkg }}: {{ $groupBuilder }}.{{ $plural }} is not achievable when grouping more than 1 field")
}
var v []{{ $t }}
if err := {{ $groupReceiver }}.Scan(ctx, &v); err != nil {
return nil, err
}
return v, nil
}
// {{ $plural }}X is like {{ $plural }}, but panics if an error occurs.
func ({{ $groupReceiver }} *{{ $groupBuilder }}) {{ $plural }}X(ctx context.Context) []{{ $t }} {
v, err := {{ $groupReceiver }}.{{ $plural }}(ctx)
if err != nil {
panic(err)
}
return v
}
{{ $singular := pascal $t -}}
// {{ $singular }} returns a single {{ $t }} from a group-by query.
// It is only allowed when executing a group-by query with one field.
func ({{ $groupReceiver }} *{{ $groupBuilder }}) {{ $singular }}(ctx context.Context) (_ {{ $t }}, err error) {
var v []{{ $t }}
if v, err = {{ $groupReceiver }}.{{ $plural }}(ctx); err != nil {
return
}
switch len(v) {
case 1:
return v[0], nil
case 0:
err = &NotFoundError{ {{ $.Package }}.Label}
default:
err = fmt.Errorf("{{ $pkg }}: {{ $groupBuilder }}.{{ $plural }} returned %d results when one was expected", len(v))
}
return
}
// {{ $singular }}X is like {{ $singular }}, but panics if an error occurs.
func ({{ $groupReceiver }} *{{ $groupBuilder }}) {{ $singular }}X(ctx context.Context) {{ $t }} {
v, err := {{ $groupReceiver }}.{{ $singular }}(ctx)
if err != nil {
panic(err)
}
return v
}
{{ end }}
{{ with extend $ "Builder" $groupBuilder }}
{{ $tmpl := printf "dialect/%s/group" $.Storage }}
{{ xtemplate $tmpl . }}
{{ end }}
{{/* select builder */}}
{{ $selectReceiver := receiver $selectBuilder }}
// {{ $selectBuilder }} is the builder for selecting fields of {{ pascal $.Name }} entities.
type {{ $selectBuilder }} struct {
*{{ $builder }}
// intermediate query (i.e. traversal path).
{{ $.Storage }} {{ $.Storage.Builder }}
}
// Scan applies the selector query and scans the result into the given value.
func ({{ $selectReceiver }} *{{ $selectBuilder }}) Scan(ctx context.Context, v interface{}) error {
if err := {{ $selectReceiver }}.prepareQuery(ctx); err != nil {
return err
}
{{ $selectReceiver }}.{{ $.Storage }} = {{ $selectReceiver }}.{{ $builder }}.{{ $.Storage }}Query()
return {{ $selectReceiver }}.{{ $.Storage }}Scan(ctx, v)
}
// ScanX is like Scan, but panics if an error occurs.
func ({{ $selectReceiver }} *{{ $selectBuilder }}) ScanX(ctx context.Context, v interface{}) {
if err := {{ $selectReceiver }}.Scan(ctx, v); err != nil {
panic(err)
}
}
{{ range $t := primitives }}
{{ $plural := pascal $t | plural }}
// {{ $plural }} returns list of {{ plural $t }} from a selector. It is only allowed when selecting one field.
func ({{ $selectReceiver }} *{{ $selectBuilder }}) {{ $plural }}(ctx context.Context) ([]{{ $t }}, error) {
if len({{ $selectReceiver }}.fields) > 1 {
return nil, errors.New("{{ $pkg }}: {{ $selectBuilder }}.{{ $plural }} is not achievable when selecting more than 1 field")
}
var v []{{ $t }}
if err := {{ $selectReceiver }}.Scan(ctx, &v); err != nil {
return nil, err
}
return v, nil
}
// {{ $plural }}X is like {{ $plural }}, but panics if an error occurs.
func ({{ $selectReceiver }} *{{ $selectBuilder }}) {{ $plural }}X(ctx context.Context) []{{ $t }} {
v, err := {{ $selectReceiver }}.{{ $plural }}(ctx)
if err != nil {
panic(err)
}
return v
}
{{ $singular := pascal $t -}}
// {{ $singular }} returns a single {{ $t }} from a selector. It is only allowed when selecting one field.
func ({{ $selectReceiver }} *{{ $selectBuilder }}) {{ $singular }}(ctx context.Context) (_ {{ $t }}, err error) {
var v []{{ $t }}
if v, err = {{ $selectReceiver }}.{{ $plural }}(ctx); err != nil {
return
}
switch len(v) {
case 1:
return v[0], nil
case 0:
err = &NotFoundError{ {{ $.Package }}.Label}
default:
err = fmt.Errorf("{{ $pkg }}: {{ $selectBuilder }}.{{ $plural }} returned %d results when one was expected", len(v))
}
return
}
// {{ $singular }}X is like {{ $singular }}, but panics if an error occurs.
func ({{ $selectReceiver }} *{{ $selectBuilder }}) {{ $singular }}X(ctx context.Context) {{ $t }} {
v, err := {{ $selectReceiver }}.{{ $singular }}(ctx)
if err != nil {
panic(err)
}
return v
}
{{ end }}
{{ with extend $ "Builder" $selectBuilder }}
{{ $tmpl := printf "dialect/%s/select" $.Storage }}
{{ xtemplate $tmpl . }}
{{ end }}
{{ end }}
|