Skip to content

Commit

Permalink
Merge pull request #1297 from beckhampu/dev
Browse files Browse the repository at this point in the history
Support multiple schema configuration for Sharding-proxy
  • Loading branch information
terrymanu authored Sep 23, 2018

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents 945ed17 + fe4f3b9 commit 75b442c
Showing 22 changed files with 421 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -22,12 +22,14 @@
import io.shardingsphere.core.merger.QueryResult;
import io.shardingsphere.core.merger.dal.show.ShowCreateTableMergedResult;
import io.shardingsphere.core.merger.dal.show.ShowDatabasesMergedResult;
import io.shardingsphere.core.merger.dal.show.ShowIndexMergedResult;
import io.shardingsphere.core.merger.dal.show.ShowOtherMergedResult;
import io.shardingsphere.core.merger.dal.show.ShowTableStatusMergedResult;
import io.shardingsphere.core.merger.dal.show.ShowTablesMergedResult;
import io.shardingsphere.core.metadata.table.ShardingTableMetaData;
import io.shardingsphere.core.parsing.parser.dialect.mysql.statement.ShowCreateTableStatement;
import io.shardingsphere.core.parsing.parser.dialect.mysql.statement.ShowDatabasesStatement;
import io.shardingsphere.core.parsing.parser.dialect.mysql.statement.ShowIndexStatement;
import io.shardingsphere.core.parsing.parser.dialect.mysql.statement.ShowTableStatusStatement;
import io.shardingsphere.core.parsing.parser.dialect.mysql.statement.ShowTablesStatement;
import io.shardingsphere.core.parsing.parser.sql.dal.DALStatement;
@@ -68,6 +70,9 @@ public MergedResult merge() throws SQLException {
if (dalStatement instanceof ShowCreateTableStatement) {
return new ShowCreateTableMergedResult(shardingRule, queryResults, shardingTableMetaData);
}
if (dalStatement instanceof ShowIndexStatement) {
return new ShowIndexMergedResult(shardingRule, queryResults, shardingTableMetaData);
}
return new ShowOtherMergedResult(queryResults.get(0));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* 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.
* </p>
*/

package io.shardingsphere.core.merger.dal.show;

import io.shardingsphere.core.merger.QueryResult;
import io.shardingsphere.core.metadata.table.ShardingTableMetaData;
import io.shardingsphere.core.rule.ShardingRule;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* Merged result for show index.
*
* @author chenqingyang
*/
public final class ShowIndexMergedResult extends LogicTablesMergedResult {

private static final Map<String, Integer> LABEL_AND_INDEX_MAP = new HashMap<>(13, 1);

static {
LABEL_AND_INDEX_MAP.put("Table", 1);
LABEL_AND_INDEX_MAP.put("Non_unique", 2);
LABEL_AND_INDEX_MAP.put("Key_name", 3);
LABEL_AND_INDEX_MAP.put("Seq_in_index", 4);
LABEL_AND_INDEX_MAP.put("Column_name", 5);
LABEL_AND_INDEX_MAP.put("Collation", 6);
LABEL_AND_INDEX_MAP.put("Cardinality", 7);
LABEL_AND_INDEX_MAP.put("Sub_part", 8);
LABEL_AND_INDEX_MAP.put("Packed", 9);
LABEL_AND_INDEX_MAP.put("Null", 10);
LABEL_AND_INDEX_MAP.put("Index_type", 11);
LABEL_AND_INDEX_MAP.put("Comment", 12);
LABEL_AND_INDEX_MAP.put("Index_comment", 13);
}

public ShowIndexMergedResult(final ShardingRule shardingRule, final List<QueryResult> queryResults, final ShardingTableMetaData shardingTableMetaData) throws SQLException {
super(LABEL_AND_INDEX_MAP, shardingRule, queryResults, shardingTableMetaData);
}
}
Original file line number Diff line number Diff line change
@@ -44,4 +44,16 @@ public final class ShowTablesMergedResult extends LogicTablesMergedResult {
public ShowTablesMergedResult(final ShardingRule shardingRule, final List<QueryResult> queryResults, final ShardingTableMetaData shardingTableMetaData) throws SQLException {
super(LABEL_AND_INDEX_MAP, shardingRule, queryResults, shardingTableMetaData);
}

/**
* Reset column label.
*
* @param schema schema
*/
public void resetColumnLabel(final String schema) {
Map<String, Integer> labelAndIndexMapnew = new HashMap<>(1, 1);
labelAndIndexMapnew.put(schema, 1);
resetLabelAndIndexMap(labelAndIndexMapnew);
}

}
Original file line number Diff line number Diff line change
@@ -47,6 +47,11 @@ public abstract class MemoryMergedResult implements MergedResult {

private boolean wasNull;

protected final void resetLabelAndIndexMap(final Map<String, Integer> labelAndIndexMap) {
this.labelAndIndexMap.clear();
this.labelAndIndexMap.putAll(labelAndIndexMap);
}

@Override
public final Object getValue(final int columnIndex, final Class<?> type) throws SQLException {
if (Blob.class == type || Clob.class == type || Reader.class == type || InputStream.class == type || SQLXML.class == type) {
Original file line number Diff line number Diff line change
@@ -24,11 +24,15 @@
import io.shardingsphere.core.parsing.lexer.token.Assist;
import io.shardingsphere.core.parsing.lexer.token.DefaultKeyword;
import io.shardingsphere.core.parsing.lexer.token.Keyword;
import io.shardingsphere.core.parsing.lexer.token.Symbol;
import io.shardingsphere.core.parsing.lexer.token.TokenType;
import io.shardingsphere.core.parsing.parser.dialect.mysql.statement.DescribeStatement;
import io.shardingsphere.core.parsing.parser.dialect.mysql.statement.SetStatement;
import io.shardingsphere.core.parsing.parser.dialect.mysql.statement.ShowColumnsStatement;
import io.shardingsphere.core.parsing.parser.dialect.mysql.statement.ShowDatabasesStatement;
import io.shardingsphere.core.parsing.parser.dialect.mysql.statement.ShowIndexStatement;
import io.shardingsphere.core.parsing.parser.dialect.mysql.statement.ShowOtherStatement;
import io.shardingsphere.core.parsing.parser.dialect.mysql.statement.ShowTableStatusStatement;
import io.shardingsphere.core.parsing.parser.dialect.mysql.statement.ShowTablesStatement;
import io.shardingsphere.core.parsing.parser.dialect.mysql.statement.UseStatement;
import io.shardingsphere.core.parsing.parser.exception.SQLParsingException;
@@ -41,6 +45,7 @@
import io.shardingsphere.core.parsing.parser.sql.dql.DQLStatement;
import io.shardingsphere.core.parsing.parser.sql.dql.select.SelectStatement;
import io.shardingsphere.core.parsing.parser.sql.tcl.TCLStatement;
import io.shardingsphere.core.parsing.parser.token.SchemaToken;
import lombok.RequiredArgsConstructor;

/**
@@ -72,7 +77,7 @@ public SQLStatement judge() {
return getDMLStatement(tokenType);
}
if (TCLStatement.isTCL(tokenType)) {
return getTCLStatement();
return getTCLStatement(tokenType);
}
if (DALStatement.isDAL(tokenType)) {
return getDALStatement(tokenType, lexerEngine);
@@ -113,7 +118,10 @@ private SQLStatement getDCLStatement() {
return new DCLStatement();
}

private SQLStatement getTCLStatement() {
private SQLStatement getTCLStatement(final TokenType tokenType) {
if (DefaultKeyword.SET == tokenType) {
return new SetStatement();
}
return new TCLStatement();
}

@@ -130,15 +138,73 @@ private SQLStatement getDALStatement(final TokenType tokenType, final LexerEngin

private SQLStatement getShowStatement(final LexerEngine lexerEngine) {
lexerEngine.nextToken();
if (MySQLKeyword.DATABASES == lexerEngine.getCurrentToken().getType()) {
lexerEngine.skipIfEqual(DefaultKeyword.FULL);
if (lexerEngine.equalAny(MySQLKeyword.DATABASES)) {
return new ShowDatabasesStatement();
}
if (MySQLKeyword.TABLES == lexerEngine.getCurrentToken().getType()) {
return new ShowTablesStatement();
if (lexerEngine.skipIfEqual(DefaultKeyword.TABLE, MySQLKeyword.STATUS)) {
return parseShowTableStatus(lexerEngine);
}
if (lexerEngine.skipIfEqual(MySQLKeyword.TABLES)) {
return parseShowTables(lexerEngine);
}
if (lexerEngine.skipIfEqual(MySQLKeyword.COLUMNS, MySQLKeyword.FIELDS)) {
return parseShowColumnsFields(lexerEngine);
}
if (MySQLKeyword.COLUMNS == lexerEngine.getCurrentToken().getType()) {
return new ShowColumnsStatement();
if (lexerEngine.skipIfEqual(DefaultKeyword.INDEX, MySQLKeyword.INDEXES, MySQLKeyword.KEYS)) {
return parseShowIndex(lexerEngine);
}
return new ShowOtherStatement();
}

private DALStatement parseShowTables(final LexerEngine lexerEngine) {
DALStatement result = new ShowTablesStatement();
if (lexerEngine.skipIfEqual(DefaultKeyword.FROM, DefaultKeyword.IN)) {
int beginPosition = lexerEngine.getCurrentToken().getEndPosition() - lexerEngine.getCurrentToken().getLiterals().length();
result.getSqlTokens().add(new SchemaToken(beginPosition, lexerEngine.getCurrentToken().getLiterals(), null));
}
return result;
}

private DALStatement parseShowColumnsFields(final LexerEngine lexerEngine) {
DALStatement result = new ShowColumnsStatement();
lexerEngine.skipIfEqual(DefaultKeyword.FROM, DefaultKeyword.IN);
parseSingleTableWithSchema(lexerEngine, result);
if (lexerEngine.skipIfEqual(DefaultKeyword.FROM, DefaultKeyword.IN)) {
int beginPosition = lexerEngine.getCurrentToken().getEndPosition() - lexerEngine.getCurrentToken().getLiterals().length();
result.getSqlTokens().add(new SchemaToken(beginPosition, lexerEngine.getCurrentToken().getLiterals(), null));
}
return result;
}

private void parseSingleTableWithSchema(final LexerEngine lexerEngine, final SQLStatement sqlStatement) {
int beginPosition = lexerEngine.getCurrentToken().getEndPosition() - lexerEngine.getCurrentToken().getLiterals().length();
String literals = lexerEngine.getCurrentToken().getLiterals();
lexerEngine.nextToken();
if (lexerEngine.skipIfEqual(Symbol.DOT)) {
sqlStatement.getSqlTokens().add(new SchemaToken(beginPosition, literals, null));
lexerEngine.nextToken();
}
}

private DALStatement parseShowIndex(final LexerEngine lexerEngine) {
DALStatement result = new ShowIndexStatement();
lexerEngine.skipIfEqual(DefaultKeyword.FROM, DefaultKeyword.IN);
parseSingleTableWithSchema(lexerEngine, result);
if (lexerEngine.skipIfEqual(DefaultKeyword.FROM, DefaultKeyword.IN)) {
int beginPosition = lexerEngine.getCurrentToken().getEndPosition() - lexerEngine.getCurrentToken().getLiterals().length();
result.getSqlTokens().add(new SchemaToken(beginPosition, lexerEngine.getCurrentToken().getLiterals(), null));
}
return result;
}

private DALStatement parseShowTableStatus(final LexerEngine lexerEngine) {
DALStatement result = new ShowTableStatusStatement();
lexerEngine.nextToken();
if (lexerEngine.skipIfEqual(DefaultKeyword.FROM, DefaultKeyword.IN)) {
int beginPosition = lexerEngine.getCurrentToken().getEndPosition() - lexerEngine.getCurrentToken().getLiterals().length();
result.getSqlTokens().add(new SchemaToken(beginPosition, lexerEngine.getCurrentToken().getLiterals(), null));
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -95,6 +95,7 @@ protected final void parseTableFactor(final SQLStatement sqlStatement, final boo
if (lexerEngine.skipIfEqual(Symbol.DOT)) {
skippedSchemaNameLength = literals.length() + Symbol.DOT.getLiterals().length();
literals = lexerEngine.getCurrentToken().getLiterals();
lexerEngine.nextToken();
}
String tableName = SQLUtil.getExactlyValue(literals);
if (Strings.isNullOrEmpty(tableName)) {
Original file line number Diff line number Diff line change
@@ -17,10 +17,12 @@

package io.shardingsphere.core.parsing.parser.dialect.mysql.sql;

import com.google.common.base.Optional;
import io.shardingsphere.core.parsing.lexer.LexerEngine;
import io.shardingsphere.core.parsing.lexer.dialect.mysql.MySQLKeyword;
import io.shardingsphere.core.parsing.lexer.token.DefaultKeyword;
import io.shardingsphere.core.parsing.parser.clause.TableReferencesClauseParser;
import io.shardingsphere.core.parsing.parser.context.table.Table;
import io.shardingsphere.core.parsing.parser.dialect.mysql.statement.ShowColumnsStatement;
import io.shardingsphere.core.parsing.parser.dialect.mysql.statement.ShowCreateTableStatement;
import io.shardingsphere.core.parsing.parser.dialect.mysql.statement.ShowDatabasesStatement;
@@ -32,7 +34,9 @@
import io.shardingsphere.core.parsing.parser.sql.dal.show.AbstractShowParser;
import io.shardingsphere.core.parsing.parser.token.RemoveToken;
import io.shardingsphere.core.parsing.parser.token.SchemaToken;
import io.shardingsphere.core.parsing.parser.token.TableToken;
import io.shardingsphere.core.rule.ShardingRule;
import io.shardingsphere.core.util.SQLUtil;
import lombok.RequiredArgsConstructor;

/**
@@ -82,7 +86,21 @@ private DALStatement showDatabases() {
}

private DALStatement parseShowTableStatus() {
return new ShowTableStatusStatement();
DALStatement result = new ShowTableStatusStatement();
lexerEngine.nextToken();
if (lexerEngine.equalAny(DefaultKeyword.FROM, DefaultKeyword.IN)) {
int beginPosition = lexerEngine.getCurrentToken().getEndPosition() - lexerEngine.getCurrentToken().getLiterals().length();
lexerEngine.nextToken();
result.getSqlTokens().add(new RemoveToken(beginPosition, lexerEngine.getCurrentToken().getEndPosition()));
}
lexerEngine.nextToken();
if (lexerEngine.skipIfEqual(DefaultKeyword.LIKE)) {
int beginPosition = lexerEngine.getCurrentToken().getEndPosition() - lexerEngine.getCurrentToken().getLiterals().length() - 1;
String literals = lexerEngine.getCurrentToken().getLiterals();
result.getSqlTokens().add(new TableToken(beginPosition, 0, literals));
result.getTables().add(new Table(SQLUtil.getExactlyValue(literals), Optional.<String>absent()));
}
return result;
}

private DALStatement parseShowTables() {
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2016-2018 shardingsphere.io.
* <p>
* 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.
* </p>
*/

package io.shardingsphere.core.parsing.parser.dialect.mysql.statement;

import io.shardingsphere.core.parsing.parser.sql.tcl.TCLStatement;

/**
* Set statement.
*
* @author chenqingyang
*/
public final class SetStatement extends TCLStatement {
}
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@
* @author zhangliang
*/
@ToString(callSuper = true)
public final class TCLStatement extends AbstractSQLStatement {
public class TCLStatement extends AbstractSQLStatement {

private static final Collection<Keyword> STATEMENT_PREFIX = Arrays.<Keyword>asList(
DefaultKeyword.SET, DefaultKeyword.COMMIT, DefaultKeyword.ROLLBACK, DefaultKeyword.SAVEPOINT, DefaultKeyword.BEGIN);
Original file line number Diff line number Diff line change
@@ -29,6 +29,7 @@
import io.shardingsphere.core.routing.SQLUnit;
import io.shardingsphere.core.routing.type.TableUnit;
import io.shardingsphere.core.rule.DataNode;
import io.shardingsphere.core.rule.MasterSlaveRule;
import io.shardingsphere.core.rule.ShardingRule;

import java.util.ArrayList;
@@ -118,6 +119,25 @@ public SQLUnit toSQL(final TableUnit tableUnit, final Map<String, String> logicA
return new SQLUnit(result.toString(), parameterSets);
}

/**
* Convert to sql for master slave rule.
*
* @param masterSlaveRule master slave rule
* @param shardingDataSourceMetaData sharding data source meta data
* @return SQL
*/
public String toSQL(final MasterSlaveRule masterSlaveRule, final ShardingDataSourceMetaData shardingDataSourceMetaData) {
StringBuilder result = new StringBuilder();
for (Object each : segments) {
if (each instanceof SchemaPlaceholder) {
result.append(shardingDataSourceMetaData.getActualDataSourceMetaData(masterSlaveRule.getMasterDataSourceName()).getSchemeName());
} else {
result.append(each);
}
}
return result.toString();
}

private void appendTablePlaceholder(final TablePlaceholder tablePlaceholder, final String actualTableName, final StringBuilder stringBuilder) {
final String logicTableName = tablePlaceholder.getLogicTableName();
final String originalLiterals = tablePlaceholder.getOriginalLiterals();
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@
import io.shardingsphere.core.parsing.parser.token.ItemsToken;
import io.shardingsphere.core.parsing.parser.token.OffsetToken;
import io.shardingsphere.core.parsing.parser.token.OrderByToken;
import io.shardingsphere.core.parsing.parser.token.RemoveToken;
import io.shardingsphere.core.parsing.parser.token.RowCountToken;
import io.shardingsphere.core.parsing.parser.token.SQLToken;
import io.shardingsphere.core.parsing.parser.token.SchemaToken;
@@ -138,6 +139,8 @@ public SQLBuilder rewrite(final boolean isRewriteLimit) {
appendOrderByToken(result, count, sqlTokens);
} else if (each instanceof InsertColumnToken) {
appendSymbolToken(result, (InsertColumnToken) each, count, sqlTokens);
} else if (each instanceof RemoveToken) {
appendRest(result, count, sqlTokens, ((RemoveToken) each).getEndPosition());
}
count++;
}
Loading

0 comments on commit 75b442c

Please sign in to comment.