select count optimization...
[mir.git] / source / mir / storage / Database.java
index dbfe137..5c40ac2 100755 (executable)
@@ -8,10 +8,11 @@ import  java.lang.*;
 import  java.io.*;
 import  java.util.*;
 import  freemarker.template.*;
-import  com.javaexchange.dbConnectionBroker.*;
 import  mir.storage.StorageObject;
 import  mir.entity.*;
 import  mir.misc.*;
+import com.codestudio.sql.*;
+import com.codestudio.util.*;
 
 
 /**
@@ -27,17 +28,17 @@ import  mir.misc.*;
  */
 public class Database implements StorageObject {
 
-  protected DbConnectionBroker        myBroker;
   protected String                    theTable;
   protected String                    theCoreTable=null;
   protected String                    thePKeyName="id";
   protected int                       thePKeyType;
   protected boolean                   evaluatedMetaData=false;
-  protected ArrayList                 metadataFields,metadataLabels,metadataNotNullFields;
+  protected ArrayList                 metadataFields,metadataLabels,
+                                      metadataNotNullFields;
   protected int[]                     metadataTypes;
   protected Class                     theEntityClass;
   protected StorageObject             myselfDatabase;
-  protected HashMap                   cache;
+  protected DatabaseCache             cache;
   protected SimpleList                popupCache=null;
   protected boolean                   hasPopupCache = false;
   protected SimpleHash                hashCache=null;
@@ -45,9 +46,8 @@ public class Database implements StorageObject {
   private       String                database_driver;
   private       String                database_url;
   private int                         defaultLimit;
-  private DatabaseAdaptor             theAdaptor;
+  protected DatabaseAdaptor             theAdaptor;
   protected Logfile                   theLog;
-  protected Connection                con;
 
   /**
    * Kontruktor bekommt den Filenamen des Konfigurationsfiles übergeben.
@@ -59,25 +59,32 @@ public class Database implements StorageObject {
    *
    * @param   String confFilename Dateiname der Konfigurationsdatei
    */
-  public Database() {
-    theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("Database.Logfile"));
-    String database_username=MirConfig.getProp("Database.Username");
-    String database_password=MirConfig.getProp("Database.Password");
-    String database_host=MirConfig.getProp("Database.Host");
+  public Database() throws StorageObjectException {
+    theLog = Logfile.getInstance(MirConfig.getProp("Home")+
+                                MirConfig.getProp("Database.Logfile"));
     String theAdaptorName=MirConfig.getProp("Database.Adaptor");
+    defaultLimit = Integer.parseInt(MirConfig.getProp("Database.Limit"));
     try {
       theEntityClass = Class.forName("mir.entity.GenericEntity");
       theAdaptor = (DatabaseAdaptor)Class.forName(theAdaptorName).newInstance();
-      defaultLimit = Integer.parseInt(MirConfig.getProp("Database.Limit"));
+    } catch (Exception e){
+      theLog.printError("Error in Database() constructor with "+
+                        theAdaptorName + " -- " +e.toString());
+      throw new StorageObjectException("Error in Database() constructor with "
+                                       +e.toString());
+    }
+    /*String database_username=MirConfig.getProp("Database.Username");
+    String database_password=MirConfig.getProp("Database.Password");
+    String database_host=MirConfig.getProp("Database.Host");
+    try {
       database_driver=theAdaptor.getDriver();
-      database_url=theAdaptor.getURL(database_username,database_password,database_host);
-      theLog.printDebugInfo("adding Broker with: " +database_driver+":"+database_url  );
+      database_url=theAdaptor.getURL(database_username,database_password,
+                                    database_host);
+      theLog.printDebugInfo("adding Broker with: " +database_driver+":"+
+                            database_url  );
       MirConfig.addBroker(database_driver,database_url);
-      myBroker=MirConfig.getBroker();
-    }
-    catch (Exception e){
-      theLog.printError("Bei Konstruktion von Database() with " + theAdaptorName + " -- " +e.toString());
-    }
+      //myBroker=MirConfig.getBroker();
+    }*/
   }
 
   /**
@@ -121,7 +128,8 @@ public class Database implements StorageObject {
 
   /*
    *   Dient dazu vererbte Tabellen bei objectrelationalen DBMS
-   *   zu speichern, wenn die id einer Tabelle in der parenttabelle verwaltet wird.
+   *   zu speichern, wenn die id einer Tabelle in der parenttabelle verwaltet
+   *   wird.
    *   @return liefert theCoreTabel als String zurueck, wenn gesetzt, sonst
    *    the Table
    */
@@ -216,13 +224,13 @@ public class Database implements StorageObject {
             break;
           case java.sql.Types.CHAR:case java.sql.Types.VARCHAR:case java.sql.Types.LONGVARCHAR:
             outValue = rs.getString(valueIndex);
-            if (outValue != null)
-              outValue = StringUtil.encodeHtml(StringUtil.unquote(outValue));
+            //if (outValue != null)
+              //outValue = StringUtil.encodeHtml(StringUtil.unquote(outValue));
             break;
           case java.sql.Types.LONGVARBINARY:
             outValue = rs.getString(valueIndex);
-            if (outValue != null)
-              outValue = StringUtil.encodeHtml(StringUtil.unquote(outValue));
+            //if (outValue != null)
+              //outValue = StringUtil.encodeHtml(StringUtil.unquote(outValue));
             break;
           case java.sql.Types.TIMESTAMP:
             Timestamp timestamp = (rs.getTimestamp(valueIndex));
@@ -253,7 +261,7 @@ public class Database implements StorageObject {
 
     if (id==null||id.equals(""))
       throw new StorageObjectException("id war null");
-    if (cache != null && cache.containsKey(id))
+    if (cache != null && (cache.containsKey(id) > -1))
       return (Entity)cache.get(id);  // wenn cache gesetzt, evtl. kein roundtrip zur Datenbank
 
     Statement stmt=null;Connection con=getPooledCon();
@@ -427,16 +435,22 @@ 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();
-        }
-        else
-          theLog.printError("Mh. Konnte nicht zaehlen: " + 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());
       if (rs != null) {
@@ -452,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);
@@ -466,6 +495,9 @@ public class Database implements StorageObject {
         if (offset + offsetCount < count) {
           theReturnList.setNextBatch(offset + limit);
         }
+
+
+
       }
     } catch (SQLException sqe) {
       throwSQLException(sqe, "selectByWhereClause");
@@ -475,6 +507,7 @@ public class Database implements StorageObject {
     return  theReturnList;
   }
 
+
   /**
    *  Bastelt aus einer Zeile der Datenbank ein EntityObjekt.
    *
@@ -482,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;
@@ -516,11 +549,10 @@ public class Database implements StorageObject {
           theResultHash.put(metadataFields.get(i), theResult);
         }
       }
-      if (cache != null && theResultHash.containsKey(thePKeyName) && cache.containsKey((String)theResultHash.get(thePKeyName))) {
-        //theLog.printDebugInfo("CACHE: (out) "+ theResultHash.get(thePKeyName)+ " :"+theTable);
+      if (cache != null && theResultHash.containsKey(thePKeyName) &&
+          (cache.containsKey((String)theResultHash.get(thePKeyName)) > -1)) {
         returnEntity = (Entity)cache.get((String)theResultHash.get(thePKeyName));
-      }
-      else {
+      } else {
         if (theEntityClass != null) {
           returnEntity = (Entity)theEntityClass.newInstance();
           returnEntity.setValues(theResultHash);
@@ -529,13 +561,11 @@ public class Database implements StorageObject {
             //theLog.printDebugInfo("CACHE: ( in) " + returnEntity.getId() + " :"+theTable);
             cache.put(returnEntity.getId(), returnEntity);
           }
-        }
-        else {
+        } else {
           throwStorageObjectException("Interner Fehler theEntityClass nicht gesetzt!");
         }
       }
-    }           // try
-    catch (IllegalAccessException e) {
+    } catch (IllegalAccessException e) {
       throwStorageObjectException("Kein Zugriff! -- " + e.toString());
     } catch (IOException e) {
       throwStorageObjectException("IOException! -- " + e.toString());
@@ -562,7 +592,6 @@ public class Database implements StorageObject {
     //cache
     invalidatePopupCache();
     try {
-      HashMap theEntityValues = theEntity.getValues();
       ArrayList streamedInput = theEntity.streamedInput();
       StringBuffer f = new StringBuffer();
       StringBuffer v = new StringBuffer();
@@ -582,8 +611,8 @@ public class Database implements StorageObject {
               aValue = "?";
             }
             else {
-              if (theEntityValues.containsKey(aField)) {
-                aValue = "'" + StringUtil.quote((String)theEntityValues.get(aField))
+              if (theEntity.hasValueForField(aField)) {
+                aValue = "'" + StringUtil.quote((String)theEntity.getValue(aField))
                     + "'";
               }
             }
@@ -611,7 +640,7 @@ public class Database implements StorageObject {
       pstmt = con.prepareStatement(sql);
       if (streamedInput != null) {
         for (int i = 0; i < streamedInput.size(); i++) {
-          String inputString = (String)theEntityValues.get(streamedInput.get(i));
+          String inputString = (String)theEntity.getValue((String)streamedInput.get(i));
           pstmt.setBytes(i + 1, inputString.getBytes());
         }
       }
@@ -648,7 +677,6 @@ public class Database implements StorageObject {
     Connection con = null;
     PreparedStatement pstmt = null;
     ArrayList streamedInput = theEntity.streamedInput();
-    HashMap theEntityValues = theEntity.getValues();
     String id = theEntity.getId();
     String aField;
     StringBuffer fv = new StringBuffer();
@@ -661,14 +689,14 @@ public class Database implements StorageObject {
       // only normal cases
       if (!(aField.equals(thePKeyName) || aField.equals("webdb_create") ||
           aField.equals("webdb_lastchange") || (streamedInput != null && streamedInput.contains(aField)))) {
-        if (theEntityValues.containsKey(aField)) {
+        if (theEntity.hasValueForField(aField)) {
           if (firstField == false) {
             fv.append(", ");
           }
           else {
             firstField = false;
           }
-          fv.append(aField).append("='").append(StringUtil.quote((String)theEntityValues.get(aField))).append("'");
+          fv.append(aField).append("='").append(StringUtil.quote((String)theEntity.getValue(aField))).append("'");
         }
       }
     }
@@ -691,7 +719,7 @@ public class Database implements StorageObject {
       pstmt = con.prepareStatement(sql.toString());
       if (streamedInput != null) {
         for (int i = 0; i < streamedInput.size(); i++) {
-          String inputString = (String)theEntityValues.get(streamedInput.get(i));
+          String inputString = theEntity.getValue((String)streamedInput.get(i));
           pstmt.setBytes(i + 1, inputString.getBytes());
         }
       }
@@ -752,7 +780,7 @@ public class Database implements StorageObject {
    * eine SimpleList mit Standard-Popupdaten erzeugt werden koennen soll.
    * @return null
    */
-  public SimpleList getPopupData () {
+  public SimpleList getPopupData () throws StorageObjectException {
     return  null;
   }
 
@@ -762,7 +790,8 @@ public class Database implements StorageObject {
    *  @param hasNullValue  Wenn true wird eine leerer  Eintrag fuer die Popups erzeugt.
    *  @return SimpleList Gibt freemarker.template.SimpleList zurueck.
    */
-  public SimpleList getPopupData (String name, boolean hasNullValue) {
+  public SimpleList getPopupData (String name, boolean hasNullValue)
+    throws StorageObjectException {
     return  getPopupData(name, hasNullValue, null);
   }
 
@@ -773,8 +802,8 @@ public class Database implements StorageObject {
    *  @param where  Schraenkt die Selektion der Datensaetze ein.
    *  @return SimpleList Gibt freemarker.template.SimpleList zurueck.
    */
-  public SimpleList getPopupData (String name, boolean hasNullValue, String where) {
-    return  getPopupData(name, hasNullValue, where, null);
+  public SimpleList getPopupData (String name, boolean hasNullValue, String where) throws StorageObjectException {
+   return  getPopupData(name, hasNullValue, where, null);
   }
 
   /**
@@ -785,8 +814,7 @@ public class Database implements StorageObject {
    *  @param order  Gibt ein Feld als Sortierkriterium an.
    *  @return SimpleList Gibt freemarker.template.SimpleList zurueck.
    */
-  public SimpleList getPopupData (String name, boolean hasNullValue, String where,
-      String order) {
+  public SimpleList getPopupData (String name, boolean hasNullValue, String where, String order) throws StorageObjectException {
     // caching
     if (hasPopupCache && popupCache != null)
       return  popupCache;
@@ -794,7 +822,9 @@ public class Database implements StorageObject {
     Connection con = null;
     Statement stmt = null;
     // build sql
-    StringBuffer sql = new StringBuffer("select ").append(thePKeyName).append(",").append(name).append(" from ").append(theTable);
+    StringBuffer sql = new StringBuffer("select ").append(thePKeyName)
+                                        .append(",").append(name).append(" from ")
+                                        .append(theTable);
     if (where != null && !(where.length() == 0))
       sql.append(" where ").append(where);
     sql.append(" order by ");
@@ -805,6 +835,10 @@ public class Database implements StorageObject {
     // execute sql
     try {
       con = getPooledCon();
+    } catch (Exception e) {
+      throw new StorageObjectException(e.toString());
+    }
+    try {
       stmt = con.createStatement();
       ResultSet rs = executeSql(stmt, sql.toString());
       if (rs != null) {
@@ -827,7 +861,8 @@ public class Database implements StorageObject {
         rs.close();
       }
     } catch (Exception e) {
-      theLog.printDebugInfo(e.toString());
+      theLog.printError("getPopupData: "+e.toString());
+      throw new StorageObjectException(e.toString());
     } finally {
       freeConnection(con, stmt);
     }
@@ -857,7 +892,7 @@ public class Database implements StorageObject {
 
   /* invalidates the popupCache
    */
-  private void invalidatePopupCache () {
+  protected void invalidatePopupCache () {
 
     /** @todo  invalidates toooo much */
     popupCache = null;
@@ -869,14 +904,25 @@ public class Database implements StorageObject {
    * @param stmt Statemnt
    * @param sql Sql-String
    * @return ResultSet
-   * @exception StorageObjectException, SQLException
+   * @exception StorageObjectException
    */
-  public ResultSet executeSql (Statement stmt, String sql) throws StorageObjectException,
-      SQLException {
+  public ResultSet executeSql (Statement stmt, String sql)
+    throws StorageObjectException, SQLException
+  {
+    ResultSet rs;
     long startTime = (new java.util.Date()).getTime();
-    ResultSet rs = stmt.executeQuery(sql);
-    theLog.printInfo((new java.util.Date().getTime() - startTime) + "ms. for: "
+    try {
+      rs = stmt.executeQuery(sql);
+      theLog.printInfo((new java.util.Date().getTime() - startTime) + "ms. for: "
         + sql);
+    }
+    catch (SQLException e)
+    {
+      theLog.printDebugInfo("Failed: " + (new java.util.Date().getTime()
+                            - startTime) + "ms. for: "+ sql);
+      throw e;
+    }
+
     return  rs;
   }
 
@@ -887,8 +933,9 @@ public class Database implements StorageObject {
    * @return Liefert ResultSet des Statements zurueck.
    * @exception StorageObjectException, SQLException
    */
-  public ResultSet executeSql (PreparedStatement stmt) throws StorageObjectException,
-      SQLException {
+  public ResultSet executeSql (PreparedStatement stmt)
+    throws StorageObjectException, SQLException {
+
     long startTime = (new java.util.Date()).getTime();
     ResultSet rs = stmt.executeQuery();
     theLog.printInfo((new java.util.Date().getTime() - startTime) + "ms.");
@@ -921,17 +968,28 @@ public class Database implements StorageObject {
       freeConnection(con,stmt);
     }
     theLog.printInfo(theTable + " has "+ result +" rows where " + where);
-    theLog.printInfo((new java.util.Date().getTime() - startTime) + "ms. for: " + sql);
+    theLog.printInfo((new java.util.Date().getTime() - startTime) + "ms. for: "
+                    + sql);
     return result;
   }
 
   public int executeUpdate(Statement stmt, String sql)
     throws StorageObjectException, SQLException
   {
+    int rs;
     long  startTime = (new java.util.Date()).getTime();
-    //theLog.printDebugInfo("trying: "+ sql);
-    int rs = stmt.executeUpdate(sql);
-    theLog.printInfo((new java.util.Date().getTime() - startTime) + "ms. for: " + sql);
+    try
+    {
+      rs = stmt.executeUpdate(sql);
+      theLog.printInfo((new java.util.Date().getTime() - startTime) + "ms. for: "
+                        + sql);
+    }
+    catch (SQLException e)
+    {
+      theLog.printDebugInfo("Failed: " + (new java.util.Date().getTime()
+                            - startTime) + "ms. for: "+ sql);
+      throw e;
+    }
     return rs;
   }
 
@@ -946,9 +1004,13 @@ public class Database implements StorageObject {
       pstmt = con.prepareStatement(sql);
       result = pstmt.executeUpdate();
     }
-    catch (Exception e) {theLog.printDebugInfo("settimage :: setImage gescheitert: "+e.toString());}
+    catch (Exception e) {
+      theLog.printDebugInfo("settimage :: setImage gescheitert: "+e.toString());
+      throw new StorageObjectException("executeUpdate failed: "+e.toString());
+    }
     finally { freeConnection(con,pstmt); }
-    theLog.printInfo((new java.util.Date().getTime() - startTime) + "ms. for: " + sql);
+    theLog.printInfo((new java.util.Date().getTime() - startTime) + "ms. for: "
+                    + sql);
     return result;
   }
 
@@ -957,7 +1019,9 @@ public class Database implements StorageObject {
    * @param md ResultSetMetaData
    * @exception StorageObjectException
    */
-  private void evalMetaData (ResultSetMetaData md) throws StorageObjectException {
+  private void evalMetaData (ResultSetMetaData md)
+    throws StorageObjectException {
+
     this.evaluatedMetaData = true;
     this.metadataFields = new ArrayList();
     this.metadataLabels = new ArrayList();
@@ -1007,48 +1071,31 @@ public class Database implements StorageObject {
     }
   }
 
-  /**
-   * Datenbankverbindung wird geschlossen
-   */
-  public void disconnectPool () {
-    try {
-      myBroker.destroy(100);
-    } catch (SQLException sqe) {
-      ;
-    }
-  }
 
-  /**
-   * Returns Connection-Object out of the PoolBroker.
-   *
-   * @return Connection Object.
-   */
-  public Connection getPooledCon () throws StorageObjectException {
-    if (myBroker != null) {
-      Connection con = myBroker.getConnection();
-      if (con != null)
-        return  con;
+  public Connection getPooledCon() throws StorageObjectException {
+    /* @todo , doublecheck but I'm pretty sure that this is unnecessary. -mh
+      try{
+      Class.forName("com.codestudio.sql.PoolMan").newInstance();
+    } catch (Exception e){
+      throw new StorageObjectException("Could not find the PoolMan Driver"
+                                        +e.toString());
+    }*/
+    Connection con = null;
+    try{
+      con = SQLManager.getInstance().requestConnection();
+    } catch(SQLException e){
+      theLog.printError("could not connect to the database "+e.toString());
+      System.err.println("could not connect to the database "+e.toString());
+      throw new StorageObjectException("Could not connect to the database"+
+                                        e.toString());
     }
-    throw  new StorageObjectException("No connection to database!");
+    return con;
   }
 
-  /**
-   * Connection und StatementObjekt werden geschlossen und an den Connectionpool
-   * zurückgeben
-   * @param con Connection zur Datenbank
-   * @param stmt Statement-Objekt
-   */
-  public void freeConnection (Connection con, Statement stmt) {
-    try {
-      if (stmt != null)
-        stmt.close();
-    } catch (SQLException e1) {
-      theLog.printDebugInfo(e1.toString());
-    }
-    if (con != null)
-      myBroker.freeConnection(con);
-    else
-      theLog.printDebugInfo("Con was null!");
+  public void freeConnection (Connection con, Statement stmt)
+    throws StorageObjectException {
+    SQLManager.getInstance().closeStatement(stmt);
+    SQLManager.getInstance().returnConnection(con);
   }
 
   /**
@@ -1057,7 +1104,8 @@ public class Database implements StorageObject {
    * @param wo Funktonsname, in der die SQLException geworfen wurde
    * @exception StorageObjectException
    */
-  void throwSQLException (SQLException sqe, String wo) throws StorageObjectException {
+  protected void throwSQLException (SQLException sqe, String wo)
+    throws StorageObjectException {
     String state = "";
     String message = "";
     int vendor = 0;
@@ -1072,15 +1120,29 @@ public class Database implements StorageObject {
         sqe.toString());
   }
 
+  protected void _throwStorageObjectException (Exception e, String wo)
+    throws StorageObjectException {
+    if (e != null) {
+        theLog.printError(e.toString()+ wo);
+        throw  new StorageObjectException(wo + e.toString());
+    } else {
+        theLog.printError(wo);
+        throw  new StorageObjectException(wo);
+    }
+
+  }
+
   /**
-   * Loggt Fehlermeldung mit dem Parameter Message und wirft dannach eine StorageObjectException
+   * Loggt Fehlermeldung mit dem Parameter Message und wirft dannach
+   * eine StorageObjectException
    * @param message Nachricht mit dem Fehler
    * @exception StorageObjectException
    */
-  void throwStorageObjectException (String message) throws StorageObjectException {
-    theLog.printError(message);
-    throw  new StorageObjectException(message);
+  void throwStorageObjectException (String message)
+    throws StorageObjectException {
+    _throwStorageObjectException(null, message);
   }
+
 }