Search in sources :

Example 86 with Expression

use of net.sf.jsqlparser.expression.Expression 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 87 with Expression

use of net.sf.jsqlparser.expression.Expression in project spanner-jdbc by olavloite.

the class CloudSpannerPreparedStatement method createUpdateMutation.

private Mutation createUpdateMutation(Update update, boolean generateParameterMetaData) throws SQLException {
    if (update.getTables().isEmpty())
        throw new CloudSpannerSQLException("No table found in update statement", Code.INVALID_ARGUMENT);
    if (update.getTables().size() > 1)
        throw new CloudSpannerSQLException("Update statements for multiple tables at once are not supported", Code.INVALID_ARGUMENT);
    String table = unquoteIdentifier(update.getTables().get(0).getFullyQualifiedName());
    getParameterStore().setTable(table);
    List<Expression> expressions = update.getExpressions();
    WriteBuilder builder = Mutation.newUpdateBuilder(table);
    int index = 0;
    for (Column col : update.getColumns()) {
        String columnName = unquoteIdentifier(col.getFullyQualifiedName());
        expressions.get(index).accept(new ValueBinderExpressionVisitorAdapter<>(getParameterStore(), builder.set(columnName), columnName));
        index++;
    }
    visitUpdateWhereClause(update.getWhere(), builder, generateParameterMetaData);
    return builder.build();
}
Also used : Expression(net.sf.jsqlparser.expression.Expression) Column(net.sf.jsqlparser.schema.Column) WriteBuilder(com.google.cloud.spanner.Mutation.WriteBuilder) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException)

Example 88 with Expression

use of net.sf.jsqlparser.expression.Expression in project spanner-jdbc by olavloite.

the class CloudSpannerPreparedStatement method createInsertMutation.

private Mutation createInsertMutation(Insert insert, boolean generateParameterMetaData) throws SQLException {
    ItemsList items = insert.getItemsList();
    if (generateParameterMetaData && items == null && insert.getSelect() != null) {
        // Just initialize the parameter meta data of the select statement
        createSelectBuilder(insert.getSelect(), insert.getSelect().toString());
        return null;
    }
    if (!(items instanceof ExpressionList)) {
        throw new CloudSpannerSQLException("Insert statement must specify a list of values", Code.INVALID_ARGUMENT);
    }
    if (insert.getColumns() == null || insert.getColumns().isEmpty()) {
        throw new CloudSpannerSQLException("Insert statement must specify a list of column names", Code.INVALID_ARGUMENT);
    }
    List<Expression> expressions = ((ExpressionList) items).getExpressions();
    String table = unquoteIdentifier(insert.getTable().getFullyQualifiedName());
    getParameterStore().setTable(table);
    WriteBuilder builder;
    if (insert.isUseDuplicate()) {
        /**
         * Do an insert-or-update. BUT: Cloud Spanner does not support supplying different values for
         * the insert and update statements, meaning that only the values specified in the INSERT part
         * of the statement will be considered. Anything specified in the 'ON DUPLICATE KEY UPDATE
         * ...' statement will be ignored.
         */
        if (this.forceUpdate)
            builder = Mutation.newUpdateBuilder(table);
        else
            builder = Mutation.newInsertOrUpdateBuilder(table);
    } else {
        /**
         * Just do an insert and throw an error if a row with the specified key alread exists.
         */
        builder = Mutation.newInsertBuilder(table);
    }
    int index = 0;
    for (Column col : insert.getColumns()) {
        String columnName = unquoteIdentifier(col.getFullyQualifiedName());
        expressions.get(index).accept(new ValueBinderExpressionVisitorAdapter<>(getParameterStore(), builder.set(columnName), columnName));
        index++;
    }
    return builder.build();
}
Also used : ItemsList(net.sf.jsqlparser.expression.operators.relational.ItemsList) Expression(net.sf.jsqlparser.expression.Expression) Column(net.sf.jsqlparser.schema.Column) WriteBuilder(com.google.cloud.spanner.Mutation.WriteBuilder) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList)

Example 89 with Expression

use of net.sf.jsqlparser.expression.Expression in project spanner-jdbc by olavloite.

the class CloudSpannerPreparedStatement method createDeleteMutation.

