public T loadByRowId(long rowId) { String[] idArray = new String[]{Long.toString(rowId)}; Cursor cursor = db.rawQuery(statements.getSelectByRowId(), idArray); return loadUniqueAndCloseCursor(cursor); }
/** Loads all available entities from the database. */ public List<T> loadAll() { Cursor cursor = db.rawQuery(statements.getSelectAll(), null); return loadAllAndCloseCursor(cursor); }
/** A raw-style query where you can pass any WHERE clause and arguments. */ public List<RelationEntity> queryDeep(String where, String... selectionArg) { Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg); return loadDeepAllAndCloseCursor(cursor); }
/** A raw-style query where you can pass any WHERE clause and arguments. */ public List<T> queryRaw(String where, String... selectionArg) { Cursor cursor = db.rawQuery(statements.getSelectAll() + where, selectionArg); return loadAllAndCloseCursor(cursor); }
/** A raw-style query where you can pass any WHERE clause and arguments. */ public List<RelationSource2> queryDeep(String where, String... selectionArg) { Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg); return loadDeepAllAndCloseCursor(cursor); }
/** A raw-style query where you can pass any WHERE clause and arguments. */ public List<TreeEntity> queryDeep(String where, String... selectionArg) { Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg); return loadDeepAllAndCloseCursor(cursor); }
/** Executes the query and returns a raw android.database.Cursor. Don't forget to close it. */ public Cursor query() { checkThread(); return dao.getDatabase().rawQuery(sql, parameters); }
public static String queryString(Database db, String sql) { Cursor cursor = db.rawQuery(sql, null); try { assertTrue(cursor.moveToNext()); return cursor.getString(0); } finally { cursor.close(); } }
/** Executes the query and returns the result as a list containing all entities loaded into memory. */ public List<T> list() { checkThread(); Cursor cursor = dao.getDatabase().rawQuery(sql, parameters); return daoAccess.loadAllAndCloseCursor(cursor); }
public RelationEntity loadDeep(Long key) { assertSinglePk(); if (key == null) { return null; } StringBuilder builder = new StringBuilder(getSelectDeep()); builder.append("WHERE "); SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns()); String sql = builder.toString(); String[] keyArray = new String[] { key.toString() }; Cursor cursor = db.rawQuery(sql, keyArray); try { boolean available = cursor.moveToFirst(); if (!available) { return null; } else if (!cursor.isLast()) { throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount()); } return loadCurrentDeep(cursor, true); } finally { cursor.close(); } }
public RelationSource2 loadDeep(Long key) { assertSinglePk(); if (key == null) { return null; } StringBuilder builder = new StringBuilder(getSelectDeep()); builder.append("WHERE "); SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns()); String sql = builder.toString(); String[] keyArray = new String[] { key.toString() }; Cursor cursor = db.rawQuery(sql, keyArray); try { boolean available = cursor.moveToFirst(); if (!available) { return null; } else if (!cursor.isLast()) { throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount()); } return loadCurrentDeep(cursor, true); } finally { cursor.close(); } }
/** * Executes the query and returns the result as a list that lazy loads the entities on first access. Entities are * cached, so accessing the same entity more than once will not result in loading an entity from the underlying * cursor again.Make sure to close it to close the underlying cursor. */ public LazyList<T> listLazy() { checkThread(); Cursor cursor = dao.getDatabase().rawQuery(sql, parameters); return new LazyList<T>(daoAccess, cursor, true); }
public TreeEntity loadDeep(Long key) { assertSinglePk(); if (key == null) { return null; } StringBuilder builder = new StringBuilder(getSelectDeep()); builder.append("WHERE "); SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns()); String sql = builder.toString(); String[] keyArray = new String[] { key.toString() }; Cursor cursor = db.rawQuery(sql, keyArray); try { boolean available = cursor.moveToFirst(); if (!available) { return null; } else if (!cursor.isLast()) { throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount()); } return loadCurrentDeep(cursor, true); } finally { cursor.close(); } }
/** * Executes the query and returns the result as a list that lazy loads the entities on every access (uncached). * Make sure to close the list to close the underlying cursor. */ public LazyList<T> listLazyUncached() { checkThread(); Cursor cursor = dao.getDatabase().rawQuery(sql, parameters); return new LazyList<T>(daoAccess, cursor, false); }
/** * Executes the query and returns the unique result or null. * * @return Entity or null if no matching entity was found * @throws DaoException if the result is not unique */ public T unique() { checkThread(); Cursor cursor = dao.getDatabase().rawQuery(sql, parameters); return daoAccess.loadUniqueAndCloseCursor(cursor); }
protected Cursor queryWithDummyColumnsInFront(int dummyCount, String valueForColumn, K pk) { StringBuilder builder = new StringBuilder("SELECT "); for (int i = 0; i < dummyCount; i++) { builder.append(valueForColumn).append(","); } SqlUtils.appendColumns(builder, "T", dao.getAllColumns()).append(" FROM "); builder.append('"').append(dao.getTablename()).append('"').append(" T"); if (pk != null) { builder.append(" WHERE "); assertEquals(1, dao.getPkColumns().length); builder.append(dao.getPkColumns()[0]).append("="); DatabaseUtils.appendValueToSql(builder, pk); } String select = builder.toString(); Cursor cursor = db.rawQuery(select, null); assertTrue(cursor.moveToFirst()); try { for (int i = 0; i < dummyCount; i++) { assertEquals(valueForColumn, cursor.getString(i)); } if (pk != null) { assertEquals(1, cursor.getCount()); } } catch (RuntimeException ex) { cursor.close(); throw ex; } return cursor; }
/** Returns the count (number of results matching the query). Uses SELECT COUNT (*) sematics. */ public long count() { checkThread(); Cursor cursor = dao.getDatabase().rawQuery(sql, parameters); try { if (!cursor.moveToNext()) { throw new DaoException("No result for count"); } else if (!cursor.isLast()) { throw new DaoException("Unexpected row count: " + cursor.getCount()); } else if (cursor.getColumnCount() != 1) { throw new DaoException("Unexpected column count: " + cursor.getColumnCount()); } return cursor.getLong(0); } finally { cursor.close(); } }
/** * Loads the entity for the given PK. * * @param key a PK value or null * @return The entity or null, if no entity matched the PK value */ public T load(K key) { assertSinglePk(); if (key == null) { return null; } if (identityScope != null) { T entity = identityScope.get(key); if (entity != null) { return entity; } } String sql = statements.getSelectByKey(); String[] keyArray = new String[]{key.toString()}; Cursor cursor = db.rawQuery(sql, keyArray); return loadUniqueAndCloseCursor(cursor); }
/** Resets all locally changed properties of the entity by reloading the values from the database. */ public void refresh(T entity) { assertSinglePk(); K key = getKeyVerified(entity); String sql = statements.getSelectByKey(); String[] keyArray = new String[]{key.toString()}; Cursor cursor = db.rawQuery(sql, keyArray); try { boolean available = cursor.moveToFirst(); if (!available) { throw new DaoException("Entity does not exist in the database anymore: " + entity.getClass() + " with key " + key); } else if (!cursor.isLast()) { throw new DaoException("Expected unique result, but count was " + cursor.getCount()); } readEntity(cursor, entity, 0); attachEntity(key, entity, true); } finally { cursor.close(); } }
public void testExecuteSqlScript() throws IOException { DbUtils.executeSqlScript(getContext(), db, "minimal-entity.sql"); Cursor cursor = db.rawQuery("SELECT count(*) from MINIMAL_ENTITY", null); try { cursor.moveToFirst(); assertEquals(5, cursor.getInt(0)); } finally { cursor.close(); } }