Search in sources :

Example 1 with SQLServerInsertStatement

use of com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerInsertStatement in project druid by alibaba.

the class SQLServerStatementParser method parseInsert0.

protected void parseInsert0(SQLInsertInto insert, boolean acceptSubQuery) {
    SQLServerInsertStatement insertStatement = (SQLServerInsertStatement) insert;
    SQLServerTop top = this.getExprParser().parseTop();
    if (top != null) {
        insertStatement.setTop(top);
    }
    if (lexer.token() == Token.INTO) {
        lexer.nextToken();
    }
    SQLName tableName = this.exprParser.name();
    insertStatement.setTableName(tableName);
    if (lexer.token() == Token.LITERAL_ALIAS) {
        insertStatement.setAlias(as());
    }
    parseInsert0_hinits(insertStatement);
    if (lexer.token() == Token.IDENTIFIER && !lexer.stringVal().equalsIgnoreCase("OUTPUT")) {
        insertStatement.setAlias(lexer.stringVal());
        lexer.nextToken();
    }
    if (lexer.token() == (Token.LPAREN)) {
        lexer.nextToken();
        this.exprParser.exprList(insertStatement.getColumns(), insertStatement);
        accept(Token.RPAREN);
    }
    SQLServerOutput output = this.getExprParser().parserOutput();
    if (output != null) {
        insertStatement.setOutput(output);
    }
    if (lexer.token() == Token.VALUES) {
        lexer.nextToken();
        for (; ; ) {
            accept(Token.LPAREN);
            SQLInsertStatement.ValuesClause values = new SQLInsertStatement.ValuesClause();
            this.exprParser.exprList(values.getValues(), values);
            insertStatement.getValuesList().add(values);
            accept(Token.RPAREN);
            if (!parseCompleteValues && insertStatement.getValuesList().size() >= parseValuesSize) {
                lexer.skipToEOF();
                break;
            }
            if (lexer.token() == Token.COMMA) {
                lexer.nextToken();
                continue;
            } else {
                break;
            }
        }
    } else if (acceptSubQuery && (lexer.token() == Token.SELECT || lexer.token() == Token.LPAREN)) {
        SQLQueryExpr queryExpr = (SQLQueryExpr) this.exprParser.expr();
        insertStatement.setQuery(queryExpr.getSubQuery());
    } else if (lexer.token() == Token.DEFAULT) {
        lexer.nextToken();
        accept(Token.VALUES);
        insertStatement.setDefaultValues(true);
    }
}
Also used : SQLServerInsertStatement(com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerInsertStatement) SQLServerTop(com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop) SQLQueryExpr(com.alibaba.druid.sql.ast.expr.SQLQueryExpr) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLServerOutput(com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerOutput)

Example 2 with SQLServerInsertStatement

use of com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerInsertStatement in project druid by alibaba.

the class WallVisitorUtils method checkInsertForMultiTenant.

private static void checkInsertForMultiTenant(WallVisitor visitor, SQLInsertInto x) {
    TenantCallBack tenantCallBack = visitor.getConfig().getTenantCallBack();
    String tenantTablePattern = visitor.getConfig().getTenantTablePattern();
    if (tenantCallBack == null && (tenantTablePattern == null || tenantTablePattern.length() == 0)) {
        return;
    }
    if (x == null) {
        throw new IllegalStateException("x is null");
    }
    SQLExprTableSource tableSource = x.getTableSource();
    String alias = null;
    String matchTableName = null;
    String tenantColumn = null;
    SQLExpr tableExpr = tableSource.getExpr();
    if (tableExpr instanceof SQLIdentifierExpr) {
        String tableName = ((SQLIdentifierExpr) tableExpr).getName();
        if (tenantCallBack != null) {
            tenantColumn = tenantCallBack.getTenantColumn(StatementType.INSERT, tableName);
        }
        if (StringUtils.isEmpty(tenantColumn) && ServletPathMatcher.getInstance().matches(tenantTablePattern, tableName)) {
            tenantColumn = visitor.getConfig().getTenantColumn();
        }
        if (!StringUtils.isEmpty(tenantColumn)) {
            matchTableName = tableName;
            alias = tableSource.getAlias();
        }
    }
    if (matchTableName == null) {
        return;
    }
    SQLExpr item = null;
    if (alias != null) {
        item = new SQLPropertyExpr(new SQLIdentifierExpr(alias), tenantColumn);
    } else {
        item = new SQLIdentifierExpr(tenantColumn);
    }
    SQLExpr value = generateTenantValue(visitor, alias, StatementType.INSERT, matchTableName);
    // add insert item and value
    x.getColumns().add(item);
    List<ValuesClause> valuesClauses = null;
    ValuesClause valuesClause = null;
    if (x instanceof MySqlInsertStatement) {
        valuesClauses = ((MySqlInsertStatement) x).getValuesList();
    } else if (x instanceof SQLServerInsertStatement) {
        valuesClauses = ((MySqlInsertStatement) x).getValuesList();
    } else {
        valuesClause = x.getValues();
    }
    if (valuesClauses != null && valuesClauses.size() > 0) {
        for (ValuesClause clause : valuesClauses) {
            clause.addValue(value);
        }
    }
    if (valuesClause != null) {
        valuesClause.addValue(value);
    }
    // insert .. select
    SQLSelect select = x.getQuery();
    if (select != null) {
        List<SQLSelectQueryBlock> queryBlocks = splitSQLSelectQuery(select.getQuery());
        for (SQLSelectQueryBlock queryBlock : queryBlocks) {
            queryBlock.getSelectList().add(new SQLSelectItem(value));
        }
    }
    visitor.setSqlModified(true);
}
Also used : TenantCallBack(com.alibaba.druid.wall.WallConfig.TenantCallBack) SQLServerInsertStatement(com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerInsertStatement) SQLIdentifierExpr(com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr) MySqlInsertStatement(com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement) SQLPropertyExpr(com.alibaba.druid.sql.ast.expr.SQLPropertyExpr) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) ValuesClause(com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause)

