com.psddev.dari.db.Query类的使用及代码示例

x33g5p2x  于2022-01-28 转载在 其他  
字(13.0k)|赞(0)|评价(0)|浏览(115)

本文整理了Java中com.psddev.dari.db.Query类的一些代码示例,展示了Query类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Query类的具体详情如下:
包路径:com.psddev.dari.db.Query
类名称:Query

Query介绍

[英]Query over objects in a Database.

Typical use looks like:

Query<Article> query = Query.from(Article.class); 
query.where("author = ?", author); 
query.sortAscending("headline"); 
List<Article> articles = query.select();

Which is roughly equivalent to the following SQL:

SELECT  
FROM Article 
WHERE author = ? 
ORDER BY headline ASC

Most methods can be chained so the above query can be rewritten as:

List<Article> articles = Query. 
    from(Article.class). 
    where("author = ?", author). 
    sortAscending("headline"). 
    select();

The #and provide a convenient way to split the PredicateParser.Static#parse around the logical flow of the program:

Query<Article> query = Query.from(Article.class); 
query.where("author = ?", author); 
query.sortAscending("headline"); 
if (...) { 
    query.and("topic = ?", topic1); 
} else { 
    query.and("topic = ?", topic2); 
}

Or you can use the predicate classes directly for more control over the whole process:

Query<Article> query = Query.from(Article.class); 
String comparison = ... 
String compound = ... 
Predicate predicate = new ComparisonPredicate(comparison, null, "author", author); 
if (...) { 
    predicate = new CompoundPredicate(compound, Arrays.asList( 
            predicate, 
            new ComparisonPredicate(comparison, null, "topic", topic1))); 
} else { 
    predicate = new CompoundPredicate(compound, Arrays.asList( 
            predicate, 
            new ComparisonPredicate(comparison, null, "topic", topic2))); 
} 
List<Article> articles = query.where(predicate).select();

Finally, joins are not supported, but subqueries are:

Query<Author> authorQuery = Query. 
    from(Author.class). 
    where("name = ?", name); 
Query<Article> articleQuery = Query. 
    from(Article.class). 
    where("author = ?", authorQuery). 
    sortAscending("headline");

[中]查询数据库中的对象。
典型用法如下所示:

Query<Article> query = Query.from(Article.class); 
query.where("author = ?", author); 
query.sortAscending("headline"); 
List<Article> articles = query.select();

这大致相当于以下SQL:

SELECT  
FROM Article 
WHERE author = ? 
ORDER BY headline ASC

大多数方法都可以链接,因此上述查询可以重写为:

List<Article> articles = Query. 
    from(Article.class). 
    where("author = ?", author). 
    sortAscending("headline"). 
    select();

#和提供了一种方便的方法来拆分谓词解析器。静态#围绕程序的逻辑流进行解析:

Query<Article> query = Query.from(Article.class); 
query.where("author = ?", author); 
query.sortAscending("headline"); 
if (...) { 
    query.and("topic = ?", topic1); 
} else { 
    query.and("topic = ?", topic2); 
}

或者,您可以直接使用谓词类对整个过程进行更多控制:

Query<Article> query = Query.from(Article.class); 
String comparison = ... 
String compound = ... 
Predicate predicate = new ComparisonPredicate(comparison, null, "author", author); 
if (...) { 
    predicate = new CompoundPredicate(compound, Arrays.asList( 
            predicate, 
            new ComparisonPredicate(comparison, null, "topic", topic1))); 
} else { 
    predicate = new CompoundPredicate(compound, Arrays.asList( 
            predicate, 
            new ComparisonPredicate(comparison, null, "topic", topic2))); 
} 
List<Article> articles = query.where(predicate).select();

最后,不支持联接,但子查询是:

Query<Author> authorQuery = Query. 
    from(Author.class). 
    where("name = ?", name); 
Query<Article> articleQuery = Query. 
    from(Article.class). 
    where("author = ?", authorQuery). 
    sortAscending("headline");

代码示例

代码示例来源:origin: perfectsense/dari

