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); }
/** * Set the limit and offset of this statement. Use a negative value for limit to remove the limit. Use a value less * than one 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(int limit, int offset) { return limit(limit < 0 ? NO_LIMIT : Field.<Integer>field(Integer.toString(limit)), offset < 1 ? NO_OFFSET : Field.<Integer>field(Integer.toString(offset))); }
/** * Set the limit of this statement. Using a negative value removes 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(int limit) { return limit(limit < 0 ? NO_LIMIT : Field.<Integer>field(Integer.toString(limit))); }
/** * Add a {@link Field} represented by the given expression to the map * * @param expression the expression to add * @return the value of any previous mapping with the specified key, or null if there was no mapping */ public Field<?> put(String expression) { if (SqlUtils.isEmpty(expression)) { throw new IllegalArgumentException("Expression cannot be empty"); } return map.put(expression, Field.field(expression)); }
projection.add(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()); } }
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()); }
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); }
public void testDefaultProjection() { final String BIRTHDAY = "birthday"; final String IS_HAPPY = "isHappy"; final List<Field<?>> columns = new ArrayList<>(); columns.add(TestModel.FIRST_NAME); columns.add(TestModel.LAST_NAME); columns.add(TestModel.BIRTHDAY.as(BIRTHDAY)); columns.add(Field.field(IS_HAPPY)); final String[] columnNames = {TestModel.FIRST_NAME.getName(), TestModel.LAST_NAME.getName(), BIRTHDAY, IS_HAPPY}; ProjectionMap map = new ProjectionMap(); map.put(TestModel.FIRST_NAME); map.put(TestModel.LAST_NAME); map.put(BIRTHDAY, TestModel.BIRTHDAY); map.put(IS_HAPPY); // test names the same String[] names = map.getDefaultProjectionNames(); assertTrue(Arrays.deepEquals(columnNames, names)); // test fields the same List<Field<?>> fields = map.getDefaultProjection(); assertTrue(fields.equals(columns)); }
public void testQueryFreeze() { Query base = Query.select().from(Employee.TABLE).limit(1).freeze(); Query fork = base.limit(2); assertFalse(base == fork); assertEquals(Field.field("1"), base.getLimit()); assertEquals(Field.field("2"), fork.getLimit()); assertEquals(base.getTable(), fork.getTable()); }
public void testFork() { Query base = Query.select().from(Employee.TABLE).limit(1); Query fork = base.fork().limit(2); base.limit(3); assertFalse(base == fork); assertEquals(Field.field("3"), base.getLimit()); assertEquals(Field.field("2"), fork.getLimit()); assertEquals(base.getTable(), fork.getTable()); }
public void testCast() { Function<String> castToString = Function.cast(Field.field("x'61'"), "TEXT"); SquidCursor<?> cursor = database.query(null, Query.select(castToString)); try { cursor.moveToFirst(); assertEquals("a", cursor.getString(0)); } finally { cursor.close(); } } }
public void testSubstr() { testSubstrInternal(2, 0); testSubstrInternal(2, 2); testSubstrInternal(3, 4); String literal = "ABC/DEF"; StringProperty prefix = StringProperty.literal(literal.substring(0, literal.indexOf('/') + 1), "prefix"); StringProperty full = StringProperty.literal(literal, "full"); Field<String> fullField = Field.field(full.getName()); Field<String> prefixField = Field.field(prefix.getName()); SquidCursor<?> cursor = database.query(null, Query.select(Function.substr(fullField, Function.add(Function.length(prefixField), 1))) .from(Query.select(full, prefix).as("subquery"))); try { assertTrue(cursor.moveToFirst()); assertEquals("DEF", cursor.getString(0)); } finally { cursor.close(); } }
public void testQueryBindingTypes() { insertBasicTestModel(); Field<Integer> one = Field.field("1"); SquidCursor<TestModel> cursor = database.query(TestModel.class, Query.select().where(Function.abs(one).eq(1))); try { assertEquals(1, cursor.getCount()); } finally { cursor.close(); } }
/** * Set the limit of this statement. Using a negative value removes 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(int limit) { return limit(limit < 0 ? NO_LIMIT : Field.<Integer>field(Integer.toString(limit))); }
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); }
public void testFetchByQueryResetsLimitAndTable() { TestModel model1 = new TestModel().setFirstName("Sam1").setLastName("Bosley1"); TestModel model2 = new TestModel().setFirstName("Sam2").setLastName("Bosley2"); TestModel model3 = new TestModel().setFirstName("Sam3").setLastName("Bosley3"); database.persist(model1); database.persist(model2); database.persist(model3); Query query = Query.select().limit(2, 1); TestModel fetched = database.fetchByQuery(TestModel.class, query); assertEquals(model2.getRowId(), fetched.getRowId()); assertEquals(Field.field("2"), query.getLimit()); assertEquals(Field.field("1"), query.getOffset()); assertEquals(null, query.getTable()); }
public void testFrozenQueryWorksWithDatabase() { Query query = Query.select().limit(2).freeze(); SquidCursor<Employee> cursor = database.query(Employee.class, query); try { assertEquals(2, cursor.getCount()); assertNull(query.getTable()); } finally { cursor.close(); } Employee employee = database.fetchByQuery(Employee.class, query); assertNotNull(employee); assertNull(query.getTable()); assertEquals(Field.field("2"), query.getLimit()); }
Field<Integer> luckyPlusPlus = Field.field("(" + TestModel.LUCKY_NUMBER.getExpression() + " + 1)"); Update update = Update.table(TestModel.TABLE).set(TestModel.LUCKY_NUMBER, luckyPlusPlus); CompiledStatement compiledUpdate = update.compile(database.getCompileContext());
Field<Integer> luckyPlusPlus = Field.field(TestModel.LUCKY_NUMBER.getExpression() + " + 1"); Update update = Update.table(TestModel.TABLE).set(TestModel.LUCKY_NUMBER, luckyPlusPlus); CompiledStatement compiled = update.compile(database.getCompileContext());