1 21 22 package org.apache.derby.impl.sql.catalog; 23 24 import org.apache.derby.iapi.types.TypeId; 25 import org.apache.derby.iapi.sql.dictionary.SystemColumn; 26 import org.apache.derby.catalog.TypeDescriptor; 27 28 import org.apache.derby.iapi.types.DataValueDescriptor; 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.ConstraintDescriptor; 35 import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor; 36 import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator; 37 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 38 import org.apache.derby.iapi.sql.dictionary.DataDictionaryContext; 39 import org.apache.derby.iapi.sql.dictionary.ForeignKeyConstraintDescriptor; 40 import org.apache.derby.iapi.sql.dictionary.KeyConstraintDescriptor; 41 import org.apache.derby.iapi.sql.dictionary.ReferencedKeyConstraintDescriptor; 42 import org.apache.derby.iapi.sql.dictionary.TupleDescriptor; 43 import org.apache.derby.iapi.sql.dictionary.SubKeyConstraintDescriptor; 44 import org.apache.derby.iapi.sql.StatementType; 45 46 import org.apache.derby.iapi.sql.execute.ExecutionContext; 47 import org.apache.derby.iapi.sql.execute.ExecIndexRow; 48 import org.apache.derby.iapi.sql.execute.ExecRow; 49 import org.apache.derby.iapi.sql.execute.ExecutionFactory; 50 51 import org.apache.derby.iapi.error.StandardException; 52 53 import org.apache.derby.iapi.types.RowLocation; 54 55 import org.apache.derby.iapi.services.sanity.SanityManager; 56 57 import org.apache.derby.iapi.services.uuid.UUIDFactory; 58 import org.apache.derby.catalog.UUID; 59 import org.apache.derby.catalog.IndexDescriptor; 60 61 66 67 public class SYSFOREIGNKEYSRowFactory extends CatalogRowFactory 68 { 69 private static final String TABLENAME_STRING = "SYSFOREIGNKEYS"; 70 71 protected static final int SYSFOREIGNKEYS_COLUMN_COUNT = 5; 72 protected static final int SYSFOREIGNKEYS_CONSTRAINTID = 1; 73 protected static final int SYSFOREIGNKEYS_CONGLOMERATEID = 2; 74 protected static final int SYSFOREIGNKEYS_KEYCONSTRAINTID = 3; 75 protected static final int SYSFOREIGNKEYS_DELETERULE = 4; 76 protected static final int SYSFOREIGNKEYS_UPDATERULE = 5; 77 78 protected static final int SYSFOREIGNKEYS_CONSTRAINTID_WIDTH = 36; 80 81 protected static final int SYSFOREIGNKEYS_INDEX1_ID = 0; 82 protected static final int SYSFOREIGNKEYS_INDEX2_ID = 1; 83 84 private static final int[][] indexColumnPositions = 85 { 86 {SYSFOREIGNKEYS_CONSTRAINTID}, 87 {SYSFOREIGNKEYS_KEYCONSTRAINTID} 88 }; 89 90 private static final boolean[] uniqueness = { 91 true, 92 false 93 }; 94 95 private static final String [] uuids = 96 { 97 "8000005b-00d0-fd77-3ed8-000a0a0b1900" ,"80000060-00d0-fd77-3ed8-000a0a0b1900" ,"8000005d-00d0-fd77-3ed8-000a0a0b1900" ,"8000005f-00d0-fd77-3ed8-000a0a0b1900" }; 102 103 109 public SYSFOREIGNKEYSRowFactory(UUIDFactory uuidf, ExecutionFactory ef, DataValueFactory dvf, 110 boolean convertIdToLower) 111 { 112 super(uuidf,ef,dvf,convertIdToLower); 113 initInfo(SYSFOREIGNKEYS_COLUMN_COUNT, TABLENAME_STRING, 114 indexColumnPositions, uniqueness, uuids ); 115 } 116 117 123 130 public ExecRow makeRow(TupleDescriptor td, TupleDescriptor parent) 131 throws StandardException 132 { 133 DataValueDescriptor col; 134 ExecIndexRow row; 135 String constraintId = null; 136 String keyConstraintId = null; 137 String conglomId = null; 138 String raDeleteRule="N"; 139 String raUpdateRule="N"; 140 141 if (td != null) 142 { 143 ForeignKeyConstraintDescriptor cd = (ForeignKeyConstraintDescriptor)td; 144 constraintId = cd.getUUID().toString(); 145 146 ReferencedKeyConstraintDescriptor refCd = cd.getReferencedConstraint(); 147 if (SanityManager.DEBUG) 148 { 149 SanityManager.ASSERT(refCd != null, "this fk returned a null referenced key"); 150 } 151 keyConstraintId = refCd.getUUID().toString(); 152 conglomId = cd.getIndexUUIDString(); 153 154 raDeleteRule = getRefActionAsString(cd.getRaDeleteRule()); 155 raUpdateRule = getRefActionAsString(cd.getRaUpdateRule()); 156 } 157 158 159 160 row = getExecutionFactory().getIndexableRow(SYSFOREIGNKEYS_COLUMN_COUNT); 161 162 163 row.setColumn(SYSFOREIGNKEYS_CONSTRAINTID, dvf.getCharDataValue(constraintId)); 164 165 166 row.setColumn(SYSFOREIGNKEYS_CONGLOMERATEID, dvf.getCharDataValue(conglomId)); 167 168 169 row.setColumn(SYSFOREIGNKEYS_KEYCONSTRAINTID, dvf.getCharDataValue(keyConstraintId)); 170 171 173 row.setColumn(SYSFOREIGNKEYS_DELETERULE, dvf.getCharDataValue(raDeleteRule)); 174 175 176 row.setColumn(SYSFOREIGNKEYS_UPDATERULE, dvf.getCharDataValue(raUpdateRule)); 177 178 return row; 179 } 180 181 187 196 public TupleDescriptor buildDescriptor( 197 ExecRow row, 198 TupleDescriptor parentTupleDescriptor, 199 DataDictionary dd ) 200 throws StandardException 201 { 202 203 if (SanityManager.DEBUG) 204 { 205 SanityManager.ASSERT( 206 row.nColumns() == SYSFOREIGNKEYS_COLUMN_COUNT, 207 "Wrong number of columns for a SYSKEYS row"); 208 } 209 210 DataValueDescriptor col; 211 DataDescriptorGenerator ddg; 212 UUID constraintUUID; 213 UUID conglomerateUUID; 214 UUID keyConstraintUUID; 215 String constraintUUIDString; 216 String conglomerateUUIDString; 217 String raRuleString; 218 int raDeleteRule; 219 int raUpdateRule; 220 221 ddg = dd.getDataDescriptorGenerator(); 222 223 224 col = row.getColumn(SYSFOREIGNKEYS_CONSTRAINTID); 225 constraintUUIDString = col.getString(); 226 constraintUUID = getUUIDFactory().recreateUUID(constraintUUIDString); 227 228 229 col = row.getColumn(SYSFOREIGNKEYS_CONGLOMERATEID); 230 conglomerateUUIDString = col.getString(); 231 conglomerateUUID = getUUIDFactory().recreateUUID(conglomerateUUIDString); 232 233 234 col = row.getColumn(SYSFOREIGNKEYS_KEYCONSTRAINTID); 235 constraintUUIDString = col.getString(); 236 keyConstraintUUID = getUUIDFactory().recreateUUID(constraintUUIDString); 237 238 239 240 col= row.getColumn(SYSFOREIGNKEYS_DELETERULE); 241 raRuleString = col.getString(); 242 raDeleteRule = getRefActionAsInt(raRuleString); 243 244 245 col = row.getColumn(SYSFOREIGNKEYS_UPDATERULE); 246 raRuleString = col.getString(); 247 raUpdateRule = getRefActionAsInt(raRuleString); 248 249 250 251 return new SubKeyConstraintDescriptor( 252 constraintUUID, 253 conglomerateUUID, 254 keyConstraintUUID, 255 raDeleteRule, 256 raUpdateRule); 257 } 258 259 265 public SystemColumn[] buildColumnList() 266 { 267 int index = 0; 268 SystemColumn[] columnList = new SystemColumn[SYSFOREIGNKEYS_COLUMN_COUNT]; 269 270 272 columnList[index++] = new SystemColumnImpl( 273 convertIdCase( "CONSTRAINTID"), SYSFOREIGNKEYS_CONSTRAINTID, 0, 0, false, "CHAR", true, 36 ); 282 columnList[index++] = new SystemColumnImpl( 283 convertIdCase( "CONGLOMERATEID"), SYSFOREIGNKEYS_CONGLOMERATEID, 0, 0, false, "CHAR", true, 36 ); 292 columnList[index++] = new SystemColumnImpl( 293 convertIdCase( "KEYCONSTRAINTID"), SYSFOREIGNKEYS_KEYCONSTRAINTID, 0, 0, false, "CHAR", true, 36 ); 302 303 columnList[index++] = new SystemColumnImpl( 304 convertIdCase( "DELETERULE"), SYSFOREIGNKEYS_DELETERULE, 0, 0, false, "CHAR", true, 1 ); 313 314 columnList[index++] = new SystemColumnImpl( 315 convertIdCase( "UPDATERULE"), SYSFOREIGNKEYS_UPDATERULE, 0, 0, false, "CHAR", true, 1 ); 324 return columnList; 325 } 326 327 328 int getRefActionAsInt(String raRuleString) 329 { 330 int raRule ; 331 switch (raRuleString.charAt(0)){ 332 case 'C': 333 raRule = StatementType.RA_CASCADE; 334 break; 335 case 'S': 336 raRule = StatementType.RA_RESTRICT; 337 break; 338 case 'R': 339 raRule = StatementType.RA_NOACTION; 340 break; 341 case 'U': 342 raRule = StatementType.RA_SETNULL; 343 break; 344 case 'D': 345 raRule = StatementType.RA_SETDEFAULT; 346 break; 347 default: 348 raRule =StatementType.RA_NOACTION; ; 349 if (SanityManager.DEBUG) 350 { 351 SanityManager.THROWASSERT("Invalid value '" 352 +raRuleString+ "' for a referetial Action"); 353 } 354 } 355 return raRule ; 356 } 357 358 359 String getRefActionAsString(int raRule) 360 { 361 String raRuleString ; 362 switch (raRule){ 363 case StatementType.RA_CASCADE: 364 raRuleString = "C"; 365 break; 366 case StatementType.RA_RESTRICT: 367 raRuleString = "S"; 368 break; 369 case StatementType.RA_NOACTION: 370 raRuleString = "R"; 371 break; 372 case StatementType.RA_SETNULL: 373 raRuleString = "U"; 374 break; 375 case StatementType.RA_SETDEFAULT: 376 raRuleString = "D"; 377 raRule = StatementType.RA_SETDEFAULT; 378 break; 379 default: 380 raRuleString ="N" ; if (SanityManager.DEBUG) 382 { 383 SanityManager.THROWASSERT("Invalid value '" 384 +raRule+ "' for a referetial Action"); 385 } 386 387 } 388 return raRuleString ; 389 } 390 391 392 } 393 | Popular Tags |