private void readFieldIntoModel(SquidCursor<?> cursor, com.yahoo.squidb.sql.Field<?> field) { try { if (field instanceof Property<?>) { Property<?> property = (Property<?>) field; saver.save(property, values, cursor.get(property)); } } catch (IllegalArgumentException e) { // underlying cursor may have changed, suppress } }
private void scanCursor(SquidCursor<Thing> cursor, String formatString) { for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) { int i = cursor.getPosition(); assertEquals(String.format(formatString, i), cursor.get(Thing.FOO)); } }
private void testReusableQueryWithInCriterionInternal(Collection<String> collection, Query query, String... list) { collection.clear(); collection.addAll(Arrays.asList(list)); SquidCursor<Employee> cursor = database.query(Employee.class, query); try { assertEquals(collection.size(), cursor.getCount()); while (cursor.moveToNext()) { String name = cursor.get(Employee.NAME); assertTrue(collection.contains(name)); } } finally { cursor.close(); } }
private void testInQuery(List<String> expectedNames, Query query) { SquidCursor<Employee> cursor = database.query(Employee.class, query); try { assertEquals(expectedNames.size(), cursor.getCount()); for (String name : expectedNames) { cursor.moveToNext(); assertEquals(name, cursor.get(Employee.NAME)); } } finally { cursor.close(); } }
@Override public long getItemId(int position) { if (hasStableIds()) { if (cursor != null && cursor.moveToPosition(position)) { return cursor.get(columnForId); } } return 0; }
private void testReusableQueryInternal(AtomicReference<String> ref, String name, Query query) { ref.set(name); SquidCursor<Employee> cursor = database.query(Employee.class, query); try { cursor.moveToFirst(); assertEquals(1, cursor.getCount()); assertEquals(name, cursor.get(Employee.NAME)); } finally { cursor.close(); } }
@Override public long getItemId(int position) { if (hasStableIds()) { if (cursor != null && cursor.moveToPosition(position)) { return cursor.get(idProperty); } return RecyclerView.NO_ID; } return super.getItemId(position); }
private void assertExpectedValues(Query query, Property<?> property, Object... expectedValues) { final int expectedCount = expectedValues == null ? 0 : expectedValues.length; SquidCursor<?> cursor = database.query(null, query); try { assertEquals(expectedCount, cursor.getCount()); for (int i = 0; i < expectedCount; i++) { cursor.moveToPosition(i); assertEquals(expectedValues[i], cursor.get(property)); } } finally { cursor.close(); } }
public void testBooleanFunctionPropertyConstants() { BooleanProperty alwaysTrue = BooleanProperty.fromFunction(Function.TRUE, "alwaysTrue"); BooleanProperty alwaysFalse = BooleanProperty.fromFunction(Function.FALSE, "alwaysFalse"); SquidCursor<TestModel> cursor = database.query(TestModel.class, Query.select(alwaysTrue, alwaysFalse)); try { cursor.moveToFirst(); assertTrue(cursor.get(alwaysTrue)); assertFalse(cursor.get(alwaysFalse)); } finally { cursor.close(); } }
public void testSelectLiteral() { StringProperty literal = StringProperty.literal("literal", "name"); LongProperty literalLong = LongProperty.literal(12, "twelve"); SquidCursor<?> c = database.query(null, Query.select(literal, literalLong)); try { assertEquals(1, c.getCount()); c.moveToFirst(); assertEquals("literal", c.get(literal)); assertEquals(12, c.get(literalLong).longValue()); } finally { if (c != null) { c.close(); } } }
private <T extends Number> void testMath(Property<T> property, T expectedValue) { SquidCursor<?> cursor = database.query(null, Query.select(property)); try { cursor.moveToFirst(); T value = cursor.get(property); assertEquals(expectedValue, value); } 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 testMin() { LongProperty minId = LongProperty.fromFunction(Function.min(TestModel.ID), "minId"); SquidCursor<TestModel> cursor = database.query(TestModel.class, Query.select(minId)); try { cursor.moveToFirst(); assertEquals(model1.getRowId(), cursor.get(minId).longValue()); } finally { cursor.close(); } }
private void testBetween(List<Long> expectedIds, boolean useNotBetween) { SquidCursor<Employee> cursor = database.query(Employee.class, Query.select(Employee.ID).where(useNotBetween ? Employee.ID.notBetween(2, 5) : Employee.ID.between(2, 5)).orderBy(Employee.ID.asc())); try { assertEquals(expectedIds.size(), cursor.getCount()); for (Long id : expectedIds) { cursor.moveToNext(); assertEquals(id.longValue(), cursor.get(Employee.ID).longValue()); } } finally { cursor.close(); } }
public void testMax() { LongProperty maxId = LongProperty.fromFunction(Function.max(TestModel.ID), "maxId"); SquidCursor<TestModel> cursor = database.query(TestModel.class, Query.select(maxId)); try { cursor.moveToFirst(); assertEquals(model3.getRowId(), cursor.get(maxId).longValue()); } finally { cursor.close(); } }
private void testGlob(List<Employee> expected, boolean useNotGlob) { SquidCursor<Employee> cursor = database.query(Employee.class, Query.select(Employee.ID, Employee.NAME).where(useNotGlob ? Employee.NAME.notGlob("b*") : Employee.NAME.glob("b*")) .orderBy(Employee.ID.asc())); try { assertEquals(expected.size(), cursor.getCount()); for (Employee e : expected) { cursor.moveToNext(); assertEquals(e.getRowId(), cursor.get(Employee.ID).longValue()); assertEquals(e.getName(), cursor.get(Employee.NAME)); } } finally { cursor.close(); } }
public void testAvgAndAvgDistinct() { setUpAggregateTest(); DoubleProperty avg = DoubleProperty.fromFunction(Function.avg(TestModel.LUCKY_NUMBER), "avg"); DoubleProperty avgDistinct = DoubleProperty.fromFunction( Function.avgDistinct(TestModel.LUCKY_NUMBER), "avgDistinct"); SquidCursor<TestModel> cursor = database.query(TestModel.class, Query.select(avg, avgDistinct)); try { cursor.moveToFirst(); assertEquals(2.0, cursor.get(avg)); assertEquals(4.0, cursor.get(avgDistinct)); } finally { cursor.close(); } }
public void testBoundArgumentsWorkInHavingClause() { Query query = Query.select(Employee.PROPERTIES) .groupBy(Employee.MANAGER_ID) .having(Function.count(Employee.MANAGER_ID).gt(2)); SquidCursor<Employee> cursor = database.query(Employee.class, query); try { assertEquals(1, cursor.getCount()); cursor.moveToFirst(); assertEquals(bigBird.getRowId(), cursor.get(Employee.MANAGER_ID).longValue()); } finally { cursor.close(); } }
public void testBooleanFunctionOnCriterion() { BooleanProperty onCriterion = BooleanProperty .fromFunction(Function.caseWhen(TestModel.FIRST_NAME.eq("Sam")), "firstNameSam"); SquidCursor<TestModel> cursor = database .query(TestModel.class, Query.select(onCriterion).orderBy(Order.asc(TestModel.ID))); try { cursor.moveToFirst(); assertTrue(cursor.get(onCriterion)); cursor.moveToNext(); assertFalse(cursor.get(onCriterion)); } finally { cursor.close(); } }
public void testOrderByArray() { Long[] order = new Long[]{5L, 1L, 4L}; SquidCursor<Employee> cursor = database.query(Employee.class, Query.select(Employee.ID).limit(order.length).orderBy(Employee.ID.byArray(order))); try { assertEquals(order.length, cursor.getCount()); for (int i = 0; i < order.length; i++) { cursor.moveToPosition(i); assertEquals(order[i], cursor.get(Employee.ID)); } } finally { cursor.close(); } }