作者:rawlings
项目:gofabric
func ExampleQuery() {
ctx := context.Background()
client, err := datastore.NewClient(ctx, "project-id")
if err != nil {
log.Fatal(err)
}
// Count the number of the post entities.
q := datastore.NewQuery("Post")
n, err := client.Count(ctx, q)
if err != nil {
log.Fatal(err)
}
log.Printf("There are %d posts.", n)
// List the posts published since yesterday.
yesterday := time.Now().Add(-24 * time.Hour)
q = datastore.NewQuery("Post").Filter("PublishedAt >", yesterday)
it := client.Run(ctx, q)
// Use the iterator.
_ = it
// Order the posts by the number of comments they have recieved.
datastore.NewQuery("Post").Order("-Comments")
// Start listing from an offset and limit the results.
datastore.NewQuery("Post").Offset(20).Limit(10)
}
作者:camlistor
项目:camlistor
func ExampleQuery_kindless() {
var lastSeenKey *datastore.Key
// [START kindless_query]
query := datastore.NewQuery("").Filter("__key__ >", lastSeenKey)
// [END kindless_query]
_ = query // Use client.Run or client.GetAll to execute the query.
}
作者:rutmi
项目:pr.optim
func _getRatesKeys(r *http.Request, jsonKey []byte, unixtime int64) <-chan keyResult {
out := make(chan keyResult)
go func() {
defer close(out)
var ctx context.Context
if r != nil {
ctx = appengine.NewContext(r)
} else {
ctx = context.Background()
}
client, err := datastore.NewClient(ctx, "rp-optima")
if err != nil {
out <- keyResult{error: err}
return
}
/*if keys, err := client.GetAll(ctx, datastore.NewQuery("ResultData").Filter("timestamp<", unixtime).KeysOnly(), nil); err != nil {
out <- keyResult{error:err}
return
}else {
out <- keyResult{keys:keys}
}*/
if keys, err := client.GetAll(ctx, datastore.NewQuery("Rate").Filter("id<", unixtime).KeysOnly(), nil); err != nil {
out <- keyResult{error: err}
} else {
out <- keyResult{keys: keys}
}
}()
return out
}
作者:trything
项目:trything
func ExampleNewQuery_options() {
// Query to order the posts by the number of comments they have recieved.
q := datastore.NewQuery("Post").Order("-Comments")
// Start listing from an offset and limit the results.
q = q.Offset(20).Limit(10)
_ = q // TODO: Use the query.
}
作者:camlistor
项目:camlistor
func ExampleIterator_Cursor() {
ctx := context.Background()
client, _ := datastore.NewClient(ctx, "my-proj")
cursorStr := ""
// [START cursor_paging]
const pageSize = 5
query := datastore.NewQuery("Tasks").Limit(pageSize)
if cursorStr != "" {
cursor, err := datastore.DecodeCursor(cursorStr)
if err != nil {
log.Fatalf("Bad cursor %q: %v", cursorStr, err)
}
query = query.Start(cursor)
}
// Read the tasks.
var tasks []Task
var task Task
it := client.Run(ctx, query)
_, err := it.Next(&task)
for err == nil {
tasks = append(tasks, task)
_, err = it.Next(&task)
}
if err != datastore.Done {
log.Fatalf("Failed fetching results: %v", err)
}
// Get the cursor for the next page of results.
nextCursor, err := it.Cursor()
// [END cursor_paging]
_ = err // Check the error.
_ = nextCursor // Use nextCursor.String as the next page's token.
}
作者:GoogleCloudPlatfor
项目:golang-sample
func ExampleQuery_EventualConsistency() {
// [START eventual_consistent_query]
ancestor := datastore.NameKey("TaskList", "default", nil)
query := datastore.NewQuery("Task").Ancestor(ancestor).EventualConsistency()
// [END eventual_consistent_query]
_ = query // Use client.Run or client.GetAll to execute the query.
}
作者:GoogleCloudPlatfor
项目:golang-sample
func ExampleQuery_Ancestor() {
// [START ancestor_query]
ancestor := datastore.NameKey("TaskList", "default", nil)
query := datastore.NewQuery("Task").Ancestor(ancestor)
// [END ancestor_query]
_ = query // Use client.Run or client.GetAll to execute the query.
}
作者:GoogleCloudPlatfor
项目:golang-sample
func ExampleQuery_keyFilter() {
// [START key_filter]
key := datastore.NameKey("Task", "someTask", nil)
query := datastore.NewQuery("Task").Filter("__key__ >", key)
// [END key_filter]
_ = query // Use client.Run or client.GetAll to execute the query.
}
作者:trything
项目:trything
func ExampleIterator_Cursor() {
ctx := context.Background()
client, err := datastore.NewClient(ctx, "project-id")
if err != nil {
// TODO: Handle error.
}
it := client.Run(ctx, datastore.NewQuery("Post"))
for {
var p Post
_, err := it.Next(&p)
if err == iterator.Done {
break
}
if err != nil {
// TODO: Handle error.
}
fmt.Println(p)
cursor, err := it.Cursor()
if err != nil {
// TODO: Handle error.
}
// When printed, a cursor will display as a string that can be passed
// to datastore.NewCursor.
fmt.Printf("to resume with this post, use cursor %s\n", cursor)
}
}
作者:camlistor
项目:camlistor
func ExampleQuery_keyFilter() {
ctx := context.Background()
// [START key_filter]
key := datastore.NewKey(ctx, "Task", "someTask", 0, nil)
query := datastore.NewQuery("Task").Filter("__key__ >", key)
// [END key_filter]
_ = query // Use client.Run or client.GetAll to execute the query.
}
作者:camlistor
项目:camlistor
func ExampleQuery_Ancestor() {
ctx := context.Background()
// [START ancestor_query]
ancestor := datastore.NewKey(ctx, "TaskList", "default", 0, nil)
query := datastore.NewQuery("Task").Ancestor(ancestor)
// [END ancestor_query]
_ = query // Use client.Run or client.GetAll to execute the query.
}
作者:camlistor
项目:camlistor
func ExampleQuery_Filter_arrayEquality() {
// [START array_value_equality]
query := datastore.NewQuery("Task").
Filter("Tag =", "fun").
Filter("Tag =", "programming")
// [END array_value_equality]
_ = query // Use client.Run or client.GetAll to execute the query.
}
作者:camlistor
项目:camlistor
func ExampleQuery_Filter_invalidInequality() {
// [START inequality_invalid]
query := datastore.NewQuery("Task").
Filter("Created >", time.Date(1990, 1, 1, 0, 0, 0, 0, time.UTC)).
Filter("Priority >", 3)
// [END inequality_invalid]
_ = query // The query is invalid.
}
作者:camlistor
项目:camlistor
func ExampleQuery_Filter_inequality() {
// [START inequality_range]
query := datastore.NewQuery("Task").
Filter("Created >", time.Date(1990, 1, 1, 0, 0, 0, 0, time.UTC)).
Filter("Created <", time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC))
// [END inequality_range]
_ = query // Use client.Run or client.GetAll to execute the query.
}
作者:camlistor
项目:camlistor
func ExampleQuery_invalidInequalitySortA() {
// [START inequality_sort_invalid_not_same]
query := datastore.NewQuery("Task").
Filter("Priority >", 3).
Order("Created")
// [END inequality_sort_invalid_not_same]
_ = query // The query is invalid.
}
作者:camlistor
项目:camlistor
func ExampleQuery_Filter_arrayInequality() {
// [START array_value_inequality_range]
query := datastore.NewQuery("Task").
Filter("Tag >", "learn").
Filter("Tag <", "math")
// [END array_value_inequality_range]
_ = query // Use client.Run or client.GetAll to execute the query.
}
作者:camlistor
项目:camlistor
func ExampleQuery_inequalitySort() {
// [START inequality_sort]
query := datastore.NewQuery("Task").
Filter("Priority >", 3).
Order("Priority").
Order("Created")
// [END inequality_sort]
_ = query // Use client.Run or client.GetAll to execute the query.
}
作者:camlistor
项目:camlistor
func ExampleQuery_Filter_mixed() {
// [START equal_and_inequality_range]
query := datastore.NewQuery("Task").
Filter("Priority =", 4).
Filter("Done =", false).
Filter("Created >", time.Date(1990, 1, 1, 0, 0, 0, 0, time.UTC)).
Filter("Created <", time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC))
// [END equal_and_inequality_range]
_ = query // Use client.Run or client.GetAll to execute the query.
}
作者:GoogleCloudPlatfor
项目:golang-sample
func queryVisits(ctx context.Context, limit int64) ([]*visit, error) {
// Print out previous visits.
q := datastore.NewQuery("Visit").
Order("-Timestamp").
Limit(10)
visits := make([]*visit, 0)
_, err := datastoreClient.GetAll(ctx, q, &visits)
return visits, err
}
作者:camlistor
项目:camlistor
func ExampleQuery_KeysOnly() {
ctx := context.Background()
client, _ := datastore.NewClient(ctx, "my-proj")
// [START keys_only_query]
query := datastore.NewQuery("Task").KeysOnly()
// [END keys_only_query]
// [START run_keys_only_query]
keys, err := client.GetAll(ctx, query, nil)
// [END run_keys_only_query]
_ = err // Make sure you check err.
_ = keys // Keys contains keys for all stored tasks.
}