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;
}
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;
}
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")));
}
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")));
}
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")));
}
Aggregations