Search in sources :

Example 11 with SQLServerSchemaStatVisitor

use of com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor in project druid by alibaba.

the class SQLServerSelectTest_cross_apply method test_0.

public void test_0() throws Exception {
    String sql = "SELECT DeptID, DeptName, DeptMgrID, EmpID, EmpLastName, EmpSalary FROM Departments d CROSS APPLY dbo.GetReports(d.DeptMgrID)";
    SQLServerStatementParser parser = new SQLServerStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    SQLSelectStatement selectStmt = (SQLSelectStatement) stmt;
    SQLSelect select = selectStmt.getSelect();
    Assert.assertNotNull(select.getQuery());
    SQLServerSelectQueryBlock queryBlock = (SQLServerSelectQueryBlock) select.getQuery();
    Assert.assertNull(queryBlock.getGroupBy());
    String fomatSQL = SQLUtils.toSQLString(statementList, JdbcUtils.SQL_SERVER);
    // System.out.println(fomatSQL);
    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(7, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertEquals(0, visitor.getOrderByColumns().size());
    String expected = // 
    "SELECT DeptID, DeptName, DeptMgrID, EmpID, EmpLastName" + // 
    "\n\t, EmpSalary" + // 
    "\nFROM Departments d" + "\n\tCROSS APPLY dbo.GetReports(d.DeptMgrID)";
    Assert.assertEquals(expected, fomatSQL);
}
Also used : SQLServerSchemaStatVisitor(com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor) SQLServerStatementParser(com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser) SQLSelect(com.alibaba.druid.sql.ast.statement.SQLSelect) SQLServerSelectQueryBlock(com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock) SQLSelectStatement(com.alibaba.druid.sql.ast.statement.SQLSelectStatement) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 12 with SQLServerSchemaStatVisitor

use of com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor in project druid by alibaba.

the class SQLServerSetTest method test_0.

public void test_0() {
    String sql = "SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED";
    SQLServerStatementParser parser = new SQLServerStatementParser(sql);
    List<SQLStatement> stmtList = parser.parseStatementList();
    SQLStatement stmt = stmtList.get(0);
    Assert.assertEquals(1, stmtList.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(0, visitor.getTables().size());
    Assert.assertEquals(0, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    String text = SQLUtils.toSQLString(stmtList, JdbcUtils.SQL_SERVER);
    Assert.assertEquals("SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED", text);
}
Also used : SQLServerSchemaStatVisitor(com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor) SQLServerStatementParser(com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 13 with SQLServerSchemaStatVisitor

use of com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor in project druid by alibaba.

the class SQLServerSetTest method test_1.

public void test_1() {
    String sql = "SET FMTONLY ON";
    SQLServerStatementParser parser = new SQLServerStatementParser(sql);
    List<SQLStatement> stmtList = parser.parseStatementList();
    SQLStatement stmt = stmtList.get(0);
    Assert.assertEquals(1, stmtList.size());
    SQLServerSchemaStatVisitor visitor = new SQLServerSchemaStatVisitor();
    stmt.accept(visitor);
    Assert.assertEquals(0, visitor.getTables().size());
    Assert.assertEquals(0, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    String text = SQLUtils.toSQLString(stmtList, JdbcUtils.SQL_SERVER);
    Assert.assertEquals("SET FMTONLY ON", text);
}
Also used : SQLServerSchemaStatVisitor(com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor) SQLServerStatementParser(com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 14 with SQLServerSchemaStatVisitor

use of com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor in project druid by alibaba.

the class SqlServerResourceTest method exec_test.

public void exec_test(String resource) throws Exception {
    // System.out.println(resource);
    InputStream is = null;
    is = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
    Reader reader = new InputStreamReader(is, "UTF-8");
    String input = Utils.read(reader);
    JdbcUtils.close(reader);
    String[] items = input.split("---------------------------");
    String sql = items[0].trim();
    String expect = null;
    if (items.length > 1) {
        expect = items[1].trim();
        expect = expect.replaceAll("\\r\\n", "\n");
    }
    SQLStatementParser parser = new SQLServerStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLStatement stmt = statementList.get(0);
    Assert.assertEquals(1, statementList.size());
    SchemaStatVisitor visitor = new SQLServerSchemaStatVisitor();
    stmt.accept(visitor);
    System.out.println(stmt.toString());
    if (expect != null && !expect.isEmpty()) {
        assertEquals(expect, stmt.toString());
    }
// System.out.println(sql);
// System.out.println("Tables : " + visitor.getTables());
// System.out.println("fields : " + visitor.getColumns());
// 
// System.out.println();
// System.out.println("---------------------------");
// System.out.println(SQLUtils.toOdpsString(stmt));
}
Also used : SQLServerSchemaStatVisitor(com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor) InputStreamReader(java.io.InputStreamReader) SQLStatementParser(com.alibaba.druid.sql.parser.SQLStatementParser) SQLServerStatementParser(com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser) InputStream(java.io.InputStream) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) SchemaStatVisitor(com.alibaba.druid.sql.visitor.SchemaStatVisitor) SQLServerSchemaStatVisitor(com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor)

Example 15 with SQLServerSchemaStatVisitor

use of com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor 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)

Aggregations

SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)65 SQLServerSchemaStatVisitor (com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor)65 SQLServerStatementParser (com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser)64 Column (com.alibaba.druid.stat.TableStat.Column)21 SQLCreateTableStatement (com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement)17 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 SQLSelectStatement (com.alibaba.druid.sql.ast.statement.SQLSelectStatement)4 SQLServerSelectQueryBlock (com.alibaba.druid.sql.dialect.sqlserver.ast.SQLServerSelectQueryBlock)4 SQLCreateViewStatement (com.alibaba.druid.sql.ast.statement.SQLCreateViewStatement)2 SQLMergeStatement (com.alibaba.druid.sql.ast.statement.SQLMergeStatement)1 SQLStatementParser (com.alibaba.druid.sql.parser.SQLStatementParser)1 SchemaStatVisitor (com.alibaba.druid.sql.visitor.SchemaStatVisitor)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1