KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > impl > sql > catalog > SYSCOLPERMSRowFactory


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.catalog.SYSCOLPERMSRowFactory
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

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 JavaDoc;
51
52 /**
53  * Factory for creating a SYSCOLPERMS row.
54  *
55  */

56
57 class SYSCOLPERMSRowFactory extends PermissionsCatalogRowFactory
58 {
59     static final String JavaDoc TABLENAME_STRING = "SYSCOLPERMS";
60
61     // Column numbers for the SYSCOLPERMS table. 1 based
62
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 JavaDoc[] uuids =
84     {
85         "286cc01e-0103-0e39-b8e7-00000010f010" // catalog UUID
86
,"6074401f-0103-0e39-b8e7-00000010f010" // heap UUID
87
,"787c0020-0103-0e39-b8e7-00000010f010" // index1
88
,"c9a3808d-010c-42a2-ae15-0000000f67f8" //index2
89
,"80220011-010c-bc85-060d-000000109ab8" //index3
90
};
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 JavaDoc colPermID = null;
105         DataValueDescriptor grantee = null;
106         DataValueDescriptor grantor = null;
107         String JavaDoc tableID = null;
108         String JavaDoc 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 JavaDoc) columns));
139         return row;
140     } // end of makeRow
141

142     /** builds a tuple descriptor from a row */
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 JavaDoc colPermsUUIDString = row.getColumn( COLPERMSID_COL_NUM).getString();
153         UUID colPermsUUID = getUUIDFactory().recreateUUID(colPermsUUIDString);
154         String JavaDoc tableUUIDString = row.getColumn( TABLEID_COL_NUM).getString();
155         UUID tableUUID = getUUIDFactory().recreateUUID(tableUUIDString);
156         String JavaDoc 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     } // end of buildDescriptor
172

173     /** builds a column list for the catalog */
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     /**
188      * builds an index key row for a given index number.
189      */

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             // RESOLVE We do not support the FOR GRANT OPTION, so column permission rows are unique on the
200
// grantee, table UUID, and type columns. The grantor column will always have the name of the owner of the
201
// table. So the index key, used for searching the index, only has grantee, table UUID, and type columns.
202
// It does not have a grantor column.
203
//
204
// If we support FOR GRANT OPTION then there may be multiple table permissions rows for a
205
// (grantee, tableID, type) combination. We must either handle the multiple rows, which is necessary for
206
// checking permissions, or add a grantor column to the key, which is necessary for granting or revoking
207
// permissions.
208
row = getExecutionFactory().getIndexableRow( 3);
209             row.setColumn(1, getAuthorizationID( perm.getGrantee()));
210             ColPermsDescriptor colPerms = (ColPermsDescriptor) perm;
211             String JavaDoc 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 JavaDoc 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     } // end of buildIndexKeyRow
229

230     public int getPrimaryKeyIndexNumber()
231     {
232         return GRANTEE_TABLE_TYPE_GRANTOR_INDEX_NUM;
233     }
234
235     /**
236      * Or a set of permissions in with a row from this catalog table
237      *
238      * @param row an existing row
239      * @param perm a permission descriptor of the appropriate class for this PermissionsCatalogRowFactory class.
240      * @param colsChanged An array with one element for each column in row. It is updated to
241      * indicate which columns in row were changed
242      *
243      * @return The number of columns that were changed.
244      *
245      * @exception StandardException standard error policy
246      */

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     } // end of orPermissions
270

271     /**
272      * Remove a set of permissions from a row from this catalog table
273      *
274      * @param row an existing row
275      * @param perm a permission descriptor of the appropriate class for this PermissionsCatalogRowFactory class.
276      * @param colsChanged An array with one element for each column in row. It is updated to
277      * indicate which columns in row were changed
278      *
279      * @return -1 if there are no permissions left in the row, otherwise the number of columns that were changed.
280      *
281      * @exception StandardException standard error policy
282      */

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             // remove all of them
290
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; // No column privileges left
308
return 1; // A change, but there are some privileges left
309
}
310         return 0; // no change
311
} // end of removePermissions
312

313     /**
314      * @see PermissionsCatalogRowFactory#setUUIDOfThePassedDescriptor
315      */

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