Permalink
Browse files

[jdbc-driver] Generated keys support for statement:

```java
import acolyte.UpdateResult;
import acolyte.RowLists;

UpdateResult.One.withGeneratedKeys(RowLists.intList().append(4));
// to be returned from an update handler:
// update count = 1 and one generated key = 4
```
  • Loading branch information...
cchantep
cchantep committed Mar 8, 2014
1 parent b62fe6c commit ce8a913be91b04759903e8dab843baf41cad7b86
@@ -100,14 +100,6 @@ public boolean isQuery(final String sql) {
return false;
} // end of isQuery
/**
* {@inheritDoc}
* @throws RuntimeException Not supported
*/
public ResultSet getGeneratedKeys() {
throw new RuntimeException("Not supported");
} // end of getGeneratedKeys
// ---
/**
@@ -26,6 +26,12 @@
protected static final List<Parameter> NO_PARAMS =
Collections.unmodifiableList(new ArrayList<Parameter>());
/**
* No generated key
*/
protected static final ResultSet NO_GENERATED_KEY =
RowLists.stringList().resultSet();
// --- Properties ---
/**
@@ -68,6 +74,12 @@
*/
protected ResultSet result = null;
/**
* Last generated keys
* see #NO_GENERATED_KEY
*/
protected ResultSet generatedKeys = NO_GENERATED_KEY;
/**
* Max rows
*/
@@ -133,6 +145,7 @@ public ResultSet executeQuery(final String sql) throws SQLException {
checkClosed();
this.updateCount = -1;
this.generatedKeys = NO_GENERATED_KEY;
final QueryResult res = this.handler.whenSQLQuery(sql, NO_PARAMS);
@@ -147,15 +160,7 @@ public ResultSet executeQuery(final String sql) throws SQLException {
* {@inheritDoc}
*/
public int executeUpdate(final String sql) throws SQLException {
checkClosed();
this.result = null;
final UpdateResult res = this.handler.whenSQLUpdate(sql, NO_PARAMS);
this.warning = res.getWarning();
return (this.updateCount = res.getUpdateCount());
return executeUpdate(sql, RETURN_GENERATED_KEYS);
} // end of executeUpdate
/**
@@ -415,27 +420,29 @@ public boolean getMoreResults(final int current) throws SQLException {
public ResultSet getGeneratedKeys() throws SQLException {
checkClosed();
final ResultSet keys = this.handler.getGeneratedKeys();
return (keys != null) ? keys
: RowLists.rowList1(String.class).resultSet();
return this.generatedKeys;
} // end of getGeneratedKeys
/**
* {@inheritDoc}
* @throws java.sql.SQLFeatureNotSupportedException if |autoGeneratedKeys| is Statement.RETURN_GENERATED_KEYS
*/
public int executeUpdate(final String sql, final int autoGeneratedKeys)
throws SQLException {
checkClosed();
if (autoGeneratedKeys == RETURN_GENERATED_KEYS) {
throw new SQLFeatureNotSupportedException();
} // end of if
this.result = null;
final UpdateResult res = this.handler.whenSQLUpdate(sql, NO_PARAMS);
return executeUpdate(sql);
this.warning = res.getWarning();
this.generatedKeys =
(res.generatedKeys == null
|| autoGeneratedKeys == NO_GENERATED_KEYS)
? RowLists.stringList().resultSet()/* empty ResultSet */
: res.generatedKeys.resultSet();
return (this.updateCount = res.getUpdateCount());
} // end of executeUpdate
/**
@@ -1437,7 +1437,7 @@ public boolean supportsMultipleOpenResults() throws SQLException {
* {@inheritDoc}
*/
public boolean supportsGetGeneratedKeys() throws SQLException {
return false;
return true;
} // end of supportsGetGeneratedKeys
/**
@@ -158,6 +158,7 @@ public ResultSet executeQuery() throws SQLException {
this.updateCount = -1;
this.warning = res.getWarning();
this.generatedKeys = NO_GENERATED_KEY;
return (this.result = res.getRowList().resultSet().withStatement(this));
} // end of executeQuery
@@ -189,6 +190,9 @@ public int executeUpdate() throws SQLException {
this.result = null;
this.warning = res.getWarning();
this.generatedKeys = (res.generatedKeys == null)
? RowLists.stringList().resultSet()/* empty ResultSet */
: res.generatedKeys.resultSet();
return (this.updateCount = res.getUpdateCount());
} // end of executeUpdate
@@ -43,12 +43,6 @@ public UpdateResult whenSQLUpdate(String sql, List<Parameter> parameters)
*/
public boolean isQuery(String sql);
/**
* Returns keys generated by last execution.
* FOR FUTURE USE.
*/
public ResultSet getGeneratedKeys();
// --- Inner classes ---
/**
@@ -24,17 +24,29 @@
public final int count;
public final SQLWarning warning;
public final RowList<?> generatedKeys;
// --- Constructors ---
/**
* Bulk constructor.
*/
private UpdateResult(final int count, final SQLWarning warning) {
private UpdateResult(final int count,
final RowList<?> generatedKeys,
final SQLWarning warning) {
this.count = count;
this.generatedKeys = generatedKeys;
this.warning = warning;
} // end of <init>
/**
* With-warning constructor.
*/
private UpdateResult(final int count, final SQLWarning warning) {
this(count, null, warning);
} // end of <init>
/**
* No result constructor.
*/
@@ -51,13 +63,29 @@ public UpdateResult(final int count) {
// ---
/**
* Returns either null if there is no row resulting from update,
* or associated row list.
*/
public RowList<?> getGeneratedKeys() {
return this.generatedKeys;
} // end of getGeneratedKeys
/**
* Returns update count.
*/
public int getUpdateCount() {
return this.count;
} // end of getUpdateCount
/**
* Returns result with updated row |keys|.
* @param keys Generated keys
*/
public UpdateResult withGeneratedKeys(final RowList<?> keys) {
return new UpdateResult(this.count, keys, this.warning);
} // end of withGeneratedKeys
/**
* {@inheritDoc}
*/
@@ -12,16 +12,13 @@
import acolyte.StatementHandler;
import acolyte.UpdateResult;
import acolyte.QueryResult;
import acolyte.Rows;
import acolyte.StatementHandler.Parameter;
import static acolyte.RowLists.booleanList;
import static acolyte.RowLists.rowList1;
import static acolyte.RowLists.rowList3;
import static acolyte.RowList.Column;
import static acolyte.Rows.row1;
import static acolyte.Rows.row3;
/**
* Use cases for testing.
@@ -173,4 +170,24 @@ public QueryResult apply(String sql,
} // end of useCase4
/**
* Use case #5 - Generated keys
*/
public static Connection useCase5() throws SQLException {
return acolyte.Driver.
connection(new CompositeHandler().
withUpdateHandler(new CompositeHandler.UpdateHandler() {
public UpdateResult apply(String sql,
List<Parameter> ps) {
// One update with generated keys
return UpdateResult.One.
withGeneratedKeys(acolyte.RowLists.
intList().append(100));
}
}));
} // end of useCase5
} // end of class JavaUseCases
Oops, something went wrong.

0 comments on commit ce8a913

Please sign in to comment.