Search in sources :

Example 1 with SQLNullExpr

use of com.alibaba.druid.sql.ast.expr.SQLNullExpr in project druid by alibaba.

the class MySqlMockExecuteHandlerImpl method executeQueryFromDual.

public ResultSet executeQueryFromDual(MockStatementBase statement, SQLSelectQueryBlock query) throws SQLException {
    MockResultSet rs = statement.getConnection().getDriver().createMockResultSet(statement);
    MockResultSetMetaData metaData = rs.getMockMetaData();
    Object[] row = new Object[query.getSelectList().size()];
    for (int i = 0, size = query.getSelectList().size(); i < size; ++i) {
        ColumnMetaData column = new ColumnMetaData();
        SQLSelectItem item = query.getSelectList().get(i);
        SQLExpr expr = item.getExpr();
        if (expr instanceof SQLIntegerExpr) {
            row[i] = ((SQLNumericLiteralExpr) expr).getNumber();
            column.setColumnType(Types.INTEGER);
        } else if (expr instanceof SQLNumberExpr) {
            row[i] = ((SQLNumericLiteralExpr) expr).getNumber();
            column.setColumnType(Types.DECIMAL);
        } else if (expr instanceof SQLCharExpr) {
            row[i] = ((SQLCharExpr) expr).getText();
            column.setColumnType(Types.VARCHAR);
        } else if (expr instanceof SQLNCharExpr) {
            row[i] = ((SQLNCharExpr) expr).getText();
            column.setColumnType(Types.NVARCHAR);
        } else if (expr instanceof SQLBooleanExpr) {
            row[i] = ((SQLBooleanExpr) expr).getValue();
            column.setColumnType(Types.NVARCHAR);
        } else if (expr instanceof SQLNullExpr) {
            row[i] = null;
        } else if (expr instanceof SQLMethodInvokeExpr) {
            SQLMethodInvokeExpr methodInvokeExpr = (SQLMethodInvokeExpr) expr;
            if ("NOW".equalsIgnoreCase(methodInvokeExpr.getMethodName())) {
                row[i] = new Timestamp(System.currentTimeMillis());
            } else {
                throw new SQLException("TODO");
            }
        } else if (expr instanceof SQLVariantRefExpr) {
            SQLVariantRefExpr varExpr = (SQLVariantRefExpr) expr;
            int varIndex = varExpr.getIndex();
            if (statement instanceof MockPreparedStatement) {
                MockPreparedStatement mockPstmt = (MockPreparedStatement) statement;
                row[i] = mockPstmt.getParameters().get(varIndex);
            } else {
                row[i] = null;
            }
        } else {
            row[i] = null;
        }
        metaData.getColumns().add(column);
    }
    rs.getRows().add(row);
    return rs;
}
Also used : SQLCharExpr(com.alibaba.druid.sql.ast.expr.SQLCharExpr) MockResultSetMetaData(com.alibaba.druid.mock.MockResultSetMetaData) SQLMethodInvokeExpr(com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr) SQLException(java.sql.SQLException) SQLNCharExpr(com.alibaba.druid.sql.ast.expr.SQLNCharExpr) MockResultSet(com.alibaba.druid.mock.MockResultSet) MockPreparedStatement(com.alibaba.druid.mock.MockPreparedStatement) SQLNumberExpr(com.alibaba.druid.sql.ast.expr.SQLNumberExpr) SQLNullExpr(com.alibaba.druid.sql.ast.expr.SQLNullExpr) Timestamp(java.sql.Timestamp) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLNumericLiteralExpr(com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr) SQLBooleanExpr(com.alibaba.druid.sql.ast.expr.SQLBooleanExpr) SQLSelectItem(com.alibaba.druid.sql.ast.statement.SQLSelectItem) SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLVariantRefExpr(com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr) ColumnMetaData(com.alibaba.druid.util.jdbc.ResultSetMetaDataBase.ColumnMetaData)

Example 2 with SQLNullExpr

use of com.alibaba.druid.sql.ast.expr.SQLNullExpr in project druid by alibaba.

the class SQLServerExprParser method parseColumnRest.

