X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Fdatabase%2FGigiPreparedStatement.java;h=4dea5f981f68eb28679734eb1ed88c74e2dc7328;hb=c3feb67ae28e66765dfcd2e7d50ddbceb64d92db;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..4dea5f98 100644 --- a/src/org/cacert/gigi/database/GigiPreparedStatement.java +++ b/src/org/cacert/gigi/database/GigiPreparedStatement.java @@ -6,26 +6,56 @@ 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); + } + } + + 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); @@ -59,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); @@ -99,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); + } + + } + }