KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > raptus > owxv3 > api > dataxs > OwxfilesManager


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