public SQLColumnDefinition parseColumnRest(SQLColumnDefinition column) {
    if (lexer.token() == Token.IDENTITY) {
        lexer.nextToken();
        SQLColumnDefinition.Identity identity = new SQLColumnDefinition.Identity();
        if (lexer.token() == Token.LPAREN) {
            lexer.nextToken();
            SQLIntegerExpr seed = (SQLIntegerExpr) this.primary();
            accept(Token.COMMA);
            SQLIntegerExpr increment = (SQLIntegerExpr) this.primary();
            accept(Token.RPAREN);
            identity.setSeed((Integer) seed.getNumber());
            identity.setIncrement((Integer) increment.getNumber());
        }
        if (lexer.token() == Token.NOT) {
            lexer.nextToken();
            if (lexer.token() == Token.NULL) {
                lexer.nextToken();
                column.setDefaultExpr(new SQLNullExpr());
            } else {
                accept(Token.FOR);
                identifierEquals("REPLICATION ");
                identity.setNotForReplication(true);
            }
        }
        column.setIdentity(identity);
    }
    return super.parseColumnRest(column);
}
Also used : SQLIntegerExpr(com.alibaba.druid.sql.ast.expr.SQLIntegerExpr) SQLNullExpr(com.alibaba.druid.sql.ast.expr.SQLNullExpr) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)

Example 3 with SQLNullExpr

use of com.alibaba.druid.sql.ast.expr.SQLNullExpr in project druid by alibaba.

the class OracleStatementParser method parseStatementList.

