Skip to content

Commit

Permalink
for #701: remove parameters count dependency from route API
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Apr 5, 2018
1 parent 48c51dd commit 41999da
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ public final class InsertStatement extends DMLStatement {
* Append generate key token.
*
* @param shardingRule databases and tables sharding rule
* @param parametersSize parameters size
*/
public void appendGenerateKeyToken(final ShardingRule shardingRule, final int parametersSize) {
public void appendGenerateKeyToken(final ShardingRule shardingRule) {
if (null != generatedKey) {
return;
}
Expand All @@ -82,26 +81,22 @@ public void appendGenerateKeyToken(final ShardingRule shardingRule, final int pa
return;
}
ItemsToken valuesToken = new ItemsToken(generatedKeysToken.get().getBeginPosition());
if (0 == parametersSize) {
appendGenerateKeyToken(shardingRule, tableRule.get(), valuesToken);
} else {
appendGenerateKeyToken(shardingRule, tableRule.get(), valuesToken, parametersSize);
}
appendGenerateKeyToken(shardingRule, tableRule.get(), valuesToken);
getSqlTokens().remove(generatedKeysToken.get());
getSqlTokens().add(valuesToken);
}

private void appendGenerateKeyToken(final ShardingRule shardingRule, final TableRule tableRule, final ItemsToken valuesToken) {
Number generatedKey = shardingRule.generateKey(tableRule.getLogicTable());
valuesToken.getItems().add(generatedKey.toString());
getConditions().add(new Condition(new Column(tableRule.getGenerateKeyColumn(), tableRule.getLogicTable()), new SQLNumberExpression(generatedKey)), shardingRule);
this.generatedKey = new GeneratedKey(tableRule.getLogicTable(), -1, generatedKey);
}

private void appendGenerateKeyToken(final ShardingRule shardingRule, final TableRule tableRule, final ItemsToken valuesToken, final int parametersSize) {
valuesToken.getItems().add(Symbol.QUESTION.getLiterals());
getConditions().add(new Condition(new Column(tableRule.getGenerateKeyColumn(), tableRule.getLogicTable()), new SQLPlaceholderExpression(parametersSize)), shardingRule);
generatedKey = new GeneratedKey(tableRule.getGenerateKeyColumn(), parametersSize, null);
if (0 == getParametersIndex()) {
Number generatedKey = shardingRule.generateKey(tableRule.getLogicTable());
valuesToken.getItems().add(generatedKey.toString());
getConditions().add(new Condition(new Column(tableRule.getGenerateKeyColumn(), tableRule.getLogicTable()), new SQLNumberExpression(generatedKey)), shardingRule);
this.generatedKey = new GeneratedKey(tableRule.getLogicTable(), -1, generatedKey);
} else {
valuesToken.getItems().add(Symbol.QUESTION.getLiterals());
getConditions().add(new Condition(new Column(tableRule.getGenerateKeyColumn(), tableRule.getLogicTable()), new SQLPlaceholderExpression(getParametersIndex())), shardingRule);
generatedKey = new GeneratedKey(tableRule.getGenerateKeyColumn(), getParametersIndex(), null);
}
}

private Optional<GeneratedKeyToken> findGeneratedKeyToken() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public PreparedStatementRoutingEngine(final String logicSQL, final ShardingRule
*/
public SQLRouteResult route(final List<Object> parameters) {
if (null == sqlStatement) {
sqlStatement = sqlRouter.parse(logicSQL, parameters.size());
sqlStatement = sqlRouter.parse(logicSQL);
}
return sqlRouter.route(logicSQL, parameters, sqlStatement);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public StatementRoutingEngine(final ShardingRule shardingRule, final DatabaseTyp
* @return route result
*/
public SQLRouteResult route(final String logicSQL) {
SQLStatement sqlStatement = sqlRouter.parse(logicSQL, 0);
SQLStatement sqlStatement = sqlRouter.parse(logicSQL);
return sqlRouter.route(logicSQL, Collections.emptyList(), sqlStatement);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public final class DatabaseHintSQLRouter implements SQLRouter {
private final boolean showSQL;

@Override
public SQLStatement parse(final String logicSQL, final int parametersSize) {
public SQLStatement parse(final String logicSQL) {
return new SQLJudgeEngine(logicSQL).judge();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ public final class ParsingSQLRouter implements SQLRouter {
private final List<Number> generatedKeys = new LinkedList<>();

@Override
public SQLStatement parse(final String logicSQL, final int parametersSize) {
public SQLStatement parse(final String logicSQL) {
SQLParsingEngine parsingEngine = new SQLParsingEngine(databaseType, logicSQL, shardingRule);
SQLStatement result = parsingEngine.parse();
if (result instanceof InsertStatement) {
((InsertStatement) result).appendGenerateKeyToken(shardingRule, parametersSize);
((InsertStatement) result).appendGenerateKeyToken(shardingRule);
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ public interface SQLRouter {
* Parse SQL.
*
* @param logicSQL logic SQL
* @param parametersSize parameters size
* @return parse result
*/
SQLStatement parse(String logicSQL, int parametersSize);
SQLStatement parse(String logicSQL);

/**
* Route SQL.
Expand Down

0 comments on commit 41999da

Please sign in to comment.