use of org.eclipse.persistence.internal.xr.NamedQueryHandler in project eclipselink by eclipse-ee4j.
the class JDBCHelper method buildProcedureOperation.
@Override
public void buildProcedureOperation(ProcedureOperationModel procedureOperationModel) {
String name = procedureOperationModel.getName();
boolean isMySQL = dbwsBuilder.getDatabasePlatform().getClass().getName().contains("MySQL");
for (ProcedureType storedProcedure : procedureOperationModel.getDbStoredProcedures()) {
StringBuilder sb = new StringBuilder();
if (name == null || name.length() == 0) {
if (storedProcedure.getOverload() > 0) {
sb.append(storedProcedure.getOverload());
sb.append('_');
}
if (storedProcedure.getCatalogName() != null && storedProcedure.getCatalogName().length() > 0) {
sb.append(storedProcedure.getCatalogName());
sb.append('_');
}
if (storedProcedure.getSchema() != null && storedProcedure.getSchema().length() > 0) {
sb.append(storedProcedure.getSchema());
sb.append('_');
}
sb.append(storedProcedure.getProcedureName());
} else {
sb.append(name);
}
QueryOperation qo = new QueryOperation();
qo.setName(sb.toString());
QueryHandler qh;
if (storedProcedure.isFunctionType()) {
qh = new StoredFunctionQueryHandler();
} else {
qh = new StoredProcedureQueryHandler();
}
sb = new StringBuilder();
if (!isMySQL) {
if (storedProcedure.getCatalogName() != null && storedProcedure.getCatalogName().length() > 0) {
sb.append(storedProcedure.getCatalogName());
sb.append('.');
}
}
if (storedProcedure.getSchema() != null && storedProcedure.getSchema().length() > 0) {
sb.append(storedProcedure.getSchema());
sb.append('.');
}
sb.append(storedProcedure.getProcedureName());
((StoredProcedureQueryHandler) qh).setName(sb.toString());
dbwsBuilder.logMessage(FINEST, "Building QueryOperation for " + sb.toString());
// before assigning queryHandler, check for named query in OR project
List<DatabaseQuery> queries = dbwsBuilder.getOrProject().getQueries();
if (queries.size() > 0) {
for (DatabaseQuery q : queries) {
if (q.getName().equals(qo.getName())) {
qh = new NamedQueryHandler();
((NamedQueryHandler) qh).setName(qo.getName());
}
}
}
qo.setQueryHandler(qh);
String returnType = procedureOperationModel.getReturnType();
boolean isCollection = procedureOperationModel.isCollection();
boolean isSimpleXMLFormat = procedureOperationModel.isSimpleXMLFormat();
Result result = null;
if (storedProcedure.isFunctionType()) {
FunctionType storedFunction = (FunctionType) storedProcedure;
DatabaseType rarg = storedFunction.getReturnArgument();
if (rarg.getTypeName().contains("CURSOR")) {
result = new CollectionResult();
result.setType(SXF_QNAME_CURSOR);
} else {
result = new Result();
int rargJdbcType = Util.getJDBCTypeFromTypeName(rarg.getTypeName());
switch(rargJdbcType) {
case STRUCT:
case ARRAY:
case OTHER:
if (returnType != null) {
result.setType(buildCustomQName(returnType, dbwsBuilder));
} else {
result.setType(ANY_QNAME);
}
break;
default:
result.setType(getXMLTypeFromJDBCType(rargJdbcType));
break;
}
}
} else {
// if user overrides returnType, assume they're right
if (returnType != null) {
result = new Result();
result.setType(buildCustomQName(returnType, dbwsBuilder));
} else {
if (isCollection) {
result = new CollectionResult();
if (isSimpleXMLFormat) {
result.setType(SXF_QNAME_CURSOR);
}
} else {
result = new Result();
result.setType(SXF_QNAME);
}
}
}
if (procedureOperationModel.getBinaryAttachment()) {
Attachment attachment = new Attachment();
attachment.setMimeType("application/octet-stream");
result.setAttachment(attachment);
}
for (ArgumentType arg : storedProcedure.getArguments()) {
String argName = arg.getArgumentName();
if (argName != null) {
ProcedureArgument pa = null;
Parameter parm = null;
ArgumentTypeDirection direction = arg.getDirection();
QName xmlType = null;
switch(Util.getJDBCTypeFromTypeName(arg.getTypeName())) {
case STRUCT:
case ARRAY:
case OTHER:
String typeString = nct.generateSchemaAlias(arg.getTypeName());
xmlType = buildCustomQName(typeString, dbwsBuilder);
break;
default:
xmlType = getXMLTypeFromJDBCType(Util.getJDBCTypeFromTypeName(arg.getTypeName()));
break;
}
if (direction == IN) {
parm = new Parameter();
parm.setName(argName);
parm.setType(xmlType);
pa = new ProcedureArgument();
pa.setName(argName);
pa.setParameterName(argName);
if (qh instanceof StoredProcedureQueryHandler) {
((StoredProcedureQueryHandler) qh).getInArguments().add(pa);
}
} else {
// the first OUT/INOUT arg determines singleResult vs. collectionResult
pa = new ProcedureOutputArgument();
ProcedureOutputArgument pao = (ProcedureOutputArgument) pa;
pao.setName(argName);
pao.setParameterName(argName);
if (arg.getTypeName().contains("CURSOR") && returnType == null) {
// if user overrides returnType, assume they're right
pao.setResultType(SXF_QNAME_CURSOR);
if (result == null) {
result = new CollectionResult();
result.setType(SXF_QNAME_CURSOR);
}
} else {
// Hmm, multiple OUT's gonna be a problem - later!
if (returnType != null && !isSimpleXMLFormat) {
xmlType = qNameFromString("{" + dbwsBuilder.getTargetNamespace() + "}" + returnType, dbwsBuilder.getSchema());
}
pao.setResultType(xmlType);
if (result == null) {
if (isCollection) {
result = new CollectionResult();
} else {
result = new Result();
}
result.setType(xmlType);
}
}
if (direction == INOUT) {
parm = new Parameter();
parm.setName(argName);
parm.setType(xmlType);
result.setType(xmlType);
// use of INOUT precludes SimpleXMLFormat
isSimpleXMLFormat = false;
if (qh instanceof StoredProcedureQueryHandler) {
((StoredProcedureQueryHandler) qh).getInOutArguments().add(pao);
}
} else {
if (qh instanceof StoredProcedureQueryHandler) {
((StoredProcedureQueryHandler) qh).getOutArguments().add(pao);
}
}
}
if (parm != null) {
qo.getParameters().add(parm);
}
}
}
handleSimpleXMLFormat(isSimpleXMLFormat, result, procedureOperationModel);
qo.setResult(result);
dbwsBuilder.getXrServiceModel().getOperations().put(qo.getName(), qo);
}
finishProcedureOperation();
}
use of org.eclipse.persistence.internal.xr.NamedQueryHandler in project eclipselink by eclipse-ee4j.
the class OracleHelper method buildProcedureOperation.
/**
* Builds query operations for a given ProcedureOperationModel.
*/
@Override
public void buildProcedureOperation(ProcedureOperationModel procedureOperationModel) {
for (ProcedureType storedProcedure : procedureOperationModel.getDbStoredProcedures()) {
boolean hasComplexArgs = hasComplexArgs(storedProcedure);
QueryOperation qo = new QueryOperation();
qo.setName(getNameForQueryOperation(procedureOperationModel, storedProcedure));
String qualifiedProcName = getQualifiedProcedureName(procedureOperationModel, storedProcedure);
dbwsBuilder.logMessage(FINEST, BUILDING_QUERYOP_FOR + qualifiedProcName);
QueryHandler qh = null;
// before assigning queryHandler, check for named query in OR project
List<DatabaseQuery> queries = dbwsBuilder.getOrProject().getQueries();
if (queries.size() > 0) {
for (DatabaseQuery q : queries) {
if (q.getName().equals(qo.getName())) {
qh = new NamedQueryHandler();
((NamedQueryHandler) qh).setName(qo.getName());
}
}
}
if (qh == null) {
if (storedProcedure.isFunctionType()) {
qh = new StoredFunctionQueryHandler();
} else {
qh = new StoredProcedureQueryHandler();
}
((StoredProcedureQueryHandler) qh).setName(qualifiedProcName);
}
qo.setQueryHandler(qh);
String returnType = procedureOperationModel.getReturnType();
boolean isCollection = procedureOperationModel.isCollection();
boolean isSimpleXMLFormat = procedureOperationModel.isSimpleXMLFormat();
Result result = null;
/**
* For multiple OUT args as well as a stored function with OUT args, we want
* the result to be a collection and the type to be "xsd:any". We will
* force SimpleXMLFormat for now as well.
*/
int outArgCount = 0;
for (ArgumentType argument : storedProcedure.getArguments()) {
ArgumentTypeDirection argDirection = argument.getDirection();
if (argDirection == OUT) {
outArgCount++;
}
}
if (outArgCount > 1 || (outArgCount > 0 && storedProcedure.isFunctionType())) {
isCollection = true;
isSimpleXMLFormat = true;
result = new CollectionResult();
result.setType(ANY_QNAME);
} else {
if (storedProcedure.isFunctionType()) {
ArgumentType returnArg = ((FunctionType) storedProcedure).getReturnArgument();
result = buildResultForStoredFunction(returnArg, returnType);
// tags to better represent the PL/SQL record/table/column type
if (returnArg.getEnclosedType().isPLSQLCursorType()) {
customizeSimpleXMLTagNames((PLSQLCursorType) returnArg.getEnclosedType(), procedureOperationModel);
}
} else if (hasComplexArgs) {
if (Util.noOutArguments(storedProcedure)) {
result = new Result();
// rowcount
result.setType(new QName(SCHEMA_URL, INT, SCHEMA_PREFIX));
}
} else {
// if user overrides returnType, assume they're right
if (returnType != null) {
result = new Result();
result.setType(buildCustomQName(returnType, dbwsBuilder));
} else {
if (isCollection) {
result = new CollectionResult();
if (isSimpleXMLFormat) {
result.setType(SXF_QNAME_CURSOR);
}
} else {
result = new Result();
result.setType(SXF_QNAME);
}
}
}
}
for (ArgumentType arg : storedProcedure.getArguments()) {
String argName = arg.getArgumentName();
if (argName != null) {
QName xmlType = null;
ProcedureArgument pa = null;
// for INOUT's
ProcedureArgument paShadow = null;
Parameter parm = null;
ArgumentTypeDirection direction = arg.getDirection();
if (!hasComplexArgs) {
if (arg.getEnclosedType().isPLSQLCursorType()) {
PLSQLCursorType cursorType = (PLSQLCursorType) arg.getEnclosedType();
if (cursorType.isWeaklyTyped()) {
xmlType = buildCustomQName("SYS_REFCURSOR", dbwsBuilder);
}
} else {
xmlType = getXMLTypeFromJDBCType(Util.getJDBCTypeFromTypeName(arg.getTypeName()));
}
} else {
// handle PL/SQL records and collections
if (arg.getEnclosedType().isPLSQLType()) {
String packageName = ((PLSQLType) arg.getEnclosedType()).getParentType().getPackageName();
// may need to prepend package name
String typeString = (packageName != null && packageName.length() > 0) ? packageName + UNDERSCORE + arg.getTypeName() : arg.getTypeName();
// may need to strip off %
typeString = typeString.contains(PERCENT) ? typeString.replace(PERCENT, UNDERSCORE) : typeString;
xmlType = buildCustomQName(nct.generateSchemaAlias(typeString), dbwsBuilder);
} else if (arg.getEnclosedType().isVArrayType() || arg.getEnclosedType().isObjectType() || arg.getEnclosedType().isObjectTableType()) {
// handle advanced JDBC types
xmlType = buildCustomQName(nct.generateSchemaAlias(arg.getTypeName()), dbwsBuilder);
} else {
switch(Util.getJDBCTypeFromTypeName(arg.getTypeName())) {
case STRUCT:
case ARRAY:
String typeString = nct.generateSchemaAlias(arg.getTypeName());
xmlType = buildCustomQName(typeString, dbwsBuilder);
break;
default:
xmlType = getXMLTypeFromJDBCType(Util.getJDBCTypeFromTypeName(arg.getTypeName()));
break;
}
}
}
if (direction == null || direction == IN) {
parm = new Parameter();
parm.setName(argName);
parm.setType(xmlType);
// handle optional arg
parm.setOptional(arg.optional());
pa = new ProcedureArgument();
pa.setName(argName);
pa.setParameterName(argName);
if (qh instanceof StoredProcedureQueryHandler) {
((StoredProcedureQueryHandler) qh).getInArguments().add(pa);
}
} else {
// the first OUT/INOUT arg determines singleResult vs. collectionResult
pa = new ProcedureOutputArgument();
ProcedureOutputArgument pao = (ProcedureOutputArgument) pa;
pao.setName(argName);
pao.setParameterName(argName);
boolean isCursor = arg.isPLSQLCursorType() || arg.getTypeName().contains(CURSOR_STR);
// tags to better represent the PL/SQL record/table/column type
if (arg.isPLSQLCursorType()) {
customizeSimpleXMLTagNames((PLSQLCursorType) arg.getEnclosedType(), procedureOperationModel);
}
if (isCursor && returnType == null) {
// if user overrides returnType, assume they're right
pao.setResultType(SXF_QNAME_CURSOR);
if (result == null) {
result = new CollectionResult();
result.setType(SXF_QNAME_CURSOR);
}
} else {
// Hmm, multiple OUT's gonna be a problem - later!
if (returnType != null && !isSimpleXMLFormat) {
xmlType = qNameFromString(OPEN_PAREN + dbwsBuilder.getTargetNamespace() + CLOSE_PAREN + returnType, dbwsBuilder.getSchema());
}
if (isCursor) {
pao.setResultType(new QName(EMPTY_STRING, CURSOR_OF_STR + returnType));
Result newResult = new CollectionResult();
newResult.setType(result.getType());
result = newResult;
} else {
pao.setResultType(xmlType);
}
if (result == null) {
if (isCollection) {
result = new CollectionResult();
} else {
result = new Result();
}
result.setType(xmlType);
}
}
if (direction == INOUT) {
parm = new Parameter();
parm.setName(argName);
parm.setType(xmlType);
result.setType(xmlType);
if (qh instanceof StoredProcedureQueryHandler) {
((StoredProcedureQueryHandler) qh).getInOutArguments().add(pao);
}
paShadow = new ProcedureArgument();
paShadow.setName(argName);
paShadow.setParameterName(argName);
} else {
// OUT arg
if (qh instanceof StoredProcedureQueryHandler) {
((StoredProcedureQueryHandler) qh).getOutArguments().add(pao);
}
}
}
// for XMLType, we want the type code to be 'OPAQUE' (2007)
if (arg.getEnclosedType() == ScalarDatabaseTypeEnum.XMLTYPE_TYPE) {
pa.setJdbcType(getJDBCTypeForTypeName(XMLTYPE_STR));
}
if (hasComplexArgs && arg.getEnclosedType().isPLSQLType()) {
pa.setComplexTypeName(storedProcedure.getCatalogName() + UNDERSCORE + arg.getTypeName());
if (paShadow != null) {
paShadow.setComplexTypeName(pa.getComplexTypeName());
}
}
if (parm != null) {
qo.getParameters().add(parm);
}
}
}
if (procedureOperationModel.getBinaryAttachment()) {
Attachment attachment = new Attachment();
attachment.setMimeType(APP_OCTET_STREAM);
result.setAttachment(attachment);
}
// the user may want simpleXMLFormat
handleSimpleXMLFormat(isSimpleXMLFormat, result, procedureOperationModel);
qo.setResult(result);
dbwsBuilder.getXrServiceModel().getOperations().put(qo.getName(), qo);
}
finishProcedureOperation();
}
use of org.eclipse.persistence.internal.xr.NamedQueryHandler in project eclipselink by eclipse-ee4j.
the class BaseDBWSBuilderHelper method buildDBWSModel.
/**
* Build the DBWS model, a.k.a. service file.
*/
@SuppressWarnings({ "rawtypes" })
public void buildDBWSModel(NamingConventionTransformer nct, OutputStream dbwsServiceStream) {
Project orProject = dbwsBuilder.getOrProject();
if (!isNullStream(dbwsServiceStream)) {
for (Iterator i = orProject.getOrderedDescriptors().iterator(); i.hasNext(); ) {
ClassDescriptor desc = (ClassDescriptor) i.next();
String tablenameAlias = desc.getAlias();
if (dbwsBuilder.requireCRUDOperations.contains(tablenameAlias)) {
String aliasType = tablenameAlias + TYPE_STR;
String tableName = desc.getTableName();
String schemaAlias = nct.generateSchemaAlias(tableName);
Map<String, String> ops;
if (!crudOps.containsKey(tableName)) {
ops = new HashMap<String, String>();
crudOps.put(tableName, ops);
}
ops = crudOps.get(tableName);
String pks = null;
int pkCount = 0;
for (Iterator j = desc.getPrimaryKeyFields().iterator(); j.hasNext(); ) {
DatabaseField field = (DatabaseField) j.next();
if (pkCount++ == 0) {
pks = OPEN_BRACKET + field.getName() + EQUALS_BINDING1_STR;
} else {
pks = pks.concat(AND_STR + field.getName() + EQUALS_BINDING_STR + pkCount++);
}
}
if (pks != null) {
pks = pks.concat(CLOSE_BRACKET);
}
// findByPk
String crudOpName = Util.PK_QUERYNAME + UNDERSCORE + aliasType;
String findByPKName = crudOpName;
QueryOperation findByPKQueryOperation = new QueryOperation();
findByPKQueryOperation.setName(crudOpName);
findByPKQueryOperation.setUserDefined(false);
NamedQueryHandler nqh1 = new NamedQueryHandler();
nqh1.setName(crudOpName);
nqh1.setDescriptor(tablenameAlias);
Result result = new Result();
QName theInstanceType = new QName(dbwsBuilder.getTargetNamespace(), schemaAlias, TARGET_NAMESPACE_PREFIX);
result.setType(theInstanceType);
findByPKQueryOperation.setResult(result);
findByPKQueryOperation.setQueryHandler(nqh1);
for (Iterator j = desc.getPrimaryKeyFields().iterator(); j.hasNext(); ) {
DatabaseField field = (DatabaseField) j.next();
Parameter p = new Parameter();
p.setName(field.getName().toLowerCase());
p.setType(getXMLTypeFromJDBCType(field.getSqlType()));
findByPKQueryOperation.getParameters().add(p);
}
dbwsBuilder.xrServiceModel.getOperations().put(findByPKQueryOperation.getName(), findByPKQueryOperation);
// find all
crudOpName = FINDALL_QUERYNAME + UNDERSCORE + aliasType;
QueryOperation findAllOperation = new QueryOperation();
findAllOperation.setName(crudOpName);
findAllOperation.setUserDefined(false);
NamedQueryHandler nqh2 = new NamedQueryHandler();
nqh2.setName(crudOpName);
nqh2.setDescriptor(tablenameAlias);
Result result2 = new CollectionResult();
result2.setType(theInstanceType);
findAllOperation.setResult(result2);
findAllOperation.setQueryHandler(nqh2);
dbwsBuilder.xrServiceModel.getOperations().put(findAllOperation.getName(), findAllOperation);
// create
crudOpName = CREATE_OPERATION_NAME + UNDERSCORE + aliasType;
InsertOperation insertOperation = new InsertOperation();
insertOperation.setName(crudOpName);
Parameter theInstance = new Parameter();
theInstance.setName(THE_INSTANCE_NAME);
theInstance.setType(theInstanceType);
insertOperation.getParameters().add(theInstance);
dbwsBuilder.xrServiceModel.getOperations().put(insertOperation.getName(), insertOperation);
StringBuilder sqlStmt = new StringBuilder(128);
sqlStmt.append(INSERT_STR).append(tableName).append(SINGLE_SPACE).append(OPEN_BRACKET);
DescriptorHelper.buildColsFromMappings(sqlStmt, desc.getMappings(), COMMA_SPACE_STR);
sqlStmt.append(CLOSE_BRACKET).append(VALUES_STR).append(OPEN_BRACKET);
DescriptorHelper.buildValuesAsQMarksFromMappings(sqlStmt, desc.getMappings(), COMMA_SPACE_STR);
sqlStmt.append(CLOSE_BRACKET);
ops.put(crudOpName, sqlStmt.toString());
// update
crudOpName = UPDATE_OPERATION_NAME + UNDERSCORE + aliasType;
UpdateOperation updateOperation = new UpdateOperation();
updateOperation.setName(crudOpName);
updateOperation.getParameters().add(theInstance);
dbwsBuilder.xrServiceModel.getOperations().put(updateOperation.getName(), updateOperation);
sqlStmt = new StringBuilder(128);
sqlStmt.append(UPDATE_STR).append(tableName).append(SET_STR);
DescriptorHelper.buildColsAndValuesBindingsFromMappings(sqlStmt, desc.getMappings(), desc.getPrimaryKeyFields(), pkCount, EQUALS_BINDING_STR, COMMA_SPACE_STR);
sqlStmt.append(WHERE_STR).append(pks);
ops.put(crudOpName, sqlStmt.toString());
// delete
crudOpName = REMOVE_OPERATION_NAME + UNDERSCORE + aliasType;
DeleteOperation deleteOperation = new DeleteOperation();
deleteOperation.setName(crudOpName);
deleteOperation.setDescriptorName(tablenameAlias);
deleteOperation.setFindByPKQuery(findByPKName);
for (Iterator j = desc.getPrimaryKeyFields().iterator(); j.hasNext(); ) {
DatabaseField field = (DatabaseField) j.next();
Parameter p = new Parameter();
p.setName(field.getName().toLowerCase());
p.setType(getXMLTypeFromJDBCType(field.getSqlType()));
deleteOperation.getParameters().add(p);
}
dbwsBuilder.xrServiceModel.getOperations().put(deleteOperation.getName(), deleteOperation);
sqlStmt = new StringBuilder(128);
sqlStmt.append(DELETE_STR).append(tableName).append(WHERE_STR).append(pks);
ops.put(crudOpName, sqlStmt.toString());
}
}
// check for additional operations
for (OperationModel operation : dbwsBuilder.operations) {
if (operation.isTableOperation()) {
TableOperationModel tableModel = (TableOperationModel) operation;
if (tableModel.additionalOperations != null && tableModel.additionalOperations.size() > 0) {
for (OperationModel additionalOperation : tableModel.additionalOperations) {
if (additionalOperation.hasBuildSql()) {
addToOROXProjectsForBuildSql((ModelWithBuildSql) additionalOperation, orProject, dbwsBuilder.getOxProject(), nct);
} else {
additionalOperation.buildOperation(dbwsBuilder);
}
}
}
} else {
// handle non-nested <sql> and <procedure> operations
operation.buildOperation(dbwsBuilder);
}
}
DBWSModelProject modelProject = new DBWSModelProject();
modelProject.ns.put(TARGET_NAMESPACE_PREFIX, dbwsBuilder.getTargetNamespace());
XMLContext context = new XMLContext(modelProject);
XMLMarshaller marshaller = context.createMarshaller();
marshaller.marshal(dbwsBuilder.xrServiceModel, dbwsServiceStream);
dbwsBuilder.getPackager().closeServiceStream(dbwsServiceStream);
}
}
Aggregations