1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
|
/*
* Copyright (c) 1998, 2019 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019 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
* http://www.eclipse.org/legal/epl-2.0,
* or the Eclipse Distribution License v. 1.0 which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
*/
// Contributors:
// Oracle - initial API and implementation from Oracle TopLink
// Markus KARG - Added methods allowing to support stored procedure creation on SQLAnywherePlatform.
package org.eclipse.persistence.tools.schemaframework;
import java.io.IOException;
import java.io.Writer;
import java.util.Enumeration;
import java.util.Vector;
import org.eclipse.persistence.exceptions.ValidationException;
import org.eclipse.persistence.internal.databaseaccess.DatabasePlatform;
import org.eclipse.persistence.internal.databaseaccess.FieldTypeDefinition;
import org.eclipse.persistence.internal.sessions.AbstractSession;
/**
* <b>Purpose</b>: Allow a semi-generic way of creating stored procedures.
*/
public class StoredProcedureDefinition extends DatabaseObjectDefinition {
protected Vector variables;
protected Vector statements;
protected Vector arguments;
protected Vector argumentTypes;
protected static final Integer IN = Integer.valueOf(1);
protected static final Integer OUT = Integer.valueOf(2);
protected static final Integer INOUT = Integer.valueOf(3);
public StoredProcedureDefinition() {
this.statements = new Vector();
this.variables = new Vector();
this.arguments = new Vector();
this.argumentTypes = new Vector();
}
/**
* The arguments are the names of the parameters to the procedure.
*/
public void addArgument(String argumentName, Class type) {
addArgument(new FieldDefinition(argumentName, type));
}
/**
* The arguments are the names of the parameters to the procedure.
*/
public void addArgument(String argumentName, Class type, int size) {
addArgument(new FieldDefinition(argumentName, type, size));
}
/**
* The arguments are the names of the parameters to the procedure.
*/
public void addArgument(String argumentName, String typeName) {
addArgument(new FieldDefinition(argumentName, typeName));
}
/**
* The arguments are the names of the parameters to the procedure.
*/
public void addArgument(FieldDefinition argument) {
getArguments().addElement(argument);
getArgumentTypes().addElement(IN);
}
/**
* The output arguments are used to get values back from the proc.
*/
public void addInOutputArgument(String argumentName, Class type) {
addInOutputArgument(new FieldDefinition(argumentName, type));
}
/**
* The output arguments are used to get values back from the proc, such as cursors.
*/
public void addInOutputArgument(FieldDefinition argument) {
getArguments().addElement(argument);
getArgumentTypes().addElement(INOUT);
}
/**
* The output arguments are used to get values back from the proc.
*/
public void addOutputArgument(String argumentName, Class type) {
addOutputArgument(new FieldDefinition(argumentName, type));
}
/**
* The output arguments are used to get values back from the proc.
*/
public void addOutputArgument(String argumentName, Class type, int size) {
addOutputArgument(new FieldDefinition(argumentName, type, size));
}
/**
* The output arguments are used to get values back from the proc, such as cursors.
*/
public void addOutputArgument(String argumentName, String typeName) {
addOutputArgument(new FieldDefinition(argumentName, typeName));
}
/**
* The output arguments are used to get values back from the proc, such as cursors.
*/
public void addOutputArgument(FieldDefinition argument) {
getArguments().addElement(argument);
getArgumentTypes().addElement(OUT);
}
/**
* The statements are the SQL lines of code in procedure.
*/
public void addStatement(String statement) {
getStatements().addElement(statement);
}
/**
* The variables are the names of the declared variables used in the procedure.
*/
public void addVariable(String variableName, String typeName) {
this.addVariable(new FieldDefinition(variableName, typeName));
}
/**
* The variables are the names of the declared variables used in the procedure.
*/
public void addVariable(FieldDefinition variable) {
getVariables().addElement(variable);
}
/**
* INTERNAL: Return the create table statement.
*/
public Writer buildCreationWriter(AbstractSession session, Writer writer) throws ValidationException {
try {
DatabasePlatform platform = session.getPlatform();
writer.write(getCreationHeader() + getFullName());
if (getArguments().size() > getFirstArgumentIndex() || platform.requiresProcedureBrackets()) {
writer.write(" (");
}
writer.write("\n");
for (int i = getFirstArgumentIndex(); i < getArguments().size(); i++) {
writer.write("\t");
FieldDefinition argument = (FieldDefinition)getArguments().elementAt(i);
Integer argumentType = (Integer)getArgumentTypes().elementAt(i);
if (argumentType == IN) {
printArgument(argument, writer, session);
} else if (argumentType == OUT) {
printOutputArgument(argument, writer, session);
} else if (argumentType == INOUT) {
printInOutputArgument(argument, writer, session);
}
if (i < (getArguments().size() - 1)) {
writer.write(",\n");
}
}
if (getArguments().size() > getFirstArgumentIndex() || platform.requiresProcedureBrackets()) {
writer.write(")");
}
printReturn(writer, session);
writer.write(platform.getProcedureAsString());
writer.write("\n");
writer.write(platform.getProcedureOptionList());
writer.write("\n");
if (platform.shouldPrintStoredProcedureVariablesAfterBeginString()) {
writer.write(platform.getProcedureBeginString());
writer.write("\n");
}
if (!getVariables().isEmpty()) {
writer.write("DECLARE\n");
}
for (Enumeration variablesEnum = getVariables().elements();
variablesEnum.hasMoreElements();) {
FieldDefinition field = (FieldDefinition)variablesEnum.nextElement();
writer.write("\t");
writer.write(field.getName());
writer.write(" ");
writer.write(field.getTypeName());
writer.write(platform.getBatchDelimiterString());
writer.write("\n");
}
if (!platform.shouldPrintStoredProcedureVariablesAfterBeginString()) {
writer.write(platform.getProcedureBeginString());
writer.write("\n");
}
for (Enumeration statementsEnum = getStatements().elements();
statementsEnum.hasMoreElements();) {
writer.write((String)statementsEnum.nextElement());
writer.write(platform.getBatchDelimiterString());
writer.write("\n");
}
writer.write(platform.getProcedureEndString());
} catch (IOException ioException) {
throw ValidationException.fileError(ioException);
}
return writer;
}
/**
* INTERNAL: Return the drop table statement.
*/
public Writer buildDeletionWriter(AbstractSession session, Writer writer) throws ValidationException {
try {
writer.write(getDeletionHeader() + getFullName());
} catch (IOException ioException) {
throw ValidationException.fileError(ioException);
}
return writer;
}
/**
* The arguments are the names of the parameters to the procedure.
*/
public Vector getArguments() {
return arguments;
}
/**
*
*/
public String getCreationHeader() {
return "CREATE PROCEDURE ";
}
/**
*
*/
public String getDeletionHeader() {
return "DROP PROCEDURE ";
}
/**
*
*/
public int getFirstArgumentIndex() {
return 0;
}
/**
*
*/
public Vector getArgumentTypes() {
return argumentTypes;
}
/**
* The statements are the SQL lines of code in procedure.
*/
public Vector getStatements() {
return statements;
}
/**
* The variables are the names of the declared variables used in the procedure.
*/
public Vector getVariables() {
return variables;
}
/**
* Print the argument and its type.
* @param argument Stored procedure argument.
* @param writer Target writer where to write argument string.
* @param session Current session context.
* @throws IOException When any IO problem occurs.
*/
protected void printArgument(final FieldDefinition argument, final Writer writer,
final AbstractSession session) throws IOException {
final DatabasePlatform platform = session.getPlatform();
final FieldTypeDefinition fieldType
= getFieldTypeDefinition(session, argument.type, argument.typeName);
writer.write(platform.getProcedureArgumentString());
if (platform.shouldPrintInputTokenAtStart()) {
writer.write(" ");
writer.write(platform.getInputProcedureToken());
writer.write(" ");
}
writer.write(argument.name);
writer.write(" ");
writer.write(fieldType.getName());
if (fieldType.isSizeAllowed() && platform.allowsSizeInProcedureArguments()
&& ((argument.size != 0) || (fieldType.isSizeRequired()))) {
writer.write("(");
if (argument.size == 0) {
writer.write(Integer.toString(fieldType.getDefaultSize()));
} else {
writer.write(Integer.toString(argument.size));
}
if (argument.subSize != 0) {
writer.write(",");
writer.write(Integer.toString(argument.subSize));
} else if (fieldType.getDefaultSubSize() != 0) {
writer.write(",");
writer.write(Integer.toString(fieldType.getDefaultSubSize()));
}
writer.write(")");
}
}
/**
* Print the argument and its type.
* @param argument Stored procedure argument.
* @param writer Target writer where to write argument string.
* @param session Current session context.
* @throws ValidationException When invalid or inconsistent data were found.
*/
protected void printInOutputArgument(final FieldDefinition argument, final Writer writer,
final AbstractSession session) throws ValidationException {
try {
final DatabasePlatform platform = session.getPlatform();
final FieldTypeDefinition fieldType
= getFieldTypeDefinition(session, argument.type, argument.typeName);
writer.write(platform.getProcedureArgumentString());
if (platform.shouldPrintOutputTokenAtStart()) {
writer.write(" ");
writer.write(platform.getCreationInOutputProcedureToken());
writer.write(" ");
}
writer.write(argument.name);
if ((!platform.shouldPrintOutputTokenAtStart()) && platform.shouldPrintOutputTokenBeforeType()) {
writer.write(" ");
writer.write(platform.getCreationInOutputProcedureToken());
}
writer.write(" ");
writer.write(fieldType.getName());
if (fieldType.isSizeAllowed() && platform.allowsSizeInProcedureArguments()
&& ((argument.size != 0) || (fieldType.isSizeRequired()))) {
writer.write("(");
if (argument.size == 0) {
writer.write(Integer.toString(fieldType.getDefaultSize()));
} else {
writer.write(Integer.toString(argument.size));
}
if (argument.subSize != 0) {
writer.write(",");
writer.write(Integer.toString(argument.subSize));
} else if (fieldType.getDefaultSubSize() != 0) {
writer.write(",");
writer.write(Integer.toString(fieldType.getDefaultSubSize()));
}
writer.write(")");
}
if ((!platform.shouldPrintOutputTokenAtStart()) && (!platform.shouldPrintOutputTokenBeforeType())) {
writer.write(" ");
writer.write(platform.getCreationInOutputProcedureToken());
}
} catch (IOException ioException) {
throw ValidationException.fileError(ioException);
}
}
/**
* Print the argument and its type.
* @param argument Stored procedure argument.
* @param writer Target writer where to write argument string.
* @param session Current session context.
* @throws ValidationException When invalid or inconsistent data were found.
*/
protected void printOutputArgument(final FieldDefinition argument, final Writer writer,
final AbstractSession session) throws ValidationException {
try {
final DatabasePlatform platform = session.getPlatform();
final FieldTypeDefinition fieldType
= getFieldTypeDefinition(session, argument.type, argument.typeName);
writer.write(platform.getProcedureArgumentString());
if (platform.shouldPrintOutputTokenAtStart()) {
writer.write(" ");
writer.write(platform.getCreationOutputProcedureToken());
writer.write(" ");
}
writer.write(argument.name);
if ((!platform.shouldPrintOutputTokenAtStart()) && platform.shouldPrintOutputTokenBeforeType()) {
writer.write(" ");
writer.write(platform.getCreationOutputProcedureToken());
}
writer.write(" ");
writer.write(fieldType.getName());
if (fieldType.isSizeAllowed() && platform.allowsSizeInProcedureArguments()
&& ((argument.size != 0) || (fieldType.isSizeRequired()))) {
writer.write("(");
if (argument.size == 0) {
writer.write(Integer.toString(fieldType.getDefaultSize()));
} else {
writer.write(Integer.toString(argument.size));
}
if (argument.subSize != 0) {
writer.write(",");
writer.write(Integer.toString(argument.subSize));
} else if (fieldType.getDefaultSubSize() != 0) {
writer.write(",");
writer.write(Integer.toString(fieldType.getDefaultSubSize()));
}
writer.write(")");
}
if ((!platform.shouldPrintOutputTokenAtStart()) && !platform.shouldPrintOutputTokenBeforeType()) {
writer.write(" ");
writer.write(platform.getCreationOutputProcedureToken());
}
} catch (IOException ioException) {
throw ValidationException.fileError(ioException);
}
}
/**
* Prints return for stored function, nothing to do for stored procedure
*/
protected void printReturn(Writer writer, AbstractSession session) throws ValidationException {
}
/**
* The arguments are the field defs of the parameters names and types to the procedure.
*/
public void setArguments(Vector arguments) {
this.arguments = arguments;
}
/**
* The statements are the SQL lines of code in procedure.
*/
public void setStatements(Vector statements) {
this.statements = statements;
}
/**
* The variables are the field defs of the declared variables used in the procedure.
*/
public void setVariables(Vector variables) {
this.variables = variables;
}
}
|