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