| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package datastore | 5 package datastore |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "bytes" | 8 "bytes" |
| 9 "fmt" | 9 "fmt" |
| 10 "sort" | 10 "sort" |
| (...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 } | 463 } |
| 464 | 464 |
| 465 // Finalize converts this Query to a FinalizedQuery. If the Query has any | 465 // Finalize converts this Query to a FinalizedQuery. If the Query has any |
| 466 // inconsistencies or violates any of the query rules, that will be returned | 466 // inconsistencies or violates any of the query rules, that will be returned |
| 467 // here. | 467 // here. |
| 468 func (q *Query) Finalize() (*FinalizedQuery, error) { | 468 func (q *Query) Finalize() (*FinalizedQuery, error) { |
| 469 if q.err != nil || q.finalized != nil { | 469 if q.err != nil || q.finalized != nil { |
| 470 return q.finalized, q.err | 470 return q.finalized, q.err |
| 471 } | 471 } |
| 472 | 472 |
| 473 ancSlice, hasAncestor := q.eqFilts["__ancestor__"] |
| 474 ancestor := (*Key)(nil) |
| 475 if hasAncestor { |
| 476 ancestor = ancSlice[0].Value().(*Key) |
| 477 } |
| 478 |
| 473 err := func() error { | 479 err := func() error { |
| 480 |
| 474 if q.kind == "" { // kindless query checks | 481 if q.kind == "" { // kindless query checks |
| 475 if q.ineqFiltProp != "" && q.ineqFiltProp != "__key__" { | 482 if q.ineqFiltProp != "" && q.ineqFiltProp != "__key__" { |
| 476 return fmt.Errorf( | 483 return fmt.Errorf( |
| 477 "kindless queries can only filter on __k
ey__, got %q", q.ineqFiltProp) | 484 "kindless queries can only filter on __k
ey__, got %q", q.ineqFiltProp) |
| 478 } | 485 } |
| 479 » » » if len(q.eqFilts) > 0 { | 486 » » » allowedEqs := 0 |
| 480 » » » » return fmt.Errorf("kindless queries not have any
equality filters") | 487 » » » if hasAncestor { |
| 488 » » » » allowedEqs = 1 |
| 489 » » » } |
| 490 » » » if len(q.eqFilts) > allowedEqs { |
| 491 » » » » return fmt.Errorf("kindless queries may not have
any equality filters") |
| 481 } | 492 } |
| 482 for _, o := range q.order { | 493 for _, o := range q.order { |
| 483 if o.Property != "__key__" || o.Descending { | 494 if o.Property != "__key__" || o.Descending { |
| 484 return fmt.Errorf("invalid order for kin
dless query: %#v", o) | 495 return fmt.Errorf("invalid order for kin
dless query: %#v", o) |
| 485 } | 496 } |
| 486 } | 497 } |
| 487 } | 498 } |
| 488 | 499 |
| 489 if q.keysOnly && q.project != nil && q.project.Len() > 0 { | 500 if q.keysOnly && q.project != nil && q.project.Len() > 0 { |
| 490 return errors.New("cannot project a keysOnly query") | 501 return errors.New("cannot project a keysOnly query") |
| 491 } | 502 } |
| 492 | 503 |
| 493 if q.ineqFiltProp != "" { | 504 if q.ineqFiltProp != "" { |
| 494 if len(q.order) > 0 && q.order[0].Property != q.ineqFilt
Prop { | 505 if len(q.order) > 0 && q.order[0].Property != q.ineqFilt
Prop { |
| 495 return fmt.Errorf( | 506 return fmt.Errorf( |
| 496 "first sort order must match inequality
filter: %q v %q", | 507 "first sort order must match inequality
filter: %q v %q", |
| 497 q.order[0].Property, q.ineqFiltProp) | 508 q.order[0].Property, q.ineqFiltProp) |
| 498 } | 509 } |
| 499 if q.ineqFiltLowSet && q.ineqFiltHighSet { | 510 if q.ineqFiltLowSet && q.ineqFiltHighSet { |
| 500 if q.ineqFiltHigh.Less(&q.ineqFiltLow) && | 511 if q.ineqFiltHigh.Less(&q.ineqFiltLow) && |
| 501 (q.ineqFiltHigh.Equal(&q.ineqFiltLow) && | 512 (q.ineqFiltHigh.Equal(&q.ineqFiltLow) && |
| 502 (!q.ineqFiltLowIncl || !q.ineqFi
ltHighIncl)) { | 513 (!q.ineqFiltLowIncl || !q.ineqFi
ltHighIncl)) { |
| 503 return ErrNullQuery | 514 return ErrNullQuery |
| 504 } | 515 } |
| 505 } | 516 } |
| 517 if q.ineqFiltProp == "__key__" { |
| 518 if q.ineqFiltLowSet { |
| 519 if q.ineqFiltLow.Type() != PTKey { |
| 520 return fmt.Errorf( |
| 521 "inequality filters on _
_key__ must use *ds.Key values") |
| 522 } |
| 523 if hasAncestor && !q.ineqFiltLow.Value()
.(*Key).HasAncestor(ancestor) { |
| 524 return fmt.Errorf( |
| 525 "inequality filters on _
_key__ must be descendants of the __ancestor__") |
| 526 } |
| 527 } |
| 528 if q.ineqFiltHighSet { |
| 529 if q.ineqFiltHigh.Type() != PTKey { |
| 530 return fmt.Errorf( |
| 531 "inequality filters on _
_key__ must use *ds.Key values") |
| 532 } |
| 533 if hasAncestor && !q.ineqFiltHigh.Value(
).(*Key).HasAncestor(ancestor) { |
| 534 return fmt.Errorf( |
| 535 "inequality filters on _
_key__ must be descendants of the __ancestor__") |
| 536 } |
| 537 } |
| 538 } |
| 506 } | 539 } |
| 507 | 540 |
| 508 err := error(nil) | 541 err := error(nil) |
| 509 if q.project != nil { | 542 if q.project != nil { |
| 510 q.project.Iter(func(p string) bool { | 543 q.project.Iter(func(p string) bool { |
| 511 if _, iseq := q.eqFilts[p]; iseq { | 544 if _, iseq := q.eqFilts[p]; iseq { |
| 512 err = fmt.Errorf("cannot project on equa
lity filter field: %s", p) | 545 err = fmt.Errorf("cannot project on equa
lity filter field: %s", p) |
| 513 return false | 546 return false |
| 514 } | 547 } |
| 515 return true | 548 return true |
| 516 }) | 549 }) |
| 517 } | 550 } |
| 518 return err | 551 return err |
| 519 }() | 552 }() |
| 520 if err != nil { | 553 if err != nil { |
| 521 q.err = err | 554 q.err = err |
| 522 return nil, err | 555 return nil, err |
| 523 } | 556 } |
| 524 | 557 |
| 525 ret := &FinalizedQuery{ | 558 ret := &FinalizedQuery{ |
| 526 original: q, | 559 original: q, |
| 527 kind: q.kind, | 560 kind: q.kind, |
| 528 | 561 |
| 529 keysOnly: q.keysOnly, | 562 keysOnly: q.keysOnly, |
| 530 » » eventuallyConsistent: q.eventualConsistency || q.eqFilts["__ance
stor__"] == nil, | 563 » » eventuallyConsistent: q.eventualConsistency || !hasAncestor, |
| 531 limit: q.limit, | 564 limit: q.limit, |
| 532 offset: q.offset, | 565 offset: q.offset, |
| 533 start: q.start, | 566 start: q.start, |
| 534 end: q.end, | 567 end: q.end, |
| 535 | 568 |
| 536 eqFilts: q.eqFilts, | 569 eqFilts: q.eqFilts, |
| 537 | 570 |
| 538 ineqFiltProp: q.ineqFiltProp, | 571 ineqFiltProp: q.ineqFiltProp, |
| 539 ineqFiltLow: q.ineqFiltLow, | 572 ineqFiltLow: q.ineqFiltLow, |
| 540 ineqFiltLowIncl: q.ineqFiltLowIncl, | 573 ineqFiltLowIncl: q.ineqFiltLowIncl, |
| 541 ineqFiltLowSet: q.ineqFiltLowSet, | 574 ineqFiltLowSet: q.ineqFiltLowSet, |
| 542 ineqFiltHigh: q.ineqFiltHigh, | 575 ineqFiltHigh: q.ineqFiltHigh, |
| 543 ineqFiltHighIncl: q.ineqFiltHighIncl, | 576 ineqFiltHighIncl: q.ineqFiltHighIncl, |
| 544 ineqFiltHighSet: q.ineqFiltHighSet, | 577 ineqFiltHighSet: q.ineqFiltHighSet, |
| 545 } | 578 } |
| 546 | 579 |
| 547 if q.project != nil { | 580 if q.project != nil { |
| 548 ret.project = q.project.ToSlice() | 581 ret.project = q.project.ToSlice() |
| 549 ret.distinct = q.distinct && q.project.Len() > 0 | 582 ret.distinct = q.distinct && q.project.Len() > 0 |
| 550 } | 583 } |
| 551 | 584 |
| 585 seenOrders := stringset.New(len(q.order)) |
| 586 |
| 552 // if len(q.order) > 0, we already enforce that the first order | 587 // if len(q.order) > 0, we already enforce that the first order |
| 553 // is the same as the inequality above. Otherwise we need to add it. | 588 // is the same as the inequality above. Otherwise we need to add it. |
| 554 if len(q.order) == 0 && q.ineqFiltProp != "" { | 589 if len(q.order) == 0 && q.ineqFiltProp != "" { |
| 555 ret.orders = []IndexColumn{{Property: q.ineqFiltProp}} | 590 ret.orders = []IndexColumn{{Property: q.ineqFiltProp}} |
| 591 seenOrders.Add(q.ineqFiltProp) |
| 556 } | 592 } |
| 557 | 593 |
| 558 seenOrders := stringset.New(len(q.order)) | |
| 559 | |
| 560 // drop orders where there's an equality filter | 594 // drop orders where there's an equality filter |
| 561 // https://cloud.google.com/appengine/docs/go/datastore/queries#sort_o
rders_are_ignored_on_properties_with_equality_filters | 595 // https://cloud.google.com/appengine/docs/go/datastore/queries#sort_o
rders_are_ignored_on_properties_with_equality_filters |
| 562 // Deduplicate orders | 596 // Deduplicate orders |
| 563 for _, o := range q.order { | 597 for _, o := range q.order { |
| 564 if _, iseq := q.eqFilts[o.Property]; !iseq { | 598 if _, iseq := q.eqFilts[o.Property]; !iseq { |
| 565 if seenOrders.Add(o.Property) { | 599 if seenOrders.Add(o.Property) { |
| 566 ret.orders = append(ret.orders, o) | 600 ret.orders = append(ret.orders, o) |
| 567 } | 601 } |
| 568 } | 602 } |
| 569 } | 603 } |
| (...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 700 if q.keysOnly { | 734 if q.keysOnly { |
| 701 p("KeysOnly") | 735 p("KeysOnly") |
| 702 } | 736 } |
| 703 | 737 |
| 704 if _, err := ret.WriteRune(')'); err != nil { | 738 if _, err := ret.WriteRune(')'); err != nil { |
| 705 panic(err) | 739 panic(err) |
| 706 } | 740 } |
| 707 | 741 |
| 708 return ret.String() | 742 return ret.String() |
| 709 } | 743 } |
| OLD | NEW |