1 package com.daffodilwoods.daffodildb.server.sql99.ddl.schemamanipulation; 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.sql99.common.*; 8 import com.daffodilwoods.daffodildb.server.sql99.ddl.descriptors.*; 9 import com.daffodilwoods.daffodildb.server.sql99.ddl.utility.*; 10 import com.daffodilwoods.daffodildb.server.sql99.expression.booleanvalueexpression.*; 11 import com.daffodilwoods.daffodildb.server.sql99.token.*; 12 import com.daffodilwoods.daffodildb.utils.*; 13 import com.daffodilwoods.daffodildb.utils.parser.*; 14 import com.daffodilwoods.database.general.*; 15 import com.daffodilwoods.database.resource.*; 16 17 public class dropdomainstatement implements SQLschemamanipulationstatement { 18 public dropbehavior _dropbehavior0; 19 public domainname _domainname1; 20 public SRESERVEDWORD1206543922 _SRESERVEDWORD12065439222; 21 public SRESERVEDWORD1206543922 _SRESERVEDWORD12065439223; 22 23 53 54 public Object run(Object object) throws com.daffodilwoods.database.resource. 55 DException { 56 _ServerSession currentSession = (_ServerSession) object; 57 DomainDescriptor domainDescriptor = new DomainDescriptor(); 58 setDomainName(domainDescriptor, currentSession); 59 String schemaOwner = GeneralUtility.getSchemaOwner(domainDescriptor. 60 catalog_name, domainDescriptor.schema_name, currentSession); 61 GeneralUtility.validateUserRights(schemaOwner, currentSession); 62 String dropBehavior = (String ) _dropbehavior0.run(null); 63 dropDomain(domainDescriptor, currentSession, dropBehavior, schemaOwner); 64 65 refreshDataDictionary(currentSession, domainDescriptor.getQualifiedIdentifier()); 66 return null; 67 } 68 69 private void setDomainName(DomainDescriptor domainDescriptor, 70 _ServerSession serverSession) throws DException { 71 _domainname1.run(null); 72 domainDescriptor.catalog_name = _domainname1.getCatalogName(); 73 domainDescriptor.schema_name = _domainname1.getSchemaName(); 74 domainDescriptor.domain_name = _domainname1.getDomainName(); 75 76 if (domainDescriptor.schema_name == null) { 77 domainDescriptor.schema_name = serverSession.getCurrentSchema(); 78 } 79 if (domainDescriptor.catalog_name == null) { 80 domainDescriptor.catalog_name = serverSession.getCurrentCatalog(); 81 } 82 domainDescriptor.load(serverSession); 83 } 84 85 public void dropDomain(DomainDescriptor domainDescriptor, 86 _ServerSession currentSession, 87 String dropBehavior, String schemaOwner) throws 88 DException { 89 if (dropBehavior.equalsIgnoreCase(SqlKeywords.RESTRICT)) { 90 checkDependencies(domainDescriptor, currentSession); 91 } else { 92 modifyColumns(domainDescriptor, currentSession); 93 } 94 deletePrivileges(domainDescriptor, currentSession, schemaOwner); 95 domainDescriptor.delete(currentSession); 96 } 97 98 private void modifyColumns(DomainDescriptor domainDescriptor, 99 _ServerSession currentSession) throws DException { 100 ArrayList columnDescriptors = domainDescriptor. 101 getColumnDescriptorsForDomain(currentSession); 102 ArrayList constraintClause = domainDescriptor.getConstraintsConditions(); 103 if (columnDescriptors != null) { 104 for (int i = 0; i < columnDescriptors.size(); i++) { 105 ColumnDescriptor columnDescriptor = (ColumnDescriptor) 106 columnDescriptors.get(i); 107 QualifiedIdentifier tableName = columnDescriptor. 108 getQualifiedTableName(); 109 String columnName = columnDescriptor.column_name; 110 if (constraintClause != null) { 111 applyDomainConstraintsToColumns(currentSession, constraintClause, 112 tableName, columnName); 113 } 114 updateColumnDescriptor(currentSession, columnDescriptor); 115 } 116 } 117 } 118 119 private void applyDomainConstraintsToColumns(_ServerSession currentSession, 120 ArrayList constraintClause, 121 QualifiedIdentifier tableName, 122 String columnName) throws 123 DException { 124 for (int j = 0; j < constraintClause.size(); j++) { 125 String clause = (String ) constraintClause.get(j); 126 alterTableAddConstraint(currentSession, tableName, clause, 127 columnName); 128 } 129 } 130 131 private void updateColumnDescriptor(_ServerSession currentSession, 132 ColumnDescriptor columnDescriptor) throws 133 DException { 134 columnDescriptor.update(currentSession, 135 new int[] { 136 SystemTablesFields.columns_domain_catalog, 137 SystemTablesFields.columns_domain_schema, 138 SystemTablesFields.columns_domain_name} 139 , new Object [] {null, null, null}); 140 } 141 142 public void refreshDataDictionary(_ServerSession currentSession, 143 QualifiedIdentifier domainName) throws 144 DException { 145 DataDictionary dd = (DataDictionary) currentSession.getDataDictionary(); 146 dd.removeCheckConstraintForDomain(new QualifiedIdentifier(domainName.catalog, domainName.schema, 147 domainName.name)); 148 } 149 150 private void alterTableAddConstraint(_ServerSession serverSession, 151 QualifiedIdentifier tableName, 152 String clause, String columnName) throws 153 DException { 154 String clauseLower = clause.toLowerCase(); 155 String newCheckClause = getReplacedCheckClause(columnName, clauseLower); 156 String query = QueryMaker.getAlterTableAddCheckConstraintQuery(tableName, 157 newCheckClause); 158 altertablestatement _alterTableStatement = (altertablestatement) Parser. 159 parseQuery(query.toString()); 160 _alterTableStatement.setIsSelfInitiated(false); 161 _alterTableStatement.run(serverSession); 162 } 163 164 private String getReplacedCheckClause(String columnName, 165 String domainCheckClause) { 166 StringBuffer newCheckClause = new StringBuffer (); 167 int index = domainCheckClause.indexOf("value"); 168 while (index != -1) { 169 newCheckClause.append(domainCheckClause.substring(0, index)) 170 .append(" ") 171 .append(QuotedUtility.getUserIdentifier(columnName)); 172 domainCheckClause = domainCheckClause.substring(index + 6); 173 index = domainCheckClause.indexOf("value"); 174 } 175 newCheckClause.append(domainCheckClause); 176 return newCheckClause.toString(); 177 } 178 179 180 188 private void checkDependencies(DomainDescriptor domainDescriptor, 189 _ServerSession serverSession) throws 190 DException { 191 checkColumnsDependency(domainDescriptor, serverSession); 192 checkRoutinesDependency(domainDescriptor, serverSession); 193 } 194 195 private void checkRoutinesDependency(DomainDescriptor domainDescriptor, 196 _ServerSession serverSession) throws 197 DException { 198 if (domainDescriptor.isDomainReferencedfromRoutines(serverSession)) { 199 throw new DException("DSE964", null); 200 } 201 } 202 203 private void checkColumnsDependency(DomainDescriptor domainDescriptor, 204 _ServerSession serverSession) throws 205 DException { 206 Object ob = domainDescriptor.isDomainReferencedfromColumns(serverSession); 207 if (ob != null) { 208 Object [] data = (Object []) ob; 209 String tabc = (String ) data[SystemTablesFields.columns_table_catalog]; 210 String tabs = (String ) data[SystemTablesFields.columns_table_schema]; 211 String tabn = (String ) data[SystemTablesFields.columns_table_name]; 212 String coln = (String ) data[SystemTablesFields.columns_column_name]; 213 String tableName = new QualifiedIdentifier(tabc, tabs, tabn). 214 getIdentifier(); 215 throw new DException("DSE5018", new Object [] {tableName, coln, 216 domainDescriptor.getQualifiedIdentifier(). 217 getIdentifier()}); 218 } 219 } 220 221 private void deletePrivileges(DomainDescriptor domainDes, 222 _ServerSession serverSession, 223 String schemaOwner) throws DException { 224 String query = QueryMaker.getDropDomainRevokeQuery(domainDes. 225 getQualifiedIdentifier(), schemaOwner); 226 revokeprivilegestatement _revokePrivilegesStatement = ( 227 revokeprivilegestatement) Parser.parseQuery(query); 228 _revokePrivilegesStatement.setIsSelfInitiated(false); 229 _revokePrivilegesStatement.run(serverSession.getSystemServerSession()); 230 231 } 232 233 public Object clone() throws CloneNotSupportedException { 234 return this; 235 } 236 237 public String toString() { 238 StringBuffer sb = new StringBuffer (); 239 sb.append(" "); 240 sb.append(_SRESERVEDWORD12065439223); 241 sb.append(" "); 242 sb.append(_SRESERVEDWORD12065439222); 243 sb.append(" "); 244 sb.append(_domainname1); 245 sb.append(" "); 246 sb.append(_dropbehavior0); 247 return sb.toString(); 248 } 249 } 250 | Popular Tags |