First commit of beehive-jdbc-mapper, which has no dependencies, but which beehive-jdbc-control now depends on

This commit is contained in:
moparisthebest 2014-04-22 14:40:18 -04:00
parent 39338e999f
commit d0e1e03756
23 changed files with 5699 additions and 0 deletions

View File

@ -33,6 +33,11 @@
<artifactId>beehive-controls</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.moparisthebest.beehive</groupId>
<artifactId>beehive-jdbc-mapper</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.moparisthebest.aptIn16</groupId>
<artifactId>netui-compiler</artifactId>

View File

@ -0,0 +1,56 @@
package org.apache.beehive.controls.system.jdbc;
import org.apache.beehive.controls.api.context.ControlBeanContext;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.sql.ResultSet;
import java.util.*;
/**
* Refer to org.apache.beehive.controls.system.jdbc.ResultSetMapper for how this class operates
*/
public class NewDefaultObjectResultSetMapper extends com.moparisthebest.jdbc.ResultSetMapper implements org.apache.beehive.controls.system.jdbc.ResultSetMapper {
/**
* Map the ResultSet to the method's return type. The object type returned is defined by the return type of the method.
*
* @param context A ControlBeanContext instance, see Beehive controls javadoc for additional information
* @param m Method assoicated with this call.
* @param rs Result set to map.
* @param cal A Calendar instance for time/date value resolution.
* @return The Object resulting from the ResultSet
*/
@SuppressWarnings({"unchecked"})
public Object mapToResultType(ControlBeanContext context, Method m, ResultSet rs, Calendar cal) {
final Class returnType = m.getReturnType();
if (returnType.isArray()) {
return toArray(rs, returnType.getComponentType(), context.getMethodPropertySet(m, JdbcControl.SQL.class).arrayMaxLength(), cal);
} else if (Collection.class.isAssignableFrom(returnType)) {
return toCollection(rs, returnType, (Class) getActualTypeArguments(m)[0], context.getMethodPropertySet(m, JdbcControl.SQL.class).arrayMaxLength(), cal);
} else if (Map.class.isAssignableFrom(returnType)) {
Type[] types = getActualTypeArguments(m);
if (types[1] instanceof ParameterizedType) { // for collectionMaps
ParameterizedType pt = (ParameterizedType) types[1];
Class collectionType = (Class) pt.getRawType();
if (Collection.class.isAssignableFrom(collectionType))
return toMapCollection(rs, returnType, (Class) types[0], collectionType, (Class) pt.getActualTypeArguments()[0], context.getMethodPropertySet(m, JdbcControl.SQL.class).arrayMaxLength(), cal);
}
return toMap(rs, returnType, (Class) types[0], (Class) types[1], context.getMethodPropertySet(m, JdbcControl.SQL.class).arrayMaxLength(), cal);
} else if (Iterator.class.isAssignableFrom(returnType)) {
return ListIterator.class.isAssignableFrom(returnType) ?
toListIterator(rs, (Class) getActualTypeArguments(m)[0], context.getMethodPropertySet(m, JdbcControl.SQL.class).arrayMaxLength(), cal) :
toIterator(rs, (Class) getActualTypeArguments(m)[0], context.getMethodPropertySet(m, JdbcControl.SQL.class).arrayMaxLength(), cal);
} else {
return toObject(rs, returnType, cal);
}
}
private static Type[] getActualTypeArguments(Method m) {
return ((ParameterizedType) m.getGenericReturnType()).getActualTypeArguments();
}
public boolean canCloseResultSet() {
return true;
}
}

View File

