From fe62c9f1600e15ce6751718539704f56ad1faaec Mon Sep 17 00:00:00 2001 From: rk Date: Sun, 17 Feb 2002 23:30:22 +0000 Subject: [PATCH] select count optimization... --- source/mir/entity/EntityRelation.java | 14 ------- source/mir/storage/Database.java | 69 ++++++++++++++++++++++------------- 2 files changed, 44 insertions(+), 39 deletions(-) diff --git a/source/mir/entity/EntityRelation.java b/source/mir/entity/EntityRelation.java index 5fe44f9d..272a7de8 100755 --- a/source/mir/entity/EntityRelation.java +++ b/source/mir/entity/EntityRelation.java @@ -45,20 +45,6 @@ public class EntityRelation { } /** - * @return Liefert ein freemarker.template.SimpleHash mit den verknuepften - * Entities, wenn es sich um eine TO_ONE Beziehung handelt, ansonsten - * null. - */ - - public SimpleHash getOneAsSimpleHash(Entity entity) throws StorageObjectException { - if (type==TO_ONE) { - Entity returnEntity = getOne(entity); - if (returnEntity!=null) return HTMLTemplateProcessor.makeSimpleHash(returnEntity); - } - return null; - } - - /** * @return Liefert eine freemarker.template.SimpleList mit den verknuepften * Entities, wenn es sich um eine TO_MANY Liste handelt, ansonsten * null. diff --git a/source/mir/storage/Database.java b/source/mir/storage/Database.java index b8d5f59c..5c40ac21 100755 --- a/source/mir/storage/Database.java +++ b/source/mir/storage/Database.java @@ -435,21 +435,21 @@ public class Database implements StorageObject { con = getPooledCon(); stmt = con.createStatement(); // counting rows - if (theAdaptor.hasLimit()) { - rs = executeSql(stmt, countSql.toString()); - if (rs != null) { - if (rs.next()) - count = rs.getInt(1); - rs.close(); - //nothing in the table: return null - if(count<=0){ - freeConnection(con, stmt); - return null; - } - } else { - theLog.printError("Could not count: " + countSql); - } - } + + /** @todo select count(*) should be optimized: + * 1. it should take place after select statement because in some + * cases it is not necessary anymore. + * 2. it is only needed if entitylist needs information about how + * many datasets there are overall + * + * optimization: + * + * if we manage to have the full information about allData already + * with the select statement, then we set it in entitylist, other + * wise we fetch it. /rk + * */ + + // hier select rs = executeSql(stmt, selectSql.toString()); @@ -466,11 +466,26 @@ public class Database implements StorageObject { } rs.close(); } - // making entitylist - if (!(theAdaptor.hasLimit())) - count = offsetCount; + + // making entitylist infos + if (!(theAdaptor.hasLimit())) count = offsetCount; + if (theReturnList != null) { + // now we decide if we have to know an overall count... + count=offsetCount; + if (limit > -1 && offset > -1) { + if (offsetCount==limit) { + rs = executeSql(stmt, countSql.toString()); + if (rs != null) { + if ( rs.next() ) count = rs.getInt(1); + rs.close(); + } + else theLog.printError("Could not count: " + countSql); + } + } + theReturnList.setCount(count); + theReturnList.setOffset(offset); theReturnList.setWhere(wc); theReturnList.setOrder(ob); @@ -480,6 +495,9 @@ public class Database implements StorageObject { if (offset + offsetCount < count) { theReturnList.setNextBatch(offset + limit); } + + + } } catch (SQLException sqe) { throwSQLException(sqe, "selectByWhereClause"); @@ -489,6 +507,7 @@ public class Database implements StorageObject { return theReturnList; } + /** * Bastelt aus einer Zeile der Datenbank ein EntityObjekt. * @@ -496,7 +515,7 @@ public class Database implements StorageObject { * @return Entity Die Entity. */ - public Entity makeEntityFromResultSet (ResultSet rs) throws StorageObjectException { + private Entity makeEntityFromResultSet (ResultSet rs) throws StorageObjectException { HashMap theResultHash = new HashMap(); String theResult = null; int theType; @@ -817,7 +836,7 @@ public class Database implements StorageObject { try { con = getPooledCon(); } catch (Exception e) { - throw new StorageObjectException(e.toString()); + throw new StorageObjectException(e.toString()); } try { stmt = con.createStatement(); @@ -843,7 +862,7 @@ public class Database implements StorageObject { } } catch (Exception e) { theLog.printError("getPopupData: "+e.toString()); - throw new StorageObjectException(e.toString()); + throw new StorageObjectException(e.toString()); } finally { freeConnection(con, stmt); } @@ -967,7 +986,7 @@ public class Database implements StorageObject { } catch (SQLException e) { - theLog.printDebugInfo("Failed: " + (new java.util.Date().getTime() + theLog.printDebugInfo("Failed: " + (new java.util.Date().getTime() - startTime) + "ms. for: "+ sql); throw e; } @@ -1052,7 +1071,7 @@ public class Database implements StorageObject { } } - + public Connection getPooledCon() throws StorageObjectException { /* @todo , doublecheck but I'm pretty sure that this is unnecessary. -mh try{ @@ -1072,7 +1091,7 @@ public class Database implements StorageObject { } return con; } - + public void freeConnection (Connection con, Statement stmt) throws StorageObjectException { SQLManager.getInstance().closeStatement(stmt); @@ -1114,7 +1133,7 @@ public class Database implements StorageObject { } /** - * Loggt Fehlermeldung mit dem Parameter Message und wirft dannach + * Loggt Fehlermeldung mit dem Parameter Message und wirft dannach * eine StorageObjectException * @param message Nachricht mit dem Fehler * @exception StorageObjectException -- 2.11.0