public void parseStatementList(List<SQLStatement> statementList, int max) {
    for (; ; ) {
        if (max != -1) {
            if (statementList.size() >= max) {
                return;
            }
        }
        if (lexer.token() == Token.EOF) {
            return;
        }
        if (lexer.token() == Token.END) {
            return;
        }
        if (lexer.token() == Token.ELSE) {
            return;
        }
        if (lexer.token() == (Token.SEMI)) {
            lexer.nextToken();
            continue;
        }
        if (lexer.token() == (Token.SELECT)) {
            SQLSelectStatement stmt = new SQLSelectStatement(new OracleSelectParser(this.exprParser).select(), JdbcConstants.ORACLE);
            statementList.add(stmt);
            continue;
        }
        if (lexer.token() == (Token.UPDATE)) {
            statementList.add(parseUpdateStatement());
            continue;
        }
        if (lexer.token() == (Token.CREATE)) {
            statementList.add(parseCreate());
            continue;
        }
        if (lexer.token() == Token.INSERT) {
            statementList.add(parseInsert());
            continue;
        }
        if (lexer.token() == (Token.DELETE)) {
            statementList.add(parseDeleteStatement());
            continue;
        }
        if (lexer.token() == (Token.SLASH)) {
            lexer.nextToken();
            statementList.add(new OraclePLSQLCommitStatement());
            continue;
        }
        if (lexer.token() == Token.ALTER) {
            statementList.add(parserAlter());
            continue;
        }
        if (lexer.token() == Token.WITH) {
            statementList.add(new SQLSelectStatement(new OracleSelectParser(this.exprParser).select()));
            continue;
        }
        if (lexer.token() == Token.LBRACE || identifierEquals("CALL")) {
            statementList.add(this.parseCall());
            continue;
        }
        if (lexer.token() == Token.MERGE) {
            statementList.add(this.parseMerge());
            continue;
        }
        if (lexer.token() == Token.BEGIN) {
            statementList.add(this.parseBlock());
            continue;
        }
        if (lexer.token() == Token.DECLARE) {
            statementList.add(this.parseBlock());
            continue;
        }
        if (lexer.token() == Token.LOCK) {
            statementList.add(this.parseLock());
            continue;
        }
        if (lexer.token() == Token.TRUNCATE) {
            statementList.add(this.parseTruncate());
            continue;
        }
        if (lexer.token() == Token.VARIANT) {
            SQLExpr variant = this.exprParser.primary();
            if (variant instanceof SQLBinaryOpExpr) {
                SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) variant;
                if (binaryOpExpr.getOperator() == SQLBinaryOperator.Assignment) {
                    SQLSetStatement stmt = new SQLSetStatement(binaryOpExpr.getLeft(), binaryOpExpr.getRight(), getDbType());
                    statementList.add(stmt);
                    continue;
                }
            }
            accept(Token.COLONEQ);
            SQLExpr value = this.exprParser.expr();
            SQLSetStatement stmt = new SQLSetStatement(variant, value, getDbType());
            statementList.add(stmt);
            continue;
        }
        if (lexer.token() == Token.EXCEPTION) {
            statementList.add(this.parseException());
            continue;
        }
        if (identifierEquals("EXIT")) {
            lexer.nextToken();
            OracleExitStatement stmt = new OracleExitStatement();
            if (lexer.token() == Token.WHEN) {
                lexer.nextToken();
                stmt.setWhen(this.exprParser.expr());
            }
            statementList.add(stmt);
            continue;
        }
        if (lexer.token() == Token.FETCH || identifierEquals("FETCH")) {
            SQLStatement stmt = parseFetch();
            statementList.add(stmt);
            continue;
        }
        if (identifierEquals("ROLLBACK")) {
            SQLRollbackStatement stmt = parseRollback();
            statementList.add(stmt);
            continue;
        }
        if (lexer.token() == Token.EXPLAIN) {
            statementList.add(this.parseExplain());
            continue;
        }
        if (lexer.token() == Token.IDENTIFIER) {
            SQLExpr expr = exprParser.expr();
            OracleExprStatement stmt = new OracleExprStatement(expr);
            statementList.add(stmt);
            continue;
        }
        if (lexer.token() == Token.LPAREN) {
            char ch = lexer.current();
            int bp = lexer.bp();
            lexer.nextToken();
            if (lexer.token() == Token.SELECT) {
                lexer.reset(bp, ch, Token.LPAREN);
                statementList.add(this.parseSelect());
                continue;
            } else {
                throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
            }
        }
        if (lexer.token() == Token.SET) {
            statementList.add(this.parseSet());
            continue;
        }
        if (lexer.token() == Token.GRANT) {
            statementList.add(this.parseGrant());
            continue;
        }
        if (lexer.token() == Token.REVOKE) {
            statementList.add(this.parseRevoke());
            continue;
        }
        if (lexer.token() == Token.COMMENT) {
            statementList.add(this.parseComment());
            continue;
        }
        if (lexer.token() == Token.FOR) {
            statementList.add(this.parseFor());
            continue;
        }
        if (lexer.token() == Token.LOOP) {
            statementList.add(this.parseLoop());
            continue;
        }
        if (lexer.token() == Token.IF) {
            statementList.add(this.parseIf());
            continue;
        }
        if (lexer.token() == Token.GOTO) {
            lexer.nextToken();
            SQLName label = this.exprParser.name();
            OracleGotoStatement stmt = new OracleGotoStatement(label);
            statementList.add(stmt);
            continue;
        }
        if (lexer.token() == Token.COMMIT) {
            lexer.nextToken();
            if (identifierEquals("WORK")) {
                lexer.nextToken();
            }
            OracleCommitStatement stmt = new OracleCommitStatement();
            if (identifierEquals("WRITE")) {
                stmt.setWrite(true);
                lexer.nextToken();
                for (; ; ) {
                    if (lexer.token() == Token.WAIT) {
                        lexer.nextToken();
                        stmt.setWait(Boolean.TRUE);
                        continue;
                    } else if (lexer.token() == Token.NOWAIT) {
                        lexer.nextToken();
                        stmt.setWait(Boolean.FALSE);
                        continue;
                    } else if (lexer.token() == Token.IMMEDIATE) {
                        lexer.nextToken();
                        stmt.setImmediate(Boolean.TRUE);
                        continue;
                    } else if (identifierEquals("BATCH")) {
                        lexer.nextToken();
                        stmt.setImmediate(Boolean.FALSE);
                        continue;
                    }
                    break;
                }
            }
            statementList.add(stmt);
            continue;
        }
        if (lexer.token() == Token.SAVEPOINT) {
            lexer.nextToken();
            OracleSavePointStatement stmt = new OracleSavePointStatement();
            if (lexer.token() == Token.TO) {
                lexer.nextToken();
                stmt.setTo(this.exprParser.name());
            }
            statementList.add(stmt);
            continue;
        }
        if (lexer.token() == Token.LTLT) {
            lexer.nextToken();
            SQLName label = this.exprParser.name();
            OracleLabelStatement stmt = new OracleLabelStatement(label);
            accept(Token.GTGT);
            statementList.add(stmt);
            continue;
        }
        if (lexer.token() == Token.DROP) {
            lexer.nextToken();
            if (lexer.token() == Token.TABLE) {
                SQLDropTableStatement stmt = parseDropTable(false);
                statementList.add(stmt);
                continue;
            }
            boolean isPublic = false;
            if (identifierEquals("PUBLIC")) {
                lexer.nextToken();
                isPublic = true;
            }
            if (lexer.token() == Token.DATABASE) {
                lexer.nextToken();
                if (identifierEquals("LINK")) {
                    lexer.nextToken();
                    OracleDropDbLinkStatement stmt = new OracleDropDbLinkStatement();
                    if (isPublic) {
                        stmt.setPublic(isPublic);
                    }
                    stmt.setName(this.exprParser.name());
                    statementList.add(stmt);
                    continue;
                }
            }
            if (lexer.token() == Token.INDEX) {
                SQLStatement stmt = parseDropIndex();
                statementList.add(stmt);
                continue;
            }
            if (lexer.token() == Token.VIEW) {
                SQLStatement stmt = parseDropView(false);
                statementList.add(stmt);
                continue;
            }
            if (lexer.token() == Token.SEQUENCE) {
                SQLDropSequenceStatement stmt = parseDropSequece(false);
                statementList.add(stmt);
                continue;
            }
            if (lexer.token() == Token.TRIGGER) {
                SQLDropTriggerStatement stmt = parseDropTrigger(false);
                statementList.add(stmt);
                continue;
            }
            if (lexer.token() == Token.USER) {
                SQLDropUserStatement stmt = parseDropUser();
                statementList.add(stmt);
                continue;
            }
            if (lexer.token() == Token.PROCEDURE) {
                SQLDropProcedureStatement stmt = parseDropProcedure(false);
                statementList.add(stmt);
                continue;
            }
            throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
        }
        if (lexer.token() == Token.NULL) {
            lexer.nextToken();
            OracleExprStatement stmt = new OracleExprStatement(new SQLNullExpr());
            statementList.add(stmt);
            continue;
        }
        if (lexer.token() == Token.OPEN) {
            SQLStatement stmt = this.parseOpen();
            statementList.add(stmt);
            continue;
        }
        throw new ParserException("TODO : " + lexer.token() + " " + lexer.stringVal());
    }
}
Also used : OracleExitStatement(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleExitStatement) OracleDropDbLinkStatement(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleDropDbLinkStatement) SQLNullExpr(com.alibaba.druid.sql.ast.expr.SQLNullExpr) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) OracleCommitStatement(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCommitStatement) OracleSavePointStatement(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleSavePointStatement) OracleExprStatement(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleExprStatement) OracleLabelStatement(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleLabelStatement) SQLBinaryOpExpr(com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr) OraclePLSQLCommitStatement(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OraclePLSQLCommitStatement) ParserException(com.alibaba.druid.sql.parser.ParserException) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLHint(com.alibaba.druid.sql.ast.SQLHint) OracleConstraint(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleConstraint) OracleGotoStatement(com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleGotoStatement)

