the fix for poolman and OID (Postgres Large Objects). a hack until real BLOBS.
authormh <mh>
Fri, 8 Feb 2002 13:41:05 +0000 (13:41 +0000)
committermh <mh>
Fri, 8 Feb 2002 13:41:05 +0000 (13:41 +0000)
source/mircoders/entity/EntityImages.java

index cb890dc..ac7159c 100755 (executable)
@@ -5,6 +5,15 @@ import java.io.*;
 import java.util.*;
 import java.sql.*;
 
+/*
+ * kind of hack for postgres non-standard LargeObjects that Poolman
+ * doesn't know about. see all the casting, LargeObj stuff in getIcon, getImage
+ * at some point when postgres has normal BLOB support, this should go.
+ */
+import org.postgresql.Connection;
+import org.postgresql.largeobject.LargeObject;
+import org.postgresql.largeobject.LargeObjectManager;
+
 import mir.entity.*;
 import mir.misc.*;
 import mir.storage.*;
@@ -37,25 +46,47 @@ public class EntityImages extends Entity
        public byte[] getImage() throws StorageObjectException
        {
                theLog.printDebugInfo("--getimage started");
-               Connection con=null;Statement stmt=null;
+               java.sql.Connection con=null;Statement stmt=null;
                byte[] img_data=null;
 
                try {
                        con = theStorageObject.getPooledCon();
                        con.setAutoCommit(false);
-                       stmt = con.createStatement();
-                       ResultSet rs = theStorageObject.executeSql(stmt,"select image_data from images where id="+getId());
+                       LargeObjectManager lom;
+            java.sql.Connection jCon;
+            stmt = con.createStatement();
+                       ResultSet rs = theStorageObject.executeSql(stmt,
+                            "select image_data from images where id="+getId());
+            jCon = ((com.codestudio.sql.PoolManConnectionHandle)con)
+                    .getNativeConnection();
+            lom = ((org.postgresql.Connection)jCon).getLargeObjectAPI();
                        if(rs!=null) {
-                               if (rs.next()) {
-                                        img_data = rs.getBytes(1);
-                               }
-                               rs.close();
+              if (rs.next()) {
+                LargeObject lob = lom.open(rs.getInt(1));
+                img_data = lob.read(lob.size());
+                System.err.println("LOB IMG SIZE: "+lob.size());
+                lob.close();
+                System.err.println("res set img NOT NULL2");
+                //img_data = rs.getBytes(1);
+              }
+            rs.close();
                        }
-               }
-               catch (Exception e) {throwStorageObjectException(e, "-- getImage gescheitert: ");}
-               finally {
-                       try {con.setAutoCommit(true); } catch (Exception e) {;}
-                       theStorageObject.freeConnection(con,stmt); }
+               } catch (Exception e) {
+          e.printStackTrace();
+          theLog.printError("EntityImages -- getImage failed"+e.toString()); 
+          throwStorageObjectException(e, "EntityImages -- getImage failed: ");
+        }
+        finally {
+          try {
+            con.setAutoCommit(true);
+          } catch (Exception e) {
+            e.printStackTrace();
+            theLog.printError(
+                    "EntityImages -- getImage reseting transaction mode failed"
+                    +e.toString()); 
+          }
+          theStorageObject.freeConnection(con,stmt);
+        }
 
                return img_data;
        }
@@ -70,7 +101,7 @@ public class EntityImages extends Entity
          //end hack
 
                if (uploadData!=null) {
-                       Connection con=null;PreparedStatement pstmt=null;
+                       java.sql.Connection con=null;PreparedStatement pstmt=null;
                        try {
 
                                theLog.printDebugInfo("settimage :: making internal representation of image");
@@ -128,27 +159,50 @@ public class EntityImages extends Entity
 
        public byte[] getIcon() throws StorageObjectException
        {
-               Connection con=null;Statement stmt=null;
+               java.sql.Connection con=null;Statement stmt=null;
                byte[] img_data=null;
 
                try {
                        con = theStorageObject.getPooledCon();
                        con.setAutoCommit(false);
+            LargeObjectManager lom;
+            java.sql.Connection jCon;
                        stmt = con.createStatement();
-                       ResultSet rs = theStorageObject.executeSql(stmt,"select icon_data from images where id="+getId());
+                       ResultSet rs = theStorageObject.executeSql(stmt,
+                            "select icon_data from images where id="+getId());
+            jCon = ((com.codestudio.sql.PoolManConnectionHandle)con)
+                    .getNativeConnection();
+            lom = ((org.postgresql.Connection)jCon).getLargeObjectAPI();
                        if(rs!=null) {
+                System.err.println("res set NOT NULL");
                                if (rs.next()) {
-                                        img_data = rs.getBytes(1);
+                  LargeObject lob = lom.open(rs.getInt(1));
+                  img_data = lob.read(lob.size());
+                  System.err.println("LOB SIZE: "+lob.size());
+                  lob.close();
+                  System.err.println("res set NOT NULL2");
+                  //img_data = rs.getBytes(1);
+                  System.err.println("res set NOT NULL3");
                                }
-                               rs.close();
+                rs.close();
                        }
-               }
-               catch (Exception e) {throwStorageObjectException(e, "-- getIcon gescheitert: ");}
-               finally {
-                       try {con.setAutoCommit(true); } catch (Exception e) {;}
-                       theStorageObject.freeConnection(con,stmt); }
-
-               return img_data;
+               } catch (Exception e) {
+          e.printStackTrace();
+          theLog.printError("EntityImages -- getIcon failed"+e.toString()); 
+          throwStorageObjectException(e, "EntityImages -- getIcon failed:");
+               } finally {
+          try {
+            con.setAutoCommit(true);
+          } catch (Exception e) {
+            e.printStackTrace();
+            theLog.printError(
+                    "EntityImages -- getIcon reseting transaction mode failed"
+                    +e.toString()); 
+          }
+          theStorageObject.freeConnection(con,stmt);
+       }
+
+       return img_data;
        }
 
 }