RouteResultset.java
12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/*
* Copyright (c) 2013, OpenCloudDB/MyCAT and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software;Designed and Developed mainly by many Chinese
* opensource volunteers. you can redistribute it and/or modify it under the
* terms of the GNU General Public License version 2 only, as published by the
* Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Any questions about this component can be directed to it's project Web address
* https://code.google.com/p/opencloudb/.
*
*/
package io.mycat.route;
import com.alibaba.druid.sql.ast.SQLStatement;
import io.mycat.MycatServer;
import io.mycat.config.MycatConfig;
import io.mycat.config.model.SchemaConfig;
import io.mycat.route.parser.util.PageSQLUtil;
import io.mycat.sqlengine.mpp.HavingCols;
import io.mycat.util.FormatUtil;
import java.io.Serializable;
import java.util.*;
/**
* @author mycat
*/
public final class RouteResultset implements Serializable {
private String statement; // 原始语句
private final int sqlType;
private RouteResultsetNode[] nodes; // 路由结果节点
private Set<String> subTables;
private SQLStatement sqlStatement;
private int limitStart;
private boolean cacheAble;
// used to store table's ID->datanodes cache
// format is table.primaryKey
private String primaryKey;
// limit output total
private int limitSize;
private SQLMerge sqlMerge;
private boolean callStatement = false; // 处理call关键字
// 是否为全局表,只有在insert、update、delete、ddl里会判断并修改。默认不是全局表,用于修正全局表修改数据的反馈。
private boolean globalTableFlag = false;
//是否完成了路由
private boolean isFinishedRoute = false;
//是否自动提交,此属性主要用于记录ServerConnection上的autocommit状态
private boolean autocommit = true;
private boolean isLoadData=false;
//是否可以在从库运行,此属性主要供RouteResultsetNode获取
private Boolean canRunInReadDB;
// 强制走 master,可以通过 RouteResultset的属性canRunInReadDB=false
// 传给 RouteResultsetNode 来实现,但是 强制走 slave需要增加一个属性来实现:
private Boolean runOnSlave = null; // 默认null表示不施加影响
//key=dataNode value=slot
private Map<String,Integer> dataNodeSlotMap=new HashMap<>();
private boolean selectForUpdate;
public boolean isSelectForUpdate() {
return selectForUpdate;
}
public void setSelectForUpdate(boolean selectForUpdate) {
this.selectForUpdate = selectForUpdate;
}
private List<String> tables;
public List<String> getTables() {
return tables;
}
public void setTables(List<String> tables) {
this.tables = tables;
}
public Map<String, Integer> getDataNodeSlotMap() {
return dataNodeSlotMap;
}
public void setDataNodeSlotMap(Map<String, Integer> dataNodeSlotMap) {
this.dataNodeSlotMap = dataNodeSlotMap;
}
public Boolean getRunOnSlave() {
return runOnSlave;
}
public void setRunOnSlave(Boolean runOnSlave) {
this.runOnSlave = runOnSlave;
}
private Procedure procedure;
public Procedure getProcedure()
{
return procedure;
}
public void setProcedure(Procedure procedure)
{
this.procedure = procedure;
}
public boolean isLoadData()
{
return isLoadData;
}
public void setLoadData(boolean isLoadData)
{
this.isLoadData = isLoadData;
}
public boolean isFinishedRoute() {
return isFinishedRoute;
}
public void setFinishedRoute(boolean isFinishedRoute) {
this.isFinishedRoute = isFinishedRoute;
}
public boolean isGlobalTable() {
return globalTableFlag;
}
public void setGlobalTable(boolean globalTableFlag) {
this.globalTableFlag = globalTableFlag;
}
public RouteResultset(String stmt, int sqlType) {
this.statement = stmt;
this.limitSize = -1;
this.sqlType = sqlType;
}
public void resetNodes() {
if (nodes != null) {
for (RouteResultsetNode node : nodes) {
node.resetStatement();
}
}
}
public void copyLimitToNodes() {
if(nodes!=null)
{
for (RouteResultsetNode node : nodes)
{
if(node.getLimitSize()==-1&&node.getLimitStart()==0)
{
node.setLimitStart(limitStart);
node.setLimitSize(limitSize);
}
}
}
}
public SQLMerge getSqlMerge() {
return sqlMerge;
}
public boolean isCacheAble() {
return cacheAble;
}
public void setCacheAble(boolean cacheAble) {
this.cacheAble = cacheAble;
}
public boolean needMerge() {
return limitSize > 0 || sqlMerge != null;
}
public int getSqlType() {
return sqlType;
}
public boolean isHasAggrColumn() {
return (sqlMerge != null) && sqlMerge.isHasAggrColumn();
}
public int getLimitStart() {
return limitStart;
}
public String[] getGroupByCols() {
return (sqlMerge != null) ? sqlMerge.getGroupByCols() : null;
}
private SQLMerge createSQLMergeIfNull() {
if (sqlMerge == null) {
sqlMerge = new SQLMerge();
}
return sqlMerge;
}
public Map<String, Integer> getMergeCols() {
return (sqlMerge != null) ? sqlMerge.getMergeCols() : null;
}
public void setLimitStart(int limitStart) {
this.limitStart = limitStart;
}
public String getPrimaryKey() {
return primaryKey;
}
public boolean hasPrimaryKeyToCache() {
return primaryKey != null;
}
public void setPrimaryKey(String primaryKey) {
if (!primaryKey.contains(".")) {
throw new java.lang.IllegalArgumentException(
"must be table.primarykey fomat :" + primaryKey);
}
this.primaryKey = primaryKey;
}
/**
* return primary key items ,first is table name ,seconds is primary key
*
* @return
*/
public String[] getPrimaryKeyItems() {
return primaryKey.split("\\.");
}
public void setOrderByCols(LinkedHashMap<String, Integer> orderByCols) {
if (orderByCols != null && !orderByCols.isEmpty()) {
createSQLMergeIfNull().setOrderByCols(orderByCols);
}
}
public void setHasAggrColumn(boolean hasAggrColumn) {
if (hasAggrColumn) {
createSQLMergeIfNull().setHasAggrColumn(true);
}
}
public void setGroupByCols(String[] groupByCols) {
if (groupByCols != null && groupByCols.length > 0) {
createSQLMergeIfNull().setGroupByCols(groupByCols);
}
}
public void setMergeCols(Map<String, Integer> mergeCols) {
if (mergeCols != null && !mergeCols.isEmpty()) {
createSQLMergeIfNull().setMergeCols(mergeCols);
}
}
public LinkedHashMap<String, Integer> getOrderByCols() {
return (sqlMerge != null) ? sqlMerge.getOrderByCols() : null;
}
public String getStatement() {
return statement;
}
public RouteResultsetNode[] getNodes() {
return nodes;
}
public void setNodes(RouteResultsetNode[] nodes) {
if(nodes!=null)
{
int nodeSize=nodes.length;
for (RouteResultsetNode node : nodes)
{
node.setTotalNodeSize(nodeSize);
}
}
this.nodes = nodes;
}
/**
* @return -1 if no limit
*/
public int getLimitSize() {
return limitSize;
}
public void setLimitSize(int limitSize) {
this.limitSize = limitSize;
}
public void setStatement(String statement) {
this.statement = statement;
}
public boolean isCallStatement() {
return callStatement;
}
public void setCallStatement(boolean callStatement) {
this.callStatement = callStatement;
if(nodes!=null)
{
for (RouteResultsetNode node : nodes)
{
node.setCallStatement(callStatement);
}
}
}
public void changeNodeSqlAfterAddLimit(SchemaConfig schemaConfig, String sourceDbType, String sql, int offset, int count, boolean isNeedConvert) {
if (nodes != null)
{
Map<String, String> dataNodeDbTypeMap = schemaConfig.getDataNodeDbTypeMap();
Map<String, String> sqlMapCache = new HashMap<>();
for (RouteResultsetNode node : nodes)
{
String dbType = dataNodeDbTypeMap.get(node.getName());
if (dbType.equalsIgnoreCase("mysql"))
{
node.setStatement(sql); //mysql之前已经加好limit
} else if (sqlMapCache.containsKey(dbType))
{
node.setStatement(sqlMapCache.get(dbType));
} else if(isNeedConvert)
{
String nativeSql = PageSQLUtil.convertLimitToNativePageSql(dbType, sql, offset, count);
sqlMapCache.put(dbType, nativeSql);
node.setStatement(nativeSql);
} else {
node.setStatement(sql);
}
node.setLimitStart(offset);
node.setLimitSize(count);
}
}
}
public boolean isAutocommit() {
return autocommit;
}
public void setAutocommit(boolean autocommit) {
this.autocommit = autocommit;
}
public Boolean getCanRunInReadDB() {
return canRunInReadDB;
}
public void setCanRunInReadDB(Boolean canRunInReadDB) {
this.canRunInReadDB = canRunInReadDB;
}
public HavingCols getHavingCols() {
return (sqlMerge != null) ? sqlMerge.getHavingCols() : null;
}
public void setSubTables(Set<String> subTables) {
this.subTables = subTables;
}
public void setHavings(HavingCols havings) {
if (havings != null) {
createSQLMergeIfNull().setHavingCols(havings);
}
}
// Added by winbill, 20160314, for having clause, Begin ==>
public void setHavingColsName(Object[] names) {
if (names != null && names.length > 0) {
createSQLMergeIfNull().setHavingColsName(names);
}
}
// Added by winbill, 20160314, for having clause, End <==
public SQLStatement getSqlStatement() {
return this.sqlStatement;
}
public void setSqlStatement(SQLStatement sqlStatement) {
this.sqlStatement = sqlStatement;
}
public Set<String> getSubTables() {
return this.subTables;
}
public boolean isDistTable(){
if(this.getSubTables()!=null && !this.getSubTables().isEmpty() ){
return true;
}
return false;
}
@Override
public String toString() {
StringBuilder s = new StringBuilder();
s.append(statement).append(", route={");
if (nodes != null) {
for (int i = 0; i < nodes.length; ++i) {
s.append("\n ").append(FormatUtil.format(i + 1, 3));
s.append(" -> ").append(nodes[i]);
}
}
s.append("\n}");
return s.toString();
}
}