Skip to content

Commit 16754dc

Browse files
committed
Merge branch 'master' of https://github.com/javalite/activejdbc
2 parents ee22d7f + fdb7255 commit 16754dc

File tree

11 files changed

+141
-232
lines changed

11 files changed

+141
-232
lines changed

activejdbc/src/main/java/org/javalite/activejdbc/Base.java

Lines changed: 7 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -226,34 +226,8 @@ public void onNext(Map row) {
226226
* @param params list of parameters if query is parametrized.
227227
* @return instance of <code>RowProcessor</code> which has with() method for convenience.
228228
*/
229-
public static RowProcessor find(String query, Object... params) {
230-
return new DB(DB.DEFAULT_NAME).find(query, params);
231-
}
232-
233-
/**
234-
* Executes a raw query and calls the listener to handle the results. The listener should extend
235-
* {@link RowListener} to process individual rows, or implement {@link ResultSetListener} to process the whole
236-
* ResultSet. For very large result sets, call this method with <tt>streaming</tt> as <tt>true</tt>, as it will
237-
* create a streaming PreparedStatement (currently only available for MySQL). Example:
238-
*
239-
* <blockquote><pre>
240-
* Base.findWith(new RowListenerAdapter() {
241-
* @Override public void onNext(Map row) {
242-
* // write your code here
243-
* Object o1 = row.get("first_name");
244-
* Object o2 = row.get("last_name");
245-
* }
246-
* }, true, "select first_name, last_name from really_large_table");
247-
* </pre></blockquote>
248-
*
249-
* @param listener a subclass of {@link RowListener} to process individual rows, or an implementation of
250-
* {@link ResultSetListener} to process the whole ResultSet
251-
* @param streaming true to create a streaming PreparedStatement, false otherwise
252-
* @param query raw SQL query
253-
* @param params parameters of parametrized query
254-
*/
255-
public static void findWith(ResultSetListener listener, boolean streaming, String query, Object... params) {
256-
new DB(DB.DEFAULT_NAME).findWith(listener, streaming, query, params);
229+
public static RowProcessor find(String query, Object ... params) {
230+
return new DB(DB.DEFAULT_NAME).find(query, params);
257231
}
258232

259233
/**
@@ -264,31 +238,17 @@ public static void findWith(ResultSetListener listener, boolean streaming, Strin
264238
* @param listener client listener implementation for processing individual rows.
265239
*/
266240
public static void find(String sql, RowListener listener) {
267-
new DB(DB.DEFAULT_NAME).find(sql, listener);
241+
new DB(DB.DEFAULT_NAME).find(sql, listener);
268242
}
269243

270-
/**
271-
* Executes a raw query and calls the listener to handle the results. The listener should extend
272-
* {@link RowListener} to process individual rows, or implement {@link ResultSetListener} to process the whole
273-
* ResultSet. For very large result sets, call this method with <tt>streaming</tt> as <tt>true</tt>, as it will
274-
* create a streaming Statement (currently only available for MySQL).
275-
*
276-
* @param listener a subclass of {@link RowListener} to process individual rows, or an implementation of
277-
* {@link ResultSetListener} to handle the whole ResultSet
278-
* @param streaming true to create a streaming Statement, false otherwise
279-
* @param query raw SQL query
280-
*/
281-
public static void findWith(ResultSetListener listener, boolean streaming, String query) {
282-
new DB(DB.DEFAULT_NAME).findWith(listener, streaming, query);
283-
}
284244

285245
/**
286246
* Executes DML. Use it for inserts and updates.
287247
*
288248
* @param query raw DML.
289249
* @return number of rows afected by query.
290250
*/
291-
public static int exec(String query) {
251+
public static int exec(String query){
292252
return new DB(DB.DEFAULT_NAME).exec(query);
293253
}
294254

@@ -300,13 +260,14 @@ public static int exec(String query) {
300260
* @param params query parameters.
301261
* @return number of records affected.
302262
*/
303-
public static int exec(String query, Object... params) {
263+
public static int exec(String query, Object ... params){
304264
return new DB(DB.DEFAULT_NAME).exec(query, params);
305265
}
266+
306267

307268
/**
308269
* This method is specific for inserts.
309-
*
270+
*
310271
* @param query SQL for inserts.
311272
* @param autoIncrementColumnName name of a column that is auto-incremented.
312273
* @param params list of parameter values.

activejdbc/src/main/java/org/javalite/activejdbc/DB.java

Lines changed: 30 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -350,23 +350,25 @@ public List<Map> all(String query, Object ... params) {
350350
* This method returns entire resultset as one list. Do not use it for large result sets.
351351
* Example:
352352
* <pre>
353-
* List&lt;Map&gt; people = db.findAll(&quot;select * from people where first_name = ?&quot;, &quot;John&quot;);
354-
* for (Map person : people)
355-
* System.out.println(person.get("first_name"));
353+
* <code>List&lt;Map&lt;String, Object&gt;&gt; people = Base.findAll(&quot;select * from people where first_name = ?&quot;, &quot;John&quot;);
354+
* for(Map person: people)
355+
* System.out.println(person.get(&quot;first_name&quot;));
356+
* </code>
356357
* </pre>
357358
*
358359
* @param query raw SQL query. This query is parametrized.
359360
* @param params list of parameters for a parametrized query.
360361
* @return entire result set corresponding to the query.
361362
*/
362-
public List<Map> findAll(String query, Object... params) {
363+
public List<Map> findAll(String query, Object ... params) {
364+
363365
final List<Map> results = new ArrayList<Map>();
364366
long start = System.currentTimeMillis();
365-
findWith(new RowListenerAdapter() {
367+
find(query, params).with(new RowListenerAdapter() {
366368
@Override public void onNext(Map<String, Object> row) {
367369
results.add(row);
368370
}
369-
}, false, query, params);
371+
});
370372
LogFilter.logQuery(logger, query, params, start);
371373
return results;
372374
}
@@ -423,13 +425,14 @@ public List<Map> all(String query) {
423425
* @return entire result set corresponding to the query.
424426
*/
425427
public List<Map> findAll(String query) {
426-
final List<Map> results = new ArrayList<Map>();
428+
429+
final ArrayList<Map> results = new ArrayList<Map>();
427430
long start = System.currentTimeMillis();
428-
findWith(new RowListenerAdapter() {
431+
find(query).with(new RowListenerAdapter() {
429432
@Override public void onNext(Map<String, Object> row) {
430433
results.add(row);
431434
}
432-
}, false, query);
435+
});
433436

434437
LogFilter.logQuery(logger, query, null, start);
435438
return results;
@@ -451,60 +454,27 @@ public void onNext(Map row) {
451454
* @param params list of parameters if query is parametrized.
452455
* @return instance of <code>RowProcessor</code> which has with() method for convenience.
453456
*/
454-
public RowProcessor find(String query, Object... params) {
457+
public RowProcessor find(String query, Object ... params) {
458+
459+
//TODO: count ? signs and number of params, throw exception if do not match
460+
455461
if(query.indexOf('?') == -1 && params.length != 0) throw new IllegalArgumentException("you passed arguments, but the query does not have placeholders: (?)");
456462

457463
if(!SELECT_PATTERN.matcher(query).find()) { throw new IllegalArgumentException("query must be 'select' query"); }
458464

459-
try {
460-
PreparedStatement ps = prepareStreamingStatement(query);
461-
setParameters(ps, params);
462-
ResultSet rs = ps.executeQuery();
463-
return new RowProcessor(rs, ps);
464-
// ps and rs will be closed by RowProcessor
465-
} catch (SQLException e) { throw new DBException(query, params, e); }
466-
}
467-
468-
/**
469-
* Executes a raw query and calls the listener to handle the results. The listener should extend
470-
* {@link RowListener} to process individual rows, or implement {@link ResultSetListener} to process the whole
471-
* ResultSet. For very large result sets, call this method with <tt>streaming</tt> as <tt>true</tt>, as it will
472-
* create a streaming PreparedStatement (currently only available for MySQL). Example:
473-
*
474-
* <blockquote><pre>
475-
* db.findWith(new RowListenerAdapter() {
476-
* @Override public void onNext(Map row) {
477-
* // write your code here
478-
* Object o1 = row.get("first_name");
479-
* Object o2 = row.get("last_name");
480-
* }
481-
* }, true, "select first_name, last_name from really_large_table");
482-
* </pre></blockquote>
483-
*
484-
* @param listener a subclass of {@link RowListener} to process individual rows, or an implementation of
485-
* {@link ResultSetListener} to process the whole ResultSet
486-
* @param streaming true to create a streaming PreparedStatement, false otherwise
487-
* @param query raw SQL query
488-
* @param params parameters of parametrized query
489-
*/
490-
public void findWith(ResultSetListener listener, boolean streaming, String query, Object... params) {
491465
//TODO: cache prepared statements here too
492-
PreparedStatement ps = null;
493-
ResultSet rs = null;
466+
PreparedStatement ps;
467+
ResultSet rs;
494468
try {
495-
ps = streaming ? prepareStreamingStatement(query) : connection().prepareStatement(query);
469+
ps = createStreamingPreparedStatement(query);
496470
setParameters(ps, params);
497471
rs = ps.executeQuery();
498-
listener.onResultSet(rs);
499-
} catch (SQLException e) {
500-
throw new DBException(query, params, e);
501-
} finally {
502-
closeQuietly(rs);
503-
closeQuietly(ps);
504-
}
472+
return new RowProcessor(rs, ps);
473+
474+
} catch (SQLException e) { throw new DBException(query, params, e); }
505475
}
506476

507-
private PreparedStatement prepareStreamingStatement(String query) throws SQLException {
477+
private PreparedStatement createStreamingPreparedStatement(String query) throws SQLException {
508478
Connection conn = connection();
509479
PreparedStatement res;
510480
if ("mysql".equalsIgnoreCase(conn.getMetaData().getDatabaseProductName())) {
@@ -524,29 +494,16 @@ private PreparedStatement prepareStreamingStatement(String query) throws SQLExce
524494
* @param listener client listener implementation for processing individual rows.
525495
*/
526496
public void find(String sql, RowListener listener) {
527-
findWith(listener, true, sql);
528-
}
529497

530-
/**
531-
* Executes a raw query and calls the listener to handle the results. The listener should extend
532-
* {@link RowListener} to process individual rows, or implement {@link ResultSetListener} to process the whole
533-
* ResultSet. For very large result sets, call this method with <tt>streaming</tt> as <tt>true</tt>, as it will
534-
* create a streaming Statement (currently only available for MySQL).
535-
*
536-
* @param listener a subclass of {@link RowListener} to process individual rows, or an implementation of
537-
* {@link ResultSetListener} to handle the whole ResultSet
538-
* @param streaming true to create a streaming Statement, false otherwise
539-
* @param query raw SQL query
540-
*/
541-
public void findWith(ResultSetListener listener, boolean streaming, String query) {
542498
Statement s = null;
543499
ResultSet rs = null;
544500
try {
545-
s = streaming ? createStreamingStatement() : connection().createStatement();
546-
rs = s.executeQuery(query);
547-
listener.onResultSet(rs);
501+
s = createStreamingStatement();
502+
rs = s.executeQuery(sql);
503+
RowProcessor p = new RowProcessor(rs, s);
504+
p.with(listener);
548505
} catch (SQLException e) {
549-
throw new DBException(query, null, e);
506+
throw new DBException(sql, null, e);
550507
} finally {
551508
closeQuietly(rs);
552509
closeQuietly(s);
@@ -687,7 +644,8 @@ Object execInsert(String query, String autoIncrementColumnName, Object... params
687644
} catch (SQLException e) {
688645
throw new DBException(query, params, e);
689646
} finally {
690-
// don't close ps as it is cached!
647+
// don't close ps as it could have come from the cache!
648+
//TODO: close ps if not cached?
691649
}
692650
}
693651

activejdbc/src/main/java/org/javalite/activejdbc/LazyList.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,11 +326,11 @@ protected void hydrate() {
326326
}
327327
delegate = new ArrayList<T>();
328328
long start = System.currentTimeMillis();
329-
new DB(metaModel.getDbName()).findWith(new RowListenerAdapter() {
329+
new DB(metaModel.getDbName()).find(sql, params).with(new RowListenerAdapter() {
330330
@Override public void onNext(Map<String, Object> map) {
331331
delegate.add(ModelDelegate.<T>instance(map, metaModel));
332332
}
333-
}, false, sql, params);
333+
});
334334
LogFilter.logQuery(logger, sql, params, start);
335335
if(metaModel.cached()){
336336
delegate = Collections.unmodifiableList(delegate);

activejdbc/src/main/java/org/javalite/activejdbc/ModelDelegate.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -211,11 +211,12 @@ public static <T extends Model, M extends T> void findWith(final Class<M> clazz,
211211
long start = System.currentTimeMillis();
212212
final MetaModel metaModel = metaModelOf(clazz);
213213
String sql = metaModel.getDialect().selectStar(metaModel.getTableName(), query);
214-
new DB(metaModel.getDbName()).findWith(new RowListenerAdapter() {
215-
@Override public void onNext(Map<String, Object> row) {
214+
new DB(metaModel.getDbName()).find(sql, params).with(new RowListenerAdapter() {
215+
@Override
216+
public void onNext(Map<String, Object> row) {
216217
listener.onModel(instance(row, metaModel, clazz));
217218
}
218-
}, true, sql, params);
219+
});
219220
LogFilter.logQuery(logger, sql, null, start);
220221
}
221222

activejdbc/src/main/java/org/javalite/activejdbc/ResultSetListener.java

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,26 @@
11
/*
2-
Copyright 2009-2015 Igor Polevoy
2+
Copyright 2009-2014 Igor Polevoy
33
4-
Licensed under the Apache License, Version 2.0 (the "License");
5-
you may not use this file except in compliance with the License.
6-
You may obtain a copy of the License at
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
77
8-
http://www.apache.org/licenses/LICENSE-2.0
8+
http://www.apache.org/licenses/LICENSE-2.0
99
10-
Unless required by applicable law or agreed to in writing, software
11-
distributed under the License is distributed on an "AS IS" BASIS,
12-
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
See the License for the specific language governing permissions and
14-
limitations under the License.
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
1515
*/
16+
17+
1618
package org.javalite.activejdbc;
1719

18-
import java.sql.ResultSet;
19-
import java.sql.ResultSetMetaData;
20-
import java.sql.SQLException;
2120
import java.util.Map;
2221

23-
public abstract class RowListener implements ResultSetListener {
24-
@Override
25-
public final void onResultSet(ResultSet rs) throws SQLException {
26-
ResultSetMetaData metaData = rs.getMetaData();
27-
while (rs.next()) {
28-
Map<String, Object> row = new CaseInsensitiveMap<Object>();
29-
int i = 1;
30-
while (i <= metaData.getColumnCount()) {
31-
row.put(metaData.getColumnLabel(i), rs.getObject(i));
32-
i++;
33-
}
34-
if (!next(row)) { break; }
35-
}
36-
}
22+
23+
public interface RowListener {
3724

3825
/**
3926
* Implementations of this interface can return "false" from the next() method in order to stop fetching more results from DB.
@@ -43,5 +30,5 @@ public final void onResultSet(ResultSet rs) throws SQLException {
4330
* @param row Map instance containing values for a row. Keys are names of columns and values are .. values.
4431
* @return false if this listener needs to stop processing (no more calls to this method)
4532
*/
46-
public abstract boolean next(Map<String, Object> row);
33+
boolean next(Map<String, Object> row);
4734
}

0 commit comments

Comments
 (0)