public NutMap data(int length, int start, int draw, Sql countSql, Sql orderSql) {
NutMap re = new NutMap();
Pager pager = new OffsetPager(start, length);
pager.setRecordCount((int) Daos.queryCount(dao(), countSql.toString()));// 记录数需手动设置
orderSql.setPager(pager);
orderSql.setCallback(Sqls.callback.records());
this.dao().execute(orderSql);
re.put("recordsFiltered", pager.getRecordCount());
re.put("data", orderSql.getList(Record.class));
re.put("draw", draw);
re.put("recordsTotal", length);
return re;
}
pager.setRecordCount((int) Daos.queryCount(dao(), countSql.toString()));// 记录数需手动设置
底层调用queryCount方法
public static long queryCount(Dao dao, String sql) {
String tmpTable = "as _nutz_tmp";
if (dao.meta().isDB2())
tmpTable = "as nutz_tmp_" + R.UU32();
else if (dao.meta().isOracle())
tmpTable = "";
else
tmpTable += "_" + R.UU32();
Sql sql2 = Sqls.fetchInt("select count(1) from (" + sql + ")" + tmpTable);
dao.execute(sql2);
return sql2.getInt();
}
fetchInt
public static Sql fetchInt(String sql) {
return create(sql).setCallback(callback.integer());
}
我从上层过来,调用data方法,ordersql必须是sql类型,但queryCount又要求是String类型参数,然后底层又是sql类型。
必须要这样吗?我遇到的问题
我上层方法没有用cnd,比如就传一个email参数,ordersql是个sql类型(这里只写条件了)
where m_account.deleted = 0 and m_account.email='morningchin@damm.es'
这个条件直接执行可以
但是countsql在后面是作为str类型的,那么这么拼接就不可以
需要两个@
where m_account.deleted = 0 and m_account.email='morningchin@@damm.es'
这么拼起来,才能成功取得count,否则就是这样
where m_account.deleted = 0 and m_account.email='morningchin'NULL'.es'
我创建两个sql的时候,把email的@
替换成@@然后再替换回来才能正常。