diff --git a/beehive-jdbc-control/pom.xml b/beehive-jdbc-control/pom.xml
index 7e043d6..b1f3909 100644
--- a/beehive-jdbc-control/pom.xml
+++ b/beehive-jdbc-control/pom.xml
@@ -34,9 +34,8 @@
${project.version}
- com.moparisthebest.beehive
- beehive-jdbc-mapper
- ${project.version}
+ com.moparisthebest.jdbcmapper
+ querymappercom.moparisthebest.aptIn16
diff --git a/beehive-jdbc-mapper/genQueryMapper.sh b/beehive-jdbc-mapper/genQueryMapper.sh
deleted file mode 100755
index 58d5800..0000000
--- a/beehive-jdbc-mapper/genQueryMapper.sh
+++ /dev/null
@@ -1,133 +0,0 @@
-#!/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" <> "$result" <> "$query" <> "$caching_query" <> "$null_query"
-
- # ListQueryMapper.java
- cat >> "$list_query" <
- junit
- junit
- test
-
-
- org.apache.derby
- derby
- test
- true
+ com.moparisthebest.jdbcmapper
+ querymapper${project.artifactId}
-
-
-
- maven-compiler-plugin
- 3.1
-
-
- 1.6
- true
-
-
-
-
- my-testCompile
- test-compile
-
- testCompile
-
-
-
- com.moparisthebest.jdbc.codegen.JdbcMapperProcessor
-
-
-
-
-
-
diff --git a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/classgen/AbstractSQLParser.java b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/classgen/AbstractSQLParser.java
deleted file mode 100644
index a22faf3..0000000
--- a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/classgen/AbstractSQLParser.java
+++ /dev/null
@@ -1,26 +0,0 @@
-package com.moparisthebest.classgen;
-
-/**
- * Created by mopar on 5/25/17.
- */
-public abstract class AbstractSQLParser implements SQLParser {
-
- private final String[] columnNames;
- private final boolean isSelect;
-
- protected AbstractSQLParser(final String[] columnNames, final boolean isSelect) {
- this.columnNames = columnNames;
- this.isSelect = isSelect;
- }
-
- @Override
- public final String[] columnNames() {
- return columnNames;
- }
-
- @Override
- public final boolean isSelect() {
- return isSelect;
- }
-
-}
diff --git a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/classgen/Compiler.java b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/classgen/Compiler.java
deleted file mode 100644
index eda2988..0000000
--- a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/classgen/Compiler.java
+++ /dev/null
@@ -1,175 +0,0 @@
-package com.moparisthebest.classgen;
-
-
-import javax.tools.*;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-/**
- * This let's you take compile and then run Java source code at runtime, this class only allows you to compile 1 class
- * at a time.
- *
- * This is not thread safe, though it could be extended and made thread safe, or locked around.
- *
- * @author moparisthebest
- * @see MultiCompiler for compiling multiple classes at once
- *
- * The original idea was taken from:
- * http://mindprod.com/jgloss/javacompiler.html#SAMPLECODE
- */
-public class Compiler {
-
- private final JavaCompiler compiler;
- private final MemoryJavaFileManager mjfm;
- //private final ClassLoader classLoader;
- private final List singleton = Arrays.asList(new JavaFileObject[1]);
-
- public Compiler() {
- this(false);
- }
-
- protected Compiler(final boolean multi) {
- compiler = ToolProvider.getSystemJavaCompiler();
- if (compiler == null)
- throw new RuntimeException("tools.jar needs to be on classpath to compile code at runtime");
- final JavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
- mjfm = multi ? new MultiMemoryJavaFileManager(fileManager) : new SingleMemoryJavaFileManager(fileManager);
- //classLoader = new MemoryClassLoader(mjfm);
- }
-
- protected ClassLoader compile(final Iterable extends JavaFileObject> source) {
- //final MemoryJavaFileManager mjfm = new MemoryJavaFileManager(compiler.getStandardFileManager(null, null, null));
- final JavaCompiler.CompilationTask task = compiler.getTask(null, mjfm, null, null, null, source);
- return task.call() ?
- new MemoryClassLoader(mjfm)
- //classLoader
- : null;
- }
-
- protected ClassLoader compile(final JavaFileObject... source) {
- return compile(Arrays.asList(source));
- }
-
- public ClassLoader compile(final JavaFileObject source) {
- singleton.set(0, source);
- return compile(singleton);
- }
-
- protected T compile(final String className, final Iterable extends JavaFileObject> source) {
- // compile item
- final ClassLoader cl = compile(source);
- if (cl == null)
- throw new RuntimeException("Error compiling class, aborting...");
- return instantiate(cl, className);
- }
-
- protected T compile(final String className, final JavaFileObject... source) {
- return compile(className, Arrays.asList(source));
- }
-
- public T compile(final String className, final JavaFileObject source) {
- singleton.set(0, source);
- return compile(className, singleton);
- }
-
- public T compile(final String className, final CharSequence code) {
- return compile(className, new StringJavaFileObject(className, code));
- }
-
- public T instantiate(final ClassLoader cl, final String className) {
- try {
- // Load class and create an instance.
- final Class> calcClass = cl.loadClass(className);
- @SuppressWarnings("unchecked") final T ret = (T) calcClass.newInstance();
- return ret;
- } catch (Exception e) {
- throw new RuntimeException("Error finding or instantiating class, exiting...", e);
- }
- }
-
-}
-
-class MemoryJavaFileObject extends ForwardingJavaFileObject {
-
- final ByteArrayOutputStream baos = new ByteArrayOutputStream();
-
- MemoryJavaFileObject(JavaFileObject fileObject) {
- super(fileObject);
- }
-
- @Override
- public OutputStream openOutputStream() throws IOException {
- return baos;
- }
-}
-
-interface MemoryJavaFileManager extends JavaFileManager {
- MemoryJavaFileObject getMemoryJavaFileObject(final String name);
-}
-
-class SingleMemoryJavaFileManager extends ForwardingJavaFileManager implements MemoryJavaFileManager {
-
- private MemoryJavaFileObject classes = null;
-
- SingleMemoryJavaFileManager(JavaFileManager fileManager) {
- super(fileManager);
- }
-
- @Override
- public MemoryJavaFileObject getMemoryJavaFileObject(final String name) {
- final MemoryJavaFileObject ret = classes;
- classes = null;
- return ret;
- }
-
- @Override
- public JavaFileObject getJavaFileForOutput(Location location, String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException {
- final MemoryJavaFileObject jfo = new MemoryJavaFileObject(super.getJavaFileForOutput(location, className, kind, sibling));
- //System.out.printf("MemoryJavaFileManager.getJavaFileForOutput(%s, %s, %s, %s): %s\n", location, className, kind, sibling, jfo);
- classes = jfo;
- return jfo;
- }
-}
-
-class MultiMemoryJavaFileManager extends ForwardingJavaFileManager implements MemoryJavaFileManager {
-
- private Map classes = new HashMap();
-
- MultiMemoryJavaFileManager(JavaFileManager fileManager) {
- super(fileManager);
- }
-
- @Override
- public MemoryJavaFileObject getMemoryJavaFileObject(final String name) {
- return classes.remove(name);
- }
-
- @Override
- public JavaFileObject getJavaFileForOutput(Location location, String className, JavaFileObject.Kind kind, FileObject sibling) throws IOException {
- final MemoryJavaFileObject jfo = new MemoryJavaFileObject(super.getJavaFileForOutput(location, className, kind, sibling));
- //System.out.printf("MemoryJavaFileManager.getJavaFileForOutput(%s, %s, %s, %s): %s\n", location, className, kind, sibling, jfo);
- classes.put(className, jfo);
- return jfo;
- }
-}
-
-class MemoryClassLoader extends ClassLoader {
- final MemoryJavaFileManager jfm;
-
- MemoryClassLoader(MemoryJavaFileManager jfm) {
- this.jfm = jfm;
- }
-
- public Class findClass(String name) throws ClassNotFoundException {
- final MemoryJavaFileObject jfo = jfm.getMemoryJavaFileObject(name);
- if (jfo == null)
- throw new ClassNotFoundException("Class '" + name + "' cannot be found in the MemoryJavaFileManager");
- final byte[] b = jfo.baos.toByteArray();
- return defineClass(name, b, 0, b.length);
- }
-}
diff --git a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/classgen/MultiCompiler.java b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/classgen/MultiCompiler.java
deleted file mode 100644
index be555f3..0000000
--- a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/classgen/MultiCompiler.java
+++ /dev/null
@@ -1,35 +0,0 @@
-package com.moparisthebest.classgen;
-
-import javax.tools.JavaFileObject;
-
-/**
- * This lets you compile multiple source files at once
- */
-public class MultiCompiler extends Compiler {
-
- public static Compiler instance = new MultiCompiler();
-
- public MultiCompiler() {
- super(true);
- }
-
- @Override
- public ClassLoader compile(final Iterable extends JavaFileObject> source) {
- return super.compile(source);
- }
-
- @Override
- public ClassLoader compile(final JavaFileObject... source) {
- return super.compile(source);
- }
-
- @Override
- public T compile(final String className, final JavaFileObject... source) {
- return super.compile(className, source);
- }
-
- @Override
- public T compile(final String className, final Iterable extends JavaFileObject> source) {
- return super.compile(className, source);
- }
-}
diff --git a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/classgen/SQLParser.java b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/classgen/SQLParser.java
deleted file mode 100644
index 9c2b20c..0000000
--- a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/classgen/SQLParser.java
+++ /dev/null
@@ -1,27 +0,0 @@
-package com.moparisthebest.classgen;
-
-/**
- * Created by mopar on 5/25/17.
- */
-public interface SQLParser {
-
- /**
- * Return SQLParser instance for given SQL
- *
- * @param sql SQL to parse
- * @return instance with string parsed
- */
- SQLParser parse(String sql);
-
- /**
- * @return column names for select, with 1-based index like ResultSet, index 0 is always null, not used if isSelect() returns false
- */
- String[] columnNames();
-
- /**
- * Return
- *
- * @return true for Select, if we will map to an object, false to call executeUpdate (insert/update/merge/?)
- */
- boolean isSelect();
-}
diff --git a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/classgen/SimpleSQLParser.java b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/classgen/SimpleSQLParser.java
deleted file mode 100644
index 6fc6ee4..0000000
--- a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/classgen/SimpleSQLParser.java
+++ /dev/null
@@ -1,39 +0,0 @@
-package com.moparisthebest.classgen;
-
-import java.util.regex.Pattern;
-
-/**
- * Created by mopar on 5/25/17.
- */
-public class SimpleSQLParser extends AbstractSQLParser {
-
- private static final Pattern aliasPattern = Pattern.compile("^.*\\.");
-
- public SimpleSQLParser() {
- super(null, false);
- }
-
-
- private SimpleSQLParser(final String[] columnNames, final boolean isSelect) {
- super(columnNames, isSelect);
- }
-
- @Override
- public SQLParser parse(String sql) {
- sql = sql.toUpperCase();
- final boolean isSelect = sql.startsWith("SELECT");
- String[] columnNames = null;
- if (isSelect) {
- final String columns = sql.substring(sql.indexOf("SELECT") + 6, sql.indexOf("FROM")).trim();
- final String[] splitColumns = columns.split(",");
- columnNames = new String[splitColumns.length + 1];
- int index = 0;
- for (String column : splitColumns) {
- final String[] singleSplit = column.split("\\s");
- // trim and remove leading table/alias
- columnNames[++index] = aliasPattern.matcher(singleSplit[singleSplit.length - 1].trim()).replaceFirst("");
- }
- }
- return new SimpleSQLParser(columnNames, isSelect);
- }
-}
diff --git a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/classgen/StringJavaFileObject.java b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/classgen/StringJavaFileObject.java
deleted file mode 100644
index 946effb..0000000
--- a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/classgen/StringJavaFileObject.java
+++ /dev/null
@@ -1,22 +0,0 @@
-package com.moparisthebest.classgen;
-
-import javax.tools.SimpleJavaFileObject;
-import java.net.URI;
-
-/**
- * For sending java source as strings to a Compiler
- * @see Compiler
- */
-public class StringJavaFileObject extends SimpleJavaFileObject {
- private final CharSequence code;
-
- public StringJavaFileObject(final String name, final CharSequence code) {
- super(URI.create("string:///" + name.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE);
- this.code = code;
- }
-
- @Override
- public CharSequence getCharContent(boolean ignoreEncodingErrors) {
- return code;
- }
-}
diff --git a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/AbstractRowMapper.java b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/AbstractRowMapper.java
deleted file mode 100644
index 2e7a9c2..0000000
--- a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/AbstractRowMapper.java
+++ /dev/null
@@ -1,113 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- * $Header:$
- */
-
-package com.moparisthebest.jdbc;
-
-import java.sql.ResultSet;
-import java.sql.ResultSetMetaData;
-import java.sql.SQLException;
-import java.util.Calendar;
-
-/**
- * Abstract base class for all row mappers.
- *
- * RowMappers are used to map the contents of a row in a ResultSet to the return type of an annotated method.
- * Supported RowMapper types include: HashMap, Map, Object, XmlObject. When a ResultSetMapper is ready to
- * map a ResultSet row to an object, it requests a RowMapper for the return type of the method from the
- * RowMapperFactory.
- *
- */
-public abstract class AbstractRowMapper implements RowMapper {
-
- /** ResultSet to map. */
- protected final ResultSet _resultSet;
-
- /** Calendar instance for date/time mappings. */
- protected final Calendar _cal;
-
- /** Class to map ResultSet Rows to. */
- protected final Class _returnTypeClass;
-
- protected final Class _mapKeyType;
-
- protected final int _columnCount;
-
- protected final boolean mapOnlySecondColumn;
-
- protected String[] keys = null; // for caching if we must generate class
-
- /**
- * Create a new RowMapper for the specified ResultSet and return type Class.
- * @param resultSet ResultSet to map
- * @param returnTypeClass Class to map ResultSet rows to.
- * @param cal Calendar instance for date/time values.
- */
- protected AbstractRowMapper(String[] keys, ResultSet resultSet, Class returnTypeClass, Calendar cal, Class mapKeyType) {
- _resultSet = resultSet;
- _returnTypeClass = returnTypeClass;
- _cal = cal;
- _mapKeyType = mapKeyType;
-
- if(keys == null) {
- try {
- _columnCount = resultSet.getMetaData().getColumnCount();
- } catch (SQLException e) {
- throw new MapperException("RowToObjectMapper: SQLException: " + e.getMessage(), e);
- }
- } else {
- this.keys = keys;
- _columnCount = keys.length - 1;
- }
-
- mapOnlySecondColumn = _mapKeyType != null && _columnCount == 2;
- }
-
- protected AbstractRowMapper(ResultSet resultSet, Class returnTypeClass, Calendar cal, Class mapKeyType) {
- this(null, resultSet, returnTypeClass, cal, mapKeyType);
- }
-
- protected AbstractRowMapper(ResultSet resultSet, Class returnTypeClass, Calendar cal) {
- this(resultSet, returnTypeClass, cal, null);
- }
-
- protected AbstractRowMapper(String[] keys, Class returnTypeClass, Calendar cal, Class mapKeyType) {
- this(keys, null, returnTypeClass, cal, mapKeyType);
- }
-
- /**
- * Build a String array of column names from the ResultSet.
- * @return A String array containing the column names contained within the ResultSet.
- * @throws java.sql.SQLException on error
- */
- protected String[] getKeysFromResultSet() throws SQLException {
-
- if(this.keys != null)
- return this.keys;
-
- String[] keys;
- final ResultSetMetaData md = _resultSet.getMetaData();
-
- keys = new String[_columnCount + 1];
- for (int i = 1; i <= _columnCount; i++) {
- //keys[i] = md.getColumnName(i).toUpperCase();
- keys[i] = md.getColumnLabel(i).toUpperCase();
- }
- return this.keys = keys;
- }
-}
diff --git a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/ArrayInList.java b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/ArrayInList.java
deleted file mode 100644
index 17c5a1e..0000000
--- a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/ArrayInList.java
+++ /dev/null
@@ -1,52 +0,0 @@
-package com.moparisthebest.jdbc;
-
-import java.sql.Array;
-import java.sql.Connection;
-import java.sql.SQLException;
-import java.util.Collection;
-
-/**
- * Created by mopar on 4/29/15.
- */
-public class ArrayInList implements InList {
-
- private static final InList instance = new ArrayInList();
-
- public static InList instance() {
- return instance;
- }
-
- protected ArrayInList() {
- }
-
- protected String columnAppend(final String columnName) {
- return "(" + columnName + " = ANY(?))";
- }
-
- protected Array toArray(final Connection conn, final Collection values) throws SQLException {
- return conn.createArrayOf(
- values.iterator().next() instanceof Number ? "number" : "text",
- values.toArray()
- );
- }
-
- public InListObject inList(final Connection conn, final String columnName, final Collection values) throws SQLException {
- return values == null || values.isEmpty() ? InListObject.empty : new ArrayListObject(
- columnAppend(columnName),
- toArray(conn, values)
- );
- }
-
- class ArrayListObject extends InListObject {
- private final Array array;
-
- public ArrayListObject(final String sql, final Array array) {
- super(sql);
- this.array = array;
- }
-
- public Array getArray() {
- return array;
- }
- }
-}
diff --git a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/BindInList.java b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/BindInList.java
deleted file mode 100644
index 80f4288..0000000
--- a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/BindInList.java
+++ /dev/null
@@ -1,92 +0,0 @@
-package com.moparisthebest.jdbc;
-
-import java.sql.Connection;
-import java.util.*;
-
-/**
- * Created by mopar on 4/29/15.
- */
-public class BindInList implements InList {
-
- private static final int defaultMaxSize = Integer.parseInt(System.getProperty("QueryMapper.BindInList.defaultMaxSize", "999"));
-
- private static final InList instance = new BindInList();
-
- public static InList instance() {
- return instance;
- }
-
- private final int maxSize;
-
- public BindInList(final int maxSize) {
- this.maxSize = maxSize;
- }
-
- protected BindInList() {
- this(defaultMaxSize);
- }
-
- private static List> split(final List list, final int maxLength) {
- final int listSize = list.size();
- final List> ret = new ArrayList>();
- if (listSize < maxLength)
- ret.add(list);
- else
- for (int fromIndex = 0, toIndex = maxLength; fromIndex < listSize; fromIndex = toIndex, toIndex += maxLength)
- ret.add(list.subList(fromIndex, toIndex > listSize ? listSize : toIndex));
- return ret;
- }
-
- private String toInList(final String fieldName, final Collection items) {
- final StringBuilder sb = new StringBuilder("(");
-
- // do it quick if it will fit in one in-list
- if (items.size() < maxSize) // 999 or less
- return buildInList(items, sb, fieldName).append(")").toString();
-
- // else we need to split lists
- boolean notFirst = false;
- for (final List item : split(new ArrayList(items), maxSize)) {
- if (notFirst) sb.append(" OR ");
- else notFirst = true;
- buildInList(item, sb, fieldName);
- }
-
- return sb.append(")").toString();
- }
-
- private static StringBuilder buildInList(Iterable list, StringBuilder sb, String fieldName) {
- return oracleCommaSeparatedList(list, sb.append(fieldName).append(" IN (")).append(")");
- }
-
- private static StringBuilder oracleCommaSeparatedList(Iterable list, StringBuilder sb) {
- boolean notFirst = false;
- for (final T obj : list) {
- // DO NOT DO THIS ANYMORE: if (obj == null) continue;
- if (notFirst) sb.append(',');
- else notFirst = true;
- sb.append('?');
- }
- return sb;
- }
-
- public InListObject inList(final Connection conn, final String columnName, final Collection values) {
- return values == null || values.isEmpty() ? InListObject.empty : new BindInListObject(
- toInList(columnName, values),
- values.toArray()
- );
- }
-
- class BindInListObject extends InListObject {
- private final Object[] bindObjects;
-
- public BindInListObject(final String sql, final Object[] bindObjects) {
- super(sql);
- this.bindObjects = bindObjects;
- }
-
- public Object[] getBindObjects() {
- return bindObjects;
- }
- }
-}
diff --git a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/CachingQueryMapper.java b/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/CachingQueryMapper.java
deleted file mode 100644
index ffb5c52..0000000
--- a/beehive-jdbc-mapper/src/main/java/com/moparisthebest/jdbc/CachingQueryMapper.java
+++ /dev/null
@@ -1,291 +0,0 @@
-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 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(initialCapacity, loadFactor, true) {
- @Override
- protected boolean removeEldestEntry(Map.Entry 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();
- }
-
- 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 toObject(String sql, Class componentType, final Object... bindObjects) throws SQLException {
- return super.toObject(getPreparedStatement(sql), componentType, bindObjects);
- }
-
- @Override
- public , V> Map toSingleMap(String sql, Class componentType, Class mapValType, final Object... bindObjects) throws SQLException {
- return super.toSingleMap(getPreparedStatement(sql), componentType, mapValType, bindObjects);
- }
-
- @Override
- public Map toSingleMap(String sql, Class mapValType, final Object... bindObjects) throws SQLException {
- return super.toSingleMap(getPreparedStatement(sql), mapValType, bindObjects);
- }
-
- @Override
- public , E> T toCollection(String sql, final Class collectionType, Class componentType, final Object... bindObjects) throws SQLException {
- return super.toCollection(getPreparedStatement(sql), collectionType, componentType, bindObjects);
- }
-
- @Override
- public , E> T toCollection(String sql, T list, Class componentType, final Object... bindObjects) throws SQLException {
- return super.toCollection(getPreparedStatement(sql), list, componentType, bindObjects);
- }
-
- @Override
- public , K, E> T toMap(String sql, T map, Class mapKeyType, Class componentType, final Object... bindObjects) throws SQLException {
- return super.toMap(getPreparedStatement(sql), map, mapKeyType, componentType, bindObjects);
- }
-
- @Override
- public , K, E extends Collection, C> T toMapCollection(String sql, final Class returnType, Class mapKeyType, Class collectionType, Class componentType, final Object... bindObjects) throws SQLException {
- return super.toMapCollection(getPreparedStatement(sql), returnType, mapKeyType, collectionType, componentType, bindObjects);
- }
-
- @Override
- public , K, E extends Collection, C> T toMapCollection(String sql, T map, Class mapKeyType, Class collectionType, Class componentType, final Object... bindObjects) throws SQLException {
- return super.toMapCollection(getPreparedStatement(sql), map, mapKeyType, collectionType, componentType, bindObjects);
- }
-
- @Override
- public ListIterator toListIterator(String sql, final Class type, final Object... bindObjects) throws SQLException {
- return super.toListIterator(getPreparedStatement(sql), type, bindObjects);
- }
-
- @Override
- public Iterator toIterator(String sql, final Class type, final Object... bindObjects) throws SQLException {
- return super.toIterator(getPreparedStatement(sql), type, bindObjects);
- }
-
- @Override
- public T[] toArray(String sql, final Class type, final Object... bindObjects) throws SQLException {
- return super.toArray(getPreparedStatement(sql), type, bindObjects);
- }
-
- @Override
- public List toList(String sql, Class componentType, final Object... bindObjects) throws SQLException {
- return super.toList(getPreparedStatement(sql), componentType, bindObjects);
- }
-
- @Override
- public Map toMap(String sql, Class mapKeyType, Class componentType, final Object... bindObjects) throws SQLException {
- return super.toMap(getPreparedStatement(sql), mapKeyType, componentType, bindObjects);
- }
-
- @Override
- public , C> Map toMapList(String sql, Class mapKeyType, Class componentType, final Object... bindObjects) throws SQLException {
- return super.toMapList(getPreparedStatement(sql), mapKeyType, componentType, bindObjects);
- }
-
- @Override
- public , E extends Map, V> T toCollectionMap(String sql, final Class collectionType, Class componentType, Class mapValType, final Object... bindObjects) throws SQLException {
- return super.toCollectionMap(getPreparedStatement(sql), collectionType, componentType, mapValType, bindObjects);
- }
-
- @Override
- public , E extends Map, V> T toCollectionMap(String sql, T list, Class componentType, Class mapValType, final Object... bindObjects) throws SQLException {
- return super.toCollectionMap(getPreparedStatement(sql), list, componentType, mapValType, bindObjects);
- }
-
- @Override
- public , K, E extends Map, V> T toMapMap(String sql, final Class returnType, Class mapKeyType, Class componentType, Class mapValType, final Object... bindObjects) throws SQLException {
- return super.toMapMap(getPreparedStatement(sql), returnType, mapKeyType, componentType, mapValType, bindObjects);
- }
-
- @Override
- public , K, E extends Map, V> T toMapMap(String sql, T map, Class mapKeyType, Class componentType, Class mapValType, final Object... bindObjects) throws SQLException {
- return super.toMapMap(getPreparedStatement(sql), map, mapKeyType, componentType, mapValType, bindObjects);
- }
-
- @Override
- public , K, C extends Collection, E extends Map, V> T toMapCollectionMap(String sql, final Class returnType, Class mapKeyType, Class collectionType, Class componentType, Class mapValType, final Object... bindObjects) throws SQLException {
- return super.toMapCollectionMap(getPreparedStatement(sql), returnType, mapKeyType, collectionType, componentType, mapValType, bindObjects);
- }
-
- @Override
- public , K, C extends Collection, E extends Map, V> T toMapCollectionMap(String sql, T map, Class mapKeyType, Class collectionType, Class componentType, Class mapValType, final Object... bindObjects) throws SQLException {
- return super.toMapCollectionMap(getPreparedStatement(sql), map, mapKeyType, collectionType, componentType, mapValType, bindObjects);
- }
-
- @Override
- public , V> ListIterator