X-Git-Url: https://code.wpia.club/?p=gigi.git;a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2Fdatabase%2FGigiPreparedStatement.java;h=4dea5f981f68eb28679734eb1ed88c74e2dc7328;hp=55ed6ad3d53d003ae6639c575dc19147a36cdfd0;hb=b37c20b3c3f2bc96ee9a93ac67949e523969be66;hpb=e0d60e5c156b4ab7bf00084d03f8c6bd98cca817 diff --git a/src/org/cacert/gigi/database/GigiPreparedStatement.java b/src/org/cacert/gigi/database/GigiPreparedStatement.java index 55ed6ad3..4dea5f98 100644 --- a/src/org/cacert/gigi/database/GigiPreparedStatement.java +++ b/src/org/cacert/gigi/database/GigiPreparedStatement.java @@ -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); + } + + } + }