X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Fclub%2Fwpia%2Fgigi%2Fdatabase%2FGigiPreparedStatement.java;fp=src%2Fclub%2Fwpia%2Fgigi%2Fdatabase%2FGigiPreparedStatement.java;h=4957d4d3edbb8221b53fa3ad369dab99b1698759;hb=bccd4cc0dba0f89aa045b113bac46eb8cc1dab4e;hp=0000000000000000000000000000000000000000;hpb=c9ed09f0007fc2c813815be927a5a24b23dab83c;p=gigi.git diff --git a/src/club/wpia/gigi/database/GigiPreparedStatement.java b/src/club/wpia/gigi/database/GigiPreparedStatement.java new file mode 100644 index 00000000..4957d4d3 --- /dev/null +++ b/src/club/wpia/gigi/database/GigiPreparedStatement.java @@ -0,0 +1,170 @@ +package club.wpia.gigi.database; + +import java.sql.Date; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Timestamp; + +public class GigiPreparedStatement implements AutoCloseable { + + private PreparedStatement target; + + 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 rs = new GigiResultSet(target.executeQuery()); + } catch (SQLException e) { + handleSQL(e); + throw new Error(e); + } + } + + public void executeUpdate() { + try { + 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); + } + } + + public boolean execute() { + try { + return target.execute(); + } catch (SQLException e) { + handleSQL(e); + throw new Error(e); + } + } + + public void setInt(int parameterIndex, int x) { + try { + target.setInt(parameterIndex, x); + } catch (SQLException e) { + handleSQL(e); + throw new Error(e); + } + } + + public void setString(int parameterIndex, String x) { + try { + target.setString(parameterIndex, x); + } catch (SQLException e) { + handleSQL(e); + throw new Error(e); + } + } + + 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); + } catch (SQLException e) { + handleSQL(e); + throw new Error(e); + } + } + + public void setTimestamp(int parameterIndex, Timestamp x) { + try { + target.setTimestamp(parameterIndex, x); + } catch (SQLException e) { + handleSQL(e); + throw new Error(e); + } + } + + public int lastInsertId() { + try { + ResultSet rs = target.getGeneratedKeys(); + rs.next(); + int id = rs.getInt(1); + rs.close(); + return id; + } catch (SQLException e) { + handleSQL(e); + throw new Error(e); + } + } + + public void setBoolean(int parameterIndex, boolean x) { + try { + target.setBoolean(parameterIndex, x); + } catch (SQLException e) { + handleSQL(e); + throw new Error(e); + } + } + + 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); + } + + } + +}