1 32 33 34 package com.knowgate.datacopy; 35 36 import com.knowgate.debug.DebugFile; 37 38 import java.sql.Connection ; 39 import java.sql.DatabaseMetaData ; 40 import java.sql.ResultSet ; 41 import java.sql.ResultSetMetaData ; 42 import java.sql.Statement ; 43 import java.sql.SQLException ; 44 45 50 51 public class DataTblDef { 52 53 public DataTblDef() { } 54 55 57 private void alloc(int cCols) { 58 ColCount = cCols; 60 ColNames = new String [cCols]; 62 ColTypes = new int[cCols]; 64 ColSizes = new int[cCols]; 66 PrimaryKeyMarks = new boolean[cCols]; 68 for (int c=0;c<cCols;c++) PrimaryKeyMarks[c] = false; 70 } 71 72 74 81 public void readMetaData(Connection oConn, String sTable, String sPK) throws SQLException { 82 int lenPK; 83 int iCurr; 84 int cCols; 85 Statement oStmt = null; 86 ResultSet oRSet = null; 87 ResultSetMetaData oMDat = null; 88 89 if (DebugFile.trace) { 90 DebugFile.writeln("Begin DataTblDef.readMetaData([Connection], \"" + sTable + "\",\"" + sPK + "\")"); 91 DebugFile.incIdent(); 92 } 93 94 BaseTable = sTable; 95 96 99 105 try { 106 oStmt = oConn.createStatement(); 107 108 if (DebugFile.trace) DebugFile.writeln ("Statement.executeQuery(SELECT * FROM " + sTable + " WHERE 1=0)"); 109 110 oRSet = oStmt.executeQuery("SELECT * FROM " + sTable + " WHERE 1=0"); 111 112 oMDat = oRSet.getMetaData(); 113 114 cCols = oMDat.getColumnCount(); 115 116 alloc(cCols); 118 for (int c=0; c<cCols; c++) { 119 ColNames[c] = oMDat.getColumnName(c+1); 120 ColTypes[c] = oMDat.getColumnType(c+1); 121 ColSizes[c] = oMDat.getPrecision(c+1); 122 123 if (DebugFile.trace) DebugFile.writeln(ColNames[c] + " SQLType " + String.valueOf(ColTypes[c]) + " precision " + ColSizes[c]); 124 } 126 oMDat = null; 127 } 128 catch (SQLException sqle) { 129 throw new SQLException (sqle.getMessage(), sqle.getSQLState(), sqle.getErrorCode()); 130 } 131 finally { 132 if (null!=oRSet) oRSet.close(); 133 if (null!=oStmt) oStmt.close(); 134 } 135 136 139 if (null!=sPK) { 140 lenPK = sPK.length()-1; 141 142 cPKs = 1; 143 for (int i=1; i<=lenPK; i++) 145 if(sPK.charAt(i)==',') cPKs++; 146 147 PrimaryKeys = new String [cPKs]; 149 150 iCurr = 0; 152 PrimaryKeys[0] = ""; 153 for (int j=0; j<=lenPK; j++) 154 if (sPK.charAt(j)!=',') { 155 PrimaryKeys[iCurr] += sPK.charAt(j); 156 } 157 else { 158 if (DebugFile.trace) DebugFile.writeln("PrimaryKeys[" + String.valueOf(iCurr) + "]=" + PrimaryKeys[iCurr]); 159 PrimaryKeys[++iCurr] = ""; 160 } 161 162 if (DebugFile.trace) DebugFile.writeln("PrimaryKeys[" + String.valueOf(iCurr) + "]=" + PrimaryKeys[iCurr]); 163 164 166 for (int l=0; l<ColCount; l++) PrimaryKeyMarks[l] = false; 167 168 for (int k=0; k<cPKs; k++) { 169 for (int f=0;f<ColCount;f++) { 170 PrimaryKeyMarks[f] |= PrimaryKeys[k].equalsIgnoreCase(ColNames[f]); 171 } } } else { 175 cPKs = 0; 176 PrimaryKeys = null; 177 } 178 179 if (DebugFile.trace) { 180 DebugFile.decIdent(); 181 DebugFile.writeln("End DataTblDef.readMetaData()"); 182 } 183 184 } 186 188 198 public String getPrimaryKeys (Connection oConn, String sSchema, String sCatalog, String sTable) 199 throws SQLException { 200 201 String sPKCols = null; 202 DatabaseMetaData oMDat = oConn.getMetaData(); 203 ResultSet oRSet = oMDat.getPrimaryKeys(sCatalog, sSchema, sTable); 204 205 while (oRSet.next()) { 206 if (null==sPKCols) 207 sPKCols = oRSet.getString(4); 208 else 209 sPKCols += "," + oRSet.getString(4); 210 211 } 213 oRSet.close(); 214 215 return sPKCols; 216 } 217 218 220 public int findColumnPosition(String sColName) { 221 224 int iCol = -1; 225 226 for (int c=0; (c<ColCount) && (iCol==-1); c++) 227 if(sColName.equalsIgnoreCase(ColNames[c])) 228 iCol = c; 229 230 return iCol; 231 } 233 235 public int findColumnType(String sColName) { 236 239 int iType = 0; 240 241 for (int c=0; c<ColCount; c++) 242 if(sColName.equalsIgnoreCase(ColNames[c])) { 243 iType = ColTypes[c]; 244 break; 245 } 246 return iType; 247 } 249 251 public boolean inheritsPK(DataTblDef oTblDef) 252 throws ArrayIndexOutOfBoundsException { 253 254 if (DebugFile.trace) { 255 DebugFile.writeln("Begin " + BaseTable + " DataTblDef.inheritsPK(" + oTblDef.BaseTable + ")"); 256 DebugFile.incIdent(); 257 DebugFile.writeln(BaseTable + " has " + String.valueOf(cPKs) +" pk columns"); 258 } 259 260 boolean bSamePK; 264 int pc, fc; 265 266 int iMatchCount = 0; 267 268 if (DebugFile.trace) 269 if (cPKs<oTblDef.cPKs) 270 DebugFile.writeln(BaseTable + " does not inherit PK from " + oTblDef.BaseTable + " because " + oTblDef.BaseTable + " has " + String.valueOf(oTblDef.cPKs) + " PK columns and " + BaseTable + " has only " + String.valueOf(cPKs) + " PK columns"); 271 272 bSamePK = (cPKs>=oTblDef.cPKs); 273 274 if (bSamePK) { 275 276 for (int fk=0; fk<cPKs; fk++) { 277 278 if (DebugFile.trace) DebugFile.writeln("fk=" + String.valueOf(fk)); 279 280 fc = findColumnPosition(PrimaryKeys[fk]); 281 282 if (DebugFile.trace && -1==fc) DebugFile.writeln("cannot find column " + PrimaryKeys[fk] + " on " + BaseTable); 283 284 if (-1!=fc) { 285 286 for (int pk=0; pk<oTblDef.cPKs; pk++) { 287 288 if (DebugFile.trace) DebugFile.writeln("pk=" + String.valueOf(pk)); 289 290 pc = oTblDef.findColumnPosition(oTblDef.PrimaryKeys[pk]); 291 292 if (DebugFile.trace && -1==pc) DebugFile.writeln("cannot find column " + oTblDef.PrimaryKeys[pk] + " on " + oTblDef.BaseTable); 293 294 if (-1!=pc) { 295 296 if (DebugFile.trace) 297 DebugFile.writeln("trying " + BaseTable + "." + ColNames[fc] + " and " + oTblDef.BaseTable + "." + oTblDef.ColNames[pc]); 298 299 if ((oTblDef.ColTypes[pc]==ColTypes[fc] && oTblDef.ColSizes[pc]==ColSizes[fc]) && 300 ((cPKs==1 && oTblDef.ColNames[pc].equalsIgnoreCase(ColNames[fc])) || (cPKs>1))) { 301 302 if (DebugFile.trace) { 303 if (cPKs>1) 304 DebugFile.writeln(BaseTable + "." + PrimaryKeys[fk] + " matches " + oTblDef.BaseTable + "." + oTblDef.PrimaryKeys[pk]); 305 else 306 DebugFile.writeln(BaseTable + "." + PrimaryKeys[fk] + " matches same column on " + oTblDef.BaseTable + "." + oTblDef.PrimaryKeys[pk]); 307 } 308 309 iMatchCount++; 310 break; 311 } 312 else { 313 if (DebugFile.trace) { 314 if (oTblDef.ColTypes[pc]!=ColTypes[fc]) 315 DebugFile.writeln(BaseTable + "." + PrimaryKeys[fk] + " has SQLType " + ColTypes[fc] + " and " + oTblDef.BaseTable + "." + oTblDef.PrimaryKeys[pk] + " has SQLType " + oTblDef.ColTypes[pc]); 316 else if (oTblDef.ColSizes[pc]==ColSizes[fc]) 317 DebugFile.writeln(BaseTable + "." + PrimaryKeys[fk] + " has size " + ColSizes[fc] + " and " + oTblDef.BaseTable + "." + oTblDef.PrimaryKeys[pk] + " has size " + oTblDef.ColSizes[pc]); 318 else if (cPKs==1 && !oTblDef.ColNames[pc].equalsIgnoreCase(ColNames[fc])) 319 DebugFile.writeln(BaseTable + "." + PrimaryKeys[fk] + " as same SQLType and size as " + oTblDef.BaseTable + "." + oTblDef.PrimaryKeys[pk] + " but it is not considered match because " + PrimaryKeys[fk] + " is a single primary key column and they don't have the same name"); 320 } 321 } 322 } } if (iMatchCount==oTblDef.cPKs) break; 325 326 } } } 329 330 if (DebugFile.trace) DebugFile.writeln("match count = " + String.valueOf(iMatchCount) + " , primary keys =" + String.valueOf(oTblDef.cPKs)); 331 332 if (iMatchCount<oTblDef.cPKs) bSamePK = false; 333 334 if (DebugFile.trace) { 335 DebugFile.decIdent(); 336 DebugFile.writeln("End DataTblDef.inheritsPK() : " + String.valueOf(bSamePK)); 337 } 338 339 return bSamePK; 340 } 342 344 public boolean bestMatch(int iThisCol, DataTblDef oTblDef, int iParentPK) { 345 int[] aScores = new int[cPKs]; 346 int iPKPos; 347 int iParentCol; 348 int iPKRelativePos = 0; 349 int iBestMatch = -1; 350 int iBestScore = -1; 351 352 if (DebugFile.trace) { 353 DebugFile.writeln("Begin DataTblDef.bestMatch(" + BaseTable + "." + ColNames[iThisCol] + " , " + oTblDef.BaseTable + "." + oTblDef.PrimaryKeys[iParentPK] + ")"); 354 DebugFile.incIdent(); 355 } 356 357 iParentCol = oTblDef.findColumnPosition(oTblDef.PrimaryKeys[iParentPK]); 358 359 for (int c=0; c<this.ColCount & iPKRelativePos<cPKs; c++) 361 if (PrimaryKeyMarks[c] && !ColNames[c].equalsIgnoreCase(ColNames[iThisCol])) 362 iPKRelativePos++; 363 else if (PrimaryKeyMarks[c] && ColNames[c].equalsIgnoreCase(ColNames[iThisCol])) 364 break; 365 366 for (int k=0; k<cPKs; k++) { 368 aScores[k] = 0; 369 370 if (PrimaryKeys[k].equalsIgnoreCase(oTblDef.ColNames[iParentCol])) 371 aScores[k] += 5; 373 iPKPos = findColumnPosition(PrimaryKeys[k]); 374 375 if (iPKPos>-1) 376 if (ColTypes[iPKPos]==oTblDef.ColTypes[iParentCol] && 377 ColSizes[iPKPos]==oTblDef.ColSizes[iParentCol]) 378 aScores[k] += 1; } 381 for (int k=0; k<cPKs; k++) { 383 if (aScores[k]>iBestScore) { 384 iBestScore = aScores[k]; 385 iBestMatch = k; 386 } } 389 if (DebugFile.trace) { 390 DebugFile.writeln("pk relative position is " + String.valueOf(iPKRelativePos) + ", best match relative position is " + String.valueOf(iBestMatch)); 391 DebugFile.decIdent(); 392 DebugFile.writeln("End DataTblDef.bestMatch() : " + String.valueOf(iPKRelativePos==iBestMatch)); 393 } 394 return (iPKRelativePos==iBestMatch); 395 396 } 398 400 public boolean isPrimaryKey(int iCol) { 401 402 boolean bRetVal = PrimaryKeyMarks[iCol]; 403 404 return bRetVal; 405 } 407 409 public boolean isPrimaryKey(String sCol) { 410 boolean bRetVal; 411 412 int iCol = findColumnPosition (sCol); 413 414 if (-1==iCol) 415 bRetVal = false; 416 else 417 bRetVal = PrimaryKeyMarks[iCol]; 418 419 return bRetVal; 420 } 422 425 public int cPKs; public boolean bMayInheritPK; 427 private boolean PrimaryKeyMarks[]; public String PrimaryKeys[]; public String ColNames[]; public int ColTypes[]; public int ColSizes[]; public int ColCount; public String BaseTable; 434 435 }
| Popular Tags
|