Search in sources :

Example 21 with SQLServerSchemaStatVisitor

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

the class SQLServerCreateIndexTest_4 method test_0.

public void test_0() throws Exception {
    String sql = "CREATE UNIQUE NONCLUSTERED INDEX ACT_IDX_EVENT_DEF_UNIQ " + "ON FLW_EVENT_DEFINITION(KEY_, VERSION_, TENANT_ID_)";
    SQLServerStatementParser parser = new SQLServerStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLCreateIndexStatement stmt = (SQLCreateIndexStatement) statementList.get(0);
    assertEquals(1, statementList.size());
    assertEquals(// 
    "CREATE UNIQUE NONCLUSTERED INDEX ACT_IDX_EVENT_DEF_UNIQ ON FLW_EVENT_DEFINITION (KEY_, VERSION_, TENANT_ID_)", SQLUtils.toSQLString(stmt, DbType.sqlserver));
    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());
    assertEquals(1, visitor.getTables().size());
    assertEquals(3, visitor.getColumns().size());
    assertEquals(0, visitor.getConditions().size());
    assertTrue(visitor.containsColumn("FLW_EVENT_DEFINITION", "KEY_"));
    assertTrue(visitor.containsColumn("FLW_EVENT_DEFINITION", "VERSION_"));
    assertTrue(visitor.containsColumn("FLW_EVENT_DEFINITION", "TENANT_ID_"));
}
Also used : SQLServerSchemaStatVisitor(com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor) SQLServerStatementParser(com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser) SQLCreateIndexStatement(com.alibaba.druid.sql.ast.statement.SQLCreateIndexStatement) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 22 with SQLServerSchemaStatVisitor

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

the class SQLServerDropIndex_Test method test_alter_first.

public void test_alter_first() throws Exception {
    String sql = "DROP INDEX async_measure_snapshots.async_m_s_snapshot_id";
    SQLServerStatementParser parser = new SQLServerStatementParser(sql);
    SQLStatement stmt = parser.parseStatementList().get(0);
    parser.match(Token.EOF);
    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());
    String output = SQLUtils.toSQLString(stmt, JdbcConstants.SQL_SERVER);
    Assert.assertEquals("DROP INDEX async_measure_snapshots.async_m_s_snapshot_id", output);
    Assert.assertEquals(0, visitor.getTables().size());
    Assert.assertEquals(0, visitor.getColumns().size());
}
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 23 with SQLServerSchemaStatVisitor

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

the class SQLServerAlterTableTest_1 method test_alter_first.

public void test_alter_first() throws Exception {
    String sql = "ALTER TABLE dependencies ALTER COLUMN id bigint";
    SQLServerStatementParser parser = new SQLServerStatementParser(sql);
    SQLStatement stmt = parser.parseStatementList().get(0);
    parser.match(Token.EOF);
    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());
    String output = SQLUtils.toSQLString(stmt, JdbcConstants.SQL_SERVER);
    Assert.assertEquals(// 
    "ALTER TABLE dependencies" + "\n\tALTER COLUMN id bigint", output);
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(1, visitor.getColumns().size());
}
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 24 with SQLServerSchemaStatVisitor

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

the class SQLServerDropTableTest method test_alter_first.

public void test_alter_first() throws Exception {
    String sql = "DROP TABLE IF EXISTS test";
    SQLServerStatementParser parser = new SQLServerStatementParser(sql);
    SQLStatement stmt = parser.parseStatementList().get(0);
    parser.match(Token.EOF);
    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());
    String output = SQLUtils.toSQLString(stmt, JdbcConstants.SQL_SERVER);
    assertEquals("DROP TABLE IF EXISTS test", output);
    assertEquals(1, visitor.getTables().size());
    assertEquals(0, visitor.getColumns().size());
}
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 25 with SQLServerSchemaStatVisitor

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

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