Issue Details (XML | Word | Printable)

Key: DBUTILS-25
Type: Improvement Improvement
Status: Closed Closed
Resolution: Fixed
Priority: Minor Minor
Assignee: Unassigned
Reporter: Piotr Lakomy
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Commons DbUtils

[dbutils] Proposal for a set of new ResultSetHandlers

Created: 28/Sep/04 11:07 AM   Updated: 02/Jan/08 07:29 AM
Return to search
Component/s: None
Affects Version/s: 1.0
Fix Version/s: 1.1

Time Tracking:
Not Specified

File Attachments:
  Size
Zip Archive new-map-handlers.zip 2004-09-28 11:08 AM Piotr Lakomy 11 kB
Environment:
Operating System: All
Platform: All

Bugzilla Id: 31446


 Description  « Hide
Add set of ResultSetHandlers that return a Map instead of List.

For each row, key is determined (default is first column, user can specify
column index/name in constructor). Next the row is converted to scalar,
Array, Map or bean and stored in the Map under key.

object Array Map Bean
----------------- ---------------- -------------- ---------------
ColumnMapHandler ArrayMapHandler MapMapHandler BeanListHandler

Examples:

String; sql = "select user_id, user_name, last_login from passwd";
Map users = (Map)run.query(
"select user_id, user_name from passwd",
new ColumnMapHandler());
System.out.println(users.get("usr007"));

Map users = (Map)run.query(sql, new ArrayMapArray());
System.out.println(users.get("usr007")[2]);

Map users = (Map)run.query(sql, new MapMapArray());
System.out.println(users.get("usr007").get("last_login"));

Map users = (Map)run.query(sql, new MapBeanArray(TestBean.class));
System.out.println(users.get("usr007").getLastLogin());

Implementation note:
Map implementation is LinkedHashMap. This class does preserve order of
insertions, thus, the sorting can be done in sql. However this
class is @since 1.4.

Attached is source code (based on HEAD) and test cases.



 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Piotr Lakomy added a comment - 28/Sep/04 11:08 AM
Created an attachment (id=12874)
new source, patch to BaseTestCase.java

Piotr Lakomy added a comment - 28/Sep/04 11:10 AM
Sorry, the attachement is a zip file.

David Graham added a comment - 28/Sep/04 08:26 PM
I think the goal of these handlers is to be able to use a key to lookup the full
object in the returned Map. So if you used the primary key column as the map
key you would be able to lookup the object in the map based on the primary key.

Rather than have a handler class for each type of object being stored in the map
it might be better to have one class with a protected method that generates the
object. The default behavior could be to generate a Map for the row but users
could override that to generate a bean, arrray, etc. Maybe the class could be
named KeyedHandler.

DbUtils' minimum Java version is 1.3 so LinkedHashMap can't be used in the
default implementation. Again, we could provide a protected factory method that
users on 1.4 could override to return a LinkedHashMap.


David Graham added a comment - 19/Dec/04 11:51 AM
      • COM-1753 has been marked as a duplicate of this bug. ***

David Graham added a comment - 19/Dec/04 12:03 PM
I added a KeyedHandler based on your MapMapHandler but customizable for possible
implementations other than Maps. Thanks for the test case!