QueryBuildRange中的表达式
在QueryBuildRange中很容易可以实现这样的功能:查询不同客户编号的客户,比如查询编号为A00001或者A00002的客户。如下代码所示:
static void GeneralQueryBuild()
{
Query q;
QueryRun qr;
CustTable cust;
str queryCriterion;
;
q = new Query();
queryCriterion = 'A000001,A000002';
q.addDataSource(tableNum(custtable)).addRange(fieldnum(custtable,accountnum)).value(queryCriterion);
qr = new QueryRun(q);
while(qr.next())
{
cust = qr.get(tablenum(custtable));
box::info(cust.Address);
}
pause;
}
{
Query q;
QueryRun qr;
CustTable cust;
str queryCriterion;
;
q = new Query();
queryCriterion = 'A000001,A000002';
q.addDataSource(tableNum(custtable)).addRange(fieldnum(custtable,accountnum)).value(queryCriterion);
qr = new QueryRun(q);
while(qr.next())
{
cust = qr.get(tablenum(custtable));
box::info(cust.Address);
}
pause;
}
这样针对一个字段的or条件很容易就实现了,如果是针对两个字段的或条件那?我们知道一般情况下针对两个不同的字段建立QueryBuildRange其关系是and关系,比如针对客户编号和客户名称建立不同的QueryBuildRange,如果分别添加value,则得到的结果是既满足客户编码是指定编码又要满足客户名称是指定名称的记录,如下代码所示(SQL中的like语法可以用统配符*来实现)
static void GeneralQueryBuild2()
{
Query q;
QueryRun qr;
QueryBuildDataSource qbds;
QueryBuildRange qbrNum;
QueryBuildRange qbrName;
CustTable cust;
str queryNumCriterion;
str queryNameCriterion;
;
q = new Query();
queryNumCriterion = 'A000001,A000002';
queryNameCriterion = 'Nanjing*';
qbds = q.addDataSource(tableNum(custtable));
qbrNum = qbds.addRange(fieldnum(custtable,accountnum));
qbrNum.value(queryNumCriterion);
qbrName = qbds.addRange(fieldNum(custtable,Name));
qbrname.value(queryNameCriterion);
qr = new QueryRun(q);
Box::info(qr.query().dataSourceNo(1).toString());
while(qr.next())
{
cust = qr.get(tablenum(custtable));
box::info(cust.Address);
}
pause;
}
{
Query q;
QueryRun qr;
QueryBuildDataSource qbds;
QueryBuildRange qbrNum;
QueryBuildRange qbrName;
CustTable cust;
str queryNumCriterion;
str queryNameCriterion;
;
q = new Query();
queryNumCriterion = 'A000001,A000002';
queryNameCriterion = 'Nanjing*';
qbds = q.addDataSource(tableNum(custtable));
qbrNum = qbds.addRange(fieldnum(custtable,accountnum));
qbrNum.value(queryNumCriterion);
qbrName = qbds.addRange(fieldNum(custtable,Name));
qbrname.value(queryNameCriterion);
qr = new QueryRun(q);
Box::info(qr.query().dataSourceNo(1).toString());
while(qr.next())
{
cust = qr.get(tablenum(custtable));
box::info(cust.Address);
}
pause;
}
如果要实现字段间的or条件该怎么玩那?这个问题郁闷了好久,后来找到一篇英文文档才得到答案,要用到QueryBuildRange中的表达式了。
为了在QueryBuildRange中运用表达式,需要像一般情况下一样对一个QueryDataSource添加Range,但具体针对哪一个字段添加Range是无所谓的,只要这个字段属于指定的表即可。
Range的value需要满足以下条件:
1.整个表达式必须要''引起来而不是"";
2.这个表达式必须用()括起来;
3.每个子表达式必须用各自的()括起来;
4.对于当前表中的字段,用字段名直接引用即可;
5.对于其他表中的字段,引用时需要添加DataSource Name作为前缀;
6.string类型的值需要用""引起来,并且包含在queryValue()中;
7.枚举类型的值需要用对应的int类型指定;
8.Date类型的值需要用Date2StrXpp()转化
OK,现在可以实现上面提到的问题了,看代码吧。
static void SpecialQueryBuild2()
{
Query q;
QueryRun qr;
QueryBuildDataSource qbds;
QueryBuildRange qbrNum;
QueryBuildRange qbrName;
str queryCriterion;
CustTable cust;
;
q = new Query();
qbds = q.addDataSource(tablenum(custtable));
qbrNum = qbds.addRange(fieldnum(custtable,accountnum));
queryCriterion = strfmt('((%1=="%2")||(%3=="%4"))',
fieldstr(custtable,AccountNum),
queryValue("A00001"),
fieldstr(custtable,Name),queryValue("Nanjing"));
qbrNum.value(queryCriterion);
qr = new QueryRun(q);
while(qr.next())
{
cust = qr.get(tablenum(custtable));
box::info(cust.Address);
}
pause;
}
{
Query q;
QueryRun qr;
QueryBuildDataSource qbds;
QueryBuildRange qbrNum;
QueryBuildRange qbrName;
str queryCriterion;
CustTable cust;
;
q = new Query();
qbds = q.addDataSource(tablenum(custtable));
qbrNum = qbds.addRange(fieldnum(custtable,accountnum));
queryCriterion = strfmt('((%1=="%2")||(%3=="%4"))',
fieldstr(custtable,AccountNum),
queryValue("A00001"),
fieldstr(custtable,Name),queryValue("Nanjing"));
qbrNum.value(queryCriterion);
qr = new QueryRun(q);
while(qr.next())
{
cust = qr.get(tablenum(custtable));
box::info(cust.Address);
}
pause;
}
不过有两点比较可惜,使用这种方式后,就不能实现SQL中的like语法(因为不能用*作为统配符)和普通QueryBuildRange中的针对同一个字段用","设定多个值的做法了(因为把,翻译成了普通的字符)。
另外还用到了一个技巧,由于调试器不怎么好用,如果想查看某个QueryRun最终要运行的SQL语句,可以用qr.query().datasourceno(1).tostring()来查看将要运行的SQL语句,虽然是X++语法的,不过看起来也没什么障碍。