use of org.eclipse.persistence.platform.database.oracle.plsql.PLSQLargument in project eclipselink by eclipse-ee4j.
the class QueryImpl method applyArguments.
/**
* Define the query arguments based on the procedure call.
*/
protected static void applyArguments(StoredProcedureCall call, DatabaseQuery query) {
if (call instanceof PLSQLStoredProcedureCall) {
PLSQLStoredProcedureCall plsqlCall = (PLSQLStoredProcedureCall) call;
for (int index = 0; index < plsqlCall.getArguments().size(); index++) {
PLSQLargument argument = plsqlCall.getArguments().get(index);
int type = argument.direction;
if ((type == StoredProcedureCall.IN) || (type == StoredProcedureCall.INOUT)) {
if (call.hasOptionalArguments()) {
query.addArgument(argument.name, Object.class, call.getOptionalArguments().contains(new DatabaseField(argument.name)));
} else {
query.addArgument(argument.name);
}
}
}
} else {
for (int index = 0; index < call.getParameters().size(); index++) {
int type = call.getParameterTypes().get(index);
if ((type == StoredProcedureCall.IN) || (type == StoredProcedureCall.INOUT)) {
Object value = call.getParameters().get(index);
DatabaseField parameter = null;
if (value instanceof Object[]) {
parameter = (DatabaseField) ((Object[]) value)[0];
} else {
parameter = (DatabaseField) call.getParameters().get(index);
}
if (call.hasOptionalArguments()) {
query.addArgument(parameter.getName(), Object.class, call.getOptionalArguments().contains(parameter));
} else {
query.addArgument(parameter.getName());
}
}
}
}
}
use of org.eclipse.persistence.platform.database.oracle.plsql.PLSQLargument in project eclipselink by eclipse-ee4j.
the class OracleHelper method buildQueryForProcedureType.
/**
* Build a Query for the given ProcedureType instance and add
* it to the given OR project's list of queries.
*/
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
protected void buildQueryForProcedureType(ProcedureType procType, Project orProject, Project oxProject, ProcedureOperationModel opModel, boolean hasPLSQLArgs) {
// if there are one or more PL/SQL args, then we need a PLSQLStoredProcedureCall
StoredProcedureCall call;
ArgumentType returnArg = procType.isFunctionType() ? ((FunctionType) procType).getReturnArgument() : null;
// check for PL/SQL cursor arg
boolean hasCursor = hasPLSQLCursorArg(getArgumentListForProcedureType(procType));
hasPLSQLArgs = hasPLSQLArgs || hasCursor || opModel.isPLSQLProcedureOperation();
if (hasPLSQLArgs) {
if (procType.isFunctionType()) {
org.eclipse.persistence.internal.helper.DatabaseType dType = buildDatabaseTypeFromMetadataType(returnArg, procType.getCatalogName());
if (hasCursor) {
call = new PLSQLStoredFunctionCall();
// constructor by default adds a RETURN argument, so remove it
((PLSQLStoredFunctionCall) call).getArguments().remove(0);
((PLSQLStoredFunctionCall) call).useNamedCursorOutputAsResultSet(CURSOR_STR, dType);
} else {
Class wrapperClass = getWrapperClass(dType);
if (wrapperClass != null) {
((ComplexDatabaseType) dType).setJavaType(wrapperClass);
}
call = new PLSQLStoredFunctionCall(dType);
// check for non-associative collection
if (returnArg.getEnclosedType().isPLSQLCollectionType() && !((PLSQLCollectionType) returnArg.getEnclosedType()).isIndexed()) {
PLSQLargument plsqlArg = ((PLSQLStoredFunctionCall) call).getArguments().get(0);
((PLSQLCollection) plsqlArg.databaseType).setIsNestedTable(true);
}
}
} else {
call = new PLSQLStoredProcedureCall();
}
} else {
if (procType.isFunctionType()) {
String javaTypeName = returnArg.getTypeName();
ClassDescriptor desc = oxProject.getDescriptorForAlias(getGeneratedAlias(javaTypeName));
if (desc != null) {
javaTypeName = desc.getJavaClassName();
}
if (returnArg.isComposite()) {
DatabaseType dataType = returnArg.getEnclosedType();
if (dataType.isVArrayType() || dataType.isObjectTableType()) {
call = new StoredFunctionCall(Types.ARRAY, returnArg.getTypeName(), javaTypeName, buildFieldForNestedType(dataType));
} else {
// assumes ObjectType
call = new StoredFunctionCall(Types.STRUCT, returnArg.getTypeName(), javaTypeName);
}
} else {
// scalar
call = new StoredFunctionCall();
if (returnArg.getEnclosedType().isBlobType()) {
// handle BLOBs
((StoredFunctionCall) call).setResult(null, ClassConstants.BLOB);
} else {
int resultType = Util.getJDBCTypeFromTypeName(javaTypeName);
// need special handling for Date types
if (resultType == Types.DATE || resultType == Types.TIME || resultType == Types.TIMESTAMP) {
((StoredFunctionCall) call).setResult(null, ClassConstants.TIMESTAMP);
} else if (returnArg.getEnclosedType() == ScalarDatabaseTypeEnum.XMLTYPE_TYPE) {
// special handling for XMLType types
((StoredFunctionCall) call).setResult(getJDBCTypeForTypeName(XMLTYPE_STR), XMLTYPE_STR, ClassConstants.OBJECT);
} else if (resultType == Types.OTHER || resultType == Types.CLOB) {
// default to OBJECT for OTHER, CLOB and LONG types
((StoredFunctionCall) call).setResult(null, ClassConstants.OBJECT);
} else {
((StoredFunctionCall) call).setResult(null, resultType);
}
}
}
} else {
call = new StoredProcedureCall();
}
}
String cat = procType.getCatalogName();
String catalogPrefix = (cat == null || cat.length() == 0) ? EMPTY_STRING : cat + DOT;
call.setProcedureName(catalogPrefix + procType.getProcedureName());
String returnType = opModel.getReturnType();
boolean hasResponse = returnType != null;
DatabaseQuery dq = null;
if (hasCursor || (hasResponse && opModel.isCollection())) {
dq = new DataReadQuery();
} else {
dq = new ValueReadQuery();
}
dq.bindAllParameters();
dq.setName(getNameForQueryOperation(opModel, procType));
dq.setCall(call);
for (ArgumentType arg : procType.getArguments()) {
// handle optional arg
if (arg.optional()) {
call.addOptionalArgument(arg.getArgumentName());
}
DatabaseType argType = arg.getEnclosedType();
ArgumentTypeDirection direction = arg.getDirection();
// for PL/SQL
org.eclipse.persistence.internal.helper.DatabaseType databaseType = null;
// for Advanced JDBC
String javaTypeName = null;
if (hasPLSQLArgs) {
databaseType = buildDatabaseTypeFromMetadataType(argType, cat);
} else {
javaTypeName = argType.getTypeName();
ClassDescriptor desc = oxProject.getDescriptorForAlias(getGeneratedAlias(javaTypeName));
if (desc != null) {
// anything there's a descriptor for will include "packagename." in the class name
javaTypeName = desc.getJavaClassName();
}
}
if (direction == IN) {
if (hasPLSQLArgs) {
Class wrapperClass = getWrapperClass(databaseType);
if (wrapperClass != null) {
((ComplexDatabaseType) databaseType).setJavaType(wrapperClass);
}
((PLSQLStoredProcedureCall) call).addNamedArgument(arg.getArgumentName(), databaseType);
// check for non-associative collection
if (argType.isPLSQLCollectionType() && !((PLSQLCollectionType) argType).isIndexed()) {
PLSQLargument plsqlArg = ((PLSQLStoredProcedureCall) call).getArguments().get(((PLSQLStoredProcedureCall) call).getArguments().size() - 1);
((PLSQLCollection) plsqlArg.databaseType).setIsNestedTable(true);
}
} else {
if (argType.isVArrayType()) {
dq.addArgument(arg.getArgumentName());
call.addNamedArgument(arg.getArgumentName(), arg.getArgumentName(), Types.ARRAY, argType.getTypeName(), javaTypeName);
} else if (argType.isObjectType()) {
dq.addArgument(arg.getArgumentName());
call.addNamedArgument(arg.getArgumentName(), arg.getArgumentName(), Types.STRUCT, argType.getTypeName(), javaTypeName);
} else if (argType.isObjectTableType()) {
dq.addArgument(arg.getArgumentName(), java.sql.Array.class);
call.addNamedArgument(arg.getArgumentName(), arg.getArgumentName(), Types.ARRAY, argType.getTypeName(), getWrapperClass(javaTypeName), buildFieldForNestedType(argType));
} else {
dq.addArgument(arg.getArgumentName());
call.addNamedArgument(arg.getArgumentName(), arg.getArgumentName(), Util.getJDBCTypeFromTypeName(argType.getTypeName()));
}
}
} else if (direction == OUT) {
if (hasPLSQLArgs) {
if (arg.isPLSQLCursorType()) {
((PLSQLStoredProcedureCall) call).useNamedCursorOutputAsResultSet(arg.getArgumentName(), databaseType);
} else {
Class wrapperClass = getWrapperClass(databaseType);
if (wrapperClass != null) {
((ComplexDatabaseType) databaseType).setJavaType(wrapperClass);
}
((PLSQLStoredProcedureCall) call).addNamedOutputArgument(arg.getArgumentName(), databaseType);
}
} else {
if (argType.isComposite()) {
Class wrapperClass = getWrapperClass(javaTypeName);
if (argType.isVArrayType() || argType.isObjectTableType()) {
call.addNamedOutputArgument(arg.getArgumentName(), arg.getArgumentName(), Types.ARRAY, argType.getTypeName(), wrapperClass, buildFieldForNestedType(argType));
} else {
// assumes ObjectType
call.addNamedOutputArgument(arg.getArgumentName(), arg.getArgumentName(), Types.STRUCT, argType.getTypeName(), wrapperClass);
}
} else {
// need special handling for XMLType - we want the type code to be 'OPAQUE' (2007)
if (argType == ScalarDatabaseTypeEnum.XMLTYPE_TYPE) {
call.addNamedOutputArgument(arg.getArgumentName(), arg.getArgumentName(), getJDBCTypeForTypeName(XMLTYPE_STR), XMLTYPE_STR);
} else if (argType == ScalarDatabaseTypeEnum.SYS_REFCURSOR_TYPE) {
call.addNamedCursorOutputArgument(arg.getArgumentName());
} else {
call.addNamedOutputArgument(arg.getArgumentName(), arg.getArgumentName(), Util.getJDBCTypeFromTypeName(argType.getTypeName()));
}
}
}
} else {
// INOUT
if (hasPLSQLArgs) {
Class wrapperClass = getWrapperClass(databaseType);
if (wrapperClass != null) {
((ComplexDatabaseType) databaseType).setJavaType(wrapperClass);
}
((PLSQLStoredProcedureCall) call).addNamedInOutputArgument(arg.getArgumentName(), databaseType);
// check for non-associative collection
if (argType.isPLSQLCollectionType() && !((PLSQLCollectionType) argType).isIndexed()) {
PLSQLargument plsqlArg = ((PLSQLStoredProcedureCall) call).getArguments().get(((PLSQLStoredProcedureCall) call).getArguments().size() - 1);
((PLSQLCollection) plsqlArg.databaseType).setIsNestedTable(true);
}
} else {
dq.addArgument(arg.getArgumentName());
if (argType.isComposite()) {
Class wrapperClass = getWrapperClass(javaTypeName);
if (argType.isVArrayType() || argType.isObjectTableType()) {
call.addNamedInOutputArgument(arg.getArgumentName(), arg.getArgumentName(), arg.getArgumentName(), Types.ARRAY, argType.getTypeName(), wrapperClass, buildFieldForNestedType(argType));
} else {
// assumes ObjectType
call.addNamedInOutputArgument(arg.getArgumentName(), arg.getArgumentName(), arg.getArgumentName(), Types.STRUCT, argType.getTypeName());
}
} else {
// for some reason setting "java.lang.String" as the java type causes problems at runtime
Class javaType = getClassFromJDBCType(argType.getTypeName(), dbwsBuilder.getDatabasePlatform());
if (shouldSetJavaType(javaType.getName())) {
call.addNamedInOutputArgument(arg.getArgumentName(), arg.getArgumentName(), arg.getArgumentName(), Util.getJDBCTypeFromTypeName(argType.getTypeName()), argType.getTypeName(), javaType);
} else {
call.addNamedInOutputArgument(arg.getArgumentName());
}
}
}
}
if (hasPLSQLArgs && (direction == IN || direction == INOUT)) {
ClassDescriptor xdesc = null;
if (hasResponse) {
int idx = returnType.indexOf(COLON);
if (idx == -1) {
idx = returnType.indexOf(CLOSE_PAREN);
}
if (idx > 0) {
String typ = returnType.substring(idx + 1);
for (XMLDescriptor xd : (List<XMLDescriptor>) (List) oxProject.getOrderedDescriptors()) {
if (xd.getSchemaReference() != null) {
String context = xd.getSchemaReference().getSchemaContext();
if (context.substring(1).equals(typ)) {
xdesc = xd;
break;
}
}
}
}
}
if (xdesc != null) {
dq.addArgumentByTypeName(arg.getArgumentName(), xdesc.getJavaClassName());
} else {
if (databaseType instanceof PLSQLCollection || databaseType instanceof VArrayType) {
dq.addArgument(arg.getArgumentName(), Array.class);
} else if (databaseType instanceof PLSQLrecord || databaseType instanceof OracleObjectType) {
dq.addArgument(arg.getArgumentName(), Struct.class);
} else {
dq.addArgument(arg.getArgumentName(), JDBCTypes.getClassForCode(databaseType.getConversionCode()));
}
}
}
}
orProject.getQueries().add(dq);
}
use of org.eclipse.persistence.platform.database.oracle.plsql.PLSQLargument in project eclipselink by eclipse-ee4j.
the class ObjectPersistenceRuntimeXMLProject_11_1_1 method buildPLSQLargumentDescriptor.
protected ClassDescriptor buildPLSQLargumentDescriptor() {
XMLDescriptor descriptor = new XMLDescriptor();
descriptor.setJavaClass(PLSQLargument.class);
XMLDirectMapping nameMapping = new XMLDirectMapping();
nameMapping.setAttributeName("name");
nameMapping.setXPath(getPrimaryNamespaceXPath() + "name/text()");
descriptor.addMapping(nameMapping);
XMLDirectMapping indexMapping = new XMLDirectMapping();
indexMapping.setAttributeName("originalIndex");
indexMapping.setXPath(getPrimaryNamespaceXPath() + "index/text()");
indexMapping.setNullValue(-1);
descriptor.addMapping(indexMapping);
XMLDirectMapping directionMapping = new XMLDirectMapping();
directionMapping.setAttributeName("direction");
directionMapping.setXPath(getPrimaryNamespaceXPath() + "direction/text()");
ObjectTypeConverter directionConverter = new ObjectTypeConverter();
directionConverter.addConversionValue("IN", IN);
directionConverter.addConversionValue("INOUT", INOUT);
directionConverter.addConversionValue("OUT", OUT);
directionMapping.setConverter(directionConverter);
directionMapping.setNullValue(IN);
descriptor.addMapping(directionMapping);
XMLDirectMapping lengthMapping = new XMLDirectMapping();
lengthMapping.setAttributeName("length");
lengthMapping.setXPath(getPrimaryNamespaceXPath() + "length/text()");
lengthMapping.setNullValue(255);
descriptor.addMapping(lengthMapping);
XMLDirectMapping precisionMapping = new XMLDirectMapping();
precisionMapping.setAttributeName("precision");
precisionMapping.setXPath(getPrimaryNamespaceXPath() + "precision/text()");
precisionMapping.setNullValue(MIN_VALUE);
descriptor.addMapping(precisionMapping);
XMLDirectMapping scaleMapping = new XMLDirectMapping();
scaleMapping.setAttributeName("scale");
scaleMapping.setXPath(getPrimaryNamespaceXPath() + "scale/text()");
scaleMapping.setNullValue(MIN_VALUE);
descriptor.addMapping(scaleMapping);
XMLDirectMapping cursorOutputMapping = new XMLDirectMapping();
cursorOutputMapping.setAttributeName("cursorOutput");
cursorOutputMapping.setXPath("@cursorOutput");
cursorOutputMapping.setNullValue(Boolean.FALSE);
descriptor.addMapping(cursorOutputMapping);
XMLCompositeObjectMapping databaseTypeMapping = new XMLCompositeObjectMapping();
databaseTypeMapping.setAttributeName("databaseTypeWrapper");
databaseTypeMapping.setAttributeAccessor(new AttributeAccessor() {
@Override
public Object getAttributeValueFromObject(Object object) {
PLSQLargument argument = (PLSQLargument) object;
DatabaseType type = argument.databaseType;
return wrapType(type);
}
@Override
public void setAttributeValueInObject(Object object, Object value) {
PLSQLargument argument = (PLSQLargument) object;
DatabaseTypeWrapper type = (DatabaseTypeWrapper) value;
argument.databaseType = type.getWrappedType();
}
});
databaseTypeMapping.setReferenceClass(DatabaseTypeWrapper.class);
databaseTypeMapping.setXPath(".");
descriptor.addMapping(databaseTypeMapping);
return descriptor;
}
use of org.eclipse.persistence.platform.database.oracle.plsql.PLSQLargument in project eclipselink by eclipse-ee4j.
the class PLSQLTestSuite method testOracleTypeProcessing.
/**
* Test processing of OracleObject and OracleArray annotations.
*
* @see OracleArray
* @see org.eclipse.persistence.platform.database.oracle.annotations.OracleObject
*/
public void testOracleTypeProcessing() {
if (!getServerSession().getPlatform().isOracle()) {
return;
}
EntityManager em = createEntityManager();
beginTransaction(em);
try {
Query query = em.createNamedQuery("TEST_ORACLE_TYPES");
assertNotNull("EntityManager could not create query [TEST_ORACLE_TYPES]", query);
assertTrue("Expected EJBQueryImpl but was [" + query.getClass().getName() + "]", query instanceof EJBQueryImpl);
DatabaseCall call = ((EJBQueryImpl) query).getDatabaseQuery().getCall();
assertNotNull("The DatabaseCall was not set on the query", call);
assertTrue("Expected PLSQLStoredProcedureCall but was [" + call.getClass().getName() + "]", call instanceof PLSQLStoredProcedureCall);
PLSQLStoredProcedureCall plsqlCall = (PLSQLStoredProcedureCall) call;
List<PLSQLargument> args = plsqlCall.getArguments();
assertTrue("Expected 2 arguments, but was [" + args.size() + "]", args.size() == 2);
boolean foundINArg = false;
boolean foundOUTArg = false;
for (PLSQLargument arg : args) {
if (arg.name.equals("P_IN")) {
foundINArg = true;
assertNotNull("databaseType for arg P_IN is null", arg.databaseType);
assertTrue("Expected arg P_IN to be an OracleArrayType, but was [" + arg.databaseType.getClass().getName() + "]", arg.databaseType instanceof OracleArrayType);
OracleArrayType arrayType = (OracleArrayType) arg.databaseType;
assertTrue("Expected arg P_IN to have databaseType set with type name VARRAY_NUMERO_UNO, but was [" + arrayType.getTypeName() + "]", arrayType.getTypeName().equals("VARRAY_NUMERO_UNO"));
assertNotNull("Expected VARRAY_NUMERO_UNO to have nested type VARCHAR, but was null", arrayType.getNestedType());
assertTrue("Expected VARRAY_NUMERO_UNO to have nested type VARCHAR, but was [" + arrayType.getNestedType().getTypeName() + "]", arrayType.getNestedType().getTypeName().equals("VARCHAR"));
} else if (arg.name.equals("P_OUT")) {
foundOUTArg = true;
assertNotNull("databaseType for arg P_OUT is null", arg.databaseType);
assertTrue("Expected arg P_OUT to be an OracleObjectType, but was [" + arg.databaseType.getClass().getName() + "]", arg.databaseType instanceof OracleObjectType);
OracleObjectType objectType = (OracleObjectType) arg.databaseType;
assertTrue("Expected arg P_OUT to have databaseType set with type name OBJECT_NUMERO_DOS, but was [" + objectType.getTypeName() + "]", objectType.getTypeName().equals("OBJECT_NUMERO_DOS"));
assertTrue("Expected OBJECT_NUMERO_DOS to have 2 fields, but was [" + objectType.getFields().size() + "]", objectType.getFields().size() == 2);
for (String key : objectType.getFields().keySet()) {
DatabaseType dbType = objectType.getFields().get(key);
if (key.equals("OO_FLD1")) {
assertTrue("Expected field OO_FLD1 to have databaseType NUMERIC, but was [" + dbType.getTypeName() + "]", dbType.getTypeName().equals("NUMERIC"));
} else if (key.equals("OO_FLD2")) {
assertTrue("Expected field OO_FLD2 to have databaseType NUMERIC, but was [" + dbType.getTypeName() + "]", dbType.getTypeName().equals("NUMERIC"));
} else {
fail("Expected OBJECT_NUMERO_DOS to have fields OO_FLD1 and OO_FLD2 but encountered field [" + key + "]");
}
}
} else {
fail("Expected arg name to be one of P_IN or P_OUT, but was [" + arg.name + "]");
}
}
assertTrue("IN arg P_IN was not processed", foundINArg);
assertTrue("OUT arg P_OUT was not processed", foundOUTArg);
} finally {
closeEntityManagerAndTransaction(em);
}
}
use of org.eclipse.persistence.platform.database.oracle.plsql.PLSQLargument in project eclipselink by eclipse-ee4j.
the class XmlEntityMappingsGenerator method generateXmlEntityMappings.
/**
* Generate an XMLEntityMappings instance based on a given OR Project's Queries and Descriptors.
*
* @param orProject the ORM Project instance containing Queries and Descriptors to be used to generate an XMLEntityMappings
* @param complexTypes list of composite database types used to generate metadata for advanced Oracle and PL/SQL types
* @param crudOperations map of maps keyed on table name - the second map are operation name to SQL string entries
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
public static XMLEntityMappings generateXmlEntityMappings(Project orProject, List<CompositeDatabaseType> complexTypes, Map<String, Map<String, String>> crudOperations) {
List<ClassDescriptor> descriptors = orProject.getOrderedDescriptors();
List<DatabaseQuery> queries = orProject.getQueries();
XMLEntityMappings xmlEntityMappings = new XMLEntityMappings();
xmlEntityMappings.setEmbeddables(new ArrayList<EmbeddableAccessor>());
xmlEntityMappings.setEntities(new ArrayList<EntityAccessor>());
xmlEntityMappings.setPLSQLRecords(new ArrayList<PLSQLRecordMetadata>());
xmlEntityMappings.setPLSQLTables(new ArrayList<PLSQLTableMetadata>());
xmlEntityMappings.setOracleObjectTypes(new ArrayList<OracleObjectTypeMetadata>());
xmlEntityMappings.setOracleArrayTypes(new ArrayList<OracleArrayTypeMetadata>());
// process PL/SQL records and collections, and Oracle advanced JDBC types
List<PLSQLRecordMetadata> plsqlRecords = null;
List<PLSQLTableMetadata> plsqlTables = null;
List<OracleObjectTypeMetadata> objectTypes = null;
List<OracleArrayTypeMetadata> arrayTypes = null;
// generate metadata for each of the composite types
List<ComplexTypeMetadata> complexTypeMetadata = processCompositeTypes(complexTypes, orProject);
for (ComplexTypeMetadata cTypeMetadata : complexTypeMetadata) {
if (cTypeMetadata.isOracleComplexTypeMetadata()) {
OracleComplexTypeMetadata octMetadata = (OracleComplexTypeMetadata) cTypeMetadata;
if (octMetadata.isOracleArrayTypeMetadata()) {
if (arrayTypes == null) {
arrayTypes = new ArrayList<OracleArrayTypeMetadata>();
}
arrayTypes.add((OracleArrayTypeMetadata) octMetadata);
} else {
// assumes OracleObjectTypeMetadata
if (objectTypes == null) {
objectTypes = new ArrayList<OracleObjectTypeMetadata>();
}
objectTypes.add((OracleObjectTypeMetadata) octMetadata);
}
} else {
// assumes PL/SQL complex type metadata
PLSQLComplexTypeMetadata plsqlctMetadata = (PLSQLComplexTypeMetadata) cTypeMetadata;
if (plsqlctMetadata.isPLSQLRecordMetadata()) {
if (plsqlRecords == null) {
plsqlRecords = new ArrayList<PLSQLRecordMetadata>();
}
plsqlRecords.add((PLSQLRecordMetadata) plsqlctMetadata);
} else {
// assumes PLSQLTableMetadata
if (plsqlTables == null) {
plsqlTables = new ArrayList<PLSQLTableMetadata>();
}
plsqlTables.add((PLSQLTableMetadata) plsqlctMetadata);
}
}
}
// add generated metadata
xmlEntityMappings.setPLSQLRecords(plsqlRecords);
xmlEntityMappings.setPLSQLTables(plsqlTables);
xmlEntityMappings.setOracleObjectTypes(objectTypes);
xmlEntityMappings.setOracleArrayTypes(arrayTypes);
// process stored procedures/functions
List<NamedPLSQLStoredProcedureQueryMetadata> plsqlStoredProcs = null;
List<NamedPLSQLStoredFunctionQueryMetadata> plsqlStoredFuncs = null;
List<NamedStoredProcedureQueryMetadata> storedProcs = null;
List<NamedStoredFunctionQueryMetadata> storedFuncs = null;
List<NamedNativeQueryMetadata> namedNativeQueries = null;
// process database queries set on the descriptor
for (DatabaseQuery query : queries) {
if (query.getCall().isStoredFunctionCall()) {
if (query.getCall() instanceof PLSQLStoredFunctionCall) {
PLSQLStoredFunctionCall call = (PLSQLStoredFunctionCall) query.getCall();
NamedPLSQLStoredFunctionQueryMetadata metadata = new NamedPLSQLStoredFunctionQueryMetadata();
metadata.setName(query.getName());
metadata.setProcedureName(call.getProcedureName());
List<PLSQLParameterMetadata> params = new ArrayList<PLSQLParameterMetadata>();
if (plsqlStoredFuncs == null) {
plsqlStoredFuncs = new ArrayList<NamedPLSQLStoredFunctionQueryMetadata>();
}
PLSQLargument arg;
PLSQLParameterMetadata param;
List<PLSQLargument> types = call.getArguments();
for (int i = 0; i < types.size(); i++) {
arg = types.get(i);
param = new PLSQLParameterMetadata();
param.setName(arg.name);
String dbType = arg.databaseType.getTypeName();
if (arg.databaseType == XMLType) {
dbType = XMLType.name();
} else if (arg.databaseType == PLSQLBoolean) {
dbType = PLSQLBoolean.name();
} else {
if (!(getJDBCTypeFromTypeName(dbType) == Types.OTHER)) {
dbType = dbType.concat(_TYPE_STR);
}
}
param.setDatabaseType(dbType);
if (i == 0) {
// first arg is the return arg
metadata.setReturnParameter(param);
if (arg.cursorOutput) {
param.setDirection(CURSOR_STR);
}
} else {
param.setDirection(getDirectionAsString(arg.direction));
params.add(param);
}
}
if (params.size() > 0) {
metadata.setParameters(params);
}
plsqlStoredFuncs.add(metadata);
} else {
StoredFunctionCall call = (StoredFunctionCall) query.getCall();
NamedStoredFunctionQueryMetadata metadata = new NamedStoredFunctionQueryMetadata();
metadata.setName(query.getName());
metadata.setProcedureName(call.getProcedureName());
List<StoredProcedureParameterMetadata> params = new ArrayList<StoredProcedureParameterMetadata>();
if (storedFuncs == null) {
storedFuncs = new ArrayList<NamedStoredFunctionQueryMetadata>();
}
DatabaseField arg;
StoredProcedureParameterMetadata param;
List<DatabaseField> paramFields = call.getParameters();
List<Integer> types = call.getParameterTypes();
for (int i = 0; i < paramFields.size(); i++) {
arg = paramFields.get(i);
param = new StoredProcedureParameterMetadata();
param.setTypeName(arg.getTypeName());
if (arg.getSqlType() != DatabaseField.NULL_SQL_TYPE) {
param.setJdbcType(arg.getSqlType());
}
if (arg.isObjectRelationalDatabaseField()) {
param.setJdbcTypeName(((ObjectRelationalDatabaseField) arg).getSqlTypeName());
}
if (i == 0) {
// first arg is the return arg
metadata.setReturnParameter(param);
// handle CURSOR types - want name/value pairs returned
if (types.get(i) == 8) {
addQueryHint(metadata);
}
} else {
param.setName(arg.getName());
param.setMode(getParameterModeAsString(types.get(i)));
params.add(param);
}
}
if (params.size() > 0) {
metadata.setParameters(params);
}
storedFuncs.add(metadata);
}
} else if (query.getCall().isStoredProcedureCall()) {
if (query.getCall() instanceof PLSQLStoredProcedureCall) {
PLSQLStoredProcedureCall call = (PLSQLStoredProcedureCall) query.getCall();
if (plsqlStoredProcs == null) {
plsqlStoredProcs = new ArrayList<NamedPLSQLStoredProcedureQueryMetadata>();
}
NamedPLSQLStoredProcedureQueryMetadata metadata = new NamedPLSQLStoredProcedureQueryMetadata();
metadata.setName(query.getName());
metadata.setProcedureName(call.getProcedureName());
PLSQLParameterMetadata param;
List<PLSQLParameterMetadata> params = new ArrayList<PLSQLParameterMetadata>();
List<PLSQLargument> types = call.getArguments();
for (PLSQLargument arg : types) {
param = new PLSQLParameterMetadata();
param.setName(arg.name);
String dbType = processTypeName(arg.databaseType.getTypeName());
if (arg.cursorOutput) {
param.setDirection(CURSOR_STR);
} else {
param.setDirection(getDirectionAsString(arg.direction));
}
if (arg.databaseType == XMLType) {
param.setDatabaseType(XMLType.name());
} else if (arg.databaseType == PLSQLBoolean) {
param.setDatabaseType(PLSQLBoolean.name());
} else {
param.setDatabaseType(dbType);
}
params.add(param);
}
if (params.size() > 0) {
metadata.setParameters(params);
}
plsqlStoredProcs.add(metadata);
} else {
StoredProcedureCall call = (StoredProcedureCall) query.getCall();
NamedStoredProcedureQueryMetadata metadata = new NamedStoredProcedureQueryMetadata();
metadata.setName(query.getName());
metadata.setProcedureName(call.getProcedureName());
metadata.setReturnsResultSet(false);
List<StoredProcedureParameterMetadata> params = new ArrayList<StoredProcedureParameterMetadata>();
DatabaseField arg;
StoredProcedureParameterMetadata param;
List paramFields = call.getParameters();
List<Integer> types = call.getParameterTypes();
for (int i = 0; i < paramFields.size(); i++) {
if (types.get(i) == DatabaseCall.INOUT) {
// for INOUT we get Object[IN, OUT]
arg = (DatabaseField) ((Object[]) paramFields.get(i))[1];
} else {
arg = (DatabaseField) paramFields.get(i);
}
param = new StoredProcedureParameterMetadata();
param.setName(arg.getName());
param.setTypeName(arg.getTypeName());
if (arg.getSqlType() != DatabaseField.NULL_SQL_TYPE) {
param.setJdbcType(arg.getSqlType());
}
if (arg.isObjectRelationalDatabaseField()) {
param.setJdbcTypeName(((ObjectRelationalDatabaseField) arg).getSqlTypeName());
}
param.setMode(getParameterModeAsString(types.get(i)));
// handle CURSOR types - want name/value pairs returned
if (types.get(i) == 8) {
addQueryHint(metadata);
}
params.add(param);
}
if (params.size() > 0) {
metadata.setParameters(params);
}
if (storedProcs == null) {
storedProcs = new ArrayList<NamedStoredProcedureQueryMetadata>();
}
storedProcs.add(metadata);
}
} else {
// named native query
NamedNativeQueryMetadata namedQuery = new NamedNativeQueryMetadata();
namedQuery.setName(query.getName());
namedQuery.setQuery(query.getSQLString());
namedQuery.setResultClassName(query.getReferenceClassName());
if (namedNativeQueries == null) {
namedNativeQueries = new ArrayList<NamedNativeQueryMetadata>();
}
namedNativeQueries.add(namedQuery);
}
}
// add generated metadata
if (plsqlStoredProcs != null) {
xmlEntityMappings.setNamedPLSQLStoredProcedureQueries(plsqlStoredProcs);
}
if (plsqlStoredFuncs != null) {
xmlEntityMappings.setNamedPLSQLStoredFunctionQueries(plsqlStoredFuncs);
}
if (storedProcs != null) {
xmlEntityMappings.setNamedStoredProcedureQueries(storedProcs);
}
if (storedFuncs != null) {
xmlEntityMappings.setNamedStoredFunctionQueries(storedFuncs);
}
if (namedNativeQueries != null) {
xmlEntityMappings.setNamedNativeQueries(namedNativeQueries);
}
// generate a ClassAccessor for each Descriptor, keeping track of Embeddables
List<String> embeddables = new ArrayList<String>();
Map<String, ClassAccessor> accessors = new HashMap<String, ClassAccessor>();
for (ClassDescriptor cdesc : descriptors) {
boolean embeddable = false;
ClassAccessor classAccessor;
if (cdesc.isAggregateDescriptor()) {
embeddable = true;
classAccessor = new EmbeddableAccessor();
embeddables.add(cdesc.getJavaClassName());
} else {
classAccessor = new EntityAccessor();
}
classAccessor.setClassName(cdesc.getJavaClassName());
classAccessor.setAccess(EL_ACCESS_VIRTUAL);
// may need add STRUCT metadata to the classAccessor to ensure correct field ordering
if (cdesc.isObjectRelationalDataTypeDescriptor()) {
ObjectRelationalDataTypeDescriptor odesc = (ObjectRelationalDataTypeDescriptor) cdesc;
if (odesc.getOrderedFields().size() > 0) {
StructMetadata struct = new StructMetadata();
struct.setName(odesc.getStructureName());
struct.setFields(odesc.getOrderedFields());
classAccessor.setStruct(struct);
}
}
if (!embeddable && cdesc.getTableName() != null) {
TableMetadata table = new TableMetadata();
table.setName(cdesc.getTableName());
((EntityAccessor) classAccessor).setTable(table);
}
if (!embeddable) {
List<NamedNativeQueryMetadata> namedNatQueries = new ArrayList<NamedNativeQueryMetadata>();
NamedNativeQueryMetadata namedQuery;
DatabaseQuery dbQuery;
// process findAll and findByPk queries
for (Iterator<DatabaseQuery> queryIt = cdesc.getQueryManager().getAllQueries().iterator(); queryIt.hasNext(); ) {
dbQuery = queryIt.next();
namedQuery = new NamedNativeQueryMetadata();
namedQuery.setName(dbQuery.getName());
namedQuery.setQuery(dbQuery.getSQLString());
namedQuery.setResultClassName(dbQuery.getReferenceClassName());
namedNatQueries.add(namedQuery);
}
// now create/update/delete operations
Map<String, String> crudOps = crudOperations.get(cdesc.getTableName());
if (!crudOps.isEmpty()) {
for (String opName : crudOps.keySet()) {
String crudSql = crudOps.get(opName);
NamedNativeQueryMetadata crudQuery = new NamedNativeQueryMetadata();
crudQuery.setName(opName);
crudQuery.setQuery(crudSql);
namedNatQueries.add(crudQuery);
}
}
if (namedNatQueries.size() > 0) {
((EntityAccessor) classAccessor).setNamedNativeQueries(namedNatQueries);
}
}
classAccessor.setAttributes(new XMLAttributes());
classAccessor.getAttributes().setIds(new ArrayList<IdAccessor>());
classAccessor.getAttributes().setBasics(new ArrayList<BasicAccessor>());
classAccessor.getAttributes().setArrays(new ArrayList<ArrayAccessor>());
classAccessor.getAttributes().setStructures(new ArrayList<StructureAccessor>());
classAccessor.getAttributes().setEmbeddeds(new ArrayList<EmbeddedAccessor>());
if (embeddable) {
xmlEntityMappings.getEmbeddables().add((EmbeddableAccessor) classAccessor);
} else {
xmlEntityMappings.getEntities().add((EntityAccessor) classAccessor);
}
accessors.put(cdesc.getJavaClassName(), classAccessor);
}
// now the we know what the embeddables are, we can process mappings
for (ClassDescriptor cdesc : descriptors) {
ClassAccessor classAccessor = accessors.get(cdesc.getJavaClassName());
MappingAccessor mapAccessor;
// generate a MappingAccessor for each mapping
for (DatabaseMapping dbMapping : cdesc.getMappings()) {
mapAccessor = generateMappingAccessor(dbMapping, embeddables);
if (mapAccessor == null) {
continue;
}
if (mapAccessor.isId()) {
classAccessor.getAttributes().getIds().add((IdAccessor) mapAccessor);
} else if (mapAccessor.isBasic()) {
classAccessor.getAttributes().getBasics().add((BasicAccessor) mapAccessor);
} else if (mapAccessor instanceof ArrayAccessor) {
classAccessor.getAttributes().getArrays().add((ArrayAccessor) mapAccessor);
} else if (mapAccessor instanceof StructureAccessor) {
classAccessor.getAttributes().getStructures().add((StructureAccessor) mapAccessor);
} else {
// assumes EmbeddedAccessor
classAccessor.getAttributes().getEmbeddeds().add((EmbeddedAccessor) mapAccessor);
}
}
}
return xmlEntityMappings;
}
Aggregations