1 21 22 package org.apache.derby.impl.sql.compile; 23 24 import org.apache.derby.iapi.services.context.ContextManager; 25 26 import org.apache.derby.iapi.sql.ResultSet; 27 28 import org.apache.derby.iapi.sql.compile.CompilerContext; 29 30 import org.apache.derby.iapi.sql.dictionary.ColumnDescriptor; 31 import org.apache.derby.iapi.sql.dictionary.DataDictionaryContext; 32 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 33 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor; 34 import org.apache.derby.iapi.sql.dictionary.TableDescriptor; 35 import org.apache.derby.iapi.sql.dictionary.DataDescriptorGenerator; 36 import org.apache.derby.iapi.sql.dictionary.ColumnDescriptor; 37 38 import org.apache.derby.iapi.reference.SQLState; 39 import org.apache.derby.iapi.reference.Limits; 40 import org.apache.derby.iapi.reference.Property; 41 42 import org.apache.derby.iapi.sql.execute.ConstantAction; 43 44 import org.apache.derby.iapi.error.StandardException; 45 46 import org.apache.derby.iapi.services.monitor.Monitor; 47 import org.apache.derby.iapi.services.property.PropertyUtil; 48 import org.apache.derby.iapi.services.sanity.SanityManager; 49 50 import org.apache.derby.impl.sql.compile.ActivationClassBuilder; 51 import org.apache.derby.impl.sql.execute.BaseActivation; 52 import org.apache.derby.impl.sql.execute.ColumnInfo; 53 import org.apache.derby.iapi.types.DataTypeDescriptor; 54 import org.apache.derby.catalog.UUID; 55 56 import java.util.Hashtable ; 57 import java.util.Properties ; 58 import java.util.Vector ; 59 60 66 67 public class CreateIndexNode extends DDLStatementNode 68 { 69 boolean unique; 70 DataDictionary dd = null; 71 Properties properties; 72 String indexType; 73 TableName indexName; 74 TableName tableName; 75 Vector columnNameList; 76 String [] columnNames = null; 77 boolean[] isAscending; 78 int[] boundColumnIDs; 79 80 TableDescriptor td; 81 82 95 public void init( 96 Object unique, 97 Object indexType, 98 Object indexName, 99 Object tableName, 100 Object columnNameList, 101 Object properties) 102 throws StandardException 103 { 104 initAndCheck(indexName); 105 this.unique = ((Boolean ) unique).booleanValue(); 106 this.indexType = (String ) indexType; 107 this.indexName = (TableName) indexName; 108 this.tableName = (TableName) tableName; 109 this.columnNameList = (Vector ) columnNameList; 110 this.properties = (Properties ) properties; 111 } 112 113 119 120 public String toString() 121 { 122 if (SanityManager.DEBUG) 123 { 124 return super.toString() + 125 "unique: " + unique + "\n" + 126 "indexType: " + indexType + "\n" + 127 "indexName: " + indexName + "\n" + 128 "tableName: " + tableName + "\n" + 129 "properties: " + properties + "\n"; 130 } 131 else 132 { 133 return ""; 134 } 135 } 136 137 public String statementToString() 138 { 139 return "CREATE INDEX"; 140 } 141 142 143 public boolean getUniqueness() { return unique; } 144 public String getIndexType() { return indexType; } 145 public TableName getIndexName() { return indexName; } 146 public UUID getBoundTableID() { return td.getUUID(); } 147 public Properties getProperties() { return properties; } 148 public TableName getIndexTableName() {return tableName; } 149 public String [] getColumnNames() { return columnNames; } 150 151 public int[] getKeyColumnIDs() { return boundColumnIDs; } 153 public boolean[] getIsAscending() { return isAscending; } 154 155 157 167 168 public QueryTreeNode bind() throws StandardException 169 { 170 CompilerContext cc = getCompilerContext(); 171 DataDictionary dd = getDataDictionary(); 172 SchemaDescriptor sd; 173 int columnCount; 174 175 sd = getSchemaDescriptor(); 176 177 td = getTableDescriptor(tableName); 178 179 if (td.getTableType() == TableDescriptor.GLOBAL_TEMPORARY_TABLE_TYPE) 181 { 182 throw StandardException.newException(SQLState.LANG_NOT_ALLOWED_FOR_DECLARED_GLOBAL_TEMP_TABLE); 183 } 184 185 if (td.getTotalNumberOfIndexes() > Limits.DB2_MAX_INDEXES_ON_TABLE) 187 { 188 throw StandardException.newException(SQLState.LANG_TOO_MANY_INDEXES_ON_TABLE, 189 String.valueOf(td.getTotalNumberOfIndexes()), 190 tableName, 191 String.valueOf(Limits.DB2_MAX_INDEXES_ON_TABLE)); 192 } 193 194 195 verifyAndGetUniqueNames(); 196 197 columnCount = columnNames.length; 198 boundColumnIDs = new int[ columnCount ]; 199 200 for (int i = 0; i < columnCount; i++) 202 { 203 ColumnDescriptor columnDescriptor; 204 205 columnDescriptor = td.getColumnDescriptor(columnNames[i]); 206 if (columnDescriptor == null) 207 { 208 throw StandardException.newException(SQLState.LANG_COLUMN_NOT_FOUND_IN_TABLE, 209 columnNames[i], 210 tableName); 211 } 212 boundColumnIDs[ i ] = columnDescriptor.getPosition(); 213 214 if ( ! columnDescriptor.getType().getTypeId(). 216 orderable(getClassFactory())) 217 { 218 throw StandardException.newException(SQLState.LANG_COLUMN_NOT_ORDERABLE_DURING_EXECUTION, 219 columnDescriptor.getType().getTypeId().getSQLTypeName()); 220 } 221 } 222 223 224 if (columnCount > 16) 225 throw StandardException.newException(SQLState.LANG_TOO_MANY_INDEX_KEY_COLS); 226 227 232 241 242 getCompilerContext().createDependency(td); 243 244 return this; 245 } 246 247 254 public boolean referencesSessionSchema() 255 throws StandardException 256 { 257 return isSessionSchema(td.getSchemaName()); 259 } 260 261 266 public ConstantAction makeConstantAction() throws StandardException 267 { 268 long conglomId = 0; 269 SchemaDescriptor sd = getSchemaDescriptor(); 270 271 int columnCount = columnNames.length; 272 int approxLength = 0; 273 boolean index_has_long_column = false; 274 275 276 for (int i = 0; i < columnCount; i++) 283 { 284 ColumnDescriptor columnDescriptor = td.getColumnDescriptor(columnNames[i]); 285 DataTypeDescriptor dts = columnDescriptor.getType(); 286 approxLength += dts.getTypeId().getApproximateLengthInBytes(dts); 287 } 288 289 290 if (approxLength > Property.IDX_PAGE_SIZE_BUMP_THRESHOLD) 291 { 292 293 if (((properties == null) || 294 (properties.get(Property.PAGE_SIZE_PARAMETER) == null)) && 295 (PropertyUtil.getServiceProperty( 296 getLanguageConnectionContext().getTransactionCompile(), 297 Property.PAGE_SIZE_PARAMETER) == null)) 298 { 299 302 if (properties == null) 303 properties = new Properties (); 304 305 properties.put( 306 Property.PAGE_SIZE_PARAMETER, 307 Property.PAGE_SIZE_DEFAULT_LONG); 308 309 } 310 } 311 312 313 return getGenericConstantActionFactory().getCreateIndexConstantAction(unique, 314 indexType, 315 sd.getSchemaName(), 316 indexName.getTableName(), 317 tableName.getTableName(), 318 td.getUUID(), 319 conglomId, 320 columnNames, 321 isAscending, 322 false, 323 null, 324 properties); 325 } 326 327 333 private void verifyAndGetUniqueNames() 334 throws StandardException 335 { 336 int size = columnNameList.size(); 337 Hashtable ht = new Hashtable (size + 2, (float) .999); 338 columnNames = new String [size]; 339 isAscending = new boolean[size]; 340 341 for (int index = 0; index < size; index++) 342 { 343 346 columnNames[index] = (String ) columnNameList.elementAt(index); 347 if (columnNames[index].endsWith(" ")) 348 { 349 columnNames[index] = columnNames[index].substring(0, columnNames[index].length() - 1); 350 isAscending[index] = false; 351 } 352 else 353 isAscending[index] = true; 354 355 Object object = ht.put(columnNames[index], columnNames[index]); 356 357 if (object != null && 358 ((String ) object).equals(columnNames[index])) 359 { 360 throw StandardException.newException(SQLState.LANG_DUPLICATE_COLUMN_NAME_CREATE_INDEX, columnNames[index]); 361 } 362 } 363 } 364 } 365 | Popular Tags |