use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.
the class QueryOperation method invoke.
/**
* Execute <code>SELECT</code> operation on the database
* @param xrService parent <code>XRService</code> that owns this <code>Operation</code>
* @param invocation contains runtime argument values to be bound to the list of
* {@link Parameter}'s.
* @return result - the result of the underlying <code>SELECT</code> operation on
* the database, or <code>null</code>.
*
* @see Operation
*/
@Override
@SuppressWarnings({ "unchecked" })
public Object invoke(XRServiceAdapter xrService, Invocation invocation) {
DatabaseQuery query = queryHandler.getDatabaseQuery();
if (query.getProperty(DATABASEQUERY_STR) != null) {
query = (DatabaseQuery) query.getProperty(DATABASEQUERY_STR);
}
// a named query created via ORM metadata processing does not have
// parameters set, however, the operation should
List<Object> argVals = new ArrayList<>();
if (query.getArguments().size() == 0) {
int idx = 0;
for (Parameter param : getParameters()) {
// for custom SQL query (as configured via ORM metadata
// processing) we add args by position
query.addArgument(Integer.toString(++idx), Util.SCHEMA_2_CLASS.get(param.getType()));
argVals.add(invocation.getParameter(param.getName()));
}
} else {
// need to set argument values
for (Parameter param : getParameters()) {
argVals.add(invocation.getParameter(param.getName()));
}
}
// for SimpleXML + DataReadQuery we need to set MAP result type
if (isSimpleXMLFormat() && query.isDataReadQuery()) {
((DataReadQuery) query).setResultType(DataReadQuery.MAP);
}
// now execute the query
Object value = xrService.getORSession().getActiveSession().executeQuery(query, argVals);
if (value != null) {
// where before we'd expect an int value (typically 1) - need to handle this
if (result != null && (result.getType() == INT_QNAME || result.getType().equals(SXF_QNAME))) {
if (value instanceof ArrayList && ((ArrayList<?>) value).isEmpty()) {
((ArrayList<Integer>) value).add(1);
} else if (value instanceof Vector && ((Vector<?>) value).isEmpty()) {
((Vector<Integer>) value).add(1);
}
}
// Note that for legacy deployment XML projects this is not the case.
if (value instanceof ArrayList) {
ArrayList<?> returnedList = (ArrayList<?>) value;
if (returnedList.size() > 0 && returnedList.get(0) instanceof Object[]) {
Object[] objs = (Object[]) returnedList.get(0);
if (isCollection()) {
value = new ArrayList<>();
for (Object obj : objs) {
((ArrayList<Object>) value).add(obj);
}
} else {
value = objs[0];
}
}
}
// handle SimpleXML
if (isSimpleXMLFormat()) {
value = createSimpleXMLFormat(xrService, value);
} else {
if (!isCollection() && value instanceof Vector) {
// JPAQuery will return a single result in a Vector
if (((Vector<?>) value).isEmpty()) {
return null;
}
value = ((Vector<?>) value).firstElement();
}
QName resultType = getResultType();
if (resultType != null) {
// handle binary content
if (isAttachment() || (!isCollection() && resultType.equals(BASE_64_BINARY_QNAME))) {
String mimeType = DEFAULT_ATTACHMENT_MIMETYPE;
if (isAttachment() && result.getAttachment().getMimeType() != null) {
mimeType = result.getAttachment().getMimeType();
}
// handle BLOB types
if (value instanceof Blob) {
value = xrService.getOXSession().getDatasourcePlatform().getConversionManager().convertObject(value, ClassConstants.APBYTE);
}
return AttachmentHelper.buildAttachmentHandler((byte[]) value, mimeType);
}
if (resultType.getNamespaceURI().equals(SCHEMA_URL)) {
// handle primitive types
ValueObject vo = new ValueObject();
vo.value = value;
value = vo;
} else {
Object targetObject = value;
if (xrService.descriptorsByQName.containsKey(resultType)) {
XMLDescriptor xdesc = xrService.descriptorsByQName.get(resultType);
ClassDescriptor desc = xrService.getORSession().getDescriptorForAlias(xdesc.getAlias());
if (desc.isAggregateDescriptor() && !desc.isObjectRelationalDataTypeDescriptor() && !desc.isRelationalDescriptor()) {
if (isCollection()) {
XRDynamicEntity_CollectionWrapper xrCollWrapper = new XRDynamicEntity_CollectionWrapper();
Vector<AbstractRecord> results = (Vector<AbstractRecord>) value;
for (int i = 0, len = results.size(); i < len; i++) {
Object o = desc.getObjectBuilder().buildNewInstance();
populateTargetObjectFromRecord(desc.getMappings(), results.get(i), o, (AbstractSession) xrService.getORSession());
xrCollWrapper.add(o);
}
targetObject = xrCollWrapper;
} else {
targetObject = desc.getObjectBuilder().buildNewInstance();
populateTargetObjectFromRecord(desc.getMappings(), (AbstractRecord) value, targetObject, (AbstractSession) xrService.getORSession());
}
} else if (isCollection() && value instanceof Vector) {
// could be a collection of populated objects, in which case we just return it
if (((Vector<?>) value).size() > 0 && !(((Vector<?>) value).get(0) instanceof AbstractRecord)) {
return value;
}
XRDynamicEntity_CollectionWrapper xrCollWrapper = new XRDynamicEntity_CollectionWrapper();
Vector<AbstractRecord> results = (Vector<AbstractRecord>) value;
for (int i = 0, len = results.size(); i < len; i++) {
Object o = desc.getObjectBuilder().buildNewInstance();
populateTargetObjectFromRecord(desc.getMappings(), results.get(i), o, (AbstractSession) xrService.getORSession());
xrCollWrapper.add(o);
}
targetObject = xrCollWrapper;
} else if (value instanceof AbstractRecord) {
targetObject = desc.getObjectBuilder().buildNewInstance();
populateTargetObjectFromRecord(desc.getMappings(), (AbstractRecord) value, targetObject, (AbstractSession) xrService.getORSession());
}
}
if (value instanceof ArrayList) {
XMLDescriptor xdesc = xrService.descriptorsByQName.get(resultType);
ClassDescriptor desc = xrService.getORSession().getDescriptorForAlias(xdesc.getAlias());
targetObject = desc.getObjectBuilder().buildNewInstance();
Object[] objs = new Object[1];
objs[0] = ((ArrayList<?>) value).get(0);
DatabaseRecord dr = new DatabaseRecord();
dr.add(new DatabaseField(ITEMS_STR), objs);
populateTargetObjectFromRecord(desc.getMappings(), dr, targetObject, (AbstractSession) xrService.getORSession());
}
value = targetObject;
}
}
}
}
return value;
}
use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.
the class ConstructingDescriptorExceptionTests method test.
@Override
public void test() throws NoSuchMethodException {
try {
Class<ConstructingDescriptorExceptionTests> javaClass = ConstructingDescriptorExceptionTests.class;
RelationalDescriptor descriptor = new RelationalDescriptor();
descriptor.setJavaClass(javaClass);
TestErrorException dummyException = new TestErrorException("no ERROR");
DirectToFieldMapping mapping = new DirectToFieldMapping();
ForeignReferenceMapping mappingFR = new OneToOneMapping();
IndirectListContainerPolicy policy = new IndirectListContainerPolicy();
Object object = new Object();
DatabaseRecord row = new DatabaseRecord();
Class<?>[] parmClasses = {};
Method method = javaClass.getDeclaredMethod("test", parmClasses);
DescriptorException descriptorException;
descriptorException = DescriptorException.javaClassNotSpecified(descriptor);
if (descriptorException.getErrorCode() != 39)
testResults.add("DescriptorException.javaClassNotSpecified");
// else testResults.add("Passed");
descriptorException = DescriptorException.writeLockFieldInChildDescriptor(descriptor);
if (descriptorException.getErrorCode() != 109)
testResults.add("DescriptorException.writeLockFieldInChildDescriptor");
// else testResults.add("Passed");
descriptorException = DescriptorException.childDoesNotDefineAbstractQueryKeyOfParent(descriptor, descriptor, "dummy");
if (descriptorException.getErrorCode() != 120)
testResults.add("DescriptorException.childDoesNotDefineAbstractQueryKeyOfParent");
// else testResults.add("Passed");
descriptorException = DescriptorException.missingIndirectContainerConstructor(javaClass);
if (descriptorException.getErrorCode() != 145)
testResults.add("DescriptorException.missingIndirectContainerConstructor");
// else testResults.add("Passed");
descriptorException = DescriptorException.couldNotInstantiateIndirectContainerClass(javaClass, dummyException);
if (descriptorException.getErrorCode() != 146)
testResults.add("DescriptorException.couldNotInstantiateIndirectContainerClass");
// else testResults.add("Passed");
descriptorException = DescriptorException.invalidContainerPolicy(policy, javaClass);
if (descriptorException.getErrorCode() != 147)
testResults.add("DescriptorException.invalidContainerPolicy");
// else testResults.add("Passed");
descriptorException = DescriptorException.invalidUseOfNoIndirection(mapping);
if (descriptorException.getErrorCode() != 149)
testResults.add("DescriptorException.invalidUseOfNoIndirection");
// else testResults.add("Passed");
descriptorException = DescriptorException.proxyIndirectionNotAvailable(mapping);
if (descriptorException.getErrorCode() != 159)
testResults.add("DescriptorException.proxyIndirectionNotAvailable");
// else testResults.add("Passed");
descriptorException = DescriptorException.illegalAccesstWhileGettingValueThruInstanceVaraibleAccessor("attributeName", "objectName", dummyException);
if (descriptorException.getErrorCode() != 13)
testResults.add("DescriptorException.illegalAccesstWhileGettingValueThruInstanceVaraibleAccessor");
// else testResults.add("Passed");
descriptorException = DescriptorException.illegalAccessWhileCloning(object, "methodName", descriptor, dummyException);
if (descriptorException.getErrorCode() != 14)
testResults.add("DescriptorException.illegalAccessWhileCloning");
// else testResults.add("Passed");
descriptorException = DescriptorException.illegalAccessWhileEventExecution("eventMethodName", descriptor, dummyException);
if (descriptorException.getErrorCode() != 16)
testResults.add("DescriptorException.illegalAccessWhileEventExecution");
// else testResults.add("Passed");
descriptorException = DescriptorException.illegalAccessWhileGettingValueThruMethodAccessor("methodName", "objectName", dummyException);
if (descriptorException.getErrorCode() != 17)
testResults.add("DescriptorException.illegalAccessWhileGettingValueThruMethodAccessor");
// else testResults.add("Passed");
descriptorException = DescriptorException.illegalAccessWhileInstantiatingMethodBasedProxy(dummyException);
if (descriptorException.getErrorCode() != 18)
testResults.add("DescriptorException.illegalAccessWhileInstantiatingMethodBasedProxy");
// else testResults.add("Passed");
descriptorException = DescriptorException.illegalAccessWhileInvokingAttributeMethod(mapping, dummyException);
if (descriptorException.getErrorCode() != 19)
testResults.add("DescriptorException.illegalAccessWhileInvokingAttributeMethod");
// else testResults.add("Passed");
descriptorException = DescriptorException.illegalAccessWhileInvokingFieldToMethod("methodName", mapping, dummyException);
if (descriptorException.getErrorCode() != 20)
testResults.add("DescriptorException.illegalAccessWhileInvokingFieldToMethod");
// else testResults.add("Passed");
descriptorException = DescriptorException.illegalAccessWhileInvokingRowExtractionMethod(row, method, descriptor, dummyException);
if (descriptorException.getErrorCode() != 21)
testResults.add("DescriptorException.illegalAccessWhileInvokingRowExtractionMethod");
// else testResults.add("Passed");
descriptorException = DescriptorException.illegalAccessWhileMethodInstantiation("methodName", descriptor, dummyException);
if (descriptorException.getErrorCode() != 22)
testResults.add("DescriptorException.illegalAccessWhileMethodInstantiation");
// else testResults.add("Passed");
descriptorException = DescriptorException.illegalAccessWhileObsoleteEventExecute("eventMethodName", descriptor, dummyException);
if (descriptorException.getErrorCode() != 23)
testResults.add("DescriptorException.illegalAccessWhileObsoleteEventExecute");
// else testResults.add("Passed");
descriptorException = DescriptorException.illegalAccessWhileSettingValueThruInstanceVariableAccessor("attributeName", "objectName", object, dummyException);
if (descriptorException.getErrorCode() != 24)
testResults.add("DescriptorException.illegalAccessWhileSettingValueThruInstanceVariableAccessor");
// else testResults.add("Passed");
descriptorException = DescriptorException.illegalAccessWhileSettingValueThruMethodAccessor("setMethodName", object, dummyException);
if (descriptorException.getErrorCode() != 25)
testResults.add("DescriptorException.illegalAccessWhileSettingValueThruMethodAccessor");
// else testResults.add("Passed");
descriptorException = DescriptorException.missingClassForIndicatorFieldValue(object, descriptor);
if (descriptorException.getErrorCode() != 43)
testResults.add("DescriptorException.missingClassForIndicatorFieldValue");
// else testResults.add("Passed");
descriptorException = DescriptorException.missingClassIndicatorField(row, descriptor);
if (descriptorException.getErrorCode() != 44)
testResults.add("DescriptorException.missingClassIndicatorField");
// else testResults.add("Passed");
descriptorException = DescriptorException.notDeserializable(mapping, dummyException);
if (descriptorException.getErrorCode() != 66)
testResults.add("DescriptorException.notDeserializable");
// else testResults.add("Passed");
descriptorException = DescriptorException.notSerializable(mapping, dummyException);
if (descriptorException.getErrorCode() != 67)
testResults.add("DescriptorException.notSerializable");
// else testResults.add("Passed");
descriptorException = DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor("attributeName", object, dummyException);
if (descriptorException.getErrorCode() != 71)
testResults.add("DescriptorException.nullPointerWhileSettingValueThruInstanceVariableAccessor");
// else testResults.add("Passed");
descriptorException = DescriptorException.nullPointerWhileSettingValueThruMethodAccessor("setMethodName", object, dummyException);
if (descriptorException.getErrorCode() != 72)
testResults.add("DescriptorException.nullPointerWhileSettingValueThruMethodAccessor");
// else testResults.add("Passed");
descriptorException = DescriptorException.securityOnFindMethod("methodName", descriptor, dummyException);
if (descriptorException.getErrorCode() != 82)
testResults.add("DescriptorException.securityOnFindMethod");
// else testResults.add("Passed");
descriptorException = DescriptorException.securityOnFindObsoleteMethod("methodName", descriptor, dummyException);
if (descriptorException.getErrorCode() != 83)
testResults.add("DescriptorException.securityOnFindObsoleteMethod");
// else testResults.add("Passed");
descriptorException = DescriptorException.securityOnInitializingAttributeMethod("attributeMethodName", mapping, dummyException);
if (descriptorException.getErrorCode() != 84)
testResults.add("DescriptorException.securityOnInitializingAttributeMethod");
// else testResults.add("Passed");
descriptorException = DescriptorException.securityWhileConvertingToMethod("methodName", mapping, dummyException);
if (descriptorException.getErrorCode() != 85)
testResults.add("DescriptorException.securityWhileConvertingToMethod");
// else testResults.add("Passed");
descriptorException = DescriptorException.securityWhileInitializingAttributesInInstanceVariableAccessor("attributeName", "javaClassName", dummyException);
if (descriptorException.getErrorCode() != 86)
testResults.add("DescriptorException.securityWhileInitializingAttributesInInstanceVariableAccessor");
// else testResults.add("Passed");
descriptorException = DescriptorException.securityWhileInitializingAttributesInMethodAccessor("setMethodName", "getMethodName", "javaClassName");
if (descriptorException.getErrorCode() != 87)
testResults.add("DescriptorException.securityWhileInitializingAttributesInMethodAccessor");
// else testResults.add("Passed");
descriptorException = DescriptorException.securityWhileInitializingClassExtractionMethod("methodName", descriptor, dummyException);
if (descriptorException.getErrorCode() != 88)
testResults.add("DescriptorException.securityWhileInitializingClassExtractionMethod");
// else testResults.add("Passed");
descriptorException = DescriptorException.securityWhileInitializingCopyPolicy("methodName", descriptor, dummyException);
if (descriptorException.getErrorCode() != 89)
testResults.add("DescriptorException.securityWhileInitializingCopyPolicy");
// else testResults.add("Passed");
descriptorException = DescriptorException.securityWhileInitializingInstantiationPolicy("methodName", descriptor, dummyException);
if (descriptorException.getErrorCode() != 90)
testResults.add("DescriptorException.securityWhileInitializingInstantiationPolicy");
// else testResults.add("Passed");
descriptorException = DescriptorException.sizeMismatchOfForeignKeys(mappingFR);
if (descriptorException.getErrorCode() != 92)
testResults.add("DescriptorException.sizeMismatchOfForeignKeys");
// else testResults.add("Passed");
descriptorException = DescriptorException.targetForeignKeysSizeMismatch(mappingFR);
if (descriptorException.getErrorCode() != 96)
testResults.add("DescriptorException.targetForeignKeysSizeMismatch");
// else testResults.add("Passed");
descriptorException = DescriptorException.multipleTableInsertOrderMismatch(descriptor);
if (descriptorException.getErrorCode() != 143)
testResults.add("DescriptorException.multipleTableInsertOrderMismatch");
// else testResults.add("Passed");
descriptorException = DescriptorException.variableOneToOneMappingIsNotDefinedProperly(mapping, descriptor, "targetKeyName");
if (descriptorException.getErrorCode() != 166)
testResults.add("DescriptorException.variableOneToOneMappingIsNotDefinedProperly");
// else testResults.add("Passed");
descriptorException = DescriptorException.illegalAccessWhileConstructorInstantiationOfFactory(descriptor, dummyException);
if (descriptorException.getErrorCode() != 170)
testResults.add("DescriptorException.illegalAccessWhileConstructorInstantiationOfFactory");
// else testResults.add("Passed");
descriptorException = DescriptorException.noSuchMethodWhileConstructorInstantiationOfFactory(descriptor, dummyException);
if (descriptorException.getErrorCode() != 172)
testResults.add("DescriptorException.noSuchMethodWhileConstructorInstantiationOfFactory");
// else testResults.add("Passed");
descriptorException = DescriptorException.nullPointerWhileConstructorInstantiationOfFactory(descriptor, dummyException);
if (descriptorException.getErrorCode() != 173)
testResults.add("DescriptorException.nullPointerWhileConstructorInstantiationOfFactory");
// else testResults.add("Passed");
descriptorException = DescriptorException.illegalAccessWhileMethodInstantiationOfFactory("methodName", descriptor, dummyException);
if (descriptorException.getErrorCode() != 174)
testResults.add("DescriptorException.illegalAccessWhileMethodInstantiationOfFactory");
// else testResults.add("Passed");
descriptorException = DescriptorException.targetInvocationWhileMethodInstantiationOfFactory("methodName", descriptor, dummyException);
if (descriptorException.getErrorCode() != 175)
testResults.add("DescriptorException.targetInvocationWhileMethodInstantiationOfFactory");
// else testResults.add("Passed");
descriptorException = DescriptorException.nullPointerWhileMethodInstantiationOfFactory("methodName", descriptor, dummyException);
if (descriptorException.getErrorCode() != 176)
testResults.add("DescriptorException.nullPointerWhileMethodInstantiationOfFactory");
// else testResults.add("Passed");
descriptorException = DescriptorException.mappingForAttributeIsMissing("attributeName", descriptor);
if (descriptorException.getErrorCode() != 177)
testResults.add("DescriptorException.mappingForAttributeIsMissing");
// else testResults.add("Passed");
descriptorException = DescriptorException.attributeMappingIsMissingForEntityBean("attributeName", "beanName");
if (descriptorException.getErrorCode() != 178)
testResults.add("DescriptorException.attributeMappingIsMissingForEntityBean");
// else testResults.add("Passed");
} catch (EclipseLinkException exception) {
caughtException = exception;
}
}
use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.
the class IllegalArgumentWhileInstantiatingMethodBasedProxyTest method setup.
@Override
protected void setup() throws NoSuchMethodException {
descriptor = new RelationalDescriptor();
descriptor.setJavaClass(IllegalArgumentWhileInstantiatingMethodBasedProxyTest.class);
descriptor.addTableName("Dummy_Table");
row = new DatabaseRecord();
Class<?>[] parmClasses = { DatabaseRecord.class };
theTransformer = new MethodBasedAttributeTransformer();
theTransformer.setAttributeTransformationMethod(IllegalArgumentWhileInstantiatingMethodBasedProxyTest.class.getDeclaredMethod("invalidMethod", parmClasses));
// theReceiver = new IllegalArgumentWhileInstantiatingMethodBasedProxyTest(); //not correct error
// the following causes the correct error to occur.
// invalid Receiver causes correct error
theReceiver = new DatabaseRecord();
valueHolder = new TransformerBasedValueHolder(theTransformer, theReceiver, row, (AbstractSession) getSession());
expectedException = DescriptorException.illegalArgumentWhileInstantiatingMethodBasedProxy(new Exception());
}
use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.
the class ProjectXMLStoredFunctionCallTest method verify.
@Override
public void verify() {
DatabaseRecord row = (DatabaseRecord) ((Vector) result).firstElement();
Long p_inout = (Long) row.get("P_INOUT");
if (!p_inout.equals(100L)) {
throw new TestErrorException("The stored function did not execute correctly. Expected: [P_INOUT = 100]");
}
Long p_out = (Long) row.get("P_OUT");
if (!p_out.equals(99L)) {
throw new TestErrorException("The stored function did not execute correctly. Expected: [P_OUT = 99]");
}
Long returnValue = (Long) row.getValues().firstElement();
if (!returnValue.equals(99L)) {
throw new TestErrorException("The stored function did not execute correctly. Expected: [return value = 99]");
}
// what the StoredFunctionCall has specified
throw new TestWarningException("The stored function did not convert returned results correctly. Expected: [Long], Actual: [Integer]");
}
use of org.eclipse.persistence.sessions.DatabaseRecord in project eclipselink by eclipse-ee4j.
the class HistoryPolicy method logicalDelete.
/**
* INTERNAL:
* Performs a logical delete (update) on the historical schema.
*/
public void logicalDelete(ModifyQuery writeQuery, boolean isUpdate, boolean isShallow) {
ClassDescriptor descriptor = writeQuery.getDescriptor();
AbstractRecord originalModifyRow = writeQuery.getModifyRow();
AbstractRecord modifyRow = new DatabaseRecord();
StatementQueryMechanism updateMechanism = new StatementQueryMechanism(writeQuery);
Object currentTime = getCurrentTime(writeQuery.getSession());
for (int i = 0; i < getHistoricalTables().size(); i++) {
DatabaseTable table = getHistoricalTables().get(i);
if (isUpdate && !checkWastedVersioning(originalModifyRow, table)) {
continue;
}
SQLUpdateStatement updateStatement = new SQLUpdateStatement();
updateStatement.setTable(table);
Expression whereClause = null;
if (writeQuery instanceof DeleteAllQuery) {
if (writeQuery.getSelectionCriteria() != null) {
whereClause = (Expression) writeQuery.getSelectionCriteria().clone();
}
} else {
whereClause = descriptor.getObjectBuilder().buildPrimaryKeyExpression(table);
}
ExpressionBuilder builder = ((whereClause == null) ? new ExpressionBuilder() : whereClause.getBuilder());
whereClause = builder.getField(getEnd(i)).isNull().and(whereClause);
updateStatement.setWhereClause(whereClause);
modifyRow.add(getEnd(i), currentTime);
// the start field in the logicalInsert.
if (isUpdate) {
if (isShallow) {
// Bug 319276 - increment the timestamp by 1 to avoid unique constraint violation potential
java.sql.Timestamp incrementedTime = (java.sql.Timestamp) currentTime;
incrementedTime.setTime(incrementedTime.getTime() + getMinimumTimeIncrement(writeQuery.getSession()));
originalModifyRow.add(getStart(i), incrementedTime);
} else {
originalModifyRow.add(getStart(i), currentTime);
}
}
updateMechanism.getSQLStatements().add(updateStatement);
}
if (updateMechanism.hasMultipleStatements()) {
writeQuery.setModifyRow(modifyRow);
updateMechanism.updateObject();
writeQuery.setModifyRow(originalModifyRow);
}
}
Aggregations