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
| package com.easy.query.solon.integration.parser;
import com.easy.query.solon.integration.excutor.TenantLineHandler; import java.util.*; import java.util.stream.Collectors; import net.sf.jsqlparser.expression.*; import net.sf.jsqlparser.expression.operators.conditional.AndExpression; import net.sf.jsqlparser.expression.operators.conditional.OrExpression; import net.sf.jsqlparser.expression.operators.relational.*; import net.sf.jsqlparser.schema.Column; import net.sf.jsqlparser.schema.Table; import net.sf.jsqlparser.statement.select.*; import org.dromara.hutool.core.collection.CollUtil;
public class TenantLineSqlParser implements SqlParser { private final TenantLineHandler tenantLineHandler;
public TenantLineSqlParser(TenantLineHandler tenantLineHandler) { this.tenantLineHandler = tenantLineHandler; }
@Override public void processSelect(Select select, int index, String sql, Object obj) { String whereSegment = (String) obj; this.processSelectBody(select.getPlainSelect(), whereSegment); List<WithItem<?>> withItemsList = select.getWithItemsList(); if (CollUtil.isNotEmpty(withItemsList)) { withItemsList.forEach( (withItem) -> this.processSelectBody(withItem.getSelect(), whereSegment)); } }
protected void processSelectBody(Select selectBody, final String whereSegment) { if (selectBody == null) { return; }
if (selectBody instanceof PlainSelect) { processPlainSelect((PlainSelect) selectBody, whereSegment); } else if (selectBody instanceof ParenthesedSelect parenthesedSelect) { processSelectBody(parenthesedSelect.getSelect(), whereSegment); } else if (selectBody instanceof SetOperationList operationList) { List<Select> selectBodyList = operationList.getSelects(); if (CollUtil.isNotEmpty(selectBodyList)) { selectBodyList.forEach(body -> processSelectBody(body, whereSegment)); } } }
protected void processPlainSelect(final PlainSelect plainSelect, final String whereSegment) { List<SelectItem<?>> selectItems = plainSelect.getSelectItems(); if (CollUtil.isNotEmpty(selectItems)) { selectItems.forEach((selectItem) -> this.processSelectItem(selectItem, whereSegment)); }
Expression where = plainSelect.getWhere(); this.processWhereSubSelect(where, whereSegment); FromItem fromItem = plainSelect.getFromItem(); List<Table> list = this.processFromItem(fromItem, whereSegment); List<Table> mainTables = new ArrayList<>(list); List<Join> joins = plainSelect.getJoins(); if (CollUtil.isNotEmpty(joins)) { mainTables = this.processJoins(mainTables, joins, whereSegment); }
if (CollUtil.isNotEmpty(mainTables)) { plainSelect.setWhere(this.builderExpression(where, mainTables, whereSegment)); } }
@SuppressWarnings({"rawtypes", "unchecked"}) protected void processWhereSubSelect(Expression where, final String whereSegment) { if (where == null) { return; } if (where instanceof FromItem) { processOtherFromItem((FromItem) where, whereSegment); return; } if (where.toString().indexOf("SELECT") > 0) { if (where instanceof BinaryExpression expression) { processWhereSubSelect(expression.getLeftExpression(), whereSegment); processWhereSubSelect(expression.getRightExpression(), whereSegment); } else if (where instanceof InExpression expression) { Expression inExpression = expression.getRightExpression(); if (inExpression instanceof Select) { processSelectBody(((Select) inExpression), whereSegment); } } else if (where instanceof ExistsExpression expression) { processWhereSubSelect(expression.getRightExpression(), whereSegment); } else if (where instanceof NotExpression expression) { processWhereSubSelect(expression.getExpression(), whereSegment); } else if (where instanceof ParenthesedExpressionList) { ParenthesedExpressionList<Expression> expression = (ParenthesedExpressionList) where; processWhereSubSelect(expression.get(0), whereSegment); } } }
@SuppressWarnings("rawtypes") protected void processSelectItem(SelectItem selectItem, final String whereSegment) { Expression expression = selectItem.getExpression(); if (expression instanceof Select) { processSelectBody(((Select) expression), whereSegment); } else if (expression instanceof Function) { processFunction((Function) expression, whereSegment); } else if (expression instanceof ExistsExpression existsExpression) { processSelectBody((Select) existsExpression.getRightExpression(), whereSegment); } }
protected void processFunction(Function function, final String whereSegment) { ExpressionList<?> parameters = function.getParameters(); if (parameters != null) { parameters.forEach( expression -> { if (expression instanceof Select) { processSelectBody(((Select) expression), whereSegment); } else if (expression instanceof Function) { processFunction((Function) expression, whereSegment); } else if (expression instanceof EqualsTo) { if (((EqualsTo) expression).getLeftExpression() instanceof Select) { processSelectBody( ((Select) ((EqualsTo) expression).getLeftExpression()), whereSegment); } if (((EqualsTo) expression).getRightExpression() instanceof Select) { processSelectBody( ((Select) ((EqualsTo) expression).getRightExpression()), whereSegment); } } }); } }
protected void processOtherFromItem(FromItem fromItem, final String whereSegment) { while (fromItem instanceof ParenthesedFromItem) { fromItem = ((ParenthesedFromItem) fromItem).getFromItem(); }
if (fromItem instanceof ParenthesedSelect) { Select subSelect = (Select) fromItem; processSelectBody(subSelect, whereSegment); } }
protected Expression builderExpression( Expression currentExpression, List<Table> tables, final String whereSegment) { if (CollUtil.isEmpty(tables)) { return currentExpression; } List<Expression> expressions = tables.stream() .map(item -> buildTableExpression(item, currentExpression, whereSegment)) .filter(Objects::nonNull) .collect(Collectors.toList());
if (CollUtil.isEmpty(expressions)) { return currentExpression; }
Expression injectExpression = expressions.get(0); if (expressions.size() > 1) { for (int i = 1; i < expressions.size(); i++) { injectExpression = new AndExpression(injectExpression, expressions.get(i)); } }
if (currentExpression == null) { return injectExpression; } if (currentExpression instanceof OrExpression) { return new AndExpression( new ParenthesedExpressionList<>(currentExpression), injectExpression); } else { return new AndExpression(currentExpression, injectExpression); } }
protected Column getAliasColumn(Table table) { StringBuilder column = new StringBuilder(); if (table.getAlias() != null) { column.append(table.getAlias().getName()).append("."); } column.append(tenantLineHandler.getTenantIdColumn()); return new Column(column.toString()); }
protected Expression buildTableExpression( final Table table, final Expression where, final String whereSegment) { if (tenantLineHandler.ignoreTable(table.getName())) { return null; }
return new EqualsTo(getAliasColumn(table), tenantLineHandler.getTenantId()); }
protected List<Table> processFromItem(FromItem fromItem, final String whereSegment) {
List<Table> mainTables = new ArrayList<>(); if (fromItem instanceof Table) { Table fromTable = (Table) fromItem; mainTables.add(fromTable); } else if (fromItem instanceof ParenthesedFromItem) { List<Table> tables = processSubJoin((ParenthesedFromItem) fromItem, whereSegment); mainTables.addAll(tables); } else { processOtherFromItem(fromItem, whereSegment); } return mainTables; }
protected List<Table> processSubJoin(ParenthesedFromItem subJoin, final String whereSegment) { while (subJoin.getJoins() == null && subJoin.getFromItem() instanceof ParenthesedFromItem) { subJoin = (ParenthesedFromItem) subJoin.getFromItem(); } List<Table> tableList = processFromItem(subJoin.getFromItem(), whereSegment); List<Table> mainTables = new ArrayList<>(tableList); if (subJoin.getJoins() != null) { processJoins(mainTables, subJoin.getJoins(), whereSegment); } return mainTables; }
protected List<Table> processJoins( List<Table> mainTables, List<Join> joins, final String whereSegment) { Table mainTable = null; Table leftTable = null;
if (mainTables.size() == 1) { mainTable = mainTables.get(0); leftTable = mainTable; }
Deque<List<Table>> onTableDeque = new LinkedList<>(); for (Join join : joins) { FromItem joinItem = join.getRightItem();
List<Table> joinTables = null; if (joinItem instanceof Table) { joinTables = new ArrayList<>(); joinTables.add((Table) joinItem); } else if (joinItem instanceof ParenthesedFromItem) { joinTables = processSubJoin((ParenthesedFromItem) joinItem, whereSegment); }
if (joinTables != null && !joinTables.isEmpty()) {
if (join.isSimple()) { mainTables.addAll(joinTables); continue; }
Table joinTable = joinTables.get(0);
List<Table> onTables = null; if (join.isRight()) { mainTable = joinTable; mainTables.clear(); if (leftTable != null) { onTables = Collections.singletonList(leftTable); } } else if (join.isInner()) { if (mainTable == null) { onTables = Collections.singletonList(joinTable); } else { onTables = Arrays.asList(mainTable, joinTable); } mainTable = null; mainTables.clear(); } else { onTables = Collections.singletonList(joinTable); }
if (mainTable != null && !mainTables.contains(mainTable)) { mainTables.add(mainTable); }
Collection<Expression> originOnExpressions = join.getOnExpressions(); if (originOnExpressions.size() == 1 && onTables != null) { List<Expression> onExpressions = new LinkedList<>(); onExpressions.add( builderExpression(originOnExpressions.iterator().next(), onTables, whereSegment)); join.setOnExpressions(onExpressions); leftTable = mainTable == null ? joinTable : mainTable; continue; } onTableDeque.push(onTables); if (originOnExpressions.size() > 1) { Collection<Expression> onExpressions = new LinkedList<>(); for (Expression originOnExpression : originOnExpressions) { List<Table> currentTableList = onTableDeque.poll(); if (CollUtil.isEmpty(currentTableList)) { onExpressions.add(originOnExpression); } else { onExpressions.add( builderExpression(originOnExpression, currentTableList, whereSegment)); } } join.setOnExpressions(onExpressions); } leftTable = joinTable; } else { processOtherFromItem(joinItem, whereSegment); leftTable = null; } }
return mainTables; } }
|