Merge pull request #813 from bucketli/master

support ALTER TABLE XXX  [[ADD KEY]]  xxx('xxx')
This commit is contained in:
Yako
2015-04-20 09:54:28 +08:00
8 changed files with 139 additions and 5 deletions
@@ -20,6 +20,7 @@ import java.util.List;
import com.alibaba.druid.sql.ast.SQLName;
import com.alibaba.druid.sql.ast.SQLObjectImpl;
import com.alibaba.druid.sql.parser.Token;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public class SQLAlterTableAddIndex extends SQLObjectImpl implements SQLAlterTableItem {
@@ -33,6 +34,8 @@ public class SQLAlterTableAddIndex extends SQLObjectImpl implements SQLAlterTabl
private String type;
private String using;
private String keyOrIndex = Token.INDEX.name;
@Override
protected void accept0(SQLASTVisitor visitor) {
@@ -79,4 +82,11 @@ public class SQLAlterTableAddIndex extends SQLObjectImpl implements SQLAlterTabl
this.using = using;
}
public String getKeyOrIndex() {
return keyOrIndex;
}
public void setKeyOrIndex(String keyOrIndex) {
this.keyOrIndex = keyOrIndex;
}
}
@@ -2290,7 +2290,11 @@ public class MySqlStatementParser extends SQLStatementParser {
SQLAlterTableAddConstraint item = new SQLAlterTableAddConstraint(primaryKey);
stmt.getItems().add(item);
} else if (lexer.token() == Token.KEY) {
throw new ParserException("TODO " + lexer.token() + " " + lexer.stringVal());
// throw new ParserException("TODO " + lexer.token() +
// " " + lexer.stringVal());
SQLAlterTableAddIndex item = parseAlterTableAddIndex();
item.setParent(stmt);
stmt.getItems().add(item);
} else if (lexer.token() == Token.CONSTRAINT) {
lexer.nextToken();
SQLName constraintName = this.exprParser.name();
@@ -1516,7 +1516,7 @@ public class SQLExprParser extends SQLParser {
if (lexer.token() == Token.KEY) {
lexer.nextToken();
}
column.getConstraints().add(new SQLColumnPrimaryKey());
column.getConstraints().add(new SQLColumnUniqueKey());
return parseColumnRest(column);
}
@@ -1688,10 +1688,20 @@ public class SQLStatementParser extends SQLParser {
item.setUnique(true);
lexer.nextToken();
if (lexer.token() == Token.INDEX) {
item.setKeyOrIndex(Token.INDEX.name);
lexer.nextToken();
} else if (lexer.token() == Token.KEY) {
item.setKeyOrIndex(Token.KEY.name);
lexer.nextToken();
}
} else {
accept(Token.INDEX);
if (lexer.token() == Token.INDEX) {
item.setKeyOrIndex(Token.INDEX.name);
accept(Token.INDEX);
} else if (lexer.token() == Token.KEY) {
item.setKeyOrIndex(Token.KEY.name);
accept(Token.KEY);
}
}
if (lexer.token() == Token.LPAREN) {
@@ -1990,7 +1990,8 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Printab
print("UNIQUE ");
}
print("INDEX ");
print(x.getKeyOrIndex());
print(" ");
if (x.getName() != null) {
x.getName().accept(this);
@@ -0,0 +1,46 @@
/*
* 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 junit.framework.TestCase;
import org.junit.Assert;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.parser.Token;
public class MySqlAlterTableTest19 extends TestCase {
public void test_alter_add_key() throws Exception {
String sql = "ALTER TABLE `test`.`tb1` ADD KEY `idx_parent_id` (`parent_id`)";
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLStatement stmt = parser.parseStatementList().get(0);
parser.match(Token.EOF);
String output = SQLUtils.toMySqlString(stmt);
Assert.assertEquals("ALTER TABLE `test`.`tb1`\n\tADD KEY `idx_parent_id` (`parent_id`)", output);
}
public void test_alter_add_index() throws Exception {
String sql = "ALTER TABLE `test`.`tb1` ADD INDEX `idx_parent_id` (`parent_id`)";
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLStatement stmt = parser.parseStatementList().get(0);
parser.match(Token.EOF);
String output = SQLUtils.toMySqlString(stmt);
Assert.assertEquals("ALTER TABLE `test`.`tb1`\n\tADD INDEX `idx_parent_id` (`parent_id`)", output);
}
}
@@ -56,7 +56,7 @@ public class MySqlCreateTableTest49 extends MysqlTest {
String output = SQLUtils.toMySqlString(stmt);
Assert.assertEquals("CREATE TABLE tauth (" //
+ "\n\tcid varchar(36) NOT NULL PRIMARY KEY, "//
+ "\n\tcid varchar(36) NOT NULL UNIQUE, "//
+ "\n\tcdesc varchar(200), "//
+ "\n\tcname varchar(100) NOT NULL, "//
+ "\n\tcseq decimal(22, 0), "//
@@ -0,0 +1,63 @@
/*
* 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 org.junit.Assert;
import org.junit.Test;
import com.alibaba.druid.sql.MysqlTest;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
public class MySqlCreateTableTest58 extends MysqlTest {
@Test
public void test_one() throws Exception {
String sql = "CREATE TABLE `appservice_account` (" + "`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,"
+ "`password` varchar(128) NOT NULL," + "`last_login` datetime NOT NULL,"
+ "`username` varchar(40) NOT NULL UNIQUE," + "`date_of_birth` date NOT NULL,"
+ "`head` varchar(100) NOT NULL," + "`headThumb` varchar(100) NOT NULL,"
+ "`name` varchar(50) NOT NULL," + "`gender` integer," + "`uploadVideoCount` integer NOT NULL,"
+ "`fansCount` integer NOT NULL," + "`balance` numeric(19, 2) NOT NULL,"
+ "`brick` integer NOT NULL," + "`reward` integer NOT NULL," + "`token` varchar(500) NOT NULL,"
+ "`weiboUserid` varchar(50) NOT NULL," + "`weiboAccesstoken` varchar(500) NOT NULL,"
+ "`qqUserid` varchar(50) NOT NULL," + "`qqAccesstoken` varchar(500) NOT NULL,"
+ "`wechatUserid` varchar(50) NOT NULL," + "`wechatAccesstoken` varchar(500) NOT NULL,"
+ "`is_active` bool NOT NULL," + "`is_admin` bool NOT NULL" + ")";
MySqlStatementParser parser = new MySqlStatementParser(sql);
SQLStatement stmt = parser.parseCreateTable();
MySqlSchemaStatVisitor visitor = new MySqlSchemaStatVisitor();
stmt.accept(visitor);
String output = SQLUtils.toMySqlString(stmt);
Assert.assertEquals("CREATE TABLE `appservice_account` (" + "\n\t`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, "
+ "\n\t`password` varchar(128) NOT NULL, " + "\n\t`last_login` datetime NOT NULL, "
+ "\n\t`username` varchar(40) NOT NULL UNIQUE, " + "\n\t`date_of_birth` date NOT NULL, "
+ "\n\t`head` varchar(100) NOT NULL, " + "\n\t`headThumb` varchar(100) NOT NULL, "
+ "\n\t`name` varchar(50) NOT NULL, " + "\n\t`gender` integer, " + "\n\t`uploadVideoCount` integer NOT NULL, "
+ "\n\t`fansCount` integer NOT NULL, " + "\n\t`balance` numeric(19, 2) NOT NULL, "
+ "\n\t`brick` integer NOT NULL, " + "\n\t`reward` integer NOT NULL, " + "\n\t`token` varchar(500) NOT NULL, "
+ "\n\t`weiboUserid` varchar(50) NOT NULL, " + "\n\t`weiboAccesstoken` varchar(500) NOT NULL, "
+ "\n\t`qqUserid` varchar(50) NOT NULL, " + "\n\t`qqAccesstoken` varchar(500) NOT NULL, "
+ "\n\t`wechatUserid` varchar(50) NOT NULL, " + "\n\t`wechatAccesstoken` varchar(500) NOT NULL, "
+ "\n\t`is_active` bool NOT NULL, " + "\n\t`is_admin` bool NOT NULL" + "\n)",output);
}
}