1 21 22 package org.apache.derby.impl.sql.catalog; 23 24 import org.apache.derby.iapi.types.DataTypeDescriptor; 25 import org.apache.derby.iapi.types.DataValueDescriptor; 26 import org.apache.derby.iapi.types.TypeId; 27 28 import org.apache.derby.iapi.types.TypeId; 29 30 import org.apache.derby.iapi.types.DataValueFactory; 31 import org.apache.derby.iapi.types.RowLocation; 32 33 import org.apache.derby.iapi.sql.dictionary.CatalogRowFactory; 34 import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator; 35 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 36 import org.apache.derby.iapi.sql.dictionary.SystemColumn; 37 import org.apache.derby.iapi.sql.dictionary.TriggerDescriptor; 38 import org.apache.derby.iapi.sql.dictionary.TupleDescriptor; 39 40 import org.apache.derby.iapi.sql.execute.ExecIndexRow; 41 import org.apache.derby.iapi.sql.execute.ExecRow; 42 import org.apache.derby.iapi.sql.execute.ExecutionFactory; 43 44 import org.apache.derby.iapi.error.StandardException; 45 46 import org.apache.derby.iapi.services.monitor.Monitor; 47 import org.apache.derby.catalog.ReferencedColumns; 48 import org.apache.derby.catalog.types.ReferencedColumnsDescriptorImpl; 49 import org.apache.derby.catalog.UUID; 50 import org.apache.derby.iapi.services.uuid.UUIDFactory; 51 52 import org.apache.derby.iapi.services.sanity.SanityManager; 53 import org.apache.derby.iapi.types.SQLTimestamp; 54 import java.sql.Timestamp ; 55 56 63 64 public class SYSTRIGGERSRowFactory extends CatalogRowFactory 65 { 66 static final String TABLENAME_STRING = "SYSTRIGGERS"; 67 68 69 public static final int SYSTRIGGERS_TRIGGERID = 1; 70 public static final int SYSTRIGGERS_TRIGGERNAME = 2; 71 public static final int SYSTRIGGERS_SCHEMAID = 3; 72 public static final int SYSTRIGGERS_CREATIONTIMESTAMP = 4; 73 public static final int SYSTRIGGERS_EVENT = 5; 74 public static final int SYSTRIGGERS_FIRINGTIME = 6; 75 public static final int SYSTRIGGERS_TYPE = 7; 76 public static final int SYSTRIGGERS_STATE = TriggerDescriptor.SYSTRIGGERS_STATE_FIELD; 77 public static final int SYSTRIGGERS_TABLEID = 9; 78 public static final int SYSTRIGGERS_WHENSTMTID = 10; 79 public static final int SYSTRIGGERS_ACTIONSTMTID = 11; 80 public static final int SYSTRIGGERS_REFERENCEDCOLUMNS = 12; 81 public static final int SYSTRIGGERS_TRIGGERDEFINITION = 13; 82 public static final int SYSTRIGGERS_REFERENCINGOLD = 14; 83 public static final int SYSTRIGGERS_REFERENCINGNEW = 15; 84 public static final int SYSTRIGGERS_OLDREFERENCINGNAME = 16; 85 public static final int SYSTRIGGERS_NEWREFERENCINGNAME = 17; 86 87 public static final int SYSTRIGGERS_COLUMN_COUNT = SYSTRIGGERS_NEWREFERENCINGNAME; 88 89 public static final int SYSTRIGGERS_INDEX1_ID = 0; 90 public static final int SYSTRIGGERS_INDEX2_ID = 1; 91 public static final int SYSTRIGGERS_INDEX3_ID = 2; 92 93 private static final int[][] indexColumnPositions = 94 { 95 {SYSTRIGGERS_TRIGGERID}, 96 {SYSTRIGGERS_TRIGGERNAME, SYSTRIGGERS_SCHEMAID}, 97 {SYSTRIGGERS_TABLEID, SYSTRIGGERS_CREATIONTIMESTAMP} 98 }; 99 100 private static final boolean[] uniqueness = { 101 true, 102 true, 103 false, 104 }; 105 106 private static final String [] uuids = 107 { 108 "c013800d-00d7-c025-4809-000a0a411200" ,"c013800d-00d7-c025-480a-000a0a411200" ,"c013800d-00d7-c025-480b-000a0a411200" ,"c013800d-00d7-c025-480c-000a0a411200" ,"c013800d-00d7-c025-480d-000a0a411200" }; 114 115 public SYSTRIGGERSRowFactory(UUIDFactory uuidf, ExecutionFactory ef, DataValueFactory dvf, 121 boolean convertIdToLower) 122 { 123 super(uuidf,ef,dvf,convertIdToLower); 124 initInfo(SYSTRIGGERS_COLUMN_COUNT, TABLENAME_STRING, 125 indexColumnPositions, uniqueness, uuids); 126 } 127 128 140 141 public ExecRow makeRow(TupleDescriptor td, TupleDescriptor parent) 142 throws StandardException 143 { 144 DataTypeDescriptor dtd; 145 ExecRow row; 146 DataValueDescriptor col; 147 String name = null; 148 UUID uuid = null; 149 UUID suuid = null; UUID tuuid = null; UUID actionSPSID = null; UUID whenSPSID = null; Timestamp createTime = null; 154 String event = null; 155 String time = null; 156 String type = null; 157 String enabled = null; 158 String triggerDefinition = null; 159 String oldReferencingName = null; 160 String newReferencingName = null; 161 ReferencedColumns rcd = null; 162 boolean referencingOld = false; 163 boolean referencingNew = false; 164 165 if (td != null) 166 { 167 TriggerDescriptor triggerDescriptor = (TriggerDescriptor)td; 168 name = triggerDescriptor.getName(); 169 uuid = triggerDescriptor.getUUID(); 170 suuid = triggerDescriptor.getSchemaDescriptor().getUUID(); 171 createTime = triggerDescriptor.getCreationTimestamp(); 172 event = triggerDescriptor.listensForEvent(TriggerDescriptor.TRIGGER_EVENT_UPDATE) ? "U" : 174 triggerDescriptor.listensForEvent(TriggerDescriptor.TRIGGER_EVENT_DELETE) ? "D" : "I"; 175 time = triggerDescriptor.isBeforeTrigger() ? "B" : "A"; 176 type = triggerDescriptor.isRowTrigger() ? "R" : "S"; 177 enabled = triggerDescriptor.isEnabled() ? "E" : "D"; 178 tuuid = triggerDescriptor.getTableDescriptor().getUUID(); 179 int[] refCols = triggerDescriptor.getReferencedCols(); 180 rcd = (refCols != null) ? new 181 ReferencedColumnsDescriptorImpl(refCols) : null; 182 183 actionSPSID = triggerDescriptor.getActionId(); 184 whenSPSID = triggerDescriptor.getWhenClauseId(); 185 triggerDefinition = triggerDescriptor.getTriggerDefinition(); 186 referencingOld = triggerDescriptor.getReferencingOld(); 187 referencingNew = triggerDescriptor.getReferencingNew(); 188 oldReferencingName = triggerDescriptor.getOldReferencingName(); 189 newReferencingName = triggerDescriptor.getNewReferencingName(); 190 } 191 192 193 row = getExecutionFactory().getValueRow(SYSTRIGGERS_COLUMN_COUNT); 194 195 196 row.setColumn(1, dvf.getCharDataValue((uuid == null) ? null : uuid.toString())); 197 198 199 row.setColumn(2, dvf.getVarcharDataValue(name)); 200 201 202 row.setColumn(3, dvf.getCharDataValue((suuid == null) ? null : suuid.toString())); 203 204 205 row.setColumn(4, dvf.getDataValue(createTime)); 206 207 208 row.setColumn(5, dvf.getCharDataValue(event)); 209 210 211 row.setColumn(6, dvf.getCharDataValue(time)); 212 213 214 row.setColumn(7, dvf.getCharDataValue(type)); 215 216 217 row.setColumn(8, dvf.getCharDataValue(enabled)); 218 219 220 row.setColumn(9, dvf.getCharDataValue((tuuid == null) ? null : tuuid.toString())); 221 222 223 row.setColumn(10, dvf.getCharDataValue((whenSPSID == null) ? null : whenSPSID.toString())); 224 225 226 row.setColumn(11, dvf.getCharDataValue((actionSPSID == null) ? null : actionSPSID.toString())); 227 228 231 row.setColumn(12, dvf.getDataValue(rcd)); 232 233 234 row.setColumn(13, dvf.getLongvarcharDataValue(triggerDefinition)); 235 236 237 row.setColumn(14, dvf.getDataValue(referencingOld)); 238 239 240 row.setColumn(15, dvf.getDataValue(referencingNew)); 241 242 243 row.setColumn(16, dvf.getVarcharDataValue(oldReferencingName)); 244 245 246 row.setColumn(17, dvf.getVarcharDataValue(newReferencingName)); 247 248 return row; 249 } 250 251 252 258 269 public TupleDescriptor buildDescriptor 270 ( 271 ExecRow row, 272 TupleDescriptor parentTupleDescriptor, 273 DataDictionary dd 274 ) throws StandardException 275 { 276 DataValueDescriptor col; 277 String name; 278 char theChar; 279 String uuidStr; 280 String triggerDefinition; 281 String oldReferencingName; 282 String newReferencingName; 283 UUID uuid; 284 UUID suuid; UUID tuuid; UUID actionSPSID = null; UUID whenSPSID = null; Timestamp createTime; 289 int eventMask = 0; 290 boolean isBefore; 291 boolean isRow; 292 boolean isEnabled; 293 boolean referencingOld; 294 boolean referencingNew; 295 ReferencedColumns rcd; 296 TriggerDescriptor descriptor; 297 DataDescriptorGenerator ddg = dd.getDataDescriptorGenerator(); 298 299 if (SanityManager.DEBUG) 300 { 301 SanityManager.ASSERT(row.nColumns() == SYSTRIGGERS_COLUMN_COUNT, 302 "Wrong number of columns for a SYSTRIGGERS row"); 303 } 304 305 col = row.getColumn(1); 307 uuidStr = col.getString(); 308 uuid = getUUIDFactory().recreateUUID(uuidStr); 309 310 col = row.getColumn(2); 312 name = col.getString(); 313 314 col = row.getColumn(3); 316 uuidStr = col.getString(); 317 suuid = getUUIDFactory().recreateUUID(uuidStr); 318 319 col = row.getColumn(4); 321 createTime = (Timestamp) col.getObject(); 322 323 col = row.getColumn(5); 325 theChar = col.getString().charAt(0); 326 switch (theChar) 327 { 328 case 'U': 329 eventMask = TriggerDescriptor.TRIGGER_EVENT_UPDATE; 330 break; 331 332 case 'I': 333 eventMask = TriggerDescriptor.TRIGGER_EVENT_INSERT; 334 break; 335 336 case 'D': 337 eventMask = TriggerDescriptor.TRIGGER_EVENT_DELETE; 338 break; 339 340 default: 341 if (SanityManager.DEBUG) 342 { 343 SanityManager.THROWASSERT("bad event mask: "+theChar); 344 } 345 } 346 347 isBefore = getCharBoolean(row.getColumn(6), 'B', 'A'); 349 350 isRow = getCharBoolean(row.getColumn(7), 'R', 'S'); 352 353 isEnabled = getCharBoolean(row.getColumn(8), 'E', 'D'); 355 356 col = row.getColumn(9); 358 uuidStr = col.getString(); 359 tuuid = getUUIDFactory().recreateUUID(uuidStr); 360 361 col = row.getColumn(10); 363 uuidStr = col.getString(); 364 if (uuidStr != null) 365 whenSPSID = getUUIDFactory().recreateUUID(uuidStr); 366 367 col = row.getColumn(11); 369 uuidStr = col.getString(); 370 if (uuidStr != null) 371 actionSPSID = getUUIDFactory().recreateUUID(uuidStr); 372 373 col = row.getColumn(12); 375 rcd = (ReferencedColumns) col.getObject(); 376 377 col = row.getColumn(13); 379 triggerDefinition = col.getString(); 380 381 col = row.getColumn(14); 383 referencingOld = col.getBoolean(); 384 385 col = row.getColumn(15); 387 referencingNew = col.getBoolean(); 388 389 col = row.getColumn(16); 391 oldReferencingName = col.getString(); 392 393 col = row.getColumn(17); 395 newReferencingName = col.getString(); 396 397 descriptor = new TriggerDescriptor( 398 dd, 399 dd.getSchemaDescriptor(suuid, null), 400 uuid, 401 name, 402 eventMask, 403 isBefore, 404 isRow, 405 isEnabled, 406 dd.getTableDescriptor(tuuid), 407 whenSPSID, 408 actionSPSID, 409 createTime, 410 (rcd == null) ? (int[])null : rcd.getReferencedColumnPositions(), 411 triggerDefinition, 412 referencingOld, 413 referencingNew, 414 oldReferencingName, 415 newReferencingName 416 ); 417 418 return descriptor; 419 } 420 421 422 432 public SystemColumn[] buildColumnList() 433 { 434 SystemColumn[] columnList = new SystemColumn[SYSTRIGGERS_COLUMN_COUNT]; 435 436 columnList[SYSTRIGGERS_TRIGGERID-1] = new SystemColumnImpl( 438 convertIdCase( "TRIGGERID"), SYSTRIGGERS_TRIGGERID, 0, 0, false, "CHAR", true, 36 ); 447 448 columnList[SYSTRIGGERS_TRIGGERNAME-1] = 449 new SystemColumnImpl( convertIdCase( "TRIGGERNAME"), SYSTRIGGERS_TRIGGERNAME, false ); 454 455 columnList[SYSTRIGGERS_SCHEMAID-1] = new SystemColumnImpl( 456 convertIdCase( "SCHEMAID"), SYSTRIGGERS_SCHEMAID, 0, 0, false, "CHAR", true, 36 ); 465 466 columnList[SYSTRIGGERS_CREATIONTIMESTAMP-1] = 467 new SystemColumnImpl( 468 convertIdCase( "CREATIONTIMESTAMP"), SYSTRIGGERS_CREATIONTIMESTAMP, 0, 0, false, "TIMESTAMP", true, TypeId.TIMESTAMP_MAXWIDTH ); 477 478 columnList[SYSTRIGGERS_EVENT-1] = 479 new SystemColumnImpl( 480 convertIdCase( "EVENT"), SYSTRIGGERS_EVENT, 0, 0, false, "CHAR", true, 1 ); 489 490 columnList[SYSTRIGGERS_FIRINGTIME-1] = 491 new SystemColumnImpl( 492 convertIdCase( "FIRINGTIME"), SYSTRIGGERS_FIRINGTIME, 0, 0, false, "CHAR", true, 1 ); 501 502 columnList[SYSTRIGGERS_TYPE-1] = 503 new SystemColumnImpl( 504 convertIdCase( "TYPE"), SYSTRIGGERS_TYPE, 0, 0, false, "CHAR", true, 1 ); 513 514 columnList[SYSTRIGGERS_STATE-1] = 515 new SystemColumnImpl( 516 convertIdCase( "STATE"), SYSTRIGGERS_STATE, 0, 0, false, "CHAR", true, 1 ); 525 526 columnList[SYSTRIGGERS_TABLEID-1] = 527 new SystemColumnImpl( 528 convertIdCase( "TABLEID"), SYSTRIGGERS_TABLEID, 0, 0, false, "CHAR", true, 36 ); 537 538 columnList[SYSTRIGGERS_WHENSTMTID-1] = 539 new SystemColumnImpl( 540 convertIdCase( "WHENSTMTID"), SYSTRIGGERS_WHENSTMTID, 0, 0, true, "CHAR", true, 36 ); 549 550 columnList[SYSTRIGGERS_ACTIONSTMTID-1] = 551 new SystemColumnImpl( 552 convertIdCase( "ACTIONSTMTID"), SYSTRIGGERS_ACTIONSTMTID, 0, 0, true, "CHAR", true, 36 ); 561 562 columnList[SYSTRIGGERS_REFERENCEDCOLUMNS-1] = 563 new SystemColumnImpl( 564 convertIdCase( "REFERENCEDCOLUMNS"), SYSTRIGGERS_REFERENCEDCOLUMNS, 0, 0, true, "org.apache.derby.catalog.ReferencedColumns", false, DataTypeDescriptor.MAXIMUM_WIDTH_UNKNOWN ); 573 574 columnList[SYSTRIGGERS_TRIGGERDEFINITION-1] = 575 new SystemColumnImpl( 576 convertIdCase( "TRIGGERDEFINITION"), SYSTRIGGERS_TRIGGERDEFINITION, 0, 0, true, "LONG VARCHAR", true, Integer.MAX_VALUE ); 585 586 columnList[SYSTRIGGERS_REFERENCINGOLD-1] = 587 new SystemColumnImpl( 588 convertIdCase( "REFERENCINGOLD"), SYSTRIGGERS_REFERENCINGOLD, 0, 0, true, "BOOLEAN", true, 1 ); 597 598 columnList[SYSTRIGGERS_REFERENCINGNEW-1] = 599 new SystemColumnImpl( 600 convertIdCase( "REFERENCINGNEW"), SYSTRIGGERS_REFERENCINGNEW, 0, 0, true, "BOOLEAN", true, 1 ); 609 610 columnList[SYSTRIGGERS_OLDREFERENCINGNAME-1] = 611 new SystemColumnImpl( convertIdCase( "OLDREFERENCINGNAME"), SYSTRIGGERS_OLDREFERENCINGNAME, true ); 616 617 columnList[SYSTRIGGERS_NEWREFERENCINGNAME-1] = 618 new SystemColumnImpl( convertIdCase( "NEWREFERENCINGNAME"), SYSTRIGGERS_NEWREFERENCINGNAME, true ); 623 624 return columnList; 625 } 626 627 private boolean getCharBoolean(DataValueDescriptor col, char trueValue, char falseValue) throws StandardException 629 { 630 char theChar = col.getString().charAt(0); 631 if (theChar == trueValue) 632 { 633 return true; 634 } 635 else if (theChar == falseValue) 636 { 637 return false; 638 } 639 else 640 { 641 if (SanityManager.DEBUG) 642 SanityManager.THROWASSERT("bad char value "+theChar); 643 644 return true; 645 } 646 } 647 } 648 | Popular Tags |