KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > dataobjs > DBKeySet


1 /*
2   Copyright (C) 2003 Know Gate S.L. All rights reserved.
3                       C/Oņa, 107 1š2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.dataobjs;
34
35 import java.sql.SQLException JavaDoc;
36 import java.sql.PreparedStatement JavaDoc;
37 import java.sql.Statement JavaDoc;
38 import java.sql.ResultSet JavaDoc;
39
40 import java.util.TreeSet JavaDoc;
41
42 import com.knowgate.debug.DebugFile;
43 import com.knowgate.jdc.JDCConnection;
44
45 /**
46  * Load a primary keys set from the database into a java.util.TreeSet
47  * @author Sergio Montoro Ten
48  * @version 1.0
49  */

50
51 public class DBKeySet extends TreeSet JavaDoc {
52   private String JavaDoc sTable;
53   private String JavaDoc sColumn;
54   private String JavaDoc sWhere;
55   private int iMaxRows;
56
57   /**
58    * @param sTableName Table Name
59    * @param sColumnName Column Name
60    * @param sWhereClause SQL WHERE clause
61    * @param iLimit Absolute maximum number of primary key to be loaded
62    */

63   public DBKeySet(String JavaDoc sTableName, String JavaDoc sColumnName, String JavaDoc sWhereClause, int iLimit) {
64     sTable = sTableName;
65     sColumn = sColumnName;
66     sWhere = sWhereClause;
67     iMaxRows = (iLimit>0 ? iLimit : 2147483647);
68   }
69
70   // ---------------------------------------------------------------------------
71

72   private String JavaDoc composeSQL(int iDbms, int iMaxRows) {
73     String JavaDoc sSQL;
74     if (iMaxRows<2147483647) {
75       switch (iDbms) {
76         case JDCConnection.DBMS_MSSQL:
77           sSQL = "SELECT " + sColumn + " FROM " + sTable + " WHERE " + sWhere + " OPTION FAST (" + String.valueOf(iMaxRows) + ")";
78           break;
79         case JDCConnection.DBMS_MYSQL:
80           sSQL = "SELECT " + sColumn + " FROM " + sTable + " WHERE " + sWhere + " LIMIT 0," + String.valueOf(iMaxRows);
81           break;
82         case JDCConnection.DBMS_POSTGRESQL:
83           sSQL = "SELECT " + sColumn + " FROM " + sTable + " WHERE " + sWhere + " LIMIT " + String.valueOf(iMaxRows);
84           break;
85         case JDCConnection.DBMS_ORACLE:
86           sSQL = "SELECT " + sColumn + " FROM " + sTable + " WHERE ROWNUM<=" + String.valueOf(iMaxRows) + " AND " + sWhere;
87           break;
88         default:
89           sSQL = "SELECT " + sColumn + " FROM " + sTable + " WHERE " + sWhere;
90       }
91     }
92     else {
93       sSQL = "SELECT " + sColumn + " FROM " + sTable + " WHERE " + sWhere;
94     }
95     return sSQL;
96   }
97
98   // ---------------------------------------------------------------------------
99

100   /**
101    * Load primary keys from the database to this TreeSet
102    * @param oConn JDBC Database Connection
103    * @return Number of keys actually readed
104    * @throws SQLException
105    */

106   public int load (JDCConnection oConn) throws SQLException JavaDoc {
107     String JavaDoc sSQL = composeSQL(oConn.getDataBaseProduct(), iMaxRows);
108     Statement JavaDoc oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
109     if (DebugFile.trace) DebugFile.writeln("Statement.eecuteQuery("+sSQL+")");
110     ResultSet JavaDoc oRSet = oStmt.executeQuery(sSQL);
111     try { if (iMaxRows<2147483647) oRSet.setFetchSize(iMaxRows); else oRSet.setFetchSize(1000); } catch (SQLException JavaDoc ignore) {}
112     clear();
113     int iReaded = 0;
114     while (oRSet.next()) {
115       add(oRSet.getObject(1));
116       iReaded++;
117     }
118     oRSet.close();
119     oStmt.close();
120     return iReaded;
121   }
122
123   // ---------------------------------------------------------------------------
124

125   /**
126    * Load primary keys from the database to this TreeSet
127    * @param oConn JDBC Database Connection
128    * @param aParams Parameters to be binded to prepared SQL
129    * @return Number of keys actually readed
130    * @throws SQLException
131    */

132   public int load (JDCConnection oConn, Object JavaDoc[] aParams) throws SQLException JavaDoc {
133     String JavaDoc sSQL = composeSQL(oConn.getDataBaseProduct(), iMaxRows);
134     if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement("+sSQL+")");
135     PreparedStatement JavaDoc oStmt = oConn.prepareStatement(sSQL, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
136     for (int p=0;p<aParams.length; p++)
137       oStmt.setObject(p+1, aParams[p]);
138     ResultSet JavaDoc oRSet = oStmt.executeQuery();
139     try { if (iMaxRows<2147483647) oRSet.setFetchSize(iMaxRows); else oRSet.setFetchSize(1000); } catch (SQLException JavaDoc ignore) {}
140     clear();
141     int iReaded = 0;
142     while (oRSet.next()) {
143       add(oRSet.getObject(1));
144       iReaded++;
145     }
146     oRSet.close();
147     oStmt.close();
148     return iReaded;
149   }
150
151   // ---------------------------------------------------------------------------
152

153   public int count(JDCConnection oConn) throws SQLException JavaDoc {
154     Statement JavaDoc oStmt = oConn.createStatement();
155     ResultSet JavaDoc oRSet = oStmt.executeQuery("SELECT COUNT("+sColumn+") FROM "+sTable+" WHERE "+sWhere);
156     int iRetVal = oRSet.getInt(1);
157     oRSet.close();
158     oStmt.close();
159     return iRetVal;
160   }
161
162   // ---------------------------------------------------------------------------
163
}
164
Popular Tags