/** * @return the selected {@link Field}s of the underlying query, qualified by this table's name */ public Field<?>[] qualifiedFields() { return qualifyFields(query.getFields()); }
private void assertValues() { if (!valuesToInsert.isEmpty()) { if (columns.isEmpty()) { throw new IllegalStateException("No columns were specified to insert into."); } assertValueSetSizes(columns.size()); } else if (query != null) { if (columns.size() != query.getFields().size()) { throw new IllegalStateException("Number of properties being selected must match the number of columns " + "specified."); } } else if (!defaultValues) { throw new IllegalStateException("No values to insert were specified."); } }
private void visitSelectClause(SqlBuilder builder, boolean forSqlValidation) { builder.sql.append("SELECT "); if (distinct) { builder.sql.append("DISTINCT "); } List<Field<?>> toSelect; if (isEmpty(fields)) { // SELECT * may yield unexpected column names, so we get the full list of fields to specify explicit aliases toSelect = getFields(); } else { toSelect = fields; } builder.appendConcatenatedCompilables(toSelect, ", ", forSqlValidation); }
/** * Query the database * * @param modelClass the type to parameterize the cursor by. If the query does not contain a FROM clause, the table * or view corresponding to this model class will be used. * @param query the query to execute * @return a {@link SquidCursor} containing the query results */ public <TYPE extends AbstractModel> SquidCursor<TYPE> query(Class<TYPE> modelClass, Query query) { query = inferTableForQuery(modelClass, query); CompiledStatement compiled = query.compile(getCompileContext()); if (compiled.needsValidation) { String validateSql = query.sqlForValidation(getCompileContext()); ensureSqlCompiles(validateSql); // throws if the statement fails to compile } ICursor cursor = rawQuery(compiled.sql, compiled.sqlArgs); return new SquidCursor<>(cursor, modelClass, query.getFields()); }
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!"); } } }
public void testAllFields() { Query query = Query.select().from(TestViewModel.VIEW) .leftJoin(Thing.TABLE, TestViewModel.TEST_MODEL_ID.eq(Thing.ID)); List<Field<?>> fields = query.getFields(); for (Property<?> p : TestViewModel.PROPERTIES) { assertTrue(fields.contains(p)); } for (Property<?> p : Thing.PROPERTIES) { assertTrue(fields.contains(p)); } assertEquals(TestViewModel.PROPERTIES.length + Thing.PROPERTIES.length, fields.size()); }
public void testBuilderFromModel() { ContentProviderQueryBuilder builder = new ContentProviderQueryBuilder(TestSubqueryModel.PROPERTIES, TestSubqueryModel.SUBQUERY); Query query = builder.build(null, null, null, null); assertEquals(Arrays.asList(TestSubqueryModel.PROPERTIES), query.getFields()); assertEquals(TestSubqueryModel.SUBQUERY, query.getTable()); }
public void testEmptyProjectionWithMapUsesDefault() { final Field<?>[] expectedProjection = new Field<?>[]{ TestModel.ID, TestModel.FIRST_NAME.as(COL_GIVEN_NAME), TestModel.LAST_NAME.as(COL_SURNAME), TestModel.LUCKY_NUMBER, TestModel.IS_HAPPY }; ContentProviderQueryBuilder builder = getBuilder(); Query query = builder.setDataSource(TestModel.TABLE).build(null, null, null, null); assertEquals(Arrays.asList(expectedProjection), query.getFields()); }
public void testNonEmptyProjectionWithoutMapCreatesFields() { final Field<?>[] expectedProjection = new Field<?>[]{Field.field("foo"), Field.field("bar")}; ContentProviderQueryBuilder builder = new ContentProviderQueryBuilder(); Query query = builder.setDataSource(TestModel.TABLE).build(new String[]{"foo", "bar"}, null, null, null); assertEquals(Arrays.asList(expectedProjection), query.getFields()); }
/** * @return the selected {@link Field}s of the underlying query, qualified by this table's name */ public Field<?>[] qualifiedFields() { return qualifyFields(query.getFields()); }
private void assertValues() { if (!valuesToInsert.isEmpty()) { if (columns.isEmpty()) { throw new IllegalStateException("No columns were specified to insert into."); } assertValueSetSizes(columns.size()); } else if (query != null) { if (columns.size() != query.getFields().size()) { throw new IllegalStateException("Number of properties being selected must match the number of columns " + "specified."); } } else if (!defaultValues) { throw new IllegalStateException("No values to insert were specified."); } }
private void visitSelectClause(SqlBuilder builder, boolean forSqlValidation) { builder.sql.append("SELECT "); if (distinct) { builder.sql.append("DISTINCT "); } List<Field<?>> toSelect; if (isEmpty(fields)) { // SELECT * may yield unexpected column names, so we get the full list of fields to specify explicit aliases toSelect = getFields(); } else { toSelect = fields; } builder.appendConcatenatedCompilables(toSelect, ", ", forSqlValidation); }
/** * Query the database * * @param modelClass the type to parameterize the cursor by. If the query does not contain a FROM clause, the table * or view corresponding to this model class will be used. * @param query the query to execute * @return a {@link SquidCursor} containing the query results */ public <TYPE extends AbstractModel> SquidCursor<TYPE> query(Class<TYPE> modelClass, Query query) { query = inferTableForQuery(modelClass, query); CompiledStatement compiled = query.compile(getCompileContext()); if (compiled.needsValidation) { String validateSql = query.sqlForValidation(getCompileContext()); ensureSqlCompiles(validateSql); // throws if the statement fails to compile } ICursor cursor = rawQuery(compiled.sql, compiled.sqlArgs); return new SquidCursor<>(cursor, modelClass, query.getFields()); }