1 21 22 package org.apache.derby.impl.sql.execute; 23 24 import org.apache.derby.iapi.store.access.TransactionController; 25 26 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 27 28 import org.apache.derby.iapi.sql.execute.ConstantAction; 29 30 import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator; 31 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 32 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor; 33 import org.apache.derby.iapi.sql.dictionary.SPSDescriptor; 34 import org.apache.derby.iapi.sql.dictionary.TableDescriptor; 35 import org.apache.derby.iapi.sql.dictionary.TriggerDescriptor; 36 37 import org.apache.derby.iapi.types.DataValueFactory; 38 39 import org.apache.derby.iapi.sql.depend.DependencyManager; 40 41 import org.apache.derby.iapi.sql.execute.ExecutionFactory; 42 43 import org.apache.derby.iapi.sql.Activation; 44 45 import org.apache.derby.iapi.error.StandardException; 46 import org.apache.derby.iapi.reference.SQLState; 47 48 import org.apache.derby.iapi.services.context.ContextService; 49 50 import org.apache.derby.iapi.services.sanity.SanityManager; 51 52 import org.apache.derby.catalog.UUID; 53 54 import java.sql.Timestamp ; 55 56 62 class CreateTriggerConstantAction extends DDLSingleTableConstantAction 63 { 64 65 private String triggerName; 66 private String triggerSchemaName; 67 private TableDescriptor triggerTable; private UUID triggerTableId; private int eventMask; 70 private boolean isBefore; 71 private boolean isRow; 72 private boolean isEnabled; 73 private boolean referencingOld; 74 private boolean referencingNew; 75 private UUID whenSPSId; 76 private String whenText; 77 private UUID actionSPSId; 78 private String actionText; 79 private String originalActionText; 80 private String oldReferencingName; 81 private String newReferencingName; 82 private UUID spsCompSchemaId; 83 private Timestamp creationTimestamp; 84 private int[] referencedCols; 85 86 88 114 CreateTriggerConstantAction 115 ( 116 String triggerSchemaName, 117 String triggerName, 118 int eventMask, 119 boolean isBefore, 120 boolean isRow, 121 boolean isEnabled, 122 TableDescriptor triggerTable, 123 UUID whenSPSId, 124 String whenText, 125 UUID actionSPSId, 126 String actionText, 127 UUID spsCompSchemaId, 128 Timestamp creationTimestamp, 129 int[] referencedCols, 130 String originalActionText, 131 boolean referencingOld, 132 boolean referencingNew, 133 String oldReferencingName, 134 String newReferencingName 135 ) 136 { 137 super(triggerTable.getUUID()); 138 this.triggerName = triggerName; 139 this.triggerSchemaName = triggerSchemaName; 140 this.triggerTable = triggerTable; 141 this.eventMask = eventMask; 142 this.isBefore = isBefore; 143 this.isRow = isRow; 144 this.isEnabled = isEnabled; 145 this.whenSPSId = whenSPSId; 146 this.whenText = whenText; 147 this.actionSPSId = actionSPSId; 148 this.actionText = actionText; 149 this.spsCompSchemaId = spsCompSchemaId; 150 this.creationTimestamp = creationTimestamp; 151 this.referencedCols = referencedCols; 152 this.originalActionText = originalActionText; 153 this.referencingOld = referencingOld; 154 this.referencingNew = referencingNew; 155 this.oldReferencingName = oldReferencingName; 156 this.newReferencingName = newReferencingName; 157 if (SanityManager.DEBUG) 158 { 159 SanityManager.ASSERT(triggerSchemaName != null, "triggerSchemaName sd is null"); 160 SanityManager.ASSERT(triggerName != null, "trigger name is null"); 161 SanityManager.ASSERT(triggerTable != null, "triggerTable is null"); 162 SanityManager.ASSERT(actionText != null, "actionText is null"); 163 } 164 } 165 166 173 public void executeConstantAction(Activation activation) 174 throws StandardException 175 { 176 SPSDescriptor whenspsd = null; 177 SPSDescriptor actionspsd; 178 179 LanguageConnectionContext lcc = activation.getLanguageConnectionContext(); 180 DataDictionary dd = lcc.getDataDictionary(); 181 DependencyManager dm = dd.getDependencyManager(); 182 TransactionController tc = lcc.getTransactionExecute(); 183 184 190 dd.startWriting(lcc); 191 192 SchemaDescriptor triggerSd = getSchemaDescriptorForCreate(dd, activation, triggerSchemaName); 193 194 if (spsCompSchemaId == null) { 195 SchemaDescriptor def = lcc.getDefaultSchema(); 196 if (def.getUUID() == null) { 197 def = dd.getSchemaDescriptor(def.getDescriptorName(), tc, 200 false); 201 } 202 spsCompSchemaId = def.getUUID(); 203 } 204 if (SanityManager.DEBUG) { 205 SanityManager.ASSERT(spsCompSchemaId != null, 206 "spsCompSchemaId is null"); 207 } 208 209 String tabName; 210 if (triggerTable != null) 211 { 212 triggerTableId = triggerTable.getUUID(); 213 tabName = triggerTable.getName(); 214 } 215 else 216 tabName = "with UUID " + triggerTableId; 217 218 222 triggerTable = dd.getTableDescriptor(triggerTableId); 223 if (triggerTable == null) 224 { 225 throw StandardException.newException( 226 SQLState.LANG_TABLE_NOT_FOUND_DURING_EXECUTION, 227 tabName); 228 } 229 232 lockTableForDDL(tc, triggerTable.getHeapConglomerateId(), true); 233 236 triggerTable = dd.getTableDescriptor(triggerTableId); 237 if (triggerTable == null) 238 { 239 throw StandardException.newException( 240 SQLState.LANG_TABLE_NOT_FOUND_DURING_EXECUTION, 241 tabName); 242 } 243 244 252 dm.invalidateFor(triggerTable, DependencyManager.CREATE_TRIGGER, lcc); 253 254 258 UUID tmpTriggerId = dd.getUUIDFactory().createUUID(); 259 260 actionSPSId = (actionSPSId == null) ? 261 dd.getUUIDFactory().createUUID() : actionSPSId; 262 263 DataDescriptorGenerator ddg = dd.getDataDescriptorGenerator(); 264 265 270 TriggerDescriptor triggerd = 271 ddg.newTriggerDescriptor( 272 triggerSd, 273 tmpTriggerId, 274 triggerName, 275 eventMask, 276 isBefore, 277 isRow, 278 isEnabled, 279 triggerTable, 280 whenspsd == null ? null : whenspsd.getUUID(), 281 actionSPSId, 282 creationTimestamp == null ? new Timestamp (System.currentTimeMillis()) : creationTimestamp, 283 referencedCols, 284 originalActionText, 285 referencingOld, 286 referencingNew, 287 oldReferencingName, 288 newReferencingName); 289 290 291 dd.addDescriptor(triggerd, triggerSd, 292 DataDictionary.SYSTRIGGERS_CATALOG_NUM, false, 293 tc); 294 295 296 299 if (whenText != null) 300 { 301 whenspsd = createSPS(lcc, ddg, dd, tc, tmpTriggerId, triggerSd, 302 whenSPSId, spsCompSchemaId, whenText, true, triggerTable); 303 } 304 305 308 actionspsd = createSPS(lcc, ddg, dd, tc, tmpTriggerId, triggerSd, 309 actionSPSId, spsCompSchemaId, actionText, false, triggerTable); 310 311 314 if (whenspsd != null) 315 { 316 dm.addDependency(triggerd, whenspsd, lcc.getContextManager()); 317 } 318 dm.addDependency(triggerd, actionspsd, lcc.getContextManager()); 319 dm.addDependency(triggerd, triggerTable, lcc.getContextManager()); 320 dm.addDependency(actionspsd, triggerTable, lcc.getContextManager()); 321 storeViewTriggerDependenciesOnPrivileges(activation, triggerd); 323 } 324 325 326 329 private SPSDescriptor createSPS 330 ( 331 LanguageConnectionContext lcc, 332 DataDescriptorGenerator ddg, 333 DataDictionary dd, 334 TransactionController tc, 335 UUID triggerId, 336 SchemaDescriptor sd, 337 UUID spsId, 338 UUID compSchemaId, 339 String text, 340 boolean isWhen, 341 TableDescriptor triggerTable 342 ) throws StandardException 343 { 344 if (text == null) 345 { 346 return null; 347 } 348 349 353 String spsName = "TRIGGER" + 354 (isWhen ? "WHEN_" : "ACTN_") + 355 triggerId + "_" + triggerTable.getUUID().toString(); 356 357 SPSDescriptor spsd = new SPSDescriptor(dd, spsName, 358 (spsId == null) ? 359 dd.getUUIDFactory().createUUID() : 360 spsId, 361 sd.getUUID(), 362 compSchemaId == null ? 363 lcc.getDefaultSchema().getUUID() : 364 compSchemaId, 365 SPSDescriptor.SPS_TYPE_TRIGGER, 366 true, text, true ); 370 376 spsd.prepareAndRelease(lcc, triggerTable); 377 378 379 dd.addSPSDescriptor(spsd, tc, true); 380 381 return spsd; 382 } 383 384 public String toString() 385 { 386 return constructToString("CREATE TRIGGER ", triggerName); 387 } 388 } 389 390 391 | Popular Tags |