Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[master] Issue 1815: Transforming object types for collections #1842

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,33 @@ public CollectionExpression(Object newValue, Expression baseExpression) {

@Override
public void printSQL(ExpressionSQLPrinter printer) {
Object value = this.value;
Object objectValue = this.value;
if(this.localBase != null) {
value = this.localBase.getFieldValue(value, getSession());
if(objectValue instanceof Collection) {
Collection<?> values = (Collection<?>)objectValue;
Vector<Object> fieldValues = new Vector<>(values.size());
for (Object value : values) {
Object translated = value;
if (!(value instanceof Expression)){
translated = this.localBase.getFieldValue(value, getSession());
}
fieldValues.add(translated);
}
objectValue = fieldValues;
}
}
printer.printList((Collection)value, this.canBind);
printer.printList((Collection)objectValue, this.canBind);
}

// @Override
// public void printSQL(ExpressionSQLPrinter printer) {
// Object value = this.value;
// if(this.localBase != null) {
// value = this.localBase.getFieldValue(value, getSession());
// }
// printer.printList((Collection)value, this.canBind);
// }

/**
* INTERNAL:
* Return the value for in memory comparison.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.eclipse.persistence.descriptors.ClassDescriptor;
import org.eclipse.persistence.exceptions.QueryException;
Expand Down Expand Up @@ -320,6 +321,24 @@ public Object getValue(AbstractRecord translationRow, DatabaseQuery query, Abstr
value = this.localBase.getFieldValue(value, session);
}

// // Convert the value to the correct type, i.e. object type mappings.
// if (this.localBase != null) {
// if(value instanceof Collection) {
// Collection<?> values = (Collection<?>)value;
// Vector<Object> fieldValues = new Vector<>(values.size());
// for (Object v : values) {
// Object translated = v;
// if (!(v instanceof Expression)){
// translated = this.localBase.getFieldValue(v, getSession());
// }
// fieldValues.add(translated);
// }
// value = fieldValues;
// } else {
// value = this.localBase.getFieldValue(value, session);
// }
// }

return value;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 1998, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2023 IBM Corporation. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down Expand Up @@ -414,6 +415,57 @@ public List<DatabaseField> getSelectionFields(ReadQuery query) {
}
}

// /**
// * INTERNAL:
// * Transform the object-level value into a database-level value
// */
// @Override
// public Object getFieldValue(Object objectValue, AbstractSession session) {
// DatabaseMapping mapping = getMapping();
// Object fieldValue = objectValue;
// if (mapping != null) {
// if (mapping.isAbstractDirectMapping() || mapping.isDirectCollectionMapping()) {
// // CR#3623207, check for IN Collection here not in mapping.
// if (objectValue instanceof Collection) {
// // This can actually be a collection for IN within expressions... however it would be better for expressions to handle this.
// Collection values = (Collection)objectValue;
// Vector fieldValues = new Vector(values.size());
// for (Iterator iterator = values.iterator(); iterator.hasNext();) {
// Object value = iterator.next();
// if (!(value instanceof Expression)){
// value = getFieldValue(value, session);
// }
// fieldValues.add(value);
// }
// fieldValue = fieldValues;
// } else {
// if (mapping.isAbstractColumnMapping()) {
// fieldValue = ((AbstractColumnMapping)mapping).getFieldValue(objectValue, session);
// } else if (mapping.isDirectCollectionMapping()) {
// fieldValue = ((DirectCollectionMapping)mapping).getFieldValue(objectValue, session);
// }
// }
// } else if ((objectValue instanceof Collection) && (mapping.isForeignReferenceMapping())) {
// // Was an IN with a collection of objects, extract their ids.
// List ids = new ArrayList();
// for (Object object : ((Collection)objectValue)) {
// if ((mapping.getReferenceDescriptor() != null) && (mapping.getReferenceDescriptor().getJavaClass().isInstance(object))) {
// Object id = mapping.getReferenceDescriptor().getObjectBuilder().extractPrimaryKeyFromObject(object, session);
// if (id instanceof CacheId) {
// id = Arrays.asList(((CacheId)id).getPrimaryKey());
// }
// ids.add(id);
// } else {
// ids.add(object);
// }
// }
// fieldValue = ids;
// }
// }
//
// return fieldValue;
// }

/**
* INTERNAL:
* Transform the object-level value into a database-level value
Expand All @@ -424,47 +476,135 @@ public Object getFieldValue(Object objectValue, AbstractSession session) {
Object fieldValue = objectValue;
if (mapping != null) {
if (mapping.isAbstractDirectMapping() || mapping.isDirectCollectionMapping()) {
// CR#3623207, check for IN Collection here not in mapping.
if (objectValue instanceof Collection) {
// This can actually be a collection for IN within expressions... however it would be better for expressions to handle this.
Collection values = (Collection)objectValue;
Vector fieldValues = new Vector(values.size());
for (Iterator iterator = values.iterator(); iterator.hasNext();) {
Object value = iterator.next();
if (!(value instanceof Expression)){
value = getFieldValue(value, session);
}
fieldValues.add(value);
}
fieldValue = fieldValues;
} else {
if (mapping.isAbstractColumnMapping()) {
fieldValue = ((AbstractColumnMapping)mapping).getFieldValue(objectValue, session);
} else if (mapping.isDirectCollectionMapping()) {
fieldValue = ((DirectCollectionMapping)mapping).getFieldValue(objectValue, session);
}
if (mapping.isAbstractColumnMapping()) {
fieldValue = ((AbstractColumnMapping)mapping).getFieldValue(objectValue, session);
} else if (mapping.isDirectCollectionMapping()) {
fieldValue = ((DirectCollectionMapping)mapping).getFieldValue(objectValue, session);
}
} else if ((objectValue instanceof Collection) && (mapping.isForeignReferenceMapping())) {
// Was an IN with a collection of objects, extract their ids.
List ids = new ArrayList();
for (Object object : ((Collection)objectValue)) {
if ((mapping.getReferenceDescriptor() != null) && (mapping.getReferenceDescriptor().getJavaClass().isInstance(object))) {
Object id = mapping.getReferenceDescriptor().getObjectBuilder().extractPrimaryKeyFromObject(object, session);
if (id instanceof CacheId) {
id = Arrays.asList(((CacheId)id).getPrimaryKey());
}
ids.add(id);
} else {
ids.add(object);
if ((mapping.getReferenceDescriptor() != null) && (mapping.getReferenceDescriptor().getJavaClass().isInstance(objectValue))) {
Object id = mapping.getReferenceDescriptor().getObjectBuilder().extractPrimaryKeyFromObject(objectValue, session);
if (id instanceof CacheId) {
id = Arrays.asList(((CacheId)id).getPrimaryKey());
}
fieldValue = id;
}
fieldValue = ids;
}
}

return fieldValue;
}

// /**
// * INTERNAL:
// * Transform the object-level value into a database-level value
// */
// @Override
// public Object getFieldValue(Object objectValue, AbstractSession session) {
// DatabaseMapping mapping = getMapping();
// Object fieldValue = objectValue;
// if (mapping != null) {
// if (mapping.isAbstractDirectMapping() || mapping.isDirectCollectionMapping()) {
//
//// if (objectValue instanceof Collection<?>) {
//// // This can actually be a collection for IN within expressions... however it would be better for expressions to handle this.
//// Collection<?> values = (Collection<?>)objectValue;
////
//// if(values.size() > 0 && ) {
////
//// }
////
//// Vector<Object> fieldValues = new Vector<>(values.size());
//// Class<?> cls = mapping.getAttributeClassification();
////
//// for (Object value : values) {
//// // If the element isn't the correct type
//// if(cls != null && !cls.isAssignableFrom(value.getClass())) {
//// value = getFieldValue(value, session);
//// } else if (!(value instanceof Expression)){
//// value = getFieldValue(value, session);
//// }
//// fieldValues.add(value);
//// }
//// fieldValue = fieldValues;
//// } else {
//// if (mapping.isAbstractColumnMapping()) {
//// fieldValue = ((AbstractColumnMapping)mapping).getFieldValue(objectValue, session);
//// } else if (mapping.isDirectCollectionMapping()) {
//// fieldValue = ((DirectCollectionMapping)mapping).getFieldValue(objectValue, session);
//// }
//// }
//
// Class<?> cls = mapping.getAttributeClassification();
//
// // CR#3623207, check for IN Collection here not in mapping.
// // Translate the individual elements of a collection to match the mapping type. This is important for IN collections
// // However, only translate the elements if the mapping is not to a collection itself. Otherwise, we want to maintain a Collection for serialization
// if(objectValue instanceof Collection<?> && cls != null && !cls.isAssignableFrom(objectValue.getClass())) {
// // This can actually be a collection for IN within expressions... however it would be better for expressions to handle this.
// Collection<?> values = (Collection<?>)objectValue;
// Vector<Object> fieldValues = new Vector<>(values.size());
// for (Iterator<?> iterator = values.iterator(); iterator.hasNext();) {
// Object value = iterator.next();
// if (!(value instanceof Expression)) {
// if (mapping.isAbstractColumnMapping()) {
// value = ((AbstractColumnMapping)mapping).getFieldValue(value, session);
// } else if (mapping.isDirectCollectionMapping()) {
// value = ((DirectCollectionMapping)mapping).getFieldValue(value, session);
// }
// }
// fieldValues.add(value);
// }
// fieldValue = fieldValues;
// } else {
// if (mapping.isAbstractColumnMapping()) {
// fieldValue = ((AbstractColumnMapping)mapping).getFieldValue(objectValue, session);
// } else if (mapping.isDirectCollectionMapping()) {
// fieldValue = ((DirectCollectionMapping)mapping).getFieldValue(objectValue, session);
// }
// }
//
//// if (objectValue instanceof Collection
//// && (cls != null && !(cls.isArray() || cls.isInstance(Collection.class)))) {
//// // This can actually be a collection for IN within expressions... however it would be better for expressions to handle this.
//// Collection<?> values = (Collection<?>)objectValue;
//// Vector<Object> fieldValues = new Vector<>(values.size());
//// for (Iterator<?> iterator = values.iterator(); iterator.hasNext();) {
//// Object value = iterator.next();
//// if (!(value instanceof Expression)){
//// value = getFieldValue(value, session);
//// }
//// fieldValues.add(value);
//// }
//// fieldValue = fieldValues;
//// } else {
//// if (mapping.isAbstractColumnMapping()) {
//// fieldValue = ((AbstractColumnMapping)mapping).getFieldValue(objectValue, session);
//// } else if (mapping.isDirectCollectionMapping()) {
//// fieldValue = ((DirectCollectionMapping)mapping).getFieldValue(objectValue, session);
//// }
//// }
// } else if ((objectValue instanceof Collection<?>) && (mapping.isForeignReferenceMapping())) {
// // Was an IN with a collection of objects, extract their ids.
// List<Object> ids = new ArrayList<>();
// for (Object object : ((Collection<?>)objectValue)) {
// if ((mapping.getReferenceDescriptor() != null) && (mapping.getReferenceDescriptor().getJavaClass().isInstance(object))) {
// Object id = mapping.getReferenceDescriptor().getObjectBuilder().extractPrimaryKeyFromObject(object, session);
// if (id instanceof CacheId) {
// id = Arrays.asList(((CacheId)id).getPrimaryKey());
// }
// ids.add(id);
// } else {
// ids.add(object);
// }
// }
// fieldValue = ids;
// }
// }
//
// return fieldValue;
// }

@Override
public DatabaseMapping getMapping() {
if (!hasMapping) {
Expand Down
Loading