Aggregations

SQLNullExpr (com.alibaba.druid.sql.ast.expr.SQLNullExpr)3 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)2 SQLIntegerExpr (com.alibaba.druid.sql.ast.expr.SQLIntegerExpr)2 MockPreparedStatement (com.alibaba.druid.mock.MockPreparedStatement)1 MockResultSet (com.alibaba.druid.mock.MockResultSet)1 MockResultSetMetaData (com.alibaba.druid.mock.MockResultSetMetaData)1 SQLHint (com.alibaba.druid.sql.ast.SQLHint)1 SQLName (com.alibaba.druid.sql.ast.SQLName)1 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)1 SQLBooleanExpr (com.alibaba.druid.sql.ast.expr.SQLBooleanExpr)1 SQLCharExpr (com.alibaba.druid.sql.ast.expr.SQLCharExpr)1 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)1 SQLNCharExpr (com.alibaba.druid.sql.ast.expr.SQLNCharExpr)1 SQLNumberExpr (com.alibaba.druid.sql.ast.expr.SQLNumberExpr)1 SQLNumericLiteralExpr (com.alibaba.druid.sql.ast.expr.SQLNumericLiteralExpr)1 SQLVariantRefExpr (com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr)1 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)1 SQLSelectItem (com.alibaba.druid.sql.ast.statement.SQLSelectItem)1 OracleCommitStatement (com.alibaba.druid.sql.dialect.oracle.ast.stmt.OracleCommitStatement)1