1 2 12 package com.versant.core.jdbc.sql.diff; 13 14 import com.versant.core.jdbc.metadata.JdbcTable; 15 import com.versant.core.jdbc.metadata.JdbcColumn; 16 import com.versant.core.jdbc.metadata.JdbcIndex; 17 import com.versant.core.jdbc.metadata.JdbcConstraint; 18 import com.versant.core.jdbc.sql.SqlDriver; 19 20 import java.util.HashMap ; 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 import java.util.Set ; 24 import java.io.PrintWriter ; 25 26 30 public class DiffUtil { 31 32 private static SqlDriver driver = null; 33 34 public static TableDiff checkTable(SqlDriver sqlDriver, JdbcTable ourTable, JdbcTable dbTable, ControlParams checks) { 35 driver = sqlDriver; 36 TableDiff tableDiff = new TableDiff(ourTable, dbTable); 37 if (dbTable == null) { 39 tableDiff.setMissingTable(true); 40 if (!checks.checkColumnsOnly()) { doIndex(tableDiff, ourTable, dbTable, checks); 43 doConstraint(tableDiff, ourTable, dbTable, checks); 45 } 46 return tableDiff; 47 } 48 49 doCols(tableDiff, ourTable, dbTable, checks); 51 52 if (!checks.checkColumnsOnly()) { 53 doPK(tableDiff, ourTable, dbTable, checks); 55 56 doIndex(tableDiff, ourTable, dbTable, checks); 58 59 doConstraint(tableDiff, ourTable, dbTable, checks); 61 } 62 driver = null; 63 if (tableDiff.hasErrors()) { 64 return tableDiff; 65 } else { 66 return null; 67 } 68 } 69 70 private static void doCols(TableDiff tableDiff, JdbcTable ourTable, JdbcTable dbTable, ControlParams checks) { 71 HashMap marks = new HashMap (); 72 if (ourTable.cols != null) { 73 74 for (int i = 0; i < ourTable.cols.length; i++) { 75 JdbcColumn ourCol = ourTable.cols[i]; 76 JdbcColumn dbCol = null; 78 if (dbTable.cols != null) { 79 for (int j = 0; j < dbTable.cols.length; j++) { 80 JdbcColumn col = dbTable.cols[j]; 81 if (ourCol.name.equalsIgnoreCase(col.name)) { 82 marks.put(new Integer (j), null); 83 dbCol = col; 84 break; 85 } 86 } 87 } 88 if (!ourCol.shared){ 89 ColumnDiff diff = checkColumn(ourCol, dbCol, checks); 90 if (diff != null) { 91 tableDiff.getColDiffs().add(diff); 92 } 93 } 94 } 95 } 96 if (checks.checkExtraColumns){ 97 if (dbTable.cols != null) { for (int i = 0; i < dbTable.cols.length; i++) { 99 if (!marks.containsKey(new Integer (i))) { 100 ColumnDiff diff = new ColumnDiff(null, dbTable.cols[i]); 102 diff.setExtraCol(true); 103 tableDiff.getColDiffs().add(diff); 104 } 105 } 106 } 107 } 108 } 109 110 private static void doPK(TableDiff tableDiff, JdbcTable ourTable, JdbcTable dbTable, ControlParams checks) { 111 112 if (checks.isCheckPK()) { 113 if (ourTable.pk != null && dbTable.pk == null) { 114 PKDiff diff = new PKDiff(null, null); 115 diff.setMissingPK(true); 116 tableDiff.getPkDiffs().add(diff); 117 } else { 118 HashMap marks = new HashMap (); 119 if (ourTable.pk != null) { 120 for (int i = 0; i < ourTable.pk.length; i++) { 121 JdbcColumn ourCol = ourTable.pk[i]; 122 JdbcColumn dbCol = null; 124 if (dbTable.pk != null) { 125 for (int j = 0; j < dbTable.pk.length; j++) { 126 JdbcColumn col = dbTable.pk[j]; 127 if (col != null){ 128 if (ourCol.name.equalsIgnoreCase(col.name)) { 129 marks.put(new Integer (j), null); 130 dbCol = col; 131 break; 132 } 133 } 134 } 135 } 136 PKDiff diff = checkPK(ourCol, dbCol, checks); 137 if (diff != null) { 138 tableDiff.getPkDiffs().add(diff); 139 } 140 } 141 } 142 if (dbTable.pk != null) { 143 for (int i = 0; i < dbTable.pk.length; i++) { 144 if (!marks.containsKey(new Integer (i))) { 145 if (dbTable.pk[i] != null){ 147 PKDiff diff = new PKDiff(null, dbTable.pk[i]); 148 diff.setExtraPKCol(true); 149 tableDiff.getPkDiffs().add(diff); 150 } 151 } 152 } 153 } 154 } 155 } 156 } 157 158 private static void doIndex(TableDiff tableDiff, JdbcTable ourTable, JdbcTable dbTable, ControlParams checks) { 159 160 if (checks.isCheckIndex()) { 161 HashMap marks = new HashMap (); 162 if (ourTable.indexes != null) { 163 for (int i = 0; i < ourTable.indexes.length; i++) { 164 JdbcIndex ourIndex = ourTable.indexes[i]; 165 HashMap possibleIndex = new HashMap (); 167 if (dbTable != null) { 168 if (dbTable.indexes != null) { 169 for (int j = 0; j < dbTable.indexes.length; j++) { 170 JdbcIndex index = dbTable.indexes[j]; 171 possibleIndex.put(new Integer (j), index); 172 } 173 } 174 } 175 IndexDiff diff = null; 176 JdbcIndex closeDbIndex = null; 177 Integer closeKey = null; 178 boolean found = false; 179 Set keys = possibleIndex.keySet(); 180 for (Iterator iterator = keys.iterator(); iterator.hasNext();) { 181 Integer key = (Integer ) iterator.next(); 182 JdbcIndex dbIndex = (JdbcIndex) possibleIndex.get(key); 183 diff = checkIndex(ourIndex, dbIndex, checks); 184 if (ourIndex.name.equalsIgnoreCase(dbIndex.name)) { 185 closeDbIndex = dbIndex; 186 closeKey = key; 187 } 188 if (diff == null) { 189 found = true; 190 marks.put(key, null); 191 } 192 } 193 194 if (!found) { 195 diff = checkIndex(ourIndex, closeDbIndex, checks); 196 tableDiff.getIndexDiffs().add(diff); 197 if (closeKey != null) { 198 marks.put(closeKey, null); 199 } 200 } 201 202 } 203 } 204 if (dbTable != null) { if (dbTable.indexes != null) { 206 for (int i = 0; i < dbTable.indexes.length; i++) { 207 if (!marks.containsKey(new Integer (i))) { 208 IndexDiff diff = new IndexDiff(null, dbTable.indexes[i]); 210 diff.setExtraIndex(true); 211 tableDiff.getIndexDiffs().add(diff); 212 } 213 } 214 } 215 } 216 } 217 } 218 219 private static void doConstraint(TableDiff tableDiff, JdbcTable ourTable, JdbcTable dbTable, ControlParams checks) { 220 if (checks.isCheckConstraint()) { 221 HashMap marks = new HashMap (); 222 if (ourTable.constraints != null) { 223 for (int i = 0; i < ourTable.constraints.length; i++) { 224 JdbcConstraint ourConstraint = ourTable.constraints[i]; 225 HashMap possibleConstraint = new HashMap (); 227 if (dbTable != null) { 228 if (dbTable.constraints != null) { 229 for (int j = 0; j < dbTable.constraints.length; j++) { 230 JdbcConstraint constraint = dbTable.constraints[j]; 231 try { 232 if (ourConstraint.src.name.equalsIgnoreCase(constraint.src.name) && 233 (ourConstraint.dest.name.equalsIgnoreCase(constraint.dest.name))) { 234 possibleConstraint.put(new Integer (j), constraint); 235 } 236 } catch (Exception e) {} 237 } 238 } 239 } 240 ConstraintDiff diff = null; 241 JdbcConstraint closeDbConstraint = null; 242 Integer closeKey = null; 243 boolean found = false; 244 Set keys = possibleConstraint.keySet(); 245 for (Iterator iterator = keys.iterator(); iterator.hasNext();) { 246 Integer key = (Integer ) iterator.next(); 247 JdbcConstraint dbConstraint = (JdbcConstraint)possibleConstraint.get(key); 248 diff = checkConstraint(ourConstraint, dbConstraint, checks); 249 if (ourConstraint.name.equalsIgnoreCase(dbConstraint.name)) { 250 closeDbConstraint = dbConstraint; 251 closeKey = key; 252 } 253 if (diff == null) { 254 found = true; 255 marks.put(key,null); 256 257 } 258 } 259 260 if (!found){ 261 diff = checkConstraint(ourConstraint, closeDbConstraint, checks); 262 tableDiff.getConstraintDiffs().add(diff); 263 if (closeKey != null){ 264 marks.put(closeKey, null); 265 } 266 } 267 } 268 } 269 if (dbTable != null) { if (dbTable.constraints != null) { 271 for (int i = 0; i < dbTable.constraints.length; i++) { 272 if (!marks.containsKey(new Integer (i))) { 273 if (dbTable.constraints[i] != null){ 275 ConstraintDiff diff = new ConstraintDiff(null, dbTable.constraints[i]); 276 diff.setExtraConstraint(true); 277 diff.setDrop(true); 278 tableDiff.getConstraintDiffs().add(diff); 279 } 280 } 281 } 282 } 283 } 284 } 285 } 286 287 288 289 private static ColumnDiff checkColumn(JdbcColumn ourCol, JdbcColumn dbCol, ControlParams checks) { 290 ColumnDiff diff = new ColumnDiff(ourCol, dbCol); 291 if (dbCol == null) { 292 diff.setMissingCol(true); 293 294 } else { if (checks.isCheckType()) { 296 if (!driver.checkType(ourCol, dbCol)) { 297 diff.setTypeDiff(true); 298 } 299 } 300 if (checks.isCheckLength()) { 301 if (!driver.checkLenght(ourCol, dbCol)) { 302 diff.setLenghtDiff(true); 303 } 304 } 305 if (checks.isCheckScale()) { 306 if (!driver.checkScale(ourCol, dbCol)) { 307 diff.setScaleDiff(true); 308 } 309 } 310 if (checks.isCheckNulls()) { 311 if (!driver.checkNulls(ourCol, dbCol)) { 312 diff.setNullDiff(true); 313 } 314 } 315 } 316 317 if (diff.hasErrors()) { 318 return diff; 319 } else { 320 return null; 321 } 322 323 } 324 325 private static PKDiff checkPK(JdbcColumn ourCol, JdbcColumn dbCol, ControlParams checks) { 326 PKDiff diff = new PKDiff(ourCol, dbCol); 327 if (dbCol == null) { 328 diff.setMissingPKCol(true); 329 } 330 if (diff.hasErrors()) { 331 return diff; 332 } else { 333 return null; 334 } 335 } 336 337 private static IndexDiff checkIndex(JdbcIndex ourIndex, JdbcIndex dbIndex, ControlParams checks) { 338 IndexDiff diff = new IndexDiff(ourIndex, dbIndex); 339 if (dbIndex == null) { 340 diff.setMissingIndex(true); 341 } else { 342 HashMap marks = new HashMap (); 344 if (ourIndex.cols != null) { 345 for (int i = 0; i < ourIndex.cols.length; i++) { 346 JdbcColumn ourCol = ourIndex.cols[i]; 347 JdbcColumn dbCol = null; 349 if (dbIndex.cols != null) { 350 for (int j = 0; j < dbIndex.cols.length; j++) { 351 JdbcColumn col = dbIndex.cols[j]; 352 if (ourCol.name.equalsIgnoreCase(col.name)) { 353 marks.put(new Integer (j), null); 354 dbCol = col; 355 break; 356 } 357 } 358 } 359 if (dbCol == null) { 360 diff.setMissingCol(true); 361 } 362 } 363 } 364 if (dbIndex != null && ourIndex != null){ 365 if (dbIndex.unique != ourIndex.unique){ 366 diff.setUniqueness(true); 367 } 368 } 369 370 if (dbIndex.cols != null) { 371 for (int i = 0; i < dbIndex.cols.length; i++) { 372 if (!marks.containsKey(new Integer (i))) { 373 diff.setExtraCol(true); 375 } 376 } 377 } 378 } 379 if (diff.hasErrors()) { 380 return diff; 381 } else { 382 return null; 383 } 384 } 385 386 public static ConstraintDiff checkConstraint(JdbcConstraint ourConstraint, JdbcConstraint dbConstraint, 387 ControlParams checks) { 388 ConstraintDiff diff = new ConstraintDiff(ourConstraint, dbConstraint); 389 if (dbConstraint == null) { 390 diff.setMissingConstraint(true); 391 } else { 392 HashMap marks = new HashMap (); 394 if (ourConstraint.srcCols != null) { 395 for (int i = 0; i < ourConstraint.srcCols.length; i++) { 396 JdbcColumn ourCol = ourConstraint.srcCols[i]; 397 JdbcColumn dbCol = null; 399 if (dbConstraint.srcCols != null) { 400 for (int j = 0; j < dbConstraint.srcCols.length; j++) { 401 JdbcColumn col = dbConstraint.srcCols[j]; 402 if (col != null){ 403 if (ourCol.name.equalsIgnoreCase(col.name)) { 404 marks.put(new Integer (j), null); 405 dbCol = col; 406 break; 407 } 408 } 409 } 410 } 411 if (dbCol == null) { 412 diff.setMissingCol(true); 413 diff.setDrop(true); 414 } 415 } 416 } 417 if (dbConstraint.srcCols != null) { 418 for (int i = 0; i < dbConstraint.srcCols.length; i++) { 419 if (!marks.containsKey(new Integer (i))) { 420 diff.setExtraCol(true); 422 diff.setDrop(true); 423 } 424 } 425 } 426 } 427 428 if (diff.hasErrors()) { 429 return diff; 430 } else { 431 return null; 432 } 433 } 434 435 436 441 public static void reportErrors(ArrayList diffList, PrintWriter out) { 442 for (Iterator iter = diffList.iterator(); iter.hasNext();) { 443 TableDiff tableDiff = (TableDiff) iter.next(); 444 if (tableDiff.hasRealErrors()){ 445 printError(out, tableDiff.getOurTable().name); 446 } 447 if (tableDiff.isMissingTable()) { 448 printErrorMsg(out, "Table '" + tableDiff.getOurTable().name + "' does not exist."); 449 } 450 ArrayList colList = tableDiff.getColDiffs(); 451 for (Iterator iterator = colList.iterator(); iterator.hasNext();) { 452 ColumnDiff diff = (ColumnDiff) iterator.next(); 453 if (diff.isExtraCol()) { 454 printErrorMsg(out, "Column '" + diff.getDbCol().name + "' is not known to JDOGenie"); 455 } 456 457 if (diff.isLenghtDiff()) { 458 printErrorMsg(out, 459 "Column '" + diff.getOurCol().name + "' length is " + diff.getDbCol().length + ", it should be " + diff.getOurCol() 460 .length); 461 } 462 463 if (diff.isMissingCol()) { 464 printErrorMsg(out, "Column '" + diff.getOurCol().name + "' does not exist."); 465 } 466 467 if (diff.isNullDiff()) { 468 printErrorMsg(out, "Column '" + diff.getOurCol().name + "' null value is " + (diff.getDbCol() 469 .nulls ? 470 "'NULL'" : "'NOT NULL'") + ", it should be " + (diff.getOurCol().nulls ? 471 "'NULL'" : "'NOT NULL'")); 472 } 473 474 if (diff.isScaleDiff()) { 475 printErrorMsg(out, 476 "Column '" + diff.getOurCol().name + "' scale is " + diff.getDbCol().scale + ", it should be " + diff.getOurCol() 477 .scale); 478 479 } 480 481 if (diff.isTypeDiff()) { 482 printErrorMsg(out, 483 "Column '" + diff.getOurCol().name + "' type is " + diff.getDbCol().sqlType + ", it should be " + diff.getOurCol() 484 .sqlType); 485 } 486 } 487 ArrayList pkList = tableDiff.getPkDiffs(); 488 for (Iterator iterator = pkList.iterator(); iterator.hasNext();) { 489 PKDiff diff = (PKDiff) iterator.next(); 490 if (diff.isMissingPK()){ 492 printErrorMsg(out, "Primary key '"+ tableDiff.getOurTable().pkConstraintName +"' on table '" + tableDiff.getOurTable().name+ "' does not exist."); 493 } else { 494 if (diff.isMissingPKCol()) { 495 printErrorMsg(out, "Primary key '"+ tableDiff.getOurTable().pkConstraintName +"' has a missing column '" + diff.getOurCol().name + "'."); 496 } 497 if (diff.isExtraPKCol()) { 498 printErrorMsg(out, "Primary key '"+ tableDiff.getOurTable().pkConstraintName +"' has a extra column '" + diff.getDbCol().name + "' that is not known to JDOGenie"); 499 } 500 } 501 } 502 ArrayList indexList = tableDiff.getIndexDiffs(); 503 for (Iterator iterator = indexList.iterator(); iterator.hasNext();) { 504 IndexDiff diff = (IndexDiff) iterator.next(); 505 if (diff.isMissingIndex()) { 507 printErrorMsg(out, "Index '" + diff.getOurIndex().name + "' does not exist."); 508 } 509 if (diff.isExtraIndex()) { 510 printErrorMsg(out, "Index '" + diff.getDbIndex().name + "' is not known to JDOGenie"); 511 } 512 if (diff.isExtraCol()) { 513 printErrorMsg(out, "Index '" + diff.getOurIndex().name + "' has extra columns not known to JDOGenie"); 514 } 515 if (diff.isMissingCol()) { 516 printErrorMsg(out, "Index '" + diff.getOurIndex().name + "' has missing columns"); 517 } 518 if (diff.isUniqueness()) { 519 JdbcIndex ourIndex = diff.getOurIndex(); 520 if (ourIndex.unique){ 521 printErrorMsg(out, "Index '" + diff.getOurIndex().name + "' is unique, but database is not"); 522 } else { 523 printErrorMsg(out, "Index '" + diff.getOurIndex().name + "' is not unique, but database is"); 524 } 525 526 } 527 } 528 ArrayList constraintList = tableDiff.getConstraintDiffs(); 529 for (Iterator iterator = constraintList.iterator(); iterator.hasNext();) { 530 ConstraintDiff diff = (ConstraintDiff) iterator.next(); 531 if (diff.isMissingConstraint()) { 533 printErrorMsg(out, "Constraint '" + diff.getOurConstraint().name + "' does not exist."); 534 } 535 if (diff.isExtraConstraint()) { 536 printErrorMsg(out, "Constraint '" + diff.getDbConstraint().name + "' is not known to JDOGenie"); 537 } 538 if (diff.isExtraCol()) { 539 printErrorMsg(out, "Constraint '" + diff.getOurConstraint().name + "' has extra columns not known to JDOGenie"); 540 } 541 if (diff.isMissingCol()) { 542 printErrorMsg(out, "Constraint '" + diff.getOurConstraint().name + "' has missing columns."); 543 } 544 } 545 } 546 } 547 548 private static void printError(PrintWriter out, String tableName) { 549 out.print("\nTable "); 550 out.print(tableName); 551 out.println(" : FAIL"); 552 } 553 554 private static void printErrorMsg(PrintWriter out, String error) { 555 out.print(" "); 556 out.println(error); 557 } 558 559 560 561 } 562 | Popular Tags |