1 21 22 package org.apache.derby.impl.sql.compile; 23 24 import org.apache.derby.iapi.reference.Property; 25 import org.apache.derby.iapi.reference.SQLState; 26 import org.apache.derby.iapi.reference.Limits; 27 28 import org.apache.derby.iapi.services.property.PropertyUtil; 29 import org.apache.derby.iapi.services.sanity.SanityManager; 30 31 import org.apache.derby.iapi.sql.execute.ConstantAction; 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.compile.C_NodeTypes; 38 39 import org.apache.derby.iapi.error.StandardException; 40 41 import org.apache.derby.impl.sql.execute.ColumnInfo; 42 import org.apache.derby.impl.sql.execute.CreateConstraintConstantAction; 43 import org.apache.derby.iapi.types.DataTypeDescriptor; 44 import java.util.Properties ; 45 46 52 53 public class CreateTableNode extends DDLStatementNode 54 { 55 private char lockGranularity; 56 private boolean onCommitDeleteRows; private boolean onRollbackDeleteRows; private Properties properties; 59 private TableElementList tableElementList; 60 protected int tableType; 62 74 75 public void init( 76 Object newObjectName, 77 Object tableElementList, 78 Object properties, 79 Object lockGranularity) 80 throws StandardException 81 { 82 tableType = TableDescriptor.BASE_TABLE_TYPE; 83 this.lockGranularity = ((Character ) lockGranularity).charValue(); 84 implicitCreateSchema = true; 85 86 if (SanityManager.DEBUG) 87 { 88 if (this.lockGranularity != TableDescriptor.TABLE_LOCK_GRANULARITY && 89 this.lockGranularity != TableDescriptor.ROW_LOCK_GRANULARITY) 90 { 91 SanityManager.THROWASSERT( 92 "Unexpected value for lockGranularity = " + this.lockGranularity); 93 } 94 } 95 96 initAndCheck(newObjectName); 97 this.tableElementList = (TableElementList) tableElementList; 98 this.properties = (Properties ) properties; 99 } 100 101 114 115 public void init( 116 Object newObjectName, 117 Object tableElementList, 118 Object properties, 119 Object onCommitDeleteRows, 120 Object onRollbackDeleteRows) 121 throws StandardException 122 { 123 tableType = TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE; 124 newObjectName = tempTableSchemaNameCheck(newObjectName); 125 this.onCommitDeleteRows = ((Boolean ) onCommitDeleteRows).booleanValue(); 126 this.onRollbackDeleteRows = ((Boolean ) onRollbackDeleteRows).booleanValue(); 127 initAndCheck(newObjectName); 128 this.tableElementList = (TableElementList) tableElementList; 129 this.properties = (Properties ) properties; 130 131 if (SanityManager.DEBUG) 132 { 133 if (this.onRollbackDeleteRows == false) 134 { 135 SanityManager.THROWASSERT( 136 "Unexpected value for onRollbackDeleteRows = " + this.onRollbackDeleteRows); 137 } 138 } 139 } 140 141 146 private Object tempTableSchemaNameCheck(Object objectName) 147 throws StandardException { 148 TableName tempTableName = (TableName) objectName; 149 if (tempTableName != null) 150 { 151 if (tempTableName.getSchemaName() == null) 152 tempTableName.setSchemaName(SchemaDescriptor.STD_DECLARED_GLOBAL_TEMPORARY_TABLES_SCHEMA_NAME); else if (!(isSessionSchema(tempTableName.getSchemaName()))) 154 throw StandardException.newException(SQLState.LANG_DECLARED_GLOBAL_TEMP_TABLE_ONLY_IN_SESSION_SCHEMA); 155 } 156 return(tempTableName); 157 } 158 159 165 166 public String toString() 167 { 168 if (SanityManager.DEBUG) 169 { 170 String tempString = "tableElementList: " + "\n" + tableElementList + "\n"; 171 if (tableType == TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE) 172 { 173 tempString = tempString + "onCommitDeleteRows: " + "\n" + onCommitDeleteRows + "\n"; 174 tempString = tempString + "onRollbackDeleteRows: " + "\n" + onRollbackDeleteRows + "\n"; 175 } else 176 tempString = tempString + "properties: " + "\n" + properties + "\n" + "lockGranularity: " + "\n" + lockGranularity + "\n"; 177 return super.toString() + tempString; 178 } 179 else 180 { 181 return ""; 182 } 183 } 184 185 public String statementToString() 186 { 187 if (tableType == TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE) 188 return "DECLARE GLOBAL TEMPORARY TABLE"; 189 else 190 return "CREATE TABLE"; 191 } 192 193 195 204 205 public QueryTreeNode bind() throws StandardException 206 { 207 DataDictionary dataDictionary = getDataDictionary(); 208 int numPrimaryKeys = 0; 209 int numCheckConstraints = 0; 210 int numReferenceConstraints = 0; 211 int numUniqueConstraints = 0; 212 213 tableElementList.validate(this, dataDictionary, (TableDescriptor) null); 214 215 216 if (tableElementList.countNumberOfColumns() > Limits.DB2_MAX_COLUMNS_IN_TABLE) 217 { 218 throw StandardException.newException(SQLState.LANG_TOO_MANY_COLUMNS_IN_TABLE_OR_VIEW, 219 String.valueOf(tableElementList.countNumberOfColumns()), 220 getRelativeName(), 221 String.valueOf(Limits.DB2_MAX_COLUMNS_IN_TABLE)); 222 } 223 224 numPrimaryKeys = tableElementList.countConstraints( 225 DataDictionary.PRIMARYKEY_CONSTRAINT); 226 227 228 if (numPrimaryKeys > 1) 229 { 230 throw StandardException.newException(SQLState.LANG_TOO_MANY_PRIMARY_KEY_CONSTRAINTS, getRelativeName()); 231 } 232 233 234 numCheckConstraints = tableElementList.countConstraints( 235 DataDictionary.CHECK_CONSTRAINT); 236 237 numReferenceConstraints = tableElementList.countConstraints( 238 DataDictionary.FOREIGNKEY_CONSTRAINT); 239 240 numUniqueConstraints = tableElementList.countConstraints( 241 DataDictionary.UNIQUE_CONSTRAINT); 242 243 if ((tableType == TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE) && 245 (numPrimaryKeys > 0 || numCheckConstraints > 0 || numReferenceConstraints > 0 || numUniqueConstraints > 0)) 246 throw StandardException.newException(SQLState.LANG_NOT_ALLOWED_FOR_DECLARED_GLOBAL_TEMP_TABLE); 247 248 if ((numPrimaryKeys + numReferenceConstraints + numUniqueConstraints) > Limits.DB2_MAX_INDEXES_ON_TABLE) 251 { 252 throw StandardException.newException(SQLState.LANG_TOO_MANY_INDEXES_ON_TABLE, 253 String.valueOf(numPrimaryKeys + numReferenceConstraints + numUniqueConstraints), 254 getRelativeName(), 255 String.valueOf(Limits.DB2_MAX_INDEXES_ON_TABLE)); 256 } 257 258 if (numCheckConstraints > 0) 259 { 260 268 FromList fromList = (FromList) getNodeFactory().getNode( 269 C_NodeTypes.FROM_LIST, 270 getNodeFactory().doJoinOrderOptimization(), 271 getContextManager()); 272 FromBaseTable table = (FromBaseTable) 273 getNodeFactory().getNode( 274 C_NodeTypes.FROM_BASE_TABLE, 275 getObjectName(), 276 null, 277 null, 278 null, 279 getContextManager()); 280 table.setTableNumber(0); 281 fromList.addFromTable(table); 282 table.setResultColumns((ResultColumnList) getNodeFactory().getNode( 283 C_NodeTypes.RESULT_COLUMN_LIST, 284 getContextManager())); 285 tableElementList.appendNewColumnsToRCL(table); 286 287 290 tableElementList.bindAndValidateCheckConstraints(fromList); 291 } 292 293 return this; 294 } 295 296 303 public boolean referencesSessionSchema() 304 throws StandardException 305 { 306 return isSessionSchema(getSchemaDescriptor()); 308 } 309 310 315 public ConstantAction makeConstantAction() throws StandardException 316 { 317 TableElementList coldefs = tableElementList; 318 319 ColumnInfo[] colInfos = new ColumnInfo[coldefs.countNumberOfColumns()]; 321 322 int numConstraints = coldefs.genColumnInfos(colInfos); 323 324 325 CreateConstraintConstantAction[] conActions = null; 326 327 SchemaDescriptor sd = getSchemaDescriptor(); 328 329 if (numConstraints > 0) 330 { 331 conActions = 332 new CreateConstraintConstantAction[numConstraints]; 333 334 coldefs.genConstraintActions( 335 conActions, getRelativeName(), sd, getDataDictionary()); 336 } 337 338 343 boolean table_has_long_column = false; 344 int approxLength = 0; 345 346 for (int i = 0; i < colInfos.length; i++) 347 { 348 DataTypeDescriptor dts = colInfos[i].dataType; 349 if (dts.getTypeId().isLongConcatableTypeId()) 350 { 351 table_has_long_column = true; 352 break; 353 } 354 355 approxLength += dts.getTypeId().getApproximateLengthInBytes(dts); 356 } 357 358 if (table_has_long_column || (approxLength > Property.TBL_PAGE_SIZE_BUMP_THRESHOLD)) 359 { 360 if (((properties == null) || 361 (properties.get(Property.PAGE_SIZE_PARAMETER) == null)) && 362 (PropertyUtil.getServiceProperty( 363 getLanguageConnectionContext().getTransactionCompile(), 364 Property.PAGE_SIZE_PARAMETER) == null)) 365 { 366 369 if (properties == null) 370 properties = new Properties (); 371 372 properties.put( 373 Property.PAGE_SIZE_PARAMETER, 374 Property.PAGE_SIZE_DEFAULT_LONG); 375 } 376 } 377 378 return( 379 getGenericConstantActionFactory().getCreateTableConstantAction( 380 sd.getSchemaName(), 381 getRelativeName(), 382 tableType, 383 colInfos, 384 conActions, 385 properties, 386 lockGranularity, 387 onCommitDeleteRows, 388 onRollbackDeleteRows)); 389 } 390 } 391 | Popular Tags |