/** * Construct an IntegerProperty from a literal value and with the given alias, e.g. "10 AS ten" * * @param literal the literal value * @param selectAs the alias to use. May be null. */ public static IntegerProperty literal(int literal, String selectAs) { return new IntegerProperty(null, String.valueOf(literal), selectAs, null); }
/** * Count the number of rows matching a given {@link Criterion}. Use null to count all rows. * * @param modelClass the model class corresponding to the table * @param criterion the criterion to match * @return the number of rows matching the given criterion */ public int count(Class<? extends AbstractModel> modelClass, Criterion criterion) { Property.IntegerProperty countProperty = Property.IntegerProperty.countProperty(); Query query = Query.select(countProperty); if (criterion != null) { query.where(criterion); } query = inferTableForQuery(modelClass, query); CompiledStatement compiled = query.compile(getCompileContext()); acquireNonExclusiveLock(); try { return (int) getDatabase().simpleQueryForLong(compiled.sql, compiled.sqlArgs); } finally { releaseNonExclusiveLock(); } }
/** * Construct an IntegerProperty represented by the expression "COUNT(1)" */ public static IntegerProperty countProperty() { return fromFunction(Function.count(), "count"); }
/** * Construct an IntegerProperty from a {@link Function} and with the given alias, e.g. "LENGTH(column) AS len" * * @param function the function * @param selectAs the alias to use. May be null. */ public static IntegerProperty fromFunction(Function<Integer> function, String selectAs) { return new IntegerProperty(function, selectAs); }
/** * Construct an IntegerProperty represented by the expression "COUNT(field)" or "COUNT(DISTINCT field)" * * @param field the field to count * @param distinct whether to count distinct values only or not */ public static IntegerProperty countProperty(Field<?> field, boolean distinct) { Function<Integer> function = distinct ? Function.countDistinct(field) : Function.count(field); return fromFunction(function, "count"); }
/** * Construct an IntegerProperty from a literal value and with the given alias, e.g. "10 AS ten" * * @param literal the literal value * @param selectAs the alias to use. May be null. */ public static IntegerProperty literal(int literal, String selectAs) { return new IntegerProperty(null, String.valueOf(literal), selectAs, null); }
.when(oldLuckyNumber.neq(newLuckyNumber)) .perform(Insert.into(TriggerTester.TABLE).columns(TriggerTester.VALUE_1, TriggerTester.VALUE_2) .values(oldLuckyNumber, newLuckyNumber)); 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());
assertExpectedValues(Query.select(test), test, PASS); caseWhen = Function.caseWhen(IntegerProperty.literal(1, null).gt(2), FAIL) .when(IntegerProperty.literal(3, null).gt(0), PASS) .elseExpr(FAIL) .end(); caseWhen = Function.caseWhen(TestModel.LUCKY_NUMBER.gt(0), "positive") .when(TestModel.LUCKY_NUMBER.lt(0), "negative") .elseExpr("zero") .end(); IntegerProperty hasLongName = IntegerProperty.fromFunction(longNameFunc, "hasLongName"); assertExpectedValues(Query.select(hasLongName).from(TestModel.TABLE).orderBy(TestModel.ID.asc()), hasLongName, 0, 0, 1);
values.put(TestModel.IS_HAPPY.getName(), 1); // Putting an int where boolean expected values.put(TestModel.SOME_DOUBLE.getName(), 1); // Putting an int where double expected values.put(TestModel.$_123_ABC.getName(), "1"); // Putting a String where int expected assertTrue(checkTypesOn.get(TestModel.IS_HAPPY.getName()) instanceof Boolean); assertTrue(checkTypesOn.get(TestModel.SOME_DOUBLE.getName()) instanceof Double); assertTrue(checkTypesOn.get(TestModel.$_123_ABC.getName()) instanceof Integer);
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());
public void testProjectionMapAliasing() { assertEquals("blahTestName", TestSubqueryModel.TEST_NAME.getName()); assertEquals("blahName", TestSubqueryModel.EMPLOYEE_NAME.getName()); assertEquals("luckyNumber", TestSubqueryModel.TEST_LUCKY_NUMBER.getName()); assertEquals("blahEnum", TestSubqueryModel.TEST_ENUM.getName()); assertEquals("uppercase_name", TestSubqueryModel.UPPERCASE_NAME.getName()); SquidCursor<TestSubqueryModel> cursor = null; try { cursor = database.query(TestSubqueryModel.class, Query.select().from(TestSubqueryModel.SUBQUERY)); cursor.moveToFirst(); TestSubqueryModel model = new TestSubqueryModel(cursor); // queried model should have "uppercase_name" assertEquals(e1.getName().toUpperCase(), model.getUppercaseName()); TestModel testModel = model.mapToModel(new TestModel()); Employee employeeModel = model.mapToModel(new Employee()); assertEquals(t1.getFirstName(), testModel.getFirstName()); assertEquals(e1.getName(), employeeModel.getName()); assertEquals(t1.getLuckyNumber(), testModel.getLuckyNumber()); assertEquals(t1.getSomeEnum(), testModel.getSomeEnum()); // neither mapped model should have "uppercase_name" assertFalse(t1.containsValue(TestSubqueryModel.UPPERCASE_NAME)); assertFalse(e1.containsValue(TestSubqueryModel.UPPERCASE_NAME)); } finally { if (cursor != null) { cursor.close(); } } }
public void testTypesafeGetter() { StringProperty literalString = StringProperty.literal("literalString", "testStr"); IntegerProperty literalInteger = IntegerProperty.literal(2, "testInt"); BooleanProperty literalBoolean = BooleanProperty.literal(true, "testBool"); // Test casting Integer to Boolean BooleanProperty castBool = BooleanProperty.literal(false, literalInteger.getName()); // Test casting Boolean to Integer IntegerProperty castInt = IntegerProperty.literal(0, literalBoolean.getName()); // Test casting Integer to String StringProperty castString = StringProperty.literal("", literalInteger.getName()); Query query = Query.select(literalString, literalInteger, literalBoolean); SquidCursor<?> cursor = database.query(null, query); try { assertTrue(cursor.moveToFirst()); assertEquals("literalString", cursor.get(literalString)); assertEquals(2, cursor.get(literalInteger).intValue()); assertTrue(cursor.get(literalInteger) instanceof Integer); assertTrue(cursor.get(literalBoolean)); assertTrue(cursor.get(castBool)); assertEquals(1, cursor.get(castInt).intValue()); assertEquals("2", cursor.get(castString)); } finally { cursor.close(); } }
public void testMath() { Function<Integer> addition = Function.add(1, 2, 3, 4, 5); IntegerProperty sum = IntegerProperty.fromFunction(addition, "sum"); testMath(sum, 15); Function<Integer> subtraction = Function.subtract(100, 30, 20); IntegerProperty difference = IntegerProperty.fromFunction(subtraction, "difference"); testMath(difference, 50); Function<Integer> multiplcation = Function.multiply(1, 2, 3, 4, 5); IntegerProperty product = IntegerProperty.fromFunction(multiplcation, "product"); testMath(product, 120); Function<Integer> division = Function.divide(1000, 10, 5); IntegerProperty quotient = IntegerProperty.fromFunction(division, "quotient"); testMath(quotient, 20); Function<Integer> modulus = Function.modulo(512, 9); IntegerProperty remainder = IntegerProperty.fromFunction(modulus, "remainder"); testMath(remainder, 8); Function<Integer> bitAnd = Function.bitwiseAnd(0xcafe0000, 0xba00, 0xbe); IntegerProperty and = IntegerProperty.fromFunction(bitAnd, "bitAnd"); testMath(and, 0); Function<Integer> bitOr = Function.bitwiseOr(0xcafe0000, 0xba00, 0xbe); IntegerProperty or = IntegerProperty.fromFunction(bitOr, "bitOr"); testMath(or, 0xcafebabe); }
public void testEqualsAndHashCode() { LongProperty test1 = new LongProperty( new TableModelName(TestModel.class, TestModel.TABLE.getName()), "testCol"); LongProperty test2 = new LongProperty( new TableModelName(TestModel.class, TestModel.TABLE.getName()), "testCol"); assertEquals(test1, test2); assertEquals(test1.hashCode(), test2.hashCode()); StringProperty test3 = new StringProperty( new TableModelName(TestModel.class, TestModel.TABLE.getName()), "testCol"); StringProperty test4 = new StringProperty( new TableModelName(TestModel.class, TestModel.TABLE.getName()), "testCol", "DEFAULT 'A'"); assertEquals(test3, test4); assertEquals(test3.hashCode(), test4.hashCode()); Function<Integer> func1 = Function.count(); Function<Integer> func2 = Function.rawFunction("COUNT(*)"); assertEquals(func1, func2); assertEquals(func1.hashCode(), func2.hashCode()); IntegerProperty test5 = Property.IntegerProperty.fromFunction(func1, "count"); IntegerProperty test6 = Property.IntegerProperty.fromFunction(func2, "count"); assertEquals(test5, test6); assertEquals(test5.hashCode(), test6.hashCode()); }
public void testDefaultOrderBy() { ContentProviderQueryBuilder builder = getBuilder(); builder.setDefaultOrder(TestModel.LUCKY_NUMBER.desc()); Query query = builder.setDataSource(TestModel.TABLE).build(null, null, null, null); CompiledStatement compiled = query.compile(database.getCompileContext()); verifyCompiledSqlArgs(compiled, 0); SquidCursor<TestModel> cursor = null; try { cursor = database.query(TestModel.class, query); assertEquals(3, cursor.getCount()); cursor.moveToFirst(); assertEquals(model2, buildModelFromCursor(cursor)); cursor.moveToNext(); assertEquals(model1, buildModelFromCursor(cursor)); cursor.moveToNext(); assertEquals(model3, buildModelFromCursor(cursor)); } finally { if (cursor != null) { cursor.close(); } } }
public void testLimitAndOffsetWithExpressions() { // limit = 1 + (count(*) / 4), offset = count(*) / 2 Field<Integer> limit = Function.add(1, Function.divide( Query.select(IntegerProperty.countProperty()).from(Employee.TABLE).asFunction(), 4)); Field<Integer> offset = Function.divide( Query.select(IntegerProperty.countProperty()).from(Employee.TABLE).asFunction(), 2); Query query = Query.select().orderBy(Employee.NAME.asc()).limit(limit, offset); SquidCursor<Employee> cursor = database.query(Employee.class, query); try { assertEquals(2, cursor.getCount()); cursor.moveToFirst(); assertEquals(elmo, new Employee(cursor)); cursor.moveToNext(); assertEquals(ernie, new Employee(cursor)); } finally { cursor.close(); } }
public void testSumAndSumDistinct() { setUpAggregateTest(); IntegerProperty sum = IntegerProperty.fromFunction( Function.sum(TestModel.LUCKY_NUMBER), "sum"); IntegerProperty sumDistinct = IntegerProperty.fromFunction( Function.sumDistinct(TestModel.LUCKY_NUMBER), "sumDistinct"); SquidCursor<TestModel> cursor = database.query(TestModel.class, Query.select(sum, sumDistinct)); try { assertEquals(1, cursor.getCount()); cursor.moveToFirst(); assertEquals(12, cursor.get(sum).intValue()); assertEquals(8, cursor.get(sumDistinct).intValue()); } finally { cursor.close(); } }
public void testUpdateWithTemplate() { Criterion criterion = TestModel.LUCKY_NUMBER.lte(0); // check preconditions int rowsBeforeWithLuckyNumberLteZero = database.count(TestModel.class, criterion); assertTrue(rowsBeforeWithLuckyNumberLteZero > 0); // update testModels set luckyNumber = 777 where luckyNumber <= 0; TestModel template = new TestModel().setLuckyNumber(777); Update update = Update.table(TestModel.TABLE).fromTemplate(template).where(criterion); CompiledStatement compiled = update.compile(database.getCompileContext()); verifyCompiledSqlArgs(compiled, 2, template.getLuckyNumber(), 0); assertEquals(rowsBeforeWithLuckyNumberLteZero, database.update(update)); int rowsAfterWithLuckyNumberLteZero = database.count(TestModel.class, criterion); int rowsWithNewLuckyNumber = database.count(TestModel.class, TestModel.LUCKY_NUMBER.eq(777)); assertEquals(0, rowsAfterWithLuckyNumberLteZero); assertTrue(rowsWithNewLuckyNumber >= rowsBeforeWithLuckyNumberLteZero); }
public void testInstanceVsStaticConjunction() { Criterion c1 = TestModel.FIRST_NAME.eq("Sam"); Criterion c2 = TestModel.LAST_NAME.eq("Bosley"); Criterion c3 = TestModel.LUCKY_NUMBER.eq(7); Criterion c = c1.and(c2); Criterion cand = c.and(c3); assertNotSame(c, cand); // Tests immutability of conjunction criterions when appending with the same operator assertEquals(Criterion.and(c1, c2, c3), c1.and(c2).and(c3)); assertEquals(Criterion.and(Arrays.asList(c1, c2, c3)), c1.and(c2).and(c3)); assertEquals(Criterion.or(c1, c2, c3), c1.or(c2).or(c3)); assertEquals(Criterion.or(Arrays.asList(c1, c2, c3)), c1.or(c2).or(c3)); assertEquals(Criterion.or(Criterion.and(c1, c2), c3), c1.and(c2).or(c3)); assertEquals(Criterion.and(Criterion.or(c1, c2), c3), c1.or(c2).and(c3)); assertEquals(Criterion.and(c1, Criterion.or(c2, c3)), c1.and(c2.or(c3))); assertEquals(Criterion.or(c1, Criterion.and(c2, c3)), c1.or(c2.and(c3))); }