Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
Shardingsphere
提交
0f2737c7
Shardingsphere
项目概览
apache
/
Shardingsphere
通知
56
Star
3
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
Shardingsphere
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
0f2737c7
编写于
6月 05, 2019
作者:
T
terrymanu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
for #2457, add ParameterRewriter
上级
01e2e827
变更
15
显示空白变更内容
内联
并排
Showing
15 changed file
with
142 addition
and
33 deletion
+142
-33
sharding-core/sharding-core-entry/src/main/java/org/apache/shardingsphere/core/BaseShardingEngine.java
...va/org/apache/shardingsphere/core/BaseShardingEngine.java
+3
-1
sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/SQLRewriteEngine.java
.../apache/shardingsphere/core/rewrite/SQLRewriteEngine.java
+6
-1
sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/builder/ParameterBuilder.java
...shardingsphere/core/rewrite/builder/ParameterBuilder.java
+9
-3
sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/rewriter/EncryptSQLRewriter.java
...rdingsphere/core/rewrite/rewriter/EncryptSQLRewriter.java
+7
-6
sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/rewriter/ParameterRewriter.java
...ardingsphere/core/rewrite/rewriter/ParameterRewriter.java
+35
-0
sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/rewriter/ShardingParameterRewriter.java
...here/core/rewrite/rewriter/ShardingParameterRewriter.java
+52
-0
sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/rewriter/ShardingSQLRewriter.java
...dingsphere/core/rewrite/rewriter/ShardingSQLRewriter.java
+3
-2
sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/rewriter/EncryptSQLRewriterTest.java
...gsphere/core/rewrite/rewriter/EncryptSQLRewriterTest.java
+1
-1
sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/rewriter/ShardingSQLRewriterTest.java
...sphere/core/rewrite/rewriter/ShardingSQLRewriterTest.java
+2
-2
sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/limit/Limit.java
...ava/org/apache/shardingsphere/core/route/limit/Limit.java
+17
-9
sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/ParsingSQLRouter.java
...ngsphere/core/route/router/sharding/ParsingSQLRouter.java
+1
-2
sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/statement/EncryptPreparedStatement.java
...ingjdbc/jdbc/core/statement/EncryptPreparedStatement.java
+1
-1
sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/statement/EncryptStatement.java
...re/shardingjdbc/jdbc/core/statement/EncryptStatement.java
+1
-1
sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/wrapper/PreparedStatementExecutorWrapper.java
...cation/jdbc/wrapper/PreparedStatementExecutorWrapper.java
+2
-2
sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/wrapper/StatementExecutorWrapper.java
.../communication/jdbc/wrapper/StatementExecutorWrapper.java
+2
-2
未找到文件。
sharding-core/sharding-core-entry/src/main/java/org/apache/shardingsphere/core/BaseShardingEngine.java
浏览文件 @
0f2737c7
...
...
@@ -25,6 +25,7 @@ import org.apache.shardingsphere.core.constant.properties.ShardingPropertiesCons
import
org.apache.shardingsphere.core.metadata.ShardingMetaData
;
import
org.apache.shardingsphere.core.rewrite.SQLRewriteEngine
;
import
org.apache.shardingsphere.core.rewrite.rewriter.EncryptSQLRewriter
;
import
org.apache.shardingsphere.core.rewrite.rewriter.ShardingParameterRewriter
;
import
org.apache.shardingsphere.core.rewrite.rewriter.ShardingSQLRewriter
;
import
org.apache.shardingsphere.core.route.RouteUnit
;
import
org.apache.shardingsphere.core.route.SQLLogger
;
...
...
@@ -103,9 +104,10 @@ public abstract class BaseShardingEngine {
private
Collection
<
RouteUnit
>
rewriteAndConvert
(
final
List
<
Object
>
parameters
,
final
SQLRouteResult
sqlRouteResult
)
{
SQLRewriteEngine
rewriteEngine
=
new
SQLRewriteEngine
(
shardingRule
,
sqlRouteResult
.
getSqlStatement
(),
parameters
,
sqlRouteResult
.
getRoutingResult
().
isSingleRouting
());
ShardingParameterRewriter
shardingParameterRewriter
=
new
ShardingParameterRewriter
(
databaseType
,
sqlRouteResult
);
ShardingSQLRewriter
shardingSQLRewriter
=
new
ShardingSQLRewriter
(
shardingRule
,
databaseType
,
sqlRouteResult
,
sqlRouteResult
.
getOptimizeResult
());
EncryptSQLRewriter
encryptSQLRewriter
=
new
EncryptSQLRewriter
(
shardingRule
.
getEncryptRule
().
getEncryptorEngine
(),
sqlRouteResult
.
getSqlStatement
(),
sqlRouteResult
.
getOptimizeResult
());
rewriteEngine
.
init
(
shardingSQLRewriter
,
encryptSQLRewriter
);
rewriteEngine
.
init
(
sharding
ParameterRewriter
,
sharding
SQLRewriter
,
encryptSQLRewriter
);
Collection
<
RouteUnit
>
result
=
new
LinkedHashSet
<>();
for
(
RoutingUnit
each
:
sqlRouteResult
.
getRoutingResult
().
getRoutingUnits
())
{
result
.
add
(
new
RouteUnit
(
each
.
getDataSourceName
(),
rewriteEngine
.
generateSQL
(
each
)));
...
...
sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/SQLRewriteEngine.java
浏览文件 @
0f2737c7
...
...
@@ -23,6 +23,7 @@ import org.apache.shardingsphere.core.parse.sql.token.SQLToken;
import
org.apache.shardingsphere.core.rewrite.builder.ParameterBuilder
;
import
org.apache.shardingsphere.core.rewrite.builder.SQLBuilder
;
import
org.apache.shardingsphere.core.rewrite.rewriter.BaseSQLRewriter
;
import
org.apache.shardingsphere.core.rewrite.rewriter.ParameterRewriter
;
import
org.apache.shardingsphere.core.rewrite.rewriter.SQLRewriter
;
import
org.apache.shardingsphere.core.rewrite.token.BaseTokenGenerateEngine
;
import
org.apache.shardingsphere.core.rewrite.token.EncryptTokenGenerateEngine
;
...
...
@@ -108,9 +109,13 @@ public final class SQLRewriteEngine {
/**
* Initialize SQL rewrite engine.
*
* @param parameterRewriter parameter rewriter
* @param sqlRewriters SQL rewriters
*/
public
void
init
(
final
SQLRewriter
...
sqlRewriters
)
{
public
void
init
(
final
ParameterRewriter
parameterRewriter
,
final
SQLRewriter
...
sqlRewriters
)
{
if
(
null
!=
parameterRewriter
)
{
parameterRewriter
.
rewrite
(
parameterBuilder
);
}
if
(
sqlTokens
.
isEmpty
())
{
baseSQLRewriter
.
appendWholeSQL
(
sqlBuilder
);
return
;
...
...
sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/builder/ParameterBuilder.java
浏览文件 @
0f2737c7
...
...
@@ -40,13 +40,16 @@ public final class ParameterBuilder {
private
final
List
<
Object
>
originalParameters
;
private
final
Map
<
Integer
,
Object
>
assistedIndexAndParametersForUpdate
;
private
final
Map
<
Integer
,
Object
>
addedIndexAndParameters
;
private
final
Map
<
Integer
,
Object
>
replacedIndexAndParameters
;
private
final
List
<
InsertParameterUnit
>
insertParameterUnits
;
public
ParameterBuilder
(
final
List
<
Object
>
parameters
)
{
originalParameters
=
new
LinkedList
<>(
parameters
);
assistedIndexAndParametersForUpdate
=
new
HashMap
<>();
addedIndexAndParameters
=
new
HashMap
<>();
replacedIndexAndParameters
=
new
HashMap
<>();
insertParameterUnits
=
new
LinkedList
<>();
}
...
...
@@ -122,9 +125,12 @@ public final class ParameterBuilder {
private
List
<
Object
>
getRevisedParameters
()
{
List
<
Object
>
result
=
new
LinkedList
<>(
originalParameters
);
for
(
Entry
<
Integer
,
Object
>
entry
:
a
ssistedIndexAndParametersForUpdate
.
entrySet
())
{
for
(
Entry
<
Integer
,
Object
>
entry
:
a
ddedIndexAndParameters
.
entrySet
())
{
result
.
add
(
entry
.
getKey
(),
entry
.
getValue
());
}
for
(
Entry
<
Integer
,
Object
>
entry
:
replacedIndexAndParameters
.
entrySet
())
{
result
.
set
(
entry
.
getKey
(),
entry
.
getValue
());
}
return
result
;
}
}
sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/rewriter/EncryptSQLRewriter.java
浏览文件 @
0f2737c7
...
...
@@ -30,10 +30,6 @@ import org.apache.shardingsphere.core.parse.sql.segment.dml.predicate.PredicateS
import
org.apache.shardingsphere.core.parse.sql.statement.SQLStatement
;
import
org.apache.shardingsphere.core.parse.sql.statement.dml.UpdateStatement
;
import
org.apache.shardingsphere.core.parse.sql.token.SQLToken
;
import
org.apache.shardingsphere.core.rewrite.token.pojo.InsertAssistedColumnsToken
;
import
org.apache.shardingsphere.core.rewrite.token.pojo.InsertSetAddAssistedColumnsToken
;
import
org.apache.shardingsphere.core.rewrite.token.pojo.InsertSetEncryptValueToken
;
import
org.apache.shardingsphere.core.rewrite.token.pojo.InsertValuesToken
;
import
org.apache.shardingsphere.core.rewrite.builder.ParameterBuilder
;
import
org.apache.shardingsphere.core.rewrite.builder.SQLBuilder
;
import
org.apache.shardingsphere.core.rewrite.placeholder.InsertAssistedColumnsPlaceholder
;
...
...
@@ -46,6 +42,10 @@ import org.apache.shardingsphere.core.rewrite.placeholder.UpdateEncryptAssistedI
import
org.apache.shardingsphere.core.rewrite.placeholder.UpdateEncryptItemPlaceholder
;
import
org.apache.shardingsphere.core.rewrite.placeholder.WhereEncryptColumnPlaceholder
;
import
org.apache.shardingsphere.core.rewrite.token.pojo.EncryptColumnToken
;
import
org.apache.shardingsphere.core.rewrite.token.pojo.InsertAssistedColumnsToken
;
import
org.apache.shardingsphere.core.rewrite.token.pojo.InsertSetAddAssistedColumnsToken
;
import
org.apache.shardingsphere.core.rewrite.token.pojo.InsertSetEncryptValueToken
;
import
org.apache.shardingsphere.core.rewrite.token.pojo.InsertValuesToken
;
import
org.apache.shardingsphere.core.rule.ColumnNode
;
import
org.apache.shardingsphere.core.strategy.encrypt.ShardingEncryptorEngine
;
import
org.apache.shardingsphere.spi.encrypt.ShardingEncryptor
;
...
...
@@ -140,7 +140,8 @@ public final class EncryptSQLRewriter implements SQLRewriter {
sqlBuilder
.
appendPlaceholder
(
new
InsertSetEncryptValuePlaceholder
(
insertOptimizeResult
.
getUnits
().
get
(
0
).
getColumnSQLExpression
(
insertSetEncryptValueToken
.
getColumnName
())));
}
private
void
appendInsertSetAddItemsPlaceholder
(
final
SQLBuilder
sqlBuilder
,
final
InsertSetAddAssistedColumnsToken
insertSetAddAssistedColumnsToken
,
final
InsertOptimizeResult
insertOptimizeResult
)
{
private
void
appendInsertSetAddItemsPlaceholder
(
final
SQLBuilder
sqlBuilder
,
final
InsertSetAddAssistedColumnsToken
insertSetAddAssistedColumnsToken
,
final
InsertOptimizeResult
insertOptimizeResult
)
{
List
<
ExpressionSegment
>
columnValues
=
new
LinkedList
<>();
for
(
String
each
:
insertSetAddAssistedColumnsToken
.
getColumnNames
())
{
columnValues
.
add
(
insertOptimizeResult
.
getUnits
().
get
(
0
).
getColumnSQLExpression
(
each
));
...
...
@@ -214,7 +215,7 @@ public final class EncryptSQLRewriter implements SQLRewriter {
return
getUpdateEncryptItemPlaceholder
(
encryptColumnToken
,
encryptColumnValues
);
}
List
<
Comparable
<?>>
encryptAssistedColumnValues
=
encryptorEngine
.
getEncryptAssistedColumnValues
(
columnNode
,
Collections
.<
Comparable
<?>>
singletonList
(
originalColumnValue
));
parameterBuilder
.
getA
ssistedIndexAndParametersForUpdate
().
putAll
(
getIndexAndParameters
(
encryptColumnToken
,
encryptAssistedColumnValues
));
parameterBuilder
.
getA
ddedIndexAndParameters
().
putAll
(
getIndexAndParameters
(
encryptColumnToken
,
encryptAssistedColumnValues
));
return
getUpdateEncryptAssistedItemPlaceholder
(
encryptColumnToken
,
encryptColumnValues
,
encryptAssistedColumnValues
);
}
...
...
sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/rewriter/ParameterRewriter.java
0 → 100644
浏览文件 @
0f2737c7
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
org.apache.shardingsphere.core.rewrite.rewriter
;
import
org.apache.shardingsphere.core.rewrite.builder.ParameterBuilder
;
/**
* Parameter rewriter.
*
* @author zhangliang
*/
public
interface
ParameterRewriter
{
/**
* Rewrite.
*
* @param parameterBuilder parameter builder
*/
void
rewrite
(
ParameterBuilder
parameterBuilder
);
}
sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/rewriter/ShardingParameterRewriter.java
0 → 100644
浏览文件 @
0f2737c7
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
org.apache.shardingsphere.core.rewrite.rewriter
;
import
lombok.RequiredArgsConstructor
;
import
org.apache.shardingsphere.core.constant.DatabaseType
;
import
org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement
;
import
org.apache.shardingsphere.core.rewrite.builder.ParameterBuilder
;
import
org.apache.shardingsphere.core.route.SQLRouteResult
;
import
java.util.Map
;
/**
* Parameter rewriter for sharding.
*
* @author zhangliang
*/
@RequiredArgsConstructor
public
final
class
ShardingParameterRewriter
implements
ParameterRewriter
{
private
final
DatabaseType
databaseType
;
private
final
SQLRouteResult
sqlRouteResult
;
@Override
public
void
rewrite
(
final
ParameterBuilder
parameterBuilder
)
{
if
(
sqlRouteResult
.
getSqlStatement
()
instanceof
SelectStatement
)
{
SelectStatement
selectStatement
=
(
SelectStatement
)
sqlRouteResult
.
getSqlStatement
();
if
(
null
==
selectStatement
.
getLimit
())
{
return
;
}
boolean
isNeedFetchAll
=
(!
selectStatement
.
getGroupByItems
().
isEmpty
()
||
!
selectStatement
.
getAggregationSelectItems
().
isEmpty
())
&&
!
selectStatement
.
isSameGroupByAndOrderByItems
();
Map
<
Integer
,
Object
>
revisedIndexAndParameters
=
sqlRouteResult
.
getLimit
().
getRevisedIndexAndParameters
(
isNeedFetchAll
,
databaseType
.
name
());
parameterBuilder
.
getReplacedIndexAndParameters
().
putAll
(
revisedIndexAndParameters
);
}
}
}
sharding-core/sharding-core-rewrite/src/main/java/org/apache/shardingsphere/core/rewrite/rewriter/ShardingSQLRewriter.java
浏览文件 @
0f2737c7
...
...
@@ -26,7 +26,6 @@ import org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.OrderByIt
import
org.apache.shardingsphere.core.parse.sql.segment.dml.order.item.TextOrderByItemSegment
;
import
org.apache.shardingsphere.core.parse.sql.statement.dml.SelectStatement
;
import
org.apache.shardingsphere.core.parse.sql.token.SQLToken
;
import
org.apache.shardingsphere.core.rewrite.token.pojo.InsertSetAddGeneratedKeyToken
;
import
org.apache.shardingsphere.core.rewrite.builder.ParameterBuilder
;
import
org.apache.shardingsphere.core.rewrite.builder.SQLBuilder
;
import
org.apache.shardingsphere.core.rewrite.placeholder.AggregationDistinctPlaceholder
;
...
...
@@ -42,6 +41,7 @@ import org.apache.shardingsphere.core.rewrite.placeholder.TablePlaceholder;
import
org.apache.shardingsphere.core.rewrite.token.pojo.AggregationDistinctToken
;
import
org.apache.shardingsphere.core.rewrite.token.pojo.IndexToken
;
import
org.apache.shardingsphere.core.rewrite.token.pojo.InsertGeneratedKeyToken
;
import
org.apache.shardingsphere.core.rewrite.token.pojo.InsertSetAddGeneratedKeyToken
;
import
org.apache.shardingsphere.core.rewrite.token.pojo.OffsetToken
;
import
org.apache.shardingsphere.core.rewrite.token.pojo.OrderByToken
;
import
org.apache.shardingsphere.core.rewrite.token.pojo.RowCountToken
;
...
...
@@ -186,7 +186,8 @@ public final class ShardingSQLRewriter implements SQLRewriter {
sqlBuilder
.
appendPlaceholder
(
new
InsertGeneratedKeyPlaceholder
(
insertGeneratedKeyToken
.
getColumn
(),
insertGeneratedKeyToken
.
isToAddCloseParenthesis
()));
}
private
void
appendInsertSetAddGeneratedKeyPlaceholder
(
final
SQLBuilder
sqlBuilder
,
final
InsertSetAddGeneratedKeyToken
insertSetAddGeneratedKeyToken
,
final
InsertOptimizeResult
insertOptimizeResult
)
{
private
void
appendInsertSetAddGeneratedKeyPlaceholder
(
final
SQLBuilder
sqlBuilder
,
final
InsertSetAddGeneratedKeyToken
insertSetAddGeneratedKeyToken
,
final
InsertOptimizeResult
insertOptimizeResult
)
{
String
columnName
=
insertSetAddGeneratedKeyToken
.
getColumnName
();
sqlBuilder
.
appendPlaceholder
(
new
InsertSetAddGeneratedKeyPlaceholder
(
columnName
,
insertOptimizeResult
.
getUnits
().
get
(
0
).
getColumnSQLExpression
(
columnName
)));
}
...
...
sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/rewriter/EncryptSQLRewriterTest.java
浏览文件 @
0f2737c7
...
...
@@ -185,7 +185,7 @@ public final class EncryptSQLRewriterTest {
SQLStatement
sqlStatement
=
encryptSQLParseEngine
.
parse
(
sql
,
false
);
SQLRewriteEngine
sqlRewriteEngine
=
new
SQLRewriteEngine
(
encryptRule
,
sqlStatement
,
parameters
);
OptimizeResult
optimizeResult
=
OptimizeEngineFactory
.
newInstance
(
encryptRule
,
sqlStatement
,
parameters
).
optimize
();
sqlRewriteEngine
.
init
(
new
EncryptSQLRewriter
(
encryptRule
.
getEncryptorEngine
(),
sqlStatement
,
optimizeResult
));
sqlRewriteEngine
.
init
(
n
ull
,
n
ew
EncryptSQLRewriter
(
encryptRule
.
getEncryptorEngine
(),
sqlStatement
,
optimizeResult
));
return
sqlRewriteEngine
.
generateSQL
();
}
}
sharding-core/sharding-core-rewrite/src/test/java/org/apache/shardingsphere/core/rewrite/rewriter/ShardingSQLRewriterTest.java
浏览文件 @
0f2737c7
...
...
@@ -539,7 +539,7 @@ public final class ShardingSQLRewriterTest {
routeResult
.
setRoutingResult
(
new
RoutingResult
());
selectStatement
.
setLogicSQL
(
"SELECT table_x.id, x.name FROM table_x x, table_y y WHERE table_x.id=? AND x.name=?"
);
SQLRewriteEngine
rewriteEngine
=
new
SQLRewriteEngine
(
shardingRule
,
routeResult
.
getSqlStatement
(),
parameters
,
routeResult
.
getRoutingResult
().
isSingleRouting
());
rewriteEngine
.
init
(
rewriteEngine
.
init
(
new
ShardingParameterRewriter
(
DatabaseType
.
MySQL
,
routeResult
),
new
ShardingSQLRewriter
(
shardingRule
,
DatabaseType
.
MySQL
,
routeResult
,
null
),
new
EncryptSQLRewriter
(
shardingRule
.
getEncryptRule
().
getEncryptorEngine
(),
routeResult
.
getSqlStatement
(),
routeResult
.
getOptimizeResult
()));
RoutingUnit
routingUnit
=
new
RoutingUnit
(
"db0"
);
...
...
@@ -902,7 +902,7 @@ public final class ShardingSQLRewriterTest {
private
SQLRewriteEngine
createSQLRewriteEngine
(
final
DatabaseType
databaseType
,
final
List
<
Object
>
parameters
)
{
SQLRewriteEngine
result
=
new
SQLRewriteEngine
(
shardingRule
,
routeResult
.
getSqlStatement
(),
parameters
,
routeResult
.
getRoutingResult
().
isSingleRouting
());
result
.
init
(
result
.
init
(
new
ShardingParameterRewriter
(
DatabaseType
.
MySQL
,
routeResult
),
new
ShardingSQLRewriter
(
shardingRule
,
databaseType
,
routeResult
,
routeResult
.
getOptimizeResult
()),
new
EncryptSQLRewriter
(
shardingRule
.
getEncryptRule
().
getEncryptorEngine
(),
routeResult
.
getSqlStatement
(),
routeResult
.
getOptimizeResult
()));
return
result
;
...
...
sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/limit/Limit.java
浏览文件 @
0f2737c7
...
...
@@ -23,7 +23,9 @@ import lombok.ToString;
import
org.apache.shardingsphere.core.parse.exception.SQLParsingException
;
import
org.apache.shardingsphere.core.util.NumberUtil
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
/**
* Limit object.
...
...
@@ -60,15 +62,12 @@ public final class Limit {
}
/**
* Fill parameters
for rewrite limit
.
* Fill parameters.
*
* @param parameters parameters
* @param isFetchAll is fetch all data or not
* @param databaseType database type
* @param parameters SQL parameters
*/
public
void
processParameters
(
final
List
<
Object
>
parameters
,
final
boolean
isFetchAll
,
final
String
databaseType
)
{
public
void
fillParameters
(
final
List
<
Object
>
parameters
)
{
fill
(
parameters
);
rewrite
(
parameters
,
isFetchAll
,
databaseType
);
}
private
void
fill
(
final
List
<
Object
>
parameters
)
{
...
...
@@ -87,7 +86,15 @@ public final class Limit {
}
}
private
void
rewrite
(
final
List
<
Object
>
parameters
,
final
boolean
isFetchAll
,
final
String
databaseType
)
{
/**
* Revise parameters.
*
* @param isFetchAll is fetch all data or not
* @param databaseType database type
* @return revised indexes and parameters
*/
public
Map
<
Integer
,
Object
>
getRevisedIndexAndParameters
(
final
boolean
isFetchAll
,
final
String
databaseType
)
{
Map
<
Integer
,
Object
>
result
=
new
HashMap
<>(
2
,
1
);
int
rewriteOffset
=
0
;
int
rewriteRowCount
;
if
(
isFetchAll
)
{
...
...
@@ -98,11 +105,12 @@ public final class Limit {
rewriteRowCount
=
rowCount
.
getValue
();
}
if
(
null
!=
offset
&&
offset
.
getIndex
()
>
-
1
)
{
parameters
.
se
t
(
offset
.
getIndex
(),
rewriteOffset
);
result
.
pu
t
(
offset
.
getIndex
(),
rewriteOffset
);
}
if
(
null
!=
rowCount
&&
rowCount
.
getIndex
()
>
-
1
)
{
parameters
.
se
t
(
rowCount
.
getIndex
(),
rewriteRowCount
);
result
.
pu
t
(
rowCount
.
getIndex
(),
rewriteRowCount
);
}
return
result
;
}
/**
...
...
sharding-core/sharding-core-route/src/main/java/org/apache/shardingsphere/core/route/router/sharding/ParsingSQLRouter.java
浏览文件 @
0f2737c7
...
...
@@ -190,9 +190,8 @@ public final class ParsingSQLRouter implements ShardingRouter {
}
private
Limit
createLimit
(
final
SelectStatement
selectStatement
,
final
List
<
Object
>
parameters
)
{
boolean
isNeedFetchAll
=
(!
selectStatement
.
getGroupByItems
().
isEmpty
()
||
!
selectStatement
.
getAggregationSelectItems
().
isEmpty
())
&&
!
selectStatement
.
isSameGroupByAndOrderByItems
();
Limit
result
=
createLimit
(
selectStatement
.
getLimit
());
result
.
processParameters
(
parameters
,
isNeedFetchAll
,
databaseType
.
name
()
);
result
.
fillParameters
(
parameters
);
return
result
;
}
...
...
sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/statement/EncryptPreparedStatement.java
浏览文件 @
0f2737c7
...
...
@@ -149,7 +149,7 @@ public final class EncryptPreparedStatement extends AbstractShardingPreparedStat
SQLStatement
sqlStatement
=
connection
.
getParseEngine
().
parse
(
sql
,
true
);
SQLRewriteEngine
encryptSQLRewriteEngine
=
new
SQLRewriteEngine
(
connection
.
getEncryptRule
(),
sqlStatement
,
getParameters
());
OptimizeResult
optimizeResult
=
OptimizeEngineFactory
.
newInstance
(
connection
.
getEncryptRule
(),
sqlStatement
,
getParameters
()).
optimize
();
encryptSQLRewriteEngine
.
init
(
new
EncryptSQLRewriter
(
connection
.
getEncryptRule
().
getEncryptorEngine
(),
sqlStatement
,
optimizeResult
));
encryptSQLRewriteEngine
.
init
(
n
ull
,
n
ew
EncryptSQLRewriter
(
connection
.
getEncryptRule
().
getEncryptorEngine
(),
sqlStatement
,
optimizeResult
));
return
encryptSQLRewriteEngine
.
generateSQL
();
}
...
...
sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/statement/EncryptStatement.java
浏览文件 @
0f2737c7
...
...
@@ -81,7 +81,7 @@ public final class EncryptStatement extends AbstractUnsupportedOperationStatemen
SQLStatement
sqlStatement
=
connection
.
getParseEngine
().
parse
(
sql
,
false
);
SQLRewriteEngine
encryptSQLRewriteEngine
=
new
SQLRewriteEngine
(
connection
.
getEncryptRule
(),
sqlStatement
,
Collections
.
emptyList
());
OptimizeResult
optimizeResult
=
OptimizeEngineFactory
.
newInstance
(
connection
.
getEncryptRule
(),
sqlStatement
,
Collections
.
emptyList
()).
optimize
();
encryptSQLRewriteEngine
.
init
(
new
EncryptSQLRewriter
(
connection
.
getEncryptRule
().
getEncryptorEngine
(),
sqlStatement
,
optimizeResult
));
encryptSQLRewriteEngine
.
init
(
n
ull
,
n
ew
EncryptSQLRewriter
(
connection
.
getEncryptRule
().
getEncryptorEngine
(),
sqlStatement
,
optimizeResult
));
return
encryptSQLRewriteEngine
.
generateSQL
().
getSql
();
}
...
...
sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/wrapper/PreparedStatementExecutorWrapper.java
浏览文件 @
0f2737c7
...
...
@@ -77,7 +77,7 @@ public final class PreparedStatementExecutorWrapper implements JDBCExecutorWrapp
private
SQLRouteResult
doMasterSlaveRoute
(
final
String
sql
)
{
SQLStatement
sqlStatement
=
((
MasterSlaveSchema
)
logicSchema
).
getParseEngine
().
parse
(
sql
,
true
);
SQLRewriteEngine
sqlRewriteEngine
=
new
SQLRewriteEngine
(
sqlStatement
);
sqlRewriteEngine
.
init
();
sqlRewriteEngine
.
init
(
null
);
String
rewriteSQL
=
sqlRewriteEngine
.
generateSQL
().
getSql
();
SQLRouteResult
result
=
new
SQLRouteResult
(
sqlStatement
);
for
(
String
each
:
new
MasterSlaveRouter
(((
MasterSlaveSchema
)
logicSchema
).
getMasterSlaveRule
(),
((
MasterSlaveSchema
)
logicSchema
).
getParseEngine
(),
...
...
@@ -92,7 +92,7 @@ public final class PreparedStatementExecutorWrapper implements JDBCExecutorWrapp
SQLStatement
sqlStatement
=
encryptSchema
.
getEncryptSQLParseEntry
().
parse
(
sql
,
true
);
SQLRewriteEngine
sqlRewriteEngine
=
new
SQLRewriteEngine
(
encryptSchema
.
getEncryptRule
(),
sqlStatement
,
parameters
);
OptimizeResult
optimizeResult
=
OptimizeEngineFactory
.
newInstance
(
encryptSchema
.
getEncryptRule
(),
sqlStatement
,
parameters
).
optimize
();
sqlRewriteEngine
.
init
(
new
EncryptSQLRewriter
(
encryptSchema
.
getEncryptRule
().
getEncryptorEngine
(),
sqlStatement
,
optimizeResult
));
sqlRewriteEngine
.
init
(
n
ull
,
n
ew
EncryptSQLRewriter
(
encryptSchema
.
getEncryptRule
().
getEncryptorEngine
(),
sqlStatement
,
optimizeResult
));
SQLRouteResult
result
=
new
SQLRouteResult
(
sqlStatement
);
result
.
getRouteUnits
().
add
(
new
RouteUnit
(
logicSchema
.
getDataSources
().
keySet
().
iterator
().
next
(),
new
SQLUnit
(
sqlRewriteEngine
.
generateSQL
().
getSql
(),
parameters
)));
return
result
;
...
...
sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/communication/jdbc/wrapper/StatementExecutorWrapper.java
浏览文件 @
0f2737c7
...
...
@@ -78,7 +78,7 @@ public final class StatementExecutorWrapper implements JDBCExecutorWrapper {
private
SQLRouteResult
doMasterSlaveRoute
(
final
String
sql
)
{
SQLStatement
sqlStatement
=
((
MasterSlaveSchema
)
logicSchema
).
getParseEngine
().
parse
(
sql
,
false
);
SQLRewriteEngine
sqlRewriteEngine
=
new
SQLRewriteEngine
(
sqlStatement
);
sqlRewriteEngine
.
init
();
sqlRewriteEngine
.
init
(
null
);
String
rewriteSQL
=
sqlRewriteEngine
.
generateSQL
().
getSql
();
SQLRouteResult
result
=
new
SQLRouteResult
(
sqlStatement
);
for
(
String
each
:
new
MasterSlaveRouter
(((
MasterSlaveSchema
)
logicSchema
).
getMasterSlaveRule
(),
((
MasterSlaveSchema
)
logicSchema
).
getParseEngine
(),
...
...
@@ -93,7 +93,7 @@ public final class StatementExecutorWrapper implements JDBCExecutorWrapper {
SQLStatement
sqlStatement
=
encryptSchema
.
getEncryptSQLParseEntry
().
parse
(
sql
,
false
);
SQLRewriteEngine
sqlRewriteEngine
=
new
SQLRewriteEngine
(
encryptSchema
.
getEncryptRule
(),
sqlStatement
,
Collections
.
emptyList
());
OptimizeResult
optimizeResult
=
OptimizeEngineFactory
.
newInstance
(
encryptSchema
.
getEncryptRule
(),
sqlStatement
,
new
LinkedList
<>()).
optimize
();
sqlRewriteEngine
.
init
(
new
EncryptSQLRewriter
(
encryptSchema
.
getEncryptRule
().
getEncryptorEngine
(),
sqlStatement
,
optimizeResult
));
sqlRewriteEngine
.
init
(
n
ull
,
n
ew
EncryptSQLRewriter
(
encryptSchema
.
getEncryptRule
().
getEncryptorEngine
(),
sqlStatement
,
optimizeResult
));
SQLRouteResult
result
=
new
SQLRouteResult
(
sqlStatement
);
result
.
getRouteUnits
().
add
(
new
RouteUnit
(
logicSchema
.
getDataSources
().
keySet
().
iterator
().
next
(),
new
SQLUnit
(
sqlRewriteEngine
.
generateSQL
().
getSql
(),
Collections
.
emptyList
())));
return
result
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录