Search in sources :

Example 6 with SQLCreateTableStatement

use of com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement 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 7 with SQLCreateTableStatement

use of com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement 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)

Example 8 with SQLCreateTableStatement

use of com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement in project druid by alibaba.

the class SQLServerCreateTableTest_8 method test_0.

public void test_0() throws Exception {
    String sql = "create table ACT_RU_VARIABLE (" + " ID_ nvarchar(64) not null," + " DOUBLE_ double precision," + " LONG_ numeric(19,0)," + " TEXT_ nvarchar(4000)," + " 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 ACT_RU_VARIABLE (" + "\n\tID_ nvarchar(64) NOT NULL," + "\n\tDOUBLE_ DOUBLE PRECISION," + "\n\tLONG_ numeric(19, 0)," + "\n\tTEXT_ nvarchar(4000)," + "\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(4, visitor.getColumns().size());
    Assert.assertEquals(0, visitor.getConditions().size());
    Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("ACT_RU_VARIABLE")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("ACT_RU_VARIABLE", "ID_")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("ACT_RU_VARIABLE", "DOUBLE_")));
}
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 9 with SQLCreateTableStatement

use of com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement in project druid by alibaba.

the class SQLCreateTableParser method parseCrateTable.

public SQLCreateTableStatement parseCrateTable() {
    List<String> comments = null;
    if (lexer.isKeepComments() && lexer.hasComment()) {
        comments = lexer.readAndResetComments();
    }
    SQLCreateTableStatement stmt = parseCrateTable(true);
    if (comments != null) {
        stmt.addBeforeComment(comments);
    }
    return stmt;
}
Also used : SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement)

Example 10 with SQLCreateTableStatement

use of com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement in project Mycat-Server by MyCATApache.

the class DruidMysqlCreateTableTest method hasColumn.

private static boolean hasColumn(SQLStatement statement) {
    for (SQLTableElement tableElement : ((SQLCreateTableStatement) statement).getTableElementList()) {
        SQLName sqlName = null;
        if (tableElement instanceof SQLColumnDefinition) {
            sqlName = ((SQLColumnDefinition) tableElement).getName();
        }
        if (sqlName != null) {
            String simpleName = sqlName.getSimpleName();
            simpleName = StringUtil.removeBackquote(simpleName);
            if (tableElement instanceof SQLColumnDefinition && "_slot".equalsIgnoreCase(simpleName)) {
                return true;
            }
        }
    }
    return false;
}
Also used : SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLTableElement(com.alibaba.druid.sql.ast.statement.SQLTableElement) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)

Aggregations

SQLCreateTableStatement (com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement)16 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)11 Column (com.alibaba.druid.stat.TableStat.Column)10 SQLServerStatementParser (com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser)8 SQLServerSchemaStatVisitor (com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor)8 SQLName (com.alibaba.druid.sql.ast.SQLName)4 SQLTableElement (com.alibaba.druid.sql.ast.statement.SQLTableElement)4 SQLColumnDefinition (com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)3 MySqlStatementParser (com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser)3 MySqlSchemaStatVisitor (com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor)3 SQLOver (com.alibaba.druid.sql.ast.SQLOver)1 SQLAllExpr (com.alibaba.druid.sql.ast.expr.SQLAllExpr)1 SQLAnyExpr (com.alibaba.druid.sql.ast.expr.SQLAnyExpr)1 SQLBinaryOpExpr (com.alibaba.druid.sql.ast.expr.SQLBinaryOpExpr)1 SQLCurrentOfCursorExpr (com.alibaba.druid.sql.ast.expr.SQLCurrentOfCursorExpr)1 SQLDefaultExpr (com.alibaba.druid.sql.ast.expr.SQLDefaultExpr)1 SQLInListExpr (com.alibaba.druid.sql.ast.expr.SQLInListExpr)1 SQLMethodInvokeExpr (com.alibaba.druid.sql.ast.expr.SQLMethodInvokeExpr)1 SQLSomeExpr (com.alibaba.druid.sql.ast.expr.SQLSomeExpr)1 SQLAlterTableAlterColumn (com.alibaba.druid.sql.ast.statement.SQLAlterTableAlterColumn)1