From 0c51dcdb8aaf8781a09621351186488383dc841a Mon Sep 17 00:00:00 2001 From: johncurrier Date: Sun, 7 Aug 2005 00:19:08 +0000 Subject: [PATCH] Better handling of row counting --- .../sourceforge/schemaspy/model/Table.java | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/net/sourceforge/schemaspy/model/Table.java b/src/net/sourceforge/schemaspy/model/Table.java index 420c939..165105e 100755 --- a/src/net/sourceforge/schemaspy/model/Table.java +++ b/src/net/sourceforge/schemaspy/model/Table.java @@ -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('.'); @@ -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();