Skip to content

Commit

Permalink
[core] Deprecates append-row operation on RowList, replaced by multiv…
Browse files Browse the repository at this point in the history
…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 committed Jan 11, 2014
1 parent cb0851c commit c68afc6
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 97 deletions.
3 changes: 2 additions & 1 deletion core/src/main/java/acolyte/RowList.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ public abstract class RowList<R extends Row> {
* 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.
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/templates/RowList.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions core/src/test/java/usecase/JavaUseCases.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});
Expand All @@ -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();
}
});
Expand Down
100 changes: 8 additions & 92 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
```

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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

}
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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();

}
Expand Down Expand Up @@ -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();

}
Expand Down

0 comments on commit c68afc6

Please sign in to comment.