@Override
public int hashCode() {
  return ObjectUtils.hashCode(
      getGroup(),
      getObjectClass(),
      getPredicate(),
      getSorters(),
      getDatabase(),
      getFields(),
      getOptions(),
      isResolveToReferenceOnly(),
      getTimeout());
}

代码示例来源:origin: perfectsense/brightspot-cms

/** Returns the number of remaining items to be worked on. */
public long countIncomplete() {
  return getQuery().clone()
      .and("id != ?", Query.from(Object.class).where("cms.workstream.completeIds ^= ?", getId().toString() + ","))
      .count();
}

代码示例来源:origin: perfectsense/brightspot-cms

@Override
  protected Section produce(String name) {
    return Query.from(Section.class).where("internalName = ?", name).first();
  }
});

代码示例来源:origin: perfectsense/brightspot-cms

/**
 * Finds the last action from this device.
 *
 * @return May be {@code null}.
 */
public ToolUserAction findLastAction() {
  return Query
      .from(ToolUserAction.class)
      .where("device = ?", this)
      .sortDescending("time")
      .first();
}

代码示例来源:origin: perfectsense/dari

/**
 * Creates an absolute version of the predicate that can be embedded
 * in other queries.
 */
public Predicate createAbsolutePredicate() {
  Predicate predicate = getPredicate();
  return predicate != null ? addPrefix(getGroup() + "/", predicate) : null;
}

代码示例来源:origin: perfectsense/dari

