NutzCN Logo
问答 如何用cnd拼接初join语句
发布于 2165天前 作者 qq_e48f71e9 3030 次浏览 复制 上一个帖子 下一个帖子
标签:

我想在cnd拼接各种条件之前,先拼接一些inner join语句

请问除了自定义sql以外,还有其他办法嘛?

23 回复

有大招cnd.and(new Static

cnd.and 的话至少也会拼接select xx from xx where的,有了where后面就无法些join了呀....

现在就是愁的如何在拼接where字符之前拼接上join语句

new Static是原样拼入sql的,试一下嘛

不行哎,join前还是会有where

Cnd cnd = Cnd.NEW();
            cnd.and(new Static(" inner join xxx as c on xxx.aaa = c.bbb"));

生成sql:

      SELECT COUNT(*) FROM aaa  WHERE   inner join xxx as c on xxx.aaa = c.bbb  AND aaa.bbb=? AND c.ddd=? AND ........

连where都不想要... 那就没办法了..

为什么连:

Cnd.wrap(String sql)
Cnd.format(String sql)

都会在我的sql前拼接一个where。。。。。。

有没有将sql直接转换成Condition下的对象,不对我的sql额外操作的???

oh~找到了。。。。

Condition cnd2 = new SimpleCriteria(sql);

我执行:

this.dao().query(xxx.class, cnd2, pager);

是正常的injo+where语句,没问题
但是我执行:

this.dao().count(xxx.class,cnd2)

执行的sql语句是:

SELECT COUNT(*) FROM xxx 

我怀疑是个bug。。。。。

cnd2是我拼接的sql,inner join+where条件的自定义sql

Condition cnd2 = new SimpleCriteria(sql);

这么来的,有了cnd2我就可以查询了,然后执行

this.dao().query(xxx.class, cnd2, pager);

ok,没问题查询语句正常,然后执行

this.dao().count(xxx.class,cnd2)

查看log里的语句为:

SELECT COUNT(*) FROM xxx 

不是应该是我们拼接的join+where的sql语句嘛?怎么成查询所有了?

莫非:

int count(Class<?> classOfT, Condition cnd);

方法中的cnd,不支持解析除where语句的其他语句???

    private int _count(Entity<?> en, String tableName, Condition cnd) {
        // 如果有条件的话
        if (null != cnd) {
            Pojo pojo = pojoMaker.makeFunc(tableName, "COUNT", "*");
            pojo.setEntity(en);
            if (cnd instanceof Criteria) {
                pojo.append(((Criteria) cnd).where());  //跟踪到这里,cnd里的内容是:inner join xxx on xxx.xxx = xxx.xxx where xxx = 'xxx',总之是我的sql,到这里是没问题的
             
                GroupBy gb = ((Criteria) cnd).getGroupBy(); //到这里pojo里的内容依然是:select count(*) from xxx,不是应该变成cnd里的sql嘛?
                pojo.append(gb);
            }
            // 否则暴力获取 WHERE 子句
            else {
                String str = Pojos.formatCondition(en, cnd);
                if (!Strings.isBlank(str)) {
                    String[] ss = str.toUpperCase().split("ORDER BY");
                    pojo.append(Pojos.Items.wrap(str.substring(0, ss[0].length())));
                }
            }
            // 设置回调,并执行 SQL
            pojo.setAfter(_pojo_fetchInt);
            _exec(pojo);
            return pojo.getInt();
        }
        // 没有条件,直接生成表达式
        return func(tableName, "COUNT", "*");
    }

SimpleCriteria.where方法返回的null了, 看了看代码

嗯。。这是bug嘛

我暂时用自定义sql查询了

报个issue哦

Static
真香。。。。

添加回复
请先登陆
回到顶部