mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Refactor FetchConfig
This commit is contained in:
+8
-5
@@ -5,7 +5,6 @@ import io.ebean.annotation.FetchPreference;
|
||||
import io.ebean.annotation.HistoryExclude;
|
||||
import io.ebean.annotation.Where;
|
||||
import io.ebean.bean.BeanCollection.ModifyListenMode;
|
||||
import io.ebean.config.BeanNotRegisteredException;
|
||||
import io.ebean.config.NamingConvention;
|
||||
import io.ebean.config.TableName;
|
||||
import io.ebean.core.type.ScalarType;
|
||||
@@ -140,15 +139,15 @@ class AnnotationAssocManys extends AnnotationAssoc {
|
||||
JoinTable joinTable = get(prop, JoinTable.class);
|
||||
if (joinTable != null) {
|
||||
if (prop.isManyToMany()) {
|
||||
// expected this
|
||||
readJoinTable(joinTable, prop);
|
||||
|
||||
} else {
|
||||
// OneToMany with @JoinTable
|
||||
prop.setO2mJoinTable();
|
||||
readJoinTable(joinTable, prop);
|
||||
manyToManyDefaultJoins(prop);
|
||||
}
|
||||
} else if (prop.isManyToMany()) {
|
||||
checkSelfManyToMany(prop);
|
||||
}
|
||||
|
||||
if (prop.getMappedBy() != null) {
|
||||
@@ -181,6 +180,12 @@ class AnnotationAssocManys extends AnnotationAssoc {
|
||||
}
|
||||
}
|
||||
|
||||
private void checkSelfManyToMany(DeployBeanPropertyAssocMany<?> prop) {
|
||||
if (prop.getTargetType().equals(descriptor.getBeanType())) {
|
||||
throw new IllegalStateException("@ManyToMany mapping for " + prop.getFullBeanName() + " requires explicit @JoinTable with joinColumns & inverseJoinColumns. Refer issue #2157");
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void readElementCollection(DeployBeanPropertyAssocMany<?> prop, ElementCollection elementCollection) {
|
||||
|
||||
@@ -430,7 +435,6 @@ class AnnotationAssocManys extends AnnotationAssoc {
|
||||
}
|
||||
|
||||
private void readToMany(ManyToMany propAnn, DeployBeanPropertyAssocMany<?> manyProp) {
|
||||
|
||||
manyProp.setMappedBy(propAnn.mappedBy());
|
||||
manyProp.setFetchType(propAnn.fetch());
|
||||
setCascadeTypes(propAnn.cascade(), manyProp.getCascadeInfo());
|
||||
@@ -442,7 +446,6 @@ class AnnotationAssocManys extends AnnotationAssoc {
|
||||
}
|
||||
|
||||
private void readToOne(OneToMany propAnn, DeployBeanPropertyAssocMany<?> manyProp) {
|
||||
|
||||
manyProp.setMappedBy(propAnn.mappedBy());
|
||||
manyProp.setFetchType(propAnn.fetch());
|
||||
setCascadeTypes(propAnn.cascade(), manyProp.getCascadeInfo());
|
||||
|
||||
@@ -1136,81 +1136,81 @@ public class DefaultOrmQuery<T> implements SpiQuery<T> {
|
||||
private String planDescription() {
|
||||
StringBuilder sb = new StringBuilder(300);
|
||||
if (type != null) {
|
||||
sb.append("t:").append(type.ordinal());
|
||||
sb.append(type.ordinal());
|
||||
}
|
||||
if (useDocStore) {
|
||||
sb.append(",ds:");
|
||||
sb.append("/ds");
|
||||
}
|
||||
if (beanDescriptor.getDiscValue() != null) {
|
||||
sb.append(",disc:").append(beanDescriptor.getDiscValue());
|
||||
sb.append("/dv").append(beanDescriptor.getDiscValue());
|
||||
}
|
||||
if (temporalMode != SpiQuery.TemporalMode.CURRENT) {
|
||||
sb.append(",temp:").append(temporalMode.ordinal());
|
||||
sb.append("/tm").append(temporalMode.ordinal());
|
||||
if (versionsStart != null) {
|
||||
sb.append(",vb:");
|
||||
sb.append("v");
|
||||
}
|
||||
}
|
||||
if (forUpdate != null) {
|
||||
sb.append(",forUpd:").append(forUpdate.ordinal());
|
||||
sb.append("/fu").append(forUpdate.ordinal());
|
||||
if (lockType != null) {
|
||||
sb.append(",lt:").append(lockType.ordinal());
|
||||
sb.append("t").append(lockType.ordinal());
|
||||
}
|
||||
}
|
||||
if (id != null) {
|
||||
sb.append(",id:");
|
||||
sb.append("/id");
|
||||
}
|
||||
if (manualId) {
|
||||
sb.append(",manId:");
|
||||
sb.append("/md");
|
||||
}
|
||||
if (distinct) {
|
||||
sb.append(",dist:");
|
||||
sb.append("/dt");
|
||||
}
|
||||
if (allowLoadErrors) {
|
||||
sb.append(",allowLoadErrors:");
|
||||
sb.append("/ae");
|
||||
}
|
||||
if (disableLazyLoading) {
|
||||
sb.append(",disLazy:");
|
||||
sb.append("/dl");
|
||||
}
|
||||
if (baseTable != null) {
|
||||
sb.append(",baseTable:").append(baseTable);
|
||||
sb.append("/bt").append(baseTable);
|
||||
}
|
||||
if (rootTableAlias != null) {
|
||||
sb.append(",root:").append(rootTableAlias);
|
||||
sb.append("/ra").append(rootTableAlias);
|
||||
}
|
||||
if (orderBy != null) {
|
||||
sb.append(",orderBy:").append(orderBy.toStringFormat());
|
||||
sb.append("/ob").append(orderBy.toStringFormat());
|
||||
}
|
||||
if (m2mIncludeJoin != null) {
|
||||
sb.append(",m2m:").append(m2mIncludeJoin.getTable());
|
||||
sb.append("/m2").append(m2mIncludeJoin.getTable());
|
||||
}
|
||||
if (mapKey != null) {
|
||||
sb.append(",mapKey:").append(mapKey);
|
||||
sb.append("/mk").append(mapKey);
|
||||
}
|
||||
if (countDistinctOrder != null) {
|
||||
sb.append(",countDistOrd:").append(countDistinctOrder.name());
|
||||
sb.append("/cd").append(countDistinctOrder.name());
|
||||
}
|
||||
if (detail != null) {
|
||||
sb.append(" detail[");
|
||||
sb.append("/d[");
|
||||
detail.queryPlanHash(sb);
|
||||
sb.append("]");
|
||||
}
|
||||
if (bindParams != null) {
|
||||
sb.append(" bindParams[");
|
||||
sb.append("/b[");
|
||||
bindParams.buildQueryPlanHash(sb);
|
||||
sb.append("]");
|
||||
}
|
||||
if (whereExpressions != null) {
|
||||
sb.append(" where[");
|
||||
sb.append("/w[");
|
||||
whereExpressions.queryPlanHash(sb);
|
||||
sb.append("]");
|
||||
}
|
||||
if (havingExpressions != null) {
|
||||
sb.append(" having[");
|
||||
sb.append("/h[");
|
||||
havingExpressions.queryPlanHash(sb);
|
||||
sb.append("]");
|
||||
}
|
||||
if (updateProperties != null) {
|
||||
sb.append(" update[");
|
||||
sb.append("/u[");
|
||||
updateProperties.buildQueryPlanHash(sb);
|
||||
sb.append("]");
|
||||
}
|
||||
|
||||
@@ -106,15 +106,10 @@ public class OrmQueryDetail implements Serializable {
|
||||
return p1 == null ? p2 == null : p1.isSameByAutoTune(p2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return asString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the detail in string form.
|
||||
*/
|
||||
public String asString() {
|
||||
public String asStringDebug() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (!baseProps.isEmpty()) {
|
||||
baseProps.asStringDebug("select ", sb);
|
||||
@@ -150,7 +145,7 @@ public class OrmQueryDetail implements Serializable {
|
||||
* Set the base query properties to be empty.
|
||||
*/
|
||||
public void setEmptyBase() {
|
||||
this.baseProps = new OrmQueryProperties(null, new LinkedHashSet<>());
|
||||
this.baseProps = new OrmQueryProperties(null, Collections.emptySet());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -316,7 +311,7 @@ public class OrmQueryDetail implements Serializable {
|
||||
if (addId) {
|
||||
parentProp = new OrmQueryProperties(parentPath, assocOne.getTargetIdProperty());
|
||||
} else {
|
||||
parentProp = new OrmQueryProperties(parentPath, new LinkedHashSet<>());
|
||||
parentProp = new OrmQueryProperties(parentPath, Collections.emptySet());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ class OrmQueryPlanKey implements CQueryPlanKey {
|
||||
|
||||
@Override
|
||||
public CQueryPlanKey withDeleteByIds() {
|
||||
return new OrmQueryPlanKey(description + ":deleteByIds", 0, 0, null);
|
||||
return new OrmQueryPlanKey(description + "/deleteByIds", 0, 0, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+41
-106
@@ -3,8 +3,8 @@ package io.ebeaninternal.server.querydefn;
|
||||
import io.ebean.FetchConfig;
|
||||
import io.ebeaninternal.server.util.DSelectColumnsParser;
|
||||
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Parses the path properties string.
|
||||
@@ -22,9 +22,9 @@ class OrmQueryPropertiesParser {
|
||||
final boolean cache;
|
||||
final FetchConfig fetchConfig;
|
||||
final String properties;
|
||||
final LinkedHashSet<String> included;
|
||||
final Set<String> included;
|
||||
|
||||
Response(boolean readOnly, boolean cache, int queryFetchBatch, int lazyFetchBatch, String properties, LinkedHashSet<String> included) {
|
||||
Response(boolean readOnly, boolean cache, int queryFetchBatch, int lazyFetchBatch, String properties, Set<String> included) {
|
||||
this.readOnly = readOnly;
|
||||
this.cache = cache;
|
||||
this.properties = properties;
|
||||
@@ -55,9 +55,7 @@ class OrmQueryPropertiesParser {
|
||||
return new OrmQueryPropertiesParser(rawProperties).parse();
|
||||
}
|
||||
|
||||
private String inputProperties;
|
||||
|
||||
private String outputProperties = "";
|
||||
private final String inputProperties;
|
||||
private boolean allProperties;
|
||||
private boolean readOnly;
|
||||
private boolean cache;
|
||||
@@ -72,124 +70,61 @@ class OrmQueryPropertiesParser {
|
||||
* Parse the raw string properties input.
|
||||
*/
|
||||
private Response parse() {
|
||||
|
||||
if (inputProperties == null || inputProperties.isEmpty()) {
|
||||
return EMPTY;
|
||||
}
|
||||
int pos = inputProperties.indexOf("+readonly");
|
||||
if (pos > -1) {
|
||||
inputProperties = inputProperties.replace("+readonly", "");
|
||||
readOnly = true;
|
||||
}
|
||||
pos = inputProperties.indexOf("+cache");
|
||||
if (pos > -1) {
|
||||
inputProperties = inputProperties.replace("+cache", "");
|
||||
cache = true;
|
||||
}
|
||||
pos = inputProperties.indexOf("+query");
|
||||
if (pos > -1) {
|
||||
queryFetchBatch = parseBatchHint(pos, "+query");
|
||||
}
|
||||
pos = inputProperties.indexOf("+lazy");
|
||||
if (pos > -1) {
|
||||
lazyFetchBatch = parseBatchHint(pos, "+lazy");
|
||||
}
|
||||
|
||||
LinkedHashSet<String> included = parseIncluded();
|
||||
String properties = (allProperties) ? "*" : outputProperties;
|
||||
return new Response(readOnly, cache, queryFetchBatch, lazyFetchBatch, properties, included);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the include separating by comma or semicolon.
|
||||
*/
|
||||
private LinkedHashSet<String> parseIncluded() {
|
||||
|
||||
inputProperties = inputProperties.trim();
|
||||
if (inputProperties.isEmpty()) {
|
||||
// default properties
|
||||
return null;
|
||||
}
|
||||
if (inputProperties.equals("*")) {
|
||||
// explicit all properties
|
||||
allProperties = true;
|
||||
return null;
|
||||
return new Response(readOnly, cache, queryFetchBatch, lazyFetchBatch, "*", null);
|
||||
}
|
||||
|
||||
List<String> res = splitRawSelect(inputProperties);
|
||||
|
||||
StringBuilder sb = new StringBuilder(70);
|
||||
LinkedHashSet<String> propertySet = new LinkedHashSet<>(res.size() * 2);
|
||||
|
||||
int count = 0;
|
||||
String temp;
|
||||
for (String re : res) {
|
||||
temp = re.trim();
|
||||
if (!temp.isEmpty()) {
|
||||
if (count > 0) {
|
||||
sb.append(",");
|
||||
}
|
||||
sb.append(temp);
|
||||
propertySet.add(temp);
|
||||
count++;
|
||||
boolean hints = false;
|
||||
Set<String> fields = splitRawSelect(inputProperties);
|
||||
final Iterator<String> iterator = fields.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
String val = iterator.next();
|
||||
if (val.startsWith("+")) {
|
||||
hints = true;
|
||||
iterator.remove();
|
||||
parseHint(val);
|
||||
} else if (val.equals("*")) {
|
||||
allProperties = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (propertySet.isEmpty()) {
|
||||
// default properties
|
||||
return null;
|
||||
String properties = allProperties ? "*" : hints ? String.join(",", fields) : inputProperties;
|
||||
if (fields.isEmpty()) {
|
||||
fields = null;
|
||||
}
|
||||
return new Response(readOnly, cache, queryFetchBatch, lazyFetchBatch, properties, fields);
|
||||
}
|
||||
|
||||
if (propertySet.contains("*")) {
|
||||
// explicit all properties
|
||||
allProperties = true;
|
||||
return null;
|
||||
private void parseHint(String val) {
|
||||
if (val.equals("+readonly")) {
|
||||
readOnly = true;
|
||||
} else if (val.equals("+cache")) {
|
||||
cache = true;
|
||||
} else if (val.startsWith("+query")) {
|
||||
queryFetchBatch = parseBatch(val);
|
||||
} else if (val.startsWith("+lazy")) {
|
||||
lazyFetchBatch = parseBatch(val);
|
||||
}
|
||||
}
|
||||
|
||||
// partial properties
|
||||
outputProperties = sb.toString();
|
||||
return propertySet;
|
||||
private int parseBatch(String val) {
|
||||
if (val.endsWith(")")) {
|
||||
int start = val.lastIndexOf('(');
|
||||
if (start > 0) {
|
||||
return Integer.parseInt(val.substring(start + 1, val.length() - 1));
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Split allowing 'dynamic function based properties'.
|
||||
*/
|
||||
private List<String> splitRawSelect(String inputProperties) {
|
||||
private Set<String> splitRawSelect(String inputProperties) {
|
||||
return DSelectColumnsParser.parse(inputProperties);
|
||||
}
|
||||
|
||||
private int parseBatchHint(int pos, String option) {
|
||||
|
||||
int startPos = pos + option.length();
|
||||
int endPos = findEndPos(startPos, inputProperties);
|
||||
if (endPos == -1) {
|
||||
inputProperties = inputProperties.replace(option, "");
|
||||
return 0;
|
||||
|
||||
} else {
|
||||
|
||||
String batchParam = inputProperties.substring(startPos + 1, endPos);
|
||||
|
||||
if (endPos + 1 >= inputProperties.length()) {
|
||||
inputProperties = inputProperties.substring(0, pos);
|
||||
} else {
|
||||
inputProperties = inputProperties.substring(0, pos) + inputProperties.substring(endPos + 1);
|
||||
}
|
||||
return Integer.parseInt(batchParam);
|
||||
}
|
||||
}
|
||||
|
||||
private int findEndPos(int pos, String props) {
|
||||
|
||||
if (pos < props.length()) {
|
||||
if (props.charAt(pos) == '(') {
|
||||
int endPara = props.indexOf(')', pos + 1);
|
||||
if (endPara == -1) {
|
||||
throw new RuntimeException("Error could not find ')' in " + props + " after position " + pos);
|
||||
}
|
||||
return endPara;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import io.ebeaninternal.server.util.DSelectColumnsParser;
|
||||
import javax.persistence.PersistenceException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
@@ -28,11 +29,8 @@ final class DRawSqlColumnsParser {
|
||||
}
|
||||
|
||||
private ColumnMapping parse() {
|
||||
|
||||
List<String> columnList = DSelectColumnsParser.parse(sqlSelect);
|
||||
|
||||
Set<String> columnList = DSelectColumnsParser.parse(sqlSelect);
|
||||
List<ColumnMapping.Column> columns = new ArrayList<>(columnList.size());
|
||||
|
||||
for (String rawColumn : columnList) {
|
||||
columns.add(parseColumn(rawColumn));
|
||||
}
|
||||
@@ -40,7 +38,6 @@ final class DRawSqlColumnsParser {
|
||||
}
|
||||
|
||||
private ColumnMapping.Column parseColumn(String colInfo) {
|
||||
|
||||
String[] split = COLINFO_SPLIT.split(colInfo);
|
||||
if (split.length > 1) {
|
||||
ArrayList<String> tmp = new ArrayList<>(split.length);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
package io.ebeaninternal.server.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Splits a select clause into 'logical columns' taking into account functions and quotes.
|
||||
@@ -14,7 +14,7 @@ public final class DSelectColumnsParser {
|
||||
|
||||
private int pos;
|
||||
|
||||
public static List<String> parse(String sqlSelect) {
|
||||
public static Set<String> parse(String sqlSelect) {
|
||||
return new DSelectColumnsParser(sqlSelect).parse();
|
||||
}
|
||||
|
||||
@@ -23,9 +23,8 @@ public final class DSelectColumnsParser {
|
||||
this.end = selectClause.length();
|
||||
}
|
||||
|
||||
private List<String> parse() {
|
||||
|
||||
ArrayList<String> columns = new ArrayList<>();
|
||||
private Set<String> parse() {
|
||||
LinkedHashSet<String> columns = new LinkedHashSet<>();
|
||||
while (pos <= end) {
|
||||
columns.add(nextColumnInfo());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user