Search in sources :

Example 1 with SQLParser

use of com.akiban.sql.parser.SQLParser in project com.revolsys.open by revolsys.

the class QueryValue method parseWhere.

static Condition parseWhere(final RecordDefinition recordDefinition, final String whereClause) {
    if (Property.hasValue(whereClause)) {
        try {
            final SQLParser sqlParser = new SQLParser();
            String name;
            if (recordDefinition == null) {
                name = "Unknown";
            } else {
                name = recordDefinition.getName();
            }
            final String query = "SELECT * FROM \"" + name + "\" WHERE " + whereClause;
            final StatementNode statement = sqlParser.parseStatement(query);
            if (statement instanceof CursorNode) {
                final CursorNode selectStatement = (CursorNode) statement;
                final ResultSetNode resultSetNode = selectStatement.getResultSetNode();
                if (resultSetNode instanceof SelectNode) {
                    final SelectNode selectNode = (SelectNode) resultSetNode;
                    final ValueNode where = selectNode.getWhereClause();
                    final Condition condition = toQueryValue(recordDefinition, where);
                    return condition;
                }
            }
            return null;
        } catch (final Throwable e) {
            throw new IllegalArgumentException("Invalid where clause: " + whereClause, e);
        }
    } else {
        return null;
    }
}
Also used : SQLParser(com.akiban.sql.parser.SQLParser) SelectNode(com.akiban.sql.parser.SelectNode) SQLToJavaValueNode(com.akiban.sql.parser.SQLToJavaValueNode) JavaValueNode(com.akiban.sql.parser.JavaValueNode) JavaToSQLValueNode(com.akiban.sql.parser.JavaToSQLValueNode) ValueNode(com.akiban.sql.parser.ValueNode) StatementNode(com.akiban.sql.parser.StatementNode) CursorNode(com.akiban.sql.parser.CursorNode) ResultSetNode(com.akiban.sql.parser.ResultSetNode)

Example 2 with SQLParser

use of com.akiban.sql.parser.SQLParser in project com.revolsys.open by revolsys.

the class QueryWhereConditionField method verifyCondition.

public void verifyCondition() {
    this.validating = true;
    this.valid = true;
    this.statusLabel.setText("");
    try {
        final String whereClause = this.whereTextField.getText();
        if (Property.hasValue(whereClause)) {
            final String sql = "SELECT * FROM X WHERE " + "\n" + whereClause;
            try {
                final StatementNode statement = new SQLParser().parseStatement(sql);
                if (statement instanceof CursorNode) {
                    final CursorNode selectStatement = (CursorNode) statement;
                    final ResultSetNode resultSetNode = selectStatement.getResultSetNode();
                    if (resultSetNode instanceof SelectNode) {
                        final SelectNode selectNode = (SelectNode) resultSetNode;
                        final ValueNode where = selectNode.getWhereClause();
                        final QueryValue queryValue = toQueryValue(where);
                        if (queryValue instanceof Condition) {
                            final Condition condition = (Condition) queryValue;
                            if (this.valid) {
                                setFieldValue(condition);
                                this.statusLabel.setForeground(WebColors.DarkGreen);
                                this.statusLabel.setText("Valid");
                            }
                        }
                    }
                }
            } catch (final SQLParserException e) {
                final int offset = e.getErrorPosition();
                setInvalidMessage(offset - this.sqlPrefix.length(), "Error parsing SQL: " + e.getMessage());
            } catch (final StandardException e) {
                Logs.error(this, "Error parsing SQL: " + whereClause, e);
            }
        } else {
            setFieldValue(Condition.ALL);
            this.statusLabel.setForeground(WebColors.DarkGreen);
            this.statusLabel.setText("Valid");
        }
    } finally {
        if (!this.valid) {
            setFieldValue(null);
        }
        this.validating = false;
    }
}
Also used : Condition(com.revolsys.record.query.Condition) StandardException(com.akiban.sql.StandardException) SQLParser(com.akiban.sql.parser.SQLParser) SelectNode(com.akiban.sql.parser.SelectNode) ValueNode(com.akiban.sql.parser.ValueNode) SQLParserException(com.akiban.sql.parser.SQLParserException) StatementNode(com.akiban.sql.parser.StatementNode) QueryValue(com.revolsys.record.query.QueryValue) CursorNode(com.akiban.sql.parser.CursorNode) ResultSetNode(com.akiban.sql.parser.ResultSetNode)

Aggregations

CursorNode (com.akiban.sql.parser.CursorNode)2 ResultSetNode (com.akiban.sql.parser.ResultSetNode)2 SQLParser (com.akiban.sql.parser.SQLParser)2 SelectNode (com.akiban.sql.parser.SelectNode)2 StatementNode (com.akiban.sql.parser.StatementNode)2 ValueNode (com.akiban.sql.parser.ValueNode)2 StandardException (com.akiban.sql.StandardException)1 JavaToSQLValueNode (com.akiban.sql.parser.JavaToSQLValueNode)1 JavaValueNode (com.akiban.sql.parser.JavaValueNode)1 SQLParserException (com.akiban.sql.parser.SQLParserException)1 SQLToJavaValueNode (com.akiban.sql.parser.SQLToJavaValueNode)1 Condition (com.revolsys.record.query.Condition)1 QueryValue (com.revolsys.record.query.QueryValue)1