Skip to content

Commit

Permalink
Merge pull request alibaba#506 from cutedemons/master
Browse files Browse the repository at this point in the history
fix parse create table bug
  • Loading branch information
cutedemons committed Mar 3, 2014
2 parents 9ed3128 + d1c5e22 commit 7f687a1
Show file tree
Hide file tree
Showing 14 changed files with 246 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.alibaba.druid.sql.dialect.mysql.ast;

import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.statement.SQLTableConstaint;
import com.alibaba.druid.sql.ast.statement.SQLUnique;
import com.alibaba.druid.sql.ast.statement.SQLUniqueConstraint;
Expand All @@ -23,7 +24,11 @@

public class MySqlKey extends SQLUnique implements SQLUniqueConstraint, SQLTableConstaint {

private String indexType;
private SQLName indexName;

private String indexType;

private boolean hasConstaint;

public MySqlKey(){

Expand All @@ -40,6 +45,7 @@ protected void accept0(MySqlASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, this.getName());
acceptChild(visitor, this.getColumns());
acceptChild(visitor, indexName);
}
visitor.endVisit(this);
}
Expand All @@ -52,4 +58,20 @@ public void setIndexType(String indexType) {
this.indexType = indexType;
}

public SQLName getIndexName() {
return indexName;
}

public void setIndexName(SQLName indexName) {
this.indexName = indexName;
}

public boolean isHasConstaint() {
return hasConstaint;
}

public void setHasConstaint(boolean hasConstaint) {
this.hasConstaint = hasConstaint;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ protected void accept0(MySqlASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, this.getName());
acceptChild(visitor, this.getColumns());
acceptChild(visitor, this.getIndexName());
}
visitor.endVisit(this);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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.SQLName;
import com.alibaba.druid.sql.ast.statement.SQLForeignKeyImpl;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;

/**
* @author kiki
*/
public class MysqlForeignKey extends SQLForeignKeyImpl {

private SQLName indexName;

private boolean hasConstaint;

public SQLName getIndexName() {
return indexName;
}

public void setIndexName(SQLName indexName) {
this.indexName = indexName;
}

public boolean isHasConstaint() {
return hasConstaint;
}

public void setHasConstaint(boolean hasConstaint) {
this.hasConstaint = hasConstaint;
}

@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());