@ -0,0 +1,117 @@
#!/bin/bash
cd "$(dirname "$0")"
function prepareFile(){
path="$1"
tmp_path="$(basename "$path")"
(
sed -n '/CODE AUTOMATICALLY GENERATED BY genQueryMapper.sh/q;p' "$path"
echo -e '\t// DO NOT EDIT BELOW THIS LINE, OR CHANGE THIS COMMENT, CODE AUTOMATICALLY GENERATED BY genQueryMapper.sh\n'
) > "$tmp_path"
echo "$tmp_path"
}
function finishFile(){
path="$1"
tmp_path="$(basename "$path")"
echo -e "}\n" >> "$tmp_path"
mv "$tmp_path" "$path"
}
result="$(prepareFile "src/main/java/com/moparisthebest/jdbc/ResultSetMapper.java")"
# single object types
cat src/main/java/com/moparisthebest/jdbc/ResultSetMapper.java | grep public | egrep '(toObject|toSingleMap)\(' | grep ', Calendar cal)' | while read method
do
#echo "method: $method"
method_name=$(echo $method | egrep -o '[^ ]+\(')
echo "ResultSetMapper.$method_name)"
cat >> "$result" <<EOF
$(echo $method | sed "s/, Calendar cal)/)/")
return this.$method_name$(echo $method | sed -e 's/^.*(//' -e 's/final //g' -e 's/, [^ ]* /, /g' -e 's/ResultSet rs/rs/' -e 's/) {/);/')
}
EOF
done
#everything else
cat src/main/java/com/moparisthebest/jdbc/ResultSetMapper.java | grep public | grep '(ResultSet rs' | grep ', int arrayMaxLength, Calendar cal)' | while read method
do
#echo "method: $method"
method_name=$(echo $method | egrep -o '[^ ]+\(')
echo "ResultSetMapper.$method_name)"
for args in '' ', int arrayMaxLength' ', Calendar cal'
do
cat >> "$result" <<EOF
$(echo $method | sed "s/, int arrayMaxLength, Calendar cal)/$args)/")
return this.$method_name$(echo $method | sed -e 's/^.*(//' -e 's/final //g' -e 's/, [^ ]* /, /g' -e 's/ResultSet rs/rs/' -e 's/) {/);/')
}
EOF
done
done
finishFile "src/main/java/com/moparisthebest/jdbc/ResultSetMapper.java"
query="$(prepareFile "src/main/java/com/moparisthebest/jdbc/QueryMapper.java")"
caching_query="$(prepareFile "src/main/java/com/moparisthebest/jdbc/CachingQueryMapper.java")"
null_query="$(prepareFile "src/main/java/com/moparisthebest/jdbc/NullQueryMapper.java")"
cat src/main/java/com/moparisthebest/jdbc/ResultSetMapper.java | grep public | grep '(ResultSet rs' | egrep -v '(int arrayMaxLength|Calendar cal)' | while read method
do
#echo "method: $method"
method_name=$(echo $method | egrep -o '[^ ]+\(')
echo "QueryMapper.$method_name)"
# QueryMapper.java
cat >> "$query" <<EOF
$(echo $method | sed -e 's/ResultSet rs/PreparedStatement ps/' -e 's/) {/, final Object... bindObjects) throws SQLException {/')
return cm.$method_name$(echo $method | sed -e 's/^.*(//' -e 's/final //g' -e 's/, [^ ]* /, /g' -e 's/ResultSet rs/bindExecute(ps, bindObjects)/' -e 's/) {/);/')
}
$(echo $method | sed -e 's/ResultSet rs/String sql/' -e 's/) {/, final Object... bindObjects) throws SQLException {/')
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
return this.$method_name$(echo $method | sed -e 's/^.*(//' -e 's/final //g' -e 's/, [^ ]* /, /g' -e 's/ResultSet rs/ps/' -e 's/) {/, bindObjects);/')
} finally {
tryClose(ps);
}
}
EOF
# CachingQueryMapper.java
cat >> "$caching_query" <<EOF
@Override
$(echo $method | sed -e 's/ResultSet rs/String sql/' -e 's/) {/, final Object... bindObjects) throws SQLException {/')
return super.$method_name$(echo $method | sed -e 's/^.*(//' -e 's/final //g' -e 's/, [^ ]* /, /g' -e 's/ResultSet rs/getPreparedStatement(sql)/' -e 's/) {/, bindObjects);/')
}
EOF
# NullQueryMapper.java
for type in PreparedStatement String
do
cat <<EOF
@Override
$(echo $method | sed -e "s/ResultSet rs/$type query/" -e 's/) {/, final Object... bindObjects) {/')
try {
return delegate.$method_name$(echo $method | sed -e 's/^.*(//' -e 's/final //g' -e 's/, [^ ]* /, /g' -e 's/ResultSet rs/query/' -e 's/) {/, bindObjects);/')
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
EOF
done >> "$null_query"
done
finishFile "src/main/java/com/moparisthebest/jdbc/QueryMapper.java"
finishFile "src/main/java/com/moparisthebest/jdbc/CachingQueryMapper.java"
finishFile "src/main/java/com/moparisthebest/jdbc/NullQueryMapper.java"

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.moparisthebest.beehive</groupId>
<artifactId>beehive</artifactId>
<version>1.0.3-SNAPSHOT</version>
</parent>
<artifactId>beehive-jdbc-mapper</artifactId>
<name>${project.artifactId}</name>
<build>
<finalName>${project.artifactId}</finalName>
</build>
</project>

View File

@ -0,0 +1,296 @@
package com.moparisthebest.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
import static com.moparisthebest.jdbc.TryClose.tryClose;
/**
* This class caches the PreparedStatement's it creates for the strings you send in, then closes them when the close() method is called.
* Since PreparedStatement is not thread-safe, this class cannot be either. Be sure to call it from only a single thread
* or synchronize around it.
*/
public class CachingQueryMapper extends QueryMapper {
protected final Map<String, PreparedStatement> cache;
protected CachingQueryMapper(Connection conn, String jndiName, ResultSetMapper cm, final int maxEntries) {
super(conn, jndiName, cm);
if (maxEntries > 0) { // we want a limited cache
final float loadFactor = 0.75f; // default for HashMaps
// if we set the initialCapacity this way, nothing should ever need re-sized
final int initialCapacity = ((int) Math.ceil(maxEntries / loadFactor)) + 1;
cache = new LinkedHashMap<String, PreparedStatement>(initialCapacity, loadFactor, true) {
@Override
protected boolean removeEldestEntry(Map.Entry<String, PreparedStatement> eldest) {
final boolean remove = size() > maxEntries;
if(remove){
//System.out.printf("closing PreparedStatement '%s' with key '%s'\n", eldest.getValue(), eldest.getKey());
tryClose(eldest.getValue());
}
return remove;
}
};
} else
cache = new HashMap<String, PreparedStatement>();
}
public CachingQueryMapper(Connection conn, ResultSetMapper cm, final int maxEntries) {
this(conn, null, cm, maxEntries);
}
public CachingQueryMapper(Connection conn, final int maxEntries) {
this(conn, null, null, maxEntries);
}
public CachingQueryMapper(String jndiName, ResultSetMapper cm, final int maxEntries) {
this(null, jndiName, cm, maxEntries);
}
public CachingQueryMapper(String jndiName, final int maxEntries) {
this(null, jndiName, null, maxEntries);
}
protected CachingQueryMapper(Connection conn, String jndiName, ResultSetMapper cm) {
this(conn, jndiName, cm, 20); // default size of 20
}
public CachingQueryMapper(Connection conn, ResultSetMapper cm) {
this(conn, null, cm);
}
public CachingQueryMapper(Connection conn) {
this(conn, null, null);
}
public CachingQueryMapper(String jndiName, ResultSetMapper cm) {
this(null, jndiName, cm);
}
public CachingQueryMapper(String jndiName) {
this(null, jndiName, null);
}
protected PreparedStatement getPreparedStatement(String sql) throws SQLException {
return getPreparedStatement(sql, ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
}
protected PreparedStatement getPreparedStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
PreparedStatement ps = cache.get(sql);
if (ps == null) {
//System.out.println("cache miss");
ps = conn.prepareStatement(sql,resultSetType,resultSetConcurrency);
cache.put(sql, ps);
}
//else System.out.println("cache hit");
return ps;
}
public void clearCache(boolean close) {
//System.out.println("cache size: "+cache.size());
for (PreparedStatement ps : cache.values())
tryClose(ps);
if (close)
super.close();
else
cache.clear();
}
public void clearCache() {
this.clearCache(false);
}
@Override
public void close() {
this.clearCache(true);
}
@Override
public int executeUpdate(String sql, Object... bindObjects) throws SQLException {
return super.executeUpdate(getPreparedStatement(sql), bindObjects);
}
@Override
public boolean executeUpdateSuccess(String sql, Object... bindObjects) throws SQLException {
return super.executeUpdateSuccess(getPreparedStatement(sql), bindObjects);
}
// these grab ResultSets from the database
@Override
public ResultSet toResultSet(String sql, Object... bindObjects) throws SQLException {
return super.toResultSet(getPreparedStatement(sql), bindObjects);
}
@Override
public ResultSet toResultSet(String sql, Integer rsType, Integer rsConcurrency, Object... bindObjects) throws SQLException {
return super.toResultSet(getPreparedStatement(sql,rsType,rsConcurrency), bindObjects);
}
// DO NOT EDIT BELOW THIS LINE, OR CHANGE THIS COMMENT, CODE AUTOMATICALLY GENERATED BY genQueryMapper.sh
@Override
public <T> T toObject(String sql, Class<T> componentType, final Object... bindObjects) throws SQLException {
return super.toObject(getPreparedStatement(sql), componentType, bindObjects);
}
@Override
public <T extends Map<String, V>, V> Map<String, V> toSingleMap(String sql, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
return super.toSingleMap(getPreparedStatement(sql), componentType, mapValType, bindObjects);
}
@Override
public <V> Map<String, V> toSingleMap(String sql, Class<V> mapValType, final Object... bindObjects) throws SQLException {
return super.toSingleMap(getPreparedStatement(sql), mapValType, bindObjects);
}
@Override
public <T extends Collection<E>, E> T toCollection(String sql, final Class<T> collectionType, Class<E> componentType, final Object... bindObjects) throws SQLException {
return super.toCollection(getPreparedStatement(sql), collectionType, componentType, bindObjects);
}
@Override
public <T extends Collection<E>, E> T toCollection(String sql, T list, Class<E> componentType, final Object... bindObjects) throws SQLException {
return super.toCollection(getPreparedStatement(sql), list, componentType, bindObjects);
}
@Override
public <T extends Map<K, E>, K, E> T toMap(String sql, final Class<T> returnType, Class<K> mapKeyType, Class<E> componentType, final Object... bindObjects) throws SQLException {
return super.toMap(getPreparedStatement(sql), returnType, mapKeyType, componentType, bindObjects);
}
@Override
public <T extends Map<K, E>, K, E> T toMap(String sql, T map, Class<K> mapKeyType, Class<E> componentType, final Object... bindObjects) throws SQLException {
return super.toMap(getPreparedStatement(sql), map, mapKeyType, componentType, bindObjects);
}
@Override
public <T extends Map<K, E>, K, E extends Collection<C>, C> T toMapCollection(String sql, final Class<T> returnType, Class<K> mapKeyType, Class<E> collectionType, Class<C> componentType, final Object... bindObjects) throws SQLException {
return super.toMapCollection(getPreparedStatement(sql), returnType, mapKeyType, collectionType, componentType, bindObjects);
}
@Override
public <T extends Map<K, E>, K, E extends Collection<C>, C> T toMapCollection(String sql, T map, Class<K> mapKeyType, Class<E> collectionType, Class<C> componentType, final Object... bindObjects) throws SQLException {
return super.toMapCollection(getPreparedStatement(sql), map, mapKeyType, collectionType, componentType, bindObjects);
}
@Override
public <T> ListIterator<T> toListIterator(String sql, final Class<T> type, final Object... bindObjects) throws SQLException {
return super.toListIterator(getPreparedStatement(sql), type, bindObjects);
}
@Override
public <T> Iterator<T> toIterator(String sql, final Class<T> type, final Object... bindObjects) throws SQLException {
return super.toIterator(getPreparedStatement(sql), type, bindObjects);
}
@Override
public <T> T[] toArray(String sql, final Class<T> type, final Object... bindObjects) throws SQLException {
return super.toArray(getPreparedStatement(sql), type, bindObjects);
}
@Override
public <E> List<E> toList(String sql, Class<E> componentType, final Object... bindObjects) throws SQLException {
return super.toList(getPreparedStatement(sql), componentType, bindObjects);
}
@Override
public <K, E> Map<K, E> toMap(String sql, Class<K> mapKeyType, Class<E> componentType, final Object... bindObjects) throws SQLException {
return super.toMap(getPreparedStatement(sql), mapKeyType, componentType, bindObjects);
}
@Override
public <K, E extends List<C>, C> Map<K, E> toMapList(String sql, Class<K> mapKeyType, Class<C> componentType, final Object... bindObjects) throws SQLException {
return super.toMapList(getPreparedStatement(sql), mapKeyType, componentType, bindObjects);
}
@Override
public <T extends Collection<E>, E extends Map<String, V>, V> T toCollectionMap(String sql, final Class<T> collectionType, Class<E> componentType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
return super.toCollectionMap(getPreparedStatement(sql), collectionType, componentType, mapValType, bindObjects);
}
@Override
public <T extends Collection<E>, E extends Map<String, V>, V> T toCollectionMap(String sql, T list, Class<E> componentType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
return super.toCollectionMap(getPreparedStatement(sql), list, componentType, mapValType, bindObjects);
}
@Override
public <T extends Map<K, E>, K, E extends Map<String, V>, V> T toMapMap(String sql, final Class<T> returnType, Class<K> mapKeyType, Class<E> componentType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
return super.toMapMap(getPreparedStatement(sql), returnType, mapKeyType, componentType, mapValType, bindObjects);
}
@Override
public <T extends Map<K, E>, K, E extends Map<String, V>, V> T toMapMap(String sql, T map, Class<K> mapKeyType, Class<E> componentType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
return super.toMapMap(getPreparedStatement(sql), map, mapKeyType, componentType, mapValType, bindObjects);
}
@Override
public <T extends Map<K, C>, K, C extends Collection<E>, E extends Map<String, V>, V> T toMapCollectionMap(String sql, final Class<T> returnType, Class<K> mapKeyType, Class<C> collectionType, Class<E> componentType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
return super.toMapCollectionMap(getPreparedStatement(sql), returnType, mapKeyType, collectionType, componentType, mapValType, bindObjects);
}
@Override
public <T extends Map<K, C>, K, C extends Collection<E>, E extends Map<String, V>, V> T toMapCollectionMap(String sql, T map, Class<K> mapKeyType, Class<C> collectionType, Class<E> componentType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
return super.toMapCollectionMap(getPreparedStatement(sql), map, mapKeyType, collectionType, componentType, mapValType, bindObjects);
}
@Override
public <T extends Map<String, V>, V> ListIterator<Map<String, V>> toListIteratorMap(String sql, final Class<T> type, Class<V> mapValType, final Object... bindObjects) throws SQLException {
return super.toListIteratorMap(getPreparedStatement(sql), type, mapValType, bindObjects);
}
@Override
public <T extends Map<String, V>, V> Iterator<Map<String, V>> toIteratorMap(String sql, final Class<T> type, Class<V> mapValType, final Object... bindObjects) throws SQLException {
return super.toIteratorMap(getPreparedStatement(sql), type, mapValType, bindObjects);
}
@Override
public <T extends Map<String, V>, V> Map<String, V>[] toArrayMap(String sql, final Class<T> type, Class<V> mapValType, final Object... bindObjects) throws SQLException {
return super.toArrayMap(getPreparedStatement(sql), type, mapValType, bindObjects);
}
@Override
public <E extends Map<String, V>, V> List<Map<String, V>> toListMap(String sql, Class<E> componentType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
return super.toListMap(getPreparedStatement(sql), componentType, mapValType, bindObjects);
}
@Override
public <K, E extends Map<String, V>, V> Map<K, Map<String, V>> toMapMap(String sql, Class<K> mapKeyType, Class<E> componentType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
return super.toMapMap(getPreparedStatement(sql), mapKeyType, componentType, mapValType, bindObjects);
}
@Override
public <K, E extends Map<String, V>, V> Map<K, List<Map<String, V>>> toMapListMap(String sql, Class<K> mapKeyType, Class<E> componentType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
return super.toMapListMap(getPreparedStatement(sql), mapKeyType, componentType, mapValType, bindObjects);
}
@Override
public <V> ListIterator<Map<String, V>> toListIteratorMap(String sql, Class<V> mapValType, final Object... bindObjects) throws SQLException {
return super.toListIteratorMap(getPreparedStatement(sql), mapValType, bindObjects);
}
@Override
public <V> Iterator<Map<String, V>> toIteratorMap(String sql, Class<V> mapValType, final Object... bindObjects) throws SQLException {
return super.toIteratorMap(getPreparedStatement(sql), mapValType, bindObjects);
}
@Override
public <V> List<Map<String, V>> toListMap(String sql, Class<V> mapValType, final Object... bindObjects) throws SQLException {
return super.toListMap(getPreparedStatement(sql), mapValType, bindObjects);
}
@Override
public <K, V> Map<K, Map<String, V>> toMapMap(String sql, Class<K> mapKeyType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
return super.toMapMap(getPreparedStatement(sql), mapKeyType, mapValType, bindObjects);
}
@Override
public <K, V> Map<K, List<Map<String, V>>> toMapListMap(String sql, Class<K> mapKeyType, Class<V> mapValType, final Object... bindObjects) throws SQLException {
return super.toMapListMap(getPreparedStatement(sql), mapKeyType, mapValType, bindObjects);
}
}

View File

@ -0,0 +1,20 @@
package com.moparisthebest.jdbc;
import java.sql.ResultSet;
import java.util.Calendar;
public class CachingResultSetMapper extends ResultSetMapper {
public CachingResultSetMapper(Calendar cal, int arrayMaxLength) {
super(cal, arrayMaxLength);
}
public CachingResultSetMapper() {
super();
}
@Override
protected <T> RowToObjectMapper<T> getRowMapper(ResultSet resultSet, Class<T> returnTypeClass, Calendar cal, Class<?> mapValType) {
return new CachingRowToObjectMapper<T>(resultSet, returnTypeClass, cal, mapValType);
}
}

View File

@ -0,0 +1,65 @@
package com.moparisthebest.jdbc;
import java.lang.reflect.AccessibleObject;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
public class CachingRowToObjectMapper<T> extends RowToObjectMapper<T> {
protected static final Map<Integer, FieldMapping> cache = new HashMap<Integer, FieldMapping>();
protected final int thisHash;
protected final String[] keys;
public CachingRowToObjectMapper(ResultSet resultSet, Class<T> returnTypeClass, Calendar cal, Class<?> mapValType) {
super(resultSet, returnTypeClass, cal, mapValType);
try {
keys = super.getKeysFromResultSet();
//System.out.printf("keys: %d, %s: %d\n", Arrays.hashCode(keys), _returnTypeClass, _returnTypeClass.hashCode());
thisHash = Arrays.hashCode(keys) ^ _returnTypeClass.hashCode();
} catch (SQLException e) {
throw new MapperException("CachingRowToObjectMapper: SQLException: " + e.getMessage(), e);
}
}
@Override
protected String[] getKeysFromResultSet() throws SQLException {
return keys;
}
@Override
protected void getFieldMappings() throws SQLException {
FieldMapping fm = cache.get(thisHash);
if (fm == null) {
//System.out.printf("cache miss, hashcode: %d\n", thisHash);
// generate and put into cache
super.getFieldMappings();
synchronized (cache) {
// I *think* we only need to synchronize here, instead of around the get as well
// it may allow some leaks (field mappings being generated more than once)
// but the performance benefits of not having this entire method synchronized
// statically probably outweighs the negatives
cache.put(thisHash, new FieldMapping(_fields, _fieldTypes));
}
} else {
//System.out.printf("cache hit, hashcode: %d\n", thisHash);
// load from cache
_fields = fm._fields;
_fieldTypes = fm._fieldTypes;
}
}
private static class FieldMapping {
public final AccessibleObject[] _fields;
public final int[] _fieldTypes;
private FieldMapping(AccessibleObject[] _fields, int[] _fieldTypes) {
this._fields = _fields;
this._fieldTypes = _fieldTypes;
}
}
}

View File

@ -0,0 +1,6 @@
package com.moparisthebest.jdbc;
public interface Cleaner<T> {
public T clean(T dto);
}

View File

@ -0,0 +1,25 @@
package com.moparisthebest.jdbc;
import java.sql.ResultSet;
import java.util.Calendar;
public class CleaningResultSetMapper<E> extends ResultSetMapper {
private final Cleaner<E> cleaner;
public CleaningResultSetMapper(Cleaner<E> cleaner, Calendar cal, int arrayMaxLength) {
super(cal, arrayMaxLength);
this.cleaner = cleaner;
}
public CleaningResultSetMapper(Cleaner<E> cleaner) {
super();
this.cleaner = cleaner;
}
@Override
@SuppressWarnings({"unchecked"})
protected <T> RowToObjectMapper<T> getRowMapper(ResultSet resultSet, Class<T> returnTypeClass, Calendar cal, Class<?> mapValType) {
return new CleaningRowToObjectMapper<T>((Cleaner<T>)cleaner, resultSet, returnTypeClass, cal, mapValType);
}
}

View File

@ -0,0 +1,21 @@
package com.moparisthebest.jdbc;
import java.sql.ResultSet;
import java.util.Calendar;
public class CleaningRowToObjectMapper<T> extends RowToObjectMapper<T> {
private final Cleaner<T> cleaner;
public CleaningRowToObjectMapper(Cleaner<T> cleaner, ResultSet resultSet, Class<T> returnTypeClass, Calendar cal, Class<?> mapValType) {
super(resultSet, returnTypeClass, cal, mapValType);
if (cleaner == null)
throw new NullPointerException("cleaner cannot be null!");
this.cleaner = cleaner;
}
@Override
public T mapRowToReturnType() {
return cleaner.clean(super.mapRowToReturnType());
}
}

View File

@ -0,0 +1,8 @@
package com.moparisthebest.jdbc;
import java.sql.ResultSet;
import java.sql.SQLException;
public interface Finishable {
public void finish(ResultSet rs) throws SQLException;
}

View File

@ -0,0 +1,45 @@
package com.moparisthebest.jdbc;
/**
* The MapperException class declares an unchecked exception that is thrown by the Controls
* runtime under certain failure conditions.
*/
public class MapperException extends RuntimeException
{
/**
* Default constructor.
*/
public MapperException() {
super();
}
/**
* Constructs a MapperException object with the specified String as a message.
*
* @param message The message to use.
*/
public MapperException(String message)
{
super(message);
}
/**
* Constructs a MapperException with the specified cause.
* @param t the cause
*/
public MapperException(Throwable t) {
super(t);
}
/**
* Constructs a MapperException object using the specified String as a message, and the
* specified Throwable as a nested exception.
*
* @param message The message to use.
* @param t The exception to nest within this exception.
*/
public MapperException(String message, Throwable t)
{
super(message + "[" + t + "]", t);
}
}

View File

@ -0,0 +1,887 @@
package com.moparisthebest.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;
public class NullQueryMapper extends QueryMapper {
protected final boolean verbose;
protected final QueryMapper delegate;
private NullQueryMapper(Connection conn, String jndiName, QueryMapper delegate, ResultSetMapper cm, boolean verbose) {
this.verbose = verbose;
this.delegate = delegate == null ? new QueryMapper(conn, jndiName, cm) : delegate;
}
public NullQueryMapper(QueryMapper delegate, boolean verbose) {
this(null, null, delegate, null, verbose);
}
public NullQueryMapper(QueryMapper delegate) {
this(null, null, delegate, null, true);
}
public NullQueryMapper(Connection conn, boolean verbose) {
this(conn, null, null, null, verbose);
}
public NullQueryMapper(Connection conn, ResultSetMapper cm, boolean verbose) {
this(conn, null, null, cm, verbose);
}
public NullQueryMapper(Connection conn) {
this(conn, true);
}
public NullQueryMapper(Connection conn, ResultSetMapper cm) {
this(conn, cm, true);
}
public NullQueryMapper(String jndiName, boolean verbose) {
this(null, jndiName, null, null, verbose);
}
public NullQueryMapper(String jndiName, ResultSetMapper cm, boolean verbose) {
this(null, jndiName, null, cm, verbose);
}
public NullQueryMapper(String jndiName) {
this(jndiName, true);
}
public NullQueryMapper(String jndiName, ResultSetMapper cm) {
this(jndiName, cm, true);
}
// these update the database
@Override
public int executeUpdate(PreparedStatement ps, Object... bindObjects) {
try {
return delegate.executeUpdate(ps, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return -1;
}
@Override
public boolean executeUpdateSuccess(PreparedStatement ps, Object... bindObjects) {
try {
return delegate.executeUpdateSuccess(ps, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return false;
}
@Override
public int executeUpdate(String sql, Object... bindObjects) {
try {
return delegate.executeUpdate(sql, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return -1;
}
@Override
public boolean executeUpdateSuccess(String sql, Object... bindObjects) {
try {
return delegate.executeUpdateSuccess(sql, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return false;
}
// these update the database using UpdateableDTOs
@Override
public int updateRows(UpdateableDTO dto) {
try {
return delegate.updateRows(dto);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return -1;
}
@Override
public int updateRows(Collection<UpdateableDTO> dtos) {
try {
return delegate.updateRows(dtos);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return -1;
}
@Override
public int updateRows(UpdateableDTO[] dtos) {
try {
return delegate.updateRows(dtos);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return -1;
}
@Override
public int insertRows(UpdateableDTO dto) {
try {
return delegate.insertRows(dto);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return -1;
}
@Override
public int insertRows(Collection<UpdateableDTO> dtos) {
try {
return delegate.insertRows(dtos);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return -1;
}
@Override
public int insertRows(UpdateableDTO[] dtos) {
try {
return delegate.insertRows(dtos);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return -1;
}
// these grab ResultSets from the database
@Override
public ResultSet toResultSet(PreparedStatement ps, Object... bindObjects) {
try {
return delegate.toResultSet(ps, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public ResultSet toResultSet(String sql, Object... bindObjects) {
try {
return delegate.toResultSet(sql, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
// these are standard getters
@Override
public ResultSetMapper getCustomResultSetMapper() {
return delegate.getCustomResultSetMapper();
}
@Override
public Connection getConnection() {
return delegate.getConnection();
}
// these just delegate and change no functionality
@Override
public void close() {
delegate.close();
}
@Override
protected PreparedStatement bind(PreparedStatement ps, Object... bindObjects) throws SQLException {
return delegate.bind(ps, bindObjects);
}
@Override
protected ResultSet bindExecute(PreparedStatement ps, Object... bindObjects) throws SQLException {
return delegate.bindExecute(ps, bindObjects);
}
// and these are standard
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
NullQueryMapper that = (NullQueryMapper) o;
if (verbose != that.verbose) return false;
if (delegate != null ? !delegate.equals(that.delegate) : that.delegate != null) return false;
return true;
}
@Override
public int hashCode() {
int result = (verbose ? 1 : 0);
result = 31 * result + (delegate != null ? delegate.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "NullQueryMapper{" +
"verbose=" + verbose +
", delegate=" + delegate +
"}";
}
// DO NOT EDIT BELOW THIS LINE, OR CHANGE THIS COMMENT, CODE AUTOMATICALLY GENERATED BY genQueryMapper.sh
@Override
public <T> T toObject(PreparedStatement query, Class<T> componentType, final Object... bindObjects) {
try {
return delegate.toObject(query, componentType, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <T> T toObject(String query, Class<T> componentType, final Object... bindObjects) {
try {
return delegate.toObject(query, componentType, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <T extends Map<String, V>, V> Map<String, V> toSingleMap(PreparedStatement query, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) {
try {
return delegate.toSingleMap(query, componentType, mapValType, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <T extends Map<String, V>, V> Map<String, V> toSingleMap(String query, Class<T> componentType, Class<V> mapValType, final Object... bindObjects) {
try {
return delegate.toSingleMap(query, componentType, mapValType, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <V> Map<String, V> toSingleMap(PreparedStatement query, Class<V> mapValType, final Object... bindObjects) {
try {
return delegate.toSingleMap(query, mapValType, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <V> Map<String, V> toSingleMap(String query, Class<V> mapValType, final Object... bindObjects) {
try {
return delegate.toSingleMap(query, mapValType, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <T extends Collection<E>, E> T toCollection(PreparedStatement query, final Class<T> collectionType, Class<E> componentType, final Object... bindObjects) {
try {
return delegate.toCollection(query, collectionType, componentType, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <T extends Collection<E>, E> T toCollection(String query, final Class<T> collectionType, Class<E> componentType, final Object... bindObjects) {
try {
return delegate.toCollection(query, collectionType, componentType, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <T extends Collection<E>, E> T toCollection(PreparedStatement query, T list, Class<E> componentType, final Object... bindObjects) {
try {
return delegate.toCollection(query, list, componentType, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <T extends Collection<E>, E> T toCollection(String query, T list, Class<E> componentType, final Object... bindObjects) {
try {
return delegate.toCollection(query, list, componentType, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <T extends Map<K, E>, K, E> T toMap(PreparedStatement query, final Class<T> returnType, Class<K> mapKeyType, Class<E> componentType, final Object... bindObjects) {
try {
return delegate.toMap(query, returnType, mapKeyType, componentType, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <T extends Map<K, E>, K, E> T toMap(String query, final Class<T> returnType, Class<K> mapKeyType, Class<E> componentType, final Object... bindObjects) {
try {
return delegate.toMap(query, returnType, mapKeyType, componentType, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <T extends Map<K, E>, K, E> T toMap(PreparedStatement query, T map, Class<K> mapKeyType, Class<E> componentType, final Object... bindObjects) {
try {
return delegate.toMap(query, map, mapKeyType, componentType, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <T extends Map<K, E>, K, E> T toMap(String query, T map, Class<K> mapKeyType, Class<E> componentType, final Object... bindObjects) {
try {
return delegate.toMap(query, map, mapKeyType, componentType, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <T extends Map<K, E>, K, E extends Collection<C>, C> T toMapCollection(PreparedStatement query, final Class<T> returnType, Class<K> mapKeyType, Class<E> collectionType, Class<C> componentType, final Object... bindObjects) {
try {
return delegate.toMapCollection(query, returnType, mapKeyType, collectionType, componentType, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <T extends Map<K, E>, K, E extends Collection<C>, C> T toMapCollection(String query, final Class<T> returnType, Class<K> mapKeyType, Class<E> collectionType, Class<C> componentType, final Object... bindObjects) {
try {
return delegate.toMapCollection(query, returnType, mapKeyType, collectionType, componentType, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <T extends Map<K, E>, K, E extends Collection<C>, C> T toMapCollection(PreparedStatement query, T map, Class<K> mapKeyType, Class<E> collectionType, Class<C> componentType, final Object... bindObjects) {
try {
return delegate.toMapCollection(query, map, mapKeyType, collectionType, componentType, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <T extends Map<K, E>, K, E extends Collection<C>, C> T toMapCollection(String query, T map, Class<K> mapKeyType, Class<E> collectionType, Class<C> componentType, final Object... bindObjects) {
try {
return delegate.toMapCollection(query, map, mapKeyType, collectionType, componentType, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <T> ListIterator<T> toListIterator(PreparedStatement query, final Class<T> type, final Object... bindObjects) {
try {
return delegate.toListIterator(query, type, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <T> ListIterator<T> toListIterator(String query, final Class<T> type, final Object... bindObjects) {
try {
return delegate.toListIterator(query, type, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <T> Iterator<T> toIterator(PreparedStatement query, final Class<T> type, final Object... bindObjects) {
try {
return delegate.toIterator(query, type, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <T> Iterator<T> toIterator(String query, final Class<T> type, final Object... bindObjects) {
try {
return delegate.toIterator(query, type, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <T> T[] toArray(PreparedStatement query, final Class<T> type, final Object... bindObjects) {
try {
return delegate.toArray(query, type, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <T> T[] toArray(String query, final Class<T> type, final Object... bindObjects) {
try {
return delegate.toArray(query, type, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <E> List<E> toList(PreparedStatement query, Class<E> componentType, final Object... bindObjects) {
try {
return delegate.toList(query, componentType, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <E> List<E> toList(String query, Class<E> componentType, final Object... bindObjects) {
try {
return delegate.toList(query, componentType, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <K, E> Map<K, E> toMap(PreparedStatement query, Class<K> mapKeyType, Class<E> componentType, final Object... bindObjects) {
try {
return delegate.toMap(query, mapKeyType, componentType, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <K, E> Map<K, E> toMap(String query, Class<K> mapKeyType, Class<E> componentType, final Object... bindObjects) {
try {
return delegate.toMap(query, mapKeyType, componentType, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <K, E extends List<C>, C> Map<K, E> toMapList(PreparedStatement query, Class<K> mapKeyType, Class<C> componentType, final Object... bindObjects) {
try {
return delegate.toMapList(query, mapKeyType, componentType, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <K, E extends List<C>, C> Map<K, E> toMapList(String query, Class<K> mapKeyType, Class<C> componentType, final Object... bindObjects) {
try {
return delegate.toMapList(query, mapKeyType, componentType, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <T extends Collection<E>, E extends Map<String, V>, V> T toCollectionMap(PreparedStatement query, final Class<T> collectionType, Class<E> componentType, Class<V> mapValType, final Object... bindObjects) {
try {
return delegate.toCollectionMap(query, collectionType, componentType, mapValType, bindObjects);
} catch (Throwable e) {
if (verbose) e.printStackTrace();
}
return null;
}
@Override
public <T extends Collection<E>, E extends Map<String, V>, V> T toCollectionMap(String query, final Class<T> collectionType, Class<E> componentType, Class<V> mapValType, final Object... bindObjects) {
try {
return delegate.toCollectionMap(query, collectionType, componentType, mapValType, bindObjects);
} catch (Throwable e) {