1 21 22 package org.apache.derby.impl.sql.catalog; 23 24 import org.apache.derby.iapi.sql.dictionary.ColPermsDescriptor; 25 import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator; 26 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 27 import org.apache.derby.iapi.sql.dictionary.StatisticsDescriptor; 28 import org.apache.derby.iapi.sql.dictionary.SystemColumn; 29 import org.apache.derby.iapi.sql.dictionary.TupleDescriptor; 30 import org.apache.derby.iapi.sql.dictionary.PermissionsDescriptor; 31 32 import org.apache.derby.iapi.error.StandardException; 33 34 import org.apache.derby.iapi.services.sanity.SanityManager; 35 import org.apache.derby.iapi.sql.execute.ExecRow; 36 import org.apache.derby.iapi.sql.execute.ExecIndexRow; 37 import org.apache.derby.iapi.sql.execute.ExecutionFactory; 38 import org.apache.derby.iapi.types.TypeId; 39 import org.apache.derby.iapi.types.DataValueFactory; 40 import org.apache.derby.iapi.types.RowLocation; 41 import org.apache.derby.iapi.types.DataTypeDescriptor; 42 import org.apache.derby.iapi.types.DataValueDescriptor; 43 import org.apache.derby.iapi.types.NumberDataValue; 44 import org.apache.derby.iapi.types.StringDataValue; 45 import org.apache.derby.iapi.services.uuid.UUIDFactory; 46 import org.apache.derby.catalog.UUID; 47 import org.apache.derby.catalog.Statistics; 48 import org.apache.derby.iapi.services.io.FormatableBitSet; 49 50 import java.sql.Timestamp ; 51 52 56 57 class SYSCOLPERMSRowFactory extends PermissionsCatalogRowFactory 58 { 59 static final String TABLENAME_STRING = "SYSCOLPERMS"; 60 61 private static final int COLPERMSID_COL_NUM = 1; 63 private static final int GRANTEE_COL_NUM = 2; 64 private static final int GRANTOR_COL_NUM = 3; 65 private static final int TABLEID_COL_NUM = 4; 66 private static final int TYPE_COL_NUM = 5; 67 protected static final int COLUMNS_COL_NUM = 6; 68 private static final int COLUMN_COUNT = 6; 69 70 static final int GRANTEE_TABLE_TYPE_GRANTOR_INDEX_NUM = 0; 71 static final int COLPERMSID_INDEX_NUM = 1; 72 static final int TABLEID_INDEX_NUM = 2; 73 protected static final int TOTAL_NUM_OF_INDEXES = 3; 74 private static final int[][] indexColumnPositions = 75 { 76 { GRANTEE_COL_NUM, TABLEID_COL_NUM, TYPE_COL_NUM, GRANTOR_COL_NUM}, 77 { COLPERMSID_COL_NUM }, 78 { TABLEID_COL_NUM } 79 }; 80 81 private static final boolean[] indexUniqueness = { true, true, false}; 82 83 private static final String [] uuids = 84 { 85 "286cc01e-0103-0e39-b8e7-00000010f010" ,"6074401f-0103-0e39-b8e7-00000010f010" ,"787c0020-0103-0e39-b8e7-00000010f010" ,"c9a3808d-010c-42a2-ae15-0000000f67f8" ,"80220011-010c-bc85-060d-000000109ab8" }; 91 92 private SystemColumn[] columnList; 93 94 SYSCOLPERMSRowFactory(UUIDFactory uuidf, ExecutionFactory ef, DataValueFactory dvf, 95 boolean convertIdToLower) 96 { 97 super(uuidf,ef,dvf,convertIdToLower); 98 initInfo(COLUMN_COUNT, TABLENAME_STRING, indexColumnPositions, indexUniqueness, uuids); 99 } 100 101 public ExecRow makeRow(TupleDescriptor td, TupleDescriptor parent) throws StandardException 102 { 103 UUID oid; 104 String colPermID = null; 105 DataValueDescriptor grantee = null; 106 DataValueDescriptor grantor = null; 107 String tableID = null; 108 String type = null; 109 FormatableBitSet columns = null; 110 111 if( td == null) 112 { 113 grantee = getNullAuthorizationID(); 114 grantor = getNullAuthorizationID(); 115 } 116 else 117 { 118 ColPermsDescriptor cpd = (ColPermsDescriptor) td; 119 oid = cpd.getUUID(); 120 if ( oid == null ) 121 { 122 oid = getUUIDFactory().createUUID(); 123 cpd.setUUID(oid); 124 } 125 colPermID = oid.toString(); 126 grantee = getAuthorizationID( cpd.getGrantee()); 127 grantor = getAuthorizationID( cpd.getGrantor()); 128 tableID = cpd.getTableUUID().toString(); 129 type = cpd.getType(); 130 columns = cpd.getColumns(); 131 } 132 ExecRow row = getExecutionFactory().getValueRow( COLUMN_COUNT); 133 row.setColumn( COLPERMSID_COL_NUM, dvf.getCharDataValue(colPermID)); 134 row.setColumn( GRANTEE_COL_NUM, grantee); 135 row.setColumn( GRANTOR_COL_NUM, grantor); 136 row.setColumn( TABLEID_COL_NUM, dvf.getCharDataValue( tableID)); 137 row.setColumn( TYPE_COL_NUM, dvf.getCharDataValue( type)); 138 row.setColumn( COLUMNS_COL_NUM, dvf.getDataValue( (Object ) columns)); 139 return row; 140 } 142 143 public TupleDescriptor buildDescriptor(ExecRow row, 144 TupleDescriptor parentTuple, 145 DataDictionary dataDictionary) 146 throws StandardException 147 { 148 if( SanityManager.DEBUG) 149 SanityManager.ASSERT( row.nColumns() == COLUMN_COUNT, 150 "Wrong size row passed to SYSCOLPERMSRowFactory.buildDescriptor"); 151 152 String colPermsUUIDString = row.getColumn( COLPERMSID_COL_NUM).getString(); 153 UUID colPermsUUID = getUUIDFactory().recreateUUID(colPermsUUIDString); 154 String tableUUIDString = row.getColumn( TABLEID_COL_NUM).getString(); 155 UUID tableUUID = getUUIDFactory().recreateUUID(tableUUIDString); 156 String type = row.getColumn( TYPE_COL_NUM).getString(); 157 FormatableBitSet columns = (FormatableBitSet) row.getColumn( COLUMNS_COL_NUM).getObject(); 158 if( SanityManager.DEBUG) 159 SanityManager.ASSERT( "s".equals( type) || "S".equals( type) || 160 "u".equals( type) || "U".equals( type) || 161 "r".equals( type) || "R".equals( type), 162 "Invalid type passed to SYSCOLPERMSRowFactory.buildDescriptor"); 163 164 ColPermsDescriptor colPermsDesc = 165 new ColPermsDescriptor( dataDictionary, 166 getAuthorizationID( row, GRANTEE_COL_NUM), 167 getAuthorizationID( row, GRANTOR_COL_NUM), 168 tableUUID, type, columns); 169 colPermsDesc.setUUID(colPermsUUID); 170 return colPermsDesc; 171 } 173 174 public SystemColumn[] buildColumnList() 175 { 176 return new SystemColumn[] { 177 SystemColumnImpl.getUUIDColumn("COLPERMSID", false), 178 SystemColumnImpl.getIdentifierColumn("GRANTEE", false), 179 SystemColumnImpl.getIdentifierColumn("GRANTOR", false), 180 SystemColumnImpl.getUUIDColumn("TABLEID", false), 181 SystemColumnImpl.getIndicatorColumn("TYPE"), 182 SystemColumnImpl.getJavaColumn("COLUMNS", 183 "org.apache.derby.iapi.services.io.FormatableBitSet", false) 184 }; 185 } 186 187 190 public ExecIndexRow buildIndexKeyRow( int indexNumber, 191 PermissionsDescriptor perm) 192 throws StandardException 193 { 194 ExecIndexRow row = null; 195 196 switch( indexNumber) 197 { 198 case GRANTEE_TABLE_TYPE_GRANTOR_INDEX_NUM: 199 row = getExecutionFactory().getIndexableRow( 3); 209 row.setColumn(1, getAuthorizationID( perm.getGrantee())); 210 ColPermsDescriptor colPerms = (ColPermsDescriptor) perm; 211 String tableUUIDStr = colPerms.getTableUUID().toString(); 212 row.setColumn(2, getDataValueFactory().getCharDataValue( tableUUIDStr)); 213 row.setColumn(3, getDataValueFactory().getCharDataValue( colPerms.getType())); 214 break; 215 case COLPERMSID_INDEX_NUM: 216 row = getExecutionFactory().getIndexableRow( 1); 217 String colPermsUUIDStr = perm.getObjectID().toString(); 218 row.setColumn(1, getDataValueFactory().getCharDataValue( colPermsUUIDStr)); 219 break; 220 case TABLEID_INDEX_NUM: 221 row = getExecutionFactory().getIndexableRow( 1); 222 colPerms = (ColPermsDescriptor) perm; 223 tableUUIDStr = colPerms.getTableUUID().toString(); 224 row.setColumn(1, getDataValueFactory().getCharDataValue( tableUUIDStr)); 225 break; 226 } 227 return row; 228 } 230 public int getPrimaryKeyIndexNumber() 231 { 232 return GRANTEE_TABLE_TYPE_GRANTOR_INDEX_NUM; 233 } 234 235 247 public int orPermissions( ExecRow row, PermissionsDescriptor perm, boolean[] colsChanged) 248 throws StandardException 249 { 250 ColPermsDescriptor colPerms = (ColPermsDescriptor) perm; 251 FormatableBitSet existingColSet = (FormatableBitSet) row.getColumn( COLUMNS_COL_NUM).getObject(); 252 FormatableBitSet newColSet = colPerms.getColumns(); 253 254 boolean changed = false; 255 for( int i = newColSet.anySetBit(); i >= 0; i = newColSet.anySetBit(i)) 256 { 257 if( ! existingColSet.get(i)) 258 { 259 existingColSet.set( i); 260 changed = true; 261 } 262 } 263 if( changed) 264 { 265 colsChanged[ COLUMNS_COL_NUM - 1] = true; 266 return 1; 267 } 268 return 0; 269 } 271 283 public int removePermissions( ExecRow row, PermissionsDescriptor perm, boolean[] colsChanged) 284 throws StandardException 285 { 286 ColPermsDescriptor colPerms = (ColPermsDescriptor) perm; 287 FormatableBitSet removeColSet = colPerms.getColumns(); 288 if( removeColSet == null) 289 return -1; 291 292 FormatableBitSet existingColSet = (FormatableBitSet) row.getColumn( COLUMNS_COL_NUM).getObject(); 293 294 boolean changed = false; 295 for( int i = removeColSet.anySetBit(); i >= 0; i = removeColSet.anySetBit(i)) 296 { 297 if( existingColSet.get(i)) 298 { 299 existingColSet.clear( i); 300 changed = true; 301 } 302 } 303 if( changed) 304 { 305 colsChanged[ COLUMNS_COL_NUM - 1] = true; 306 if( existingColSet.anySetBit() < 0) 307 return -1; return 1; } 310 return 0; } 313 316 public void setUUIDOfThePassedDescriptor(ExecRow row, PermissionsDescriptor perm) 317 throws StandardException 318 { 319 DataValueDescriptor existingPermDVD = row.getColumn(COLPERMSID_COL_NUM); 320 perm.setUUID(getUUIDFactory().recreateUUID(existingPermDVD.getString())); 321 } 322 } 323 | Popular Tags |