11package javaxt .sql ;
22import java .sql .SQLException ;
33import javaxt .utils .Generator ;
4+ import java .util .*;
45
56//******************************************************************************
67//** Connection Class
@@ -182,18 +183,125 @@ public void close(){
182183 //** getRecordset
183184 //**************************************************************************
184185 /** Used to execute a SQL statement and returns a Recordset as an iterator.
185- * This simplifies using the Recordset object insofar as it eliminate the
186- * need to call the hasNext(), moveNext(), and close() methods. Instead,
187- * you can execute a query and iterate through records like this:
188- <pre>
189- Connection conn = db.getConnection();
190- for (Recordset rs : conn.getRecordset("select distinct(name) from contacts")){
186+ * Example:
187+ <pre>
188+ try (javaxt.sql.Connection conn = db.getConnection()){
189+ for (Recordset rs : conn.getRecordset("select distinct(first_name) from contacts")){
190+ System.out.println(rs.getValue(0));
191+ }
192+ }
193+ catch(Exception e){
194+ e.printStackTrace();
195+ }
196+ </pre>
197+ * Note that records returned by this method are read-only. See the other
198+ * getRecordset() methods for options to create or update records.
199+ */
200+ public Generator <Recordset > getRecordset (String sql ) throws SQLException {
201+ return getRecordset (sql , true );
202+ }
203+
204+
205+ //**************************************************************************
206+ //** getRecordset
207+ //**************************************************************************
208+ /** Used to execute a SQL statement and returns a Recordset as an iterator.
209+ * Provides an option to return records that are read-only or editable. To
210+ * perform a query with read-only records, do something like this:
211+ <pre>
212+ try (javaxt.sql.Connection conn = db.getConnection()){
213+ for (Recordset rs : conn.getRecordset("select * from contacts", true)){
191214 System.out.println(rs.getValue(0));
192215 }
193- conn.close();
194- </pre>
216+ }
217+ catch(Exception e){
218+ e.printStackTrace();
219+ }
220+ </pre>
221+ * To insert records, do something like this:
222+ <pre>
223+ try (javaxt.sql.Connection conn = db.getConnection()){
224+ for (Recordset rs : conn.getRecordset("select * from contacts where id=-1", false)){
225+ rs.addNew();
226+ rs.setValue("first_name", "John");
227+ rs.setValue("last_name", "Smith");
228+ rs.update();
229+ }
230+ }
231+ catch(Exception e){
232+ e.printStackTrace();
233+ }
234+ </pre>
235+ * To update existing records, do something like this:
236+ <pre>
237+ try (javaxt.sql.Connection conn = db.getConnection()){
238+ for (Recordset rs : conn.getRecordset("select * from contacts where last_name='Smith'", false)){
239+ String firstName = rs.getValue("first_name").toString();
240+ if (firstName.equals("John")){
241+ rs.setValue("name", "Jonathan");
242+ rs.update();
243+ }
244+ }
245+ }
246+ catch(Exception e){
247+ e.printStackTrace();
248+ }
249+ </pre>
250+ */
251+ public Generator <Recordset > getRecordset (String sql , boolean readOnly ) throws SQLException {
252+ HashMap <String , Object > props = new HashMap <>();
253+ props .put ("readOnly" , readOnly );
254+ if (readOnly ) props .put ("fetchSize" , 1000 );
255+ return getRecordset (sql , props );
256+ }
257+
258+
259+ //**************************************************************************
260+ //** getRecordset
261+ //**************************************************************************
262+ /** Used to execute a SQL statement and returns a Recordset as an iterator.
263+ * Example:
264+ <pre>
265+ try (javaxt.sql.Connection conn = db.getConnection()){
266+ for (Recordset rs : conn.getRecordset("select * from contacts",
267+ new HashMap<String, Object>() {{
268+ put("readOnly", true);
269+ put("fetchSize", 1000);
270+ }}))
271+ {
272+
273+ System.out.println(rs.getValue("first_name") + " " + rs.getValue("last_name"));
274+ }
275+ }
276+ catch(Exception e){
277+ e.printStackTrace();
278+ }
279+ </pre>
280+ * @param sql Query statement. This parameter is required.
281+ * @param props Recordset options (e.g. readOnly, fetchSize, batchSize).
282+ * See the Recordset class for more information about this properties. This
283+ * parameter is optional.
195284 */
196- public Generator <Recordset > getRecordset (final String sql , final boolean readOnly ) throws SQLException {
285+ public Generator <Recordset > getRecordset (final String sql , Map <String , Object > props ) throws SQLException {
286+
287+ if (props ==null ){
288+ props = new HashMap <>();
289+ props .put ("readOnly" , true );
290+ props .put ("fetchSize" , 1000 );
291+ }
292+
293+ Boolean readOnly = new Value (props .get ("readOnly" )).toBoolean ();
294+ if (readOnly ==null ) readOnly = true ;
295+ Integer fetchSize = new Value (props .get ("fetchSize" )).toInteger ();
296+ if (fetchSize ==null ) fetchSize = 1000 ;
297+ Integer batchSize = new Value (props .get ("batchSize" )).toInteger ();
298+ if (batchSize ==null ) batchSize = 0 ;
299+
300+
301+ final boolean _readOnly = readOnly ;
302+ final int _fetchSize = fetchSize ;
303+ final int _batchSize = batchSize ;
304+
197305
198306 final Connection conn = this ;
199307 try (Generator g = new Generator <Recordset >(){
@@ -202,15 +310,16 @@ public Generator<Recordset> getRecordset(final String sql, final boolean readOnl
202310 @ Override
203311 public void run () throws InterruptedException {
204312 rs = new Recordset ();
205- if (readOnly ) rs .setFetchSize (1000 );
313+ if (_readOnly ) rs .setFetchSize (_fetchSize );
206314 try {
207- rs .open (sql , conn , readOnly );
315+ rs .open (sql , conn , _readOnly );
316+ if (!_readOnly ) rs .setBatchSize (_batchSize );
208317 while (rs .next ()){
209318 this .yield (rs );
210319 }
211320 }
212321 catch (Exception e ){
213- InterruptedException ex = new InterruptedException (e .getMessage ());
322+ RuntimeException ex = new RuntimeException (e .getMessage ());
214323 ex .setStackTrace (e .getStackTrace ());
215324 throw ex ;
216325 }
@@ -227,18 +336,6 @@ public void close() {
227336 }
228337
229338
230- //**************************************************************************
231- //** getRecordset
232- //**************************************************************************
233- /** Used to execute a SQL statement and returns a Recordset as an iterator.
234- * The Recordset is read-only. Use the other getRecordset() method for
235- * creating and updating records.
236- */
237- public Generator <Recordset > getRecordset (String sql ) throws SQLException {
238- return getRecordset (sql , true );
239- }
240-
241-
242339 //**************************************************************************
243340 //** execute
244341 //**************************************************************************
0 commit comments