1 32 33 package com.knowgate.dataobjs; 34 35 import java.sql.SQLException ; 36 import java.sql.PreparedStatement ; 37 import java.sql.Statement ; 38 import java.sql.ResultSet ; 39 40 import java.util.TreeSet ; 41 42 import com.knowgate.debug.DebugFile; 43 import com.knowgate.jdc.JDCConnection; 44 45 50 51 public class DBKeySet extends TreeSet { 52 private String sTable; 53 private String sColumn; 54 private String sWhere; 55 private int iMaxRows; 56 57 63 public DBKeySet(String sTableName, String sColumnName, String sWhereClause, int iLimit) { 64 sTable = sTableName; 65 sColumn = sColumnName; 66 sWhere = sWhereClause; 67 iMaxRows = (iLimit>0 ? iLimit : 2147483647); 68 } 69 70 72 private String composeSQL(int iDbms, int iMaxRows) { 73 String 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 100 106 public int load (JDCConnection oConn) throws SQLException { 107 String sSQL = composeSQL(oConn.getDataBaseProduct(), iMaxRows); 108 Statement oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 109 if (DebugFile.trace) DebugFile.writeln("Statement.eecuteQuery("+sSQL+")"); 110 ResultSet oRSet = oStmt.executeQuery(sSQL); 111 try { if (iMaxRows<2147483647) oRSet.setFetchSize(iMaxRows); else oRSet.setFetchSize(1000); } catch (SQLException 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 125 132 public int load (JDCConnection oConn, Object [] aParams) throws SQLException { 133 String sSQL = composeSQL(oConn.getDataBaseProduct(), iMaxRows); 134 if (DebugFile.trace) DebugFile.writeln("Connection.prepareStatement("+sSQL+")"); 135 PreparedStatement 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 oRSet = oStmt.executeQuery(); 139 try { if (iMaxRows<2147483647) oRSet.setFetchSize(iMaxRows); else oRSet.setFetchSize(1000); } catch (SQLException 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 153 public int count(JDCConnection oConn) throws SQLException { 154 Statement oStmt = oConn.createStatement(); 155 ResultSet 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 } 164
| Popular Tags
|