Package cn.leancloud

Class LCQuery<T extends LCObject>

java.lang.Object
cn.leancloud.LCQuery<T>
All Implemented Interfaces:
Cloneable
Direct Known Subclasses:
LCStatusQuery

public class LCQuery<T extends LCObject> extends Object implements Cloneable
  • Constructor Details

    • LCQuery

      public LCQuery(String theClassName)
      Constructs a query. A default query with no further parameters will retrieve all AVObjects of the provided class.
      Parameters:
      theClassName - The name of the class to retrieve AVObjects for.
  • Method Details

    • clone

      public LCQuery clone() throws CloneNotSupportedException
      Clone a new query object, which fully same to this.
      Overrides:
      clone in class Object
      Returns:
      a new AVQuery object.
      Throws:
      CloneNotSupportedException
    • getQuery

      public static <T extends LCObject> LCQuery<T> getQuery(String theClassName)
      Constructs a query. A default query with no further parameters will retrieve all AVObjects of the provided class.
      Type Parameters:
      T - template type.
      Parameters:
      theClassName - The name of the class to retrieve AVObjects for.
      Returns:
      the query object.
    • getQuery

      public static <T extends LCObject> LCQuery<T> getQuery(Class<T> clazz)
      Create a AVQuery with special sub-class.
      Type Parameters:
      T - template type.
      Parameters:
      clazz - The AVObject subclass
      Returns:
      The AVQuery
    • getClassName

      public String getClassName()
      Get class name
      Returns:
      class name.
    • setClassName

      public LCQuery<T> setClassName(String className)
      Set class name
      Parameters:
      className - class name.
      Returns:
      current instance.
    • getCachePolicy

      public LCQuery.CachePolicy getCachePolicy()
      Accessor for the caching policy.
      Returns:
      cache policy
    • setCachePolicy

      public LCQuery<T> setCachePolicy(LCQuery.CachePolicy cachePolicy)
      Change the caching policy of this query.
      Parameters:
      cachePolicy - cache policy.
      Returns:
      this query.
    • getPolicy

      public LCQuery.CachePolicy getPolicy()
      Get cache policy.
      Returns:
      cache policy.
    • setPolicy

      public LCQuery<T> setPolicy(LCQuery.CachePolicy policy)
      Change the caching policy of this query.
      Parameters:
      policy - cache policy.
      Returns:
      this query.
    • getMaxCacheAge

      public long getMaxCacheAge()
      Gets the maximum age of cached data that will be considered in this query. The returned value is in milliseconds
      Returns:
      max cache age(milliseconds).
    • setMaxCacheAge

      public LCQuery<T> setMaxCacheAge(long maxCacheAge)
      Sets the maximum age of cached data that will be considered in this query.
      Parameters:
      maxCacheAge - mac cached age.
      Returns:
      this query.
    • clearAllCachedResults

      public static void clearAllCachedResults()
      Clears the cached result for all queries.
    • clearCachedResult

      public void clearCachedResult()
      Removes the previously cached result for this query, forcing the next find() to hit the network. If there is no cached result for this query, then this is a no-op.
    • getLimit

      public int getLimit()
      Accessor for the limit.
      Returns:
      query limit.
    • setLimit

      public LCQuery<T> setLimit(int limit)
      Controls the maximum number of results that are returned. Setting a negative limit denotes retrieval without a limit. The default limit is 100, with a maximum of 1000 results being returned at a time.
      Parameters:
      limit - query limit.
      Returns:
      this query.
    • limit

      public LCQuery<T> limit(int limit)
      Parameters:
      limit - query limit
      Returns:
      this query.
      See Also:
    • skip

      public LCQuery<T> skip(int skip)
      Parameters:
      skip - qury skip
      Returns:
      this query.
      See Also:
    • getSkip

      public int getSkip()
      Accessor for the skip value.
      Returns:
      current skip value.
    • setSkip

      public LCQuery<T> setSkip(int skip)
      Controls the number of results to skip before returning any results. This is useful for pagination. Default is to skip zero results.
      Parameters:
      skip - query skip.
      Returns:
      this query
    • getOrder

      public String getOrder()
      Get order string.
      Returns:
      order string.
    • setOrder

      public LCQuery<T> setOrder(String order)
      Set query order fields.
      Parameters:
      order - order string.
      Returns:
      this query.
    • order

      public LCQuery<T> order(String order)
      Parameters:
      order - order string.
      Returns:
      this query.
      See Also:
    • addAscendingOrder

      public LCQuery<T> addAscendingOrder(String key)
      Also sorts the results in ascending order by the given key. The previous sort keys have precedence over this key.
      Parameters:
      key - The key to order by
      Returns:
      Returns the query so you can chain this call.
    • addDescendingOrder

      public LCQuery<T> addDescendingOrder(String key)
      Also sorts the results in descending order by the given key. The previous sort keys have precedence over this key.
      Parameters:
      key - The key to order by
      Returns:
      Returns the query so you can chain this call.
    • isIncludeACL

      public boolean isIncludeACL()
      Flag to indicate need ACL returned in result.
      Returns:
      include flag.
    • includeACL

      public LCQuery<T> includeACL(boolean includeACL)
      set include ACL or not.
      Parameters:
      includeACL - Flag to indicate need ACL returned in result.
      Returns:
      this query.
    • include

      public LCQuery<T> include(String key)
      Include nested AVObjects for the provided key. You can use dot notation to specify which fields in the included object that are also fetched.
      Parameters:
      key - The key that should be included.
      Returns:
      this query.
    • selectKeys

      public LCQuery<T> selectKeys(Collection<String> keys)
      Restrict the fields of returned AVObjects to only include the provided keys. If this is called multiple times, then all of the keys specified in each of the calls will be included.
      Parameters:
      keys - The set of keys to include in the result.
      Returns:
      this query.
    • orderByAscending

      public LCQuery<T> orderByAscending(String key)
      Sorts the results in ascending order by the given key.
      Parameters:
      key - The key to order by.
      Returns:
      Returns the query, so you can chain this call.
    • orderByDescending

      public LCQuery<T> orderByDescending(String key)
      Sorts the results in descending order by the given key.
      Parameters:
      key - The key to order by.
      Returns:
      Returns the query, so you can chain this call.
    • whereContainedIn

      public LCQuery<T> whereContainedIn(String key, Collection<? extends Object> values)
      Add a constraint to the query that requires a particular key's value to be contained in the provided list of values.
      Parameters:
      key - The key to check.
      values - The values that will match.
      Returns:
      Returns the query, so you can chain this call.
    • whereContains

      public LCQuery<T> whereContains(String key, String substring)
      Add a constraint for finding string values that contain a provided string. This will be slow for large datasets.
      Parameters:
      key - The key that the string to match is stored in.
      substring - The substring that the value must contain.
      Returns:
      Returns the query, so you can chain this call.
    • whereSizeEqual

      public LCQuery<T> whereSizeEqual(String key, int size)
      添加查询约束条件,查找key类型是数组,该数组的长度匹配提供的数值。
      Parameters:
      key - 查询的key
      size - 数组的长度
      Returns:
      this query.
      Since:
      2.0.2
    • whereContainsAll

      public LCQuery<T> whereContainsAll(String key, Collection<?> values)
      Add a constraint to the query that requires a particular key's value match another AVQuery. This only works on keys whose values are AVObjects or lists of AVObjects. Add a constraint to the query that requires a particular key's value to contain every one of the provided list of values.
      Parameters:
      key - The key to check. This key's value must be an array.
      values - The values that will match.
      Returns:
      Returns the query, so you can chain this call.
    • whereDoesNotExist

      public LCQuery<T> whereDoesNotExist(String key)
      Add a constraint for finding objects that do not contain a given key.
      Parameters:
      key - The key that should not exist
      Returns:
      this query.
    • whereEndsWith

      public LCQuery<T> whereEndsWith(String key, String suffix)
      Add a constraint for finding string values that end with a provided string. This will be slow for large datasets.
      Parameters:
      key - The key that the string to match is stored in.
      suffix - The substring that the value must end with.
      Returns:
      Returns the query, so you can chain this call.
    • whereEqualTo

      public LCQuery<T> whereEqualTo(String key, Object value)
      Add a constraint to the query that requires a particular key's value to be equal to the provided value.
      Parameters:
      key - The key to check.
      value - The value that the AVObject must contain.
      Returns:
      Returns the query, so you can chain this call.
    • addWhereItem

      protected LCQuery<T> addWhereItem(String key, String op, Object value)
    • whereExists

      public LCQuery<T> whereExists(String key)
      Add a constraint for finding objects that contain the given key.
      Parameters:
      key - The key that should exist.
      Returns:
      this query.
    • whereGreaterThan

      public LCQuery<T> whereGreaterThan(String key, Object value)
      Add a constraint to the query that requires a particular key's value to be greater than the provided value. w
      Parameters:
      key - The key to check.
      value - The value that provides an lower bound.
      Returns:
      Returns the query, so you can chain this call.
    • whereGreaterThanOrEqualTo

      public LCQuery<T> whereGreaterThanOrEqualTo(String key, Object value)
      Add a constraint to the query that requires a particular key's value to be greater or equal to than the provided value.
      Parameters:
      key - The key to check.
      value - The value that provides an lower bound.
      Returns:
      Returns the query, so you can chain this call.
    • whereLessThan

      public LCQuery<T> whereLessThan(String key, Object value)
      Add a constraint to the query that requires a particular key's value to be less than the provided value.
      Parameters:
      key - The key to check.
      value - The value that provides an upper bound.
      Returns:
      Returns the query, so you can chain this call.
    • whereLessThanOrEqualTo

      public LCQuery<T> whereLessThanOrEqualTo(String key, Object value)
      Add a constraint to the query that requires a particular key's value to be less or equal to than the provided value.
      Parameters:
      key - The key to check.
      value - The value that provides an lower bound.
      Returns:
      Returns the query, so you can chain this call.
    • whereMatches

      public LCQuery<T> whereMatches(String key, String regex)
      Add a regular expression constraint for finding string values that match the provided regular expression. This may be slow for large datasets.
      Parameters:
      key - The key that the string to match is stored in.
      regex - The regular expression pattern to match.
      Returns:
      Returns the query, so you can chain this call.
    • whereMatches

      public LCQuery<T> whereMatches(String key, String regex, String modifiers)
      Add a regular expression constraint for finding string values that match the provided regular expression. This may be slow for large datasets.
      Parameters:
      key - The key that the string to match is stored in.
      regex - The regular expression pattern to match.
      modifiers - Any of the following supported PCRE modifiers: i - Case insensitive search m - Search across multiple lines of input
      Returns:
      this query.
    • whereNear

      public LCQuery<T> whereNear(String key, LCGeoPoint point)
      Add a proximity based constraint for finding objects with key point values near the point given.
      Parameters:
      key - The key that the AVGeoPoint is stored in.
      point - The reference AVGeoPoint that is used.
      Returns:
      Returns the query, so you can chain this call.
    • whereNotContainedIn

      public LCQuery<T> whereNotContainedIn(String key, Collection<? extends Object> values)
      Add a constraint to the query that requires a particular key's value not be contained in the provided list of values.
      Parameters:
      key - The key to check.
      values - The values that will not match.
      Returns:
      Returns the query, so you can chain this call.
    • whereNotEqualTo

      public LCQuery<T> whereNotEqualTo(String key, Object value)
      Add a constraint to the query that requires a particular key's value to be not equal to the provided value.
      Parameters:
      key - The key to check.
      value - The value that must not be equalled.
      Returns:
      Returns the query, so you can chain this call.
    • whereStartsWith

      public LCQuery<T> whereStartsWith(String key, String prefix)
      Add a constraint for finding string values that start with a provided string. This query will use the backend index, so it will be fast even for large datasets.
      Parameters:
      key - The key that the string to match is stored in.
      prefix - The substring that the value must start with.
      Returns:
      Returns the query, so you can chain this call.
    • whereWithinGeoBox

      public LCQuery<T> whereWithinGeoBox(String key, LCGeoPoint southwest, LCGeoPoint northeast)
      Add a constraint to the query that requires a particular key's coordinates be contained within a given rectangular geographic bounding box.
      Parameters:
      key - The key to be constrained.
      southwest - The lower-left inclusive corner of the box.
      northeast - The upper-right inclusive corner of the box.
      Returns:
      Returns the query, so you can chain this call.
    • whereWithinKilometers

      public LCQuery<T> whereWithinKilometers(String key, LCGeoPoint point, double maxDistance)
      Add a proximity based constraint for finding objects with key point values near the point given and within the maximum distance given. Radius of earth used is 6371.0 kilometers.
      Parameters:
      key - The key that the AVGeoPoint is stored in.
      point - The reference AVGeoPoint that is used.
      maxDistance - Maximum distance (in kilometers) of results to return.
      Returns:
      Returns the query, so you can chain this call.
    • whereWithinKilometers

      public LCQuery<T> whereWithinKilometers(String key, LCGeoPoint point, double maxDistance, double minDistance)
      Add a proximity based constraint for finding objects with key point values near the point given and within the given ring area Radius of earth used is 6371.0 kilometers.
      Parameters:
      key - The key that the AVGeoPoint is stored in.
      point - The reference AVGeoPoint that is used.
      maxDistance - outer radius of the given ring in kilometers
      minDistance - inner radius of the given ring in kilometers
      Returns:
      Returns the query, so you can chain this call.
    • whereWithinMiles

      public LCQuery<T> whereWithinMiles(String key, LCGeoPoint point, double maxDistance)
      Add a proximity based constraint for finding objects with key point values near the point given and within the maximum distance given. Radius of earth used is 3958.8 miles.
      Parameters:
      key - The key that the AVGeoPoint is stored in.
      point - The reference AVGeoPoint that is used.
      maxDistance - outer radius of the given ring in miles.
      Returns:
      Returns the query, so you can chain this call.
    • whereWithinMiles

      public LCQuery<T> whereWithinMiles(String key, LCGeoPoint point, double maxDistance, double minDistance)
      Add a proximity based constraint for finding objects with key point values near the point given and within the given ring. Radius of earth used is 3958.8 miles.
      Parameters:
      key - The key that the AVGeoPoint is stored in.
      point - The reference AVGeoPoint that is used.
      maxDistance - outer radius of the given ring in miles.
      minDistance - inner radius of the given ring in miles.
      Returns:
      Returns the query, so you can chain this call.
    • whereWithinRadians

      public LCQuery<T> whereWithinRadians(String key, LCGeoPoint point, double maxDistance)
      Add a proximity based constraint for finding objects with key point values near the point given and within the maximum distance given.
      Parameters:
      key - The key that the AVGeoPoint is stored in.
      point - The reference AVGeoPoint that is used.
      maxDistance - Maximum distance (in radians) of results to return.
      Returns:
      Returns the query, so you can chain this call.
    • whereWithinRadians

      public LCQuery<T> whereWithinRadians(String key, LCGeoPoint point, double maxDistance, double minDistance)
      Add a proximity based constraint for finding objects with key point values near the point given and within the maximum distance given.
      Parameters:
      key - The key that the AVGeoPoint is stored in.
      point - The reference AVGeoPoint that is used.
      maxDistance - outer radius of the given radians.
      minDistance - inner radius of the given radians.
      Returns:
      Returns the query, so you can chain this call.
    • whereMatchesKeyInQuery

      public LCQuery<T> whereMatchesKeyInQuery(String key, String keyInQuery, LCQuery<?> query)
      Add a constraint to the query that requires a particular key's value matches a value for a key in the results of another AVQuery
      Parameters:
      key - The key whose value is being checked
      keyInQuery - The key in the objects from the sub query to look in
      query - The sub query to run
      Returns:
      Returns the query so you can chain this call.
    • whereMatchesQuery

      public LCQuery<T> whereMatchesQuery(String key, LCQuery<?> query)
      Add a constraint to the query that requires a particular key's value match another AVQuery. This only works on keys whose values are AVObjects or lists of AVObjects.
      Parameters:
      key - The key to check.
      query - The query that the value should match
      Returns:
      Returns the query so you can chain this call.
    • whereDoesNotMatchKeyInQuery

      public LCQuery<T> whereDoesNotMatchKeyInQuery(String key, String keyInQuery, LCQuery<?> query)
      Add a constraint to the query that requires a particular key's value does not match any value for a key in the results of another AVQuery.
      Parameters:
      key - The key whose value is being checked and excluded
      keyInQuery - The key in the objects from the sub query to look in
      query - The sub query to run
      Returns:
      Returns the query so you can chain this call.
    • whereDoesNotMatchQuery

      public LCQuery<T> whereDoesNotMatchQuery(String key, LCQuery<?> query)
      Add a constraint to the query that requires a particular key's value does not match another AVQuery. This only works on keys whose values are AVObjects or lists of AVObjects.
      Parameters:
      key - The key to check.
      query - The query that the value should not match
      Returns:
      Returns the query so you can chain this call.
    • or

      public static <T extends LCObject> LCQuery<T> or(List<LCQuery<T>> queries)
      Constructs a query that is the or of the given queries.
      Type Parameters:
      T - template type.
      Parameters:
      queries - The list of AVQueries to 'or' together
      Returns:
      A AVQuery that is the 'or' of the passed in queries
    • and

      public static <T extends LCObject> LCQuery<T> and(List<LCQuery<T>> queries)
      Constructs a query that is the and of the given queries.
      Type Parameters:
      T - template type.
      Parameters:
      queries - The list of AVQueries to 'and' together
      Returns:
      A AVQuery that is the 'and' of the passed in queries
    • hasCachedResult

      public boolean hasCachedResult()
      Has cached result or not.
      Returns:
      cache result existed or not.
    • find

      public List<T> find()
      Execute query in blocking mode.
      Returns:
      result of list.
    • findInBackground

      public io.reactivex.Observable<List<T>> findInBackground()
      Execute query in async mode.
      Returns:
      observable instance.
    • findInBackground

      public io.reactivex.Observable<List<T>> findInBackground(LCUser asAuthenticatedUser)
      Execute query in async mode.
      Parameters:
      asAuthenticatedUser - explicit user for request authentication.
      Returns:
      observable instance. in general, this method should be invoked in lean engine.
    • findInBackground

      protected io.reactivex.Observable<List<T>> findInBackground(LCUser asAuthenticatedUser, int explicitLimit)
    • get

      public T get(String objectId)
      Get Object with specified objectId in blocking mode.
      Parameters:
      objectId - object id.
      Returns:
      object instance.
    • getInBackground

      public io.reactivex.Observable<T> getInBackground(String objectId)
      Get Object with specified objectId in async mode.
      Parameters:
      objectId - object id.
      Returns:
      observable instance.
    • getInBackground

      public io.reactivex.Observable<T> getInBackground(LCUser asAuthenticatedUser, String objectId)
      Get Object with specified objectId in async mode.
      Parameters:
      asAuthenticatedUser - explicit user for request authentication.
      objectId - object id.
      Returns:
      observable instance. in general, this method should be invoked in lean engine.
    • getFirst

      public T getFirst()
      Get first result in blocking mode.
      Returns:
      first result.
    • getFirst

      public T getFirst(LCUser asAuthenticatedUser)
      Get first result in blocking mode.
      Parameters:
      asAuthenticatedUser - explicit user for request authentication.
      Returns:
      first result. in general, this method should be invoked in lean engine.
    • getFirstInBackground

      public io.reactivex.Observable<T> getFirstInBackground()
      Get first result in async mode.
      Returns:
      observable instance.
    • getFirstInBackground

      public io.reactivex.Observable<T> getFirstInBackground(LCUser asAuthenticatedUser)
      Get first result in async mode.
      Parameters:
      asAuthenticatedUser - explicit user for request authentication.
      Returns:
      observable instance. in general, this method should be invoked in lean engine.
    • count

      public int count()
      Get result count in blocking mode.
      Returns:
      result count.
    • count

      public int count(LCUser asAuthenticatedUser)
      Get result count in blocking mode.
      Parameters:
      asAuthenticatedUser - explicit user for request authentication.
      Returns:
      result count. in general, this method should be invoked in lean engine.
    • countInBackground

      public io.reactivex.Observable<Integer> countInBackground()
      Get result count in async mode.
      Returns:
      observable instance.
    • countInBackground

      public io.reactivex.Observable<Integer> countInBackground(LCUser asAuthenticatedUser)
      Get result count in async mode.
      Parameters:
      asAuthenticatedUser - explicit user for request authentication.
      Returns:
      observable instance. in general, this method should be invoked in lean engine.
    • deleteAll

      public void deleteAll()
      Delete all query result in blocking mode.
    • deleteAll

      public void deleteAll(LCUser asAuthenticatedUser)
      Delete all query result in blocking mode.
      Parameters:
      asAuthenticatedUser - explicit user for request authentication. in general, this method should be invoked in lean engine.
    • deleteAllInBackground

      public io.reactivex.Observable<LCNull> deleteAllInBackground()
      Delete all query result in async mode.
      Returns:
      observable instance.
    • deleteAllInBackground

      public io.reactivex.Observable<LCNull> deleteAllInBackground(LCUser asAuthenticatedUser)
      Delete all query result in async mode.
      Parameters:
      asAuthenticatedUser - explicit user for request authentication.
      Returns:
      observable instance. in general, this method should be invoked in lean engine.
    • assembleParameters

      public Map<String,String> assembleParameters()
      Assemble query parameters.
      Returns:
      map of query parameters.
    • assembleJsonParam

      protected Map<String,Object> assembleJsonParam()