Skip to content

Commit

Permalink
For #525 4th.
Browse files Browse the repository at this point in the history
  • Loading branch information
haocao committed Jan 3, 2018
1 parent 16eb573 commit af45853
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ public class TableRuleConfiguration {

private String logicTable;

private String logicIndex;

private String actualDataNodes;

private ShardingStrategyConfiguration databaseShardingStrategyConfig;
Expand All @@ -55,6 +53,8 @@ public class TableRuleConfiguration {

private String keyGeneratorClass;

private String logicIndex;

/**
* Build table rule.
*
Expand All @@ -67,6 +67,6 @@ public TableRule build(final Map<String, DataSource> dataSourceMap) {
ShardingStrategy databaseShardingStrategy = null == databaseShardingStrategyConfig ? null : databaseShardingStrategyConfig.build();
ShardingStrategy tableShardingStrategy = null == tableShardingStrategyConfig ? null : tableShardingStrategyConfig.build();
KeyGenerator keyGenerator = !Strings.isNullOrEmpty(keyGeneratorColumnName) && !Strings.isNullOrEmpty(keyGeneratorClass) ? KeyGeneratorFactory.newInstance(keyGeneratorClass) : null;
return new TableRule(logicTable, logicIndex, actualDataNodes, dataSourceMap, databaseShardingStrategy, tableShardingStrategy, keyGeneratorColumnName, keyGenerator);
return new TableRule(logicTable, actualDataNodes, dataSourceMap, databaseShardingStrategy, tableShardingStrategy, keyGeneratorColumnName, keyGenerator, logicIndex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
*/
@RequiredArgsConstructor
@Getter
@Setter
@ToString
public abstract class AbstractSQLStatement implements SQLStatement {

Expand All @@ -48,6 +47,7 @@ public abstract class AbstractSQLStatement implements SQLStatement {

private final List<SQLToken> sqlTokens = new LinkedList<>();

@Setter
private int parametersIndex;

private boolean containsTableName = true;
Expand All @@ -63,12 +63,7 @@ public int increaseParametersIndex() {
}

@Override
public void setContainsTableName(final boolean containsTableName) {
this.containsTableName = containsTableName;
}

@Override
public boolean containsTableName() {
return this.containsTableName;
public void withoutTableName() {
containsTableName = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,21 @@ public interface SQLStatement {
void setParametersIndex(int parametersIndex);

/**
* 增加索引偏移量.
* Increase parameters index.
*
* @return 增加后的索引偏移量
* @return increased parameters index
*/
int increaseParametersIndex();

/**
* Set contain table name.
* Without table name.
*/
void setContainsTableName(boolean containsTableName);
void withoutTableName();

/**
* Adjust contains table name is empty or not.
*
* @return table name is empty or not
*/
boolean containsTableName();
boolean isContainsTableName();
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ public DDLStatement parse() {
return result;
}

protected abstract Keyword[] getSkippedKeywordsBetweenCreateIndexAndKeyword();

protected abstract Keyword[] getSkippedKeywordsBetweenCreateAndKeyword();

protected Keyword[] getSkippedKeywordsBetweenCreateIndexAndIndexName() {
return new Keyword[] {};
}

private void parseIndex(final DDLStatement ddlStatement) {
Token currentToken = lexerEngine.getCurrentToken();
int beginPosition = currentToken.getEndPosition() - currentToken.getLiterals().length();
Expand All @@ -78,13 +86,5 @@ private void parseIndex(final DDLStatement ddlStatement) {
ddlStatement.getSqlTokens().add(new IndexToken(beginPosition, literals, tableName));
}

protected Keyword[] getSkippedKeywordsBetweenCreateIndexAndIndexName() {
return new Keyword[] {};
}

protected abstract Keyword[] getSkippedKeywordsBetweenCreateAndKeyword();

protected abstract Keyword[] getSkippedKeywordsBetweenCreateTableAndTableName();

protected abstract Keyword[] getSkippedKeywordsBetweenCreateIndexAndKeyword();
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,18 @@ public DDLStatement parse() {
throw new SQLParsingException("Can't support other DROP grammar unless DROP TABLE, DROP INDEX.");
}
tableReferencesClauseParser.parse(result, true);
for (SQLToken each : result.getSqlTokens()) {
if (each instanceof IndexToken && result.getSqlTokens().size() == 1) {
result.setContainsTableName(false);
}
}
indexWithoutTable(result);
return result;
}

protected Keyword[] getSkippedKeywordsBetweenDropAndTable() {
return new Keyword[0];
}

protected Keyword[] getSkippedKeywordsBetweenDropIndexAndIndexName() {
return new Keyword[] {};
}

private void parseIndex(final DDLStatement ddlStatement) {
Token currentToken = lexerEngine.getCurrentToken();
int beginPosition = currentToken.getEndPosition() - currentToken.getLiterals().length();
Expand All @@ -83,13 +87,13 @@ private void parseIndex(final DDLStatement ddlStatement) {
ddlStatement.getSqlTokens().add(new IndexToken(beginPosition, literals, tableName));
}

protected Keyword[] getSkippedKeywordsBetweenDropIndexAndIndexName() {
return new Keyword[] {};
}
protected abstract Keyword[] getSkippedKeywordsBetweenDropTableAndTableName();

protected Keyword[] getSkippedKeywordsBetweenDropAndTable() {
return new Keyword[0];
private void indexWithoutTable(final DDLStatement ddlStatement) {
for (SQLToken each : ddlStatement.getSqlTokens()) {
if (each instanceof IndexToken && 1 == ddlStatement.getSqlTokens().size()) {
ddlStatement.withoutTableName();
}
}
}

protected abstract Keyword[] getSkippedKeywordsBetweenDropTableAndTableName();
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private SelectStatement parseInternal() {
return result;
}

protected abstract void parseInternal(final SelectStatement selectStatement);
protected abstract void parseInternal(SelectStatement selectStatement);

protected final void parseDistinct() {
selectClauseParserFacade.getDistinctClauseParser().parse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public SQLRouteResult route(final String logicSQL, final List<Object> parameters
private RoutingResult route(final List<Object> parameters, final SQLStatement sqlStatement) {
Collection<String> tableNames = sqlStatement.getTables().getTableNames();
RoutingEngine routingEngine;
if (sqlStatement instanceof DDLStatement && !sqlStatement.containsTableName() && tableNames.size() == 0) {
if (sqlStatement instanceof DDLStatement && !sqlStatement.isContainsTableName()) {
routingEngine = new NoneTableRoutingEngine(shardingRule, parameters, sqlStatement);
} else if (tableNames.isEmpty()) {
routingEngine = new DatabaseAllRoutingEngine(shardingRule.getDataSourceMap());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ public RoutingResult route() {
}

private String getLogicTableName() {
Preconditions.checkState(sqlStatement.getSqlTokens().size() == 1);
Preconditions.checkState(1 == sqlStatement.getSqlTokens().size());
IndexToken indexToken = (IndexToken) sqlStatement.getSqlTokens().get(0);
String indexName = indexToken.getIndexName();
String logicTableName = "";
String result = "";
for (TableRule each : shardingRule.getTableRules()) {
if (indexName.equals(each.getLogicIndex())) {
logicTableName = each.getLogicTable();
result = each.getLogicTable();
}
}
return logicTableName;
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private TableRule createTableRuleWithDefaultDataSource(final String logicTableNa
config.setLogicTable(logicTableName);
config.setDatabaseShardingStrategyConfig(new NoneShardingStrategyConfiguration());
config.setTableShardingStrategyConfig(new NoneShardingStrategyConfiguration());
return new TableRule(logicTableName, null, null, defaultDataSourceMap, null, null, null, null);
return new TableRule(logicTableName, null, defaultDataSourceMap, null, null, null, null, null);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ public final class TableRule {

private final String logicTable;

private final String logicIndex;

private final List<DataNode> actualDataNodes;

private final ShardingStrategy databaseShardingStrategy;
Expand All @@ -53,15 +51,18 @@ public final class TableRule {

private final KeyGenerator keyGenerator;

public TableRule(final String logicTable, final String logicIndex, final List<String> actualDataNodes, final Map<String, DataSource> dataSourceMap,
final ShardingStrategy databaseShardingStrategy, final ShardingStrategy tableShardingStrategy, final String generateKeyColumn, final KeyGenerator keyGenerator) {
private final String logicIndex;

public TableRule(final String logicTable, final List<String> actualDataNodes, final Map<String, DataSource> dataSourceMap,
final ShardingStrategy databaseShardingStrategy, final ShardingStrategy tableShardingStrategy,
final String generateKeyColumn, final KeyGenerator keyGenerator, final String logicIndex) {
this.logicTable = logicTable;
this.logicIndex = logicIndex;
this.actualDataNodes = null == actualDataNodes || actualDataNodes.isEmpty() ? generateDataNodes(logicTable, dataSourceMap) : generateDataNodes(actualDataNodes, dataSourceMap);
this.databaseShardingStrategy = databaseShardingStrategy;
this.tableShardingStrategy = tableShardingStrategy;
this.generateKeyColumn = generateKeyColumn;
this.keyGenerator = keyGenerator;
this.logicIndex = logicIndex;
}

private List<DataNode> generateDataNodes(final String logicTable, final Map<String, DataSource> dataSourceMap) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,12 @@ public void cleanupDdlTables() throws SQLException {
if (getSql().startsWith("CREATE UNIQUE INDEX")) {
executeSql("DROP TABLE t_log");
} else if (getSql().startsWith("CREATE INDEX")) {
executeSql("DROP INDEX t_order_index");
if (getCurrentDatabaseType() == DatabaseType.PostgreSQL || getCurrentDatabaseType() == DatabaseType.Oracle || getCurrentDatabaseType() == DatabaseType.H2) {
executeSql("DROP INDEX t_order_index");
} else {
executeSql("DROP INDEX t_order_index ON t_order");
}

} else if (getSql().startsWith("ALTER") || getSql().startsWith("TRUNCATE") || getSql().startsWith("CREATE") || getSql().startsWith("DROP INDEX")) {
if (getSql().contains("TEMP")) {
executeSql("DROP TABLE t_temp_log");
Expand Down
6 changes: 3 additions & 3 deletions sharding-jdbc-core/src/test/resources/parser/create.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@
</assert>
` <assert id="assertCreateIndex">
<tables>
<table name="t_log" />
<table name="t_order" />
</tables>
<index-token begin-position="13" original-literals="t_log_index" table-name="t_log" />
<index-token begin-position="13" original-literals="t_order_index" table-name="t_order" />
<table-tokens>
<table-token begin-position="28" original-literals="t_log" />
<table-token begin-position="30" original-literals="t_order" />
</table-tokens>
</assert>
<assert id="assertCreateUniqueIndex">
Expand Down
2 changes: 1 addition & 1 deletion sharding-jdbc-core/src/test/resources/parser/drop.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@
<index-token begin-position="11" original-literals="t_log_index" table-name="" />
</assert>
<assert id="assertDropIndexIfExists">
<index-token begin-position="21" original-literals="t_log_index" table-name="" />
<index-token begin-position="21" original-literals="t_order_index" table-name="" />
</assert>
</asserts>
2 changes: 1 addition & 1 deletion sharding-jdbc-core/src/test/resources/sql/dql/select.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<sql id="assertSelectIterator" value="SELECT t.* FROM t_order_item t WHERE t.item_id IN (%s, %s)" />
<sql id="assertSelectNoShardingTable" value="SELECT i.* FROM t_order o JOIN t_order_item i ON o.user_id = i.user_id AND o.order_id = i.order_id ORDER BY i.item_id" />
<sql id="assertSelectLikeWithCount" value="SELECT count(0) as orders_count FROM `t_order` o WHERE o.status LIKE CONCAT('%%', %s, '%%') AND o.`user_id` IN (%s, %s) AND o.`order_id` BETWEEN %s AND %s" type="MySQL,H2" />
<sql id="assertSelectWithBindingTable" value="SELECT i.* FROM T_order o JOIN t_order_item i ON o.user_id = i.user_id AND o.order_id = i.order_id WHERE o.user_id IN (%s, %s) AND o.order_id BETWEEN %s AND %s ORDER BY i.item_id" />
<sql id="assertSelectWithBindingTable" value="SELECT i.* FROM t_order o JOIN t_order_item i ON o.user_id = i.user_id AND o.order_id = i.order_id WHERE o.user_id IN (%s, %s) AND o.order_id BETWEEN %s AND %s ORDER BY i.item_id" />
<sql id="assertSelectWithBindingTableAndConfigTable" value="SELECT i.* FROM t_order o JOIN t_order_item i ON o.user_id = i.user_id AND o.order_id = i.order_id JOIN t_config c ON o.status = c.status WHERE o.user_id IN (%s, %s) AND o.order_id BETWEEN %s AND %s AND c.status = %s ORDER BY i.item_id" />
<sql id="assertSelectCountWithBindingTable" value="SELECT COUNT(*) AS items_count FROM t_order o, t_order_item i WHERE o.user_id = i.user_id AND o.order_id = i.order_id AND o.user_id IN (%s, %s) AND o.order_id BETWEEN %s AND %s" />
<sql id="assertSelectCountWithBindingTableWithJoin" value="SELECT COUNT(*) AS items_count FROM t_order o JOIN t_order_item i ON o.user_id = i.user_id AND o.order_id = i.order_id WHERE o.user_id IN (%s, %s) AND o.order_id BETWEEN %s AND %s" />
Expand Down

0 comments on commit af45853

Please sign in to comment.