forked from GoogleCloudPlatform/golang-samples
/
projectionqueries.go
72 lines (62 loc) · 1.56 KB
/
projectionqueries.go
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
// Copyright 2011 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package sample
import (
"golang.org/x/net/context"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/log"
)
var ctx context.Context
type EventLog struct {
Title, ReadPath, DateWritten string
}
func example() {
// [START using_1]
q := datastore.NewQuery("People").Project("FirstName", "LastName")
// [END using_1]
_ = q
}
func example2() {
// [START using_2]
q := datastore.NewQuery("EventLog").
Project("Title", "ReadPath", "DateWritten").
Order("DateWritten")
t := q.Run(ctx)
for {
var l EventLog
_, err := t.Next(&l)
if err == datastore.Done {
break
}
if err != nil {
log.Errorf(ctx, "Running query: %v", err)
break
}
log.Infof(ctx, "Log record: %v, %v, %v", l.Title, l.ReadPath, l.DateWritten)
}
// [END using_2]
}
func example3() {
// [START grouping]
q := datastore.NewQuery("Person").
Project("LastName", "Height").Distinct().
Filter("Height >", 20).
Order("-Height").Order("LastName")
// [END grouping]
_ = q
type Foo struct {
A []int
B []string
}
// [START projections_and_multiple_valued_properties_1]
entity := Foo{A: []int{1, 1, 2, 3}, B: []string{"x", "y", "x"}}
// [END projections_and_multiple_valued_properties_1]
_ = entity
}
func example4() {
// [START projections_and_multiple_valued_properties_2]
q := datastore.NewQuery("Foo").Project("A", "B").Filter("A <", 3)
// [END projections_and_multiple_valued_properties_2]
_ = q
}