1 package com.raptus.owxv3.api.dataxs; 2 3 import java.util.Vector ; 4 import java.sql.*; 5 import com.raptus.owxv3.*; 6 7 8 9 10 11 12 public class OwxmsgsManager extends SQLDatabaseManager 13 14 15 16 17 { 18 19 20 21 22 23 24 protected static final String tableNamebase = "owxmsgs"; 25 26 public OwxmsgsManager() 27 { 28 tableID = tableNamebase; 29 initialize(); 30 } 31 32 public OwxmsgsManager(String tableNamespace) 33 { 34 this.tableNamespace = tableNamespace; 35 if(tableNamespace != null && tableNamespace.length() > 0) 36 tableID = tableNamebase + tableNamespace; 37 else 38 tableID = tableNamebase; 39 initialize(); 40 } 41 42 public static final int ROWID = 0; 43 public static final int SRCTBL = 1; 44 public static final int SRCROWID = 2; 45 public static final int FNAME = 3; 46 public static final int LOCALEKEY = 4; 47 public static final int MSG = 5; 48 49 protected void initialize() 50 { 51 ALL_FIELDS = "\"rowid\"" 52 + ",\"srctbl\"" 53 + ",\"srcrowid\"" 54 + ",\"fname\"" 55 + ",\"localekey\"" 56 + ",\"msg\""; 57 S2J_FIELD_NAMES = new String [] 58 { 59 tableID + ".\"rowid\"", 60 tableID + ".\"srctbl\"", 61 tableID + ".\"srcrowid\"", 62 tableID + ".\"fname\"", 63 tableID + ".\"localekey\"", 64 tableID + ".\"msg\"" 65 }; 66 } 67 68 public Owxmsgs loadByKey(int _rowid) throws SQLException { 69 Connection _conn = null; 70 try { 71 _conn = openConnection(); 72 Owxmsgs _val = loadByKey(_rowid, _conn); 73 closeConnection(_conn); 74 return _val; 75 } 76 catch(SQLException e) { 77 if(_conn != null) try { closeConnection(_conn); } catch(SQLException e2) { } 78 throw e; 79 } 80 } 81 82 public Owxmsgs loadByKey(int _rowid, Connection _conn) throws SQLException { 83 PreparedStatement _pstmt = null; 84 try { 85 _pstmt = _conn.prepareStatement("SELECT " + ALL_FIELDS + " FROM " + tableID + " WHERE \"rowid\"=?"); 86 _pstmt.setLong(1, _rowid); 87 Owxmsgs _list[] = loadByPreparedStatement(_pstmt); 88 _pstmt.close(); 89 if(_list.length < 1) return null; 90 else return _list[0]; 91 } 92 catch(SQLException e) { 93 if(_pstmt != null) try { _pstmt.close(); } catch(SQLException e2) { } 94 throw e; 95 } 96 } 97 98 public Owxmsgs[] loadAll() throws SQLException { 99 Connection _conn = null; 100 try { 101 _conn = openConnection(); 102 Owxmsgs _list[] = loadAll(_conn); 103 closeConnection(_conn); 104 return _list; 105 } 106 catch(SQLException e) { 107 if(_conn != null) try { closeConnection(_conn); } catch(SQLException e2) { } 108 throw e; 109 } 110 } 111 112 public Owxmsgs[] loadAll(Connection _conn) throws SQLException { 113 PreparedStatement _pstmt = null; 114 try { 115 _pstmt = _conn.prepareStatement("SELECT " + ALL_FIELDS + " FROM " + tableID ); 116 Owxmsgs _list[] = loadByPreparedStatement(_pstmt); 117 _pstmt.close(); 118 return _list; 119 } 120 catch(SQLException e) { 121 if(_pstmt != null) try { _pstmt.close(); } catch(SQLException e2) { } 122 throw e; 123 } 124 } 125 126 public Owxmsgs[] loadByPreparedStatement(PreparedStatement pstmt) throws SQLException { 127 return loadByPreparedStatement(pstmt, null, 0); 128 } 129 130 public Owxmsgs[] loadByPreparedStatement(PreparedStatement pstmt, int maxRows) throws SQLException { 131 return loadByPreparedStatement(pstmt, null, maxRows); 132 } 133 134 public Owxmsgs[] loadByPreparedStatement(PreparedStatement pstmt, int[] fieldList) throws SQLException { 135 return loadByPreparedStatement(pstmt, fieldList, 0); 136 } 137 138 public Owxmsgs[] loadByPreparedStatement(PreparedStatement pstmt, int[] fieldList, int maxRows) throws SQLException { 139 ResultSet rs = null; 140 try { 141 rs = pstmt.executeQuery(); 142 java.util.Vector v = new java.util.Vector (); 143 while(rs.next() && (maxRows == 0 || v.size() < maxRows)) { 144 if(fieldList == null) v.addElement(decodeRow(rs)); 145 else v.addElement(decodeRow(rs, fieldList)); 146 } 147 rs.close(); 148 Owxmsgs list[] = new Owxmsgs[v.size()]; 149 v.copyInto(list); 150 return list; 151 } 152 catch(SQLException e) { 153 if(rs!=null) try { rs.close();} catch(Exception e2) {} 154 throw e; 155 } 156 } 157 158 public Owxmsgs[] loadByWhere(String where) throws SQLException { 159 return loadByWhere(where, (int[])null, 0, (String )null); 160 } 161 public Owxmsgs[] loadByWhere(String where, int maxRows) throws SQLException { 162 return loadByWhere(where, (int[])null, maxRows, (String )null); 163 } 164 public Owxmsgs[] loadByWhere(String where, String orderby, int maxRows) throws SQLException { 165 return loadByWhere(where, (int[])null, maxRows, orderby); 166 } 167 public Owxmsgs[] loadByWhere(String where, int[] fieldList) throws SQLException { 168 return loadByWhere(where, fieldList, 0, (String )null); 169 } 170 public Owxmsgs[] loadByWhere(String where, int[] fieldList, int maxRows, String orderby) throws SQLException { 171 Connection conn = null; 172 try { 173 conn = openConnection(); 174 Owxmsgs _list[] = loadByWhere(where, conn, fieldList, maxRows, orderby); 175 closeConnection(conn); 176 return _list; 177 } 178 catch(SQLException e) { 179 if(conn!=null) try { closeConnection(conn);} catch(Exception e2) {} 180 throw e; 181 } 182 } 183 184 public Owxmsgs[] loadByWhere(String where, Connection conn) throws SQLException { 185 return loadByWhere(where, conn, (int[])null, 0, (String )null); 186 } 187 188 public Owxmsgs[] loadByWhere(String where, Connection conn, int[] fieldList) throws SQLException { 189 return loadByWhere(where, conn, fieldList, 0, (String )null); 190 } 191 192 public Owxmsgs[] loadByWhere(String where, Connection conn, int[] fieldList, int maxRows, String orderby) 193 throws SQLException { 194 String sql = null; 195 if(fieldList == null) 196 { 197 sql = "select " + ALL_FIELDS + " from " + tableID + " where (" + where + ")"; 198 if(orderby != null) sql += " order by " + orderby; 199 } 200 else 201 { 202 StringBuffer buff = new StringBuffer (128); 203 buff.append("select "); 204 for(int i = 0; i < fieldList.length; i++) 205 { 206 if(i != 0) buff.append(","); 207 buff.append(S2J_FIELD_NAMES[fieldList[i]]); 208 } 209 buff.append(" from " + tableID + " "); 210 buff.append(" where (" + where + ")"); 211 if(orderby != null) 212 buff.append(" order by " + orderby); 213 sql = buff.toString(); 214 } 215 Statement stmt = null; 216 ResultSet rs = null; 217 try { 218 stmt = conn.createStatement(); 219 rs = stmt.executeQuery(sql); 220 java.util.Vector v = new java.util.Vector (); 221 while(rs.next() && (maxRows == 0 || v.size() < maxRows)) { 222 if(fieldList == null) v.addElement(decodeRow(rs)); 223 else v.addElement(decodeRow(rs, fieldList)); 224 } 225 rs.close(); 226 stmt.close(); 227 228 Owxmsgs _list[] = new Owxmsgs[v.size()]; 229 v.copyInto(_list); 230 return _list; 231 } 232 catch(SQLException e) { 233 if(rs!=null) try { rs.close();} catch(Exception e2) {} 234 if(stmt!=null) try { stmt.close();} catch(Exception e2) {} 235 throw e; 236 } 237 } 238 239 public int deleteByKey(int _rowid) throws SQLException { 240 Connection _conn = null; 241 try { 242 _conn = openConnection(); 243 int _rows = deleteByKey(_rowid, _conn); 244 closeConnection(_conn); 245 return _rows; 246 } 247 catch(SQLException e) { 248 if(_conn!=null) try { closeConnection(_conn);} catch(Exception e2) {} 249 throw e; 250 } 251 } 252 253 public int deleteByKey(int _rowid, Connection _conn) throws SQLException { 254 PreparedStatement _pstmt = null; 255 try { 256 _pstmt = _conn.prepareStatement("DELETE from " + tableID + " WHERE \"rowid\"=?"); 257 _pstmt.setLong(1, _rowid); 258 int _rows = _pstmt.executeUpdate(); 259 _pstmt.close(); 260 return _rows; 261 } 262 catch(SQLException e) { 263 if(_pstmt!=null) try { _pstmt.close();} catch(Exception e2) {} 264 throw e; 265 } 266 } 267 268 public void save(Owxmsgs obj) throws SQLException { 269 if(!obj.isModifiedS2J()) return; 270 Connection _conn = null; 271 try { 272 _conn = openConnection(); 273 save(obj, _conn); 274 closeConnection(_conn); 275 } 276 catch(SQLException e) { 277 if(_conn!=null) try { closeConnection(_conn);} catch(Exception e2) {} 278 throw e; 279 } 280 } 281 282 public void save(Owxmsgs obj, Connection _conn) throws SQLException { 283 PreparedStatement _pstmt = null; 284 try { 285 if(obj.isNew()) { 286 int _dirtyCount = 0; 287 StringBuffer _sql = new StringBuffer ("INSERT into " + tableID + " ("); 288 if(obj.rowidIsModifiedS2j()) { _sql.append("\"rowid\"").append(","); _dirtyCount++; } 289 if(obj.srctblIsModifiedS2j()) { _sql.append("\"srctbl\"").append(","); _dirtyCount++; } 290 if(obj.srcrowidIsModifiedS2j()) { _sql.append("\"srcrowid\"").append(","); _dirtyCount++; } 291 if(obj.fnameIsModifiedS2j()) { _sql.append("\"fname\"").append(","); _dirtyCount++; } 292 if(obj.localekeyIsModifiedS2j()) { _sql.append("\"localekey\"").append(","); _dirtyCount++; } 293 if(obj.msgIsModifiedS2j()) { _sql.append("\"msg\"").append(","); _dirtyCount++; } 294 _sql.setLength(_sql.length() - 1); 295 _sql.append(") values ("); 296 for(int i = 0; i < _dirtyCount; i++) _sql.append("?,"); 297 _sql.setLength(_sql.length() - 1); 298 _sql.append(")"); 299 300 _pstmt = _conn.prepareStatement(_sql.toString()); 301 _dirtyCount = 0; 302 if(obj.rowidIsModifiedS2j()) { _pstmt.setLong(++_dirtyCount, obj.getRowid()); } 303 if(obj.srctblIsModifiedS2j()) { _pstmt.setString(++_dirtyCount, obj.getSrctbl()); } 304 if(obj.srcrowidIsModifiedS2j()) { _pstmt.setLong(++_dirtyCount, obj.getSrcrowid()); } 305 if(obj.fnameIsModifiedS2j()) { _pstmt.setString(++_dirtyCount, obj.getFname()); } 306 if(obj.localekeyIsModifiedS2j()) { _pstmt.setString(++_dirtyCount, obj.getLocalekey()); } 307 if(obj.msgIsModifiedS2j()) { _pstmt.setString(++_dirtyCount, obj.getMsg()); } 308 _pstmt.executeUpdate(); 309 _pstmt.close(); 310 _pstmt = _conn.prepareStatement("SELECT currval('" + tableID + "_ROWID_seq')"); 311 ResultSet _rs = _pstmt.executeQuery(); 312 if(_rs.next()) obj.setRowid(_rs.getInt(1)); 313 _rs.close(); 314 obj.setIsNew(false); 315 obj.resetIsModifiedS2J(); 316 } 317 else { 318 StringBuffer _sql = new StringBuffer ("UPDATE " + tableID + " SET "); 319 if(obj.rowidIsModifiedS2j()) { _sql.append("\"rowid\"").append("=?,"); } 320 if(obj.srctblIsModifiedS2j()) { _sql.append("\"srctbl\"").append("=?,"); } 321 if(obj.srcrowidIsModifiedS2j()) { _sql.append("\"srcrowid\"").append("=?,"); } 322 if(obj.fnameIsModifiedS2j()) { _sql.append("\"fname\"").append("=?,"); } 323 if(obj.localekeyIsModifiedS2j()) { _sql.append("\"localekey\"").append("=?,"); } 324 if(obj.msgIsModifiedS2j()) { _sql.append("\"msg\"").append("=?,"); } 325 _sql.setLength(_sql.length() - 1); 326 _sql.append(" WHERE "); 327 _sql.append("\"rowid\"=?"); 328 _pstmt = _conn.prepareStatement(_sql.toString()); 329 int _dirtyCount = 0; 330 if(obj.rowidIsModifiedS2j()) { _pstmt.setLong(++_dirtyCount, obj.getRowid()); } 331 if(obj.srctblIsModifiedS2j()) { _pstmt.setString(++_dirtyCount, obj.getSrctbl()); } 332 if(obj.srcrowidIsModifiedS2j()) { _pstmt.setLong(++_dirtyCount, obj.getSrcrowid()); } 333 if(obj.fnameIsModifiedS2j()) { _pstmt.setString(++_dirtyCount, obj.getFname()); } 334 if(obj.localekeyIsModifiedS2j()) { _pstmt.setString(++_dirtyCount, obj.getLocalekey()); } 335 if(obj.msgIsModifiedS2j()) { _pstmt.setString(++_dirtyCount, obj.getMsg()); } 336 _pstmt.setLong(++_dirtyCount, obj.getRowid()); 337 _pstmt.executeUpdate(); 338 obj.resetIsModifiedS2J(); 339 } 340 _pstmt.close(); 341 } 342 catch(SQLException e) { 343 if(_pstmt!=null) try { _pstmt.close();} catch(Exception e2) {} 344 throw e; 345 } 346 } 347 348 private Owxmsgs decodeRow(ResultSet rs) throws SQLException { 349 Owxmsgs obj = new Owxmsgs(); 350 obj.setRowid(rs.getInt(1)); 351 obj.setSrctbl(rs.getString(2)); 352 obj.setSrcrowid(rs.getInt(3)); 353 obj.setFname(rs.getString(4)); 354 obj.setLocalekey(rs.getString(5)); 355 obj.setMsg(rs.getString(6)); 356 obj.setIsNew(false); 357 obj.resetIsModifiedS2J(); 358 359 return obj; 360 } 361 362 private Owxmsgs decodeRow(ResultSet rs, int[] fieldList) throws SQLException { 363 Owxmsgs obj = new Owxmsgs(); 364 int pos = 0; 365 for(int i = 0; i < fieldList.length; i++) { 366 switch(fieldList[i]) { 367 case ROWID: obj.setRowid(rs.getInt(++pos)); 368 break; 369 case SRCTBL: obj.setSrctbl(rs.getString(++pos)); 370 break; 371 case SRCROWID: obj.setSrcrowid(rs.getInt(++pos)); 372 break; 373 case FNAME: obj.setFname(rs.getString(++pos)); 374 break; 375 case LOCALEKEY: obj.setLocalekey(rs.getString(++pos)); 376 break; 377 case MSG: obj.setMsg(rs.getString(++pos)); 378 break; 379 } 380 } 381 obj.setIsNew(false); 382 obj.resetIsModifiedS2J(); 383 384 return obj; 385 } 386 387 } 388 | Popular Tags |