1 package com.daffodilwoods.daffodildb.server.sql99.ddl.schemamanipulation; 2 3 import java.util.*; 4 5 import com.daffodilwoods.daffodildb.server.serversystem.*; 6 import com.daffodilwoods.daffodildb.server.sql99.common.*; 7 import com.daffodilwoods.daffodildb.server.sql99.ddl.descriptors.*; 8 import com.daffodilwoods.daffodildb.server.sql99.ddl.schemadefinition.*; 9 import com.daffodilwoods.daffodildb.server.sql99.ddl.utility.*; 10 import com.daffodilwoods.daffodildb.server.sql99.dql.iterator.*; 11 import com.daffodilwoods.daffodildb.server.sql99.token.*; 12 import com.daffodilwoods.daffodildb.utils.*; 13 import com.daffodilwoods.database.resource.*; 14 15 public class addtableconstraintdefinition implements altertableaction { 16 public tableconstraintdefinition _tableconstraintdefinition0; 17 public SRESERVEDWORD1206543922 _SRESERVEDWORD12065439221; 18 19 private TableDescriptor tableDescriptor; 20 21 public void setTableDescriptor(_Descriptor tableDes) throws DException { 22 tableDescriptor = (TableDescriptor) tableDes; 23 } 24 25 public Object run(Object object) throws DException { 26 _tableconstraintdefinition0.setTableDescriptor(tableDescriptor); 27 try { 28 _tableconstraintdefinition0.run(object); 29 } catch (DException ex) { 30 if (ex.getDseCode().equalsIgnoreCase("DSE1205")) { 31 throw new DException("DSE7073", new Object [] {tableDescriptor.getQualifiedTableName().getIdentifier()}); 32 } 33 throw ex; 34 } 35 checkConstraintValidity( (_ServerSession) object); 36 return null; 37 } 38 39 private void checkConstraintValidity(_ServerSession serverSession) throws 40 DException { 41 TableConstraintDescriptor tableConstraintDescriptor = 42 _tableconstraintdefinition0.getTableConstraintDescriptor(); 43 String constraintType = tableConstraintDescriptor.constraint_type; 44 if (constraintType.equalsIgnoreCase(SqlKeywords.CHECK)) { 45 isCheckConstraintValid(tableConstraintDescriptor, serverSession); 46 } else if (constraintType.equalsIgnoreCase(SqlKeywords.UNIQUE)) { 47 isUniqueConstraintValid(tableConstraintDescriptor, serverSession); 48 } else if (constraintType.equalsIgnoreCase(SqlKeywords.PRIMARY + " " + 49 SqlKeywords.KEY)) { 50 isPrimaryConstraintValid(tableConstraintDescriptor, serverSession); 51 } else { 52 isForeignConstraintValid(tableConstraintDescriptor, serverSession); 53 } 54 } 55 56 private void isCheckConstraintValid(TableConstraintDescriptor 57 tableConstraintDescriptor, 58 _ServerSession serverSession) throws 59 DException { 60 StringBuffer checkQuery = new StringBuffer (); 61 checkQuery.append("select * from ") 62 .append(QuotedUtility.getUserIdentifier(tableDescriptor.table_catalog)) 63 .append(".").append(QuotedUtility.getUserIdentifier(tableDescriptor. 64 table_schema)) 65 .append(".").append(QuotedUtility.getUserIdentifier(tableDescriptor. 66 table_name)) 67 .append(" where not ( ") 68 .append( ( (CheckConstraintDescriptor) tableConstraintDescriptor. 69 constraintDescriptor).check_clause) 70 .append(" ) "); 71 _Iterator iter = SqlSchemaConstants.getIteratorOnCurrent(serverSession, 72 checkQuery.toString(), (Object [])null); 73 if (iter.first()) { 74 CheckConstraintDescriptor ccd = (CheckConstraintDescriptor) tableConstraintDescriptor.constraintDescriptor; 75 throw new DException("DSE7086", new Object [] {ccd.getQualifiedIdentifier().getIdentifier(), tableDescriptor.getQualifiedTableName().getIdentifier()}); 76 } 77 78 } 79 80 private void isPrimaryConstraintValid(TableConstraintDescriptor 81 tableConstraintDescriptor, 82 _ServerSession serverSession) throws 83 DException { 84 String primaryColumns = QueryMaker.getColumnsList(tableConstraintDescriptor. 85 getConstraintColumns()); 86 StringBuffer checkQuery = new StringBuffer (); 87 checkQuery.append("select ").append(primaryColumns).append(" from ") 88 .append(QuotedUtility.getUserIdentifier(tableDescriptor.table_catalog)). 89 append(".") 90 .append(QuotedUtility.getUserIdentifier(tableDescriptor.table_schema)). 91 append(".") 92 .append(QuotedUtility.getUserIdentifier(tableDescriptor.table_name)) 93 .append(" group By ") 94 .append(primaryColumns) 95 .append(" having count(*) > 1 OR ( ") 96 .append(getColumnsNullCheckCondition(tableConstraintDescriptor. 97 getConstraintColumns())).append( 98 ") "); _Iterator iter = SqlSchemaConstants.getIteratorOnCurrent(serverSession, 100 checkQuery.toString(), (Object [])null); 101 if (iter.first()) { 102 throw new DException("DSE7085", new Object [] {tableConstraintDescriptor.getQualifiedConstraintName().getIdentifier(), tableDescriptor.getQualifiedTableName().getIdentifier()}); 103 } 104 105 } 106 107 private void isUniqueConstraintValid(TableConstraintDescriptor 108 tableConstraintDescriptor, 109 _ServerSession serverSession) throws 110 DException { 111 String uniqueColumns = QueryMaker.getColumnsList(tableConstraintDescriptor. 112 getConstraintColumns()); 113 StringBuffer checkQuery = new StringBuffer (); 114 checkQuery.append("select ").append(uniqueColumns).append(" from ") 115 .append(QuotedUtility.getUserIdentifier(tableDescriptor.table_catalog)). 116 append(".") 117 .append(QuotedUtility.getUserIdentifier(tableDescriptor.table_schema)). 118 append(".") 119 .append(QuotedUtility.getUserIdentifier(tableDescriptor.table_name)) 120 .append(" group By ") 121 .append(uniqueColumns) 122 .append(" having count(*) > 1 "); 123 _Iterator iter = SqlSchemaConstants.getIteratorOnCurrent(serverSession, 124 checkQuery.toString(), (Object [])null); 125 if (iter.first()) { 126 throw new DException("DSE7085", new Object [] {tableConstraintDescriptor.getQualifiedConstraintName().getIdentifier(), tableDescriptor.getQualifiedTableName().getIdentifier()}); 127 } 128 129 } 130 131 private void isForeignConstraintValid(TableConstraintDescriptor 132 tableConstraintDescriptor, 133 _ServerSession serverSession) throws DException { 134 ReferentialConstraintDescriptor referentialConstraintDescriptor = ( 135 ReferentialConstraintDescriptor) tableConstraintDescriptor. 136 constraintDescriptor; 137 String matchType = referentialConstraintDescriptor.match_option; 138 if (matchType.equalsIgnoreCase(SqlKeywords.SIMPLE)) { 139 checkSimpleMatch(referentialConstraintDescriptor, serverSession); 140 } else if (matchType.equalsIgnoreCase(SqlKeywords.PARTIAL)) { 141 checkPartialMatch(referentialConstraintDescriptor, serverSession); 142 } else { 143 checkFullMatch(referentialConstraintDescriptor, serverSession); 144 } 145 } 146 147 private void checkSimpleMatch(ReferentialConstraintDescriptor 148 referentialConstraintDescriptor, 149 _ServerSession serverSession) throws DException { 150 151 StringBuffer checkQuery = new StringBuffer (); 152 String [] referencedColumns = referentialConstraintDescriptor.getReferencedColumnNames(); 153 154 String referencedTable = "\"" + 155 referentialConstraintDescriptor.referencedTableConstraintDescriptor. 156 table_catalog + "\".\"" 157 + 158 referentialConstraintDescriptor.referencedTableConstraintDescriptor. 159 table_schema + 160 "\".\"" 161 + 162 referentialConstraintDescriptor.referencedTableConstraintDescriptor. 163 table_name + 164 "\""; 165 166 String referencingTable = "\"" + referentialConstraintDescriptor. 167 tableConstraintDescriptor.table_catalog + "\".\"" + referentialConstraintDescriptor. 168 tableConstraintDescriptor.table_schema + "\".\"" + referentialConstraintDescriptor. 169 tableConstraintDescriptor.table_name + 170 "\""; 171 172 ArrayList columnNames = referentialConstraintDescriptor. 173 tableConstraintDescriptor.getConstraintColumns(); 174 checkQuery.append("select * from ").append(" ( ").append(" select "); 175 176 for (int i = 0; i < columnNames.size(); i++) { 177 checkQuery.append("childTable"); 178 checkQuery.append("."); 179 checkQuery.append(QuotedUtility.getUserIdentifier( (String ) columnNames.get(i))); 180 checkQuery.append(" , "); 181 } 182 183 checkQuery.append("parentTable").append(".").append("__rowid"); 184 checkQuery.append(" from "); 185 checkQuery.append(referencingTable).append(" AS "); 186 checkQuery.append("childTable"); 187 checkQuery.append(" left Outer Join "); 188 checkQuery.append(referencedTable).append(" AS "); 189 checkQuery.append("parentTable"); 190 checkQuery.append(" on "); 191 192 for (int i = 0; i < columnNames.size(); i++) { 193 if (i != 0) { 194 checkQuery.append(" AND "); 195 } 196 checkQuery.append("childTable"); 197 checkQuery.append("."); 198 checkQuery.append(QuotedUtility.getUserIdentifier( (String ) columnNames.get(i))); 199 checkQuery.append(" = "); 200 checkQuery.append("parentTable").append("."); 201 checkQuery.append(QuotedUtility.getUserIdentifier(referencedColumns[i])); 202 } 203 checkQuery.append(" ) ") 204 .append(" as ").append(" view1 ").append(" where ").append(" ( ") 205 .append("__rowid").append(" is null ").append(" and ").append(" not ( "); 206 for (int i = 0; i < columnNames.size(); i++) { 207 checkQuery.append(QuotedUtility.getUserIdentifier( (String ) columnNames.get(i))).append(" is null "); 208 if (columnNames.size() > 0 && i != (columnNames.size() - 1)) 209 checkQuery.append(" or "); 210 } 211 checkQuery.append(" ) )"); 212 _Iterator iter = SqlSchemaConstants.getIteratorOnCurrent(serverSession, 213 checkQuery.toString(), (Object [])null); 214 if (iter.first()) { 215 throw new DException("DSE7075", new Object [] {referentialConstraintDescriptor.getQualifiedIdentifier().getIdentifier(), referentialConstraintDescriptor.getReferencedTable().getIdentifier()}); } 217 } 218 219 private void checkPartialMatch(ReferentialConstraintDescriptor 220 referentialConstraintDescriptor, 221 _ServerSession serverSession) throws 222 DException { 223 224 StringBuffer checkQuery = new StringBuffer (); 225 String [] referencedColumns = referentialConstraintDescriptor.getReferencedColumnNames(); 226 227 String referencedTable = "\"" + 228 referentialConstraintDescriptor.referencedTableConstraintDescriptor. 229 table_catalog + "\".\"" 230 + 231 referentialConstraintDescriptor.referencedTableConstraintDescriptor. 232 table_schema + 233 "\".\"" 234 + 235 referentialConstraintDescriptor.referencedTableConstraintDescriptor. 236 table_name + 237 "\""; 238 239 String referencingTable = "\"" + 240 referentialConstraintDescriptor.tableConstraintDescriptor.table_catalog + "\".\"" 241 + 242 referentialConstraintDescriptor.tableConstraintDescriptor.table_schema + 243 "\".\"" 244 + 245 referentialConstraintDescriptor.tableConstraintDescriptor.table_name + 246 "\""; 247 248 ArrayList columnNames = referentialConstraintDescriptor. 249 tableConstraintDescriptor.getConstraintColumns(); 250 checkQuery.append("select * from ").append(" ( ").append(" select "); 251 252 for (int i = 0; i < columnNames.size(); i++) { 253 checkQuery.append("childTable"); 254 checkQuery.append("."); 255 checkQuery.append(QuotedUtility.getUserIdentifier( (String ) columnNames.get(i))); 256 checkQuery.append(" , "); 257 } 258 259 checkQuery.append("parentTable").append(".").append("__rowid"); 260 checkQuery.append(" from "); 261 checkQuery.append(referencingTable).append(" AS ").append("childTable"); 262 checkQuery.append(" left Outer Join "); 263 checkQuery.append(referencedTable).append(" AS ").append("parentTable"); 264 checkQuery.append(" on "); 265 266 for (int i = 0; i < columnNames.size(); i++) { 267 if (i != 0) { 268 checkQuery.append(" AND "); 269 } 270 checkQuery.append(" ( "). 271 append("childTable"); 272 checkQuery.append("."); 273 checkQuery.append(QuotedUtility.getUserIdentifier( (String ) columnNames.get(i))); 274 checkQuery.append(" is null ").append(" OR "); 275 checkQuery.append("childTable"); 276 checkQuery.append("."); 277 checkQuery.append(QuotedUtility.getUserIdentifier( (String ) columnNames.get(i))); 278 checkQuery.append(" = "); 279 checkQuery.append("parentTable").append("."); 280 checkQuery.append(QuotedUtility.getUserIdentifier(referencedColumns[i])); 281 checkQuery.append(" ) "); 282 } 283 checkQuery.append(" ) ") 284 .append(" as ").append(" view1 ").append(" where ").append(" ( ") 285 .append("__rowid").append(" is null ").append(" and ( "); for (int i = 0; i < columnNames.size(); i++) { 287 checkQuery.append(QuotedUtility.getUserIdentifier( (String ) columnNames.get(i))).append(" is not null "); 288 if (columnNames.size() > 0 && i != (columnNames.size() - 1)) 289 checkQuery.append(" or "); 290 } 291 checkQuery.append(" )) "); 292 293 _Iterator iter = SqlSchemaConstants.getIteratorOnCurrent(serverSession, 294 checkQuery.toString(), (Object [])null); 295 if (iter.first()) { 296 throw new DException("DSE7075", new Object [] {referentialConstraintDescriptor.getQualifiedIdentifier().getIdentifier(), referentialConstraintDescriptor.getReferencedTable().getIdentifier()}); } 298 } 299 300 private void checkFullMatch(ReferentialConstraintDescriptor 301 referentialConstraintDescriptor, 302 _ServerSession serverSession) throws DException { 303 304 StringBuffer checkQuery = new StringBuffer (); 305 String [] referencedColumns = referentialConstraintDescriptor.getReferencedColumnNames(); 306 307 String referencedTable = "\"" + 308 referentialConstraintDescriptor.referencedTableConstraintDescriptor. 309 table_catalog + "\".\"" 310 + 311 referentialConstraintDescriptor.referencedTableConstraintDescriptor. 312 table_schema + 313 "\".\"" 314 + 315 referentialConstraintDescriptor.referencedTableConstraintDescriptor. 316 table_name + 317 "\""; 318 319 String referencingTable = "\"" + 320 referentialConstraintDescriptor.tableConstraintDescriptor.table_catalog + "\".\"" 321 + 322 referentialConstraintDescriptor.tableConstraintDescriptor.table_schema + 323 "\".\"" 324 + 325 referentialConstraintDescriptor.tableConstraintDescriptor.table_name + 326 "\""; 327 328 ArrayList columnNames = referentialConstraintDescriptor. 329 tableConstraintDescriptor.getConstraintColumns(); 330 checkQuery.append("select * from ").append(" ( ").append(" select "); 331 332 for (int i = 0; i < columnNames.size(); i++) { 333 checkQuery.append("childTable"); 334 checkQuery.append("."); 335 checkQuery.append(QuotedUtility.getUserIdentifier( (String ) columnNames.get(i))); 336 checkQuery.append(" , "); 337 } 338 339 checkQuery.append("parentTable").append(".").append("__rowid"); 340 checkQuery.append(" from "); 341 checkQuery.append(referencingTable).append(" AS ").append("childTable"); 342 checkQuery.append(" left Outer Join "); 343 checkQuery.append(referencedTable).append(" AS ").append("parentTable"); 344 checkQuery.append(" on "); 345 346 for (int i = 0; i < columnNames.size(); i++) { 347 if (i != 0) { 348 checkQuery.append(" AND "); 349 } 350 checkQuery.append("childTable"); 351 checkQuery.append("."); 352 checkQuery.append(QuotedUtility.getUserIdentifier( (String ) columnNames.get(i))); 353 checkQuery.append(" = "); 354 checkQuery.append("parentTable").append("."); 355 checkQuery.append(QuotedUtility.getUserIdentifier(referencedColumns[i])); 356 } 357 checkQuery.append(" ) ") 358 .append(" as ").append(" view1 ").append(" where ").append(" ( ") 359 .append("__rowid").append(" is null ").append(" and ").append(" not ( "); 360 for (int i = 0; i < columnNames.size(); i++) { 361 checkQuery.append(QuotedUtility.getUserIdentifier( (String ) columnNames.get(i))).append(" is null "); 362 if (columnNames.size() > 0 && i != (columnNames.size() - 1)) 363 checkQuery.append(" AND "); 364 } 365 checkQuery.append(" ) )"); 366 367 _Iterator iter = SqlSchemaConstants.getIteratorOnCurrent(serverSession, 368 checkQuery.toString(), (Object [])null); 369 if (iter.first()) { 370 throw new DException("DSE7075", new Object [] {referentialConstraintDescriptor.getQualifiedIdentifier().getIdentifier(), referentialConstraintDescriptor.getReferencedTable().getIdentifier()}); } 372 } 373 374 private String getColumnsNullCheckCondition(ArrayList columns) throws 375 DException { 376 int len = columns.size(); 377 StringBuffer columnsList = new StringBuffer (); 378 for (int i = 0; i < len - 1; i++) { 379 columnsList.append(QuotedUtility.getUserIdentifier( (String ) columns.get( 380 i))).append(" is null OR "); 381 } 382 columnsList.append(QuotedUtility.getUserIdentifier( (String ) columns.get( 383 len - 1))).append(" is null "); 384 return columnsList.toString(); 385 } 386 387 public Object clone() throws CloneNotSupportedException { 388 return this; 389 } 390 391 public String toString() { 392 StringBuffer sb = new StringBuffer (); 393 sb.append(" "); 394 sb.append(_SRESERVEDWORD12065439221); 395 sb.append(" "); 396 sb.append(_tableconstraintdefinition0); 397 return sb.toString(); 398 } 399 } 400 | Popular Tags |