add experimental Fountain.DatabaseColumn
authoradam <adam@megacz.com>
Sat, 17 Mar 2007 08:50:30 +0000 (08:50 +0000)
committeradam <adam@megacz.com>
Sat, 17 Mar 2007 08:50:30 +0000 (08:50 +0000)
darcs-hash:20070317085030-5007d-491876651021b006d25b7aa7578363f2dbab97a3.gz

src/org/ibex/io/Fountain.java

index dd21658..849544f 100644 (file)
@@ -77,6 +77,55 @@ public interface Fountain {
         }
     }
 
+    // FIXME: untested
+    // note: only one ResultSet is allowed per Statement
+    public static class DatabaseColumn implements Fountain {
+        private ResultSet rs;
+        private int row;
+        private int column;
+        public DatabaseColumn(ResultSet rs, int column) throws SQLException {
+            this(rs, column, rs.getRow());
+        }
+        public DatabaseColumn(ResultSet rs, int column, int row) throws SQLException {
+            this.rs = rs;
+            this.row = row;
+            this.column = column;
+            synchronized(rs) {
+                int type = rs.getType();
+                if ((type | ResultSet.TYPE_SCROLL_INSENSITIVE) == 0)
+                    throw new RuntimeException("Fountain.DatabaseColumn(ResultSet,int) requires a SCROLL_INSENSITIVE ResultSet");
+                if ((type | ResultSet.TYPE_FORWARD_ONLY) != 0)
+                    throw new RuntimeException("Fountain.DatabaseColumn(ResultSet,int) cannot use TYPE_FORWARD_ONLY ResultSets");
+            }
+            // FIXME: do we want to check HOLD_CURSORS_OVER_COMMIT ?
+        }
+        private Blob getBlob() throws SQLException {
+            synchronized(rs) {
+                if (!rs.absolute(row))
+                    throw new RuntimeException("resultset was unable to move to desired row");
+                return rs.getBlob(column);
+            }
+        }
+        public Stream getStream() {
+            try {
+                synchronized(rs) {
+                    return new Stream(rs.getBinaryStream(column));
+                }
+            } catch (SQLException e) { throw new RuntimeException(e); }
+        }
+        public long getLength() {
+            try {
+                synchronized(rs) {
+                    return getBlob().length();
+                }
+            } catch (SQLException e) { throw new RuntimeException(e); }
+        }
+        public int getNumLines()       { 
+            throw new RuntimeException("not implemented");
+        } 
+    }
+
+
     //public static class LazyCachingStreamFountain implements Fountain {
     //}