]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/database/GigiPreparedStatement.java
upd: make verification processes more consistent on failure
[gigi.git] / src / org / cacert / gigi / database / GigiPreparedStatement.java
index 55ed6ad3d53d003ae6639c575dc19147a36cdfd0..4dea5f981f68eb28679734eb1ed88c74e2dc7328 100644 (file)
@@ -6,17 +6,31 @@ import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Timestamp;
 
-public class GigiPreparedStatement {
+public class GigiPreparedStatement implements AutoCloseable {
 
-    PreparedStatement target;
+    private PreparedStatement target;
 
-    public GigiPreparedStatement(PreparedStatement preparedStatement) {
+    private GigiResultSet rs;
+
+    protected GigiPreparedStatement(PreparedStatement preparedStatement) {
         target = preparedStatement;
     }
 
+    public GigiPreparedStatement(String stmt) {
+        this(stmt, false);
+    }
+
+    public GigiPreparedStatement(String stmt, boolean scroll) {
+        try {
+            target = DatabaseConnection.getInstance().prepareInternal(stmt, scroll);
+        } catch (SQLException e) {
+            throw new Error(e);
+        }
+    }
+
     public GigiResultSet executeQuery() {
         try {
-            return new GigiResultSet(target.executeQuery());
+            return rs = new GigiResultSet(target.executeQuery());
         } catch (SQLException e) {
             handleSQL(e);
             throw new Error(e);
@@ -35,6 +49,19 @@ public class GigiPreparedStatement {
         }
     }
 
+    public boolean executeMaybeUpdate() {
+        try {
+            int updated = target.executeUpdate();
+            if (updated > 1) {
+                throw new Error("More than one record (" + updated + ") updated.");
+            }
+            return updated == 1;
+        } catch (SQLException e) {
+            handleSQL(e);
+            throw new Error(e);
+        }
+    }
+
     public boolean execute() {
         try {
             return target.execute();
@@ -62,6 +89,15 @@ public class GigiPreparedStatement {
         }
     }
 
+    public void setEnum(int parameterIndex, DBEnum x) {
+        try {
+            target.setString(parameterIndex, x.getDBName());
+        } catch (SQLException e) {
+            handleSQL(e);
+            throw new Error(e);
+        }
+    }
+
     public void setDate(int parameterIndex, Date x) {
         try {
             target.setDate(parameterIndex, x);
@@ -102,8 +138,33 @@ public class GigiPreparedStatement {
         }
     }
 
+    public int getParameterCount() {
+        try {
+            return target.getParameterMetaData().getParameterCount();
+        } catch (SQLException e) {
+            throw new Error(e);
+        }
+    }
+
     private void handleSQL(SQLException e) {
         // TODO Auto-generated method stub
 
     }
+
+    @Override
+    public void close() {
+        GigiResultSet r = rs;
+        if (r != null) {
+            r.close();
+        }
+        PreparedStatement tg = target;
+        target = null;
+        try {
+            DatabaseConnection.getInstance().returnStatement(tg);
+        } catch (SQLException e) {
+            throw new Error(e);
+        }
+
+    }
+
 }