1 21 22 package org.apache.derby.impl.sql.compile; 23 24 import org.apache.derby.iapi.error.StandardException; 25 import org.apache.derby.iapi.services.sanity.SanityManager; 26 import org.apache.derby.iapi.sql.depend.Provider; 27 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor; 28 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 29 import org.apache.derby.iapi.sql.dictionary.TableDescriptor; 30 import org.apache.derby.iapi.sql.dictionary.AliasDescriptor; 31 import org.apache.derby.catalog.types.RoutineAliasInfo; 32 import org.apache.derby.catalog.AliasInfo; 33 import org.apache.derby.iapi.reference.SQLState; 34 import org.apache.derby.impl.sql.execute.PrivilegeInfo; 35 import org.apache.derby.catalog.TypeDescriptor; 36 37 import java.util.HashMap ; 38 import java.util.List ; 39 40 43 public class PrivilegeNode extends QueryTreeNode 44 { 45 public static final int TABLE_PRIVILEGES = 0; 47 public static final int ROUTINE_PRIVILEGES = 1; 48 49 private int objectType; 50 private Object objectOfPrivilege; 51 private TablePrivilegesNode specificPrivileges; 53 60 public void init( Object objectType, Object objectOfPrivilege, Object specificPrivileges) 61 { 62 this.objectType = ((Integer ) objectType).intValue(); 63 this.objectOfPrivilege = objectOfPrivilege; 64 this.specificPrivileges = (TablePrivilegesNode) specificPrivileges; 65 if( SanityManager.DEBUG) 66 { 67 SanityManager.ASSERT( objectOfPrivilege != null, 68 "null privilge object"); 69 switch( this.objectType) 70 { 71 case TABLE_PRIVILEGES: 72 SanityManager.ASSERT( objectOfPrivilege instanceof TableName, 73 "incorrect name type, " + objectOfPrivilege.getClass().getName() 74 + ", used with table privilege"); 75 SanityManager.ASSERT( specificPrivileges != null, 76 "null specific privileges used with table privilege"); 77 break; 78 79 case ROUTINE_PRIVILEGES: 80 SanityManager.ASSERT( objectOfPrivilege instanceof RoutineDesignator, 81 "incorrect name type, " + objectOfPrivilege.getClass().getName() 82 + ", used with table privilege"); 83 SanityManager.ASSERT( specificPrivileges == null, 84 "non-null specific privileges used with execute privilege"); 85 break; 86 87 default: 88 SanityManager.THROWASSERT( "Invalid privilege objectType: " + this.objectType); 89 } 90 } 91 } 93 106 public QueryTreeNode bind( HashMap dependencies, List grantees, boolean isGrant ) throws StandardException 107 { 108 Provider dependencyProvider = null; 109 SchemaDescriptor sd = null; 110 111 switch( objectType) 112 { 113 case TABLE_PRIVILEGES: 114 TableName tableName = (TableName) objectOfPrivilege; 115 sd = getSchemaDescriptor( tableName.getSchemaName(), true); 116 if (sd.isSystemSchema()) 117 throw StandardException.newException(SQLState.AUTH_GRANT_REVOKE_NOT_ALLOWED, tableName.getFullTableName()); 118 119 TableDescriptor td = getTableDescriptor( tableName.getTableName(), sd); 120 if( td == null) 121 throw StandardException.newException( SQLState.LANG_TABLE_NOT_FOUND, tableName); 122 123 if (isSessionSchema(sd.getSchemaName())) 126 throw StandardException.newException(SQLState.LANG_OPERATION_NOT_ALLOWED_ON_SESSION_SCHEMA_TABLES); 127 128 if (td.getTableType() != TableDescriptor.BASE_TABLE_TYPE && 129 td.getTableType() != TableDescriptor.VIEW_TYPE) 130 throw StandardException.newException(SQLState.AUTH_GRANT_REVOKE_NOT_ALLOWED, tableName.getFullTableName()); 131 132 if (grantees.contains(sd.getAuthorizationId())) 134 throw StandardException.newException(SQLState.AUTH_GRANT_REVOKE_NOT_ALLOWED, 135 td.getQualifiedName()); 136 137 specificPrivileges.bind( td, isGrant); 138 dependencyProvider = td; 139 break; 140 141 case ROUTINE_PRIVILEGES: 142 RoutineDesignator rd = (RoutineDesignator) objectOfPrivilege; 143 sd = getSchemaDescriptor( rd.name.getSchemaName(), true); 144 145 if (!sd.isSchemaWithGrantableRoutines()) 146 throw StandardException.newException(SQLState.AUTH_GRANT_REVOKE_NOT_ALLOWED, rd.name.getFullTableName()); 147 148 AliasDescriptor proc = null; 149 RoutineAliasInfo routineInfo = null; 150 java.util.List list = getDataDictionary().getRoutineList( 151 sd.getUUID().toString(), rd.name.getTableName(), 152 rd.isFunction ? AliasInfo.ALIAS_NAME_SPACE_FUNCTION_AS_CHAR : AliasInfo.ALIAS_NAME_SPACE_PROCEDURE_AS_CHAR 153 ); 154 155 if (grantees.contains(sd.getAuthorizationId())) 157 throw StandardException.newException(SQLState.AUTH_GRANT_REVOKE_NOT_ALLOWED, 158 rd.name.getFullTableName()); 159 160 if( rd.paramTypeList == null) 161 { 162 if( list.size() > 1) 164 throw StandardException.newException( ( rd.isFunction ? SQLState.LANG_AMBIGUOUS_FUNCTION_NAME 165 : SQLState.LANG_AMBIGUOUS_PROCEDURE_NAME), 166 rd.name.getFullTableName()); 167 if( list.size() != 1) 168 throw StandardException.newException(SQLState.LANG_NO_SUCH_METHOD_ALIAS, rd.name.getFullTableName()); 169 proc = (AliasDescriptor) list.get(0); 170 } 171 else 172 { 173 boolean found = false; 175 for (int i = list.size() - 1; (!found) && i >= 0; i--) 176 { 177 proc = (AliasDescriptor) list.get(i); 178 179 routineInfo = (RoutineAliasInfo) proc.getAliasInfo(); 180 int parameterCount = routineInfo.getParameterCount(); 181 if (parameterCount != rd.paramTypeList.size()) 182 continue; 183 TypeDescriptor[] parameterTypes = routineInfo.getParameterTypes(); 184 found = true; 185 for( int parmIdx = 0; parmIdx < parameterCount; parmIdx++) 186 { 187 if( ! parameterTypes[parmIdx].equals( rd.paramTypeList.get( parmIdx))) 188 { 189 found = false; 190 break; 191 } 192 } 193 } 194 if( ! found) 195 { 196 StringBuffer sb = new StringBuffer ( rd.name.getFullTableName()); 198 sb.append( "("); 199 for( int i = 0; i < rd.paramTypeList.size(); i++) 200 { 201 if( i > 0) 202 sb.append(","); 203 sb.append( rd.paramTypeList.get(i).toString()); 204 } 205 throw StandardException.newException(SQLState.LANG_NO_SUCH_METHOD_ALIAS, sb.toString()); 206 } 207 } 208 rd.setAliasDescriptor( proc); 209 dependencyProvider = proc; 210 break; 211 } 212 213 if( dependencyProvider != null) 214 { 215 if( dependencies.get( dependencyProvider) == null) 216 { 217 getCompilerContext().createDependency( dependencyProvider); 218 dependencies.put( dependencyProvider, dependencyProvider); 219 } 220 } 221 return this; 222 } 224 227 PrivilegeInfo makePrivilegeInfo() 228 { 229 switch( objectType) 230 { 231 case TABLE_PRIVILEGES: 232 return specificPrivileges.makePrivilegeInfo(); 233 234 case ROUTINE_PRIVILEGES: 235 return ((RoutineDesignator) objectOfPrivilege).makePrivilegeInfo(); 236 } 237 return null; 238 } 239 } 240 | Popular Tags |