private VirtualTable(Class<? extends TableModel> modelClass, Property<?>[] properties, String name, String databaseName, String module, String alias) { super(modelClass, properties, name, databaseName); this.moduleName = module; this.alias = alias; anyColumn = Field.field(expression); }
/** * Clone the given {@link Field} with this object's name as its qualifier. This is useful for selecting * from views, subqueries, or aliased tables. * * @param field the field to clone * @return a clone of the given field with this object as its qualifier */ @SuppressWarnings("unchecked") public <F extends Field<?>> F qualifyField(F field) { if (field instanceof Property<?>) { return (F) ((Property<?>) field).asSelectionFromTable(this, null); } else { return (F) Field.field(field.getName(), getName()); } }
/** * Construct a Field with the given expression * * @param expression the string-literal representation of the returned field */ public static <T> Field<T> field(String expression) { return new Field<>(expression); }
private void visitLimitClause(SqlBuilder builder, boolean forSqlValidation) { if (!NO_LIMIT.equals(limit) || !NO_OFFSET.equals(offset)) { builder.sql.append(" LIMIT "); limit.appendQualifiedExpression(builder, forSqlValidation); if (!NO_OFFSET.equals(offset)) { builder.sql.append(" OFFSET "); offset.appendQualifiedExpression(builder, forSqlValidation); } } }
/** * Add a {@link Field} to the map. If the name of the Field being added does not match the name, it will be aliased * to that name before being added to the map. * * @param name the key to use * @param column the Field to add * @return the value of any previous mapping with the specified key, or null if there was no mapping */ public Field<?> put(String name, Field<?> column) { if (column == null) { throw new IllegalArgumentException("Cannot use null column in ProjectionMap"); } if (SqlUtils.isEmpty(name)) { throw new IllegalArgumentException("Cannot use empty string as a key"); } if (!SqlUtils.equals(name, column.getName())) { column = column.as(name); } return map.put(name, column); }
/** * Add a {@link Field} to the map using its current name as the key * * @param column the Field to add * @return the value of any previous mapping with the specified key, or null if there was no mapping */ public Field<?> put(Field<?> column) { if (column == null) { throw new IllegalArgumentException("Cannot use null column in ProjectionMap"); } return map.put(column.getName(), column); }
public void testInCriterion() { List<String> expectedNames = Arrays.asList("bigBird", "cookieMonster"); Query query = Query.select().where(Employee.NAME.in("bigBird", "cookieMonster")).orderBy(Employee.NAME.asc()); testInQuery(expectedNames, query); query = Query.select().where(Employee.NAME.notIn("bigBird", "cookieMonster")).orderBy(Employee.NAME.asc()); testInQuery(Arrays.asList("bert", "elmo", "ernie", "oscar"), query); List<String> list = Arrays.asList("bigBird", "cookieMonster"); query = Query.select().where(Employee.NAME.in(list)).orderBy(Employee.NAME.asc()); testInQuery(expectedNames, query); // Test off-by-one error that used to occur when the in criterion wasn't the last criterion in the list query = Query.select().where(Employee.NAME.in(list).or(Field.field("1").neq(1))).orderBy(Employee.NAME.asc()); testInQuery(expectedNames, query); }
protected void beforePopulateOperator(SqlBuilder builder, boolean forSqlValidation) { field.appendQualifiedExpression(builder, forSqlValidation); }
/** * Set the limit of this statement as a SQL expression; e.g. a {@link Function} or the result of * {@link #asFunction()} to use a subquery. Use {@link #NO_LIMIT} for limit to remove the limit. Use * {@link #NO_OFFSET} for offset to remove the offset. * * @param limit the maximum number of rows this query should return * @param offset the number of rows this query should skip * @return this Query object, to allow chaining method calls */ public Query limit(Field<Integer> limit, Field<Integer> offset) { if (limit == null) { limit = NO_LIMIT; } if (offset == null) { offset = NO_OFFSET; } if (immutable) { return fork().limit(limit, offset); } if (!this.limit.equals(limit) || !this.offset.equals(offset)) { this.limit = limit; this.offset = offset; invalidateCompileCache(); } return this; }
@Override public String getExpression() { if (function != null) { throw new UnsupportedOperationException("Can't call getExpression() on a Property that wraps a Function"); } return super.getExpression(); }
@Override protected String expressionForComparison() { if (function != null) { return function.expressionForComparison(); } return super.expressionForComparison(); }
/** * Clone this property with the given alias. For example, when called on a property represented by * "table.property as property", returns a property represented by "table.property as newAlias". */ @Override public Property<TYPE> as(String newAlias) { return (Property<TYPE>) super.as(newAlias); }
/** * @return a {@link Criterion} that the field must be null */ public Criterion isNull() { return is(null); }
/** * Add a {@link Field} to the map. If the name of the Field being added does not match the name, it will be aliased * to that name before being added to the map. * * @param name the key to use * @param column the Field to add * @return the value of any previous mapping with the specified key, or null if there was no mapping */ public Field<?> put(String name, Field<?> column) { if (column == null) { throw new IllegalArgumentException("Cannot use null column in ProjectionMap"); } if (SqlUtils.isEmpty(name)) { throw new IllegalArgumentException("Cannot use empty string as a key"); } if (!SqlUtils.equals(name, column.getName())) { column = column.as(name); } return map.put(name, column); }
public void testInvalidProjectionIgnored() { ContentProviderQueryBuilder builder = getBuilder(); final String IGNORE = "foo"; String[] projection = {IGNORE, COL_GIVEN_NAME, COL_SURNAME, COL_LUCKY_NUMBER}; Query query = builder.setDataSource(TestModel.TABLE).build(projection, null, null, null); List<Field<?>> fields = query.getFields(); assertEquals(3, fields.size()); for (int i = 0; i < fields.size(); i++) { if (IGNORE.equals(fields.get(i).getName())) { fail("Invalid projection not ignored!"); } } }
private void visitLimitClause(SqlBuilder builder, boolean forSqlValidation) { if (!NO_LIMIT.equals(limit) || !NO_OFFSET.equals(offset)) { builder.sql.append(" LIMIT "); limit.appendQualifiedExpression(builder, forSqlValidation); if (!NO_OFFSET.equals(offset)) { builder.sql.append(" OFFSET "); offset.appendQualifiedExpression(builder, forSqlValidation); } } }
@Override protected void appendQualifiedExpression(SqlBuilder builder, boolean forSqlValidation) { if (function != null) { function.appendToSqlBuilder(builder, forSqlValidation); } else { super.appendQualifiedExpression(builder, forSqlValidation); } }
/** * @return a {@link Criterion} that the field's value is in the result of the {@link Query}. If the query is null, * this is equivalent to 'IN ()', which will always be false. */ public Criterion in(Query query) { if (query == null) { return in((Collection<?>) null); } return new BinaryCriterion(this, Operator.in, query); }
/** * Set the limit of this statement as a SQL expression; e.g. a {@link Function} or the result of * {@link #asFunction()} to use a subquery. Use {@link #NO_LIMIT} to remove the limit. * * @param limit the maximum number of rows this query should return * @return this Query object, to allow chaining method calls */ public Query limit(Field<Integer> limit) { if (limit == null) { limit = NO_LIMIT; } if (immutable) { return fork().limit(limit); } if (!this.limit.equals(limit)) { this.limit = limit; invalidateCompileCache(); } return this; }