mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved sql parser schema repository.
This commit is contained in:
@@ -10,7 +10,7 @@
|
||||
-->
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>druid</artifactId>
|
||||
<version>1.1.2</version>
|
||||
<version>1.1.3-preview_11</version>
|
||||
|
||||
<packaging>jar</packaging>
|
||||
<name>druid</name>
|
||||
|
||||
@@ -257,6 +257,10 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
|
||||
if (element instanceof MySqlUnique) {
|
||||
MySqlUnique unique = (MySqlUnique) element;
|
||||
|
||||
if (unique.getColumns().size() == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
SQLExpr column = unique.getColumns().get(0).getExpr();
|
||||
if (column instanceof SQLIdentifierExpr
|
||||
&& SQLUtils.nameEquals(columnName, ((SQLIdentifierExpr) column).getName())) {
|
||||
@@ -738,9 +742,20 @@ public class SQLCreateTableStatement extends SQLStatementImpl implements SQLDDLS
|
||||
}
|
||||
}
|
||||
|
||||
for (SQLTableElement e : tableElementList) {
|
||||
for (int i = tableElementList.size() - 1; i >= 0; --i) {
|
||||
SQLTableElement e = tableElementList.get(i);
|
||||
if (e instanceof SQLUnique) {
|
||||
((SQLUnique) e).applyDropColumn(column);
|
||||
SQLUnique unique = (SQLUnique) e;
|
||||
unique.applyDropColumn(column);
|
||||
if (unique.getColumns().size() == 0) {
|
||||
tableElementList.remove(i);
|
||||
}
|
||||
} else if (e instanceof MySqlTableIndex) {
|
||||
MySqlTableIndex index = (MySqlTableIndex) e;
|
||||
index.applyDropColumn(column);
|
||||
if (index.getColumns().size() == 0) {
|
||||
tableElementList.remove(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+6
-4
@@ -314,11 +314,13 @@ public class MySqlCreateTableStatement extends SQLCreateTableStatement implement
|
||||
}
|
||||
}
|
||||
|
||||
for (SQLTableElement e : tableElementList) {
|
||||
for (int i = 0; i < tableElementList.size(); i++) {
|
||||
SQLTableElement e = tableElementList.get(i);
|
||||
if(e instanceof MySqlTableIndex) {
|
||||
((MySqlTableIndex) e).apply(item);
|
||||
} else if (e instanceof MySqlKey) {
|
||||
((MySqlKey) e).applyColumnRename(columnName, column.getName());
|
||||
((MySqlTableIndex) e).applyColumnRename(columnName, column.getName());
|
||||
} else if (e instanceof SQLUnique) {
|
||||
SQLUnique unique = (SQLUnique) e;
|
||||
unique.applyColumnRename(columnName, column.getName());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+22
-1
@@ -18,6 +18,7 @@ package com.alibaba.druid.sql.dialect.mysql.ast.statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLExpr;
|
||||
import com.alibaba.druid.sql.ast.SQLName;
|
||||
import com.alibaba.druid.sql.ast.statement.SQLColumnDefinition;
|
||||
@@ -85,7 +86,27 @@ public class MySqlTableIndex extends MySqlObjectImpl implements SQLTableElement
|
||||
return x;
|
||||
}
|
||||
|
||||
public boolean apply(MySqlAlterTableChangeColumn x) {
|
||||
public boolean applyColumnRename(SQLName columnName, SQLName to) {
|
||||
for (SQLSelectOrderByItem orderByItem : columns) {
|
||||
SQLExpr expr = orderByItem.getExpr();
|
||||
if (expr instanceof SQLName
|
||||
&& SQLUtils.nameEquals((SQLName) expr, columnName)) {
|
||||
orderByItem.setExpr(to.clone());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean applyDropColumn(SQLName columnName) {
|
||||
for (int i = columns.size() - 1; i >= 0; i--) {
|
||||
SQLExpr expr = columns.get(i).getExpr();
|
||||
if (expr instanceof SQLName
|
||||
&& SQLUtils.nameEquals((SQLName) expr, columnName)) {
|
||||
columns.remove(i);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
+23
@@ -654,5 +654,28 @@ public class MySqlCreateTable_showColumns_repository_test extends MysqlTest {
|
||||
") ENGINE = InnoDB AUTO_INCREMENT = 1769503 CHARSET = utf8mb4 COMMENT = '10000000'"
|
||||
|
||||
, repository.console("show create table test1"));
|
||||
|
||||
assertEquals("+--------------+--------------+------+-----+---------------------+-----------------------------+\n" +
|
||||
"| Field | Type | Null | Key | Default | Extra |\n" +
|
||||
"+--------------+--------------+------+-----+---------------------+-----------------------------+\n" +
|
||||
"| c_decimal_1 | INT(11) | YES | | NULL | |\n" +
|
||||
"| id | bigint(20) | YES | | NULL | auto_increment |\n" +
|
||||
"| c_char | char(32) | NO | | NULL | |\n" +
|
||||
"| c_smallint | smallint(6) | NO | | 0 | |\n" +
|
||||
"| c_mediumint | mediumint(9) | NO | | NULL | |\n" +
|
||||
"| c_int | int(11) | NO | MUL | NULL | |\n" +
|
||||
"| c_bigint | bigint(20) | NO | | NULL | |\n" +
|
||||
"| c_date | date | NO | | 0000-00-00 | |\n" +
|
||||
"| c_datetime | datetime | NO | | 0000-00-00 00:00:00 | |\n" +
|
||||
"| c_timestamp | timestamp | NO | | NULL | on update CURRENT_TIMESTAMP |\n" +
|
||||
"| c_time | time | NO | | NULL | |\n" +
|
||||
"| c_varchar | varchar(10) | NO | | hello | |\n" +
|
||||
"| c_blob | blob | NO | | NULL | |\n" +
|
||||
"| c_text | text | NO | | NULL | |\n" +
|
||||
"| c_mediumtext | mediumtext | NO | | NULL | |\n" +
|
||||
"| c_longblob | longblob | NO | | NULL | |\n" +
|
||||
"+--------------+--------------+------+-----+---------------------+-----------------------------+\n"
|
||||
|
||||
, repository.console("show columns from test1"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user