]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/database/GigiPreparedStatement.java
upd: cleanup SQL statements to make them statically verifiable.
[gigi.git] / src / org / cacert / gigi / database / GigiPreparedStatement.java
index 6d967648e9695a0c0e8781c95888b571904a4bed..a779f965fb56c150a2329b9dc6bddb6fd4842424 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 {
 
     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);
@@ -62,6 +76,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,9 +125,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);
+        }
+
+    }
+
 }