Search in sources :

Example 41 with SQLServerStatementParser

use of com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser in project druid by alibaba.

the class SQLServerCreateTableTest_1 method test_0.

public void test_0() throws Exception {
    String sql = // 
    "CREATE TABLE [projects] (" + // 
    "[id] int NOT NULL IDENTITY(1, 1) PRIMARY KEY, " + // 
    "[name] NVARCHAR(256), [description] NVARCHAR(2000), " + // 
    "[enabled] bit DEFAULT 1 NOT NULL, " + // 
    "[scope] NVARCHAR(3), " + // 
    "[qualifier] NVARCHAR(3), " + // 
    "[kee] NVARCHAR(400), " + // 
    "[root_id] int, " + // 
    "[profile_id] int, " + // 
    "[language] NVARCHAR(5), " + // 
    "[copy_resource_id] int, " + "[long_name] NVARCHAR(256)) ";
    SQLServerStatementParser parser = new SQLServerStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLCreateTableStatement stmt = (SQLCreateTableStatement) statementList.get(0);
    Assert.assertEquals(1, statementList.size());
    SQLServerSchemaStatVisitor visitor = new SQLServerSchemaStatVisitor();
    stmt.accept(visitor);
    System.out.println("Tables : " + visitor.getTables());
    // System.out.println("fields : " + visitor.getColumns());
    // System.out.println("coditions : " + visitor.getConditions());
    // System.out.println("orderBy : " + visitor.getOrderByColumns());
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(12, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("projects")));
    Assert.assertTrue(visitor.containsColumn("projects", "id"));
    Assert.assertTrue(visitor.containsColumn("projects", "name"));
    Assert.assertTrue(visitor.containsColumn("projects", "enabled"));
    Assert.assertTrue(visitor.containsColumn("projects", "scope"));
}
Also used : SQLServerSchemaStatVisitor(com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor) SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) SQLServerStatementParser(com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 42 with SQLServerStatementParser

use of com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser in project druid by alibaba.

the class SQLServerUpdateTest3 method test_update.

public void test_update() throws Exception {
    String sql = // 
    "UPDATE Sales.SalesPerson " + // 
    "SET SalesYTD = SalesYTD + " + // 
    "    (SELECT SUM(so.SubTotal) " + // 
    "     FROM Sales.SalesOrderHeader AS so" + // 
    "     WHERE so.OrderDate = (SELECT MAX(OrderDate)" + // 
    "                           FROM Sales.SalesOrderHeader AS so2" + // 
    "                           WHERE so2.SalesPersonID = " + // 
    "                                 so.SalesPersonID)" + // 
    "     AND Sales.SalesPerson.SalesPersonID = so.SalesPersonID" + "     GROUP BY so.SalesPersonID);";
    String expect = "UPDATE Sales.SalesPerson" + "\nSET SalesYTD = SalesYTD + (" + "\n\tSELECT SUM(so.SubTotal)" + "\n\tFROM Sales.SalesOrderHeader so" + "\n\tWHERE so.OrderDate = (" + "\n\t\t\tSELECT MAX(OrderDate)" + "\n\t\t\tFROM Sales.SalesOrderHeader so2" + "\n\t\t\tWHERE so2.SalesPersonID = so.SalesPersonID" + "\n\t\t)" + "\n\t\tAND Sales.SalesPerson.SalesPersonID = so.SalesPersonID" + "\n\tGROUP BY so.SalesPersonID" + "\n);";
    SQLServerStatementParser parser = new SQLServerStatementParser(sql);
    SQLStatement stmt = parser.parseStatementList().get(0);
    String text = TestUtils.outputSqlServer(stmt);
    Assert.assertEquals(expect, text);
// System.out.println(text);
}
Also used : SQLServerStatementParser(com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 43 with SQLServerStatementParser

use of com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser in project druid by alibaba.

the class SQLServerUpdateTest5 method test_update.

public void test_update() throws Exception {
    String sql = "UPDATE TOP (10) HumanResources.Employee SET VacationHours = VacationHours * 1.25, ModifiedDate = GETDATE()  OUTPUT inserted.BusinessEntityID, deleted.VacationHours, inserted.VacationHours, inserted.ModifiedDate INTO @MyTableVar;";
    String expect = "UPDATE TOP (10) HumanResources.Employee" + "\nSET VacationHours = VacationHours * 1.25, ModifiedDate = GETDATE()" + "\nOUTPUT inserted.BusinessEntityID, deleted.VacationHours, inserted.VacationHours, inserted.ModifiedDate" + "\n\tINTO @MyTableVar;";
    SQLServerStatementParser parser = new SQLServerStatementParser(sql);
    SQLStatement stmt = parser.parseStatementList().get(0);
    String text = TestUtils.outputSqlServer(stmt);
    Assert.assertEquals(expect, text);
// System.out.println(text);
}
Also used : SQLServerStatementParser(com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 44 with SQLServerStatementParser

use of com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser in project druid by alibaba.

the class SQLServerWaitForTest method test_1.

public void test_1() {
    String sql = "WAITFOR DELAY '02:00';";
    SQLServerStatementParser parser = new SQLServerStatementParser(sql);
    List<SQLStatement> stmtList = parser.parseStatementList();
    Assert.assertEquals(1, stmtList.size());
    String text = SQLUtils.toSQLString(stmtList, JdbcUtils.SQL_SERVER);
    Assert.assertEquals("WAITFOR DELAY '02:00';", text);
}
Also used : SQLServerStatementParser(com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 45 with SQLServerStatementParser

use of com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser in project druid by alibaba.

the class SQLServerWaitForTest method test_0.

public void test_0() {
    String sql = "WAITFOR TIME '22:20';";
    SQLServerStatementParser parser = new SQLServerStatementParser(sql);
    List<SQLStatement> stmtList = parser.parseStatementList();
    Assert.assertEquals(1, stmtList.size());
    String text = SQLUtils.toSQLString(stmtList, JdbcUtils.SQL_SERVER);
    Assert.assertEquals("WAITFOR TIME '22:20';", text);
}
Also used : SQLServerStatementParser(com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Aggregations

SQLServerStatementParser (com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser)152 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)145 SQLServerSchemaStatVisitor (com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor)64 Column (com.alibaba.druid.stat.TableStat.Column)21 SQLCreateTableStatement (com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement)17 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)9 SQLServerSelectQueryBlock (com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock)8 SQLServerInsertStatement (com.alibaba.druid.sql.dialect.sqlserver.ast.stmt.SQLServerInsertStatement)8 SQLCreateIndexStatement (com.alibaba.druid.sql.ast.statement.SQLCreateIndexStatement)4 SQLSelect (com.alibaba.druid.sql.ast.statement.SQLSelect)4 SQLOrderBy (com.alibaba.druid.sql.ast.SQLOrderBy)2 SQLCreateViewStatement (com.alibaba.druid.sql.ast.statement.SQLCreateViewStatement)2 SQLSelectQuery (com.alibaba.druid.sql.ast.statement.SQLSelectQuery)2 DB2StatementParser (com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser)2 MySqlSelectQueryBlock (com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlSelectQueryBlock)2 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)2 OracleStatementParser (com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser)2 PGSelectQueryBlock (com.alibaba.druid.sql.dialect.postgresql.ast.stmt.PGSelectQueryBlock)2 PGSQLStatementParser (com.alibaba.druid.sql.dialect.postgresql.parser.PGSQLStatementParser)2 SQLMergeStatement (com.alibaba.druid.sql.ast.statement.SQLMergeStatement)1