support mysql invisible index

This commit is contained in:
高铁
2021-06-07 00:18:59 +08:00
parent deb60688f1
commit af4fe59581
15 changed files with 494 additions and 4 deletions
@@ -21,6 +21,7 @@ public class SQLIndexOptions extends SQLObjectImpl {
private String algorithm;
private String lock;
private boolean invisible;
private boolean visible;
private List<SQLAssignItem> otherOptions = new ArrayList<SQLAssignItem>();
public SQLIndexOptions() {
@@ -133,6 +134,14 @@ public class SQLIndexOptions extends SQLObjectImpl {
this.invisible = invisible;
}
public boolean isVisible() {
return visible;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
public List<SQLAssignItem> getOtherOptions() {
return otherOptions;
}
@@ -0,0 +1,227 @@
/*
* Copyright 1999-2017 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 com.alibaba.druid.sql.ast.*;
import com.alibaba.druid.sql.dialect.mysql.ast.MySqlKey;
import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlTableIndex;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
import java.util.List;
public class SQLAlterTableAlterIndex extends SQLObjectImpl implements SQLAlterTableItem, SQLIndex {
private SQLIndexDefinition indexDefinition = new SQLIndexDefinition();
public SQLAlterTableAlterIndex() {
indexDefinition.setParent(this);
}
public SQLIndexDefinition getIndexDefinition() {
return indexDefinition;
}
@Override
protected void accept0(SQLASTVisitor visitor) {
if (visitor.visit(this)) {
if (indexDefinition.getName() != null) {
indexDefinition.getName().accept(visitor);
}
for (int i = 0; i < getColumns().size(); i++) {
final SQLSelectOrderByItem item = getColumns().get(i);
if (item != null) {
item.accept(visitor);
}
}
for (int i = 0; i < getCovering().size(); i++) {
final SQLName item = getCovering().get(i);
if (item != null) {
item.accept(visitor);
}
}
}
visitor.endVisit(this);
}
public boolean isUnique() {
return indexDefinition.getType() != null && indexDefinition.getType().equalsIgnoreCase("UNIQUE");
}
public void setUnique(boolean unique) {
indexDefinition.setType("UNIQUE");
}
public List<SQLSelectOrderByItem> getItems() {
return indexDefinition.getColumns();
}
public void addItem(SQLSelectOrderByItem item) {
if (item != null) {
item.setParent(this);
}
indexDefinition.getColumns().add(item);
}
public SQLName getName() {
return indexDefinition.getName();
}
public void setName(SQLName name) {
indexDefinition.setName(name);
}
public String getType() {
return indexDefinition.getType();
}
public void setType(String type) {
indexDefinition.setType(type);
}
public String getUsing() {
if (indexDefinition.hasOptions()) {
return indexDefinition.getOptions().getIndexType();
}
return null;
}
public void setUsing(String using) {
indexDefinition.getOptions().setIndexType(using);
}
public boolean isKey() {
return indexDefinition.isKey();
}
public void setKey(boolean key) {
indexDefinition.setKey(key);
}
public void cloneTo(MySqlTableIndex x) {
indexDefinition.cloneTo(x.getIndexDefinition());
}
public void cloneTo(MySqlKey x) {
indexDefinition.cloneTo(x.getIndexDefinition());
}
public SQLExpr getComment() {
if (indexDefinition.hasOptions()) {
return indexDefinition.getOptions().getComment();
}
return null;
}
public void setComment(SQLExpr comment) {
if (comment != null) {
comment.setParent(this);
}
indexDefinition.getOptions().setComment(comment);
}
public SQLExpr getKeyBlockSize() {
if (indexDefinition.hasOptions()) {
return indexDefinition.getOptions().getKeyBlockSize();
}
return null;
}
public void setKeyBlockSize(SQLExpr keyBlockSize) {
indexDefinition.getOptions().setKeyBlockSize(keyBlockSize);
}
public String getParserName() {
if (indexDefinition.hasOptions()) {
return indexDefinition.getOptions().getParserName();
}
return null;
}
public void setParserName(String parserName) {
indexDefinition.getOptions().setParserName(parserName);
}
public boolean isHashMapType() {
return indexDefinition.isHashMapType();
}
public void setHashMapType(boolean hashMapType) {
indexDefinition.setHashMapType(hashMapType);
}
protected SQLExpr getOption(long hash64) {
return indexDefinition.getOption(hash64);
}
public String getDistanceMeasure() {
return indexDefinition.getDistanceMeasure();
}
public String getAlgorithm() {
return indexDefinition.getAlgorithm();
}
public void addOption(String name, SQLExpr value) {
indexDefinition.addOption(name, value);
}
public List<SQLAssignItem> getOptions() {
return indexDefinition.getCompatibleOptions();
}
public boolean isGlobal() {
return indexDefinition.isGlobal();
}
public void setGlobal(boolean global) {
indexDefinition.setGlobal(global);
}
public SQLExpr getDbPartitionBy() {
return indexDefinition.getDbPartitionBy();
}
public void setDbPartitionBy(SQLExpr x) {
indexDefinition.setDbPartitionBy(x);
}
public SQLExpr getTablePartitionBy() {
return indexDefinition.getTbPartitionBy();
}
public void setTablePartitionBy(SQLExpr x) {
indexDefinition.setTbPartitionBy(x);
}
public SQLExpr getTablePartitions() {
return indexDefinition.getTbPartitions();
}
public void setTablePartitions(SQLExpr x) {
indexDefinition.setTbPartitions(x);
}
@Override
public List<SQLName> getCovering() {
return indexDefinition.getCovering();
}
@Override
public List<SQLSelectOrderByItem> getColumns() {
return indexDefinition.getColumns();
}
}
@@ -6531,8 +6531,19 @@ public class MySqlStatementParser extends SQLStatementParser {
if (lexer.token() == Token.INDEX) {
// Caution: Not in MySql documents.
lexer.nextToken();
MySqlAlterTableAlterFullTextIndex alterIndex = new MySqlAlterTableAlterFullTextIndex();
SQLName indexName = this.exprParser.name();
if (lexer.identifierEquals("VISIBLE")) {
SQLAlterTableAlterIndex alterIndex = new SQLAlterTableAlterIndex();
alterIndex.setName(indexName);
lexer.nextToken();
alterIndex.getIndexDefinition().getOptions().setVisible(true);
stmt.addItem(alterIndex);
break;
}
MySqlAlterTableAlterFullTextIndex alterIndex = new MySqlAlterTableAlterFullTextIndex();
alterIndex.setIndexName(indexName);
accept(Token.SET);
@@ -4780,6 +4780,10 @@ public class SQLExprParser extends SQLParser {
lexer.identifierEquals("INVISIBLE")) {
lexer.nextToken();
indexDefinition.getOptions().setInvisible(true);
} else if ((DbType.mysql == dbType) &&
lexer.identifierEquals("VISIBLE")) {
lexer.nextToken();
indexDefinition.getOptions().setVisible(true);
} else if ((DbType.mysql == dbType) &&
lexer.token == Token.HINT && lexer.stringVal().trim().equals("!80000 INVISIBLE")) {
lexer.nextToken();
@@ -6035,6 +6035,8 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
if (x.isInvisible()) {
print0(ucase ? " INVISIBLE" : " invisible");
} else if (x.isVisible()) {
print0(ucase ? " VISIBLE" : " visible");
}
SQLExpr keyBlockSize = x.getKeyBlockSize();
@@ -6128,9 +6130,11 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
print0(ucase ? "HASHMAP " : "hashmap ");
}
print('(');
printAndAccept(x.getColumns(), ", ");
print(')');
if (x.getColumns().size() > 0) {
print('(');
printAndAccept(x.getColumns(), ", ");
print(')');
}
if (x.getAnalyzerName() != null) {
print0(ucase ? " WITH ANALYZER " : " with analyzer ");
@@ -6193,6 +6197,15 @@ public class SQLASTOutputVisitor extends SQLASTVisitorAdapter implements Paramet
return false;
}
@Override
public boolean visit(SQLAlterTableAlterIndex x) {
print0(ucase ? "ALTER " : "alter ");
visit(x.getIndexDefinition());
return false;
}
@Override
public boolean visit(SQLAlterTableAddConstraint x) {
if (x.isWithNoCheck()) {
@@ -881,6 +881,14 @@ public interface SQLASTVisitor {
return true;
}
default void endVisit(SQLAlterTableAlterIndex x) {
}
default boolean visit(SQLAlterTableAlterIndex x) {
return true;
}
default void endVisit(SQLAlterTableAddConstraint x) {
}