acceptChild(visitor, indexName);
}
visitor.endVisit(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
import com.alibaba.druid.sql.ast.statement.SQLForeignKeyConstraint;
import com.alibaba.druid.sql.ast.statement.SQLPrimaryKey;
import com.alibaba.druid.sql.ast.statement.SQLSelect;
import com.alibaba.druid.sql.ast.statement.SQLTableConstaint;
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.MysqlForeignKey;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlCreateTableStatement.TableSpaceOption;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlPartitionByHash;
Expand Down Expand Up @@ -592,7 +593,9 @@ private boolean parseTableOptionCharsetOrCollate(MySqlCreateTableStatement stmt)

protected SQLTableConstaint parseConstraint() {
SQLName name = null;
boolean hasConstaint = false;
if (lexer.token() == (Token.CONSTRAINT)) {
hasConstaint = true;
lexer.nextToken();
}

Expand All @@ -604,6 +607,7 @@ protected SQLTableConstaint parseConstraint() {
lexer.nextToken();

MySqlKey key = new MySqlKey();
key.setHasConstaint(hasConstaint);

if (identifierEquals("USING")) {
lexer.nextToken();
Expand All @@ -612,7 +616,10 @@ protected SQLTableConstaint parseConstraint() {
}

if (lexer.token() == Token.IDENTIFIER) {
name = this.exprParser.name();
SQLName indexName = this.exprParser.name();
if (indexName != null) {
key.setIndexName(indexName);
}
}

accept(Token.LPAREN);
Expand Down Expand Up @@ -640,24 +647,26 @@ protected SQLTableConstaint parseConstraint() {
}

if (lexer.token() == Token.PRIMARY) {
SQLPrimaryKey pk = this.exprParser.parsePrimaryKey();
MySqlPrimaryKey pk = this.getExprParser().parsePrimaryKey();
pk.setName(name);
pk.setHasConstaint(hasConstaint);
return (SQLTableConstaint) pk;
}

if (lexer.token() == Token.UNIQUE) {
MySqlUnique uk = this.getExprParser().parseUnique();
uk.setName(name);
uk.setHasConstaint(hasConstaint);
return (SQLTableConstaint) uk;
}

if (lexer.token() == Token.FOREIGN) {
SQLForeignKeyConstraint fk = this.getExprParser().parseForeignKey();
MysqlForeignKey fk = this.getExprParser().parseForeignKey();
fk.setName(name);
fk.setHasConstaint(hasConstaint);
return (SQLTableConstaint) fk;
}

throw new ParserException("TODO :" + lexer.token());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
import com.alibaba.druid.sql.ast.expr.SQLVariantRefExpr;
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.MysqlForeignKey;
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;
Expand Down Expand Up @@ -689,7 +689,7 @@ public Limit parseLimit() {
}

@Override
public SQLPrimaryKey parsePrimaryKey() {
public MySqlPrimaryKey parsePrimaryKey() {
accept(Token.PRIMARY);
accept(Token.KEY);

Expand Down Expand Up @@ -729,8 +729,8 @@ public MySqlUnique parseUnique() {
MySqlUnique unique = new MySqlUnique();

if (lexer.token() != Token.LPAREN) {
SQLName name = name();
unique.setName(name);
SQLName indexName = name();
unique.setIndexName(indexName);
}

accept(Token.LPAREN);
Expand All @@ -753,6 +753,31 @@ public MySqlUnique parseUnique() {
return unique;
}

public MysqlForeignKey parseForeignKey() {
accept(Token.FOREIGN);
accept(Token.KEY);

MysqlForeignKey fk = new MysqlForeignKey();

if (lexer.token() != Token.LPAREN) {
SQLName indexName = name();
fk.setIndexName(indexName);
}

accept(Token.LPAREN);
this.names(fk.getReferencingColumns());
accept(Token.RPAREN);

accept(Token.REFERENCES);

fk.setReferencedTableName(this.name());

accept(Token.LPAREN);
this.names(fk.getReferencedColumns());
accept(Token.RPAREN);
return fk;
}

protected SQLAggregateExpr parseAggregateExprRest(SQLAggregateExpr aggregateExpr) {
if (lexer.token() == Token.ORDER) {
SQLOrderBy orderBy = this.parseOrderBy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
import com.alibaba.druid.sql.ast.statement.SQLCreateDatabaseStatement;
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
import com.alibaba.druid.sql.ast.statement.SQLExprTableSource;
import com.alibaba.druid.sql.ast.statement.SQLForeignKeyConstraint;
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement;
import com.alibaba.druid.sql.ast.statement.SQLInsertStatement.ValuesClause;
import com.alibaba.druid.sql.ast.statement.SQLPrimaryKey;
Expand All @@ -57,6 +56,7 @@
import com.alibaba.druid.sql.ast.statement.SQLTableSource;
import com.alibaba.druid.sql.ast.statement.SQLUpdateSetItem;
import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement;
import com.alibaba.druid.sql.dialect.mysql.ast.MysqlForeignKey;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.CobarShowStatus;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableAddColumn;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlAlterTableChangeColumn;
Expand Down Expand Up @@ -1997,14 +1997,14 @@ public SQLStatement parseSet() {
if (identifierEquals("PASSWORD")) {
lexer.nextToken();
MySqlSetPasswordStatement stmt = new MySqlSetPasswordStatement();

if (lexer.token() == Token.FOR) {
lexer.nextToken();
stmt.setUser(this.exprParser.name());
}

accept(Token.EQ);

stmt.setPassword(this.exprParser.expr());

return stmt;
Expand Down Expand Up @@ -2193,8 +2193,9 @@ public SQLStatement parseAlter() {

stmt.getItems().add(item);
} else if (lexer.token() == Token.FOREIGN) {
SQLForeignKeyConstraint fk = this.getExprParser().parseForeignKey();
MysqlForeignKey fk = this.getExprParser().parseForeignKey();
fk.setName(constraintName);
fk.setHasConstaint(true);

SQLAlterTableAddConstraint item = new SQLAlterTableAddConstraint(fk);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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.MysqlForeignKey;
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;
Expand Down Expand Up @@ -147,6 +148,10 @@ public interface MySqlASTVisitor extends SQLASTVisitor {

void endVisit(MySqlUnique x);

boolean visit(MysqlForeignKey x);

void endVisit(MysqlForeignKey x);

void endVisit(MySqlIntervalExpr x);

boolean visit(MySqlIntervalExpr x);
Expand Down Expand Up @@ -558,8 +563,8 @@ public interface MySqlASTVisitor extends SQLASTVisitor {
boolean visit(MySqlOptimizeStatement x);

void endVisit(MySqlOptimizeStatement x);

boolean visit(MySqlSetPasswordStatement x);

void endVisit(MySqlSetPasswordStatement x);
} //
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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.MysqlForeignKey;
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;
Expand Down Expand Up @@ -1108,6 +1109,16 @@ public void endVisit(MySqlUnique x) {

}

@Override
public boolean visit(MysqlForeignKey x) {
return true;
}

@Override
public void endVisit(MysqlForeignKey x) {

}

@Override
public boolean visit(MySqlAlterTableModifyColumn x) {
return true;
Expand Down Expand Up @@ -1237,14 +1248,15 @@ public boolean visit(MySqlOptimizeStatement x) {
public void endVisit(MySqlOptimizeStatement x) {

}

@Override
public boolean visit(MySqlSetPasswordStatement x) {
return true;
}

@Override
public void endVisit(MySqlSetPasswordStatement x) {

}

} //
Loading

0 comments on commit 7f687a1

Please sign in to comment.