1 21 22 package org.apache.derby.impl.sql.execute; 23 24 import org.apache.derby.iapi.services.sanity.SanityManager; 25 26 import org.apache.derby.iapi.error.StandardException; 27 28 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 29 30 import org.apache.derby.iapi.types.DataValueDescriptor; 31 32 import org.apache.derby.iapi.sql.execute.CursorResultSet; 33 import org.apache.derby.iapi.sql.execute.ExecRow; 34 import org.apache.derby.iapi.sql.execute.NoPutResultSet; 35 36 import org.apache.derby.iapi.sql.Activation; 37 import org.apache.derby.iapi.sql.ResultDescription; 38 39 import org.apache.derby.vti.DeferModification; 40 41 import java.sql.PreparedStatement ; 42 import java.sql.ResultSet ; 43 44 import java.util.Properties ; 45 46 52 class InsertVTIResultSet extends DMLVTIResultSet 53 { 54 55 private PreparedStatement ps; 56 private VTIResultSet vtiRS; 57 private java.sql.ResultSet rs; 58 59 private TemporaryRowHolderImpl rowHolder; 60 61 65 public InsertVTIResultSet(NoPutResultSet source, 66 NoPutResultSet vtiRS, 67 Activation activation) 68 throws StandardException 69 { 70 super(source, activation); 71 this.vtiRS = (VTIResultSet) vtiRS; 72 } 73 74 77 protected void openCore() throws StandardException 78 { 79 82 if (ps == null) 83 { 84 ps = (PreparedStatement ) vtiRS.getVTIConstructor().invoke(activation); 85 } 86 87 if( ps instanceof DeferModification) 88 { 89 try 90 { 91 ((DeferModification) ps).modificationNotify( DeferModification.INSERT_STATEMENT, constants.deferred); 92 } 93 catch (Throwable t) 94 { 95 throw StandardException.unexpectedUserException(t); 96 } 97 } 98 99 ExecRow row = getNextRowCore(sourceResultSet); 100 101 try 102 { 103 rs = ps.executeQuery(); 104 } 105 catch (Throwable t) 106 { 107 throw StandardException.unexpectedUserException(t); 108 } 109 110 115 if (! firstExecute) 116 { 117 lcc.getStatementContext().setTopResultSet(this, subqueryTrackingArray); 118 } 119 120 126 if (constants.deferred) 127 { 128 activation.clearIndexScanInfo(); 129 } 130 131 if (firstExecute && constants.deferred) 132 { 133 Properties properties = new Properties (); 134 135 138 rowHolder = 139 new TemporaryRowHolderImpl(activation, properties, 140 resultDescription); 141 } 142 143 while ( row != null ) 144 { 145 150 if (constants.deferred) 151 { 152 rowHolder.insert(row); 153 } 154 else 155 { 156 insertIntoVTI(rs, row); 157 } 158 159 rowCount++; 160 161 if (constants.singleRowSource) 163 { 164 row = null; 165 } 166 else 167 { 168 row = getNextRowCore(sourceResultSet); 169 } 170 } 171 172 176 if (constants.deferred) 177 { 178 CursorResultSet tempRS = rowHolder.getResultSet(); 179 try 180 { 181 tempRS.open(); 182 while ((row = tempRS.getNextRow()) != null) 183 { 184 insertIntoVTI(rs, row); 185 } 186 } finally 187 { 188 sourceResultSet.clearCurrentRow(); 189 tempRS.close(); 190 } 191 } 192 193 if (rowHolder != null) 194 { 195 rowHolder.close(); 196 } 198 } 200 private void insertIntoVTI(ResultSet target, ExecRow row) 201 throws StandardException 202 { 203 try 204 { 205 target.moveToInsertRow(); 206 207 DataValueDescriptor[] rowArray = row.getRowArray(); 208 for (int index = 0; index < rowArray.length; index++) 209 { 210 DataValueDescriptor dvd = rowArray[index]; 211 212 try { 213 if (dvd.isNull()) 214 target.updateNull(index + 1); 215 else 216 dvd.setInto(target, index + 1); 217 } catch (Throwable t) { 218 target.updateObject(index + 1, dvd.getObject()); 221 } 222 } 223 224 target.insertRow(); 225 } 226 catch (Throwable t) 227 { 228 throw StandardException.unexpectedUserException(t); 229 } 230 } 231 232 237 public void cleanUp() throws StandardException 238 { 239 if (rowHolder != null) 240 { 241 rowHolder.close(); 242 } 243 244 if (rs != null) 245 { 246 try 247 { 248 rs.close(); 249 } 250 catch (Throwable t) 251 { 252 throw StandardException.unexpectedUserException(t); 253 } 254 rs = null; 255 } 256 257 if (!vtiRS.isReuseablePs() && ps != null) 259 { 260 try 261 { 262 ps.close(); 263 ps = null; 264 } 265 catch (Throwable t) 266 { 267 throw StandardException.unexpectedUserException(t); 268 } 269 } 270 super.cleanUp(); 271 } 273 275 public void finish() throws StandardException { 276 277 if ((ps != null) && !vtiRS.isReuseablePs()) 278 { 279 try 280 { 281 ps.close(); 282 ps = null; 283 } 284 catch (Throwable t) 285 { 286 throw StandardException.unexpectedUserException(t); 287 } 288 } 289 super.finish(); 290 } } 292 293 | Popular Tags |