这是Eclipse Link中缺失的一个功能。我已经为此开发了一个补丁。
package org.eclipse.persistence.internal.expressions;
import java.io.*;
import java.util.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eclipse.persistence.internal.helper.*;
import org.eclipse.persistence.internal.sessions.AbstractSession;
import org.eclipse.persistence.queries.*;
import org.eclipse.persistence.exceptions.*;
import org.eclipse.persistence.expressions.*;
import org.eclipse.persistence.internal.databaseaccess.*;
import org.eclipse.persistence.internal.sessions.AbstractRecord;
public class ExpressionSQLPrinter {
protected AbstractSession session;
protected DatabasePlatform platform;
protected SQLCall call;
protected AbstractRecord translationRow;
protected boolean shouldPrintQualifiedNames;
protected Writer writer;
protected boolean requiresDistinct;
protected boolean isFirstElementPrinted;
private final ExpressionBuilder builder;
public ExpressionSQLPrinter(AbstractSession session, AbstractRecord translationRow, SQLCall call, boolean printQualifiedNames, ExpressionBuilder builder) {
this.session = session;
this.translationRow = translationRow;
this.call = call;
this.shouldPrintQualifiedNames = printQualifiedNames;
if (builder == null || builder.getDescriptor() == null) {
this.platform = getSession().getPlatform();
} else {
this.platform = (DatabasePlatform) getSession().getPlatform(builder.getDescriptor().getJavaClass());
}
this.requiresDistinct = false;
this.builder = builder;
isFirstElementPrinted = false;
}
public SQLCall getCall() {
return call;
}
public DatabasePlatform getPlatform() {
return this.platform;
}
protected AbstractSession getSession() {
return session;
}
protected AbstractRecord getTranslationRow() {
return translationRow;
}
public Writer getWriter() {
return writer;
}
public boolean isFirstElementPrinted() {
return isFirstElementPrinted;
}
public void printExpression(Expression expression) {
translateExpression(expression);
}
public void printField(DatabaseField field) {
if (field == null) {
return;
}
if (builder.getTableAliases() != null) {
DatabaseTable keyAtValue = builder.getTableAliases().keyAtValue(field.getTable());
if (keyAtValue != null) {
field.setTableName(keyAtValue.getName());
}
}
try {
if (shouldPrintQualifiedNames()) {
getWriter().write(field.getQualifiedNameDelimited(platform));
} else {
getWriter().write(field.getNameDelimited(platform));
}
} catch (IOException exception) {
throw ValidationException.fileError(exception);
}
}
public void printParameter(ParameterExpression expression) {
try {
final Logger logger = LogManager.getLogger();
getCall().appendTranslationParameter(getWriter(), expression, getPlatform(), getTranslationRow());
} catch (IOException exception) {
throw ValidationException.fileError(exception);
}
}
public void printParameter(DatabaseField field) {
getCall().appendTranslation(getWriter(), field);
}
public void printPrimitive(Object value) {
if (value instanceof Collection) {
printValuelist((Collection) value);
return;
}
session.getPlatform().appendLiteralToCall(getCall(), getWriter(), value);
}
public void printNull(ConstantExpression nullValueExpression) {
if (session.getPlatform().shouldBindLiterals()) {
DatabaseField field = null;
Expression localBase = nullValueExpression.getLocalBase();
if (localBase.isFieldExpression()) {
field = ((FieldExpression) localBase).getField();
} else if (localBase.isQueryKeyExpression()) {
field = ((QueryKeyExpression) localBase).getField();
}
session.getPlatform().appendLiteralToCall(getCall(), getWriter(), field);
} else {
session.getPlatform().appendLiteralToCall(getCall(), getWriter(), null);
}
}
public void printString(String value) {
try {
getWriter().write(value);
} catch (IOException exception) {
throw ValidationException.fileError(exception);
}
}
public void printValuelist(Collection values) {
try {
getWriter().write("(");
Iterator valuesEnum = values.iterator();
while (valuesEnum.hasNext()) {
Object value = valuesEnum.next();
if (value instanceof Collection) {
printValuelist((Collection) value);
} else if (value instanceof Expression) {
((Expression) value).printSQL(this);
} else if (value instanceof DatabaseField) {
printExpression(builder.getField((DatabaseField) value));
} else {
session.getPlatform().appendLiteralToCall(getCall(), getWriter(), value);
}
if (valuesEnum.hasNext()) {
getWriter().write(", ");
}
}
getWriter().write(")");
} catch (IOException exception) {
throw ValidationException.fileError(exception);
}
}
public void printList(Collection values) {
try {
getWriter().write("(");
Iterator valuesEnum = values.iterator();
while (valuesEnum.hasNext()) {
Object value = valuesEnum.next();
if (value instanceof Expression) {
((Expression) value).printSQL(this);
} else {
session.getPlatform().appendLiteralToCall(getCall(), getWriter(), value);
}
if (valuesEnum.hasNext()) {
getWriter().write(", ");
}
}
getWriter().write(")");
} catch (IOException exception) {
throw ValidationException.fileError(exception);
}
}
public boolean requiresDistinct() {
return requiresDistinct;
}
protected void setCall(SQLCall call) {
this.call = call;
}
public void setIsFirstElementPrinted(boolean isFirstElementPrinted) {
this.isFirstElementPrinted = isFirstElementPrinted;
}
public void setRequiresDistinct(boolean requiresDistinct) {
this.requiresDistinct = requiresDistinct;
}
protected void setSession(AbstractSession theSession) {
session = theSession;
}
protected void setShouldPrintQualifiedNames(boolean shouldPrintQualifiedNames) {
this.shouldPrintQualifiedNames = shouldPrintQualifiedNames;
}
protected void setTranslationRow(AbstractRecord theRow) {
translationRow = theRow;
}
public void setWriter(Writer writer) {
this.writer = writer;
}
public boolean shouldPrintParameterValues() {
return getTranslationRow() != null;
}
protected boolean shouldPrintQualifiedNames() {
return shouldPrintQualifiedNames;
}
protected void translateExpression(Expression theExpression) {
theExpression.printSQL(this);
}
}
补丁由//start of patch n和//end of patch n分隔。
我会尝试将其提交到上游,但可能需要一些时间。
WHERE ((user_group_id, group_id) IN ((?, ?), (?, ?)))
的查询?MySQL 5.x支持此功能。如果可能的话,这将更好 :) 无论如何,非常感谢。 - Tiny