Search in sources :

Example 1 with JSQLParserException

use of net.sf.jsqlparser.JSQLParserException 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 JSQLParserException

use of net.sf.jsqlparser.JSQLParserException in project dbeaver by serge-rider.

the class SQLSemanticProcessor method addWhereToSelect.

public static void addWhereToSelect(PlainSelect select, String condString) throws JSQLParserException {
    Expression filterWhere;
    try {
        filterWhere = CCJSqlParserUtil.parseCondExpression(condString);
    } catch (JSQLParserException e) {
        throw new JSQLParserException("Bad query condition: [" + condString + "]", e);
    }
    addWhereToSelect(select, filterWhere);
}
Also used : Expression(net.sf.jsqlparser.expression.Expression) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) JSQLParserException(net.sf.jsqlparser.JSQLParserException)

Aggregations

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