X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Fdatabase%2FGigiPreparedStatement.java;h=eae804b800d20fb6cc29de98ff3c56a624b56a66;hb=12af1eb93bac7e18ca48e9bcf67ce72648132b46;hp=81e5f4e31aa27004efb9df0468b1a2b31784b516;hpb=3e123160ad59a2e1162518923965562ff947b6d1;p=gigi.git diff --git a/src/org/cacert/gigi/database/GigiPreparedStatement.java b/src/org/cacert/gigi/database/GigiPreparedStatement.java index 81e5f4e3..eae804b8 100644 --- a/src/org/cacert/gigi/database/GigiPreparedStatement.java +++ b/src/org/cacert/gigi/database/GigiPreparedStatement.java @@ -6,26 +6,43 @@ 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); } } - 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 +120,21 @@ 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; + try { + DatabaseConnection.getInstance().returnStatement(tg); + } catch (SQLException e) { + throw new Error(e); + } + + } + }