1 21 22 package org.apache.derby.impl.sql.compile; 23 24 import org.apache.derby.iapi.services.compiler.MethodBuilder; 25 26 import org.apache.derby.iapi.sql.ResultSet; 27 28 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 29 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor; 30 import org.apache.derby.iapi.sql.dictionary.TableDescriptor; 31 import org.apache.derby.iapi.sql.compile.CompilerContext; 32 import org.apache.derby.iapi.sql.conn.Authorizer; 33 import org.apache.derby.iapi.reference.SQLState; 34 import org.apache.derby.iapi.error.StandardException; 35 36 import org.apache.derby.impl.sql.compile.ActivationClassBuilder; 37 38 import org.apache.derby.iapi.services.sanity.SanityManager; 39 import org.apache.derby.iapi.reference.ClassName; 40 41 import org.apache.derby.iapi.services.classfile.VMOpcode; 42 import org.apache.derby.catalog.UUID; 43 44 50 51 abstract class DDLStatementNode extends StatementNode 52 { 53 59 public static final int UNKNOWN_TYPE = 0; 60 public static final int ADD_TYPE = 1; 61 public static final int DROP_TYPE = 2; 62 public static final int MODIFY_TYPE = 3; 63 public static final int LOCKING_TYPE = 4; 64 65 66 72 private TableName objectName; 73 private boolean initOk; 74 75 79 boolean implicitCreateSchema; 80 81 82 88 public void init(Object objectName) 89 throws StandardException { 90 initAndCheck(objectName); 91 } 92 93 98 protected void initAndCheck(Object objectName) 99 throws StandardException { 100 101 this.objectName = (TableName) objectName; 102 103 initOk = true; 104 } 105 106 111 public boolean isAtomic() 112 { 113 return true; 114 } 115 116 122 public String getRelativeName() 123 { 124 return objectName.getTableName() ; 125 } 126 127 133 public String getFullName() 134 { 135 return objectName.getFullTableName() ; 136 } 137 138 public final TableName getObjectName() { return objectName; } 139 140 146 147 public String toString() 148 { 149 if (SanityManager.DEBUG) 150 { 151 return ((objectName==null)?"":objectName.toString()) + super.toString(); 152 } 153 else 154 { 155 return ""; 156 } 157 } 158 159 int activationKind() 160 { 161 return StatementNode.NEED_DDL_ACTIVATION; 162 } 163 164 172 173 public final void generate(ActivationClassBuilder acb, 174 MethodBuilder mb) 175 throws StandardException 176 { 177 if (SanityManager.DEBUG) { 178 if (!initOk) 179 SanityManager.THROWASSERT(getClass() + " never called initAndCheck()"); 180 } 181 182 186 acb.pushGetResultSetFactoryExpression(mb); acb.pushThisAsActivation(mb); 189 mb.callMethod(VMOpcode.INVOKEINTERFACE, (String ) null, "getDDLResultSet", ClassName.ResultSet, 1); 190 } 191 192 193 194 205 protected final SchemaDescriptor getSchemaDescriptor() throws StandardException 206 { 207 return getSchemaDescriptor(true); 208 } 209 210 222 protected final SchemaDescriptor getSchemaDescriptor(boolean ownerCheck) 223 throws StandardException 224 { 225 String schemaName = objectName.getSchemaName(); 226 boolean needError = !implicitCreateSchema; 228 SchemaDescriptor sd = getSchemaDescriptor(schemaName, needError); 229 CompilerContext cc = getCompilerContext(); 230 231 if (sd == null) { 232 233 if (schemaName.startsWith("SYS")) 234 throw StandardException.newException( 235 SQLState.LANG_NO_USER_DDL_IN_SYSTEM_SCHEMA, 236 statementToString(), 237 schemaName); 238 239 sd = new SchemaDescriptor(getDataDictionary(), schemaName, 240 (String ) null, (UUID)null, false); 241 242 if (isPrivilegeCollectionRequired()) 243 cc.addRequiredSchemaPriv(schemaName, null, Authorizer.CREATE_SCHEMA_PRIV); 244 } 245 246 if (ownerCheck && isPrivilegeCollectionRequired()) 247 cc.addRequiredSchemaPriv(sd.getSchemaName(), null, 248 Authorizer.MODIFY_SCHEMA_PRIV); 249 250 253 if (sd.isSystemSchema()) 254 { 255 throw StandardException.newException(SQLState.LANG_NO_USER_DDL_IN_SYSTEM_SCHEMA, 256 statementToString(), sd); 257 } 258 return sd; 259 } 260 261 protected final TableDescriptor getTableDescriptor() 262 throws StandardException 263 { 264 return getTableDescriptor(objectName); 265 } 266 267 protected final TableDescriptor getTableDescriptor(UUID tableId) 268 throws StandardException { 269 270 TableDescriptor td = getDataDictionary().getTableDescriptor(tableId); 271 272 td = checkTableDescriptor(td); 273 return td; 274 275 } 276 277 278 287 protected final TableDescriptor getTableDescriptor(TableName tableName) 288 throws StandardException 289 { 290 String schemaName = tableName.getSchemaName(); 291 SchemaDescriptor sd = getSchemaDescriptor(schemaName); 292 293 TableDescriptor td = getTableDescriptor(tableName.getTableName(), sd); 294 295 if (td == null) 296 { 297 throw StandardException.newException(SQLState.LANG_OBJECT_DOES_NOT_EXIST, 298 statementToString(), tableName); 299 } 300 301 302 td = checkTableDescriptor(td); 303 return td; 304 305 } 306 307 private TableDescriptor checkTableDescriptor(TableDescriptor td) 308 throws StandardException 309 { 310 String sqlState = null; 311 312 switch (td.getTableType()) { 313 case TableDescriptor.VTI_TYPE: 314 case TableDescriptor.SYSTEM_TABLE_TYPE: 315 316 320 sqlState = SQLState.LANG_INVALID_OPERATION_ON_SYSTEM_TABLE; 321 break; 322 323 case TableDescriptor.BASE_TABLE_TYPE: 324 329 return lockTableForCompilation(td); 330 331 case TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE: 332 return td; 333 334 337 case TableDescriptor.VIEW_TYPE: 338 sqlState = SQLState.LANG_INVALID_OPERATION_ON_VIEW; 339 break; 340 } 341 342 343 throw StandardException.newException(sqlState, 344 statementToString(), td.getQualifiedName()); 345 346 } 347 348 356 public void bindName( DataDictionary dataDictionary ) 357 throws StandardException 358 { 359 if (objectName != null) 360 objectName.bind( dataDictionary ); 361 } 362 } 363 | Popular Tags |