X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Fdatabase%2FGigiPreparedStatement.java;h=411b1fd5fb840056fa5000acc58d1d8be7a164aa;hp=81e5f4e31aa27004efb9df0468b1a2b31784b516;hb=a0232b6e40e7e09767f0444d24e18bf12dafc362;hpb=3e123160ad59a2e1162518923965562ff947b6d1 diff --git a/src/org/cacert/gigi/database/GigiPreparedStatement.java b/src/org/cacert/gigi/database/GigiPreparedStatement.java index 81e5f4e3..411b1fd5 100644 --- a/src/org/cacert/gigi/database/GigiPreparedStatement.java +++ b/src/org/cacert/gigi/database/GigiPreparedStatement.java @@ -6,26 +6,47 @@ 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 { + if (scroll) { + target = DatabaseConnection.getInstance().prepareInternalScrollable(stmt); + } else { + target = DatabaseConnection.getInstance().prepareInternal(stmt); + } + } 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); } } - public int executeUpdate() { + public void executeUpdate() { try { - return target.executeUpdate(); + int updated = target.executeUpdate(); + if (updated != 1) { + throw new Error("FATAL: multiple or no data updated: " + updated); + } } catch (SQLException e) { handleSQL(e); throw new Error(e); @@ -103,4 +124,17 @@ public class GigiPreparedStatement { // TODO Auto-generated method stub } + + @Override + public void close() { + GigiResultSet r = rs; + if (r != null) { + r.close(); + } + PreparedStatement tg = target; + target = null; + DatabaseConnection.getInstance().returnStatement(tg); + + } + }