StructuredQuery . Builder buildQuery ( ) { StructuredQuery . Builder structuredQuery = StructuredQuery . newBuilder ( ) ; structuredQuery . addFrom ( StructuredQuery . CollectionSelector . newBuilder ( ) . setCollectionId ( path . getId ( ) ) ) ; if ( options . fieldFilters . size ( ) == 1 ) { Filter filter = options . fieldFilters . get ( 0 ) . toProto ( ) ; if ( filter . hasFieldFilter ( ) ) { structuredQuery . getWhereBuilder ( ) . setFieldFilter ( filter . getFieldFilter ( ) ) ; } else { Preconditions . checkState ( filter . hasUnaryFilter ( ) , "Expected a UnaryFilter or a FieldFilter." ) ; structuredQuery . getWhereBuilder ( ) . setUnaryFilter ( filter . getUnaryFilter ( ) ) ; } } else if ( options . fieldFilters . size ( ) > 1 ) { Filter . Builder filter = Filter . newBuilder ( ) ; StructuredQuery . CompositeFilter . Builder compositeFilter = StructuredQuery . CompositeFilter . newBuilder ( ) ; compositeFilter . setOp ( CompositeFilter . Operator . AND ) ; for ( FieldFilter fieldFilter : options . fieldFilters ) { compositeFilter . addFilters ( fieldFilter . toProto ( ) ) ; } filter . setCompositeFilter ( compositeFilter . build ( ) ) ; structuredQuery . setWhere ( filter . build ( ) ) ; } if ( ! options . fieldOrders . isEmpty ( ) ) { for ( FieldOrder order : options . fieldOrders ) { structuredQuery . addOrderBy ( order . toProto ( ) ) ; } } if ( ! options . fieldProjections . isEmpty ( ) ) { structuredQuery . getSelectBuilder ( ) . addAllFields ( options . fieldProjections ) ; } if ( options . limit != - 1 ) { structuredQuery . setLimit ( Int32Value . newBuilder ( ) . setValue ( options . limit ) ) ; } if ( options . offset != - 1 ) { structuredQuery . setOffset ( options . offset ) ; } if ( options . startCursor != null ) { structuredQuery . setStartAt ( options . startCursor ) ; } if ( options . endCursor != null ) { structuredQuery . setEndAt ( options . endCursor ) ; } return structuredQuery ; }
Build the final Firestore query .