Search in sources :

Example 1 with PlainSelect

use of net.sf.jsqlparser.statement.select.PlainSelect 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 PlainSelect

use of net.sf.jsqlparser.statement.select.PlainSelect in project dal by ctripcorp.

the class SqlBuilder method pagingQuerySql.

/**
     * Re-build the query SQL to implement paging function. The new SQL
     * Statement will contains limit if the database type is MYSQL, CET wrapped
     * if database type is SQL Server. Note: the final SQL will contain two %s,
     * which should be replaced in run time.
     *
     * @param sql    The original SQL Statement
     * @param dbType The database type
     * @return Re-build SQL which contains limit if the database type is MYSQL,
     * CET wrapped if database type is SQL Server.
     * @throws Exception
     */
public static String pagingQuerySql(String sql, DatabaseCategory dbType, CurrentLanguage lang) throws Exception {
    String sql_content = sql.replace("@", ":");
    boolean withNolock = StringUtils.containsIgnoreCase(sql_content, "WITH (NOLOCK)");
    if (withNolock)
        sql_content = sql_content.replaceAll("(?i)WITH \\(NOLOCK\\)", "");
    StringBuilder sb = new StringBuilder();
    try {
        Select select = (Select) parserManager.parse(new StringReader(sql_content));
        PlainSelect plain = (PlainSelect) select.getSelectBody();
        if (dbType == DatabaseCategory.MySql) {
            sb.append(plain.toString());
            sb.append(lang == CurrentLanguage.Java ? mysqlPageClausePattern : mysqlCSPageClausePattern);
        } else if (dbType == DatabaseCategory.SqlServer) {
            sb.append(plain.toString());
            sb.append(lang == CurrentLanguage.Java ? sqlserverPagingClausePattern : sqlseverCSPagingClausePattern);
        } else {
            throw new Exception("Unknow database category.");
        }
    } catch (Exception e) {
        log.error("Paging the SQL Failed.", e);
        throw e;
    }
    return sb.toString().replace(":", "@");
}
Also used : PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Select(net.sf.jsqlparser.statement.select.Select) StringReader(java.io.StringReader) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect)

Example 3 with PlainSelect

use of net.sf.jsqlparser.statement.select.PlainSelect 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 4 with PlainSelect

use of net.sf.jsqlparser.statement.select.PlainSelect 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)

Example 5 with PlainSelect

use of net.sf.jsqlparser.statement.select.PlainSelect in project dbeaver by serge-rider.

the class QueryTransformerTop method transformQueryString.

@Override
public String transformQueryString(SQLQuery query) throws DBCException {
    limitSet = false;
    if (query.isPlainSelect()) {
        try {
            Statement statement = query.getStatement();
            if (statement instanceof Select) {
                Select select = (Select) statement;
                if (select.getSelectBody() instanceof PlainSelect) {
                    PlainSelect selectBody = (PlainSelect) select.getSelectBody();
                    if (selectBody.getTop() == null && CommonUtils.isEmpty(selectBody.getIntoTables())) {
                        Top top = new Top();
                        top.setPercentage(false);
                        top.setExpression(new LongValue(offset.longValue() + length.longValue()));
                        selectBody.setTop(top);
                        limitSet = true;
                        return statement.toString();
                    }
                }
            }
        } catch (Throwable e) {
            // ignore
            log.debug(e);
        }
    }
    return query.getQuery();
}
Also used : Top(net.sf.jsqlparser.statement.select.Top) DBCStatement(org.jkiss.dbeaver.model.exec.DBCStatement) Statement(net.sf.jsqlparser.statement.Statement) Select(net.sf.jsqlparser.statement.select.Select) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) LongValue(net.sf.jsqlparser.expression.LongValue)

Aggregations

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