-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d5d574
commit 6091112
Showing
4 changed files
with
95 additions
and
2 deletions.
There are no files selected for viewing
26 changes: 26 additions & 0 deletions
26
...main/java/com/alibaba/druid/sql/dialect/mysql/ast/statement/MySqlAlterTableAlgorithm.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.alibaba.druid.sql.dialect.mysql.ast.statement; | ||
|
||
import com.alibaba.druid.sql.ast.SQLExpr; | ||
import com.alibaba.druid.sql.ast.statement.SQLAlterTableItem; | ||
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlObjectImpl; | ||
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlASTVisitor; | ||
|
||
public class MySqlAlterTableAlgorithm extends MySqlObjectImpl implements SQLAlterTableItem { | ||
private SQLExpr algorithmType; | ||
|
||
@Override | ||
public void accept0(MySqlASTVisitor visitor) { | ||
if (visitor.visit(this)) { | ||
acceptChild(visitor, algorithmType); | ||
} | ||
visitor.endVisit(this); | ||
} | ||
|
||
public SQLExpr getAlgorithmType() { | ||
return algorithmType; | ||
} | ||
|
||
public void setAlgorithmType(SQLExpr algorithmType) { | ||
this.algorithmType = algorithmType; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
...java/com/alibaba/druid/sql/dialect/mysql/ast/statement/MySqlCreateTableStatementTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.alibaba.druid.sql.dialect.mysql.ast.statement; | ||
|
||
import com.alibaba.druid.sql.ast.SQLName; | ||
import com.alibaba.druid.sql.ast.SQLStatement; | ||
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition; | ||
import com.alibaba.druid.sql.repository.SchemaObject; | ||
import com.alibaba.druid.sql.repository.SchemaRepository; | ||
import com.alibaba.druid.util.JdbcConstants; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertArrayEquals; | ||
|
||
public class MySqlCreateTableStatementTest { | ||
@Test | ||
public void testAlterTable() { | ||
SchemaRepository repository = new SchemaRepository(JdbcConstants.MYSQL); | ||
repository.setDefaultSchema("`db`"); | ||
|
||
// init table struct | ||
repository.console("CREATE TABLE tb (\n" | ||
+ "\tid bigint(20) NOT NULL AUTO_INCREMENT,\n" | ||
+ "\tc0 varchar(40) NOT NULL COMMENT 'c0',\n" | ||
+ "\tPRIMARY KEY (id)\n" | ||
+ ")"); | ||
// expect 2 column | ||
assertArrayEquals(new String[] {"id", "c0"}, findColumnNames(repository.findSchema("db").findTable("tb"))); | ||
|
||
// add column with alter option | ||
repository.console("ALTER TABLE `db`.`tb` ADD COLUMN `c1` int COMMENT 'c1' , algorithm=inplace ,lock=none"); | ||
SchemaObject schemaObject = repository.findSchema("db").findTable("tb"); | ||
// expect 3 column after add column | ||
assertArrayEquals(new String[] {"id", "c0", "c1"}, findColumnNames(schemaObject)); | ||
|
||
// generate create table sql | ||
String createTableSql = schemaObject.getStatement().toString(); | ||
|
||
// restore from full create table sql | ||
repository = new SchemaRepository(JdbcConstants.MYSQL); | ||
repository.setDefaultSchema("`db`"); | ||
repository.console(createTableSql); | ||
|
||
// expect 3 column after restore | ||
assertArrayEquals(new String[] {"id", "c0", "c1"}, | ||
findColumnNames(repository.findSchema("db").findTable("tb"))); | ||
} | ||
|
||
private Object[] findColumnNames(SchemaObject schemaObject) { | ||
SQLStatement statement = schemaObject.getStatement(); | ||
assert statement instanceof MySqlCreateTableStatement; | ||
return ((MySqlCreateTableStatement)statement).getTableElementList() | ||
.stream() | ||
.filter(element -> element instanceof SQLColumnDefinition) | ||
.map(element -> ((SQLColumnDefinition)element).getName()) | ||
.map(SQLName::getSimpleName) | ||
.toArray(); | ||
} | ||
} |