/* This class was generated by Cartographer * Licensing info here. */ package dbobjects; import java.io.*; import java.math.*; import java.sql.*; import java.util.*; public class MyTableFactory extends DBObject { // // Constants // /** * */ private String SQL_BASE_QUERY = "SELECT c_id,c_name,c_description FROM t_my_table "; /** * */ private String SQL_UPDATE_QUERY = "UPDATE t_my_table "; /** * */ private String SQL_DELETE_QUERY = "DELETE FROM t_my_table "; // // Class Fields // /** * */ private Connection con; /** * */ private Vector sqlVector; /** * */ private Vector objectVector; /** * */ private Vector comparisonVector; /** * */ private Vector booleanVector; /** * */ private String orderBy; /** * */ private String customSql; // // Constructors // /** * Default Constructor. */ public MyTableFactory() { sqlVector = new Vector(); objectVector = new Vector(); comparisonVector = new Vector(); booleanVector = new Vector(); orderBy = ""; } // // Search Methods // /** * Set the criteria to search for the given id value. */ public void findById(Integer id) { findById("=",id,"AND"); } /** * Set the criteria to search for the given id value. */ public void findById(String comparisonOperator, Integer id) { findById(comparisonOperator,id,"AND"); } /** * Set the criteria to search for the given id value. */ public void findById(String comparisonOperator, Integer id, String booleanOperator) { sqlVector.addElement("c_id"); objectVector.addElement(new DBInteger(id)); comparisonVector.addElement(comparisonOperator); booleanVector.addElement(booleanOperator); } /** * Set the criteria to search for the given name value. */ public void findByName(String name) { findByName("=",name,"AND"); } /** * Set the criteria to search for the given name value. */ public void findByName(String comparisonOperator, String name) { findByName(comparisonOperator,name,"AND"); } /** * Set the criteria to search for the given name value. */ public void findByName(String comparisonOperator, String name, String booleanOperator) { sqlVector.addElement("c_name"); objectVector.addElement(new DBVarChar(name)); comparisonVector.addElement(comparisonOperator); booleanVector.addElement(booleanOperator); } /** * Set the criteria to search for the given description value. */ public void findByDescription(String description) { findByDescription("=",description,"AND"); } /** * Set the criteria to search for the given description value. */ public void findByDescription(String comparisonOperator, String description) { findByDescription(comparisonOperator,description,"AND"); } /** * Set the criteria to search for the given description value. */ public void findByDescription(String comparisonOperator, String description, String booleanOperator) { sqlVector.addElement("c_description"); objectVector.addElement(new DBLongVarChar(description)); comparisonVector.addElement(comparisonOperator); booleanVector.addElement(booleanOperator); } /** * Set the custom criteria to search for. */ public void findByCustomSQL(String criteria) { customSql = criteria; } /** * Set the objects in the prepared statement generated for findByCustomSql(). */ public void setCustomObject(Object obj) { objectVector.addElement(new DBNoType(obj)); } /** * Set the Order By clause in the prepared statement. */ public void orderBy(String orderBy) { this.orderBy = orderBy; } /** * */ public Vector search() { String query = SQL_BASE_QUERY; Vector resultVector = new Vector(); try { con = getConnection(); if( (customSql == null) || (customSql.equals("")) ) { // Note: sqlVector,comparisonVector, and booleanVector should ALWAYS contain the // same number of elements. if( sqlVector.size() > 0 ) { query += "WHERE "; for(int i = 0; i < sqlVector.size()-1; i++) { query += sqlVector.elementAt(i) + " " + (String)comparisonVector.elementAt(i) + " ? " + (String)booleanVector.elementAt(i) + " "; } query += sqlVector.elementAt(sqlVector.size()-1) + " " + (String)comparisonVector.elementAt(comparisonVector.size()-1) + " ? "; if( (orderBy != null) && (!orderBy.equals("")) ) { query += "ORDER BY " + orderBy; } } } else { query += "WHERE " + customSql; } PreparedStatement ps = con.prepareStatement(query); for( int i = 0; i < objectVector.size(); i++) { psSetObject(ps,i+1,(DBField)objectVector.elementAt(i)); } ResultSet rs = ps.executeQuery(); while( rs.next() ) { resultVector.addElement(loadFromRow(rs)); } rs.close(); con.close(); } catch(SQLException sqle) { sqle.printStackTrace(); } clear(); return resultVector; } /** * Searches the database for records that match the given criteria. * This method uses the supplied database connection. */ public Vector search(Connection con) { String query = SQL_BASE_QUERY; Vector resultVector = new Vector(); try { if( (customSql == null) || (customSql.equals("")) ) { // Note: sqlVector,comparisonVector, and booleanVector should ALWAYS contain the // same number of elements. if( sqlVector.size() > 0 ) { query += "WHERE "; for(int i = 0; i < sqlVector.size()-1; i++) { query += sqlVector.elementAt(i) + " " + (String)comparisonVector.elementAt(i) + " ? " + (String)booleanVector.elementAt(i) + " "; } query += sqlVector.elementAt(sqlVector.size()-1) + " " + (String)comparisonVector.elementAt(comparisonVector.size()-1) + " ? "; if( (orderBy != null) && (!orderBy.equals("")) ) { query += "ORDER BY " + orderBy; } } } else { query += "WHERE " + customSql; } PreparedStatement ps = con.prepareStatement(query); for( int i = 0; i < objectVector.size(); i++) { psSetObject(ps,i+1,(DBField)objectVector.elementAt(i)); } ResultSet rs = ps.executeQuery(); while( rs.next() ) { resultVector.addElement(loadFromRow(rs)); } rs.close(); } catch(SQLException sqle) { sqle.printStackTrace(); } clear(); return resultVector; } // // Batch Persistence Methods // /** * */ public int updateBatch(String setString) { String query = SQL_UPDATE_QUERY + setString + " "; int rowCount = 0; try { con = getConnection(); if( (customSql == null) || (customSql.equals("")) ) { // Note: sqlVector,comparisonVector, and booleanVector should ALWAYS contain the // same number of elements. if( sqlVector.size() > 0 ) { query += "WHERE "; for(int i = 0; i < sqlVector.size()-1; i++) { query += sqlVector.elementAt(i) + " " + (String)comparisonVector.elementAt(i) + " ? " + (String)booleanVector.elementAt(i) + " "; } query += sqlVector.elementAt(sqlVector.size()-1) + " " + (String)comparisonVector.elementAt(comparisonVector.size()-1) + " ? "; } } else { query += "WHERE " + customSql; } PreparedStatement ps = con.prepareStatement(query); for( int i = 0; i < objectVector.size(); i++) { psSetObject(ps,i+1,(DBField)objectVector.elementAt(i)); } rowCount = ps.executeUpdate(); con.close(); } catch(SQLException sqle) { sqle.printStackTrace(); } return rowCount; } /** * */ public int deleteBatch() { String query = SQL_DELETE_QUERY; int rowCount = 0; try { con = getConnection(); if( (customSql == null) || (customSql.equals("")) ) { // Note: sqlVector,comparisonVector, and booleanVector should ALWAYS contain the // same number of elements. if( sqlVector.size() > 0 ) { query += "WHERE "; for(int i = 0; i < sqlVector.size()-1; i++) { query += sqlVector.elementAt(i) + " " + (String)comparisonVector.elementAt(i) + " ? " + (String)booleanVector.elementAt(i) + " "; } query += sqlVector.elementAt(sqlVector.size()-1) + " " + (String)comparisonVector.elementAt(comparisonVector.size()-1) + " ? "; } } else { query += "WHERE " + customSql; } PreparedStatement ps = con.prepareStatement(query); for( int i = 0; i < objectVector.size(); i++) { psSetObject(ps,i+1,(DBField)objectVector.elementAt(i)); } rowCount = ps.executeUpdate(); con.close(); } catch(SQLException sqle) { sqle.printStackTrace(); } return rowCount; } // // Utility Methods // /** * */ public void clear() { sqlVector.clear(); objectVector.clear(); comparisonVector.clear(); booleanVector.clear(); } /** * */ private MyTable loadFromRow(ResultSet rs) { MyTable myTable = new MyTable(); try { myTable.setId((Integer)rs.getObject(1)); myTable.setName((String)rs.getObject(2)); myTable.setDescription((String)rs.getObject(3)); } catch(SQLException sqle) { sqle.printStackTrace(); } return myTable; } }// end class MyTableFactory {}