Ejemplo n.º 1
0
        public void LazyStructuredQuery()
        {
            string projectId   = _fixture.ProjectId;
            string namespaceId = _fixture.NamespaceId;

            // Snippet: RunQueryLazily(Query,*,*)
            DatastoreDb db    = DatastoreDb.Create(projectId, namespaceId);
            Query       query = new Query("book")
            {
                Filter = Filter.Equal("author", "Jane Austen")
            };
            LazyDatastoreQuery results = db.RunQueryLazily(query);

            // LazyDatastoreQuery implements IEnumerable<Entity>, but you can
            // call AsResponses() to see the raw RPC responses, or
            // GetAllResults() to get all the results into memory, complete with
            // the end cursor and the reason for the query finishing.
            foreach (Entity entity in results)
            {
                Console.WriteLine(entity);
            }
            // End snippet

            // This will run the query again, admittedly...
            List <Entity> entities = results.ToList();

            Assert.Equal(1, entities.Count);
            Entity book = entities[0];

            Assert.Equal("Jane Austen", (string)book["author"]);
            Assert.Equal("Pride and Prejudice", (string)book["title"]);
        }
Ejemplo n.º 2
0
        public void GetAllResults()
        {
            var results = new LazyDatastoreQuery(_responses.Select(r => r.Clone())).GetAllResults();

            Assert.Equal(_entities, results.Entities.ToArray());
            Assert.Equal(MoreResultsType.MoreResultsAfterLimit, results.MoreResults);
            Assert.Equal(ByteString.CopyFromUtf8("after-batch-4"), results.EndCursor);
        }
Ejemplo n.º 3
0
        public void LazyGqlQuery()
        {
            string projectId   = _fixture.ProjectId;
            string namespaceId = _fixture.NamespaceId;

            // Snippet: RunQueryLazily(GqlQuery,*)
            DatastoreDb db         = DatastoreDb.Create(projectId, namespaceId);
            KeyFactory  keyFactory = db.CreateKeyFactory("player");

            // Prepare the data: a player with two game child entities
            Entity player = new Entity
            {
                Key      = keyFactory.CreateIncompleteKey(),
                ["name"] = "Sophie"
            };
            Key    playerKey = db.Insert(player);
            Entity game1     = new Entity
            {
                Key = playerKey.WithElement(new PathElement {
                    Kind = "game"
                }),
                ["score"]     = 10,
                ["timestamp"] = new DateTime(2017, 2, 16, 8, 35, 0, DateTimeKind.Utc)
            };
            Entity game2 = new Entity
            {
                Key = playerKey.WithElement(new PathElement {
                    Kind = "game"
                }),
                ["score"]     = 25,
                ["timestamp"] = new DateTime(2017, 3, 15, 10, 35, 0, DateTimeKind.Utc)
            };

            db.Insert(game1, game2);

            // Perform a query within a transaction
            using (DatastoreTransaction transaction = db.BeginTransaction())
            {
                // Any query executed in a transaction must at least have an ancestor filter.
                GqlQuery query = new GqlQuery
                {
                    QueryString   = "SELECT * FROM game WHERE __key__ HAS ANCESTOR @player",
                    NamedBindings = { { "player", playerKey } }
                };
                LazyDatastoreQuery results = db.RunQueryLazily(query);
                // LazyDatastoreQuery implements IEnumerable<Entity>, but you can
                // call AsResponses() to see the raw RPC responses, or
                // GetAllResults() to get all the results into memory, complete with
                // the end cursor and the reason for the query finishing.
                foreach (Entity entity in results)
                {
                    Console.WriteLine(entity);
                }
            }
            // End snippet
        }
Ejemplo n.º 4
0
        public void AsResponses()
        {
            var query = new LazyDatastoreQuery(_responses.Select(r => r.Clone()));

            Assert.Equal(_responses, query.AsResponses());
        }