mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
mysql ddl parser support forign key
This commit is contained in:
@@ -19,7 +19,7 @@ import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
|
||||
public interface SQLForeignKeyConstraint extends SQLConstaint {
|
||||
public interface SQLForeignKeyConstraint extends SQLConstaint, SQLTableElement {
|
||||
|
||||
List<SQLName> getReferencingColumns();
|
||||
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.sql.ast.statement;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
|
||||
public abstract class SQLForeignKeyImpl extends SQLConstaintImpl implements SQLForeignKeyConstraint {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
private SQLName referencedTableName;
|
||||
private List<SQLName> referencingColumns = new ArrayList<SQLName>();
|
||||
private List<SQLName> referencedColumns = new ArrayList<SQLName>();
|
||||
|
||||
@Override
|
||||
public List<SQLName> getReferencingColumns() {
|
||||
return referencingColumns;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SQLName getReferencedTableName() {
|
||||
return referencedTableName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setReferencedTableName(SQLName value) {
|
||||
this.referencedTableName = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<SQLName> getReferencedColumns() {
|
||||
return referencedColumns;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.mysql.ast;
|
||||
|
||||
import com.alibaba.druid.sql.ast.statement.SQLForeignKeyConstraint;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLForeignKeyImpl;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTableElement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
|
||||
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
|
||||
|
||||
public class MySqlForeignKey extends SQLForeignKeyImpl implements SQLForeignKeyConstraint, SQLTableElement {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Override
|
||||
protected void accept0(SQLASTVisitor visitor) {
|
||||
if (visitor instanceof MySqlASTVisitor) {
|
||||
accept0((MySqlASTVisitor) visitor);
|
||||
}
|
||||
}
|
||||
|
||||
protected void accept0(MySqlASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, this.getName());
|
||||
acceptChild(visitor, this.getReferencedTableName());
|
||||
acceptChild(visitor, this.getReferencingColumns());
|
||||
acceptChild(visitor, this.getReferencedColumns());
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.sql.dialect.mysql.ast;
|
||||
|
||||
import com.alibaba.druid.sql.ast.statement.SQLPrimaryKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class MySqlUnique extends MySqlKey implements SQLPrimaryKey {
|
||||
|
||||
public MySqlUnique(){
|
||||
|
||||
}
|
||||
|
||||
protected void accept0(MySqlASTVisitor visitor) {
|
||||
if (visitor.visit(this)) {
|
||||
acceptChild(visitor, this.getName());
|
||||
acceptChild(visitor, this.getColumns());
|
||||
}
|
||||
visitor.endVisit(this);
|
||||
}
|
||||
}
|
||||
+34
-3
@@ -20,6 +20,7 @@ import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelect;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLTableConstaint;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlForeignKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByKey;
|
||||
@@ -43,6 +44,10 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
return parseCrateTable(true);
|
||||
}
|
||||
|
||||
public MySqlExprParser getExprParser() {
|
||||
return (MySqlExprParser) exprParser;
|
||||
}
|
||||
|
||||
public MySqlCreateTableStatement parseCrateTable(boolean acceptCreate) {
|
||||
if (acceptCreate) {
|
||||
accept(Token.CREATE);
|
||||
@@ -73,7 +78,10 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
if (lexer.token() == Token.IDENTIFIER) {
|
||||
SQLColumnDefinition column = this.exprParser.parseColumn();
|
||||
stmt.getTableElementList().add(column);
|
||||
} else if (lexer.token() == (Token.CONSTRAINT)) {
|
||||
} else if (lexer.token() == Token.CONSTRAINT //
|
||||
|| lexer.token() == Token.PRIMARY //
|
||||
|| lexer.token() == Token.UNIQUE
|
||||
) {
|
||||
stmt.getTableElementList().add(parseConstraint());
|
||||
} else if (lexer.token() == (Token.INDEX)) {
|
||||
lexer.nextToken();
|
||||
@@ -108,6 +116,25 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
stmt.getTableElementList().add(parseConstraint());
|
||||
} else if (lexer.token() == (Token.PRIMARY)) {
|
||||
stmt.getTableElementList().add(parseConstraint());
|
||||
} else if (lexer.token() == (Token.FOREIGN)) {
|
||||
lexer.nextToken();
|
||||
accept(Token.KEY);
|
||||
|
||||
MySqlForeignKey fk = new MySqlForeignKey();
|
||||
|
||||
accept(Token.LPAREN);
|
||||
this.exprParser.names(fk.getReferencingColumns());
|
||||
accept(Token.RPAREN);
|
||||
|
||||
accept(Token.REFERENCES);
|
||||
|
||||
fk.setReferencedTableName(this.exprParser.name());
|
||||
|
||||
accept(Token.LPAREN);
|
||||
this.exprParser.names(fk.getReferencedColumns());
|
||||
accept(Token.RPAREN);
|
||||
|
||||
stmt.getTableElementList().add(fk);
|
||||
}
|
||||
|
||||
if (!(lexer.token() == (Token.COMMA))) {
|
||||
@@ -164,7 +191,7 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (lexer.token() == Token.DEFAULT) {
|
||||
lexer.nextToken();
|
||||
if (identifierEquals("CHARACTER")) {
|
||||
@@ -235,7 +262,7 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
}
|
||||
}
|
||||
accept(Token.RPAREN);
|
||||
|
||||
|
||||
if (name != null) {
|
||||
key.setName(name);
|
||||
}
|
||||
@@ -247,6 +274,10 @@ public class MySqlCreateTableParser extends SQLCreateTableParser {
|
||||
return (SQLTableConstaint) this.exprParser.parsePrimaryKey();
|
||||
}
|
||||
|
||||
if (lexer.token() == (Token.UNIQUE)) {
|
||||
return (SQLTableConstaint) this.getExprParser().parseUnique();
|
||||
}
|
||||
|
||||
throw new ParserException("TODO");
|
||||
}
|
||||
|
||||
|
||||
@@ -31,6 +31,7 @@ import com.alibaba.druid.sql.ast.statement.SQLAssignItem;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLPrimaryKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlBinaryExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlBooleanExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlCharExpr;
|
||||
@@ -638,4 +639,29 @@ public class MySqlExprParser extends SQLExprParser {
|
||||
|
||||
return primaryKey;
|
||||
}
|
||||
|
||||
public MySqlUnique parseUnique() {
|
||||
accept(Token.UNIQUE);
|
||||
|
||||
MySqlUnique primaryKey = new MySqlUnique();
|
||||
|
||||
if (identifierEquals("USING")) {
|
||||
lexer.nextToken();
|
||||
primaryKey.setIndexType(lexer.stringVal());
|
||||
lexer.nextToken();
|
||||
}
|
||||
|
||||
accept(Token.LPAREN);
|
||||
for (;;) {
|
||||
primaryKey.getColumns().add(this.expr());
|
||||
if (!(lexer.token() == (Token.COMMA))) {
|
||||
break;
|
||||
} else {
|
||||
lexer.nextToken();
|
||||
}
|
||||
}
|
||||
accept(Token.RPAREN);
|
||||
|
||||
return primaryKey;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,9 +16,11 @@
|
||||
package com.alibaba.druid.sql.dialect.mysql.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlForceIndexHint;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlForeignKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlIgnoreIndexHint;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlUseIndexHint;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlBinaryExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlBooleanExpr;
|
||||
@@ -136,6 +138,10 @@ public interface MySqlASTVisitor extends SQLASTVisitor {
|
||||
|
||||
void endVisit(MySqlPrimaryKey x);
|
||||
|
||||
boolean visit(MySqlUnique x);
|
||||
|
||||
void endVisit(MySqlUnique x);
|
||||
|
||||
void endVisit(MySqlIntervalExpr x);
|
||||
|
||||
boolean visit(MySqlIntervalExpr x);
|
||||
@@ -451,68 +457,73 @@ public interface MySqlASTVisitor extends SQLASTVisitor {
|
||||
boolean visit(MySqlRenameTableStatement.Item x);
|
||||
|
||||
void endVisit(MySqlRenameTableStatement.Item x);
|
||||
|
||||
|
||||
boolean visit(MySqlRenameTableStatement x);
|
||||
|
||||
|
||||
void endVisit(MySqlRenameTableStatement x);
|
||||
|
||||
|
||||
boolean visit(MySqlDropViewStatement x);
|
||||
|
||||
|
||||
void endVisit(MySqlDropViewStatement x);
|
||||
|
||||
|
||||
boolean visit(MySqlUnionQuery x);
|
||||
|
||||
|
||||
void endVisit(MySqlUnionQuery x);
|
||||
|
||||
|
||||
boolean visit(MySqlUseIndexHint x);
|
||||
|
||||
|
||||
void endVisit(MySqlUseIndexHint x);
|
||||
|
||||
|
||||
boolean visit(MySqlIgnoreIndexHint x);
|
||||
|
||||
|
||||
void endVisit(MySqlIgnoreIndexHint x);
|
||||
|
||||
|
||||
boolean visit(MySqlLockTableStatement x);
|
||||
|
||||
|
||||
void endVisit(MySqlLockTableStatement x);
|
||||
|
||||
|
||||
boolean visit(MySqlUnlockTablesStatement x);
|
||||
|
||||
|
||||
void endVisit(MySqlUnlockTablesStatement x);
|
||||
|
||||
|
||||
boolean visit(MySqlForceIndexHint x);
|
||||
|
||||
|
||||
void endVisit(MySqlForceIndexHint x);
|
||||
|
||||
|
||||
boolean visit(MySqlAlterTableChangeColumn x);
|
||||
|
||||
|
||||
void endVisit(MySqlAlterTableChangeColumn x);
|
||||
|
||||
|
||||
boolean visit(MySqlAlterTableCharacter x);
|
||||
|
||||
|
||||
void endVisit(MySqlAlterTableCharacter x);
|
||||
|
||||
|
||||
boolean visit(MySqlAlterTableAddIndex x);
|
||||
|
||||
|
||||
void endVisit(MySqlAlterTableAddIndex x);
|
||||
|
||||
|
||||
boolean visit(MySqlAlterTableOption x);
|
||||
|
||||
|
||||
void endVisit(MySqlAlterTableOption x);
|
||||
|
||||
|
||||
boolean visit(MySqlCreateTableStatement x);
|
||||
|
||||
|
||||
void endVisit(MySqlCreateTableStatement x);
|
||||
|
||||
|
||||
boolean visit(MySqlHelpStatement x);
|
||||
|
||||
|
||||
void endVisit(MySqlHelpStatement x);
|
||||
|
||||
|
||||
boolean visit(MySqlCharExpr x);
|
||||
|
||||
|
||||
void endVisit(MySqlCharExpr x);
|
||||
|
||||
|
||||
boolean visit(MySqlAlterTableAddUnique x);
|
||||
|
||||
|
||||
void endVisit(MySqlAlterTableAddUnique x);
|
||||
|
||||
boolean visit(MySqlForeignKey x);
|
||||
|
||||
void endVisit(MySqlForeignKey x);
|
||||
|
||||
}
|
||||
|
||||
@@ -16,9 +16,11 @@
|
||||
package com.alibaba.druid.sql.dialect.mysql.visitor;
|
||||
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlForceIndexHint;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlForeignKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlIgnoreIndexHint;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlUseIndexHint;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlBinaryExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlBooleanExpr;
|
||||
@@ -1138,4 +1140,24 @@ public class MySqlASTVisitorAdapter extends SQLASTVisitorAdapter implements
|
||||
public void endVisit(MySqlAlterTableAddUnique x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlUnique x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlUnique x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlForeignKey x) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlForeignKey x) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,9 +34,11 @@ import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLSelectQueryBlock;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlForceIndexHint;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlForeignKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlIgnoreIndexHint;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlUseIndexHint;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlBinaryExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlBooleanExpr;
|
||||
@@ -2708,4 +2710,59 @@ public class MySqlOutputVisitor extends SQLASTOutputVisitor implements MySqlASTV
|
||||
public void endVisit(MySqlCharExpr x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlUnique x) {
|
||||
if (x.getName() != null) {
|
||||
print("CONSTRAINT ");
|
||||
x.getName().accept(this);
|
||||
print(' ');
|
||||
}
|
||||
|
||||
print("UNIQUE");
|
||||
|
||||
if (x.getIndexType() != null) {
|
||||
print(" USING ");
|
||||
print(x.getIndexType());
|
||||
}
|
||||
|
||||
print(" (");
|
||||
printAndAccept(x.getColumns(), ", ");
|
||||
print(")");
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlUnique x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlForeignKey x) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlForeignKey x) {
|
||||
if (x.getName() != null) {
|
||||
print("CONSTRAINT ");
|
||||
x.getName().accept(this);
|
||||
print(' ');
|
||||
}
|
||||
|
||||
print("FOREIGN KEY ");
|
||||
|
||||
print(" (");
|
||||
printAndAccept(x.getReferencedColumns(), ", ");
|
||||
print(")");
|
||||
|
||||
print(" REFERENCES ");
|
||||
x.getReferencedTableName().accept(this);
|
||||
|
||||
print(" (");
|
||||
printAndAccept(x.getReferencedColumns(), ", ");
|
||||
print(")");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,9 +29,11 @@ import com.alibaba.druid.sql.ast.statement.SQLSelectStatement;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUnionQuery;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlForceIndexHint;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlForeignKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlIgnoreIndexHint;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlPrimaryKey;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlUnique;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlUseIndexHint;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlBinaryExpr;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.expr.MySqlBooleanExpr;
|
||||
@@ -1221,5 +1223,25 @@ public class MySqlSchemaStatVisitor extends SchemaStatVisitor implements MySqlAS
|
||||
public void endVisit(MySqlAlterTableAddUnique x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlUnique x) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlUnique x) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean visit(MySqlForeignKey x) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void endVisit(MySqlForeignKey x) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ import com.alibaba.druid.stat.TableStat.Column;
|
||||
public class MySqlAlterTableTest5 extends TestCase {
|
||||
|
||||
public void test_alter_first() throws Exception {
|
||||
String sql = "ALTER TABLE table_name ALTER COLUMN column_name char(50)";
|
||||
String sql = "ALTER TABLE table_name change COLUMN column_name char(50)";
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
SQLStatement stmt = parser.parseStatementList().get(0);
|
||||
parser.match(Token.EOF);
|
||||
@@ -43,7 +43,7 @@ public class MySqlAlterTableTest5 extends TestCase {
|
||||
|
||||
String output = SQLUtils.toMySqlString(stmt);
|
||||
Assert.assertEquals("ALTER TABLE table_name" + //
|
||||
"\n\tALTER COLUMN column_name char(50)", output);
|
||||
"\n\tCHANGE COLUMN column_name char(50)", output);
|
||||
|
||||
Assert.assertEquals(1, visitor.getTables().size());
|
||||
Assert.assertEquals(1, visitor.getColumns().size());
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.mysql;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.MysqlTest;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
import com.alibaba.druid.stat.TableStat.Column;
|
||||
|
||||
public class MySqlCreateTableTest6 extends MysqlTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "CREATE TABLE Persons\n" + //
|
||||
"(\n" + //
|
||||
"Id_P int NOT NULL,\n" + //
|
||||
"LastName varchar(255) NOT NULL,\n" + //
|
||||
"FirstName varchar(255),\n" + //
|
||||
"Address varchar(255),\n" + //
|
||||
"City varchar(255),\n" + //
|
||||
"UNIQUE (Id_P)\n" + //
|
||||
")";
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
MySqlCreateTableStatement stmt = (MySqlCreateTableStatement) 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(5, visitor.getColumns().size());
|
||||
Assert.assertEquals(0, visitor.getConditions().size());
|
||||
|
||||
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("Persons")));
|
||||
|
||||
Assert.assertTrue(visitor.getColumns().contains(new Column("Persons", "Id_P")));
|
||||
Assert.assertTrue(visitor.getColumns().contains(new Column("Persons", "LastName")));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
/*
|
||||
* Copyright 1999-2011 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.alibaba.druid.bvt.sql.mysql;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import junit.framework.Assert;
|
||||
|
||||
import com.alibaba.druid.sql.MysqlTest;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
|
||||
import com.alibaba.druid.stat.TableStat;
|
||||
import com.alibaba.druid.stat.TableStat.Column;
|
||||
|
||||
public class MySqlCreateTableTest7 extends MysqlTest {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
String sql = "CREATE TABLE Orders\n" + //
|
||||
"(\n" + //
|
||||
"O_Id int NOT NULL,\n" + //
|
||||
"OrderNo int NOT NULL,\n" + //
|
||||
"Id_P int,\n" + //
|
||||
"PRIMARY KEY (O_Id),\n" + //
|
||||
"FOREIGN KEY (Id_P) REFERENCES Persons(Id_P)\n" + //
|
||||
")";
|
||||
|
||||
MySqlStatementParser parser = new MySqlStatementParser(sql);
|
||||
List<SQLStatement> statementList = parser.parseStatementList();
|
||||
MySqlCreateTableStatement stmt = (MySqlCreateTableStatement) 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(3, visitor.getColumns().size());
|
||||
Assert.assertEquals(0, visitor.getConditions().size());
|
||||
|
||||
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("Orders")));
|
||||
|
||||
Assert.assertTrue(visitor.getColumns().contains(new Column("Orders", "O_Id")));
|
||||
Assert.assertTrue(visitor.getColumns().contains(new Column("Orders", "OrderNo")));
|
||||
Assert.assertTrue(visitor.getColumns().contains(new Column("Orders", "Id_P")));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user