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