@Override
public <T> T readFirst(Query<T> query) {
  if (query.getSorters().isEmpty()) {
    Predicate predicate = query.getPredicate();
    if (predicate instanceof CompoundPredicate) {
          Query<T> childQuery = query.clone();
          childQuery.setPredicate(child);
    Class<?> objectClass = query.getObjectClass();
    List<Object> ids;
        && query.getPredicate() == null) {
      ids = query.findIdOnlyQueryValues();

代码示例来源:origin: perfectsense/dari

Set<ObjectType> queryTypes = query.getConcreteTypes(database.getEnvironment());
Set<UUID> unresolvedTypeIds = (Set<UUID>) query.getOptions().get(State.UNRESOLVED_TYPE_IDS_QUERY_OPTION);
String extraJoins = ObjectUtils.to(String.class, query.getOptions().get(SqlDatabase.EXTRA_JOINS_QUERY_OPTION));
    Query.MappedKey mappedKey = query.mapEmbeddedKey(database.getEnvironment(), queryKey);
    mappedKeys.put(queryKey, mappedKey);
    selectIndex(queryKey, mappedKey);
String extraWhere = ObjectUtils.to(String.class, query.getOptions().get(SqlDatabase.EXTRA_WHERE_QUERY_OPTION));
if (!ObjectUtils.isBlank(extraWhere)) {
  whereBuilder.append('(');
if (!query.isFromAll()) {
  Set<UUID> typeIds = query.getConcreteTypeIds(database);
  whereBuilder.append("\nAND ");
Predicate predicate = query.getPredicate();
for (Sorter sorter : query.getSorters()) {
  addOrderByClause(orderByBuilder, sorter, true, false);
  Query.MappedKey key = query.mapEmbeddedKey(database.getEnvironment(), keyNameBuilder.toString());
  ObjectIndex useIndex = null;
    query.getExtraSourceColumns().put(indexFieldName, indexFieldName);
String extraHaving = ObjectUtils.to(String.class, query.getOptions().get(SqlDatabase.EXTRA_HAVING_QUERY_OPTION));

代码示例来源:origin: perfectsense/dari

@Override
public void format(HtmlWriter writer) throws IOException {
  String objectClass = getObjectClass() != null ? getObjectClass().getName() : null;
  if (isFromAll()) {
    codeBuilder.append(".fromAll()");
      codeBuilder.append(getGroup());
      codeBuilder.append("\")");
  Predicate predicate = getPredicate();
  if (predicate != null) {
    codeBuilder.append(".where(\"");
  for (Sorter sorter : getSorters()) {
    codeBuilder.append(".sort(\"");
    codeBuilder.append(sorter.getOperator());
  List<String> fields = getFields();
  if (fields != null) {
    codeBuilder.append(".fields(");

代码示例来源:origin: perfectsense/dari

Predicate predicate = query.getPredicate();
if (predicate != null) {
  appendPredicate(query, queryBuilder, predicate);
Query<?> facetedQuery = query.getFacetQuery();
if (facetedQuery != null) {
  StringBuilder fq = new StringBuilder();
  appendPredicate(facetedQuery, fq, query.getPredicate());
  solrQuery.addFacetQuery(fq.toString());
  solrQuery.setFacetMinCount(1);
Map<String, Object> facetedFields = query.getFacetedFields();
if (!facetedFields.isEmpty()) {
  boolean facet = false;
Map<String, Object> facetedRanges = query.getFacetedRanges();
if (!facetedRanges.isEmpty()) {
  for (Map.Entry<String, Object> entry : facetedRanges.entrySet()) {
if (!query.isFromAll()) {
  Set<ObjectType> types = query.getConcreteTypes(getEnvironment());
      for (UUID typeId : query.getConcreteTypeIds(this)) {
        queryBuilder.append(Static.escapeValue(typeId));
        queryBuilder.append(" || ");
for (Sorter sorter : query.getSorters()) {
  String operator = sorter.getOperator();
  boolean isAscending = Sorter.ASCENDING_OPERATOR.equals(operator);

代码示例来源:origin: perfectsense/dari

Query<T> query = Query.from(applicationClass).where("_type = ?", type.getId()).using(database);
T app = query.first();
    app = query.clone().noCache().first();
    if (app == null) {
      app = (T) type.createObject(null);

代码示例来源:origin: perfectsense/dari

Query<T> nextQuery = query.clone();
if (lastTypeId != null) {
  nextQuery.and("_type = ? and _id > ?", lastTypeId, lastId);
items = nextQuery.select(0, fetchSize).getItems();
    nextQuery = query.clone().and("_type > ?", lastTypeId);
    items = nextQuery.select(0, fetchSize).getItems();
    size = items.size();

代码示例来源:origin: perfectsense/brightspot-cms

public static ToolUser getByToken(String token) {
    ToolUser user = Query.from(ToolUser.class).option(Database.DISABLE_FUNNEL_CACHE_QUERY_OPTION, true).where("loginTokens/token = ?", token).first();
    return user != null && user.getLoginToken(token) != null ? user : null;
  }
}

代码示例来源:origin: perfectsense/brightspot-cms

@Override
  protected <T> Query<T> filterQuery(Query<T> query) {
    return query.clone().master().resolveInvisible().option(Database.DISABLE_FUNNEL_CACHE_QUERY_OPTION, true);
  }
};

代码示例来源:origin: perfectsense/brightspot-cms

public Query<?> toQuery() {
  State state = getState();
  Query<?> query = Query.fromType(getQueryType());
  Predicate predicate = query.getPredicate();
  for (ObjectField field : getIndexedFields()) {
    String name = field.getInternalName();
    Object value = state.get(FIELD_PREFIX + name);
    if (!ObjectUtils.isBlank(value)) {
      String type = field.getInternalItemType();
      String operator = (String) state.get(OPERATOR_PREFIX + name);
      if (operator == null) {
        operator = ObjectField.REFERENTIAL_TEXT_TYPE.equals(type) || ObjectField.TEXT_TYPE.equals(type) ? "matchesAll" : "equalsAny";
      }
      predicate = CompoundPredicate.combine(
          PredicateParser.AND_OPERATOR,
          predicate,
          PredicateParser.Static.parse(name + " " + operator + " ?", value));
    }
  }
  query.setPredicate(predicate);
  ObjectField sortField = getSortField();
  if (ObjectField.DATE_TYPE.equals(sortField.getInternalItemType())) {
    query.sortDescending(sortField.getInternalName());
  } else {
    query.sortAscending(sortField.getInternalName());
  }
  return query;
}

代码示例来源:origin: perfectsense/dari

public ByIdIterator(Query<T> query, int fetchSize) {
  if (!query.getSorters().isEmpty()) {
    throw new IllegalArgumentException("Can't iterate over a query that has sorters!");
  }
  this.query = query.clone().timeout(0.0).sortAscending("_type").sortAscending("_id");
  this.fetchSize = fetchSize > 0 ? fetchSize : 200;
}

代码示例来源:origin: perfectsense/dari

protected Table<?> initialize(Table<?> table) {
  whereCondition = query.isFromAll()
      ? DSL.trueCondition()
      : recordTypeIdField.in(query.getConcreteTypeIds(database));
  Predicate predicate = query.getPredicate();
  for (Sorter sorter : query.getSorters()) {
    SortField<?> sortField = database.sort(sorter, new SqlSortOptions(recordTableAlias));

代码示例来源:origin: perfectsense/dari

@Override
public boolean equals(Object other) {
  if (this == other) {
    return true;
  } else if (other instanceof Query) {
    Query<?> otherQuery = (Query<?>) other;
    return ObjectUtils.equals(group, otherQuery.group)
        && ObjectUtils.equals(objectClass, otherQuery.objectClass)
        && ObjectUtils.equals(predicate, otherQuery.predicate)
        && ObjectUtils.equals(getSorters(), otherQuery.getSorters())
        && ObjectUtils.equals(getDatabase(), otherQuery.getDatabase())
        && ObjectUtils.equals(getFields(), otherQuery.getFields())
        && ObjectUtils.equals(getOptions(), otherQuery.getOptions())
        && isResolveToReferenceOnly == otherQuery.isResolveToReferenceOnly
        && ObjectUtils.equals(timeout, otherQuery.timeout);
  } else {
    return false;
  }
}

代码示例来源:origin: perfectsense/brightspot-cms

public static <T extends Taxon> List<T> getRoots(Class<T> taxonClass, Site site, Predicate predicate) {
  Query<T> query = Query.from(taxonClass).where("cms.taxon.root = true");
  if (site != null) {
    query.and(site.itemsPredicate());
  }
  List<T> roots = query.selectAll();
  return filter(roots, predicate);
}

代码示例来源:origin: perfectsense/brightspot-cms

private PaginatedResult<WorkStream> getResults(ToolPageContext page) {
  Query<WorkStream> query = Query.from(WorkStream.class).where(page.siteItemsPredicate());
  ToolEntityType entityType = page.pageParam(ToolEntityType.class, TOOL_ENTITY_TYPE_PARAMETER, ToolEntityType.ANYONE);
  UUID entityId = null;
  if (entityType == ToolEntityType.USER || entityType == ToolEntityType.ROLE) {
    entityId = page.pageParam(UUID.class, TOOL_ENTITY_VALUE_PARAMETER, null);
  } else if (entityType == ToolEntityType.ME) {
    entityId = page.getUser().getId();
  }
  if (entityId != null) {
    query.and("assignedEntities = ?", entityId);
  }
  return query.select(page.param(long.class, OFFSET_PARAMETER), page.paramOrDefault(int.class, LIMIT_PARAMETER, LIMITS[0]));
}

代码示例来源:origin: perfectsense/dari

/**
 * Maps all keys used in this query to the fields of the types in the
 * given {@code environment}. This is a helper method for database
 * implementations and isn't meant for general consumption.
 *
 * @see #mapEmbeddedKey
 */
public Map<String, MappedKey> mapEmbeddedKeys(DatabaseEnvironment environment) {
  Map<String, MappedKey> mappedKeys = new HashMap<String, MappedKey>();
  addMappedPredicate(mappedKeys, environment, getPredicate());
  for (Sorter sorter : getSorters()) {
    Object first = sorter.getOptions().get(0);
    if (first instanceof String) {
      addMappedKey(mappedKeys, environment, (String) first);
    }
  }
  return mappedKeys;
}

相关文章