1 package com.raptus.owxv3.api.dataxs; 2 3 import java.util.Vector ; 4 import java.sql.*; 5 import com.raptus.owxv3.api.dataxs.*; 6 import com.raptus.owxv3.*; 7 8 9 10 11 12 public class OwxUsersManager extends SQLDatabaseManager 13 14 15 16 17 { 18 19 20 21 22 23 24 protected static final String tableNamebase = "owx_users"; 25 26 public OwxUsersManager() 27 { 28 tableID = tableNamebase; 29 initialize(); 30 } 31 32 public OwxUsersManager(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 USER_NAME = 0; 43 public static final int USER_PASSWORD = 1; 44 public static final int NAME = 2; 45 public static final int EMAIL = 3; 46 public static final int LOCALE = 4; 47 48 protected void initialize() 49 { 50 ALL_FIELDS = "\"user_name\"" 51 + ",\"user_password\"" 52 + ",\"name\"" 53 + ",\"email\"" 54 + ",\"locale\""; 55 S2J_FIELD_NAMES = new String [] 56 { 57 tableID + ".\"user_name\"", 58 tableID + ".\"user_password\"", 59 tableID + ".\"name\"", 60 tableID + ".\"email\"", 61 tableID + ".\"locale\"" 62 }; 63 } 64 65 public OwxUsers loadByKey(String _userName) throws SQLException { 66 Connection _conn = null; 67 try { 68 _conn = openConnection(); 69 OwxUsers _val = loadByKey(_userName, _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 OwxUsers loadByKey(String _userName, Connection _conn) throws SQLException { 80 PreparedStatement _pstmt = null; 81 try { 82 _pstmt = _conn.prepareStatement("SELECT " + ALL_FIELDS + " FROM " + tableID + " WHERE \"user_name\"=?"); 83 _pstmt.setString(1, _userName); 84 OwxUsers _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 OwxUsers[] loadAll() throws SQLException { 96 Connection _conn = null; 97 try { 98 _conn = openConnection(); 99 OwxUsers _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 OwxUsers[] loadAll(Connection _conn) throws SQLException { 110 PreparedStatement _pstmt = null; 111 try { 112 _pstmt = _conn.prepareStatement("SELECT " + ALL_FIELDS + " FROM " + tableID ); 113 OwxUsers _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 OwxUsers[] loadByPreparedStatement(PreparedStatement pstmt) throws SQLException { 124 return loadByPreparedStatement(pstmt, null, 0); 125 } 126 127 public OwxUsers[] loadByPreparedStatement(PreparedStatement pstmt, int maxRows) throws SQLException { 128 return loadByPreparedStatement(pstmt, null, maxRows); 129 } 130 131 public OwxUsers[] loadByPreparedStatement(PreparedStatement pstmt, int[] fieldList) throws SQLException { 132 return loadByPreparedStatement(pstmt, fieldList, 0); 133 } 134 135 public OwxUsers[] 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 OwxUsers list[] = new OwxUsers[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 OwxUsers[] loadByWhere(String where) throws SQLException { 156 return loadByWhere(where, (int[])null, 0, (String )null); 157 } 158 public OwxUsers[] loadByWhere(String where, int maxRows) throws SQLException { 159 return loadByWhere(where, (int[])null, maxRows, (String )null); 160 } 161 public OwxUsers[] loadByWhere(String where, String orderby, int maxRows) throws SQLException { 162 return loadByWhere(where, (int[])null, maxRows, orderby); 163 } 164 public OwxUsers[] loadByWhere(String where, int[] fieldList) throws SQLException { 165 return loadByWhere(where, fieldList, 0, (String )null); 166 } 167 public OwxUsers[] loadByWhere(String where, int[] fieldList, int maxRows, String orderby) throws SQLException { 168 Connection conn = null; 169 try { 170 conn = openConnection(); 171 OwxUsers _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 OwxUsers[] loadByWhere(String where, Connection conn) throws SQLException { 182 return loadByWhere(where, conn, (int[])null, 0, (String )null); 183 } 184 185 public OwxUsers[] loadByWhere(String where, Connection conn, int[] fieldList) throws SQLException { 186 return loadByWhere(where, conn, fieldList, 0, (String )null); 187 } 188 189 public OwxUsers[] 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 OwxUsers _list[] = new OwxUsers[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(String _userName) throws SQLException { 237 Connection _conn = null; 238 try { 239 _conn = openConnection(); 240 int _rows = deleteByKey(_userName, _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(String _userName, Connection _conn) throws SQLException { 251 PreparedStatement _pstmt = null; 252 try { 253 _pstmt = _conn.prepareStatement("DELETE from " + tableID + " WHERE \"user_name\"=?"); 254 _pstmt.setString(1, _userName); 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(OwxUsers 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(OwxUsers 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.userNameIsModifiedS2j()) { _sql.append("\"user_name\"").append(","); _dirtyCount++; } 286 if(obj.userPasswordIsModifiedS2j()) { _sql.append("\"user_password\"").append(","); _dirtyCount++; } 287 if(obj.nameIsModifiedS2j()) { _sql.append("\"name\"").append(","); _dirtyCount++; } 288 if(obj.emailIsModifiedS2j()) { _sql.append("\"email\"").append(","); _dirtyCount++; } 289 if(obj.localeIsModifiedS2j()) { _sql.append("\"locale\"").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.userNameIsModifiedS2j()) { _pstmt.setString(++_dirtyCount, obj.getUserName()); } 299 if(obj.userPasswordIsModifiedS2j()) { _pstmt.setString(++_dirtyCount, obj.getUserPassword()); } 300 if(obj.nameIsModifiedS2j()) { _pstmt.setString(++_dirtyCount, obj.getName()); } 301 if(obj.emailIsModifiedS2j()) { _pstmt.setString(++_dirtyCount, obj.getEmail()); } 302 if(obj.localeIsModifiedS2j()) { _pstmt.setString(++_dirtyCount, obj.getLocale()); } 303 _pstmt.executeUpdate(); 304 obj.setIsNew(false); 305 obj.resetIsModifiedS2J(); 306 } 307 else { 308 StringBuffer _sql = new StringBuffer ("UPDATE " + tableID + " SET "); 309 if(obj.userNameIsModifiedS2j()) { _sql.append("\"user_name\"").append("=?,"); } 310 if(obj.userPasswordIsModifiedS2j()) { _sql.append("\"user_password\"").append("=?,"); } 311 if(obj.nameIsModifiedS2j()) { _sql.append("\"name\"").append("=?,"); } 312 if(obj.emailIsModifiedS2j()) { _sql.append("\"email\"").append("=?,"); } 313 if(obj.localeIsModifiedS2j()) { _sql.append("\"locale\"").append("=?,"); } 314 _sql.setLength(_sql.length() - 1); 315 _sql.append(" WHERE "); 316 _sql.append("\"user_name\"=?"); 317 _pstmt = _conn.prepareStatement(_sql.toString()); 318 int _dirtyCount = 0; 319 if(obj.userNameIsModifiedS2j()) { _pstmt.setString(++_dirtyCount, obj.getUserName()); } 320 if(obj.userPasswordIsModifiedS2j()) { _pstmt.setString(++_dirtyCount, obj.getUserPassword()); } 321 if(obj.nameIsModifiedS2j()) { _pstmt.setString(++_dirtyCount, obj.getName()); } 322 if(obj.emailIsModifiedS2j()) { _pstmt.setString(++_dirtyCount, obj.getEmail()); } 323 if(obj.localeIsModifiedS2j()) { _pstmt.setString(++_dirtyCount, obj.getLocale()); } 324 _pstmt.setString(++_dirtyCount, obj.getUserName()); 325 _pstmt.executeUpdate(); 326 obj.resetIsModifiedS2J(); 327 } 328 _pstmt.close(); 329 } 330 catch(SQLException e) { 331 if(_pstmt!=null) try { _pstmt.close();} catch(Exception e2) {} 332 throw e; 333 } 334 } 335 336 private OwxUsers decodeRow(ResultSet rs) throws SQLException { 337 OwxUsers obj = new OwxUsers(); 338 obj.setUserName(rs.getString(1)); 339 obj.setUserPassword(rs.getString(2)); 340 obj.setName(rs.getString(3)); 341 obj.setEmail(rs.getString(4)); 342 obj.setLocale(rs.getString(5)); 343 obj.setIsNew(false); 344 obj.resetIsModifiedS2J(); 345 346 return obj; 347 } 348 349 private OwxUsers decodeRow(ResultSet rs, int[] fieldList) throws SQLException { 350 OwxUsers obj = new OwxUsers(); 351 int pos = 0; 352 for(int i = 0; i < fieldList.length; i++) { 353 switch(fieldList[i]) { 354 case USER_NAME: obj.setUserName(rs.getString(++pos)); 355 break; 356 case USER_PASSWORD: obj.setUserPassword(rs.getString(++pos)); 357 break; 358 case NAME: obj.setName(rs.getString(++pos)); 359 break; 360 case EMAIL: obj.setEmail(rs.getString(++pos)); 361 break; 362 case LOCALE: obj.setLocale(rs.getString(++pos)); 363 break; 364 } 365 } 366 obj.setIsNew(false); 367 obj.resetIsModifiedS2J(); 368 369 return obj; 370 } 371 372 } 373 | Popular Tags |