baliuka 2003/03/16 01:29:32
Modified: dbutils/src/java/org/apache/commons/dbutils DbException.java
ProcedureUtils.java ScalarHandler.java
dbutils/src/test/org/apache/commons/dbutils Demo.java
ProcedureUtilsTest.java
Added: dbutils/src/java/org/apache/commons/dbutils
CollectionHandler.java VectorHandler.java
Log:
added more predefined handlers
Revision Changes Path
1.2 +1 -1
jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/DbException.java
Index: DbException.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/DbException.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DbException.java 9 Mar 2003 17:14:18 -0000 1.1
+++ DbException.java 16 Mar 2003 09:29:32 -0000 1.2
@@ -20,7 +20,7 @@
* Constructs an instance of <code>DbException</code> with the specified detail
message.
* @param msg the detail message.
*/
- public DbException(Throwable cause,String msg) {
+ public DbException(String msg, Throwable cause) {
super(msg);
this.cause = cause;
}
1.10 +112 -21
jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/ProcedureUtils.java
Index: ProcedureUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/ProcedureUtils.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- ProcedureUtils.java 14 Mar 2003 19:53:43 -0000 1.9
+++ ProcedureUtils.java 16 Mar 2003 09:29:32 -0000 1.10
@@ -79,12 +79,14 @@
private static final Map PROCEDURES = new HashMap();
private static final Map PREDEFINED_HANDLERS = new HashMap();
- private static final ResultSetHandler SCALAR_HANDLER = new ScalarHandler();
- private static final SoftRefMemoryCache CACHE = new SoftRefMemoryCache();
+ private static final ResultSetHandler SCALAR_HANDLER = new ScalarHandler();
+ private static final ResultSetHandler VECTOR_HANDLER = new VectorHandler();
+ private static final ResultSetHandler COLLECTION_HANDLER = new
CollectionHandler();
private static final String UPDATE_TAG = "update";
private static final String QUERY_TAG = "query";
private static final String CACHE_TAG = "useCache";
private static final String FLUSH_TAG = "flushCache";
+ private static final String HANDLER_TAG = "handler";
static class ProcedureDescriptor{
@@ -94,7 +96,6 @@
ResultSetHandler handler;
boolean update;
boolean dynamic;
- //TODO:
boolean cached;
boolean flushOnExecute;
@@ -117,7 +118,6 @@
for (int i = 0; i < sql.length; i++ ){
-
if(sql[i] == '$'){
if( state.get(ESCAPE) ){
sb.append('$');
@@ -202,6 +202,25 @@
return descriptor;
}
+
+ private static boolean isCollection(Class cls){
+
+ return cls.isAssignableFrom(List.class);
+ }
+
+
+ private static boolean isVector(Class cls){
+
+ return cls.isArray() && ! cls.getComponentType().isPrimitive();
+ }
+
+ private static boolean isScalar(Class cls){
+
+ return cls.isPrimitive() || Number.class.isAssignableFrom(cls) ||
+ Character.class == cls || String.class == cls ||
+ Boolean.class == cls || java.util.Date.class.isAssignableFrom(cls);
+
+ }
static Method findMethod(Class cls, JavaMethod jmethod){
@@ -240,7 +259,7 @@
String name = null;
- DocletTag tag = jmethod.getTagByName("handler");
+ DocletTag tag = jmethod.getTagByName(HANDLER_TAG);
if(tag != null ){
name = tag.getValue();
@@ -256,11 +275,24 @@
}
- if( name == null && method.getReturnType().isPrimitive() ){
+ if( name == null && isScalar( method.getReturnType() ) ){
return SCALAR_HANDLER;
}
+ if( name == null && isVector( method.getReturnType() ) ){
+
+ return VECTOR_HANDLER;
+ }
+
+ if( name == null && isCollection( method.getReturnType() ) ){
+
+ return COLLECTION_HANDLER;
+ }
+
+
+
+
if(name != null){
String imports[] = jmethod.getParentClass().
@@ -355,12 +387,62 @@
return desctiptors;
}
- //TODO: ConvertUtils
- static Object convert( Class to, Object from ){
+
+ static Object convert( Class to, Object from, Method forErrorReport ){
+
+ if( from == null ){
+
+ if(to.isPrimitive()){
+ throw new IllegalStateException("can not convert null to primitive as
return value in " +
+ forErrorReport
+ );
+ }else{
+ return null;
+ }
+
+ }
- return from;
+ if( to.isAssignableFrom(from.getClass()) ){
+
+ return from;
+
+ }else{
+
+ if(to.isPrimitive() &&
+ ( ( from instanceof Number ) ||
+ from.getClass() == Boolean.class ||
+ from.getClass() == Character.class ) ){
+
+ return from;
+ }
+
+ throw new IllegalStateException("can not convert " +
from.getClass().getName()
+ + " to " + to.getName() + " as return value in " +
+ forErrorReport
+ );
+
+ }
+
+
+ }
+
+ public static void flush(Object procedures){
+
+ Invocation inv = (Invocation)Proxy.getInvocationHandler(procedures);
+ inv.CACHE.clear();
+ }
+
+ public static Connection getConnection(Object procedures){
+
+ Invocation inv = (Invocation)Proxy.getInvocationHandler(procedures);
+ return inv.connection;
}
+ public static void connect(Object procedures, Connection connection){
+
+ Invocation inv = (Invocation)Proxy.getInvocationHandler(procedures);
+ inv.connection = connection;
+ }
public static Object getInstance(Class cls, Connection connection){
@@ -429,8 +511,10 @@
static class Invocation implements InvocationHandler{
- Connection connection;
- Map descriptors;
+ private Connection connection;
+ private Map descriptors;
+ private Object delegate = new Object();
+ private final SoftRefMemoryCache CACHE = new SoftRefMemoryCache();
Invocation(Connection connection,Map descriptors){
this.connection = connection;
@@ -452,17 +536,24 @@
if(method.getDeclaringClass() == Object.class ){
- return method.invoke(connection,args);
+ return method.invoke(delegate,args);
}
ProcedureDescriptor descriptor =
(ProcedureDescriptor)descriptors.get(method);
if(descriptor == null){
- throw new IllegalStateException("no descriptor found for " +
method);
+ throw new IllegalStateException("descriptor not found: " +
method);
}
+ if(connection == null){
+
+ throw new IllegalStateException("object disconnected: " +
method);
+
+ }
+
+
if(!descriptor.update){
CacheKey key = null;
Object value = null;
@@ -478,8 +569,8 @@
prepareArgs(descriptor,args),
descriptor.handler,
null,args
- ));
- if(descriptor.cached || value != null){
+ ),method);
+ if(descriptor.cached && value != null){
CACHE.put(key,value);
}
return value;
@@ -488,7 +579,7 @@
descriptor.dynamic ?
MessageFormat.format(descriptor.jdbcSQL,args) : descriptor.jdbcSQL,
prepareArgs(descriptor,args)
);
- if(descriptor.flushOnExecute){
+ if(descriptor.flushOnExecute && updateCount > 0 ){
CACHE.clear();
}
if(method.getReturnType() == Void.TYPE ){
@@ -511,7 +602,7 @@
}
}
- throw new DbException(e, method.toString() );
+ throw new DbException( method.toString() + ":" + e.getMessage() , e
);
}
}
@@ -528,9 +619,6 @@
[EMAIL PROTECTED] $Id$
*/
final class SoftRefMemoryCache {
-
-
-
private int m_current = 0;
private int m_maxStrongRefCount;
private Map m_map = new Hashtable();
@@ -591,10 +679,13 @@
}
void clear(){
+ removeSoftRef();
+ if(m_map.size() > 0){
m_map.clear();
if(m_strongRefs != null){
Arrays.fill(m_strongRefs, null);
}
+ }
}
private SoftRef makeValue(Object key, Object value, ReferenceQueue queue) {
1.4 +1 -1
jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/ScalarHandler.java
Index: ScalarHandler.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/ScalarHandler.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ScalarHandler.java 10 Mar 2003 19:35:42 -0000 1.3
+++ ScalarHandler.java 16 Mar 2003 09:29:32 -0000 1.4
@@ -3,7 +3,7 @@
import java.sql.*;
/**
*
- * @author user
+ * @author baliuka
*/
public class ScalarHandler implements ResultSetHandler {
1.1
jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/CollectionHandler.java
Index: CollectionHandler.java
===================================================================
package org.apache.commons.dbutils;
import java.util.*;
/**
*
* @author baliuka
*/
public class CollectionHandler implements ResultSetHandler{
/** Creates a new instance of CollectionHandler */
public CollectionHandler() {
}
public Object handle(java.sql.ResultSet rs, Object[] params, Object userObject)
throws java.sql.SQLException {
Collection result = new Vector();
while(rs.next()){
result.add( DbUtils.resultSetToArray(rs) );
}
return result;
}
}
1.1
jakarta-commons-sandbox/dbutils/src/java/org/apache/commons/dbutils/VectorHandler.java
Index: VectorHandler.java
===================================================================
package org.apache.commons.dbutils;
import java.sql.*;
/**
*
* @author baliuka
*/
public class VectorHandler implements ResultSetHandler {
/** Creates a new instance of VectorHandler */
public VectorHandler() {
}
public Object handle(java.sql.ResultSet rs, Object[] params, Object userObject)
throws java.sql.SQLException{
if (rs.next()) {
Object result = DbUtils.resultSetToArray(rs);
return result;
}
throw new SQLException();
}
}
1.8 +30 -0
jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/Demo.java
Index: Demo.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/Demo.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- Demo.java 14 Mar 2003 17:27:35 -0000 1.7
+++ Demo.java 16 Mar 2003 09:29:32 -0000 1.8
@@ -37,6 +37,36 @@
/**
+ [EMAIL PROTECTED] SELECT max(ID) FROM {0}
+ [EMAIL PROTECTED]
+ */
+ public Integer maxId(String table);
+
+ /**
+ [EMAIL PROTECTED] SELECT now()
+ */
+ public java.util.Date now();
+
+ /**
+ [EMAIL PROTECTED] SELECT * FROM TBL
+ */
+ public Object[] getRow( int id );
+
+ /**
+ [EMAIL PROTECTED] SELECT * FROM {0}
+ */
+ public java.util.Collection getAll( String tbl );
+
+
+
+
+ /**
+ [EMAIL PROTECTED] SELECT $0 || $1
+ */
+ public String concat(String s1, String s2);
+
+
+ /**
[EMAIL PROTECTED] SELECT * FROM TBL
[EMAIL PROTECTED] DemoHandler
*/
1.7 +7 -0
jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/ProcedureUtilsTest.java
Index: ProcedureUtilsTest.java
===================================================================
RCS file:
/home/cvs/jakarta-commons-sandbox/dbutils/src/test/org/apache/commons/dbutils/ProcedureUtilsTest.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- ProcedureUtilsTest.java 11 Mar 2003 18:57:47 -0000 1.6
+++ ProcedureUtilsTest.java 16 Mar 2003 09:29:32 -0000 1.7
@@ -117,6 +117,13 @@
demo.print(System.out);
System.out.println("DYNAMIC SQL:");
demo.dynamicPrint(System.out,"TBL","ID",8);
+ Integer max = demo.maxId("TBL");
+ assertTrue( max != null );
+ assertTrue("cached query", max == demo.maxId("TBL") );
+ assertTrue( demo.now() != null );
+ assertEquals(demo.concat("A","B"), "AB" );
+ assertTrue(demo.getRow(max.intValue()).length == 2 );
+ assertTrue(demo.getAll("TBL").size() > 2 );
demo.clear();
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]