1 21 package oracle.toplink.essentials.queryframework; 23 24 import java.util.Vector ; 25 import java.io.*; 26 import oracle.toplink.essentials.internal.databaseaccess.*; 27 import oracle.toplink.essentials.internal.helper.DatabaseField; 28 import oracle.toplink.essentials.internal.sessions.AbstractSession; 29 import oracle.toplink.essentials.exceptions.ValidationException; 30 import oracle.toplink.essentials.internal.expressions.ParameterExpression; 31 32 36 public class SQLCall extends DatabaseCall implements QueryStringCall { 37 protected boolean hasCustomSQLArguments; 38 39 43 public SQLCall() { 44 super(); 45 this.hasCustomSQLArguments = false; 46 } 47 48 52 public SQLCall(String sqlString) { 53 this(); 54 setSQLString(sqlString); 55 } 56 57 61 protected void afterTranslateCustomQuery(Vector updatedParameters, Vector updatedParameterTypes) { 62 for (int i = 0; i < getParameters().size(); i++) { 63 Integer parameterType = (Integer )getParameterTypes().elementAt(i); 64 Object parameter = getParameters().elementAt(i); 65 if ((parameterType == MODIFY) || (parameterType == OUT) || (parameterType == OUT_CURSOR) || ((parameterType == IN) && parameter instanceof DatabaseField)) { 66 DatabaseField field = (DatabaseField)parameter; 67 afterTranslateCustomQueryUpdateParameter(field, i, parameterType, updatedParameters, updatedParameterTypes); 68 } else if (parameterType == INOUT) { 69 DatabaseField outField = (DatabaseField)((Object [])parameter)[1]; 70 afterTranslateCustomQueryUpdateParameter(outField, i, parameterType, updatedParameters, updatedParameterTypes); 71 if ((((Object [])parameter)[0] instanceof DatabaseField) && (((Object [])parameter)[0] != ((Object [])parameter)[1])) { 72 DatabaseField inField = (DatabaseField)((Object [])parameter)[0]; 73 afterTranslateCustomQueryUpdateParameter(inField, i, parameterType, updatedParameters, updatedParameterTypes); 74 } 75 } 76 } 77 } 78 79 83 protected void afterTranslateCustomQueryUpdateParameter(DatabaseField field, int index, Integer parameterType, Vector updatedParameters, Vector updatedParameterTypes) { 84 for (int j = 0; j < updatedParameters.size(); j++) { 85 DatabaseField updateField = (DatabaseField)updatedParameters.elementAt(j); 86 if (field.equals(updateField)) { 87 Integer updateParameterType = (Integer )updatedParameterTypes.elementAt(j); 88 if (updateParameterType == null) { 89 field.setType(updateField.getType()); 90 } else if (updateParameterType == OUT_CURSOR) { 91 if (parameterType == OUT) { 92 getParameterTypes().setElementAt(OUT_CURSOR, index); 93 } else { 94 throw ValidationException.cannotSetCursorForParameterTypeOtherThanOut(field.getName(), toString()); 95 } 96 } 97 break; 98 } 99 } 100 } 101 102 106 public boolean hasCustomSQLArguments() { 107 return hasCustomSQLArguments; 108 } 109 110 public boolean isSQLCall() { 111 return true; 112 } 113 114 public boolean isQueryStringCall() { 115 return true; 116 } 117 118 122 protected void prepareInternal(AbstractSession session) { 123 if (hasCustomSQLArguments()) { 124 Vector updatedParameters = null; 126 Vector updatedParameterTypes = null; 127 if (getParameters().size() > 0) { 128 updatedParameters = getParameters(); 129 setParameters(oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance()); 130 updatedParameterTypes = getParameterTypes(); 131 setParameterTypes(oracle.toplink.essentials.internal.helper.NonSynchronizedVector.newInstance()); 132 } 133 134 translateCustomQuery(); 135 136 if (updatedParameters != null) { 137 afterTranslateCustomQuery(updatedParameters, updatedParameterTypes); 138 } 139 } 140 141 super.prepareInternal(session); 142 } 143 144 148 public void setHasCustomSQLArguments(boolean hasCustomSQLArguments) { 149 this.hasCustomSQLArguments = hasCustomSQLArguments; 150 } 151 152 157 public void setCustomSQLArgumentType(String customParameterName, Class type) { 158 DatabaseField field = new DatabaseField(customParameterName); 159 field.setType(type); 160 getParameters().add(field); 161 getParameterTypes().add(null); 162 } 163 164 167 public void setSQLString(String sqlString) { 168 setSQLStringInternal(sqlString); 169 } 170 171 175 public void appendTranslationParameter(Writer writer, ParameterExpression expression, DatabasePlatform platform) throws IOException { 176 try { 177 platform.writeParameterMarker(writer, expression); 178 } catch (IOException exception) { 179 throw ValidationException.fileError(exception); 180 } 181 getParameters().addElement(expression); 182 getParameterTypes().addElement(TRANSLATION); 183 } 184 185 192 public void useCustomSQLCursorOutputAsResultSet(String customParameterName) { 193 DatabaseField field = new DatabaseField(customParameterName); 194 getParameters().add(field); 195 getParameterTypes().add(OUT_CURSOR); 196 setIsCursorOutputProcedure(true); 197 } 198 } 199 | Popular Tags |