Permalink
Browse files

[core] Deprecates append-row operation on RowList, replaced by multiv…

…alue-append.

Prevents null inference issues.

```java
import acolyte.RowLists;
import acolyte.Rows;

// Before
RowLists.rowList2(String.class, Integer.class).
  append(Rows.row2("str", 1)); // now deprecated

RowLists.rowList2(String.class, Integer.class).
  append("str", 1);
```
  • Loading branch information...
cchantep
cchantep committed Jan 9, 2014
1 parent cb0851c commit c68afc6a732f50c4e8b7cea9628a335caf38583b
@@ -36,8 +36,9 @@
* Appends |row|.
*
* @return Updated row list
* @deprecated Append operation with multiple column values provided by sub-classes.
*/
public abstract RowList<R> append(R row);
protected abstract RowList<R> append(R row);
/**
* Returns copy of row list with updated column names/labels.
@@ -32,8 +32,9 @@ public abstract class RowList#N#<#CP#,UPDATED extends RowList#N#<#CP#,?>>
/**
* {@inheritDoc}
* @deprecated Use append operation with multiple column values.
*/
public UPDATED append(final Row#N#<#CP#> row) {
protected UPDATED append(final Row#N#<#CP#> row) {
final ArrayList<Row#N#<#CP#>> copy = new ArrayList<Row#N#<#CP#>>(getRows());
copy.add(row);
@@ -74,7 +74,7 @@ public QueryResult apply(String sql,
withLabel(1, "String"). // Optional: set labels
withLabel(3, "Date").
append("str", 1.2f, new Date(1l)).
append(row3("val", 2.34f, (Date)null)).
append("val", 2.34f, null).
asResult();
}
});
@@ -101,8 +101,8 @@ public QueryResult apply(String sql,
return rowList3(Column(String.class, "str"),
Column(Float.class, "f"),
Column(Date.class, "date")).
append(row3("text", 2.3f, new Date(3l))).
append(row3("label", 4.56f, new Date(4l))).
append("text", 2.3f, new Date(3l)).
append("label", 4.56f, new Date(4l)).
asResult();
}
});
View
100 readme.md
@@ -95,7 +95,7 @@ StatementHandler handler = new CompositeHandler().
rowList3(String.class, Float.class, Date.class).
withLabel(1, "String").withLabel(3, "Date"). // Optional: set labels
append("str", 1.2f, new Date(1, 2, 3)). // values append
append(row3("val", 2.34f, (Date)null));
append("val", 2.34f, null);
return rows.asResult();
}
@@ -210,18 +210,18 @@ import static acolyte.Rows.row1;
// we have declared list1 and list2 (see previous example)
list1 = list1.append(row1("str"));
list1 = list1.append("str");
ResultSet rs1 = list1.resultSet();
ResultSet rs2 = list2.resultSet();
```
From previous example, result set `rs1` will contain 1 row, whereas `rs2` is empty.
Take care to `list1 = list1.append(row1("str"));`. As provided `RowList` classes are immutable, you should get updated instance from `append` to work on the list containing added row. This is more safe, and allow to rewrite previous example like:
Take care to `list1 = list1.append("str");`. As provided `RowList` classes are immutable, you should get updated instance from `append` to work on the list containing added row. This is more safe, and allow to rewrite previous example like:
```java
ResultSet rs1 = list1.append(row1("str")).resultSet();
ResultSet rs1 = list1.append("str").resultSet();
ResultSet rs2 = list2.resultSet();
```
@@ -277,48 +277,6 @@ RowLists.timestampList().append(tsRow);
RowLists.timestampList(tsRow/* ... */);
```
On single column row list, it's also possible to directly append unwrapped value, instead of row object wrapping a single value:
```java
RowLists.stringList().append("stringVal")
// ... instead of ...
//RowLists.stringList().append(Rows.row1("stringVal"))
```
##### NULL values
To be able to add a row with a `null` values, types must be explicitly given,
otherwise `java.lang.Object` will be used.
There are various ways to do so:
```java
import acolyte.RowList2;
import acolyte.RowLists;
import acolyte.Row2;
import acolyte.Rows;
// ...
RowList2<String,Float> list = RowLists.rowList2(String.class, Float.class);
// Indicates types through row constructor
list.append(new Row2<String,Float>(null, null));
// Indicates types through row factory
list.append(Rows.<String,Float>row2(null, null));
// Indicates types by cast
list.append(Rows.row2((String) null, (Float) null));
```
Type inference works well for `null` with multiple-args-append:
```java
list.append(null, null);
```
#### SQL Warnings
Acolyte can also mock up SQL warnings, on update or query, so that `java.sql.Statement.getWarnings()` will returned expected instance.
@@ -394,7 +352,7 @@ val handler: CompositeHandler = Acolyte.handleStatement.
1 -> "String",
3 -> "Date")
:+ ("str", 1.2f, new Date(1l)) // tuple as row
:+ row3[String,Float,Date]("val", 2.34f, null)).
:+ ("val", 2.34f, null)).
asResult
}
@@ -581,15 +539,6 @@ RowLists.timeList() :+ timeRow
RowLists.timestampList() :+ tsRow
```
On single column row list, it's also possible to directly append unwrapped value, instead of row object wrapping a single value:
```java
RowLists.stringList :+ "stringVal"
// ... instead of ...
//RowLists.stringList() :+ Rows.row1("stringVal")
```
Once you have declared your row list, and before turning it as result set, you can either add rows to it, or leave it empty.
```scala
@@ -599,41 +548,10 @@ import acolyte.Rows.row1
// ...
val rs1: ResultSet = list1.append(row1("str")).resultSet()
val rs1: ResultSet = list1.append("str").resultSet()
val rs2: ResultSet = list2.resultSet()
```
##### NULL values
To be able to add a row with a `null` values, types must be explicitly given,
otherwise `Any` will be used.
There are various ways to do so:
```scala
import acolyte.{ RowList2, RowLists, Row2, Rows }
// ...
val list: RowList2[String,Float] =
RowLists.rowList2(classOf[String], classOf[Float])
// Indicates types through row constructor
list.append(new Row2[String,Float](null, null))
// Indicates types through row factory
list :+ Rows.row2[String,Float](null, null)
// Indicates types by cast
list :+ Rows.row2(null.asInstanceOf[String], null.asInstanceOf[Float])
```
Type inference works well for `null` with multiple-args-append:
```scala
list :+ (null, null)
```
### Specs2
Acolyte can be used with specs2 to write executable specification for function accessing persistence.
@@ -752,8 +670,7 @@ public class ZooTest {
withQueryHandler(new QueryHandler() {
public QueryResult apply(String sql, List<Parameter> parameters) throws SQLException {
return zooSchema.
append(row5("dog", 1, "Scooby",
(Boolean)null, "red")).
append("dog", 1, "Scooby", null, "red").
asResult();
}
@@ -782,8 +699,7 @@ public class ZooTest {
withQueryHandler(new QueryHandler() {
public QueryResult apply(String sql, List<Parameter> parameters) throws SQLException {
return zooSchema.
append(row5("bird", 2, "Ostrich",
false, (String)null)).
append("bird", 2, "Ostrich", false, null)).
asResult();
}

0 comments on commit c68afc6

Please sign in to comment.