1 21 package oracle.toplink.essentials.internal.helper; 23 24 import java.sql.*; 25 import java.util.*; 26 import oracle.toplink.essentials.internal.expressions.SQLSelectStatement; 27 import oracle.toplink.essentials.internal.expressions.ForUpdateClause; 28 import oracle.toplink.essentials.queryframework.*; 29 import oracle.toplink.essentials.expressions.Expression; 30 import oracle.toplink.essentials.internal.sessions.AbstractSession; 31 import oracle.toplink.essentials.internal.databaseaccess.DatabaseCall; 32 import oracle.toplink.essentials.internal.databaseaccess.Accessor; 33 34 51 public class LOBValueWriter { 52 private Collection calls = null; 54 private Accessor accessor; 55 56 62 public LOBValueWriter(Accessor accessor) { 63 this.accessor = accessor; 64 } 65 66 protected void buildAndExecuteCall(DatabaseCall dbCall, AbstractSession session) { 67 DatabaseQuery query = dbCall.getQuery(); 68 if (!query.isWriteObjectQuery()) { 69 return; 71 } 72 WriteObjectQuery writeQuery = (WriteObjectQuery)query; 73 writeQuery.setAccessor(accessor); 74 SQLSelectStatement selectStatement = buildSelectStatementForLocator(writeQuery, dbCall, session); 76 77 DatabaseCall call = buildCallFromSelectStatementForLocator(selectStatement, writeQuery, dbCall, session); 79 80 accessor.executeCall(call, call.getQuery().getTranslationRow(), session); 81 } 82 83 86 public void fetchLocatorAndWriteValue(DatabaseCall dbCall, Object resultSet) throws SQLException { 87 Enumeration enumFields = dbCall.getContexts().getFields().elements(); 88 Enumeration enumValues = dbCall.getContexts().getValues().elements(); 89 AbstractSession executionSession = dbCall.getQuery().getSession().getExecutionSession(dbCall.getQuery()); 90 while (enumFields.hasMoreElements()) { 91 DatabaseField field = (DatabaseField)enumFields.nextElement(); 92 Object value = enumValues.nextElement(); 93 94 executionSession.getPlatform().writeLOB(field, value, (ResultSet)resultSet, executionSession); 96 } 97 } 98 99 102 private SQLSelectStatement buildSelectStatementForLocator(WriteObjectQuery writeQuery, DatabaseCall call, AbstractSession session) { 103 SQLSelectStatement selectStatement = new SQLSelectStatement(); 104 Vector tables = writeQuery.getDescriptor().getTables(); 105 selectStatement.setTables(tables); 106 selectStatement.setFields(call.getContexts().getFields()); 108 selectStatement.setWhereClause(writeQuery.getDescriptor().getObjectBuilder().buildPrimaryKeyExpressionFromObject(writeQuery.getObject(), session)); 110 selectStatement.setLockingClause(ForUpdateClause.newInstance(ObjectBuildingQuery.LOCK)); 112 113 if (tables.size() > 1) { 114 Expression expression = selectStatement.getWhereClause(); 116 117 Expression additionalJoin = (Expression)writeQuery.getDescriptor().getQueryManager().getAdditionalJoinExpression(); 119 if (additionalJoin != null) { 120 expression = expression.and(additionalJoin); 121 } 122 123 selectStatement.setWhereClause(expression); 125 } 126 127 selectStatement.normalize(session, writeQuery.getDescriptor()); 129 return selectStatement; 130 } 131 132 135 private DatabaseCall buildCallFromSelectStatementForLocator(SQLSelectStatement selectStatement, WriteObjectQuery writeQuery, DatabaseCall dbCall, AbstractSession session) { 136 DatabaseCall call = selectStatement.buildCall(session); 137 138 call.setContexts(dbCall.getContexts()); 140 call.returnOneRow(); 142 call.setQuery(writeQuery); 144 call.prepare(session); 146 call.translate(writeQuery.getTranslationRow(), writeQuery.getModifyRow(), session); 148 return call; 149 } 150 151 158 161 public void addCall(Call call) { 162 if (calls == null) { 163 calls = new ArrayList(2); 165 } 166 calls.add(call); 167 } 168 169 176 179 public void buildAndExecuteSelectCalls(AbstractSession session) { 180 if ((calls == null) || calls.isEmpty()) { 181 return; 183 } 184 185 try { 187 for (Iterator callIt = calls.iterator(); callIt.hasNext();) { 188 DatabaseCall dbCall = (DatabaseCall)callIt.next(); 189 buildAndExecuteCall(dbCall, session); 190 } 191 } finally { 192 calls.clear(); 195 } 196 } 197 } 198 | Popular Tags |