Skip to content

Commit

Permalink
Better handling of row counting
Browse files Browse the repository at this point in the history
  • Loading branch information
johncurrier committed Aug 7, 2005
1 parent eb56ed9 commit 0c51dcd
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/net/sourceforge/schemaspy/model/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -501,9 +501,27 @@ public int getNumRows() {
* @return int
*/
protected int fetchNumRows(Database db) throws SQLException {
try {
// '*' should work best for the majority of cases
return fetchNumRows(db, "count(*)");
} catch (SQLException exc) {
try {
// except nested tables...try using '1' instead
return fetchNumRows(db, "count(1)");
} catch (SQLException try2Exception) {
System.err.println(try2Exception);
System.err.println("Unable to extract the number of rows for table " + getName() + ", using '-1'");
return -1;
}
}
}

protected int fetchNumRows(Database db, String clause) throws SQLException {
PreparedStatement stmt = null;
ResultSet rs = null;
StringBuffer sql = new StringBuffer("select count(*) from ");
StringBuffer sql = new StringBuffer("select ");
sql.append(clause);
sql.append(" from ");
if (getSchema() != null) {
sql.append(getSchema());
sql.append('.');
Expand All @@ -513,15 +531,10 @@ protected int fetchNumRows(Database db) throws SQLException {
try {
stmt = db.getConnection().prepareStatement(sql.toString());
rs = stmt.executeQuery();

while (rs.next()) {
return rs.getInt(1);
}
return -1;
} catch (SQLException sqlException) {
System.err.println("Unable to extract the number of rows for table " + getName() + ", using '-1'");
System.err.println(sql);
return -1;
} finally {
if (rs != null)
rs.close();
Expand Down

0 comments on commit 0c51dcd

Please sign in to comment.