Search in sources :

Example 1 with SQLCreateTableStatement

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

the class GlobalTableUtil method hasGlobalColumn.

private static boolean hasGlobalColumn(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 && GLOBAL_TABLE_MYCAT_COLUMN.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)

Example 2 with SQLCreateTableStatement

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

the class SQLCreateTableParser method parseCrateTable.

public SQLCreateTableStatement parseCrateTable(boolean acceptCreate) {
    if (acceptCreate) {
        accept(Token.CREATE);
    }
    SQLCreateTableStatement createTable = newCreateStatement();
    if (identifierEquals("GLOBAL")) {
        lexer.nextToken();
        if (identifierEquals("TEMPORARY")) {
            lexer.nextToken();
            createTable.setType(SQLCreateTableStatement.Type.GLOBAL_TEMPORARY);
        } else {
            throw new ParserException("syntax error " + lexer.token() + " " + lexer.stringVal());
        }
    } else if (lexer.token() == Token.IDENTIFIER && lexer.stringVal().equalsIgnoreCase("LOCAL")) {
        lexer.nextToken();
        if (lexer.token() == Token.IDENTIFIER && lexer.stringVal().equalsIgnoreCase("TEMPORAY")) {
            lexer.nextToken();
            createTable.setType(SQLCreateTableStatement.Type.LOCAL_TEMPORARY);
        } else {
            throw new ParserException("syntax error");
        }
    }
    accept(Token.TABLE);
    createTable.setName(this.exprParser.name());
    if (lexer.token() == Token.LPAREN) {
        lexer.nextToken();
        for (; ; ) {
            if (//
            lexer.token() == Token.IDENTIFIER || lexer.token() == Token.LITERAL_ALIAS) {
                SQLColumnDefinition column = this.exprParser.parseColumn();
                createTable.getTableElementList().add(column);
            } else if (//
            lexer.token == Token.PRIMARY || //
            lexer.token == Token.UNIQUE || //
            lexer.token == Token.CHECK || lexer.token == Token.CONSTRAINT) {
                SQLConstraint constraint = this.exprParser.parseConstaint();
                constraint.setParent(createTable);
                createTable.getTableElementList().add((SQLTableElement) constraint);
            } else if (lexer.token() == Token.TABLESPACE) {
                throw new ParserException("TODO " + lexer.token());
            } else {
                SQLColumnDefinition column = this.exprParser.parseColumn();
                createTable.getTableElementList().add(column);
            }
            if (lexer.token() == Token.COMMA) {
                lexer.nextToken();
                if (lexer.token() == Token.RPAREN) {
                    // compatible for sql server
                    break;
                }
                continue;
            }
            break;
        }
        // while
        // (this.tokenList.current().equals(OracleToken.ConstraintToken)) {
        // parseConstaint(table.getConstraints());
        //
        // if (this.tokenList.current().equals(OracleToken.CommaToken))
        // ;
        // lexer.nextToken();
        // }
        accept(Token.RPAREN);
        if (identifierEquals("INHERITS")) {
            lexer.nextToken();
            accept(Token.LPAREN);
            SQLName inherits = this.exprParser.name();
            createTable.setInherits(new SQLExprTableSource(inherits));
            accept(Token.RPAREN);
        }
    }
    return createTable;
}
Also used : SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) SQLConstraint(com.alibaba.druid.sql.ast.statement.SQLConstraint) SQLName(com.alibaba.druid.sql.ast.SQLName) SQLTableElement(com.alibaba.druid.sql.ast.statement.SQLTableElement) SQLExprTableSource(com.alibaba.druid.sql.ast.statement.SQLExprTableSource) SQLColumnDefinition(com.alibaba.druid.sql.ast.statement.SQLColumnDefinition)

Example 3 with SQLCreateTableStatement

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

the class MySqlCreateTableTest11 method test_0.

public void test_0() throws Exception {
    String sql = //
    "CREATE TABLE `tmall_campaign` (" + //
    "`id` int(11) NOT NULL AUTO_INCREMENT," + //
    "`campaign_name` varchar(200) NOT NULL COMMENT '活动名称'," + //
    "`create_date` datetime DEFAULT NULL COMMENT '活动创建时间'," + //
    "`delete_flag` int(11) DEFAULT '0' COMMENT '活动删除标识'," + //
    "PRIMARY KEY (`id`)" + ") ENGINE=InnoDB DEFAULT CHARSET=gbk;";
    MySqlStatementParser parser = new MySqlStatementParser(sql);
    List<SQLStatement> statementList = parser.parseStatementList();
    SQLCreateTableStatement stmt = (SQLCreateTableStatement) statementList.get(0);
    //        print(statementList);
    Assert.assertEquals(1, statementList.size());
    MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
    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("tmall_campaign")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("tmall_campaign", "id")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("tmall_campaign", "campaign_name")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("tmall_campaign", "create_date")));
    Assert.assertTrue(visitor.getColumns().contains(new Column("tmall_campaign", "delete_flag")));
}
Also used : MySqlSchemaStatVisitor(com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor) SQLCreateTableStatement(com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement) Column(com.alibaba.druid.stat.TableStat.Column) MySqlStatementParser(com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement)

Example 4 with SQLCreateTableStatement

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

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

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