1 21 22 package org.apache.derby.impl.sql.compile; 23 24 import org.apache.derby.iapi.reference.SQLState; 25 import org.apache.derby.iapi.reference.Limits; 26 27 import org.apache.derby.iapi.services.sanity.SanityManager; 28 29 import org.apache.derby.iapi.error.StandardException; 30 31 import org.apache.derby.iapi.sql.compile.C_NodeTypes; 32 33 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 34 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor; 35 import org.apache.derby.iapi.sql.dictionary.TableDescriptor; 36 37 import org.apache.derby.iapi.sql.execute.ConstantAction; 38 39 import org.apache.derby.impl.sql.execute.ColumnInfo; 40 import org.apache.derby.impl.sql.execute.ConstraintConstantAction; 41 42 48 49 public class AlterTableNode extends DDLStatementNode 50 { 51 public TableElementList tableElementList = null; 53 public char lockGranularity; 54 public boolean compressTable = false; 55 public boolean sequential = false; 56 public int behavior; 58 public TableDescriptor baseTable; 59 60 protected int numConstraints; 61 62 private int changeType = UNKNOWN_TYPE; 63 64 private boolean truncateTable = false; 65 66 68 protected SchemaDescriptor schemaDescriptor = null; 69 protected ColumnInfo[] colInfos = null; 70 protected ConstraintConstantAction[] conActions = null; 71 72 73 79 80 public void init(Object objectName) 81 throws StandardException 82 { 83 84 if (SanityManager.DEBUG) 88 { 89 initAndCheck(objectName); 90 91 truncateTable = true; 92 schemaDescriptor = getSchemaDescriptor(); 93 }else 94 { 95 throw StandardException.newException(SQLState.NOT_IMPLEMENTED, 96 "truncate table"); 97 } 98 } 99 100 108 109 public void init(Object objectName, 110 Object sequential) 111 throws StandardException 112 { 113 initAndCheck(objectName); 114 115 this.sequential = ((Boolean ) sequential).booleanValue(); 116 117 compressTable = true; 118 119 schemaDescriptor = getSchemaDescriptor(); 120 } 121 122 132 133 public void init( 134 Object objectName, 135 Object tableElementList, 136 Object lockGranularity, 137 Object changeType, 138 Object behavior, 139 Object sequential ) 140 throws StandardException 141 { 142 initAndCheck(objectName); 143 this.tableElementList = (TableElementList) tableElementList; 144 this.lockGranularity = ((Character ) lockGranularity).charValue(); 145 146 int[] ct = (int[]) changeType, bh = (int[]) behavior; 147 this.changeType = ct[0]; 148 this.behavior = bh[0]; 149 boolean[] seq = (boolean[]) sequential; 150 this.sequential = seq[0]; 151 switch ( this.changeType ) 152 { 153 case ADD_TYPE: 154 case DROP_TYPE: 155 case MODIFY_TYPE: 156 case LOCKING_TYPE: 157 158 break; 159 160 default: 161 162 throw StandardException.newException(SQLState.NOT_IMPLEMENTED); 163 } 164 165 schemaDescriptor = getSchemaDescriptor(); 166 } 167 168 174 175 public String toString() 176 { 177 if (SanityManager.DEBUG) 178 { 179 return super.toString() + 180 "objectName: " + "\n" + getObjectName() + "\n" + 181 "tableElementList: " + "\n" + tableElementList + "\n" + 182 "lockGranularity: " + "\n" + lockGranularity + "\n" + 183 "compressTable: " + "\n" + compressTable + "\n" + 184 "sequential: " + "\n" + sequential + "\n" + 185 "truncateTable: " + "\n" + truncateTable + "\n"; 186 } 187 else 188 { 189 return ""; 190 } 191 } 192 193 public String statementToString() 194 { 195 if(truncateTable) 196 return "TRUNCATE TABLE"; 197 else 198 return "ALTER TABLE"; 199 } 200 201 public int getChangeType() { return changeType; } 202 203 205 215 public QueryTreeNode bind() throws StandardException 216 { 217 DataDictionary dd = getDataDictionary(); 218 int numCheckConstraints = 0; 219 int numBackingIndexes = 0; 220 221 225 baseTable = getTableDescriptor(); 226 if (baseTable.getTableType() == TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE) 228 { 229 throw StandardException.newException(SQLState.LANG_NOT_ALLOWED_FOR_DECLARED_GLOBAL_TEMP_TABLE); 230 } 231 232 233 getCompilerContext().createDependency(baseTable); 234 235 if (tableElementList != null) 236 { 237 tableElementList.validate(this, dd, baseTable); 238 239 240 if ((tableElementList.countNumberOfColumns() + baseTable.getNumberOfColumns()) > Limits.DB2_MAX_COLUMNS_IN_TABLE) 241 { 242 throw StandardException.newException(SQLState.LANG_TOO_MANY_COLUMNS_IN_TABLE_OR_VIEW, 243 String.valueOf(tableElementList.countNumberOfColumns() + baseTable.getNumberOfColumns()), 244 getRelativeName(), 245 String.valueOf(Limits.DB2_MAX_COLUMNS_IN_TABLE)); 246 } 247 248 numBackingIndexes = tableElementList.countConstraints(DataDictionary.PRIMARYKEY_CONSTRAINT) + 249 tableElementList.countConstraints(DataDictionary.FOREIGNKEY_CONSTRAINT) + 250 tableElementList.countConstraints(DataDictionary.UNIQUE_CONSTRAINT); 251 252 numCheckConstraints = tableElementList.countConstraints( 253 DataDictionary.CHECK_CONSTRAINT); 254 } 255 256 if ((numBackingIndexes + baseTable.getTotalNumberOfIndexes()) > Limits.DB2_MAX_INDEXES_ON_TABLE) 259 { 260 throw StandardException.newException(SQLState.LANG_TOO_MANY_INDEXES_ON_TABLE, 261 String.valueOf(numBackingIndexes + baseTable.getTotalNumberOfIndexes()), 262 getRelativeName(), 263 String.valueOf(Limits.DB2_MAX_INDEXES_ON_TABLE)); 264 } 265 266 if (numCheckConstraints > 0) 267 { 268 276 FromList fromList = (FromList) getNodeFactory().getNode( 277 C_NodeTypes.FROM_LIST, 278 getNodeFactory().doJoinOrderOptimization(), 279 getContextManager()); 280 FromBaseTable table = (FromBaseTable) 281 getNodeFactory().getNode( 282 C_NodeTypes.FROM_BASE_TABLE, 283 getObjectName(), 284 null, 285 null, 286 null, 287 getContextManager()); 288 fromList.addFromTable(table); 289 fromList.bindTables(dd, 290 (FromList) getNodeFactory().getNode( 291 C_NodeTypes.FROM_LIST, 292 getNodeFactory().doJoinOrderOptimization(), 293 getContextManager())); 294 tableElementList.appendNewColumnsToRCL(table); 295 296 299 tableElementList.bindAndValidateCheckConstraints(fromList); 300 301 } 302 303 310 getCompilerContext().createDependency(baseTable); 311 312 return this; 313 } 314 315 322 public boolean referencesSessionSchema() 323 throws StandardException 324 { 325 return isSessionSchema(baseTable.getSchemaName()); 327 } 328 329 334 public ConstantAction makeConstantAction() throws StandardException 335 { 336 prepConstantAction(); 337 338 return getGenericConstantActionFactory().getAlterTableConstantAction(schemaDescriptor, 339 getRelativeName(), 340 baseTable.getUUID(), 341 baseTable.getHeapConglomerateId(), 342 TableDescriptor.BASE_TABLE_TYPE, 343 colInfos, 344 conActions, 345 lockGranularity, 346 compressTable, 347 behavior, 348 sequential, 349 truncateTable); 350 } 351 352 359 public void prepConstantAction() throws StandardException 360 { 361 if (tableElementList != null) 362 { 363 genColumnInfo(); 364 } 365 366 367 368 if (numConstraints > 0) 369 { 370 conActions = new ConstraintConstantAction[numConstraints]; 371 372 tableElementList.genConstraintActions(conActions, getRelativeName(), schemaDescriptor, 373 getDataDictionary()); 374 } 375 } 376 377 380 public void genColumnInfo() 381 { 382 colInfos = new ColumnInfo[tableElementList.countNumberOfColumns()]; 384 385 numConstraints = tableElementList.genColumnInfos(colInfos); 386 } 387 388 389 392 } 393 394 395 396 397 | Popular Tags |