Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add distinct expression for count query #1032

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,14 @@ public static String createSqlSelect(String tablename, String tableAlias, String

/** Creates SELECT COUNT(*) with a trailing space. */
public static String createSqlSelectCountStar(String tablename, String tableAliasOrNull) {
StringBuilder builder = new StringBuilder("SELECT COUNT(*) FROM ");
return createSqlSelectCount("*", tablename, tableAliasOrNull);
}

/** Creates SELECT COUNT($expression) with a trailing space. */
public static String createSqlSelectCount(String expression, String tablename, String tableAliasOrNull) {
StringBuilder builder = new StringBuilder("SELECT COUNT(");
builder.append(expression);
builder.append(") FROM ");
builder.append('"').append(tablename).append('"').append(' ');
if (tableAliasOrNull != null) {
builder.append(tableAliasOrNull).append(' ');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public QueryBuilder<T> offset(int offset) {

/**
* Builds a reusable query object (Query objects can be executed more efficiently than creating a QueryBuilder for
* each execution.
* each execution.)
*/
public Query<T> build() {
StringBuilder builder = createSelectBuilder();
Expand All @@ -291,7 +291,7 @@ public Query<T> build() {

/**
* Builds a reusable query object for low level android.database.Cursor access.
* (Query objects can be executed more efficiently than creating a QueryBuilder for each execution.
* (Query objects can be executed more efficiently than creating a QueryBuilder for each execution.)
*/
public CursorQuery buildCursor() {
StringBuilder builder = createSelectBuilder();
Expand Down Expand Up @@ -341,7 +341,7 @@ private int checkAddOffset(StringBuilder builder) {

/**
* Builds a reusable query object for deletion (Query objects can be executed more efficiently than creating a
* QueryBuilder for each execution.
* QueryBuilder for each execution.)
*/
public DeleteQuery<T> buildDelete() {
if (!joins.isEmpty()) {
Expand All @@ -366,7 +366,7 @@ public DeleteQuery<T> buildDelete() {

/**
* Builds a reusable query object for counting rows (Query objects can be executed more efficiently than creating a
* QueryBuilder for each execution.
* QueryBuilder for each execution.)
*/
public CountQuery<T> buildCount() {
String tablename = dao.getTablename();
Expand All @@ -380,6 +380,26 @@ public CountQuery<T> buildCount() {
return CountQuery.create(dao, sql, values.toArray());
}

/**
* Builds a reusable query object for counting optionally distinct rows according to a column expression.
* (Query objects can be executed more efficiently than creating a QueryBuilder for each execution.)
*
* @param expression column or expression that involves columns to which the COUNT function should be applied
* @param distinct whether the COUNT should be DISTINCT instead of ALL
*/
public CountQuery<T> buildCount(String expression, boolean distinct) {
String tablename = dao.getTablename();
String optionExpression = distinct ? "DISTINCT " + expression : expression;
String baseSql = SqlUtils.createSqlSelectCount(optionExpression, tablename, tablePrefix);
StringBuilder builder = new StringBuilder(baseSql);
appendJoinsAndWheres(builder, tablePrefix);

String sql = builder.toString();
checkLog(sql);

return CountQuery.create(dao, sql, values.toArray());
}

private void checkLog(String sql) {
if (LOG_SQL) {
DaoLog.d("Built SQL for query: " + sql);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.ArrayList;

import org.greenrobot.greendao.daotest.TestEntityDao;
import org.greenrobot.greendao.daotest.entity.TestEntityTestBase;
import org.greenrobot.greendao.query.CountQuery;
import org.greenrobot.greendao.query.Query;
Expand Down Expand Up @@ -123,4 +124,19 @@ public void testBuildQueryAndCountQuery() {
assertEquals(1, countQuery.count());
}

public void testDistinctExpressionCountQuery() {
int value = getSimpleInteger(1);
CountQuery<TestEntity> query = dao.queryBuilder().buildCount(Properties.SimpleInteger.columnName, true);
assertEquals(0, query.count());

ArrayList<TestEntity> inserted = insert(3);
assertEquals(3, query.count());

inserted.get(2).setSimpleInteger(value);
dao.update(inserted.get(2));
assertEquals(2, query.count());

dao.deleteAll();
assertEquals(0, query.count());
}
}