Search in sources :

Example 1 with SelectItem

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);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) Statement(net.sf.jsqlparser.statement.Statement) SelectExpressionItem(net.sf.jsqlparser.statement.select.SelectExpressionItem) JSQLParserException(net.sf.jsqlparser.JSQLParserException) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) ArrayList(java.util.ArrayList) SQLQuery(org.jkiss.dbeaver.model.sql.SQLQuery) Function(net.sf.jsqlparser.expression.Function) Expression(net.sf.jsqlparser.expression.Expression) Column(net.sf.jsqlparser.schema.Column) SelectItem(net.sf.jsqlparser.statement.select.SelectItem) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Select(net.sf.jsqlparser.statement.select.Select) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList)

Example 2 with SelectItem

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());
        }
    }
}
Also used : Function(net.sf.jsqlparser.expression.Function) Expression(net.sf.jsqlparser.expression.Expression) SelectItem(net.sf.jsqlparser.statement.select.SelectItem) SelectExpressionItem(net.sf.jsqlparser.statement.select.SelectExpressionItem) Select(net.sf.jsqlparser.statement.select.Select) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Test(org.junit.Test)

Example 3 with SelectItem

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());
        }
    }
}
Also used : Function(net.sf.jsqlparser.expression.Function) SelectItem(net.sf.jsqlparser.statement.select.SelectItem) Select(net.sf.jsqlparser.statement.select.Select) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Test(org.junit.Test)

Aggregations

Function (net.sf.jsqlparser.expression.Function)3 PlainSelect (net.sf.jsqlparser.statement.select.PlainSelect)3 Select (net.sf.jsqlparser.statement.select.Select)3 SelectItem (net.sf.jsqlparser.statement.select.SelectItem)3 Expression (net.sf.jsqlparser.expression.Expression)2 SelectExpressionItem (net.sf.jsqlparser.statement.select.SelectExpressionItem)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 JSQLParserException (net.sf.jsqlparser.JSQLParserException)1 ExpressionList (net.sf.jsqlparser.expression.operators.relational.ExpressionList)1 Column (net.sf.jsqlparser.schema.Column)1 Statement (net.sf.jsqlparser.statement.Statement)1 DBException (org.jkiss.dbeaver.DBException)1 SQLQuery (org.jkiss.dbeaver.model.sql.SQLQuery)1