1 package com.daffodilwoods.daffodildb.server.sql99.ddl.schemamanipulation; 2 3 import com.daffodilwoods.daffodildb.server.datadictionarysystem.*; 4 import com.daffodilwoods.daffodildb.server.serversystem.*; 5 import com.daffodilwoods.daffodildb.server.sql99.common.*; 6 import com.daffodilwoods.daffodildb.server.sql99.ddl.descriptors.*; 7 import com.daffodilwoods.daffodildb.server.sql99.ddl.utility.*; 8 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 9 import com.daffodilwoods.daffodildb.server.sql99.expression.*; 10 import com.daffodilwoods.daffodildb.server.sql99.token.*; 11 import com.daffodilwoods.daffodildb.utils.parser.*; 12 import com.daffodilwoods.database.general.*; 13 import com.daffodilwoods.database.resource.*; 14 15 public class dropcolumndefinition implements altertableaction { 16 public dropbehavior _dropbehavior0; 17 public columnname _columnname1; 18 public SRESERVEDWORD1206543922 _OptSRESERVEDWORD12065439222; 19 public SRESERVEDWORD1206543922 _SRESERVEDWORD12065439223; 20 21 private TableDescriptor tableDescriptor; 22 23 public void setTableDescriptor(_Descriptor tableDes) throws DException { 24 tableDescriptor = (TableDescriptor) tableDes; 25 } 26 27 public Object run(Object object) throws DException { 28 _ServerSession currentSession = (_ServerSession) object; 29 String [] columnName = (String []) _columnname1.getColumnName(currentSession); 30 31 String table_Name = tableDescriptor.table_name; 32 String tableSchema = tableDescriptor.table_schema; 33 String tableCatalog = tableDescriptor.table_catalog; 34 35 String tablename = null, schemaName = null, catalogName = null, column = null; 36 catalogName = columnName.length > 3 ? columnName[0] : null; 37 schemaName = columnName.length > 3 ? columnName[1] : columnName.length > 2 ? columnName[0] : null; 38 tablename = columnName.length > 3 ? columnName[2] : columnName.length > 2 ? columnName[1] : columnName.length > 1 ? columnName[0] : null; 39 column = columnName[columnName.length - 1]; 40 41 if ( (catalogName != null && ! (catalogName.equalsIgnoreCase(tableCatalog))) || 42 (schemaName != null && ! (schemaName.equalsIgnoreCase(tableSchema))) 43 || (tablename != null && ! (tablename.equalsIgnoreCase(table_Name)))) { 44 throw new DException("DSE504", null); 45 } 46 47 QualifiedIdentifier tableName = tableDescriptor.getQualifiedTableName(); 48 ColumnDescriptor columnDescriptor = null; 49 try { 50 columnDescriptor = tableDescriptor.getColumnDescriptor(column); 51 } catch (DException ex) { 52 if (ex.getDseCode().equalsIgnoreCase("DSE255")) { 53 throw new DException("DSE251", new Object [] {column, 54 tableDescriptor.getQualifiedTableName(). 55 getIdentifier()}); 56 } 57 } 58 checkColumnName(columnDescriptor); 59 checkForLastColumn(columnDescriptor); 60 String dropBehavior = (String ) _dropbehavior0.run(null); 61 dropColumn(columnDescriptor, currentSession, dropBehavior); 62 63 refreshServerSystem(currentSession, tableName); 64 return null; 65 } 66 67 private void refreshServerSystem(_ServerSession currentSession, 68 QualifiedIdentifier tableName) throws 69 DException { 70 _DataDictionary dataDictionary = currentSession.getDataDictionary(); 71 dataDictionary.removeTable(tableName); 72 _ColumnCharacteristics newCharacteristics = currentSession. 73 getColumnCharacteristics(tableName); 74 currentSession.alterTable(tableName, newCharacteristics, null); 75 } 76 77 private void checkForLastColumn(ColumnDescriptor columnDescriptor) throws 78 DException { 79 int checkCount = 2; 80 81 if (columnDescriptor.tableDescriptor.getColumnCount() <= checkCount) { 82 throw new DException("DSE252", new Object [] {columnDescriptor.column_name, 83 columnDescriptor.table_name}); 84 } 85 } 86 87 private void checkColumnName(ColumnDescriptor columnDescriptor) throws 88 DException { 89 if (columnDescriptor.column_name.equalsIgnoreCase(SystemFields.systemFields[SystemFields.rowId])) { 90 throw new DException("DSE8110", new Object [] {columnDescriptor.column_name, 91 columnDescriptor.table_name}); 92 } 93 } 94 95 private void dropColumn(ColumnDescriptor columnDescriptor, 96 _ServerSession currentSession, String dropBehavior) throws 97 DException { 98 if (dropBehavior.equalsIgnoreCase(SqlKeywords.RESTRICT)) { 99 checkDependencies(columnDescriptor, currentSession); 100 } else { 101 dropDependents(columnDescriptor, currentSession); 102 } 103 dropIndexes(columnDescriptor, currentSession); 104 if (currentSession.getDbVersion() >= 3.0) 105 dropFullTextIndexes(columnDescriptor, currentSession); 106 deletePrivileges(currentSession, columnDescriptor.column_name); 107 columnDescriptor.deleteConstraints(currentSession); 108 columnDescriptor.delete(currentSession); 109 columnDescriptor.tableDescriptor.removeColumnDescriptor(columnDescriptor, 110 currentSession); 111 } 112 113 114 private void checkDependencies(ColumnDescriptor columnDescriptor, 115 _ServerSession currentSession) throws 116 DException { 117 checkTableDependency(columnDescriptor, currentSession); 118 checkViewsDependency(columnDescriptor, currentSession); 119 checkTriggerDependency(columnDescriptor, currentSession); 120 checkCheckConstraintDependency(columnDescriptor, currentSession); 121 checkCompositeKeyDependency(columnDescriptor, currentSession); 122 } 123 124 private void checkCompositeKeyDependency(ColumnDescriptor columnDes, 125 _ServerSession currentSession) throws 126 DException { 127 QualifiedIdentifier ob = columnDes.isColumnReferencedFromCompositeKey(currentSession); 128 if (ob != null) { 129 throw new DException("DSE7082", new Object [] {ob.getIdentifier(), 130 tableDescriptor.getQualifiedTableName(). 131 getIdentifier(), columnDes.column_name}); 132 } 133 } 134 135 private void checkTriggerDependency(ColumnDescriptor columnDes, 136 _ServerSession currentSession) throws 137 DException { 138 QualifiedIdentifier ob = columnDes.isColumnReferencedFromTriggers( 139 currentSession); 140 if (ob != null) { 141 throw new DException("DSE7082", new Object [] {ob.getIdentifier(), 142 tableDescriptor.getQualifiedTableName(). 143 getIdentifier(), columnDes.column_name}); 144 } 145 } 146 147 private void checkCheckConstraintDependency(ColumnDescriptor columnDes, 148 _ServerSession currentSession) throws 149 DException { 150 QualifiedIdentifier ob = columnDes.isColumnReferencedFromCheckConstraints( 151 currentSession); 152 if (ob != null) { 153 throw new DException("DSE7083", new Object [] {ob.getIdentifier(), 154 tableDescriptor.getQualifiedTableName(). 155 getIdentifier(), columnDes.column_name}); 156 } 157 } 158 159 private void checkTableDependency(ColumnDescriptor columnDes, 160 _ServerSession currentSession) throws 161 DException { 162 QualifiedIdentifier ob = columnDes. 163 isColumnReferencedFromReferentialConstraints(currentSession); 164 if (ob != null) { 165 throw new DException("DSE7078", new Object [] {ob.getIdentifier(), 166 tableDescriptor.getQualifiedTableName(). 167 getIdentifier(), columnDes.column_name}); 168 } 169 } 170 171 private void checkViewsDependency(ColumnDescriptor columnDes, 172 _ServerSession currentSession) throws 173 DException { 174 QualifiedIdentifier ob = columnDes.isColumnReferencedFromViews( 175 currentSession); 176 if (ob != null) { 177 throw new DException("DSE7081", new Object [] {ob.getIdentifier(), 178 tableDescriptor.getQualifiedTableName(). 179 getIdentifier(), columnDes.column_name}); 180 } 181 } 182 183 private void dropDependents(ColumnDescriptor columnDescriptor, 184 _ServerSession currentSession) throws 185 DException { 186 dropTriggersHavingColumnAsTriggerColumn(columnDescriptor, currentSession); 187 dropTriggersHavingColumnAsTriggerActionColumn(columnDescriptor, 188 currentSession); 189 } 190 191 private void dropTriggersHavingColumnAsTriggerColumn( 192 ColumnDescriptor columnDescriptor, _ServerSession currentSession) throws 193 DException { 194 201 } 202 203 private void dropTriggersHavingColumnAsTriggerActionColumn( 204 ColumnDescriptor columnDescriptor, _ServerSession currentSession) throws 205 DException { 206 218 } 219 220 private void deletePrivileges(_ServerSession currentSession, String columnName) throws 221 DException { 222 String schemaOwner = GeneralUtility.getSchemaOwner(tableDescriptor. 223 table_catalog, tableDescriptor.table_schema, currentSession); 224 String query = QueryMaker.getDropColumnRevokeQuery(tableDescriptor. 225 getQualifiedTableName(), columnName, schemaOwner); 226 revokeprivilegestatement _revokePrivilegeStatement = ( 227 revokeprivilegestatement) Parser.parseQuery(query); 228 _revokePrivilegeStatement.setIsSelfInitiated(false); 229 _revokePrivilegeStatement.run(currentSession.getSystemServerSession()); 230 } 231 232 public String toString() { 233 StringBuffer sb = new StringBuffer (); 234 sb.append(" "); 235 sb.append(_SRESERVEDWORD12065439223); 236 sb.append(" "); 237 if (_OptSRESERVEDWORD12065439222 != null) { 238 sb.append(_OptSRESERVEDWORD12065439222); 239 } 240 sb.append(" "); 241 sb.append(_columnname1); 242 sb.append(" "); 243 sb.append(_dropbehavior0); 244 return sb.toString(); 245 } 246 247 private void dropIndexes(ColumnDescriptor columnDes, 248 _ServerSession currentSession) throws DException { 249 String query = QueryMaker.getDropColumnIndexQuery(); 250 _SelectQueryIterator iter = SqlSchemaConstants.getIterator(currentSession, query, 251 new Object [] {columnDes.table_catalog, 252 columnDes.table_schema, columnDes.table_name, columnDes.column_name}); 253 if (iter.first()) { 254 do { 255 Object [] values = (Object []) iter.getObject(); 256 IndexDescriptor indexDescriptor = new IndexDescriptor(); 257 indexDescriptor.table_catalog = (String ) values[SystemTablesFields. 258 indexColumns_table_catalog]; 259 indexDescriptor.table_schema = (String ) values[SystemTablesFields. 260 indexColumns_table_schema]; 261 indexDescriptor.table_name = (String ) values[SystemTablesFields. 262 indexColumns_table_name]; 263 indexDescriptor.indexname = (String ) values[SystemTablesFields. 264 indexColumns_indexname]; 265 indexDescriptor.load(currentSession); 266 dropindexstatement dropIndex = new dropindexstatement(); 267 dropIndex.dropIndex(indexDescriptor, currentSession); 268 } while (iter.next()); 269 } 270 } 271 272 private void dropFullTextIndexes(ColumnDescriptor columnDes, 273 _ServerSession currentSession) throws DException { 274 String query = QueryMaker.getDropFullTextColumnIndexQuery(); 275 _SelectQueryIterator iter = SqlSchemaConstants.getIterator(currentSession, query, 276 new Object [] {columnDes.table_catalog, 277 columnDes.table_schema, columnDes.table_name, columnDes.column_name}); 278 if (iter.first()) { 279 do { 280 Object [] values = (Object []) iter.getObject(); 281 FullTextIndexDescriptor indexDescriptor = new FullTextIndexDescriptor(); 282 indexDescriptor.table_catalog = (String ) values[SystemTablesFields. 283 fulltextindex_table_catalog]; 284 indexDescriptor.table_schema = (String ) values[SystemTablesFields. 285 fulltextindex_table_schema]; 286 indexDescriptor.table_name = (String ) values[SystemTablesFields. 287 fulltextindex_table_name]; 288 indexDescriptor.indexname = (String ) values[SystemTablesFields. 289 fulltextindex_indexname]; 290 indexDescriptor.load(currentSession); 291 dropfulltextindexstatement dropIndex = new dropfulltextindexstatement(); 292 dropIndex.dropIndex(indexDescriptor, currentSession); 293 } while (iter.next()); 294 } 295 } 296 297 public Object clone() throws CloneNotSupportedException { 298 return this; 299 } 300 } 301 | Popular Tags |