Search in sources :

Example 1 with SQLServerStatementParser

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

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

the class SQLServerCreateTableTest_2 method test_0.

public void test_0() throws Exception {
    String sql = "CREATE TABLE #Test (C1 nvarchar(10), C2 nvarchar(50), C3 datetime);";
    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 #Test (" + //
    "\n\tC1 nvarchar(10)," + //
    "\n\tC2 nvarchar(50)," + //
    "\n\tC3 datetime" + "\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("#Test")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("#Test", "C1")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("#Test", "C2")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("#Test", "C3")));
}
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 3 with SQLServerStatementParser

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

the class SQLServerCreateTableTest_4 method test_0.

public void test_0() throws Exception {
    String sql = //
    "CREATE TABLE dbo.customfieldoption (" + //
    "ID NUMERIC NOT NULL, CUSTOMFIELD NUMERIC, CUSTOMFIELDCONFIG NUMERIC, " + //
    "PARENTOPTIONID NUMERIC, SEQUENCE NUMERIC, customvalue NVARCHAR(255), " + //
    "optiontype NVARCHAR(60), disabled NVARCHAR(60), " + "CONSTRAINT PK_customfieldoption 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 dbo.customfieldoption (" + //
    "\n\tID NUMERIC NOT NULL," + //
    "\n\tCUSTOMFIELD NUMERIC," + //
    "\n\tCUSTOMFIELDCONFIG NUMERIC," + //
    "\n\tPARENTOPTIONID NUMERIC," + //
    "\n\tSEQUENCE NUMERIC," + //
    "\n\tcustomvalue NVARCHAR(255)," + //
    "\n\toptiontype NVARCHAR(60)," + //
    "\n\tdisabled NVARCHAR(60)," + //
    "\n\tCONSTRAINT PK_customfieldoption PRIMARY 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(9, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("dbo.customfieldoption")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("dbo.customfieldoption", "ID")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("dbo.customfieldoption", "CUSTOMFIELD")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("dbo.customfieldoption", "CUSTOMFIELDCONFIG")));
}
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 4 with SQLServerStatementParser

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

the class SQLServerCreateTableTest_5 method test_0.

public void test_0() throws Exception {
    String sql = //
    "CREATE TABLE dbo.AO_563AEE_ACTOR_ENTITY (" + //
    "    FULL_NAME VARCHAR(255)," + //
    "    ID INTEGER IDENTITY(1,1) NOT NULL," + //
    "    PROFILE_PAGE_URI VARCHAR(767)," + //
    "    PROFILE_PICTURE_URI VARCHAR(767)," + //
    "    USERNAME VARCHAR(255)," + ")";
    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.AO_563AEE_ACTOR_ENTITY (" + //
    "\n\tFULL_NAME VARCHAR(255)," + //
    "\n\tID INTEGER DEFAULT NULL IDENTITY (1, 1)," + //
    "\n\tPROFILE_PAGE_URI VARCHAR(767)," + //
    "\n\tPROFILE_PICTURE_URI VARCHAR(767)," + //
    "\n\tUSERNAME VARCHAR(255)" + "\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(5, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("dbo.AO_563AEE_ACTOR_ENTITY")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("dbo.AO_563AEE_ACTOR_ENTITY", "FULL_NAME")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("dbo.AO_563AEE_ACTOR_ENTITY", "ID")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("dbo.AO_563AEE_ACTOR_ENTITY", "USERNAME")));
}
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 5 with SQLServerStatementParser

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

the class SQLServerCreateTableTest_6 method test_0.

public void test_0() throws Exception {
    String sql = //
    "CREATE TABLE dbo.AO_E8B6CC_ISSUE_MAPPING_V2 (" + //
    "    AUTHOR VARCHAR(255)," + //
    "    BRANCH VARCHAR(255)," + //
    "    \"DATE\" DATETIME," + //
    "    FILES_DATA NTEXT," + //
    "    ID INTEGER IDENTITY(1,1) NOT NULL," + //
    "    ISSUE_ID VARCHAR(255)," + //
    "    MESSAGE NTEXT," + //
    "    NODE VARCHAR(255)," + //
    "    PARENTS_DATA VARCHAR(255)," + //
    "    RAW_AUTHOR VARCHAR(255)," + //
    "    RAW_NODE VARCHAR(255)," + //
    "    REPOSITORY_ID INTEGER CONSTRAINT df_AO_E8B6CC_ISSUE_MAPPING_V2_REPOSITORY_ID DEFAULT 0," + //
    "    VERSION INTEGER," + //
    "CONSTRAINT pk_AO_E8B6CC_ISSUE_MAPPING_V2_ID 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 dbo.AO_E8B6CC_ISSUE_MAPPING_V2 (" + //
    "\n\tAUTHOR VARCHAR(255)," + //
    "\n\tBRANCH VARCHAR(255)," + //
    "\n\t\"DATE\" DATETIME," + //
    "\n\tFILES_DATA NTEXT," + //
    "\n\tID INTEGER DEFAULT NULL IDENTITY (1, 1)," + //
    "\n\tISSUE_ID VARCHAR(255)," + //
    "\n\tMESSAGE NTEXT," + //
    "\n\tNODE VARCHAR(255)," + //
    "\n\tPARENTS_DATA VARCHAR(255)," + //
    "\n\tRAW_AUTHOR VARCHAR(255)," + //
    "\n\tRAW_NODE VARCHAR(255)," + //
    "\n\tREPOSITORY_ID INTEGER DEFAULT 0," + //
    "\n\tVERSION INTEGER," + //
    "\n\tCONSTRAINT pk_AO_E8B6CC_ISSUE_MAPPING_V2_ID PRIMARY 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(14, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("dbo.AO_E8B6CC_ISSUE_MAPPING_V2")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("dbo.AO_E8B6CC_ISSUE_MAPPING_V2", "AUTHOR")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("dbo.AO_E8B6CC_ISSUE_MAPPING_V2", "BRANCH")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("dbo.AO_E8B6CC_ISSUE_MAPPING_V2", "VERSION")));
}
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)

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