| 1 package com.daffodilwoods.daffodildb.server.sql99.ddl.descriptors; 2 3 import java.util.*; 4 5 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*; 6 import com.daffodilwoods.daffodildb.server.serversystem.*; 7 import com.daffodilwoods.daffodildb.server.serversystem.dmlvalidation.constraintsystem.*; 8 import com.daffodilwoods.daffodildb.server.sql99.*; 9 import com.daffodilwoods.daffodildb.server.sql99.common.*; 10 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 11 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*; 12 import com.daffodilwoods.database.general.*; 13 import com.daffodilwoods.database.resource.*; 14 15 public class TableDescriptor extends Descriptor { 16 19 public HashMap constraintMap = new HashMap(); 20 21 24 public boolean isAlterTable; 25 26 28 30 38 public String table_catalog; 39 public String table_schema; 40 public String table_name; 41 42 47 public String table_type; 48 49 52 public String self_referencing_column_name; 53 public String reference_generation; 54 55 58 public String user_defined_type_catalog; 59 public String user_defined_type_schema; 60 public String user_defined_type_name; 61 62 65 public String commitAction; 66 67 70 public String country_code; 71 72 75 public String language_code; 76 77 80 81 82 public SchemaDescriptor schemaDescriptor; 83 84 private int columnCount = 0; 85 private TreeMap columnDescriptors = new TreeMap(String.CASE_INSENSITIVE_ORDER); 86 private TreeMap columnConstraintDescriptors = new TreeMap(String.CASE_INSENSITIVE_ORDER); 87 private boolean flag = false; 88 public ArrayList indexesList; 89 90 92 public boolean primarKeyCreated; 93 94 96 private boolean isAutoIncrementAlreadyDefined; 97 98 public TableDescriptor() { 102 } 103 104 public TableDescriptor(String table_catalog0, String table_schema0, 105 String table_name0) throws DException { 106 table_catalog = table_catalog0; 107 table_name = table_name0; 108 table_schema = table_schema0; 109 } 110 111 116 public void load(_ServerSession serverSession) throws DException { 117 DataDictionary dd = (DataDictionary) serverSession.getDataDictionary(); 118 _SelectQueryIterator iter = (_SelectQueryIterator) dd.getPreparedStatementGetter(). 119 getTablesTableExecuter().executeForFresh( (Object []) getParameters()); 120 if (!iter.first()) { 121 throw new DException("DSE959", new Object [] {getQualifiedTableName()}); 122 } 123 Object [] obj = (Object []) iter.getObject(); 124 table_type = (String ) obj[SystemTablesFields.tables_table_type]; 125 self_referencing_column_name = (String ) obj[SystemTablesFields. 126 tables_self_referencing_column_name]; 127 reference_generation = (String ) obj[SystemTablesFields. 128 tables_reference_generation]; 129 commitAction = (String ) obj[SystemTablesFields.tables_commitAction]; 130 country_code = (String ) obj[SystemTablesFields.tables_country_code]; 131 language_code = (String ) obj[SystemTablesFields.tables_language_code]; 132 loadColumnDescriptors(serverSession); 133 } 134 135 143 public void loadColumnDescriptors(_ServerSession serverSession) throws 144 DException { 145 _SelectQueryIterator columnsIterator = (_SelectQueryIterator) ( (DataDictionary) serverSession.getDataDictionary()).getPreparedStatementGetter().getColumnsExecuter().executeForFresh(getParameters()); 146 if (columnsIterator.first()) { 147 _SelectQueryIterator dataTypeIterator = (_SelectQueryIterator) ( (DataDictionary) serverSession.getDataDictionary()).getPreparedStatementGetter().getDataTypeExecuter().executeForFresh(getParameters()); 148 if (!dataTypeIterator.first()) { 149 throw new DException("DSE310", null); 150 } 151 boolean flag = false; 152 do { 153 columnCount++; 154 ColumnDescriptor columnDescriptor = new ColumnDescriptor(); 155 columnDescriptor.tableDescriptor = this; 156 columnDescriptor.loadDataFromRecord(columnsIterator); 157 DataTypeDescriptor dataTypeDescriptor = new DataTypeDescriptor(); 158 dataTypeDescriptor.loadDataFromRecord(dataTypeIterator); 159 columnDescriptor.dataTypeDescriptor = dataTypeDescriptor; 160 columnDescriptors.put(columnDescriptor.column_name.toLowerCase(), 161 columnDescriptor); 162 boolean boo = columnsIterator.next(); 163 flag = dataTypeIterator.next(); 164 if (boo ^ flag) { 165 throw new DException("DSE5017", null); 166 } 167 } while (flag); 168 } 169 } 170 171 181 182 187 public void save(_ServerSession serverSession) throws DException { 188 if ( (!table_type.equalsIgnoreCase(SqlKeywords.VIEW)) && 189 table_catalog.equalsIgnoreCase("system")) { 190 table_type = "System Table"; 191 } 192 Object [] columnValues = new Object [] { 193 table_catalog, table_schema, table_name, 194 table_type, self_referencing_column_name, reference_generation, 195 user_defined_type_catalog, user_defined_type_schema, 196 user_defined_type_name, commitAction, country_code, language_code}; 197 try { 198 SqlSchemaConstants.insert(serverSession, 199 SqlSchemaConstants.tables_TableName, null, 200 columnValues); 201 } catch (PrimaryConstraintException de) { 202 DException tde = new DException("DSE1135", new Object [] {getTableName()}); 203 throw tde; 204 } catch (SizeMisMatchException de) { 205 if (de.getDseCode().equals("DSE773")) { 206 DException tde = new DException("DSE8103", null); 207 throw tde; 208 } 209 } catch (DException de) { 210 if (de.getDseCode().equals("DSE1255")) { 211 DException tde = new DException("DSE1135", new Object [] {getTableName()}); 212 throw tde; 213 } 214 if (de.getDseCode().equals("DSE773")) { 215 DException tde = new DException("DSE8103", null); 216 throw tde; 217 } 218 throw de; 219 } 220 } 221 222 224 public String getTableName() { 225 return table_catalog + "." + table_schema + "." + table_name; 226 } 227 228 230 public QualifiedIdentifier getQualifiedTableName() { 231 return new QualifiedIdentifier(table_catalog, table_schema, table_name); 232 } 233 234 237 238 241 public QualifiedIdentifier isTableReferencedFromViews(_ServerSession serverSession) throws DException { 242 _Executer viewTableUsageExecuter = ( (DataDictionary) serverSession.getDataDictionary()).getPreparedStatementGetter().getViewTableUsageExecuter(); 243 _SelectQueryIterator iterator = (_SelectQueryIterator) viewTableUsageExecuter.execute(new Object [] {table_catalog, table_schema, table_name}); 244 if (!iterator.first()) { 245 return null; 246 } 247 Object [] data = (Object []) iterator.getObject(); 248 return new QualifiedIdentifier( (String ) data[0], (String ) data[1], (String ) data[2]); 249 } 250 251 254 public QualifiedIdentifier isTableReferencedFromTriggers(_ServerSession serverSession) throws DException { 255 _Executer triggerTableUsageExecuter = ( (DataDictionary) serverSession.getDataDictionary()).getPreparedStatementGetter().getTriggerTableUsageExecuter(); 256 _SelectQueryIterator iterator = (_SelectQueryIterator) triggerTableUsageExecuter.execute(new Object [] {table_catalog, table_schema, table_name}); 257 if (!iterator.first()) { 258 return null; 259 } 260 Object [] data = (Object []) iterator.getObject(); 261 return new QualifiedIdentifier( (String ) data[0], (String ) data[1], (String ) data[2]); 262 } 263 264 268 public QualifiedIdentifier isTableReferencedFromCheckConstraints(_ServerSession serverSession) throws DException { 269 270 _Executer tableReferencedCheckConstraintExecuter = ( (DataDictionary) serverSession.getDataDictionary()).getPreparedStatementGetter().getTableReferencedCheckConstraintExecuter(); 271 _SelectQueryIterator iterator = (_SelectQueryIterator) tableReferencedCheckConstraintExecuter.execute(new Object [] {table_catalog, table_schema, table_name, table_catalog, table_schema, table_name}); 272 if (!iterator.first()) { 273 return null; 274 } 275 Object [] data = (Object []) iterator.getObject(); 276 return new QualifiedIdentifier( (String ) data[0], (String ) data[1], 277 (String ) data[2]); 278 } 279 280 284 public QualifiedIdentifier isTableReferencedFromReferentialConstraints( 285 _ServerSession serverSession) throws DException { 286 PreparedStatementGetter psg = ( (DataDictionary) serverSession.getDataDictionary()).getPreparedStatementGetter(); 287 _Executer referentialConstraintExecuter = psg.getReferentialConstraintExecuter(); 288 _SelectQueryIterator iterator = (_SelectQueryIterator) referentialConstraintExecuter.execute(new Object [] {table_catalog, table_schema, table_name}); 289 if (!iterator.first()) { 290 return null; 291 } 292 String catalog, schema, name = null; 293 _Executer executer = psg.getConstraintExecuter(); 294 do { 295 Object [] data = (Object []) iterator.getObject(); 296 catalog = (String ) data[SystemTablesFields. 297 referential_constraints_constraint_catalog]; 298 schema = (String ) data[SystemTablesFields. 299 referential_constraints_constraint_schema]; 300 name = (String ) data[SystemTablesFields. 301 referential_constraints_constraint_name]; 302 _SelectQueryIterator iter = (_SelectQueryIterator) executer.execute(new Object [] {catalog, 303 schema, name}); 304 iter.first(); 305 Object [] values = (Object []) iter.getObject(); ; 306 if (! (table_name.equalsIgnoreCase( (String ) values[SystemTablesFields. 307 table_constraints_table_name]) 308 && 309 table_schema.equalsIgnoreCase( (String ) values[SystemTablesFields. 310 table_constraints_table_schema]) 311 && 312 table_catalog.equalsIgnoreCase( (String ) values[SystemTablesFields. 313 table_constraints_table_catalog]))) { 314 return new QualifiedIdentifier(catalog, schema, name); 315 } 316 } while (iterator.next()); 317 return null; 318 } 319 320 322 public void removeColumnDescriptor(ColumnDescriptor columnDescriptor, 323 _ServerSession serverSession) throws DException { 324 columnDescriptors.remove(columnDescriptor.column_name); 325 columnCount--; 326 int columnPosition = columnDescriptor.ordinal_position; 327 331 332 Iterator iterator = columnDescriptors.values().iterator(); 333 ArrayList columnDescriptorsToBeUpdated = new ArrayList(); 334 while (iterator.hasNext()) { 335 ColumnDescriptor columnDescriptor1 = (ColumnDescriptor) iterator.next(); 336 if (columnDescriptor1.ordinal_position > columnPosition) { 337 columnDescriptor1.ordinal_position--; 338 columnDescriptorsToBeUpdated.add(columnDescriptor1); 339 } 340 } 341 int checkColumnposition = columnPosition; 342 while (columnDescriptorsToBeUpdated.size() != 0) { 343 for (int i = 0; i < columnDescriptorsToBeUpdated.size(); i++) { 344 ColumnDescriptor columnDescriptor1 = (ColumnDescriptor) 345 columnDescriptorsToBeUpdated.get(i); 346 if (columnDescriptor1.ordinal_position == checkColumnposition) { 347 columnDescriptor1.update(serverSession, 348 new int[] {SystemTablesFields. 349 columns_ordinal_position} 350 , 351 new Object [] {new Integer (columnDescriptor1. 352 ordinal_position)}); 353 checkColumnposition++; 354 columnDescriptorsToBeUpdated.remove(i); 355 break; 356 } 357 } 358 } 359 } 360 361 363 private String [] getParameters() throws DException { 364 return new String [] {table_catalog, table_schema, table_name}; 365 } 366 367 369 public void setAutoIncrement() { 370 isAutoIncrementAlreadyDefined = true; 371 } 372 373 375 public boolean isAutoIncrementColumnPresent() { 376 return isAutoIncrementAlreadyDefined; 377 } 378 379 381 public String [] getColumnNames() throws DException { 382 Iterator columnsIterator = columnDescriptors.keySet().iterator(); 383 String [] columnNames = new String [columnCount]; 384 int i = 0; 385 while (columnsIterator.hasNext()) { 386 columnNames[i++] = (String ) columnsIterator.next(); 387 } 388 return columnNames; 389 } 390 391 393 public void saveColumns(_ServerSession serverSession) throws DException { 394 Iterator iterator = columnDescriptors.values().iterator(); 395 while (iterator.hasNext()) { 396 ColumnDescriptor cd = (ColumnDescriptor) iterator.next(); 397 cd.save(serverSession); 398 } 399 } 400 401 403 public int getDescriptorType() { 404 return TABLE_DESCRIPTOR; 405 } 406 407 409 public int getColumnCount() { 410 return columnCount; 411 } 412 413 415 public void delete(_ServerSession serverSession) throws DException { 416 booleanvalueexpression condition = ( (DataDictionary) serverSession.getDataDictionary()). 417 getPreparedStatementGetter().getTablesTableCondition(); 418 super.delete(serverSession, SqlSchemaConstants.tables_TableName, condition, 419 new Object [] {table_catalog, table_schema, table_name}); 420 deleteColumns(serverSession); 421 deleteConstraints(serverSession); 422 } 423 424 426 public void deleteColumns(_ServerSession serverSession) throws DException { 427 Iterator iterator = columnDescriptors.values().iterator(); 428 while (iterator.hasNext()) { 429 ColumnDescriptor columnDescriptor = (ColumnDescriptor) iterator.next(); 430 columnDescriptor.delete(serverSession); 431 } 432 } 433 434 436 public void deleteConstraints(_ServerSession serverSession) throws DException { 437 _Executer deleteConstraintExecuter = ( (DataDictionary) serverSession.getDataDictionary()).getPreparedStatementGetter().getDeleteConstraintExecuter(); 438 _SelectQueryIterator retIter = (_SelectQueryIterator) deleteConstraintExecuter.execute(new Object [] {table_catalog, table_schema, table_name}); 439 if (retIter.first()) { 440 do { 441 TableConstraintDescriptor constraint = new TableConstraintDescriptor(); 442 constraint.loadDataFromRecord(retIter, serverSession); 443 constraint.delete(serverSession); 444 } while (retIter.next()); 445 } 446 } 447 448 450 public void addColumnDescriptor(ColumnDescriptor columnDescriptor) throws 451 DException { 452 String columnName = columnDescriptor.column_name.toLowerCase(); 453 Object oldObject = columnDescriptors.put(columnName, columnDescriptor); 454 if (oldObject != null) { 455 throw new DException("DSE250", new Object [] {columnName, getQualifiedTableName().toString()}); 456 } 457 columnDescriptor.ordinal_position = ++columnCount; 458 } 459 460 462 public ColumnDescriptor getColumnDescriptor(String columnName) throws DException { 463 ColumnDescriptor column = (ColumnDescriptor) columnDescriptors.get(columnName.toLowerCase()); 464 if (column != null) { 465 return column; 466 } 467 if (columnCount == 0) { 468 throw new DException("DSE301", new Object [] {columnName}); 469 } 470 throw new DException("DSE255", new Object [] {columnName, 471 getQualifiedTableName().toString()}); 472 } 473 474 476 public int isTableColumns(Object [] columns) throws DException { 477 for (int i = 0; i < columns.length; i++) { 478 String columnName = (String ) columns[i]; 479 if (columnDescriptors.get(columnName) == null) { 480 return i; 481 } 482 } 483 return -1; 484 } 485 486 488 public void loadSchemaDescriptor(_ServerSession serverSession) throws DException { 489 if (schemaDescriptor == null) { 490 schemaDescriptor = new SchemaDescriptor(); 491 } 492 if (schemaDescriptor.catalog_name == null) { 493 schemaDescriptor.catalog_name = table_catalog; 494 } 495 if (schemaDescriptor.schema_name == null) { 496 schemaDescriptor.schema_name = table_schema; 497 } 498 schemaDescriptor.load(serverSession); 499 } 500 501 503 public void addIndexName(String indexName) throws DException { 504 if (indexesList == null) { 505 indexesList = new ArrayList(); 506 } 507 indexesList.add(indexName); 508 } 509 510 512 public ArrayList get_all_column_descriptors() throws DException { 513 ArrayList list = new ArrayList(); 514 Iterator iter = columnDescriptors.values().iterator(); 515 while (iter.hasNext()) { 516 list.add(iter.next()); 517 } 518 return list; 519 } 520 521 522 524 public void addConstraintColumnDescriptor(ColumnDescriptor columnDes) throws 525 DException { 526 columnConstraintDescriptors.put(columnDes.column_name, columnDes); 527 flag = true; 528 } 529 530 533 543 546 public QualifiedIdentifier isTableReferencedFromRoutines(_ServerSession serverSession) throws DException { 547 _Executer routineTableUsageExecuter = ( (DataDictionary) serverSession.getDataDictionary()).getPreparedStatementGetter().getRoutineTableUsageExecuter(); 548 _SelectQueryIterator iterator = (_SelectQueryIterator) routineTableUsageExecuter.execute(new Object [] {table_catalog, table_schema, table_name}); 549 if (!iterator.first()) { 550 return null; 551 } 552 Object [] data = (Object []) iterator.getObject(); 553 return new QualifiedIdentifier( (String ) data[0], (String ) data[1], (String ) data[2]); 554 } 555 } 556 | Popular Tags |