protected <TYPE extends AbstractModel> TYPE returnFetchResult(Class<TYPE> modelClass, SquidCursor<TYPE> cursor) { try { if (cursor.getCount() == 0) { return null; } TYPE toReturn = modelClass.newInstance(); toReturn.readPropertiesFromCursor(cursor); return toReturn; } catch (Exception e) { throw new RuntimeException(e); } finally { cursor.close(); } }
/** * Change the SquidCursor backing the adapter. If there is an existing SquidCursor it will be closed. * * @param newCursor the new SquidCursor */ public void changeCursor(SquidCursor<? extends M> newCursor) { SquidCursor<?> oldCursor = swapCursor(newCursor); if (oldCursor != null) { oldCursor.close(); } }
@Override public void onCanceled(SquidCursor<T> data) { if (data != null && !data.isClosed()) { data.close(); } }
@Override public void onCanceled(SquidCursor<T> data) { if (data != null && !data.isClosed()) { data.close(); } }
/** * Change the cursor backing this adapter and close the old cursor if necessary * * @param newCursor the new cursor */ public void changeCursor(SquidCursor<? extends T> newCursor) { SquidCursor<? extends T> oldCursor = swapCursor(newCursor); if (oldCursor != null) { oldCursor.close(); } } }
@Override protected void onReset() { super.onReset(); // Ensure the loader is stopped onStopLoading(); if (cursor != null && !cursor.isClosed()) { cursor.close(); } cursor = null; }
@Override protected void onReset() { super.onReset(); // Ensure the loader is stopped onStopLoading(); if (cursor != null && !cursor.isClosed()) { cursor.close(); } cursor = null; }
private void testMaxSqlArgRowIds(Query query, int expectedSize) { SquidCursor<TestModel> cursor = database.query(TestModel.class, query); try { assertEquals(expectedSize, cursor.getCount()); } finally { if (cursor != null) { 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(); } }
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(); } }
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 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(); } }
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(); } }
public void testSelectFunction() { Function<String> upper = Function.upper(TestModel.LAST_NAME); SquidCursor<TestModel> cursor = database .query(TestModel.class, Query.select(TestModel.PROPERTIES).selectMore(upper)); try { cursor.moveToFirst(); new TestModel(cursor); // Should succeed without throwing an exception } finally { cursor.close(); } }
public void testBindArgsProtectsInjection() { Query q = Query.select().where(Employee.NAME.eq("'Sam'); drop table " + Employee.TABLE.getName() + ";")); SquidCursor<Employee> cursor = database.query(Employee.class, q); try { assertFalse(database.countAll(Employee.class) == 0); } finally { cursor.close(); } }
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 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 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 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(); } }
public void testChangeCursorClosesOldCursor() { TestAdapter adapter = new TestAdapter(new TestModel()); SquidCursor<TestModel> cursor1 = database.query(TestModel.class, Query.select()); adapter.swapCursor(cursor1); SquidCursor<TestModel> cursor2 = database.query(TestModel.class, Query.select().where(TestModel.ID.eq(1))); adapter.changeCursor(cursor2); assertTrue(cursor1.isClosed()); adapter.changeCursor(null); cursor2.close(); }