use of net.sf.jsqlparser.statement.select.SelectItem in project dbeaver by serge-rider.
the class SQLQueryTransformerCount method transformQuery.
@Override
public SQLQuery transformQuery(SQLDataSource dataSource, SQLQuery query) throws DBException {
try {
Statement statement = CCJSqlParserUtil.parse(query.getQuery());
if (statement instanceof Select && ((Select) statement).getSelectBody() instanceof PlainSelect) {
PlainSelect select = (PlainSelect) ((Select) statement).getSelectBody();
List<SelectItem> selectItems = new ArrayList<>();
Function countFunc = new Function();
countFunc.setName("count");
countFunc.setParameters(new ExpressionList(Collections.<Expression>singletonList(new Column("*"))));
SelectItem countItem = new SelectExpressionItem(countFunc);
selectItems.add(countItem);
select.setSelectItems(selectItems);
return new SQLQuery(dataSource, select.toString(), query, false);
} else {
throw new DBException("Query [" + query.getQuery() + "] can't be modified");
}
} catch (JSQLParserException e) {
throw new DBException("Can't transform query to SELECT count(*)", e);
}
}
use of net.sf.jsqlparser.statement.select.SelectItem in project Mybatis-PageHelper by pagehelper.
the class FunctionCountTest method test.
@Test
public void test() {
Select select = select("select max(name),code,min(aa),nvl(ab,0),heh from user where a > 100");
List<SelectItem> selectItems = ((PlainSelect) select.getSelectBody()).getSelectItems();
for (SelectItem item : selectItems) {
if (item instanceof SelectExpressionItem) {
Expression exp = ((SelectExpressionItem) item).getExpression();
if (exp instanceof Function) {
System.out.println("Function:" + item.toString());
} else {
System.out.println("Not a function:" + exp.toString());
}
} else {
System.out.println("Not a function:" + item.toString());
}
}
}
use of net.sf.jsqlparser.statement.select.SelectItem in project Mybatis-PageHelper by pagehelper.
the class FunctionCountTest method test2.
@Test
public void test2() {
Select select = select("select distinct(name) from user where a > 100");
List<SelectItem> selectItems = ((PlainSelect) select.getSelectBody()).getSelectItems();
for (SelectItem item : selectItems) {
if (item instanceof Function) {
System.out.println("Function:" + item.toString());
} else {
System.out.println("Not a function:" + item.toString());
}
}
}
Aggregations