private Mutation createDeleteMutation(Delete delete, boolean generateParameterMetaData) throws SQLException {
    String table = unquoteIdentifier(delete.getTable().getFullyQualifiedName());
    getParameterStore().setTable(table);
    Expression where = delete.getWhere();
    if (where == null) {
        // Delete all
        return Mutation.delete(table, KeySet.all());
    } else {
        // Delete one
        DeleteKeyBuilder keyBuilder = new DeleteKeyBuilder(getConnection().getTable(table), generateParameterMetaData);
        visitDeleteWhereClause(where, keyBuilder, generateParameterMetaData);
        return Mutation.delete(table, keyBuilder.getKeyBuilder().build());
    }
}
Also used : Expression(net.sf.jsqlparser.expression.Expression)

Example 90 with Expression

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

the class SQLQueryTransformerCount method tryInjectCount.

private SQLQuery tryInjectCount(DBPDataSource dataSource, SQLQuery query) throws DBException {
    try {
        Statement statement = CCJSqlParserUtil.parse(query.getText());
        if (statement instanceof Select && ((Select) statement).getSelectBody() instanceof PlainSelect) {
            PlainSelect select = (PlainSelect) ((Select) statement).getSelectBody();
            if (select.getHaving() != null) {
                throw new DBException("Can't inject COUNT into query with HAVING clause");
            }
            if (select.getGroupBy() != null && !CommonUtils.isEmpty(select.getGroupBy().getGroupByExpressions())) {
                throw new DBException("Can't inject COUNT into query with GROUP BY clause");
            }
            Distinct selectDistinct = select.getDistinct();
            if (selectDistinct != null) {
                // Remove distinct
                select.setDistinct(null);
            }
            Function countFunc = new Function();
            countFunc.setName("count");
            if (selectDistinct != null) {
                countFunc.setDistinct(true);
                List<Expression> exprs = new ArrayList<>();
                for (SelectItem item : select.getSelectItems()) {
                    if (item instanceof SelectExpressionItem) {
                        exprs.add(((SelectExpressionItem) item).getExpression());
                    }
                }
                if (!exprs.isEmpty()) {
                    countFunc.setParameters(new ExpressionList(exprs));
                }
            }
            countFunc.setAllColumns(true);
            List<SelectItem> selectItems = new ArrayList<>();
            selectItems.add(new SelectExpressionItem(countFunc));
            select.setSelectItems(selectItems);
            select.setOrderByElements(null);
            return new SQLQuery(dataSource, select.toString(), query, false);
        } else {
            throw new DBException("Query [" + query.getText() + "] 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) JSQLParserException(net.sf.jsqlparser.JSQLParserException) ArrayList(java.util.ArrayList) Function(net.sf.jsqlparser.expression.Function) Expression(net.sf.jsqlparser.expression.Expression) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList)

Aggregations

Expression (net.sf.jsqlparser.expression.Expression)92 AndExpression (net.sf.jsqlparser.expression.operators.conditional.AndExpression)30 Test (org.junit.Test)30 ArrayList (java.util.ArrayList)26 BinaryExpression (net.sf.jsqlparser.expression.BinaryExpression)25 Column (net.sf.jsqlparser.schema.Column)19 SignedExpression (net.sf.jsqlparser.expression.SignedExpression)18 ExpressionList (net.sf.jsqlparser.expression.operators.relational.ExpressionList)17 Table (net.sf.jsqlparser.schema.Table)14 CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)12 LikeExpression (net.sf.jsqlparser.expression.operators.relational.LikeExpression)12 AlterExpression (net.sf.jsqlparser.statement.alter.AlterExpression)12 NotExpression (net.sf.jsqlparser.expression.NotExpression)11 StatementExecutionException (herddb.model.StatementExecutionException)10 Select (net.sf.jsqlparser.statement.select.Select)10 AnalyticExpression (net.sf.jsqlparser.expression.AnalyticExpression)9 CaseExpression (net.sf.jsqlparser.expression.CaseExpression)9 KeepExpression (net.sf.jsqlparser.expression.KeepExpression)9 OrExpression (net.sf.jsqlparser.expression.operators.conditional.OrExpression)9 PlainSelect (net.sf.jsqlparser.statement.select.PlainSelect)9