debugging DatabaseCache
[mir.git] / source / mir / storage / Database.java
index 5b37923..32eb579 100755 (executable)
@@ -37,7 +37,7 @@ public class Database implements StorageObject {
   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;
@@ -47,7 +47,6 @@ public class Database implements StorageObject {
   private int                         defaultLimit;
   protected DatabaseAdaptor             theAdaptor;
   protected Logfile                   theLog;
-  //protected Connection                con;
 
   /**
    * Kontruktor bekommt den Filenamen des Konfigurationsfiles übergeben.
@@ -217,13 +216,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));
@@ -254,7 +253,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();
@@ -434,10 +433,16 @@ public class Database implements StorageObject {
           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);
         }
-        else
-          theLog.printError("Mh. Konnte nicht zaehlen: " + countSql);
       }
+
       // hier select
       rs = executeSql(stmt, selectSql.toString());
       if (rs != null) {
@@ -517,11 +522,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);
@@ -530,13 +534,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());
@@ -1119,6 +1121,7 @@ public class Database implements StorageObject {
   void throwStorageObjectException (String message) throws StorageObjectException {
     _throwStorageObjectException(null, message);
   }
+
 }