Search in sources :

Example 6 with SQLServerInsertStatement

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

the class SQLServerInsertTest5 method test_parseCompleteValues_true.

public void test_parseCompleteValues_true() 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(true);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    SQLServerInsertStatement insertStmt = (SQLServerInsertStatement) stmt;
    Assert.assertEquals(4, 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')," + "\n('a4', 'b4')";
    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 7 with SQLServerInsertStatement

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

the class SQLServerInsertTest7 method test_0.

public void test_0() throws Exception {
    String sql = "INSERT INTO MMS_SETTLEMENT_COM(handler,handleTime,MID,MERCHANTNAME,TOTALAMT,ACTUALAMT," + "     paymentMoney,STATUS,SERIAL_NUM,REMARKS)" + "SELECT 'admin',getdate(),MID,MERCHANTNAME,SUM(CONVERT(DECIMAL(18,2),isnull(TOTALAMT,0))) " + " TOTALAMT,SUM(CONVERT(DECIMAL(18,2),isnull(ACTUALAMT,0))) ACTUALAMT,SUM(CONVERT(DECIMAL(18,2)," + " isnull(paymentMoney,0))) paymentMoney,2,126,(" + " SELECT REMARKS+'' " + " FROM MMS_SETTLEMENT_COM " + " WHERE MID=A.MID " + " FOR XML PATH('')" + ") AS REMARKS FROM MMS_SETTLEMENT_COM A WHERE ID IN (304,305,306,297,108,184) GROUP BY MID ,MERCHANTNAME";
    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(0, insertStmt.getValuesList().size());
    Assert.assertEquals(10, insertStmt.getColumns().size());
    Assert.assertEquals(1, statementList.size());
    SQLServerSchemaStatVisitor visitor = new SQLServerSchemaStatVisitor();
    stmt.accept(visitor);
    String formatSql = "INSERT INTO MMS_SETTLEMENT_COM" + "\n\t(handler, handleTime, MID, MERCHANTNAME, TOTALAMT" + "\n\t, ACTUALAMT, paymentMoney, STATUS, SERIAL_NUM, REMARKS)" + "\nSELECT 'admin', getdate(), MID, MERCHANTNAME, SUM(CONVERT(DECIMAL(18, 2), isnull(TOTALAMT, 0))) AS TOTALAMT" + "\n\t, SUM(CONVERT(DECIMAL(18, 2), isnull(ACTUALAMT, 0))) AS ACTUALAMT, SUM(CONVERT(DECIMAL(18, 2), isnull(paymentMoney, 0))) AS paymentMoney, 2, 126, (" + "\n\t\tSELECT REMARKS + ''" + "\n\t\tFROM MMS_SETTLEMENT_COM" + "\n\t\tWHERE MID = A.MID" + "\n\t\tFOR XML PATH('')" + "\n\t\t) AS REMARKS" + "\nFROM MMS_SETTLEMENT_COM A" + "\nWHERE ID IN (304, 305, 306, 297, 108, 184)" + "\nGROUP BY MID, MERCHANTNAME";
    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 8 with SQLServerInsertStatement

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

the class SQLServerInsertTest8 method test_1.

public void test_1() throws Exception {
    String sql = "INSERT TOP(5)INTO dbo.EmployeeSales  OUTPUT inserted.EmployeeID, inserted.FirstName, inserted.LastName, inserted.YearlySales SELECT sp.BusinessEntityID, c.LastName, c.FirstName, sp.SalesYTD FROM Sales.SalesPerson AS sp INNER JOIN Person.Person AS c ON sp.BusinessEntityID = c.BusinessEntityID WHERE sp.SalesYTD > 250000.00 ORDER BY sp.SalesYTD DESC;";
    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(0, insertStmt.getValuesList().size());
    Assert.assertEquals(0, insertStmt.getColumns().size());
    Assert.assertEquals(1, statementList.size());
    SQLServerSchemaStatVisitor visitor = new SQLServerSchemaStatVisitor();
    stmt.accept(visitor);
    String formatSql = //
    "INSERT TOP (5) INTO dbo.EmployeeSales" + //
    "\nOUTPUT inserted.EmployeeID, inserted.FirstName, inserted.LastName, inserted.YearlySales" + //
    "\nSELECT sp.BusinessEntityID, c.LastName, c.FirstName, sp.SalesYTD" + //
    "\nFROM Sales.SalesPerson sp" + //
    "\nINNER JOIN Person.Person c ON sp.BusinessEntityID = c.BusinessEntityID" + //
    "\nWHERE sp.SalesYTD > 250000.00" + "\nORDER BY sp.SalesYTD DESC";
    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 9 with SQLServerInsertStatement

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

the class SQLServerInsertTest8 method test_0.

public void test_0() throws Exception {
    String sql = "INSERT Production.ScrapReason OUTPUT INSERTED.ScrapReasonID, INSERTED.Name, INSERTED.ModifiedDate INTO @MyTableVar VALUES (N'Operator error', GETDATE());";
    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(2, insertStmt.getValues().getValues().size());
    Assert.assertEquals(0, insertStmt.getColumns().size());
    Assert.assertEquals(1, statementList.size());
    SQLServerSchemaStatVisitor visitor = new SQLServerSchemaStatVisitor();
    stmt.accept(visitor);
    String formatSql = //
    "INSERT INTO Production.ScrapReason" + //
    "\nOUTPUT INSERTED.ScrapReasonID, INSERTED.Name, INSERTED.ModifiedDate" + //
    "\n\tINTO @MyTableVar" + "\nVALUES (N'Operator error', GETDATE())";
    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 10 with SQLServerInsertStatement

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

the class SQLServerInsertTest9 method test_0.

public void test_0() throws Exception {
    String sql = "insert into dashboard_role_res (res_id, role_id,res_type) values (?, ? ,?) ,  (?, ? ,?)";
    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(2, insertStmt.getValuesList().size());
    Assert.assertEquals(3, insertStmt.getValues().getValues().size());
    Assert.assertEquals(3, insertStmt.getColumns().size());
    Assert.assertEquals(1, statementList.size());
    SQLServerSchemaStatVisitor visitor = new SQLServerSchemaStatVisitor();
    stmt.accept(visitor);
    String formatSql = "INSERT INTO dashboard_role_res (res_id, role_id, res_type)\n" + "VALUES (?, ?, ?),\n" + "(?, ?, ?)";
    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)

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