使用ContentProvider执行复杂原始查询的优雅方式

时间:2010-12-28 20:24:45

标签: android android-contentprovider

我使用ContentProvider与我的应用程序的数据库对话,我遇到了一些问题。

我的查询有点复杂。它看起来像这样:

String sql =
    "select " +
    "  tblHistory._id _id, " +
    "  tblHistory.item item, " +
    "  tblHistory.updated_on updated_on, " +
    "  (select _id from tblList " +
        "where tblList.item = tblHistory.item) list_id, " +
    "  1 priority, " +
    "from " +
    "  tblHistory " +
    "where " +
    "  tblHistory.status <> 'STATE_DELETING' and " + selection + " " +
    "union " +
    "select " +
    "  tblSearch._id _id, " +
    "  tblSearch.item item, " +
    "  -1 updated_on, " +
    "  (select _id from tblList " +
        "where tblList.item = tblSearch.product_name) list_id, " +
    "  2 priority, " +
    "from " +
    "  tblSearch " +
    "where " +
    "  not exists (select * from tblHistory " +
        "where tblHistory.item = tblSearch.product_name) " +
    "order by " +
    "  priority, _id asc";

    c = mDb.rawQuery(sql, null);

选择是:

String where = "tblHistory.user_id="
               + Integer.toString(intUserId)
               + " and tblHistory.item like '%"
               + strSearch + "%'";

我的问题是我的子查询。我有一个我需要添加的约束,但是没有好的方法可以将该约束下载到该方法的管道中。我需要在子查询中使用正确的user_id。

此时,我认为我有两个选择:

1)从选择中解析出user_id子串。

2)使用selectionArgs作为黑客将"user_id = " + Integer.toString(intUserId)传递给方法。

还有其他想法吗?

我应该注意,虽然我宁愿不做任何黑客行为,但我已将ContentProvider设为私有,因为它仅供我的应用程序使用;所以,如果我绝对必须,我可以。

1 个答案:

答案 0 :(得分:2)

在处理这样的复杂查询时,构建器类可以派上用场。 它避免了你必须将它们转换为String,希望这对你有帮助。

public class SqlQueryBuilder {

private static final String SELECT = "SELECT";

private static final String COMA = " , ";

private static final String WHERE = " where ";

private static final String EQUALS = " = ";

private static final String NOT_EQUALS = " != ";

private static final String FROM = " FROM ";

private final StringBuilder mStrBuilder = new StringBuilder();

public void init() {
    mStrBuilder.setLength(0);
}

public SqlQueryBuilder select(Object ...columns) {
    mStrBuilder.append(SELECT);
    for(Object column:columns) {
        mStrBuilder.append(column);
        mStrBuilder.append(COMA);
    }
    return this;
}

public SqlQueryBuilder where(Object conditionVar) {
    mStrBuilder.append(WHERE);
    mStrBuilder.append(conditionVar);
    return this;
}


public SqlQueryBuilder equalsVal(Object equalsVal) {
    mStrBuilder.append(EQUALS);
    mStrBuilder.append(equalsVal);
    return this;
}

public SqlQueryBuilder notEqualsVal(Object notEqualsVal) {
    mStrBuilder.append(NOT_EQUALS);
    mStrBuilder.append(notEqualsVal);
    return this;
}

public SqlQueryBuilder from(Object from) {
    mStrBuilder.append(FROM);
    mStrBuilder.append(from);
    return this;
}

public String build() {
    final String sqlQuery = mStrBuilder.toString(); 
    init();
    return sqlQuery;
}

}

用法如下

{

    SqlQueryBuilder sqlQueryBuilder = new SqlQueryBuilder();

    String innerQuery = sqlQueryBuilder.select("_id").from("tblList")
            .where("tblList.item").equalsVal("tblHistory.item").build();

    String query = sqlQueryBuilder.select("tblHistory._id _id",
            "tblHistory.item", "tblHistory.updated_on updated_on",
            innerQuery + " list_id", "1 priority").build();

}