Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library 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 Lesser General Public License for more
* details.
*/
package com.liferay.change.tracking.internal.reference;
import com.liferay.change.tracking.spi.reference.TableReferenceDefinition;
import com.liferay.petra.sql.dsl.Column;
import com.liferay.petra.sql.dsl.DSLQueryFactoryUtil;
import com.liferay.petra.sql.dsl.Table;
import com.liferay.petra.sql.dsl.ast.ASTNode;
import com.liferay.petra.sql.dsl.ast.ASTNodeListener;
import com.liferay.petra.sql.dsl.expression.Expression;
import com.liferay.petra.sql.dsl.expression.Predicate;
import com.liferay.petra.sql.dsl.query.DSLQuery;
import com.liferay.petra.sql.dsl.query.FromStep;
import com.liferay.petra.sql.dsl.query.JoinStep;
import com.liferay.petra.sql.dsl.query.WhereStep;
import com.liferay.petra.sql.dsl.spi.expression.DSLFunction;
import com.liferay.petra.sql.dsl.spi.expression.DefaultPredicate;
import com.liferay.petra.sql.dsl.spi.expression.Operand;
import com.liferay.petra.sql.dsl.spi.expression.Scalar;
import com.liferay.petra.sql.dsl.spi.query.From;
import com.liferay.petra.sql.dsl.spi.query.Join;
import com.liferay.petra.sql.dsl.spi.query.JoinType;
import com.liferay.petra.string.StringBundler;
import com.liferay.petra.string.StringPool;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Queue;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Function;
/**
* @author Preston Crary
*/
public class TableJoinHolderFactory {
public static > TableJoinHolder create(
Function joinFunction, boolean parent,
Column primaryKeyColumn,
TableReferenceDefinition tableReferenceDefinition) {
JoinStep joinStep = joinFunction.apply(_validationFromStep);
if (!(joinStep instanceof Join)) {
throw new IllegalArgumentException(
StringBundler.concat("Missing join in \"", joinStep, "\""));
}
JoinStepASTNodeListener joinStepASTNodeListener =
new JoinStepASTNodeListener<>(tableReferenceDefinition.getTable());
joinStep.toSQL(_emptyStringConsumer, joinStepASTNodeListener);
if (joinStepASTNodeListener._fromTable == null) {
throw new IllegalArgumentException(
StringBundler.concat(
"Join function must use provided from step for join step ",
"\"", joinStep, "\""));
}
if (!joinStepASTNodeListener._hasRequiredTable) {
throw new IllegalArgumentException(
StringBundler.concat(
"Required table \"", tableReferenceDefinition.getTable(),
"\" is unused in join step \"", joinStep, "\""));
}
if (joinStepASTNodeListener._invalidJoin != null) {
throw new IllegalArgumentException(
StringBundler.concat(
"Invalid join for join step \"", joinStep,
"\", ensure table alias is used for self joins"));
}
if (joinStepASTNodeListener._invalidJoinOrder) {
throw new IllegalArgumentException(
StringBundler.concat(
"First join must be on table \"",
tableReferenceDefinition.getTable(), "\" for join step \"",
joinStep, "\""));
}
if (joinStepASTNodeListener._invalidJoinType != null) {
throw new IllegalArgumentException(
StringBundler.concat(
"Invalid join type \"",
joinStepASTNodeListener._invalidJoinType,
"\" for join step \"", joinStep, "\""));
}
if (joinStepASTNodeListener._invalidOperand != null) {
throw new IllegalArgumentException(
StringBundler.concat(
"Invalid predicate operand \"",
joinStepASTNodeListener._invalidOperand,
"\" for join step \"", joinStep, "\""));
}
if (!joinStepASTNodeListener._tables.containsAll(
joinStepASTNodeListener._columnTables)) {
List
> columnTables = new ArrayList<>(
joinStepASTNodeListener._columnTables);
Comparator
> comparator = Comparator.comparing(
Table::getName);
columnTables.sort(comparator);
List
> joinTables = new ArrayList<>(
joinStepASTNodeListener._tables);
joinTables.sort(comparator);
throw new IllegalArgumentException(
StringBundler.concat(
"Predicate column tables ", columnTables,
" do not match join tables ", joinTables,
" for join step \"", joinStep, "\""));
}
Column, Long> fromPKColumn = TableUtil.getPrimaryKeyColumn(
joinStepASTNodeListener._fromTable);
if (fromPKColumn == null) {
throw new IllegalArgumentException(
StringBundler.concat(
"No long type primary key column found for table \"",
joinStepASTNodeListener._fromTable, "\" for join step \"",
joinStep, "\""));
}
WhereStep missingRequirementWhereStep = null;
Predicate missingRequirementWherePredicate = null;
if (parent) {
missingRequirementWherePredicate = fromPKColumn.isNull();
List bridgePredicates = _getBridgePredicates(
joinStep);
Set> childColumns = _getChildColumns(
primaryKeyColumn.getTable(), joinStep);
Iterator iterator = bridgePredicates.iterator();
while (iterator.hasNext()) {
BridgePredicate bridgePredicate = iterator.next();
if (bridgePredicate.hasOnlyTable(primaryKeyColumn.getTable())) {
missingRequirementWherePredicate =
missingRequirementWherePredicate.and(
bridgePredicate._predicate);
iterator.remove();
}
}
missingRequirementWhereStep = _getMissingRequirementWhereStep(
primaryKeyColumn, fromPKColumn, bridgePredicates);
for (Column, ?> column : childColumns) {
missingRequirementWherePredicate =
missingRequirementWherePredicate.and(column.isNotNull());
Class> clazz = column.getJavaType();
if (clazz == String.class) {
Column, String> stringColumn = (Column, String>)column;
missingRequirementWherePredicate =
missingRequirementWherePredicate.and(
stringColumn.neq(StringPool.BLANK));
}
else if (clazz == Long.class) {
Column, Long> longColumn = (Column, Long>)column;
missingRequirementWherePredicate =
missingRequirementWherePredicate.and(
longColumn.neq(0L));
}
}
}
return new TableJoinHolder(
primaryKeyColumn, joinFunction, missingRequirementWherePredicate,
missingRequirementWhereStep, fromPKColumn);
}
private static List _getBridgePredicates(
JoinStep joinStep) {
Queue defaultPredicateQueue = new LinkedList<>();
Queue> expressionQueue = new LinkedList<>();
List bridgePredicates = new LinkedList<>();
ASTNode astNode = joinStep;
while (astNode instanceof Join) {
Join join = (Join)astNode;
defaultPredicateQueue.add((DefaultPredicate)join.getOnPredicate());
DefaultPredicate defaultPredicate = null;
while ((defaultPredicate = defaultPredicateQueue.poll()) != null) {
Expression> leftExpression =
defaultPredicate.getLeftExpression();
Expression> rightExpression =
defaultPredicate.getRightExpression();
if (defaultPredicate.getOperand() == Operand.AND) {
defaultPredicateQueue.add((DefaultPredicate)leftExpression);
defaultPredicateQueue.add(
(DefaultPredicate)rightExpression);
}
else {
Set