Example 3 with SQLServerInsertStatement

use of com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerInsertStatement in project druid by alibaba.

the class SQLServerInsertTest5 method test_parseCompleteValues_false.

public void test_parseCompleteValues_false() throws Exception {
    String sql = "insert into t(a,b) values ('a1','b1'),('a2','b2'),('a3','b3'),('a4','b4');";
    SQLServerStatementParser parser = new SQLServerStatementParser(sql);
    parser.setParseCompleteValues(false);
    parser.setParseValuesSize(3);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    SQLServerInsertStatement insertStmt = (SQLServerInsertStatement) stmt;
    Assert.assertEquals(3, insertStmt.getValuesList().size());
    Assert.assertEquals(2, insertStmt.getValues().getValues().size());
    Assert.assertEquals(2, insertStmt.getColumns().size());
    Assert.assertEquals(1, statementList.size());
    SQLServerSchemaStatVisitor visitor = new SQLServerSchemaStatVisitor();
    stmt.accept(visitor);
    String formatSql = //
    "INSERT INTO t (a, b)" + //
    "\nVALUES ('a1', 'b1')," + //
    "\n('a2', 'b2')," + "\n('a3', 'b3')";
    Assert.assertEquals(formatSql, SQLUtils.toSQLServerString(insertStmt));
}
Also used : SQLServerSchemaStatVisitor(com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor) SQLServerInsertStatement(com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerInsertStatement) SQLServerStatementParser(com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 4 with SQLServerInsertStatement

use of com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerInsertStatement in project druid by alibaba.

the class SQLServerInsertTest6 method test.

public void test() throws Exception {
    String sql = "INSERT [dbo].[SurveyAnswer]([CustomerId], [QuestionId], [OptionId], [CreateTime], [LastUpdateTime]) VALUES (@0, @1, @2, @3, @4)";
    SQLServerStatementParser parser = new SQLServerStatementParser(sql);
    parser.setParseCompleteValues(false);
    parser.setParseValuesSize(3);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    SQLServerInsertStatement insertStmt = (SQLServerInsertStatement) stmt;
    Assert.assertEquals(1, insertStmt.getValuesList().size());
    Assert.assertEquals(5, insertStmt.getValues().getValues().size());
    Assert.assertEquals(5, insertStmt.getColumns().size());
    Assert.assertEquals(1, statementList.size());
    SQLServerSchemaStatVisitor visitor = new SQLServerSchemaStatVisitor();
    stmt.accept(visitor);
    String formatSql = //
    "INSERT INTO [dbo].[SurveyAnswer] ([CustomerId], [QuestionId], [OptionId], [CreateTime], [LastUpdateTime])" + "\nVALUES (@0, @1, @2, @3, @4)";
    Assert.assertEquals(formatSql, SQLUtils.toSQLServerString(insertStmt));
}
Also used : SQLServerSchemaStatVisitor(com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor) SQLServerInsertStatement(com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerInsertStatement) SQLServerStatementParser(com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 5 with SQLServerInsertStatement

use of com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerInsertStatement in project druid by alibaba.

the class SQLServerStatementParser method parseInsert.

public SQLStatement parseInsert() {
    SQLServerInsertStatement insertStatement = new SQLServerInsertStatement();
    if (lexer.token() == Token.INSERT) {
        accept(Token.INSERT);
    }
    parseInsert0(insertStatement);
    return insertStatement;
}
Also used : SQLServerInsertStatement(com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerInsertStatement)

Aggregations

SQLServerInsertStatement (com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerInsertStatement)10 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)7 SQLServerStatementParser (com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser)7 SQLServerSchemaStatVisitor (com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor)7 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)1 SQLName (com.alibaba.druid.sql.ast.SQLName)1 SQLIdentifierExpr (com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr)1 SQLPropertyExpr (com.alibaba.druid.sql.ast.expr.SQLPropertyExpr)1 SQLQueryExpr (com.alibaba.druid.sql.ast.expr.SQLQueryExpr)1 ValuesClause (com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause)1 MySqlInsertStatement (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlInsertStatement)1 SQLServerOutput (com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerOutput)1 SQLServerTop (com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerTop)1 TenantCallBack (com.alibaba.druid.wall.WallConfig.TenantCallBack)1