4.3.1.17 查询

Query 实例表示具有绑定参数的特定查询。该实例由方法创建 com.mysql.clusterj.Session.<T>createQuery(com.mysql.clusterj.query.QueryDefinition<T>)

4.3.1.17.1 概要
 public  interface  Query<E>  {
// 公共静态字段// 公共方法}  public static final String INDEX_USED = "IndexUsed";
  public static final String SCAN_TYPE = "ScanType";
  public static final String SCAN_TYPE_INDEX_SCAN = "INDEX_SCAN";
  public static final String SCAN_TYPE_PRIMARY_KEY = "PRIMARY_KEY";
  public static final String SCAN_TYPE_TABLE_SCAN = "TABLE_SCAN";
  public static final String SCAN_TYPE_UNIQUE_KEY = "UNIQUE_KEY";
  public abstract int deletePersistentAll();
  public abstract Results<E> execute(Object parameter);
  public abstract Results<E> execute(Object[] parameters);
  public abstract Results<E> execute(Map<String, ?> parameters);
  public abstract Map<String, Object> explain();
  public abstract List<E> getResultList();
  public abstract void setLimits(long skip,
                                 long limit);

  public abstract void setOrdering(Ordering ordering,
                                   String[] orderingFields);

  public abstract void setParameter(String parameterName,
                                    Object value);

4.3.1.17.2 INDEX_USED
public static final String INDEX_USED = "IndexUsed";

查询解释索引使用的键

4.3.1.17.3 扫描类型
public static final String SCAN_TYPE = "ScanType";

查询说明扫描类型键

4.3.1.17.4 SCAN_TYPE_INDEX_SCAN
public static final String SCAN_TYPE_INDEX_SCAN = "INDEX_SCAN";

索引扫描的查询说明扫描类型值

4.3.1.17.5 SCAN_TYPE_PRIMARY_KEY
public static final String SCAN_TYPE_PRIMARY_KEY = "PRIMARY_KEY";

查询说明主键的扫描类型值

4.3.1.17.6 SCAN_TYPE_TABLE_SCAN
public static final String SCAN_TYPE_TABLE_SCAN = "TABLE_SCAN";

表扫描的查询说明扫描类型值

4.3.1.17.7 SCAN_TYPE_UNIQUE_KEY
public static final String SCAN_TYPE_UNIQUE_KEY = "UNIQUE_KEY";

查询解释唯一键的扫描类型值

4.3.1.17.8 deletePersistentAll()
public abstract int deletePersistentAll();

删除满足查询条件的实例。

表 4.35 deletePersistentAll()

范围 描述
返回 删除的实例数

4.3.1.17.9 执行(地图<字符串,?>)
public abstract Results<E> execute(Map<String, ?> parameters);

使用一个或多个命名参数执行查询。参数按名称解析。

表 4.36 execute(Map<String, ?>)

范围 描述
参数 参数
返回 结果

4.3.1.17.10 执行(对象...)
public abstract Results<E> execute(Object[] parameters);

使用一个或多个参数执行查询。参数按照它们在查询中声明的顺序解析。

表 4.37 执行(对象...)

范围 描述
参数 参数
返回 结果

4.3.1.17.11 执行(对象)
public abstract Results<E> execute(Object parameter);

仅使用一个参数执行查询。

Table 4.38 execute(Object)

Parameter Description
parameter the parameter
return the result

4.3.1.17.12 explain()
public abstract Map<String, Object> explain();

Explain how this query will be or was executed. If called before binding all parameters, throws ClusterJUserException. Return a map of key:value pairs that explain how the query will be or was executed. Details can be obtained by calling toString on the value. The following keys are returned:

  • ScanType: the type of scan, with values:

    • PRIMARY_KEY: the query used key lookup with the primary key

    • UNIQUE_KEY: the query used key lookup with a unique key

    • INDEX_SCAN: the query used a range scan with a non-unique key

    • TABLE_SCAN: the query used a table scan

  • IndexUsed: the name of the index used, if any

Table 4.39 explain()

Parameter Description
return the data about the execution of this query

Exceptions

ClusterJUserException

if not all parameters are bound

4.3.1.17.13 getResultList()
public abstract List<E> getResultList();

Get the results as a list.

Table 4.40 getResultList()

Parameter Description
return the result

Exceptions

ClusterJUserException

if not all parameters are bound

ClusterJDatastoreException

if an exception is reported by the datastore

4.3.1.17.14 setLimits(long, long)
public abstract void setLimits(long skip,
                               long limit);

Set limits on results to return. The execution of the query is modified to return only a subset of results. If the filter would normally return 100 instances, skip is set to 50, and limit is set to 40, then the first 50 results that would have been returned are skipped, the next 40 results are returned and the remaining 10 results are ignored.

Skip must be greater than or equal to 0. Limit must be greater than or equal to 0. Limits may not be used with deletePersistentAll.

Table 4.41 setLimits(long, long)

Parameter Description
skip the number of results to skip
limit the number of results to return after skipping; use Long.MAX_VALUE for no limit.

4.3.1.17.15 setOrdering(Query.Ordering, String...)
public abstract void setOrdering(Ordering ordering,
                                 String[] orderingFields);

Set ordering for the results of this query. The execution of the query is modified to use an index previously defined.

  • There must be an index defined on the columns mapped to the ordering fields, in the order of the ordering fields.

  • There must be no gaps in the ordering fields relative to the index.

  • All ordering fields must be in the index, but not all fields in the index need be in the ordering fields.

  • If an "in" predicate is used in the filter on a field in the ordering, it can only be used with the first field.

  • If any of these conditions is violated, ClusterJUserException is thrown when the query is executed.

If an "in" predicate is used, each element in the parameter defines a separate range, and ordering is performed within that range. There may be a better (more efficient) index based on the filter, but specifying the ordering will force the query to use an index that contains the ordering fields.

Table 4.42 setOrdering(Query.Ordering, String...)

Parameter Description
ordering either Ordering.ASCENDING or Ordering.DESCENDING
orderingFields the fields to order by

4.3.1.17.16 setParameter(String, Object)
public abstract void setParameter(String parameterName,
                                  Object value);

Set the value of a parameter. If called multiple times for the same parameter, silently replace the value.

Table 4.43 setParameter(String, Object)

Parameter Description
parameterName the name of the parameter
value the value for the parameter