1 21 22 package org.apache.derby.impl.sql.catalog; 23 24 import org.apache.derby.catalog.UUID; 25 import org.apache.derby.iapi.error.StandardException; 26 import org.apache.derby.iapi.services.sanity.SanityManager; 27 import org.apache.derby.iapi.services.uuid.UUIDFactory; 28 import org.apache.derby.iapi.sql.dictionary.PermissionsDescriptor; 29 import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator; 30 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 31 import org.apache.derby.iapi.sql.dictionary.SystemColumn; 32 import org.apache.derby.iapi.sql.dictionary.TableDescriptor; 33 import org.apache.derby.iapi.sql.dictionary.TablePermsDescriptor; 34 import org.apache.derby.iapi.sql.dictionary.TupleDescriptor; 35 import org.apache.derby.iapi.sql.execute.ExecIndexRow; 36 import org.apache.derby.iapi.sql.execute.ExecRow; 37 import org.apache.derby.iapi.sql.execute.ExecutionFactory; 38 import org.apache.derby.iapi.types.DataValueDescriptor; 39 import org.apache.derby.iapi.types.DataValueFactory; 40 import org.apache.derby.iapi.types.RowLocation; 41 import org.apache.derby.iapi.types.StringDataValue; 42 43 47 48 public class SYSTABLEPERMSRowFactory extends PermissionsCatalogRowFactory 49 { 50 static final String TABLENAME_STRING = "SYSTABLEPERMS"; 51 52 private static final int TABLEPERMSID_COL_NUM = 1; 54 private static final int GRANTEE_COL_NUM = 2; 55 private static final int GRANTOR_COL_NUM = 3; 56 private static final int TABLEID_COL_NUM = 4; 57 private static final int SELECTPRIV_COL_NUM = 5; 58 private static final int DELETEPRIV_COL_NUM = 6; 59 private static final int INSERTPRIV_COL_NUM = 7; 60 private static final int UPDATEPRIV_COL_NUM = 8; 61 private static final int REFERENCESPRIV_COL_NUM = 9; 62 private static final int TRIGGERPRIV_COL_NUM = 10; 63 private static final int COLUMN_COUNT = 10; 64 65 public static final int GRANTEE_TABLE_GRANTOR_INDEX_NUM = 0; 66 public static final int TABLEPERMSID_INDEX_NUM = 1; 67 public static final int TABLEID_INDEX_NUM = 2; 68 private static final int[][] indexColumnPositions = 69 { 70 { GRANTEE_COL_NUM, TABLEID_COL_NUM, GRANTOR_COL_NUM}, 71 { TABLEPERMSID_COL_NUM }, 72 { TABLEID_COL_NUM } 73 }; 74 75 private static final boolean[] indexUniqueness = { true, true, false}; 76 77 private static final String [] uuids = 78 { 79 "b8450018-0103-0e39-b8e7-00000010f010" ,"004b0019-0103-0e39-b8e7-00000010f010" ,"c851401a-0103-0e39-b8e7-00000010f010" ,"80220011-010c-426e-c599-0000000f1120" ,"f81e0010-010c-bc85-060d-000000109ab8" }; 85 86 private SystemColumn[] columnList; 87 88 public SYSTABLEPERMSRowFactory(UUIDFactory uuidf, ExecutionFactory ef, DataValueFactory dvf, 89 boolean convertIdToLower) 90 { 91 super(uuidf,ef,dvf,convertIdToLower); 92 initInfo(COLUMN_COUNT, TABLENAME_STRING, indexColumnPositions, indexUniqueness, uuids); 93 } 94 95 public ExecRow makeRow(TupleDescriptor td, TupleDescriptor parent) throws StandardException 96 { 97 UUID oid; 98 DataValueDescriptor grantee = null; 99 DataValueDescriptor grantor = null; 100 String tablePermID = null; 101 String tableID = null; 102 String selectPriv = null; 103 String deletePriv = null; 104 String insertPriv = null; 105 String updatePriv = null; 106 String referencesPriv = null; 107 String triggerPriv = null; 108 109 if( td == null) 110 { 111 grantee = getNullAuthorizationID(); 112 grantor = getNullAuthorizationID(); 113 } 114 else 115 { 116 TablePermsDescriptor tpd = (TablePermsDescriptor) td; 117 oid = tpd.getUUID(); 118 if ( oid == null ) 119 { 120 oid = getUUIDFactory().createUUID(); 121 tpd.setUUID(oid); 122 } 123 tablePermID = oid.toString(); 124 125 grantee = getAuthorizationID( tpd.getGrantee()); 126 grantor = getAuthorizationID( tpd.getGrantor()); 127 tableID = tpd.getTableUUID().toString(); 128 selectPriv = tpd.getSelectPriv(); 129 deletePriv = tpd.getDeletePriv(); 130 insertPriv = tpd.getInsertPriv(); 131 updatePriv = tpd.getUpdatePriv(); 132 referencesPriv = tpd.getReferencesPriv(); 133 triggerPriv = tpd.getTriggerPriv(); 134 } 135 ExecRow row = getExecutionFactory().getValueRow( COLUMN_COUNT); 136 row.setColumn( TABLEPERMSID_COL_NUM, dvf.getCharDataValue(tablePermID)); 137 row.setColumn( GRANTEE_COL_NUM, grantee); 138 row.setColumn( GRANTOR_COL_NUM, grantor); 139 row.setColumn( TABLEID_COL_NUM, dvf.getCharDataValue( tableID)); 140 row.setColumn( SELECTPRIV_COL_NUM, dvf.getCharDataValue( selectPriv)); 141 row.setColumn( DELETEPRIV_COL_NUM, dvf.getCharDataValue( deletePriv)); 142 row.setColumn( INSERTPRIV_COL_NUM, dvf.getCharDataValue( insertPriv)); 143 row.setColumn( UPDATEPRIV_COL_NUM, dvf.getCharDataValue( updatePriv)); 144 row.setColumn( REFERENCESPRIV_COL_NUM,dvf.getCharDataValue( referencesPriv)); 145 row.setColumn( TRIGGERPRIV_COL_NUM, dvf.getCharDataValue( triggerPriv)); 146 147 return row; 148 } 150 151 public TupleDescriptor buildDescriptor(ExecRow row, 152 TupleDescriptor parentTuple, 153 DataDictionary dataDictionary) 154 throws StandardException 155 { 156 if( SanityManager.DEBUG) 157 SanityManager.ASSERT( row.nColumns() == COLUMN_COUNT, 158 "Wrong size row passed to SYSTABLEPERMSRowFactory.buildDescriptor"); 159 160 String tablePermsUUIDString = row.getColumn(TABLEPERMSID_COL_NUM).getString(); 161 UUID tablePermsUUID = getUUIDFactory().recreateUUID(tablePermsUUIDString); 162 String tableUUIDString = row.getColumn( TABLEID_COL_NUM).getString(); 163 UUID tableUUID = getUUIDFactory().recreateUUID(tableUUIDString); 164 String selectPriv = row.getColumn( SELECTPRIV_COL_NUM).getString(); 165 String deletePriv = row.getColumn( DELETEPRIV_COL_NUM).getString(); 166 String insertPriv = row.getColumn( INSERTPRIV_COL_NUM).getString(); 167 String updatePriv = row.getColumn( UPDATEPRIV_COL_NUM).getString(); 168 String referencesPriv = row.getColumn( REFERENCESPRIV_COL_NUM).getString(); 169 String triggerPriv = row.getColumn( TRIGGERPRIV_COL_NUM).getString(); 170 if( SanityManager.DEBUG) 171 { 172 SanityManager.ASSERT( "y".equals(selectPriv) || "Y".equals(selectPriv) || "N".equals(selectPriv), 173 "Invalid SYSTABLEPERMS.selectPriv column value: " + selectPriv); 174 SanityManager.ASSERT( "y".equals(deletePriv) || "Y".equals(deletePriv) || "N".equals(deletePriv), 175 "Invalid SYSTABLEPERMS.deletePriv column value: " + deletePriv); 176 SanityManager.ASSERT( "y".equals(insertPriv) || "Y".equals(insertPriv) || "N".equals(insertPriv), 177 "Invalid SYSTABLEPERMS.insertPriv column value: " + insertPriv); 178 SanityManager.ASSERT( "y".equals(updatePriv) || "Y".equals(updatePriv) || "N".equals(updatePriv), 179 "Invalid SYSTABLEPERMS.updatePriv column value: " + updatePriv); 180 SanityManager.ASSERT( "y".equals(referencesPriv) || "Y".equals(referencesPriv) || "N".equals(referencesPriv), 181 "Invalid SYSTABLEPERMS.referencesPriv column value: " + referencesPriv); 182 SanityManager.ASSERT( "y".equals(triggerPriv) || "Y".equals(triggerPriv) || "N".equals(triggerPriv), 183 "Invalid SYSTABLEPERMS.triggerPriv column value: " + triggerPriv); 184 } 185 186 TablePermsDescriptor tabPermsDesc = 187 new TablePermsDescriptor( dataDictionary, 188 getAuthorizationID( row, GRANTEE_COL_NUM), 189 getAuthorizationID( row, GRANTOR_COL_NUM), 190 tableUUID, 191 selectPriv, deletePriv, insertPriv, 192 updatePriv, referencesPriv, triggerPriv); 193 tabPermsDesc.setUUID(tablePermsUUID); 194 return tabPermsDesc; 195 } 197 198 public SystemColumn[] buildColumnList() 199 { 200 if (columnList == null) 201 { 202 columnList = new SystemColumn[ COLUMN_COUNT]; 203 204 columnList[ TABLEPERMSID_COL_NUM - 1] = 205 new SystemColumnImpl( convertIdCase( "TABLEPERMSID"), 206 TABLEPERMSID_COL_NUM, 207 0, 0, false, "CHAR", 211 true, 212 36); 213 columnList[ GRANTEE_COL_NUM - 1] = 214 new SystemColumnImpl( convertIdCase( "GRANTEE"), 215 GRANTEE_COL_NUM, 216 0, 0, false, AUTHORIZATION_ID_TYPE, 220 AUTHORIZATION_ID_IS_BUILTIN_TYPE, 221 AUTHORIZATION_ID_LENGTH); 222 columnList[ GRANTOR_COL_NUM - 1] = 223 new SystemColumnImpl( convertIdCase( "GRANTOR"), 224 GRANTOR_COL_NUM, 225 0, 0, false, AUTHORIZATION_ID_TYPE, 229 AUTHORIZATION_ID_IS_BUILTIN_TYPE, 230 AUTHORIZATION_ID_LENGTH); 231 columnList[ TABLEID_COL_NUM - 1] = 232 new SystemColumnImpl( convertIdCase( "TABLEID"), 233 TABLEID_COL_NUM, 234 0, 0, false, "CHAR", true, 36); 240 columnList[ SELECTPRIV_COL_NUM - 1] = 241 new SystemColumnImpl( convertIdCase( "SELECTPRIV"), 242 SELECTPRIV_COL_NUM, 243 0, 0, false, "CHAR", true, 1); 249 columnList[ DELETEPRIV_COL_NUM - 1] = 250 new SystemColumnImpl( convertIdCase( "DELETEPRIV"), 251 DELETEPRIV_COL_NUM, 252 0, 0, false, "CHAR", true, 1); 258 columnList[ INSERTPRIV_COL_NUM - 1] = 259 new SystemColumnImpl( convertIdCase( "INSERTPRIV"), 260 INSERTPRIV_COL_NUM, 261 0, 0, false, "CHAR", true, 1); 267 columnList[ UPDATEPRIV_COL_NUM - 1] = 268 new SystemColumnImpl( convertIdCase( "UPDATEPRIV"), 269 UPDATEPRIV_COL_NUM, 270 0, 0, false, "CHAR", true, 1); 276 columnList[ REFERENCESPRIV_COL_NUM - 1] = 277 new SystemColumnImpl( convertIdCase( "REFERENCESPRIV"), 278 REFERENCESPRIV_COL_NUM, 279 0, 0, false, "CHAR", true, 1); 285 columnList[ TRIGGERPRIV_COL_NUM - 1] = 286 new SystemColumnImpl( convertIdCase( "TRIGGERPRIV"), 287 TRIGGERPRIV_COL_NUM, 288 0, 0, false, "CHAR", true, 1); 294 } 295 return columnList; 296 } 298 301 public ExecIndexRow buildIndexKeyRow( int indexNumber, 302 PermissionsDescriptor perm) 303 throws StandardException 304 { 305 ExecIndexRow row = null; 306 307 switch( indexNumber) 308 { 309 case GRANTEE_TABLE_GRANTOR_INDEX_NUM: 310 row = getExecutionFactory().getIndexableRow( 2); 320 row.setColumn(1, getAuthorizationID( perm.getGrantee())); 321 String tableUUIDStr = ((TablePermsDescriptor) perm).getTableUUID().toString(); 322 row.setColumn(2, getDataValueFactory().getCharDataValue( tableUUIDStr)); 323 break; 324 case TABLEPERMSID_INDEX_NUM: 325 row = getExecutionFactory().getIndexableRow( 1); 326 String tablePermsUUIDStr = perm.getObjectID().toString(); 327 row.setColumn(1, getDataValueFactory().getCharDataValue( tablePermsUUIDStr)); 328 break; 329 case TABLEID_INDEX_NUM: 330 row = getExecutionFactory().getIndexableRow( 1); 331 tableUUIDStr = ((TablePermsDescriptor) perm).getTableUUID().toString(); 332 row.setColumn(1, getDataValueFactory().getCharDataValue( tableUUIDStr)); 333 break; 334 } 335 return row; 336 } 338 public int getPrimaryKeyIndexNumber() 339 { 340 return GRANTEE_TABLE_GRANTOR_INDEX_NUM; 341 } 342 343 355 public int orPermissions( ExecRow row, PermissionsDescriptor perm, boolean[] colsChanged) 356 throws StandardException 357 { 358 TablePermsDescriptor tablePerms = (TablePermsDescriptor) perm; 359 int changeCount = 0; 360 changeCount += orOnePermission( row, colsChanged, SELECTPRIV_COL_NUM, tablePerms.getSelectPriv()); 361 changeCount += orOnePermission( row, colsChanged, DELETEPRIV_COL_NUM, tablePerms.getDeletePriv()); 362 changeCount += orOnePermission( row, colsChanged, INSERTPRIV_COL_NUM, tablePerms.getInsertPriv()); 363 changeCount += orOnePermission( row, colsChanged, UPDATEPRIV_COL_NUM, tablePerms.getUpdatePriv()); 364 changeCount += orOnePermission( row, colsChanged, REFERENCESPRIV_COL_NUM, tablePerms.getReferencesPriv()); 365 changeCount += orOnePermission( row, colsChanged, TRIGGERPRIV_COL_NUM, tablePerms.getTriggerPriv()); 366 367 return changeCount; 368 } 370 private int orOnePermission( ExecRow row, boolean[] colsChanged, int column, String permission) 371 throws StandardException 372 { 373 if( permission.charAt(0) == 'N') 374 return 0; 375 376 if( SanityManager.DEBUG) 377 SanityManager.ASSERT( permission.charAt(0) == 'Y' || permission.charAt(0) == 'y', 378 "Invalid permission passed to SYSTABLEPERMSRowFactory.orOnePermission"); 379 DataValueDescriptor existingPermDVD = row.getColumn( column); 380 char existingPerm = existingPermDVD.getString().charAt(0); 381 if( existingPerm == 'Y' || existingPerm == permission.charAt(0)) 382 return 0; 383 existingPermDVD.setValue( permission); 384 colsChanged[ column - 1] = true; 385 return 1; 386 } 388 400 public int removePermissions( ExecRow row, PermissionsDescriptor perm, boolean[] colsChanged) 401 throws StandardException 402 { 403 TablePermsDescriptor tablePerms = (TablePermsDescriptor) perm; 404 int changeCount = 0; 405 boolean permissionsLeft = 406 ( removeOnePermission( row, colsChanged, SELECTPRIV_COL_NUM, tablePerms.getSelectPriv()) | 407 removeOnePermission( row, colsChanged, DELETEPRIV_COL_NUM, tablePerms.getDeletePriv()) | 408 removeOnePermission( row, colsChanged, INSERTPRIV_COL_NUM, tablePerms.getInsertPriv()) | 409 removeOnePermission( row, colsChanged, UPDATEPRIV_COL_NUM, tablePerms.getUpdatePriv()) | 410 removeOnePermission( row, colsChanged, REFERENCESPRIV_COL_NUM, tablePerms.getReferencesPriv()) | 411 removeOnePermission( row, colsChanged, TRIGGERPRIV_COL_NUM, tablePerms.getTriggerPriv())); 412 if( ! permissionsLeft) 413 return -1; 414 for( int i = 0; i < colsChanged.length; i++) 415 { 416 if( colsChanged[ i]) 417 changeCount++; 418 } 419 return changeCount; 420 } 422 private boolean removeOnePermission( ExecRow row, boolean[] colsChanged, int column, String permission) 423 throws StandardException 424 { 425 DataValueDescriptor existingPermDVD = row.getColumn( column); 426 char existingPerm = existingPermDVD.getString().charAt(0); 427 428 if( permission.charAt(0) == 'N') return existingPerm != 'N'; if( SanityManager.DEBUG) 431 SanityManager.ASSERT( permission.charAt(0) == 'Y' || permission.charAt(0) == 'y', 432 "Invalid permission passed to SYSTABLEPERMSRowFactory.removeOnePermission"); 433 if( existingPerm != 'N') 434 { 435 existingPermDVD.setValue( "N"); 436 colsChanged[ column - 1] = true; 437 } 438 return false; 439 } 441 444 public void setUUIDOfThePassedDescriptor(ExecRow row, PermissionsDescriptor perm) 445 throws StandardException 446 { 447 DataValueDescriptor existingPermDVD = row.getColumn(TABLEPERMSID_COL_NUM); 448 perm.setUUID(getUUIDFactory().recreateUUID(existingPermDVD.getString())); 449 } 450 } 451 | Popular Tags |