1 package sellwin.db; 2 3 import sellwin.domain.*; 4 import sellwin.utils.*; 5 6 import java.util.ArrayList ; 7 import java.sql.*; 8 9 13 14 19 public class CustomerInventoryDB extends DBType implements DBInterface { 20 private Connection con; 21 22 private final static String deleteQuery = 23 "DELETE FROM customer_inventory WHERE pk = "; 24 25 private final static String insertQuery = 26 "INSERT INTO customer_inventory " + 27 " (PK,CUSTOMER_PK,PRODUCT_PK,CNT,MODIFIED_BY,MODIFIED_DATE) " + 28 "VALUES ("; 29 30 private final static String updateQuery = 31 "UPDATE customer_inventory " + 32 "SET "; 33 34 private final static String selectQuery = 35 "SELECT customer_pk, product_pk, cnt, " + 36 "modified_by, modified_date " + 37 "FROM customer_inventory "+ 38 "WHERE pk = "; 39 private final static String selectAllQuery = 40 "SELECT customer_pk, product_pk, cnt, " + 41 "modified_by, modified_date " + 42 "FROM customer_inventory "+ 43 "WHERE customer_pk = "; 44 45 50 public CustomerInventoryDB() { 51 } 52 53 57 public CustomerInventoryDB(int dbType) { 58 DB_TYPE = dbType; 59 } 60 61 67 public CustomerInventoryDB(Connection con) { 68 this.con = con; 69 } 70 71 76 public Connection getConnection() { 77 return this.con; 78 } 79 80 85 public final void setConnection(Connection con) 86 throws SQLException { 87 88 this.con = con; 89 } 90 91 99 public final Object selectRow(Object pk) 100 throws SQLException { 101 102 CustomerInventory sp = new CustomerInventory(); 103 sp.setPK(((Long )pk).longValue()); 104 105 Statement stmt = null; 106 ResultSet rs = null; 107 String query = selectQuery + sp.getPK(); 108 109 try { 110 stmt = con.createStatement(); 111 if (Prefs.DEBUG) LogWrite.write(query); 112 rs = stmt.executeQuery(query); 113 114 int i; 115 long prodPK=0; 116 117 while (rs.next()) { 118 i=1; 119 sp.setCustomerPK(rs.getLong(i)); i++; 120 121 prodPK = rs.getLong(i); i++; 122 ProductDB pdb = new ProductDB(DB_TYPE); 123 pdb.setConnection(getConnection()); 124 sp.setProduct((Product)pdb.selectRow(new Long (prodPK))); 125 126 sp.setCount(new Integer (rs.getInt(i))); i++; 127 sp.setModifiedBy(rs.getString(i)); i++; 128 sp.setModifiedDate(rs.getDate(i)); 129 } 130 } catch (SQLException e) { 131 throw e; 132 } finally { 133 try { 134 if (rs != null) rs.close(); 135 } catch (SQLException x) { throw x; } 136 try { 137 if (stmt != null) stmt.close(); 138 } catch (SQLException x) { throw x; } 139 } 140 141 return sp; 142 } 143 144 145 154 public final void updateRow(Object obj) 155 throws SQLException { 156 157 CustomerInventory customer_inventory = (CustomerInventory)obj; 158 159 StringBuffer query = new StringBuffer (updateQuery); 160 Statement stmt = con.createStatement(); 161 162 query.append("cnt="); 163 query.append(customer_inventory.getCount().intValue()).append(","); 164 query.append(" WHERE pk="); 165 query.append(customer_inventory.getPK()); 166 167 if (Prefs.DEBUG) LogWrite.write(query.toString()); 168 int updatedRows = stmt.executeUpdate(query.toString()); 169 } 170 171 180 public final long insertRow(Object obj, boolean load) 181 throws SQLException { 182 183 CustomerInventory customer_inventory = (CustomerInventory)obj; 184 185 if (!load) 189 customer_inventory.setPK(DBUtils.generatePK()); 190 191 Date modified_date = new Date(customer_inventory.getModifiedDate().getTime()); 192 193 StringBuffer query = new StringBuffer (insertQuery); 194 Statement stmt = con.createStatement(); 195 196 query.append(customer_inventory.getPK()).append(","); 197 query.append(customer_inventory.getCustomerPK()).append(","); 198 query.append(customer_inventory.getProduct().getPK()).append(","); 199 query.append(customer_inventory.getCount().intValue()).append(","); 200 query.append(JDBC.quoteMore(customer_inventory.getModifiedBy())); 201 query.append(JDBC.quote(DateUtils.format(DB_TYPE, customer_inventory.getModifiedDate()))); 202 query.append(")"); 203 204 if (Prefs.DEBUG) LogWrite.write(query.toString()); 205 int rc = stmt.executeUpdate(query.toString()); 206 207 return customer_inventory.getPK(); 208 } 209 210 217 public final void deleteRow(Object obj) 218 throws SQLException { 219 220 long pkValue = ((Long )obj).longValue(); 221 222 String query = deleteQuery + pkValue; 223 224 Statement stmt = null; 225 226 try { 227 stmt = con.createStatement(); 228 if (Prefs.DEBUG) LogWrite.write(query); 229 stmt.executeUpdate(query); 230 } catch (SQLException e) { 231 throw e; 232 } finally { 233 try { 234 if (stmt != null) stmt.close(); 235 } catch (SQLException x) { } 236 } 237 } 238 239 247 public final ArrayList selectAllRows(long custpk) 248 throws SQLException { 249 250 CustomerInventory ci=null; 251 ArrayList rows = new ArrayList (); 252 253 Statement stmt = null; 254 ResultSet rs = null; 255 String query = selectAllQuery + custpk; 256 257 try { 258 stmt = con.createStatement(); 259 if (Prefs.DEBUG) LogWrite.write(query); 260 rs = stmt.executeQuery(query); 261 262 int i; 263 long prodPK=0; 264 265 while (rs.next()) { 266 i=1; 267 ci = new CustomerInventory(); 268 ci.setCustomerPK(rs.getLong(i)); i++; 269 270 prodPK = rs.getLong(i); i++; 271 ProductDB pdb = new ProductDB(DB_TYPE); 272 pdb.setConnection(getConnection()); 273 ci.setProduct((Product)pdb.selectRow(new Long (prodPK))); 274 275 ci.setCount(new Integer (rs.getInt(i))); i++; 276 ci.setModifiedBy(rs.getString(i)); i++; 277 ci.setModifiedDate(rs.getDate(i)); 278 rows.add(ci); 279 } 280 } catch (SQLException e) { 281 throw e; 282 } finally { 283 try { 284 if (rs != null) rs.close(); 285 } catch (SQLException x) { throw x; } 286 try { 287 if (stmt != null) stmt.close(); 288 } catch (SQLException x) { throw x; } 289 } 290 291 return rows; 292 } 293 298 public final void truncate() 299 throws SQLException { 300 301 String query = "truncate table customer_inventory"; 302 303 Statement stmt = null; 304 try { 305 stmt = con.createStatement(); 306 if (Prefs.DEBUG) LogWrite.write(query); 307 stmt.executeUpdate(query); 308 } catch (SQLException e) { 309 throw e; 310 } finally { 311 try { if (stmt != null) stmt.close(); 312 } catch (SQLException x) { } 313 } 314 } 315 316 } 317 | Popular Tags |