Search in sources :

Example 16 with SQLServerSchemaStatVisitor

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

the class SQLServerCreateTableTest method test_0.

public void test_0() throws Exception {
    String sql = // 
    "CREATE VIEW [Current Product List] AS\n" + // 
    "SELECT ProductID,ProductName\n" + // 
    "FROM Products\n" + "WHERE Discontinued=No";
    SQLServerStatementParser parser = new SQLServerStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLCreateViewStatement stmt = (SQLCreateViewStatement) 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(4, visitor.getColumns().size());
    Assert.assertEquals(2, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("Products")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("Products", "ProductID")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("Products", "ProductName")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("Products", "Discontinued")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("Products", "No")));
}
Also used : SQLServerSchemaStatVisitor(com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor) SQLServerStatementParser(com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser) Column(com.alibaba.druid.stat.TableStat.Column) SQLCreateViewStatement(com.alibaba.druid.sql.ast.statement.SQLCreateViewStatement) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 17 with SQLServerSchemaStatVisitor

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

the class SQLServerCreateTableTest_7 method test_0.

public void test_0() throws Exception {
    String sql = "create table leave_jpa (" + " id bigint identity not null, " + " user_id varchar(255), " + " primary key (id)" + ")";
    SQLServerStatementParser parser = new SQLServerStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLCreateTableStatement stmt = (SQLCreateTableStatement) statementList.get(0);
    Assert.assertEquals(1, statementList.size());
    String output = SQLUtils.toSQLString(stmt, JdbcConstants.SQL_SERVER);
    Assert.assertEquals("CREATE TABLE leave_jpa (" + "\n\tid bigint DEFAULT NULL IDENTITY," + "\n\tuser_id varchar(255)," + "\n\tPRIMARY KEY (id)" + "\n)", output);
    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(2, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("leave_jpa")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("leave_jpa", "id")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("leave_jpa", "user_id")));
}
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) Column(com.alibaba.druid.stat.TableStat.Column) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 18 with SQLServerSchemaStatVisitor

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

the class SQLServerCreateTableTest_9 method test_0.

public void test_0() throws Exception {
    String sql = "CREATE TABLE [dbo].[users2](\n" + "\t[id] [bigint] IDENTITY(1,1) NOT NULL,\n" + "\t[email] [varchar](255) NOT NULL,\n" + "\t[name] [varchar](255) NOT NULL,\n" + "PRIMARY KEY CLUSTERED \n" + "(\n" + "\t[id] ASC\n" + ")\n" + ")";
    SQLServerStatementParser parser = new SQLServerStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLCreateTableStatement stmt = (SQLCreateTableStatement) statementList.get(0);
    Assert.assertEquals(1, statementList.size());
    String output = SQLUtils.toSQLString(stmt, JdbcConstants.SQL_SERVER);
    Assert.assertEquals("CREATE TABLE [dbo].[users2] (\n" + "\t[id] [bigint] DEFAULT NULL IDENTITY (1, 1),\n" + "\t[email] [varchar](255) NOT NULL,\n" + "\t[name] [varchar](255) NOT NULL,\n" + "\tPRIMARY KEY CLUSTERED ([id] ASC)\n" + ")", output);
    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(3, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("dbo.users2")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("dbo.users2", "id")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("dbo.users2", "email")));
}
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) Column(com.alibaba.druid.stat.TableStat.Column) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 19 with SQLServerSchemaStatVisitor

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

the class SQLServerAlterTableTest_5 method test_alter_first.

public void test_alter_first() throws Exception {
    String sql = "ALTER TABLE project_measures DROP COLUMN diff_value_1, diff_value_2, diff_value_3";
    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 project_measures" + "\n\tDROP COLUMN diff_value_1, diff_value_2, diff_value_3", output);
    Assert.assertEquals(1, visitor.getTables().size());
    Assert.assertEquals(3, 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 20 with SQLServerSchemaStatVisitor

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

the class SQLServerAlterTableTest_9 method test_alter_first.

public void test_alter_first() throws Exception {
    String sql = "ALTER TABLE dbo.doc_exy ALTER COLUMN column_a DECIMAL (5, 2) ;";
    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 dbo.doc_exy" + "\n\tALTER COLUMN column_a DECIMAL(5, 2);", 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)

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