ORMLite中的多个组合OR条件 [英] Multiple, combined OR conditions in ORMLite

查看:239
本文介绍了ORMLite中的多个组合OR条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢这样的查询:

select data from table
 where (x > 1 and x < 100)
    or (x > 250 and x < 300)

在ORMlite中,可以使用此代码:

In ORMlite, that's possible using this code:

final QueryBuilder<Data,Integer> qb = queryBuilder();
final Where<Data, Integer> w = qb.where();

w.or(
    w.gt("x", 1).and().lt("x", 100),
    w.gt("x", 250).and().lt("x", 300)
)

如果事先知道条件,那就太好了。在编码时,我需要动态添加条件。

While thats great if one knows the conditions beforehand & at the time of coding, I need the conditions to be dynamically added.

基本上该方法 public com.j256.ormlite.stmt.Where< T,ID>或(com.j256.ormlite.stmt.Where< T,ID> left,com.j256.ormlite.stmt.Where< T,ID> right,com.j256.ormlite.stmt.Where< T,ID> .. 。其他)是不够的。
它需要另一个方法,它支持 其中条件。

Basically that method public com.j256.ormlite.stmt.Where<T,ID> or(com.j256.ormlite.stmt.Where<T,ID> left, com.j256.ormlite.stmt.Where<T,ID> right, com.j256.ormlite.stmt.Where<T,ID>... others) is not enough. It needs another or method that supports a ArrayList of Where conditions.

感谢您的任何建议。

推荐答案


如果事先知道条件,那就太好了。在编码时,我需要动态添加条件。

While thats great if one knows the conditions beforehand & at the time of coding, I need the conditions to be dynamically added.

ORMLite Where.or(其中< T,ID> left,Where< T,ID> right,Where< T,ID> ...其他)是一种语法黑客攻击。当你致电:

In ORMLite Where.or(Where<T, ID> left, Where<T, ID> right, Where<T, ID>... others) is a bit of a syntax hack. When you call:

w.or(
    w.gt("x", 1).and().lt("x", 100),
    w.gt("x", 250).and().lt("x", 300)
);

或()方法得到的是:

w.or(w, w);

你真的可以把它重写为:

You really could rewrite it as:

w.gt("x", 1).and().lt("x", 100);
w.gt("x", 250).and().lt("x", 300);
w.or(w, w);

方法只使用计算从堆栈中弹出多少子句的参数。当您调用 gt lt 和其他人时,它会推送子句堆栈上的项目。 和()方法将1个项目从堆栈中拉出,然后在将来获取另一个项目。我们做这些语法黑客,因为我们想支持线性,链接和基于参数的查询:

The or method there is only using the arguments to count how many clauses it needs to pop off of the stack. When you call gt and lt and others, it pushes items on a clause stack. The and() method pulls 1 item off the stack and then takes another item in the future. We do these syntax hacks because we want to support linear, chained, and argument based queries:

w.gt("x", 1);
w.and();
w.lt("x", 100);

vs:

w.gt("x", 1).and().lt("x", 100);

vs:

w.and(w.gt("x", 1), w.lt("x", 100));

但这意味着您可以使用 Where.or(int many)方法。所以在上面的示例中也可以是:

But this means that you have the power to simplify your code immensely by using the Where.or(int many) method. So in the or example above can also be:

w.gt("x", 1).and().lt("x", 100);
w.gt("x", 250).and().lt("x", 300);
// create an OR statement from the last 2 clauses on the stack
w.or(2);

所以你不需要条件完全列出。你需要的只是一个柜台。所以你可以这样做:

So you don't need the conditions list at all. All you need is a counter. So you could do something like:

int clauseC = 0;
for (int i : values) {
    if (i == 1) {
        w.le(C_PREIS, 1000);
        clauseC++;
    } else if (i == 2) {
        w.gt(C_PREIS, 1000).and().le(C_PREIS, 2500);
        clauseC++;
    } else if (i == 3) {
        w.gt(C_PREIS, 2500).and().le(C_PREIS, 5000);
        clauseC++;
    } else if (i == 4) {
        w.gt(C_PREIS, 5000).and().le(C_PREIS, 10000);
        clauseC++;
    } else if (i == 5) {
        w.gt(C_PREIS, 10000);
        clauseC++;
    }
}
// create one big OR(...) statement with all of the clauses pushed above
if (clauseC > 1) {
    w.or(clauseC);
}

如果可以只有1到5然后你可以使用 values.size()并跳过 clauseC 。请注意,如果我们只添加一个子句,那么我们可以完全跳过 OR 方法调用。

If i can only be 1 to 5 then you can just use values.size() and skip the clauseC. Notice that if we are only adding one clause then we can skip the OR method call entirely.

哦,和以下语句将工作:

Oh, and the following statement will not work:

target.or().raw(first.getStatement());

因为目标第一个是同一个对象。 first.getStatement()转储整个SQL WHERE 子句,我不认为这是你想要的。

because target and first are the same object. first.getStatement() dumps the entire SQL WHERE clause which I don't think is what you want.

这篇关于ORMLite中的多个组合OR条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