Java Code Examples for com.google.cloud.datastore.Query#newGqlQueryBuilder()
The following examples show how to use
com.google.cloud.datastore.Query#newGqlQueryBuilder() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultDatastoreMetadata.java From catatumbo with Apache License 2.0 | 6 votes |
@Override public List<String> getKinds(boolean excludeSystemKinds) { try { String query = "SELECT __key__ FROM " + ENTITY_KINDS + " ORDER BY __key__"; GqlQuery.Builder<Key> gqlQueryBuilder = Query.newGqlQueryBuilder(ResultType.KEY, query); gqlQueryBuilder.setNamespace(entityManager.getEffectiveNamespace()); GqlQuery<Key> gqlQuery = gqlQueryBuilder.build(); QueryResults<Key> results = entityManager.getDatastore().run(gqlQuery); List<String> kinds = new ArrayList<>(50); while (results.hasNext()) { Key key = results.next(); String kind = key.getName(); if (excludeSystemKinds && kind.startsWith("__")) { continue; } kinds.add(key.getName()); } return kinds; } catch (DatastoreException exp) { throw new EntityManagerException(exp); } }
Example 2
Source File: DefaultDatastoreMetadata.java From catatumbo with Apache License 2.0 | 5 votes |
@Override public QueryResponse<String> getNamespaces(DatastoreCursor fromCursor, int limit) { try { String query = "SELECT __key__ FROM " + ENTITY_NAMESPACES + " ORDER BY __key__"; if (limit > 0) { query += " LIMIT @Limit"; } query += " OFFSET @Offset"; GqlQuery.Builder<Key> gqlQueryBuilder = Query.newGqlQueryBuilder(ResultType.KEY, query); if (limit > 0) { gqlQueryBuilder.setBinding("Limit", limit); } gqlQueryBuilder.setBinding("Offset", Cursor.fromUrlSafe(fromCursor.getEncoded())); GqlQuery<Key> gqlQuery = gqlQueryBuilder.build(); Datastore datastore = entityManager.getDatastore(); QueryResults<Key> results = datastore.run(gqlQuery); DefaultQueryResponse<String> response = new DefaultQueryResponse<>(); List<String> namespaces = new ArrayList<>(Math.max(limit, 50)); response.setStartCursor(new DefaultDatastoreCursor(results.getCursorAfter().toUrlSafe())); while (results.hasNext()) { Key key = results.next(); String name = key.getName(); namespaces.add(name == null ? "" : name); } response.setResults(namespaces); response.setEndCursor(new DefaultDatastoreCursor(results.getCursorAfter().toUrlSafe())); return response; } catch (DatastoreException exp) { throw new EntityManagerException(exp); } }
Example 3
Source File: DefaultDatastoreReader.java From catatumbo with Apache License 2.0 | 5 votes |
/** * Executes the given {@link EntityQueryRequest} and returns the response. * * @param expectedResultType * the expected type of results. * @param request * the entity query request * @return the query response */ public <E> QueryResponse<E> executeEntityQueryRequest(Class<E> expectedResultType, EntityQueryRequest request) { try { GqlQuery.Builder<Entity> queryBuilder = Query.newGqlQueryBuilder(ResultType.ENTITY, request.getQuery()); queryBuilder.setNamespace(entityManager.getEffectiveNamespace()); queryBuilder.setAllowLiteral(request.isAllowLiterals()); QueryUtils.applyNamedBindings(queryBuilder, request.getNamedBindings()); QueryUtils.applyPositionalBindings(queryBuilder, request.getPositionalBindings()); GqlQuery<Entity> gqlQuery = queryBuilder.build(); QueryResults<Entity> results = nativeReader.run(gqlQuery); List<E> entities = new ArrayList<>(); DefaultQueryResponse<E> response = new DefaultQueryResponse<>(); response.setStartCursor(new DefaultDatastoreCursor(results.getCursorAfter().toUrlSafe())); while (results.hasNext()) { Entity result = results.next(); E entity = Unmarshaller.unmarshal(result, expectedResultType); entities.add(entity); } response.setResults(entities); response.setEndCursor(new DefaultDatastoreCursor(results.getCursorAfter().toUrlSafe())); response.setQueryResponseMetadata( new DefaultQueryResponseMetadata( QueryResponseMetadata.QueryState.forMoreResultsType(results.getMoreResults()))); entityManager.executeEntityListeners(CallbackType.POST_LOAD, entities); return response; } catch (DatastoreException exp) { throw new EntityManagerException(exp); } }
Example 4
Source File: DefaultDatastoreReader.java From catatumbo with Apache License 2.0 | 5 votes |
/** * Executes the given {@link ProjectionQueryRequest} and returns the response. * * @param expectedResultType * the expected type of results. * @param request * the projection query request * @return the query response */ public <E> QueryResponse<E> executeProjectionQueryRequest(Class<E> expectedResultType, ProjectionQueryRequest request) { try { GqlQuery.Builder<ProjectionEntity> queryBuilder = Query .newGqlQueryBuilder(ResultType.PROJECTION_ENTITY, request.getQuery()); queryBuilder.setNamespace(entityManager.getEffectiveNamespace()); queryBuilder.setAllowLiteral(request.isAllowLiterals()); QueryUtils.applyNamedBindings(queryBuilder, request.getNamedBindings()); QueryUtils.applyPositionalBindings(queryBuilder, request.getPositionalBindings()); GqlQuery<ProjectionEntity> gqlQuery = queryBuilder.build(); QueryResults<ProjectionEntity> results = nativeReader.run(gqlQuery); List<E> entities = new ArrayList<>(); DefaultQueryResponse<E> response = new DefaultQueryResponse<>(); response.setStartCursor(new DefaultDatastoreCursor(results.getCursorAfter().toUrlSafe())); while (results.hasNext()) { ProjectionEntity result = results.next(); E entity = Unmarshaller.unmarshal(result, expectedResultType); entities.add(entity); } response.setResults(entities); response.setEndCursor(new DefaultDatastoreCursor(results.getCursorAfter().toUrlSafe())); response.setQueryResponseMetadata( new DefaultQueryResponseMetadata( QueryResponseMetadata.QueryState.forMoreResultsType(results.getMoreResults()))); // TODO should we invoke PostLoad callback for projected entities? return response; } catch (DatastoreException exp) { throw new EntityManagerException(exp); } }
Example 5
Source File: DefaultDatastoreReader.java From catatumbo with Apache License 2.0 | 5 votes |
/** * Executes the given {@link KeyQueryRequest} and returns the response. * * @param request * the key query request * @return the query response */ public QueryResponse<DatastoreKey> executeKeyQueryRequest(KeyQueryRequest request) { try { GqlQuery.Builder<Key> queryBuilder = Query.newGqlQueryBuilder(ResultType.KEY, request.getQuery()); queryBuilder.setNamespace(entityManager.getEffectiveNamespace()); queryBuilder.setAllowLiteral(request.isAllowLiterals()); QueryUtils.applyNamedBindings(queryBuilder, request.getNamedBindings()); QueryUtils.applyPositionalBindings(queryBuilder, request.getPositionalBindings()); GqlQuery<Key> gqlQuery = queryBuilder.build(); QueryResults<Key> results = nativeReader.run(gqlQuery); List<DatastoreKey> entities = new ArrayList<>(); DefaultQueryResponse<DatastoreKey> response = new DefaultQueryResponse<>(); response.setStartCursor(new DefaultDatastoreCursor(results.getCursorAfter().toUrlSafe())); while (results.hasNext()) { Key result = results.next(); DatastoreKey datastoreKey = new DefaultDatastoreKey(result); entities.add(datastoreKey); } response.setResults(entities); response.setEndCursor(new DefaultDatastoreCursor(results.getCursorAfter().toUrlSafe())); response.setQueryResponseMetadata( new DefaultQueryResponseMetadata( QueryResponseMetadata.QueryState.forMoreResultsType(results.getMoreResults()))); return response; } catch (DatastoreException exp) { throw new EntityManagerException(exp); } }