Skip to content

Commit e7731f0

Browse files
firefartJean-Baptiste Queru
authored andcommitted
Added overload methods for DatabaseUtils.queryNumEntries
Now you can filter the count statement with a selection and selection args UnitTests for this new methods are added to the cts project Change-Id: Id9233aec0eaac08839041ae7cbaba203470ad3d8
1 parent ec58dff commit e7731f0

File tree

2 files changed

+71
-11
lines changed

2 files changed

+71
-11
lines changed

api/current.xml

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52633,6 +52633,42 @@
5263352633
<parameter name="table" type="java.lang.String">
5263452634
</parameter>
5263552635
</method>
52636+
<method name="queryNumEntries"
52637+
return="long"
52638+
abstract="false"
52639+
native="false"
52640+
synchronized="false"
52641+
static="true"
52642+
final="false"
52643+
deprecated="not deprecated"
52644+
visibility="public"
52645+
>
52646+
<parameter name="db" type="android.database.sqlite.SQLiteDatabase">
52647+
</parameter>
52648+
<parameter name="table" type="java.lang.String">
52649+
</parameter>
52650+
<parameter name="selection" type="java.lang.String">
52651+
</parameter>
52652+
</method>
52653+
<method name="queryNumEntries"
52654+
return="long"
52655+
abstract="false"
52656+
native="false"
52657+
synchronized="false"
52658+
static="true"
52659+
final="false"
52660+
deprecated="not deprecated"
52661+
visibility="public"
52662+
>
52663+
<parameter name="db" type="android.database.sqlite.SQLiteDatabase">
52664+
</parameter>
52665+
<parameter name="table" type="java.lang.String">
52666+
</parameter>
52667+
<parameter name="selection" type="java.lang.String">
52668+
</parameter>
52669+
<parameter name="selectionArgs" type="java.lang.String[]">
52670+
</parameter>
52671+
</method>
5263652672
<method name="readExceptionFromParcel"
5263752673
return="void"
5263852674
abstract="false"
@@ -243064,7 +243100,7 @@
243064243100
<method name="activeCount"
243065243101
return="int"
243066243102
abstract="false"
243067-
native="false"
243103+
native="true"
243068243104
synchronized="false"
243069243105
static="false"
243070243106
final="false"

core/java/android/database/DatabaseUtils.java

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ public class DatabaseUtils {
5050
private static final boolean DEBUG = false;
5151
private static final boolean LOCAL_LOGV = DEBUG ? Config.LOGD : Config.LOGV;
5252

53-
private static final String[] countProjection = new String[]{"count(*)"};
54-
5553
/**
5654
* Special function for writing an exception result at the header of
5755
* a parcel, to be used when returning an exception from a transaction.
@@ -604,14 +602,40 @@ public static void cursorRowToContentValues(Cursor cursor, ContentValues values)
604602
* @return the number of rows in the table
605603
*/
606604
public static long queryNumEntries(SQLiteDatabase db, String table) {
607-
Cursor cursor = db.query(table, countProjection,
608-
null, null, null, null, null);
609-
try {
610-
cursor.moveToFirst();
611-
return cursor.getLong(0);
612-
} finally {
613-
cursor.close();
614-
}
605+
return queryNumEntries(db, table, null, null);
606+
}
607+
608+
/**
609+
* Query the table for the number of rows in the table.
610+
* @param db the database the table is in
611+
* @param table the name of the table to query
612+
* @param selection A filter declaring which rows to return,
613+
* formatted as an SQL WHERE clause (excluding the WHERE itself).
614+
* Passing null will count all rows for the given table
615+
* @return the number of rows in the table filtered by the selection
616+
*/
617+
public static long queryNumEntries(SQLiteDatabase db, String table, String selection) {
618+
return queryNumEntries(db, table, selection, null);
619+
}
620+
621+
/**
622+
* Query the table for the number of rows in the table.
623+
* @param db the database the table is in
624+
* @param table the name of the table to query
625+
* @param selection A filter declaring which rows to return,
626+
* formatted as an SQL WHERE clause (excluding the WHERE itself).
627+
* Passing null will count all rows for the given table
628+
* @param selectionArgs You may include ?s in selection,
629+
* which will be replaced by the values from selectionArgs,
630+
* in order that they appear in the selection.
631+
* The values will be bound as Strings.
632+
* @return the number of rows in the table filtered by the selection
633+
*/
634+
public static long queryNumEntries(SQLiteDatabase db, String table, String selection,
635+
String[] selectionArgs) {
636+
String s = (!TextUtils.isEmpty(selection)) ? " where " + selection : "";
637+
return longForQuery(db, "select count(*) from " + table + s,
638+
selectionArgs);
615639
}
616640

617641
/**

0 commit comments

Comments
 (0)