Skip to content

Commit

Permalink
[CONJ-322] ResultSet.update* implementation - using virtual insert ro…
Browse files Browse the repository at this point in the history
…w in respect of java API philosophy
  • Loading branch information
rusher committed Jul 23, 2017
1 parent ed270f9 commit b0ecfc2
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 37 deletions.
31 changes: 31 additions & 0 deletions src/main/java/org/mariadb/jdbc/MariaDbDatabaseMetaData.java
Expand Up @@ -2274,10 +2274,41 @@ public ResultSet getIndexInfo(String catalog, String schema, String table,
return executeQuery(sql);
}

/**
* Retrieves whether this database supports the given result set type.
* ResultSet.TYPE_FORWARD_ONLY and ResultSet.TYPE_SCROLL_INSENSITIVE are supported.
*
* @param type one of the following <code>ResultSet</code> constants:
* <ul>
* <li><code>ResultSet.TYPE_FORWARD_ONLY</code></li>
* <li><code>ResultSet.TYPE_SCROLL_INSENSITIVE</code></li>
* <li><code>ResultSet.TYPE_SCROLL_SENSITIVE</code></li>
* </ul>
* @return true if supported
* @throws SQLException cannot occur here
*/
public boolean supportsResultSetType(int type) throws SQLException {
return (type == ResultSet.TYPE_SCROLL_INSENSITIVE || type == ResultSet.TYPE_FORWARD_ONLY);
}

