1 21 22 package org.apache.derby.impl.sql.execute; 23 24 import org.apache.derby.iapi.sql.execute.ConstantAction; 25 26 import org.apache.derby.iapi.store.access.TransactionController; 27 28 import org.apache.derby.iapi.sql.conn.LanguageConnectionContext; 29 30 import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator; 31 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 32 import org.apache.derby.iapi.sql.dictionary.DefaultDescriptor; 33 import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptor; 34 import org.apache.derby.iapi.sql.dictionary.ConglomerateDescriptorList; 35 import org.apache.derby.iapi.sql.dictionary.ColumnDescriptor; 36 import org.apache.derby.iapi.sql.dictionary.ColumnDescriptorList; 37 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor; 38 import org.apache.derby.iapi.sql.dictionary.TableDescriptor; 39 40 import org.apache.derby.iapi.sql.depend.Provider; 41 import org.apache.derby.iapi.sql.depend.ProviderInfo; 42 43 import org.apache.derby.iapi.sql.execute.ExecRow; 44 45 import org.apache.derby.iapi.sql.Activation; 46 import org.apache.derby.iapi.sql.depend.DependencyManager; 47 48 import org.apache.derby.iapi.error.StandardException; 49 50 import org.apache.derby.iapi.services.sanity.SanityManager; 51 52 import org.apache.derby.catalog.UUID; 53 54 import org.apache.derby.catalog.types.DefaultInfoImpl; 55 56 import java.util.Properties ; 57 58 64 65 class CreateTableConstantAction extends DDLConstantAction 66 { 67 68 private char lockGranularity; 69 private boolean onCommitDeleteRows; private boolean onRollbackDeleteRows; private String tableName; 72 private String schemaName; 73 private int tableType; 74 private ColumnInfo[] columnInfo; 75 private CreateConstraintConstantAction[] constraintActions; 76 private Properties properties; 77 78 92 CreateTableConstantAction( 93 String schemaName, 94 String tableName, 95 int tableType, 96 ColumnInfo[] columnInfo, 97 CreateConstraintConstantAction[] constraintActions, 98 Properties properties, 99 char lockGranularity, 100 boolean onCommitDeleteRows, 101 boolean onRollbackDeleteRows) 102 { 103 this.schemaName = schemaName; 104 this.tableName = tableName; 105 this.tableType = tableType; 106 this.columnInfo = columnInfo; 107 this.constraintActions = constraintActions; 108 this.properties = properties; 109 this.lockGranularity = lockGranularity; 110 this.onCommitDeleteRows = onCommitDeleteRows; 111 this.onRollbackDeleteRows = onRollbackDeleteRows; 112 113 if (SanityManager.DEBUG) 114 { 115 if (tableType == TableDescriptor.BASE_TABLE_TYPE && lockGranularity != TableDescriptor.TABLE_LOCK_GRANULARITY && 116 lockGranularity != TableDescriptor.ROW_LOCK_GRANULARITY) 117 { 118 SanityManager.THROWASSERT( 119 "Unexpected value for lockGranularity = " + lockGranularity); 120 } 121 if (tableType == TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE && onRollbackDeleteRows == false) 122 { 123 SanityManager.THROWASSERT( 124 "Unexpected value for onRollbackDeleteRows = " + onRollbackDeleteRows); 125 } 126 SanityManager.ASSERT(schemaName != null, "SchemaName is null"); 127 } 128 } 129 130 132 public String toString() 133 { 134 if (tableType == TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE) 135 return constructToString("DECLARE GLOBAL TEMPORARY TABLE ", tableName); 136 else 137 return constructToString("CREATE TABLE ", tableName); 138 } 139 140 142 143 150 public void executeConstantAction( Activation activation ) 151 throws StandardException 152 { 153 TableDescriptor td; 154 UUID toid; 155 SchemaDescriptor schemaDescriptor; 156 ColumnDescriptor columnDescriptor; 157 ExecRow template; 158 159 LanguageConnectionContext lcc = activation.getLanguageConnectionContext(); 160 DataDictionary dd = lcc.getDataDictionary(); 161 DependencyManager dm = dd.getDependencyManager(); 162 TransactionController tc = lcc.getTransactionExecute(); 163 164 165 activation.setForCreateTable(); 166 167 171 template = RowUtil.getEmptyValueRow(columnInfo.length, lcc); 172 173 174 for (int ix = 0; ix < columnInfo.length; ix++) 175 { 176 177 if (columnInfo[ix].defaultValue != null) 178 template.setColumn(ix + 1, columnInfo[ix].defaultValue); 179 else 180 template.setColumn(ix + 1, 181 columnInfo[ix].dataType.getNull() 182 ); 183 } 184 185 190 long conglomId = tc.createConglomerate( 191 "heap", template.getRowArray(), null, properties, tableType == TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE ? 196 (TransactionController.IS_TEMPORARY | TransactionController.IS_KEPT) : TransactionController.IS_DEFAULT); 197 198 207 if ( tableType != TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE ) 208 dd.startWriting(lcc); 209 210 SchemaDescriptor sd; 211 if (tableType == TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE) 212 sd = dd.getSchemaDescriptor(schemaName, tc, true); 213 else 214 sd = DDLConstantAction.getSchemaDescriptorForCreate(dd, activation, schemaName); 215 216 DataDescriptorGenerator ddg = dd.getDataDescriptorGenerator(); 220 221 if ( tableType != TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE ) 222 { 223 td = ddg.newTableDescriptor(tableName, sd, tableType, lockGranularity); 224 dd.addDescriptor(td, sd, DataDictionary.SYSTABLES_CATALOG_NUM, false, tc); 225 } else 226 { 227 td = ddg.newTableDescriptor(tableName, sd, tableType, onCommitDeleteRows, onRollbackDeleteRows); 228 td.setUUID(dd.getUUIDFactory().createUUID()); 229 } 230 toid = td.getUUID(); 231 232 activation.setDDLTableDescriptor(td); 234 235 242 243 int index = 1; 245 246 ColumnDescriptor[] cdlArray = new ColumnDescriptor[columnInfo.length]; 247 for (int ix = 0; ix < columnInfo.length; ix++) 248 { 249 UUID defaultUUID = columnInfo[ix].newDefaultUUID; 250 251 254 if (columnInfo[ix].defaultInfo != null && 255 defaultUUID == null) 256 { 257 defaultUUID = dd.getUUIDFactory().createUUID(); 258 } 259 260 if (columnInfo[ix].autoincInc != 0) columnDescriptor = new ColumnDescriptor( 262 columnInfo[ix].name, 263 index++, 264 columnInfo[ix].dataType, 265 columnInfo[ix].defaultValue, 266 columnInfo[ix].defaultInfo, 267 td, 268 defaultUUID, 269 columnInfo[ix].autoincStart, 270 columnInfo[ix].autoincInc, 271 columnInfo[ix].autoinc_create_or_modify_Start_Increment 272 ); 273 else 274 columnDescriptor = new ColumnDescriptor( 275 columnInfo[ix].name, 276 index++, 277 columnInfo[ix].dataType, 278 columnInfo[ix].defaultValue, 279 columnInfo[ix].defaultInfo, 280 td, 281 defaultUUID, 282 columnInfo[ix].autoincStart, 283 columnInfo[ix].autoincInc 284 ); 285 286 cdlArray[ix] = columnDescriptor; 287 } 288 289 if ( tableType != TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE ) 290 { 291 dd.addDescriptorArray(cdlArray, td, 292 DataDictionary.SYSCOLUMNS_CATALOG_NUM, 293 false, tc); 294 } 295 296 ColumnDescriptorList cdl = td.getColumnDescriptorList(); 298 for (int i = 0; i < cdlArray.length; i++) 299 cdl.add(cdlArray[i]); 300 301 ConglomerateDescriptor cgd = 309 ddg.newConglomerateDescriptor(conglomId, null, false, null, false, null, toid, 310 sd.getUUID()); 311 if ( tableType != TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE ) 312 { 313 dd.addDescriptor(cgd, sd, DataDictionary.SYSCONGLOMERATES_CATALOG_NUM, 314 false, tc); 315 } 316 317 ConglomerateDescriptorList conglomList = td.getConglomerateDescriptorList(); 319 conglomList.add(cgd); 320 321 322 if (constraintActions != null) 323 { 324 328 for (int conIndex = 0; conIndex < constraintActions.length; conIndex++) 329 { 330 if (!constraintActions[conIndex].isForeignKeyConstraint()) 332 { 333 constraintActions[conIndex].executeConstantAction(activation); 334 } 335 } 336 337 for (int conIndex = 0; conIndex < constraintActions.length; conIndex++) 338 { 339 if (constraintActions[conIndex].isForeignKeyConstraint()) 341 { 342 constraintActions[conIndex].executeConstantAction(activation); 343 } 344 } 345 } 346 if ( tableType == TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE ) 347 { 348 lcc.addDeclaredGlobalTempTable(td); 349 } 350 } 351 352 353 } 354 | Popular Tags |