You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by th...@apache.org on 2017/07/07 02:06:27 UTC
[05/58] [abbrv] commons-dbutils git commit: Changed the package names
so dbutils and dbutils2 won't conflict if both loaded
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/main/java/org/apache/commons/dbutils2/handlers/ArrayHandler.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/dbutils2/handlers/ArrayHandler.java b/src/main/java/org/apache/commons/dbutils2/handlers/ArrayHandler.java
new file mode 100644
index 0000000..0863894
--- /dev/null
+++ b/src/main/java/org/apache/commons/dbutils2/handlers/ArrayHandler.java
@@ -0,0 +1,81 @@
+/*
+ * 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.
+ */
+package org.apache.commons.dbutils2.handlers;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import org.apache.commons.dbutils2.BasicRowProcessor;
+import org.apache.commons.dbutils2.ResultSetHandler;
+import org.apache.commons.dbutils2.RowProcessor;
+
+/**
+ * <code>ResultSetHandler</code> implementation that converts a
+ * <code>ResultSet</code> into an <code>Object[]</code>. This class is
+ * thread safe.
+ *
+ * @see org.apache.commons.dbutils2.ResultSetHandler
+ */
+public class ArrayHandler implements ResultSetHandler<Object[]> {
+
+ /**
+ * Singleton processor instance that handlers share to save memory. Notice
+ * the default scoping to allow only classes in this package to use this
+ * instance.
+ */
+ static final RowProcessor ROW_PROCESSOR = new BasicRowProcessor();
+
+ /**
+ * The RowProcessor implementation to use when converting rows
+ * into arrays.
+ */
+ private final RowProcessor convert;
+
+ /**
+ * Creates a new instance of ArrayHandler using a
+ * <code>BasicRowProcessor</code> for conversion.
+ */
+ public ArrayHandler() {
+ this(ROW_PROCESSOR);
+ }
+
+ /**
+ * Creates a new instance of ArrayHandler.
+ *
+ * @param convert The <code>RowProcessor</code> implementation
+ * to use when converting rows into arrays.
+ */
+ public ArrayHandler(RowProcessor convert) {
+ super();
+ this.convert = convert;
+ }
+
+ /**
+ * Places the column values from the first row in an <code>Object[]</code>.
+ * @param rs <code>ResultSet</code> to process.
+ * @return An Object[] or <code>null</code> if there are no rows in the
+ * <code>ResultSet</code>.
+ *
+ * @throws SQLException if a database access error occurs
+ * @see org.apache.commons.dbutils2.ResultSetHandler#handle(java.sql.ResultSet)
+ */
+ @Override
+ public Object[] handle(ResultSet rs) throws SQLException {
+ return rs.next() ? this.convert.toArray(rs) : null;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/main/java/org/apache/commons/dbutils2/handlers/ArrayListHandler.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/dbutils2/handlers/ArrayListHandler.java b/src/main/java/org/apache/commons/dbutils2/handlers/ArrayListHandler.java
new file mode 100644
index 0000000..6328c58
--- /dev/null
+++ b/src/main/java/org/apache/commons/dbutils2/handlers/ArrayListHandler.java
@@ -0,0 +1,72 @@
+/*
+ * 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.
+ */
+package org.apache.commons.dbutils2.handlers;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import org.apache.commons.dbutils2.RowProcessor;
+
+/**
+ * <code>ResultSetHandler</code> implementation that converts the
+ * <code>ResultSet</code> into a <code>List</code> of <code>Object[]</code>s.
+ * This class is thread safe.
+ *
+ * @see org.apache.commons.dbutils2.ResultSetHandler
+ */
+public class ArrayListHandler extends AbstractListHandler<Object[]> {
+
+ /**
+ * The RowProcessor implementation to use when converting rows
+ * into Object[]s.
+ */
+ private final RowProcessor convert;
+
+ /**
+ * Creates a new instance of ArrayListHandler using a
+ * <code>BasicRowProcessor</code> for conversions.
+ */
+ public ArrayListHandler() {
+ this(ArrayHandler.ROW_PROCESSOR);
+ }
+
+ /**
+ * Creates a new instance of ArrayListHandler.
+ *
+ * @param convert The <code>RowProcessor</code> implementation
+ * to use when converting rows into Object[]s.
+ */
+ public ArrayListHandler(RowProcessor convert) {
+ super();
+ this.convert = convert;
+ }
+
+
+ /**
+ * Convert row's columns into an <code>Object[]</code>.
+ * @param rs <code>ResultSet</code> to process.
+ * @return <code>Object[]</code>, never <code>null</code>.
+ *
+ * @throws SQLException if a database access error occurs
+ * @see org.apache.commons.dbutils2.handlers.AbstractListHandler#handle(ResultSet)
+ */
+ @Override
+ protected Object[] handleRow(ResultSet rs) throws SQLException {
+ return this.convert.toArray(rs);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/main/java/org/apache/commons/dbutils2/handlers/BeanHandler.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/dbutils2/handlers/BeanHandler.java b/src/main/java/org/apache/commons/dbutils2/handlers/BeanHandler.java
new file mode 100644
index 0000000..9146bd0
--- /dev/null
+++ b/src/main/java/org/apache/commons/dbutils2/handlers/BeanHandler.java
@@ -0,0 +1,83 @@
+/*
+ * 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.
+ */
+package org.apache.commons.dbutils2.handlers;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import org.apache.commons.dbutils2.ResultSetHandler;
+import org.apache.commons.dbutils2.RowProcessor;
+
+/**
+ * <code>ResultSetHandler</code> implementation that converts the first
+ * <code>ResultSet</code> row into a JavaBean. This class is thread safe.
+ *
+ * @param <T> the target bean type
+ * @see org.apache.commons.dbutils2.ResultSetHandler
+ */
+public class BeanHandler<T> implements ResultSetHandler<T> {
+
+ /**
+ * The Class of beans produced by this handler.
+ */
+ private final Class<T> type;
+
+ /**
+ * The RowProcessor implementation to use when converting rows
+ * into beans.
+ */
+ private final RowProcessor convert;
+
+ /**
+ * Creates a new instance of BeanHandler.
+ *
+ * @param type The Class that objects returned from <code>handle()</code>
+ * are created from.
+ */
+ public BeanHandler(Class<T> type) {
+ this(type, ArrayHandler.ROW_PROCESSOR);
+ }
+
+ /**
+ * Creates a new instance of BeanHandler.
+ *
+ * @param type The Class that objects returned from <code>handle()</code>
+ * are created from.
+ * @param convert The <code>RowProcessor</code> implementation
+ * to use when converting rows into beans.
+ */
+ public BeanHandler(Class<T> type, RowProcessor convert) {
+ this.type = type;
+ this.convert = convert;
+ }
+
+ /**
+ * Convert the first row of the <code>ResultSet</code> into a bean with the
+ * <code>Class</code> given in the constructor.
+ * @param rs <code>ResultSet</code> to process.
+ * @return An initialized JavaBean or <code>null</code> if there were no
+ * rows in the <code>ResultSet</code>.
+ *
+ * @throws SQLException if a database access error occurs
+ * @see org.apache.commons.dbutils2.ResultSetHandler#handle(java.sql.ResultSet)
+ */
+ @Override
+ public T handle(ResultSet rs) throws SQLException {
+ return rs.next() ? this.convert.toBean(rs, this.type) : null;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/main/java/org/apache/commons/dbutils2/handlers/BeanListHandler.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/dbutils2/handlers/BeanListHandler.java b/src/main/java/org/apache/commons/dbutils2/handlers/BeanListHandler.java
new file mode 100644
index 0000000..2997926
--- /dev/null
+++ b/src/main/java/org/apache/commons/dbutils2/handlers/BeanListHandler.java
@@ -0,0 +1,85 @@
+/*
+ * 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.
+ */
+package org.apache.commons.dbutils2.handlers;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.List;
+
+import org.apache.commons.dbutils2.ResultSetHandler;
+import org.apache.commons.dbutils2.RowProcessor;
+
+/**
+ * <code>ResultSetHandler</code> implementation that converts a
+ * <code>ResultSet</code> into a <code>List</code> of beans. This class is
+ * thread safe.
+ *
+ * @param <T> the target bean type
+ * @see org.apache.commons.dbutils2.ResultSetHandler
+ */
+public class BeanListHandler<T> implements ResultSetHandler<List<T>> {
+
+ /**
+ * The Class of beans produced by this handler.
+ */
+ private final Class<T> type;
+
+ /**
+ * The RowProcessor implementation to use when converting rows
+ * into beans.
+ */
+ private final RowProcessor convert;
+
+ /**
+ * Creates a new instance of BeanListHandler.
+ *
+ * @param type The Class that objects returned from <code>handle()</code>
+ * are created from.
+ */
+ public BeanListHandler(Class<T> type) {
+ this(type, ArrayHandler.ROW_PROCESSOR);
+ }
+
+ /**
+ * Creates a new instance of BeanListHandler.
+ *
+ * @param type The Class that objects returned from <code>handle()</code>
+ * are created from.
+ * @param convert The <code>RowProcessor</code> implementation
+ * to use when converting rows into beans.
+ */
+ public BeanListHandler(Class<T> type, RowProcessor convert) {
+ this.type = type;
+ this.convert = convert;
+ }
+
+ /**
+ * Convert the whole <code>ResultSet</code> into a List of beans with
+ * the <code>Class</code> given in the constructor.
+ *
+ * @param rs The <code>ResultSet</code> to handle.
+ *
+ * @return A List of beans, never <code>null</code>.
+ *
+ * @throws SQLException if a database access error occurs
+ * @see org.apache.commons.dbutils2.RowProcessor#toBeanList(ResultSet, Class)
+ */
+ @Override
+ public List<T> handle(ResultSet rs) throws SQLException {
+ return this.convert.toBeanList(rs, type);
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/main/java/org/apache/commons/dbutils2/handlers/BeanMapHandler.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/dbutils2/handlers/BeanMapHandler.java b/src/main/java/org/apache/commons/dbutils2/handlers/BeanMapHandler.java
new file mode 100644
index 0000000..6f37fca
--- /dev/null
+++ b/src/main/java/org/apache/commons/dbutils2/handlers/BeanMapHandler.java
@@ -0,0 +1,185 @@
+/*
+ * 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.
+ */
+package org.apache.commons.dbutils2.handlers;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import org.apache.commons.dbutils2.RowProcessor;
+
+/**
+ * <p>
+ * <code>ResultSetHandler</code> implementation that returns a Map of Beans.
+ * <code>ResultSet</code> rows are converted into Beans which are then stored in
+ * a Map under the given key.
+ * </p>
+ * <p>
+ * If you had a Person table with a primary key column called ID, you could
+ * retrieve rows from the table like this:
+ *
+ * <pre>
+ * ResultSetHandler<Map<Long, Person>> h = new BeanMapdHandler<Long, Person>(Person.class, "id");
+ * Map&ltLong, Person> found = queryRunner.query("select id, name, age from person", h);
+ * Person jane = found.get(1L); // jane's id is 1
+ * String janesName = jane.getName();
+ * Integer janesAge = jane.getAge();
+ * </pre>
+ *
+ * Note that the "id" passed to BeanMapHandler can be in any case. The data type
+ * returned for id is dependent upon how your JDBC driver converts SQL column
+ * types from the Person table into Java types. The "name" and "age" columns are
+ * converted according to their property descriptors by DbUtils.
+ * </p>
+ * <p>
+ * This class is thread safe.
+ * </p>
+ *
+ * @param <K>
+ * the type of keys maintained by the returned map
+ * @param <V>
+ * the type of the bean
+ * @see org.apache.commons.dbutils2.ResultSetHandler
+ * @since DbUtils 1.5
+ */
+public class BeanMapHandler<K, V> extends AbstractKeyedHandler<K, V> {
+
+ /**
+ * The Class of beans produced by this handler.
+ */
+ private final Class<V> type;
+
+ /**
+ * The RowProcessor implementation to use when converting rows into Objects.
+ */
+ private final RowProcessor convert;
+
+ /**
+ * The column index to retrieve key values from. Defaults to 1.
+ */
+ private final int columnIndex;
+
+ /**
+ * The column name to retrieve key values from. Either columnName or
+ * columnIndex will be used but never both.
+ */
+ private final String columnName;
+
+ /**
+ * Creates a new instance of BeanMapHandler. The value of the first column
+ * of each row will be a key in the Map.
+ *
+ * @param type
+ * The Class that objects returned from <code>createRow()</code>
+ * are created from.
+ */
+ public BeanMapHandler(Class<V> type) {
+ this(type, ArrayHandler.ROW_PROCESSOR, 1, null);
+ }
+
+ /**
+ * Creates a new instance of BeanMapHandler. The value of the first column
+ * of each row will be a key in the Map.
+ *
+ * @param type
+ * The Class that objects returned from <code>createRow()</code>
+ * are created from.
+ * @param convert
+ * The <code>RowProcessor</code> implementation to use when
+ * converting rows into Beans
+ */
+ public BeanMapHandler(Class<V> type, RowProcessor convert) {
+ this(type, convert, 1, null);
+ }
+
+ /**
+ * Creates a new instance of BeanMapHandler.
+ *
+ * @param type
+ * The Class that objects returned from <code>createRow()</code>
+ * are created from.
+ * @param columnIndex
+ * The values to use as keys in the Map are retrieved from the
+ * column at this index.
+ */
+ public BeanMapHandler(Class<V> type, int columnIndex) {
+ this(type, ArrayHandler.ROW_PROCESSOR, columnIndex, null);
+ }
+
+ /**
+ * Creates a new instance of BeanMapHandler.
+ *
+ * @param type
+ * The Class that objects returned from <code>createRow()</code>
+ * are created from.
+ * @param columnName
+ * The values to use as keys in the Map are retrieved from the
+ * column with this name.
+ */
+ public BeanMapHandler(Class<V> type, String columnName) {
+ this(type, ArrayHandler.ROW_PROCESSOR, 1, columnName);
+ }
+
+ /**
+ * Private Helper
+ *
+ * @param convert
+ * The <code>RowProcessor</code> implementation to use when
+ * converting rows into Beans
+ * @param columnIndex
+ * The values to use as keys in the Map are retrieved from the
+ * column at this index.
+ * @param columnName
+ * The values to use as keys in the Map are retrieved from the
+ * column with this name.
+ */
+ private BeanMapHandler(Class<V> type, RowProcessor convert,
+ int columnIndex, String columnName) {
+ super();
+ this.type = type;
+ this.convert = convert;
+ this.columnIndex = columnIndex;
+ this.columnName = columnName;
+ }
+
+ /**
+ * This factory method is called by <code>handle()</code> to retrieve the
+ * key value from the current <code>ResultSet</code> row.
+ * @param rs ResultSet to create a key from
+ *
+ * @return K from the configured key column name/index
+ *
+ * @throws SQLException if a database access error occurs
+ * @throws ClassCastException if the class datatype does not match the column type
+ *
+ * @see org.apache.commons.dbutils2.handlers.AbstractKeyedHandler#createKey(ResultSet)
+ */
+ // We assume that the user has picked the correct type to match the column
+ // so getObject will return the appropriate type and the cast will succeed.
+ @SuppressWarnings("unchecked")
+ @Override
+ protected K createKey(ResultSet rs) throws SQLException {
+ return (columnName == null) ?
+ (K) rs.getObject(columnIndex) :
+ (K) rs.getObject(columnName);
+ }
+
+ @Override
+ protected V createRow(ResultSet rs) throws SQLException {
+ return this.convert.toBean(rs, type);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/main/java/org/apache/commons/dbutils2/handlers/ColumnListHandler.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/dbutils2/handlers/ColumnListHandler.java b/src/main/java/org/apache/commons/dbutils2/handlers/ColumnListHandler.java
new file mode 100644
index 0000000..a432682
--- /dev/null
+++ b/src/main/java/org/apache/commons/dbutils2/handlers/ColumnListHandler.java
@@ -0,0 +1,105 @@
+/*
+ * 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.
+ */
+package org.apache.commons.dbutils2.handlers;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+/**
+ * <code>ResultSetHandler</code> implementation that converts one
+ * <code>ResultSet</code> column into a <code>List</code> of
+ * <code>Object</code>s. This class is thread safe.
+ *
+ * @param <T> The type of the column.
+ * @see org.apache.commons.dbutils2.ResultSetHandler
+ * @since DbUtils 1.1
+ */
+public class ColumnListHandler<T> extends AbstractListHandler<T> {
+
+ /**
+ * The column number to retrieve.
+ */
+ private final int columnIndex;
+
+ /**
+ * The column name to retrieve. Either columnName or columnIndex
+ * will be used but never both.
+ */
+ private final String columnName;
+
+ /**
+ * Creates a new instance of ColumnListHandler. The first column of each
+ * row will be returned from <code>handle()</code>.
+ */
+ public ColumnListHandler() {
+ this(1, null);
+ }
+
+ /**
+ * Creates a new instance of ColumnListHandler.
+ *
+ * @param columnIndex The index of the column to retrieve from the
+ * <code>ResultSet</code>.
+ */
+ public ColumnListHandler(int columnIndex) {
+ this(columnIndex, null);
+ }
+
+ /**
+ * Creates a new instance of ColumnListHandler.
+ *
+ * @param columnName The name of the column to retrieve from the
+ * <code>ResultSet</code>.
+ */
+ public ColumnListHandler(String columnName) {
+ this(1, columnName);
+ }
+
+ /** Private Helper
+ * @param columnIndex The index of the column to retrieve from the
+ * <code>ResultSet</code>.
+ * @param columnName The name of the column to retrieve from the
+ * <code>ResultSet</code>.
+ */
+ private ColumnListHandler(int columnIndex, String columnName) {
+ super();
+ this.columnIndex = columnIndex;
+ this.columnName = columnName;
+ }
+
+ /**
+ * Returns one <code>ResultSet</code> column value as <code>Object</code>.
+ * @param rs <code>ResultSet</code> to process.
+ * @return <code>Object</code>, never <code>null</code>.
+ *
+ * @throws SQLException if a database access error occurs
+ * @throws ClassCastException if the class datatype does not match the column type
+ *
+ * @see org.apache.commons.dbutils2.handlers.AbstractListHandler#handle(ResultSet)
+ */
+ // We assume that the user has picked the correct type to match the column
+ // so getObject will return the appropriate type and the cast will succeed.
+ @SuppressWarnings("unchecked")
+ @Override
+ protected T handleRow(ResultSet rs) throws SQLException {
+ if (this.columnName == null) {
+ return (T) rs.getObject(this.columnIndex);
+ }
+ return (T) rs.getObject(this.columnName);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/main/java/org/apache/commons/dbutils2/handlers/KeyedHandler.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/dbutils2/handlers/KeyedHandler.java b/src/main/java/org/apache/commons/dbutils2/handlers/KeyedHandler.java
new file mode 100644
index 0000000..f7fe89a
--- /dev/null
+++ b/src/main/java/org/apache/commons/dbutils2/handlers/KeyedHandler.java
@@ -0,0 +1,161 @@
+/*
+ * 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.
+ */
+package org.apache.commons.dbutils2.handlers;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Map;
+
+import org.apache.commons.dbutils2.RowProcessor;
+
+/**
+ * <p>
+ * <code>ResultSetHandler</code> implementation that returns a Map of Maps.
+ * <code>ResultSet</code> rows are converted into Maps which are then stored
+ * in a Map under the given key.
+ * </p>
+ * <p>
+ * If you had a Person table with a primary key column called ID, you could
+ * retrieve rows from the table like this:
+ * <pre>
+ * ResultSetHandler h = new KeyedHandler("id");
+ * Map found = (Map) queryRunner.query("select id, name, age from person", h);
+ * Map jane = (Map) found.get(new Long(1)); // jane's id is 1
+ * String janesName = (String) jane.get("name");
+ * Integer janesAge = (Integer) jane.get("age");
+ * </pre>
+ * Note that the "id" passed to KeyedHandler and "name" and "age" passed to the
+ * returned Map's get() method can be in any case. The data types returned for
+ * name and age are dependent upon how your JDBC driver converts SQL column
+ * types from the Person table into Java types.
+ * </p>
+ * <p>This class is thread safe.</p>
+ *
+ * @param <K> The type of the key
+ * @see org.apache.commons.dbutils2.ResultSetHandler
+ * @since DbUtils 1.1
+ */
+public class KeyedHandler<K> extends AbstractKeyedHandler<K, Map<String, Object>> {
+
+ /**
+ * The RowProcessor implementation to use when converting rows
+ * into Objects.
+ */
+ protected final RowProcessor convert;
+
+ /**
+ * The column index to retrieve key values from. Defaults to 1.
+ */
+ protected final int columnIndex;
+
+ /**
+ * The column name to retrieve key values from. Either columnName or
+ * columnIndex will be used but never both.
+ */
+ protected final String columnName;
+
+ /**
+ * Creates a new instance of KeyedHandler. The value of the first column
+ * of each row will be a key in the Map.
+ */
+ public KeyedHandler() {
+ this(ArrayHandler.ROW_PROCESSOR, 1, null);
+ }
+
+ /**
+ * Creates a new instance of KeyedHandler. The value of the first column
+ * of each row will be a key in the Map.
+ *
+ * @param convert The <code>RowProcessor</code> implementation
+ * to use when converting rows into Maps
+ */
+ public KeyedHandler(RowProcessor convert) {
+ this(convert, 1, null);
+ }
+
+ /**
+ * Creates a new instance of KeyedHandler.
+ *
+ * @param columnIndex The values to use as keys in the Map are
+ * retrieved from the column at this index.
+ */
+ public KeyedHandler(int columnIndex) {
+ this(ArrayHandler.ROW_PROCESSOR, columnIndex, null);
+ }
+
+ /**
+ * Creates a new instance of KeyedHandler.
+ *
+ * @param columnName The values to use as keys in the Map are
+ * retrieved from the column with this name.
+ */
+ public KeyedHandler(String columnName) {
+ this(ArrayHandler.ROW_PROCESSOR, 1, columnName);
+ }
+
+ /** Private Helper
+ * @param convert The <code>RowProcessor</code> implementation
+ * to use when converting rows into Maps
+ * @param columnIndex The values to use as keys in the Map are
+ * retrieved from the column at this index.
+ * @param columnName The values to use as keys in the Map are
+ * retrieved from the column with this name.
+ */
+ private KeyedHandler(RowProcessor convert, int columnIndex,
+ String columnName) {
+ super();
+ this.convert = convert;
+ this.columnIndex = columnIndex;
+ this.columnName = columnName;
+ }
+ /**
+ * This factory method is called by <code>handle()</code> to retrieve the
+ * key value from the current <code>ResultSet</code> row. This
+ * implementation returns <code>ResultSet.getObject()</code> for the
+ * configured key column name or index.
+ * @param rs ResultSet to create a key from
+ * @return Object from the configured key column name/index
+ *
+ * @throws SQLException if a database access error occurs
+ * @throws ClassCastException if the class datatype does not match the column type
+ */
+ // We assume that the user has picked the correct type to match the column
+ // so getObject will return the appropriate type and the cast will succeed.
+ @SuppressWarnings("unchecked")
+ @Override
+ protected K createKey(ResultSet rs) throws SQLException {
+ return (columnName == null) ?
+ (K) rs.getObject(columnIndex) :
+ (K) rs.getObject(columnName);
+ }
+
+ /**
+ * This factory method is called by <code>handle()</code> to store the
+ * current <code>ResultSet</code> row in some object. This
+ * implementation returns a <code>Map</code> with case insensitive column
+ * names as keys. Calls to <code>map.get("COL")</code> and
+ * <code>map.get("col")</code> return the same value.
+ * @param rs ResultSet to create a row from
+ * @return Object typed Map containing column names to values
+ * @throws SQLException if a database access error occurs
+ */
+ @Override
+ protected Map<String, Object> createRow(ResultSet rs) throws SQLException {
+ return this.convert.toMap(rs);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/main/java/org/apache/commons/dbutils2/handlers/MapHandler.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/dbutils2/handlers/MapHandler.java b/src/main/java/org/apache/commons/dbutils2/handlers/MapHandler.java
new file mode 100644
index 0000000..c767948
--- /dev/null
+++ b/src/main/java/org/apache/commons/dbutils2/handlers/MapHandler.java
@@ -0,0 +1,76 @@
+/*
+ * 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.
+ */
+package org.apache.commons.dbutils2.handlers;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Map;
+
+import org.apache.commons.dbutils2.ResultSetHandler;
+import org.apache.commons.dbutils2.RowProcessor;
+
+/**
+ * <code>ResultSetHandler</code> implementation that converts the first
+ * <code>ResultSet</code> row into a <code>Map</code>. This class is thread
+ * safe.
+ *
+ * @see org.apache.commons.dbutils2.ResultSetHandler
+ */
+public class MapHandler implements ResultSetHandler<Map<String, Object>> {
+
+ /**
+ * The RowProcessor implementation to use when converting rows
+ * into Maps.
+ */
+ private final RowProcessor convert;
+
+ /**
+ * Creates a new instance of MapHandler using a
+ * <code>BasicRowProcessor</code> for conversion.
+ */
+ public MapHandler() {
+ this(ArrayHandler.ROW_PROCESSOR);
+ }
+
+ /**
+ * Creates a new instance of MapHandler.
+ *
+ * @param convert The <code>RowProcessor</code> implementation
+ * to use when converting rows into Maps.
+ */
+ public MapHandler(RowProcessor convert) {
+ super();
+ this.convert = convert;
+ }
+
+ /**
+ * Converts the first row in the <code>ResultSet</code> into a
+ * <code>Map</code>.
+ * @param rs <code>ResultSet</code> to process.
+ * @return A <code>Map</code> with the values from the first row or
+ * <code>null</code> if there are no rows in the <code>ResultSet</code>.
+ *
+ * @throws SQLException if a database access error occurs
+ *
+ * @see org.apache.commons.dbutils2.ResultSetHandler#handle(java.sql.ResultSet)
+ */
+ @Override
+ public Map<String, Object> handle(ResultSet rs) throws SQLException {
+ return rs.next() ? this.convert.toMap(rs) : null;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/main/java/org/apache/commons/dbutils2/handlers/MapListHandler.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/dbutils2/handlers/MapListHandler.java b/src/main/java/org/apache/commons/dbutils2/handlers/MapListHandler.java
new file mode 100644
index 0000000..f557f59
--- /dev/null
+++ b/src/main/java/org/apache/commons/dbutils2/handlers/MapListHandler.java
@@ -0,0 +1,73 @@
+/*
+ * 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.
+ */
+package org.apache.commons.dbutils2.handlers;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Map;
+
+import org.apache.commons.dbutils2.RowProcessor;
+
+/**
+ * <code>ResultSetHandler</code> implementation that converts a
+ * <code>ResultSet</code> into a <code>List</code> of <code>Map</code>s.
+ * This class is thread safe.
+ *
+ * @see org.apache.commons.dbutils2.ResultSetHandler
+ */
+public class MapListHandler extends AbstractListHandler<Map<String, Object>> {
+
+ /**
+ * The RowProcessor implementation to use when converting rows
+ * into Maps.
+ */
+ private final RowProcessor convert;
+
+ /**
+ * Creates a new instance of MapListHandler using a
+ * <code>BasicRowProcessor</code> for conversion.
+ */
+ public MapListHandler() {
+ this(ArrayHandler.ROW_PROCESSOR);
+ }
+
+ /**
+ * Creates a new instance of MapListHandler.
+ *
+ * @param convert The <code>RowProcessor</code> implementation
+ * to use when converting rows into Maps.
+ */
+ public MapListHandler(RowProcessor convert) {
+ super();
+ this.convert = convert;
+ }
+
+ /**
+ * Converts the <code>ResultSet</code> row into a <code>Map</code> object.
+ * @param rs <code>ResultSet</code> to process.
+ * @return A <code>Map</code>, never null.
+ *
+ * @throws SQLException if a database access error occurs
+ *
+ * @see org.apache.commons.dbutils2.handlers.AbstractListHandler#handle(ResultSet)
+ */
+ @Override
+ protected Map<String, Object> handleRow(ResultSet rs) throws SQLException {
+ return this.convert.toMap(rs);
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/main/java/org/apache/commons/dbutils2/handlers/ScalarHandler.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/dbutils2/handlers/ScalarHandler.java b/src/main/java/org/apache/commons/dbutils2/handlers/ScalarHandler.java
new file mode 100644
index 0000000..902c380
--- /dev/null
+++ b/src/main/java/org/apache/commons/dbutils2/handlers/ScalarHandler.java
@@ -0,0 +1,110 @@
+/*
+ * 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.
+ */
+package org.apache.commons.dbutils2.handlers;
+
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import org.apache.commons.dbutils2.ResultSetHandler;
+
+/**
+ * <code>ResultSetHandler</code> implementation that converts one
+ * <code>ResultSet</code> column into an Object. This class is thread safe.
+ *
+ * @param <T> The type of the scalar
+ * @see org.apache.commons.dbutils2.ResultSetHandler
+ */
+public class ScalarHandler<T> implements ResultSetHandler<T> {
+
+ /**
+ * The column number to retrieve.
+ */
+ private final int columnIndex;
+
+ /**
+ * The column name to retrieve. Either columnName or columnIndex
+ * will be used but never both.
+ */
+ private final String columnName;
+
+ /**
+ * Creates a new instance of ScalarHandler. The first column will
+ * be returned from <code>handle()</code>.
+ */
+ public ScalarHandler() {
+ this(1, null);
+ }
+
+ /**
+ * Creates a new instance of ScalarHandler.
+ *
+ * @param columnIndex The index of the column to retrieve from the
+ * <code>ResultSet</code>.
+ */
+ public ScalarHandler(int columnIndex) {
+ this(columnIndex, null);
+ }
+
+ /**
+ * Creates a new instance of ScalarHandler.
+ *
+ * @param columnName The name of the column to retrieve from the
+ * <code>ResultSet</code>.
+ */
+ public ScalarHandler(String columnName) {
+ this(1, columnName);
+ }
+
+ /** Helper constructor
+ * @param columnIndex The index of the column to retrieve from the
+ * <code>ResultSet</code>.
+ * @param columnName The name of the column to retrieve from the
+ * <code>ResultSet</code>.
+ */
+ private ScalarHandler(int columnIndex, String columnName) {
+ this.columnIndex = columnIndex;
+ this.columnName = columnName;
+ }
+
+ /**
+ * Returns one <code>ResultSet</code> column as an object via the
+ * <code>ResultSet.getObject()</code> method that performs type
+ * conversions.
+ * @param rs <code>ResultSet</code> to process.
+ * @return The column or <code>null</code> if there are no rows in
+ * the <code>ResultSet</code>.
+ *
+ * @throws SQLException if a database access error occurs
+ * @throws ClassCastException if the class datatype does not match the column type
+ *
+ * @see org.apache.commons.dbutils2.ResultSetHandler#handle(java.sql.ResultSet)
+ */
+ // We assume that the user has picked the correct type to match the column
+ // so getObject will return the appropriate type and the cast will succeed.
+ @SuppressWarnings("unchecked")
+ @Override
+ public T handle(ResultSet rs) throws SQLException {
+
+ if (rs.next()) {
+ if (this.columnName == null) {
+ return (T) rs.getObject(this.columnIndex);
+ }
+ return (T) rs.getObject(this.columnName);
+ }
+ return null;
+ }
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/main/java/org/apache/commons/dbutils2/handlers/package-info.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/dbutils2/handlers/package-info.java b/src/main/java/org/apache/commons/dbutils2/handlers/package-info.java
new file mode 100644
index 0000000..6cc9e99
--- /dev/null
+++ b/src/main/java/org/apache/commons/dbutils2/handlers/package-info.java
@@ -0,0 +1,21 @@
+/*
+ * 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.
+ */
+
+/**
+ * Implementations of the org.apache.commons.dbutils.ResultSetHandler interface.
+ */
+package org.apache.commons.dbutils2.handlers;
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/main/java/org/apache/commons/dbutils2/package-info.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/dbutils2/package-info.java b/src/main/java/org/apache/commons/dbutils2/package-info.java
new file mode 100644
index 0000000..e3a98fb
--- /dev/null
+++ b/src/main/java/org/apache/commons/dbutils2/package-info.java
@@ -0,0 +1,26 @@
+/*
+ * 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.
+ */
+
+/**
+ * DbUtils is a small set of classes designed to make working with JDBC easier. JDBC resource cleanup code is mundane,
+ * error prone work so these classes abstract out all of the cleanup tasks from your code leaving you with what you
+ * really wanted to do with JDBC in the first place: query and update data.
+ *
+ * This package contains the core classes and interfaces - DbUtils, QueryRunner and the ResultSetHandler interface
+ * should be your first items of interest.
+ */
+package org.apache.commons.dbutils2;
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/main/java/org/apache/commons/dbutils2/wrappers/SqlNullCheckedResultSet.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/dbutils2/wrappers/SqlNullCheckedResultSet.java b/src/main/java/org/apache/commons/dbutils2/wrappers/SqlNullCheckedResultSet.java
new file mode 100644
index 0000000..7868e66
--- /dev/null
+++ b/src/main/java/org/apache/commons/dbutils2/wrappers/SqlNullCheckedResultSet.java
@@ -0,0 +1,608 @@
+/*
+ * 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.
+ */
+package org.apache.commons.dbutils2.wrappers;
+
+import java.io.InputStream;
+import java.io.Reader;
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.math.BigDecimal;
+import java.net.URL;
+import java.sql.Blob;
+import java.sql.Clob;
+import java.sql.Date;
+import java.sql.Ref;
+import java.sql.ResultSet;
+import java.sql.Time;
+import java.sql.Timestamp;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.commons.dbutils2.ProxyFactory;
+
+/**
+ * Decorates a <code>ResultSet</code> with checks for a SQL NULL value on each
+ * <code>getXXX</code> method. If a column value obtained by a
+ * <code>getXXX</code> method is not SQL NULL, the column value is returned. If
+ * the column value is SQL null, an alternate value is returned. The alternate
+ * value defaults to the Java <code>null</code> value, which can be overridden
+ * for instances of the class.
+ *
+ * <p>
+ * Usage example:
+ * <blockquote>
+ * <pre>
+ * Connection conn = // somehow get a connection
+ * Statement stmt = conn.createStatement();
+ * ResultSet rs = stmt.executeQuery("SELECT col1, col2 FROM table1");
+ *
+ * // Wrap the result set for SQL NULL checking
+ * SqlNullCheckedResultSet wrapper = new SqlNullCheckedResultSet(rs);
+ * wrapper.setNullString("---N/A---"); // Set null string
+ * wrapper.setNullInt(-999); // Set null integer
+ * rs = ProxyFactory.instance().createResultSet(wrapper);
+ *
+ * while (rs.next()) {
+ * // If col1 is SQL NULL, value returned will be "---N/A---"
+ * String col1 = rs.getString("col1");
+ * // If col2 is SQL NULL, value returned will be -999
+ * int col2 = rs.getInt("col2");
+ * }
+ * rs.close();
+ * </pre>
+ * </blockquote>
+ * </p>
+ * <p>Unlike some other classes in DbUtils, this class is NOT thread-safe.</p>
+ */
+public class SqlNullCheckedResultSet implements InvocationHandler {
+
+ /**
+ * Maps normal method names (ie. "getBigDecimal") to the corresponding null
+ * Method object (ie. getNullBigDecimal).
+ */
+ private static final Map<String, Method> nullMethods = new HashMap<String, Method>();
+
+ /**
+ * The {@code getNull} string prefix.
+ * @since 1.4
+ */
+ private static final String GET_NULL_PREFIX = "getNull";
+
+ static {
+ Method[] methods = SqlNullCheckedResultSet.class.getMethods();
+ for (int i = 0; i < methods.length; i++) {
+ String methodName = methods[i].getName();
+
+ if (methodName.startsWith(GET_NULL_PREFIX)) {
+ String normalName = "get" + methodName.substring(GET_NULL_PREFIX.length());
+ nullMethods.put(normalName, methods[i]);
+ }
+ }
+ }
+
+ /**
+ * The factory to create proxies with.
+ */
+ private static final ProxyFactory factory = ProxyFactory.instance();
+
+ /**
+ * Wraps the <code>ResultSet</code> in an instance of this class. This is
+ * equivalent to:
+ * <pre>
+ * ProxyFactory.instance().createResultSet(new SqlNullCheckedResultSet(rs));
+ * </pre>
+ *
+ * @param rs The <code>ResultSet</code> to wrap.
+ * @return wrapped ResultSet
+ */
+ public static ResultSet wrap(ResultSet rs) {
+ return factory.createResultSet(new SqlNullCheckedResultSet(rs));
+ }
+
+ private InputStream nullAsciiStream = null;
+ private BigDecimal nullBigDecimal = null;
+ private InputStream nullBinaryStream = null;
+ private Blob nullBlob = null;
+ private boolean nullBoolean = false;
+ private byte nullByte = 0;
+ private byte[] nullBytes = null;
+ private Reader nullCharacterStream = null;
+ private Clob nullClob = null;
+ private Date nullDate = null;
+ private double nullDouble = 0.0;
+ private float nullFloat = 0.0f;
+ private int nullInt = 0;
+ private long nullLong = 0;
+ private Object nullObject = null;
+ private Ref nullRef = null;
+ private short nullShort = 0;
+ private String nullString = null;
+ private Time nullTime = null;
+ private Timestamp nullTimestamp = null;
+ private URL nullURL = null;
+
+ /**
+ * The wrapped result.
+ */
+ private final ResultSet rs;
+
+ /**
+ * Constructs a new instance of
+ * <code>SqlNullCheckedResultSet</code>
+ * to wrap the specified <code>ResultSet</code>.
+ * @param rs ResultSet to wrap
+ */
+ public SqlNullCheckedResultSet(ResultSet rs) {
+ super();
+ this.rs = rs;
+ }
+
+ /**
+ * Returns the value when a SQL null is encountered as the result of
+ * invoking a <code>getAsciiStream</code> method.
+ *
+ * @return the value
+ */
+ public InputStream getNullAsciiStream() {
+ return this.nullAsciiStream;
+ }
+
+ /**
+ * Returns the value when a SQL null is encountered as the result of
+ * invoking a <code>getBigDecimal</code> method.
+ *
+ * @return the value
+ */
+ public BigDecimal getNullBigDecimal() {
+ return this.nullBigDecimal;
+ }
+
+ /**
+ * Returns the value when a SQL null is encountered as the result of
+ * invoking a <code>getBinaryStream</code> method.
+ *
+ * @return the value
+ */
+ public InputStream getNullBinaryStream() {
+ return this.nullBinaryStream;
+ }
+
+ /**
+ * Returns the value when a SQL null is encountered as the result of
+ * invoking a <code>getBlob</code> method.
+ *
+ * @return the value
+ */
+ public Blob getNullBlob() {
+ return this.nullBlob;
+ }
+
+ /**
+ * Returns the value when a SQL null is encountered as the result of
+ * invoking a <code>getBoolean</code> method.
+ *
+ * @return the value
+ */
+ public boolean getNullBoolean() {
+ return this.nullBoolean;
+ }
+
+ /**
+ * Returns the value when a SQL null is encountered as the result of
+ * invoking a <code>getByte</code> method.
+ *
+ * @return the value
+ */
+ public byte getNullByte() {
+ return this.nullByte;
+ }
+
+ /**
+ * Returns the value when a SQL null is encountered as the result of
+ * invoking a <code>getBytes</code> method.
+ *
+ * @return the value
+ */
+ public byte[] getNullBytes() {
+ if (this.nullBytes == null) {
+ return null;
+ }
+ byte[] copy = new byte[this.nullBytes.length];
+ System.arraycopy(this.nullBytes, 0, copy, 0, this.nullBytes.length);
+ return copy;
+ }
+
+ /**
+ * Returns the value when a SQL null is encountered as the result of
+ * invoking a <code>getCharacterStream</code> method.
+ *
+ * @return the value
+ */
+ public Reader getNullCharacterStream() {
+ return this.nullCharacterStream;
+ }
+
+ /**
+ * Returns the value when a SQL null is encountered as the result of
+ * invoking a <code>getClob</code> method.
+ *
+ * @return the value
+ */
+ public Clob getNullClob() {
+ return this.nullClob;
+ }
+
+ /**
+ * Returns the value when a SQL null is encountered as the result of
+ * invoking a <code>getDate</code> method.
+ *
+ * @return the value
+ */
+ public Date getNullDate() {
+ return this.nullDate;
+ }
+
+ /**
+ * Returns the value when a SQL null is encountered as the result of
+ * invoking a <code>getDouble</code> method.
+ *
+ * @return the value
+ */
+ public double getNullDouble() {
+ return this.nullDouble;
+ }
+
+ /**
+ * Returns the value when a SQL null is encountered as the result of
+ * invoking a <code>getFloat</code> method.
+ *
+ * @return the value
+ */
+ public float getNullFloat() {
+ return this.nullFloat;
+ }
+
+ /**
+ * Returns the value when a SQL null is encountered as the result of
+ * invoking a <code>getInt</code> method.
+ *
+ * @return the value
+ */
+ public int getNullInt() {
+ return this.nullInt;
+ }
+
+ /**
+ * Returns the value when a SQL null is encountered as the result of
+ * invoking a <code>getLong</code> method.
+ *
+ * @return the value
+ */
+ public long getNullLong() {
+ return this.nullLong;
+ }
+
+ /**
+ * Returns the value when a SQL null is encountered as the result of
+ * invoking a <code>getObject</code> method.
+ *
+ * @return the value
+ */
+ public Object getNullObject() {
+ return this.nullObject;
+ }
+
+ /**
+ * Returns the value when a SQL null is encountered as the result of
+ * invoking a <code>getRef</code> method.
+ *
+ * @return the value
+ */
+ public Ref getNullRef() {
+ return this.nullRef;
+ }
+
+ /**
+ * Returns the value when a SQL null is encountered as the result of
+ * invoking a <code>getShort</code> method.
+ *
+ * @return the value
+ */
+ public short getNullShort() {
+ return this.nullShort;
+ }
+
+ /**
+ * Returns the value when a SQL null is encountered as the result of
+ * invoking a <code>getString</code> method.
+ *
+ * @return the value
+ */
+ public String getNullString() {
+ return this.nullString;
+ }
+
+ /**
+ * Returns the value when a SQL null is encountered as the result of
+ * invoking a <code>getTime</code> method.
+ *
+ * @return the value
+ */
+ public Time getNullTime() {
+ return this.nullTime;
+ }
+
+ /**
+ * Returns the value when a SQL null is encountered as the result of
+ * invoking a <code>getTimestamp</code> method.
+ *
+ * @return the value
+ */
+ public Timestamp getNullTimestamp() {
+ return this.nullTimestamp;
+ }
+
+ /**
+ * Returns the value when a SQL null is encountered as the result of
+ * invoking a <code>getURL</code> method.
+ *
+ * @return the value
+ */
+ public URL getNullURL() {
+ return this.nullURL;
+ }
+
+ /**
+ * Intercepts calls to <code>get*</code> methods and calls the appropriate
+ * <code>getNull*</code> method if the <code>ResultSet</code> returned
+ * <code>null</code>.
+ *
+ * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
+ * @param proxy Not used; all method calls go to the internal result set
+ * @param method The method to invoke on the result set
+ * @param args The arguments to pass to the result set
+ * @return null checked result
+ * @throws Throwable error
+ */
+ @Override
+ public Object invoke(Object proxy, Method method, Object[] args)
+ throws Throwable {
+
+ Object result = method.invoke(this.rs, args);
+
+ Method nullMethod = nullMethods.get(method.getName());
+
+ // Check nullMethod != null first so that we don't call wasNull()
+ // before a true getter method was invoked on the ResultSet.
+ return (nullMethod != null && this.rs.wasNull())
+ ? nullMethod.invoke(this, (Object[]) null)
+ : result;
+ }
+
+ /**
+ * Sets the value to return when a SQL null is encountered as the result of
+ * invoking a <code>getAsciiStream</code> method.
+ *
+ * @param nullAsciiStream the value
+ */
+ public void setNullAsciiStream(InputStream nullAsciiStream) {
+ this.nullAsciiStream = nullAsciiStream;
+ }
+
+ /**
+ * Sets the value to return when a SQL null is encountered as the result of
+ * invoking a <code>getBigDecimal</code> method.
+ *
+ * @param nullBigDecimal the value
+ */
+ public void setNullBigDecimal(BigDecimal nullBigDecimal) {
+ this.nullBigDecimal = nullBigDecimal;
+ }
+
+ /**
+ * Sets the value to return when a SQL null is encountered as the result of
+ * invoking a <code>getBinaryStream</code> method.
+ *
+ * @param nullBinaryStream the value
+ */
+ public void setNullBinaryStream(InputStream nullBinaryStream) {
+ this.nullBinaryStream = nullBinaryStream;
+ }
+
+ /**
+ * Sets the value to return when a SQL null is encountered as the result of
+ * invoking a <code>getBlob</code> method.
+ *
+ * @param nullBlob the value
+ */
+ public void setNullBlob(Blob nullBlob) {
+ this.nullBlob = nullBlob;
+ }
+
+ /**
+ * Sets the value to return when a SQL null is encountered as the result of
+ * invoking a <code>getBoolean</code> method.
+ *
+ * @param nullBoolean the value
+ */
+ public void setNullBoolean(boolean nullBoolean) {
+ this.nullBoolean = nullBoolean;
+ }
+
+ /**
+ * Sets the value to return when a SQL null is encountered as the result of
+ * invoking a <code>getByte</code> method.
+ *
+ * @param nullByte the value
+ */
+ public void setNullByte(byte nullByte) {
+ this.nullByte = nullByte;
+ }
+
+ /**
+ * Sets the value to return when a SQL null is encountered as the result of
+ * invoking a <code>getBytes</code> method.
+ *
+ * @param nullBytes the value
+ */
+ public void setNullBytes(byte[] nullBytes) {
+ byte[] copy = new byte[nullBytes.length];
+ System.arraycopy(nullBytes, 0, copy, 0, nullBytes.length);
+ this.nullBytes = copy;
+ }
+
+ /**
+ * Sets the value to return when a SQL null is encountered as the result of
+ * invoking a <code>getCharacterStream</code> method.
+ *
+ * @param nullCharacterStream the value
+ */
+ public void setNullCharacterStream(Reader nullCharacterStream) {
+ this.nullCharacterStream = nullCharacterStream;
+ }
+
+ /**
+ * Sets the value to return when a SQL null is encountered as the result of
+ * invoking a <code>getClob</code> method.
+ *
+ * @param nullClob the value
+ */
+ public void setNullClob(Clob nullClob) {
+ this.nullClob = nullClob;
+ }
+
+ /**
+ * Sets the value to return when a SQL null is encountered as the result of
+ * invoking a <code>getDate</code> method.
+ *
+ * @param nullDate the value
+ */
+ public void setNullDate(Date nullDate) {
+ this.nullDate = nullDate;
+ }
+
+ /**
+ * Sets the value to return when a SQL null is encountered as the result of
+ * invoking a <code>getDouble</code> method.
+ *
+ * @param nullDouble the value
+ */
+ public void setNullDouble(double nullDouble) {
+ this.nullDouble = nullDouble;
+ }
+
+ /**
+ * Sets the value to return when a SQL null is encountered as the result of
+ * invoking a <code>getFloat</code> method.
+ *
+ * @param nullFloat the value
+ */
+ public void setNullFloat(float nullFloat) {
+ this.nullFloat = nullFloat;
+ }
+
+ /**
+ * Sets the value to return when a SQL null is encountered as the result of
+ * invoking a <code>getInt</code> method.
+ *
+ * @param nullInt the value
+ */
+ public void setNullInt(int nullInt) {
+ this.nullInt = nullInt;
+ }
+
+ /**
+ * Sets the value to return when a SQL null is encountered as the result of
+ * invoking a <code>getLong</code> method.
+ *
+ * @param nullLong the value
+ */
+ public void setNullLong(long nullLong) {
+ this.nullLong = nullLong;
+ }
+
+ /**
+ * Sets the value to return when a SQL null is encountered as the result of
+ * invoking a <code>getObject</code> method.
+ *
+ * @param nullObject the value
+ */
+ public void setNullObject(Object nullObject) {
+ this.nullObject = nullObject;
+ }
+
+ /**
+ * Sets the value to return when a SQL null is encountered as the result of
+ * invoking a <code>getRef</code> method.
+ *
+ * @param nullRef the value
+ */
+ public void setNullRef(Ref nullRef) {
+ this.nullRef = nullRef;
+ }
+
+ /**
+ * Sets the value to return when a SQL null is encountered as the result of
+ * invoking a <code>getShort</code> method.
+ *
+ * @param nullShort the value
+ */
+ public void setNullShort(short nullShort) {
+ this.nullShort = nullShort;
+ }
+
+ /**
+ * Sets the value to return when a SQL null is encountered as the result of
+ * invoking a <code>getString</code> method.
+ *
+ * @param nullString the value
+ */
+ public void setNullString(String nullString) {
+ this.nullString = nullString;
+ }
+
+ /**
+ * Sets the value to return when a SQL null is encountered as the result of
+ * invoking a <code>getTime</code> method.
+ *
+ * @param nullTime the value
+ */
+ public void setNullTime(Time nullTime) {
+ this.nullTime = nullTime;
+ }
+
+ /**
+ * Sets the value to return when a SQL null is encountered as the result of
+ * invoking a <code>getTimestamp</code> method.
+ *
+ * @param nullTimestamp the value
+ */
+ public void setNullTimestamp(Timestamp nullTimestamp) {
+ this.nullTimestamp = nullTimestamp;
+ }
+
+ /**
+ * Sets the value to return when a SQL null is encountered as the result of
+ * invoking a <code>getURL</code> method.
+ *
+ * @param nullURL the value
+ */
+ public void setNullURL(URL nullURL) {
+ this.nullURL = nullURL;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/main/java/org/apache/commons/dbutils2/wrappers/StringTrimmedResultSet.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/dbutils2/wrappers/StringTrimmedResultSet.java b/src/main/java/org/apache/commons/dbutils2/wrappers/StringTrimmedResultSet.java
new file mode 100644
index 0000000..c65dbb6
--- /dev/null
+++ b/src/main/java/org/apache/commons/dbutils2/wrappers/StringTrimmedResultSet.java
@@ -0,0 +1,109 @@
+/*
+ * 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.
+ */
+package org.apache.commons.dbutils2.wrappers;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.Method;
+import java.sql.ResultSet;
+
+import org.apache.commons.dbutils2.ProxyFactory;
+
+/**
+ * Wraps a <code>ResultSet</code> to trim strings returned by the
+ * <code>getString()</code> and <code>getObject()</code> methods.
+ *
+ * <p>
+ * Usage Example:
+ * This example shows how to decorate ResultSets so processing continues as
+ * normal but all Strings are trimmed before being returned from the
+ * <code>ResultSet</code>.
+ * </p>
+ *
+ * <pre>
+ * ResultSet rs = // somehow get a ResultSet;
+ *
+ * // Substitute wrapped ResultSet with additional behavior for real ResultSet
+ * rs = StringTrimmedResultSet.wrap(rs);
+ *
+ * // Pass wrapped ResultSet to processor
+ * List list = new BasicRowProcessor().toBeanList(rs);
+ * </pre>
+ */
+public class StringTrimmedResultSet implements InvocationHandler {
+
+ /**
+ * The factory to create proxies with.
+ */
+ private static final ProxyFactory factory = ProxyFactory.instance();
+
+ /**
+ * Wraps the <code>ResultSet</code> in an instance of this class. This is
+ * equivalent to:
+ * <pre>
+ * ProxyFactory.instance().createResultSet(new StringTrimmedResultSet(rs));
+ * </pre>
+ *
+ * @param rs The <code>ResultSet</code> to wrap.
+ * @return wrapped ResultSet
+ */
+ public static ResultSet wrap(ResultSet rs) {
+ return factory.createResultSet(new StringTrimmedResultSet(rs));
+ }
+
+ /**
+ * The wrapped result.
+ */
+ private final ResultSet rs;
+
+ /**
+ * Constructs a new instance of <code>StringTrimmedResultSet</code>
+ * to wrap the specified <code>ResultSet</code>.
+ * @param rs ResultSet to wrap
+ */
+ public StringTrimmedResultSet(ResultSet rs) {
+ super();
+ this.rs = rs;
+ }
+
+ /**
+ * Intercept calls to the <code>getString()</code> and
+ * <code>getObject()</code> methods and trim any Strings before they're
+ * returned.
+ *
+ * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
+ * @param proxy Not used; all method calls go to the internal result set
+ * @param method The method to invoke on the result set
+ * @param args The arguments to pass to the result set
+ * @return string trimmed result
+ * @throws Throwable error
+ */
+ @Override
+ public Object invoke(Object proxy, Method method, Object[] args)
+ throws Throwable {
+
+ Object result = method.invoke(this.rs, args);
+
+ if ((method.getName().equals("getObject")
+ || method.getName().equals("getString"))
+ && result instanceof String) {
+ result = ((String) result).trim();
+ }
+
+ return result;
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/main/java/org/apache/commons/dbutils2/wrappers/package-info.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/dbutils2/wrappers/package-info.java b/src/main/java/org/apache/commons/dbutils2/wrappers/package-info.java
new file mode 100644
index 0000000..e14b91d
--- /dev/null
+++ b/src/main/java/org/apache/commons/dbutils2/wrappers/package-info.java
@@ -0,0 +1,21 @@
+/*
+ * 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.
+ */
+
+/**
+ * Wrappers that add functionality to java.sql classes.
+ */
+package org.apache.commons.dbutils2.wrappers;
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils/AbstractExecutorTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils/AbstractExecutorTest.java b/src/test/java/org/apache/commons/dbutils/AbstractExecutorTest.java
deleted file mode 100644
index 606458b..0000000
--- a/src/test/java/org/apache/commons/dbutils/AbstractExecutorTest.java
+++ /dev/null
@@ -1,122 +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.
- */
-package org.apache.commons.dbutils;
-
-import static org.mockito.Matchers.any;
-import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
-import java.sql.Connection;
-import java.sql.PreparedStatement;
-import java.sql.SQLException;
-import java.sql.Types;
-import org.junit.Before;
-import org.junit.Test;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-
-
-public class AbstractExecutorTest {
-
- @SuppressWarnings("rawtypes") // don't care about this in the unit test
- private AbstractExecutor executor;
-
- @Mock private Connection conn;
- @Mock private PreparedStatement stmt;
-
- @Before
- public void setup() throws SQLException {
- MockitoAnnotations.initMocks(this);
-
- when(conn.prepareStatement(any(String.class))).thenReturn(stmt);
- }
-
- @SuppressWarnings("rawtypes")
- public void createExecutor(String sql) throws SQLException {
- executor = new AbstractExecutor(conn, sql) { };
- }
-
- @Test
- public void testGoodSql() throws SQLException {
- createExecutor("select * from blah :first = first and :last=last and phone=:phone");
-
- verify(conn, times(1)).prepareStatement("select * from blah ? = first and ?=last and phone=?");
-
- executor.bind("first", "first_name")
- .bind(":last", "last_name")
- .bind("phone", Integer.valueOf(12345));
-
- verify(stmt, times(1)).setObject(1, "first_name");
- verify(stmt, times(1)).setObject(2, "last_name");
- verify(stmt, times(1)).setObject(eq(3), eq(Integer.valueOf(12345)));
-
- executor.throwIfUnmappedParams();
- }
-
- @Test
- public void testNoParamsSql() throws SQLException {
- createExecutor("select * from blah");
-
- verify(conn, times(1)).prepareStatement("select * from blah");
- verify(stmt, times(0)).setObject(any(Integer.class), any(Object.class));
-
- executor.throwIfUnmappedParams();
- }
-
- @Test(expected=SQLException.class)
- public void testMissingParamSql() throws SQLException {
- createExecutor("select * from blah :first = first and :last=last");
-
- verify(conn, times(1)).prepareStatement("select * from blah ? = first and ?=last");
-
- executor.bind("first", "first_name")
- .bind(":last", "last_name")
- .bind("phone", Integer.valueOf(12345)); // should throw
-
- verify(stmt, times(1)).setObject(1, "first_name");
- verify(stmt, times(1)).setObject(2, "last_name");
- verify(stmt, times(1)).setObject(eq(3), eq(Integer.valueOf(12345)));
- }
-
- @Test(expected=SQLException.class)
- public void testDoubleBind() throws SQLException {
- createExecutor("select * from blah :first = first and :last=last");
-
- verify(conn, times(1)).prepareStatement("select * from blah ? = first and ?=last");
-
- executor.bind("first", "first_name")
- .bind(":last", "last_name")
- .bind(":last", "last_name");
-
- verify(stmt, times(1)).setObject(1, "first_name");
- verify(stmt, times(1)).setObject(2, "last_name");
- }
-
- @Test
- public void testNullBind() throws SQLException {
- createExecutor("select * from blah :first = first and :last=last");
-
- verify(conn, times(1)).prepareStatement("select * from blah ? = first and ?=last");
-
- executor.bindNull("first")
- .bindNull(":last", Types.NULL);
-
- verify(stmt, times(1)).setNull(1, Types.VARCHAR);
- verify(stmt, times(1)).setNull(2, Types.NULL);
- }
-}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils/AsyncExecutorTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils/AsyncExecutorTest.java b/src/test/java/org/apache/commons/dbutils/AsyncExecutorTest.java
deleted file mode 100644
index cdc671f..0000000
--- a/src/test/java/org/apache/commons/dbutils/AsyncExecutorTest.java
+++ /dev/null
@@ -1,93 +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.
- */
-package org.apache.commons.dbutils;
-
-import static org.mockito.Mockito.doThrow;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-import java.sql.SQLException;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.Executors;
-import org.junit.Before;
-import org.junit.Test;
-import org.mockito.Mock;
-import org.mockito.MockitoAnnotations;
-
-@SuppressWarnings("boxing") // test code
-public class AsyncExecutorTest {
- AsyncExecutor runner;
-
- @Mock QueryRunner qRunner;
- @Mock ResultSetHandler<Object> handler;
- @Mock QueryExecutor queryExecutor;
- @Mock UpdateExecutor updateExecutor;
- @Mock InsertExecutor insertExecutor;
-
- @Before
- public void setUp() throws Exception {
- MockitoAnnotations.initMocks(this); // init the mocks
-
- runner = new AsyncExecutor(Executors.newFixedThreadPool(1));
- }
-
- @Test
- public void testQueryExecutor() throws Exception {
- runner.execute(queryExecutor, handler).get();
-
- verify(queryExecutor, times(1)).execute(handler);
- }
-
- @Test(expected=ExecutionException.class)
- public void testQueryExecutorException() throws Exception {
- doThrow(SQLException.class).when(queryExecutor).execute(handler);
- runner.execute(queryExecutor, handler).get();
-
- verify(queryExecutor, times(1)).execute(handler);
- }
-
- @Test
- public void testUpdateExecutor() throws Exception {
- runner.execute(updateExecutor).get();
-
- verify(updateExecutor, times(1)).execute();
- }
-
- @Test(expected=ExecutionException.class)
- public void testUpdateExecutorException() throws Exception {
- doThrow(SQLException.class).when(updateExecutor).execute();
- runner.execute(updateExecutor).get();
-
- verify(updateExecutor, times(1)).execute();
- }
-
- @Test
- public void testInsertExecutor() throws Exception {
- runner.execute(insertExecutor, handler).get();
-
- verify(insertExecutor, times(1)).execute(handler);
- }
-
- @Test(expected=ExecutionException.class)
- public void testInsertExecutorException() throws Exception {
- doThrow(SQLException.class).when(insertExecutor).execute(handler);
- runner.execute(insertExecutor, handler).get();
-
- verify(insertExecutor, times(1)).execute(handler);
- }
-
-
-}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils/BaseResultSetHandlerTestCase.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils/BaseResultSetHandlerTestCase.java b/src/test/java/org/apache/commons/dbutils/BaseResultSetHandlerTestCase.java
deleted file mode 100644
index a574c73..0000000
--- a/src/test/java/org/apache/commons/dbutils/BaseResultSetHandlerTestCase.java
+++ /dev/null
@@ -1,71 +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.
- */
-package org.apache.commons.dbutils;
-
-import java.sql.SQLException;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.LinkedList;
-import java.util.Map;
-
-import org.junit.Test;
-
-public final class BaseResultSetHandlerTestCase extends BaseTestCase {
-
- @Test
- public void handleWithoutExplicitResultSetInvocation() throws Exception {
- Collection<Map<String, Object>> result = new ToMapCollectionHandler().handle(createMockResultSet());
-
- assertFalse(result.isEmpty());
-
- for (Map<String, Object> current : result) {
- assertTrue(current.containsKey("one"));
- assertTrue(current.containsKey("two"));
- assertTrue(current.containsKey("three"));
- assertTrue(current.containsKey("notInBean"));
- assertTrue(current.containsKey("intTest"));
- assertTrue(current.containsKey("integerTest"));
- assertTrue(current.containsKey("nullObjectTest"));
- assertTrue(current.containsKey("nullPrimitiveTest"));
- assertTrue(current.containsKey("notDate"));
- assertTrue(current.containsKey("columnProcessorDoubleTest"));
- }
- }
-
- private static final class ToMapCollectionHandler
- extends BaseResultSetHandler<Collection<Map<String, Object>>> {
-
- @Override
- protected Collection<Map<String, Object>> handle() throws SQLException {
- Collection<Map<String, Object>> result = new LinkedList<Map<String, Object>>();
-
- while (next()) {
- Map<String, Object> current = new HashMap<String, Object>();
-
- for (int i = 1; i <= getMetaData().getColumnCount(); i++) {
- current.put(getMetaData().getColumnName(i), getObject(i));
- }
-
- result.add(current);
- }
-
- return result;
- }
-
- }
-
-}
http://git-wip-us.apache.org/repos/asf/commons-dbutils/blob/41d6d58c/src/test/java/org/apache/commons/dbutils/BaseTestCase.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/dbutils/BaseTestCase.java b/src/test/java/org/apache/commons/dbutils/BaseTestCase.java
deleted file mode 100644
index f0270a2..0000000
--- a/src/test/java/org/apache/commons/dbutils/BaseTestCase.java
+++ /dev/null
@@ -1,127 +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.
- */
-package org.apache.commons.dbutils;
-
-import java.math.BigInteger;
-import java.sql.ResultSet;
-import java.sql.ResultSetMetaData;
-import java.util.Date;
-
-import junit.framework.TestCase;
-
-/**
- * BaseTestCase is the base class for all test cases as well as the "all tests"
- * runner.
- */
-public class BaseTestCase extends TestCase {
-
- private static final String[] columnNames =
- new String[] {
- "one",
- "two",
- "three",
- "notInBean",
- "intTest",
- "integerTest",
- "nullObjectTest",
- "nullPrimitiveTest",
- "notDate",
- "columnProcessorDoubleTest" };
-
- /**
- * The number of columns in the MockResultSet.
- */
- protected static final int COLS = columnNames.length;
-
- protected static final ResultSetMetaData metaData =
- MockResultSetMetaData.create(columnNames);
-
- private static final Object[] row1 =
- new Object[] {
- "1",
- "2",
- "3",
- " notInBean ",
- Integer.valueOf(1),
- Integer.valueOf(2),
- null,
- null,
- new Date(),
- BigInteger.valueOf(13)};
-
- private static final Object[] row2 =
- new Object[] {
- "4",
- "5",
- "6",
- " notInBean ",
- Integer.valueOf(3),
- Integer.valueOf(4),
- null,
- null,
- new Date(),
- BigInteger.valueOf(13)};
-
- private static final Object[][] rows = new Object[][] { row1, row2 };
-
- /**
- * The number of rows in the MockResultSet.
- */
- protected static final int ROWS = rows.length;
-
- /**
- * The ResultSet all test methods will use.
- */
- protected ResultSet rs = null;
-
- /**
- * A ResultSet with 0 rows.
- */
- protected ResultSet emptyResultSet = null;
-
- /**
- * This is called before each test method so ResultSet will be fresh each
- * time.
- * @see junit.framework.TestCase#setUp()
- */
- @Override
- protected void setUp() throws Exception {
- super.setUp();
-
- rs = this.createMockResultSet();
- emptyResultSet = MockResultSet.create(metaData, null);
- }
-
- /**
- * Creates a freshly initialized ResultSet.
- */
- protected ResultSet createMockResultSet() {
- return MockResultSet.create(metaData, rows);
- }
-
- // Test which allows Eclipse to be run on full project (avoids no tests found)
- // check that the rows are valid for the column definition
- public void testCheckDataSizes() {
- assertEquals("Row 1 must contain correct number of columns", columnNames.length, row1.length);
- assertEquals("Row 1 must contain correct number of columns", columnNames.length, row2.length);
- }
-
- public void testResultSets() throws Exception {
- assertFalse("emptyResultSet should be empty", emptyResultSet.next());
- // fails in SqlNullCheckedResultSetTest assertTrue("rs should not be empty", rs.next());
- }
-}