public QueryBuilder skipFrom() { current = from; appendSpace(); return this; }
/** * Similar to how skipFrom and skipWhere were previously used, this sets * the current builder to {@link #where()} but without prefacing the * "where " string. Instead, it adds a space so that further calls to * {@link #where()} also won't add it. This can be used to create a clause * that can later be combined via {@link #subselect(QueryBuilder)}. */ public QueryBuilder whereClause() { current = where; appendSpace(); // Add something empty. return this; }
public QueryBuilder skipWhere() { current = where; appendSpace(); return this; }
void _type(String type) { current = select; if (select.length() == 0) { select.append(type); appendSpace(); } }
public QueryBuilder join(String path, String alias, boolean outer, boolean fetch) { current = join; if (outer) { join.append("left outer "); } join.append("join "); if (fetch) { join.append("fetch "); } join.append(path); appendSpace(); join.append("as "); join.append(alias); appendSpace(); return this; }
public QueryBuilder from(String type, String alias) { current = from; if (from.length() == 0) { from.append("from "); } else { from.append(", "); } this.self = alias; from.append(type); appendSpace(); from.append("as "); from.append(alias); appendSpace(); return this; }
private QueryBuilder _where(String bool, String str) { where(); // check size and set current whereCount++; if (whereCount != 1) { where.append(bool); } where.append(str); appendSpace(); return this; }
public QueryBuilder order(String path, boolean ascending) { current = order; if (order.length() == 0) { order.append("order by "); } else { order.append(", "); } order.append(path); appendSpace(); if (ascending) { order.append("asc "); } else { order.append("desc "); } return this; }
public QueryBuilder select(String... selects) { _type("select"); if (selects == null || selects.length == 0) { throw new ApiUsageException("Empty select"); } for (int i = 0; i < selects.length; i++) { if (i != 0) { select.append(", "); } select.append(selects[i]); appendSpace(); } return this; }
public void update(String table) { _type("update"); select.append(table); appendSpace(); skipFrom(); }
/** * Appends the string representation of the {@link QueryBuilder} argument * inside of parentheses. */ public QueryBuilder subselect(QueryBuilder subselect) { // Leave current as is. if ("".equals(subselect.queryString().trim())) { // Nothing to do. return this; } current.append("("); current.append(subselect.queryString()); current.append(")"); for (String key : subselect.listParams.keySet()) { this.listParams.put(key, subselect.listParams.get(key)); } appendSpace(); return this; }
public void delete(String table) { if (sqlQuery) { _type("delete from "); } else { _type("delete"); } select.append(table); appendSpace(); skipFrom(); }
qb.where(); qb.and(clause); qb.appendSpace();
public static void notNullOrLikeOrEqual(QueryBuilder qb, String path, Class type, Object value, boolean useLike, boolean caseSensitive) { if (null == value) { qb.and(path + " is null "); } else { String operator; if (useLike && String.class.isAssignableFrom(type)) { if (caseSensitive) { operator = "like"; } else { operator = "ilike"; } } else { operator = "="; } String alias = qb.unique_alias("main"); qb.and(path); qb.append(operator); qb.append(":"); qb.append(alias); qb.appendSpace(); qb.param(alias, value); } }
void on(QueryBuilder qb, boolean equals) { check(); String op = equals ? "=" : "!="; String unique = qb.unique_alias("owner"); qb.and(path); qb.append(" "); qb.append(op); qb.append(" :"); qb.append(unique); qb.appendSpace(); qb.param(unique, id); } }
/** * In order to support the order() method in addition * to a filter, we allow applying the filter and nulling * the instance eagerly before the user calls order. */ public QueryBuilder filterNow() { if (filter != null && filterTarget != null) { if (filter.owner() >= 0) { this.and(filterTarget+".details.owner.id = "); String alias = this.unique_alias("owner"); this.append(":"); this.append(alias); this.param(alias, filter.owner()); appendSpace(); } if (filter.group() >= 0) { this.and(filterTarget+".details.group.id = "); String alias = this.unique_alias("group"); this.append(":"); this.append(alias); this.param(alias, filter.group()); appendSpace(); } } return this; }