Search in sources :

Example 36 with SQLServerStatementParser

use of com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser 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 37 with SQLServerStatementParser

use of com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser 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 38 with SQLServerStatementParser

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

the class SQLServerUpdateTest4 method test_update.

public void test_update() throws Exception {
    String sql = "UPDATE TOP (10) HumanResources.Employee " + "SET VacationHours = VacationHours * 1.25 ;";
    String expect = "UPDATE TOP (10) HumanResources.Employee" + "\nSET VacationHours = VacationHours * 1.25;";
    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 39 with SQLServerStatementParser

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

the class SQLServerUpdateTest6 method test_update.

public void test_update() throws Exception {
    String sql = "update clients set name =N'小明',age =1";
    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);
    assertEquals("UPDATE clients\n" + "SET name = N'小明', age = 1", stmt.toString());
}
Also used : SQLServerStatementParser(com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 40 with SQLServerStatementParser

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

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