/** * 租户字段别名设置 * <p>tableName.tenantId 或 tableAlias.tenantId</p> * * @param table 表对象 * @return 字段 */ protected Column getAliasColumn(Table table) { StringBuilder column = new StringBuilder(); if (null == table.getAlias()) { column.append(table.getName()); } else { column.append(table.getAlias().getName()); } column.append(StringPool.DOT); column.append(tenantHandler.getTenantIdColumn()); return new Column(column.toString()); } }
@Override public void visit(SelectExpressionItem selectExpressionItem) { if (firstRun) { if (selectExpressionItem.getAlias() != null) { aliases.add(selectExpressionItem.getAlias().getName().toUpperCase()); } } else { if (selectExpressionItem.getAlias() == null) { while (true) { String alias = getNextAlias().toUpperCase(); if (!aliases.contains(alias)) { aliases.add(alias); selectExpressionItem.setAlias(new Alias(alias)); break; } } } } }
@Override public void visit(SelectExpressionItem selectExpr) { Alias alias = selectExpr.getAlias(); if (alias != null) { Expression e = selectExpr.getExpression(); e.accept(expressionVisitor); // NORMALIZE EXPRESSION ALIAS NAME QuotedID aliasName = idfac.createAttributeID(alias.getName()); alias.setName(aliasName.getSQLRendering()); aliasMap.put(aliasName, e); } // ELSE // ROMAN (27 Sep 2015): set an error flag -- each complex expression must have a name (alias) } };
res.getAlias().setUseAs(useASInTableAlias); res.getAlias().setUseAs(useASInTableAlias); } else { assert plainSelect.getFromItem().getAlias()!=null; leftTable = new Table(plainSelect.getFromItem().getAlias().getName()); } else { assert join.getRightItem().getAlias()!=null; rightTable = new Table(join.getRightItem().getAlias().getName());
@Override public void visit(PlainSelect plainSelect) { for (SelectItem item : plainSelect.getSelectItems()) { item.accept(this); } if (itemsExpr.size() > 1) { BinaryExpression binExpr = createBinaryExpression(); binExpr.setLeftExpression(itemsExpr.get(0).getExpression()); for (int i = 1; i < itemsExpr.size() - 1; i++) { binExpr.setRightExpression(itemsExpr.get(i).getExpression()); BinaryExpression binExpr2 = createBinaryExpression(); binExpr2.setLeftExpression(binExpr); binExpr = binExpr2; } binExpr.setRightExpression(itemsExpr.get(itemsExpr.size() - 1).getExpression()); SelectExpressionItem sei = new SelectExpressionItem(); sei.setExpression(binExpr); plainSelect.getSelectItems().clear(); plainSelect.getSelectItems().add(sei); } ((SelectExpressionItem) plainSelect.getSelectItems().get(0)).setAlias(new Alias(alias)); }
@Override public void visit(Table tableName) { if (tableName == null) { return; } String name = tableName.getName(); if (name != null) { tableName.setName(CompatibilityUtils.fixMySqlName(name.toLowerCase())); } Alias alias = tableName.getAlias(); if (alias != null) { String aliasName = alias.getName(); if (aliasName != null) { alias.setName(CompatibilityUtils.fixMySqlName(aliasName.toLowerCase())); } } }
/** * 包装SetOperationList * * @param setOperationList * @return */ protected SelectBody wrapSetOperationList(SetOperationList setOperationList) { //获取最后一个plainSelect SelectBody setSelectBody = setOperationList.getSelects().get(setOperationList.getSelects().size() - 1); if (!(setSelectBody instanceof PlainSelect)) { throw new PageException("目前无法处理该SQL,您可以将该SQL发送给abel533@gmail.com协助作者解决!"); } PlainSelect plainSelect = (PlainSelect) setSelectBody; PlainSelect selectBody = new PlainSelect(); List<SelectItem> selectItems = getSelectItems(plainSelect); selectBody.setSelectItems(selectItems); //设置fromIterm SubSelect fromItem = new SubSelect(); fromItem.setSelectBody(setOperationList); fromItem.setAlias(new Alias(WRAP_TABLE)); selectBody.setFromItem(fromItem); //order by if (isNotEmptyList(plainSelect.getOrderByElements())) { selectBody.setOrderByElements(plainSelect.getOrderByElements()); plainSelect.setOrderByElements(null); } return selectBody; }
/** * Get name with out without using aliases. * * @param aliases * @return */ public String getName(boolean aliases) { StringBuilder fqn = new StringBuilder(); if (table != null) { if (table.getAlias() != null && aliases) { fqn.append(table.getAlias().getName()); } else { fqn.append(table.getFullyQualifiedName()); } } if (fqn.length() > 0) { fqn.append('.'); } if (columnName != null) { fqn.append(columnName); } return fqn.toString(); }
throw new ParseException(); return new Alias(name,useAs);
@Override public void visit(Column tableColumn) { final Table table = tableColumn.getTable(); String tableName = null; if (table != null) { if (table.getAlias() != null) { tableName = table.getAlias().getName(); } else { tableName = table.getFullyQualifiedName(); } } if (tableName != null && !tableName.isEmpty()) { buffer.append(tableName).append("."); } buffer.append(tableColumn.getColumnName()); }
@Override public void visit(SelectExpressionItem selectExpressionItem) { if (firstRun) { if (selectExpressionItem.getAlias() != null) { aliases.add(selectExpressionItem.getAlias().getName().toUpperCase()); } } else { if (selectExpressionItem.getAlias() == null) { while (true) { String alias = getNextAlias().toUpperCase(); if (!aliases.contains(alias)) { aliases.add(alias); selectExpressionItem.setAlias(new Alias(alias)); break; } } } } }
table.setAlias(new Alias(name,useAs)); break;
if (selectExpressionItem.getAlias() != null) { Column column = new Column(selectExpressionItem.getAlias().getName()); SelectExpressionItem expressionItem = new SelectExpressionItem(column); selectItems.add(expressionItem);
@Override public void visit(PlainSelect plainSelect) { for (SelectItem item : plainSelect.getSelectItems()) { item.accept(this); } if (itemsExpr.size() > 1) { BinaryExpression binExpr = createBinaryExpression(); binExpr.setLeftExpression(itemsExpr.get(0).getExpression()); for (int i = 1; i < itemsExpr.size() - 1; i++) { binExpr.setRightExpression(itemsExpr.get(i).getExpression()); BinaryExpression binExpr2 = createBinaryExpression(); binExpr2.setLeftExpression(binExpr); binExpr = binExpr2; } binExpr.setRightExpression(itemsExpr.get(itemsExpr.size() - 1).getExpression()); SelectExpressionItem sei = new SelectExpressionItem(); sei.setExpression(binExpr); plainSelect.getSelectItems().clear(); plainSelect.getSelectItems().add(sei); } ((SelectExpressionItem) plainSelect.getSelectItems().get(0)).setAlias(new Alias(alias)); }
Expression rightExpression = ((BinaryExpression) expression).getRightExpression(); if (joinTable != null && rightExpression instanceof Column) { if (Objects.equals(((Column) rightExpression).getTable().getName(), table.getAlias().getName())) { validUseIndex(table, ((Column) rightExpression).getColumnName(), connection); validUseIndex(joinTable, ((Column) leftExpression).getColumnName(), connection);
@Override public void visit(Column column) { Table table = column.getTable(); if (table == null || StringUtils.isBlank(table.getName())) { Table t = new Table(tableName); if (tableAlias != null) { t.setAlias(new Alias(tableAlias, false)); } column.setTable(t); } } });
private Table findTable(List<Table> tables, String alias) { for (Table t : tables) { if (alias.equals(t.getAlias().getName())) { return t; } } return null; }