KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derby.impl.sql.catalog.SYSTABLEPERMSRowFactory
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.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 /**
44  * Factory for creating a SYSTABLEPERMS row.
45  *
46  */

47
48 public class SYSTABLEPERMSRowFactory extends PermissionsCatalogRowFactory
49 {
50     static final String JavaDoc TABLENAME_STRING = "SYSTABLEPERMS";
51
52     // Column numbers for the SYSTABLEPERMS table. 1 based
53
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 JavaDoc[] uuids =
78     {
79         "b8450018-0103-0e39-b8e7-00000010f010" // catalog UUID
80
,"004b0019-0103-0e39-b8e7-00000010f010" // heap UUID
81
,"c851401a-0103-0e39-b8e7-00000010f010" // index1
82
,"80220011-010c-426e-c599-0000000f1120" // index2
83
,"f81e0010-010c-bc85-060d-000000109ab8" // index3
84
};
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 JavaDoc tablePermID = null;
101         String JavaDoc tableID = null;
102         String JavaDoc selectPriv = null;
103         String JavaDoc deletePriv = null;
104         String JavaDoc insertPriv = null;
105         String JavaDoc updatePriv = null;
106         String JavaDoc referencesPriv = null;
107         String JavaDoc 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     } // end of makeRow
149

150     /** builds a tuple descriptor from a row */
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 JavaDoc tablePermsUUIDString = row.getColumn(TABLEPERMSID_COL_NUM).getString();
161         UUID tablePermsUUID = getUUIDFactory().recreateUUID(tablePermsUUIDString);
162         String JavaDoc tableUUIDString = row.getColumn( TABLEID_COL_NUM).getString();
163         UUID tableUUID = getUUIDFactory().recreateUUID(tableUUIDString);
164         String JavaDoc selectPriv = row.getColumn( SELECTPRIV_COL_NUM).getString();
165         String JavaDoc deletePriv = row.getColumn( DELETEPRIV_COL_NUM).getString();
166         String JavaDoc insertPriv = row.getColumn( INSERTPRIV_COL_NUM).getString();
167         String JavaDoc updatePriv = row.getColumn( UPDATEPRIV_COL_NUM).getString();
168         String JavaDoc referencesPriv = row.getColumn( REFERENCESPRIV_COL_NUM).getString();
169         String JavaDoc 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     } // end of buildDescriptor
196

197     /** builds a column list for the catalog */
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, // precision
208
0, // scale
209
false, // nullability
210
"CHAR",
211                                       true,
212                                       36);
213             columnList[ GRANTEE_COL_NUM - 1] =
214               new SystemColumnImpl( convertIdCase( "GRANTEE"),
215                                     GRANTEE_COL_NUM,
216                                     0, // precision
217
0, // scale
218
false, // nullability
219
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, // precision
226
0, // scale
227
false, // nullability
228
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, // precision
235
0, // scale
236
false, // nullability
237
"CHAR", // dataType
238
true, // built-in type
239
36);
240             columnList[ SELECTPRIV_COL_NUM - 1] =
241               new SystemColumnImpl( convertIdCase( "SELECTPRIV"),
242                                     SELECTPRIV_COL_NUM,
243                                     0, // precision
244
0, // scale
245
false, // nullability
246
"CHAR", // dataType
247
true, // built-in type
248
1);
249             columnList[ DELETEPRIV_COL_NUM - 1] =
250               new SystemColumnImpl( convertIdCase( "DELETEPRIV"),
251                                     DELETEPRIV_COL_NUM,
252                                     0, // precision
253
0, // scale
254
false, // nullability
255
"CHAR", // dataType
256
true, // built-in type
257
1);
258             columnList[ INSERTPRIV_COL_NUM - 1] =
259               new SystemColumnImpl( convertIdCase( "INSERTPRIV"),
260                                     INSERTPRIV_COL_NUM,
261                                     0, // precision
262
0, // scale
263
false, // nullability
264
"CHAR", // dataType
265
true, // built-in type
266
1);
267             columnList[ UPDATEPRIV_COL_NUM - 1] =
268               new SystemColumnImpl( convertIdCase( "UPDATEPRIV"),
269                                     UPDATEPRIV_COL_NUM,
270                                     0, // precision
271
0, // scale
272
false, // nullability
273
"CHAR", // dataType
274
true, // built-in type
275
1);
276             columnList[ REFERENCESPRIV_COL_NUM - 1] =
277               new SystemColumnImpl( convertIdCase( "REFERENCESPRIV"),
278                                     REFERENCESPRIV_COL_NUM,
279                                     0, // precision
280
0, // scale
281
false, // nullability
282
"CHAR", // dataType
283
true, // built-in type
284
1);
285             columnList[ TRIGGERPRIV_COL_NUM - 1] =
286               new SystemColumnImpl( convertIdCase( "TRIGGERPRIV"),
287                                     TRIGGERPRIV_COL_NUM,
288                                     0, // precision
289
0, // scale
290
false, // nullability
291
"CHAR", // dataType
292
true, // built-in type
293
1);
294         }
295         return columnList;
296     } // end of buildColumnList
297

298     /**
299      * builds a key row given for a given index number.
300      */

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             // RESOLVE We do not support the FOR GRANT OPTION, so table permission rows are unique on the
311
// grantee and table UUID columns. The grantor column will always have the name of the owner of the
312
// table. So the index key, used for searching the index, only has grantee and table UUID columns.
313
// It does not have a grantor column.
314
//
315
// If we support FOR GRANT OPTION then there may be multiple table permissions rows for a
316
// (grantee, tableID) combination. We must either handle the multiple rows, which is necessary for
317
// checking permissions, or add a grantor column to the key, which is necessary for granting or revoking
318
// permissions.
319
row = getExecutionFactory().getIndexableRow( 2);
320             row.setColumn(1, getAuthorizationID( perm.getGrantee()));
321             String JavaDoc 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 JavaDoc 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     } // end of buildIndexRow
337

338     public int getPrimaryKeyIndexNumber()
339     {
340         return GRANTEE_TABLE_GRANTOR_INDEX_NUM;
341     }
342
343     /**
344      * Or a set of permissions in with a row from this catalog table
345      *
346      * @param row an existing row
347      * @param perm a permission descriptor of the appropriate class for this PermissionsCatalogRowFactory class.
348      * @param colsChanged An array with one element for each column in row. It is updated to
349      * indicate which columns in row were changed
350      *
351      * @return The number of columns that were changed.
352      *
353      * @exception StandardException standard error policy
354      */

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

370     private int orOnePermission( ExecRow row, boolean[] colsChanged, int column, String JavaDoc 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     } // end of orOnePermission
387

388     /**
389      * Remove a set of permissions from a row from this catalog table
390      *
391      * @param row an existing row
392      * @param perm a permission descriptor of the appropriate class for this PermissionsCatalogRowFactory class.
393      * @param colsChanged An array with one element for each column in row. It is updated to
394      * indicate which columns in row were changed
395      *
396      * @return -1 if there are no permissions left in the row, otherwise the number of columns that were changed.
397      *
398      * @exception StandardException standard error policy
399      */

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     } // end of removePermissions
421

422     private boolean removeOnePermission( ExecRow row, boolean[] colsChanged, int column, String JavaDoc 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') // Don't remove this one
429
return existingPerm != 'N'; // The grantee still has some permissions on this table
430
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     } // end of removeOnePermission
440

441     /**
442      * @see PermissionsCatalogRowFactory#setUUIDOfThePassedDescriptor
443      */

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