/** * Execute a statement that returns a 1x1 long result. If you know your result set will only have one row and * column, this is much more efficient than calling {@link #rawQuery(String, Object[])} and parsing the cursor. * <br> * Note: This will throw an exception if the given SQL query returns a result that is not a single column * * @param sql a sql statement * @param sqlArgs arguments to bind to the sql statement * @return the long result of the query */ public long simpleQueryForLong(String sql, Object[] sqlArgs) { acquireNonExclusiveLock(); try { return getDatabase().simpleQueryForLong(sql, sqlArgs); } finally { releaseNonExclusiveLock(); } }
/** * 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(); } }
/** * Execute a statement that returns a 1x1 long result. If you know your result set will only have one row and * column, this is much more efficient than calling {@link #rawQuery(String, Object[])} and parsing the cursor. * <br> * Note: This will throw an exception if the given SQL query returns a result that is not a single column * * @param sql a sql statement * @param sqlArgs arguments to bind to the sql statement * @return the long result of the query */ public long simpleQueryForLong(String sql, Object[] sqlArgs) { acquireNonExclusiveLock(); try { return getDatabase().simpleQueryForLong(sql, sqlArgs); } finally { releaseNonExclusiveLock(); } }
/** * 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(); } }