package de.uhilger.baselink; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author Ulrich Hilger */ public class Eraser extends DBActor { private static final Logger logger = Logger.getLogger(Eraser.class.getName()); public Eraser(PersistenceManager pm) { this.pm = pm; } /** * Delete a given object from the database * *

Use this method for single deletes. In cases where * several subsequent deletes for objects of the same class * are required the use of method delete(Object, Record) * is recommended instead to minimise instantiation * overhead.

* * @param o object to delete * @return the deleted object */ public Object delete(Object o) { return delete(o, new GenericRecord(o.getClass())); } /** * Delete a given object from the database * *

Use this method for single deletes. In cases where * several subsequent deletes for objects of the same class * are required the use of method delete(Connection, Object, Record) * is recommended instead to minimise instantiation * overhead.

* * @param c the connection to use, expected to be open and established * @param o object to delete * @return the deleted object */ public Object delete(Connection c, Object o) { return delete(c, o, new GenericRecord(o.getClass())); } /** * Delete a given object from the database * @param o object to delete * @param record reference to object to use to map between object and database * @return the deleted object */ public Object delete(Object o, Record record) { Connection c = null; try { c = pm.getConnection(); o = delete(c, o, record); c.close(); c = null; } catch (SQLException ex) { logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); } finally { pm.closeConnectionFinally(c); } return o; } /** * Delete a given object from the database * @param c the connection to use, expected to be open and established * @param o object to delete * @param record reference to object to use to map between object and database * @return the deleted object */ public Object delete(Connection c, Object o, Record record) { Object deletedObject = null; PreparedStatement ps = null; try { ps = record.getDeleteStatment(c, o); ps.executeUpdate(); ps.close(); ps = null; deletedObject = o; } catch (Exception ex) { logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); } finally { pm.closeStatementFinally(ps); } return deletedObject; } }