Permalink
Browse files

[jdbc-driver] Add a new `RowList` factory for a single row with a sin…

…gle value.

```java
import static acolyte.jdbc.RowLists;

RowLists.scalar("Foo"); // Scalar RowList of String
```
  • Loading branch information...
cchantep
cchantep committed Apr 25, 2015
1 parent 92f72ae commit 05d7fbaaf2d31c96afcfed9ddcdb101555d615ea
@@ -178,7 +178,8 @@ public T withQueryDetection(final String... pattern) {
* Query handler.
*/
public static interface QueryHandler {
public QueryResult apply(String sql, List<Parameter> parameters) throws SQLException;
public QueryResult apply(String sql, List<Parameter> parameters)
throws SQLException;
} // end of interfaceQueryHandler
@@ -52,6 +52,9 @@
/**
* Creates a not nullable column.
*
* @param columnClass the class of the column value
* @param name the colunm name
*/
Column(final Class<T> columnClass, final String name) {
this(columnClass, name, false);
@@ -89,8 +89,7 @@ public CompositeHandler withUpdateHandler(final UpdateHandler handler) {
*/
public CompositeHandler withQueryDetection(final Pattern... pattern) {
return new CompositeHandler(queryDetectionPattern(pattern),
this.queryHandler,
this.updateHandler);
this.queryHandler, this.updateHandler);
} // end of withQueryDetection
} // end of class CompositeHandler
@@ -1,5 +1,6 @@
package acolyte.jdbc;
import java.util.LinkedHashMap;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
@@ -56,7 +57,7 @@
static {
// JDBC type mappings
final HashMap<Integer,String> mappings = new HashMap<Integer,String>();
final LinkedHashMap<Integer,String> mappings = new LinkedHashMap<Integer,String>();
mappings.put(Types.ARRAY, Array.class.getName());
mappings.put(Types.BIGINT, Long.class.getName());
@@ -68,14 +69,14 @@
mappings.put(Types.DOUBLE, Double.class.getName());
mappings.put(Types.FLOAT, Float.class.getName());
mappings.put(Types.INTEGER, Integer.class.getName());
mappings.put(Types.LONGVARCHAR, String.class.getName());
mappings.put(Types.NUMERIC, BigDecimal.class.getName());
mappings.put(Types.REAL, Float.class.getName());
mappings.put(Types.SMALLINT, Short.class.getName());
mappings.put(Types.TINYINT, Byte.class.getName());
mappings.put(Types.TIME, Time.class.getName());
mappings.put(Types.TIMESTAMP, Timestamp.class.getName());
mappings.put(Types.VARCHAR, String.class.getName());
mappings.put(Types.LONGVARCHAR, String.class.getName());
mappings.put(Types.BINARY, byte[].class.getName());
mappings.put(Types.VARBINARY, byte[].class.getName());
mappings.put(Types.LONGVARBINARY, byte[].class.getName());
@@ -327,5 +327,18 @@ public final class RowLists {
return list;
}
public static <T> RowList1.Impl<T> scalar(final T value) {
if (value == null) {
throw new IllegalArgumentException("Cannot create a scalar row list with a null value");
}
@SuppressWarnings("unchecked")
final Class<T> cls = (Class<T>) value.getClass();
RowList1.Impl<T> list = new RowList1.Impl<T>(cls);
return list.append(value);
}
#F#
}
@@ -1275,6 +1275,22 @@ object RowListSpec extends Specification with RowListTest {
}
}
"Scalar row list" should {
"be created for a string" in {
val rs = RowLists.scalar("Foo").resultSet
rs.next
rs.getString(1) aka "scalar value" must_== "Foo"
}
"be created for an integer" in {
val rs = RowLists.scalar(123).resultSet
rs.next
rs.getInt(1) aka "scalar value" must_== 123
}
}
temporalGetterSpec[Date]("Date", new Date(1, 2, 3))
temporalGetterSpec[Time]("Time", new Time(1, 2, 3))
temporalGetterSpec[Timestamp]("Timestamp", new Timestamp(1, 2, 3, 5, 6, 7, 8))

0 comments on commit 05d7fba

Please sign in to comment.