/**
* Retrieves whether this database supports the given concurrency type in combination with the given result set type.
* All are supported, but combination that use ResultSet.TYPE_SCROLL_INSENSITIVE.
*
* @param type one of the following <code>ResultSet</code> constants:
* <ul>
* <li><code>ResultSet.TYPE_FORWARD_ONLY</code></li>
* <li><code>ResultSet.TYPE_SCROLL_INSENSITIVE</code></li>
* <li><code>ResultSet.TYPE_SCROLL_SENSITIVE</code></li>
* </ul>
* @param concurrency one of the following <code>ResultSet</code> constants:
* <ul>
* <li><code>ResultSet.CONCUR_READ_ONLY</code></li>
* <li><code>ResultSet.CONCUR_UPDATABLE</code></li>
* </ul>
* @return true if supported
* @throws SQLException cannot occur here
*/
public boolean supportsResultSetConcurrency(int type, int concurrency) throws SQLException {
//Support all concurrency (ResultSet.CONCUR_READ_ONLY and ResultSet.CONCUR_UPDATABLE)
//so just return scroll type
Expand Down
Expand Up @@ -549,6 +549,7 @@ protected void updateRowData(byte[] rawData) {
/**
* Delete current data.
* Position cursor to the previous row.
* @throws SQLException if previous() fail.
*/
protected void deleteCurrentRowData() throws SQLException {
//move data
Expand Down
Expand Up @@ -79,15 +79,13 @@ public class UpdatableResultSet extends SelectResultSet {
private static final int STATE_UPDATE = 1;
private static final int STATE_UPDATED = 2;
private static final int STATE_INSERT = 3;
public boolean rowUpdated = false;
public boolean rowInserted = false;
public boolean rowDeleted = false;
private String database;
private String table;

private boolean canBeUpdate;
private boolean canBeInserted;
private boolean canBeRefresh;
private int notInsertRowPointer;

private String exceptionUpdateMsg;
private String exceptionInsertMsg;
Expand Down Expand Up @@ -117,6 +115,12 @@ public UpdatableResultSet(ColumnInformation[] columnsInformation, Results result
parameterHolders = new ParameterHolder[columnInformationLength];
}


@Override
public int getConcurrency() throws SQLException {
return CONCUR_UPDATABLE;
}

private void checkIfUpdatable(Results results) throws IOException, SQLException {

database = null;
Expand Down Expand Up @@ -1199,7 +1203,6 @@ public void insertRow() throws SQLException {
}

insertPreparedStatement.execute();
rowInserted = true;

if (hasGeneratedPrimaryFields) {
//primary is auto_increment (only one field)
Expand All @@ -1219,7 +1222,6 @@ public void insertRow() throws SQLException {
}

Arrays.fill(parameterHolders, null);
state = STATE_STANDARD;
}
}

Expand All @@ -1229,7 +1231,7 @@ public void insertRow() throws SQLException {
public void updateRow() throws SQLException {

if (state == STATE_INSERT) {
throw new SQLException("Cannot call updateRow() when inserting a new row", "24000");
throw new SQLException("Cannot call updateRow() when inserting a new row");
}

if (state == STATE_UPDATE) {
Expand Down Expand Up @@ -1288,7 +1290,6 @@ public void updateRow() throws SQLException {

refreshRow();

rowUpdated = true;
Arrays.fill(parameterHolders, null);
state = STATE_STANDARD;
}
Expand All @@ -1301,11 +1302,11 @@ public void updateRow() throws SQLException {
public void deleteRow() throws SQLException {

if (state == STATE_INSERT) {
throw new SQLException("Cannot call deleteRow() when inserting a new row", "24000");
throw new SQLException("Cannot call deleteRow() when inserting a new row");
}

if (!canBeUpdate) {
throw new SQLDataException(exceptionUpdateMsg, "24000");
throw new SQLDataException(exceptionUpdateMsg);
}

if (getRowPointer() < 0) {
Expand Down Expand Up @@ -1346,7 +1347,6 @@ public void deleteRow() throws SQLException {
deletePreparedStatement.executeUpdate();

deleteCurrentRowData();
rowDeleted = true;
}


Expand Down Expand Up @@ -1418,39 +1418,27 @@ private byte[] refreshRawData() throws SQLException {
* {inheritDoc}.
*/
public void refreshRow() throws SQLException {
if (canBeRefresh) {
updateRowData(refreshRawData());
if (state == STATE_INSERT) {
throw new SQLException("Cannot call deleteRow() when inserting a new row");
}
}

/**
* {inheritDoc}.
*/
public boolean rowUpdated() throws SQLException {
return rowUpdated;
}
if (getRowPointer() < 0) {
throw new SQLDataException("Current position is before the first row", "22023");
}

/**
* {inheritDoc}.
*/
public boolean rowInserted() throws SQLException {
return rowInserted;
}
if (getRowPointer() >= getDataSize()) {
throw new SQLDataException("Current position is after the last row", "22023");
}

/**
* {inheritDoc}.
*/
public boolean rowDeleted() throws SQLException {
return rowDeleted;
if (canBeRefresh) {
updateRowData(refreshRawData());
}
}

/**
* {inheritDoc}.
*/
public void cancelRowUpdates() throws SQLException {
if (state == STATE_INSERT) {
throw new SQLException("Cannot call cancelRowUpdates() when inserting a new row", "24000");
}
Arrays.fill(parameterHolders, null);
state = STATE_STANDARD;
}
Expand All @@ -1459,9 +1447,10 @@ public void cancelRowUpdates() throws SQLException {
* {inheritDoc}.
*/
public void moveToInsertRow() throws SQLException {
if (!canBeInserted) throw new SQLException(exceptionInsertMsg, "24000");
if (!canBeInserted) throw new SQLException(exceptionInsertMsg);
Arrays.fill(parameterHolders, null);
state = STATE_INSERT;
notInsertRowPointer = getRowPointer();
}

/**
Expand All @@ -1470,5 +1459,80 @@ public void moveToInsertRow() throws SQLException {
public void moveToCurrentRow() throws SQLException {
Arrays.fill(parameterHolders, null);
state = STATE_STANDARD;
setRowPointer(notInsertRowPointer);
}


@Override
public void beforeFirst() throws SQLException {
if (state == STATE_INSERT) {
state = STATE_UPDATE;
setRowPointer(notInsertRowPointer);
}
super.beforeFirst();
}

@Override
public boolean first() throws SQLException {
if (state == STATE_INSERT) {
state = STATE_UPDATE;
setRowPointer(notInsertRowPointer);
}
return super.first();
}

@Override
public boolean last() throws SQLException {
if (state == STATE_INSERT) {
state = STATE_UPDATE;
setRowPointer(notInsertRowPointer);
}
return super.last();
}

@Override
public void afterLast() throws SQLException {
if (state == STATE_INSERT) {
state = STATE_UPDATE;
setRowPointer(notInsertRowPointer);
}
super.afterLast();
}

@Override
public boolean absolute(int row) throws SQLException {
if (state == STATE_INSERT) {
state = STATE_UPDATE;
setRowPointer(notInsertRowPointer);
}
return super.absolute(row);
}

@Override
public boolean relative(int rows) throws SQLException {
if (state == STATE_INSERT) {
state = STATE_UPDATE;
setRowPointer(notInsertRowPointer);
}
return super.relative(rows);
}

@Override
public boolean next() throws SQLException {
if (state == STATE_INSERT) {
state = STATE_UPDATE;
setRowPointer(notInsertRowPointer);
}
return super.next();
}

@Override
public boolean previous() throws SQLException {
if (state == STATE_INSERT) {
state = STATE_UPDATE;
setRowPointer(notInsertRowPointer);
}
return super.previous();
}

}
Expand Up @@ -479,9 +479,9 @@ public void dataConformity() throws SQLException {
new String(varbinary0, StandardCharsets.UTF_8));


/***************************************************************************
* Update Row
**************************************************************************/
//***************************************************************************
// Update Row
//***************************************************************************

rs.updateBoolean(1, Boolean.TRUE);
rs.updateByte(2, (byte) 2);
Expand Down

0 comments on commit b0ecfc2

Please sign in to comment.