Skip to content

Commit

Permalink
[CONJ-389] getGeneratedKeys reviewed implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
rusher committed Dec 2, 2016
1 parent 4dd309e commit 6295808
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 287 deletions.
55 changes: 0 additions & 55 deletions src/main/java/org/mariadb/jdbc/MariaDbClientPreparedStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -384,61 +384,6 @@ private int[] executeInternalBatch(MultiExecutionResult internalExecutionResult,
return internalExecutionResult.getAffectedRows();
}

/**
* {inheritdoc}.
*/
@Override
public ResultSet getGeneratedKeys() throws SQLException {
if (executionResult != null && executionResult.getResultSet() == null) {
int autoIncrementIncrement = connection.getAutoIncrementIncrement();
//multi insert in one execution. will create result based on autoincrement
if (executionResult.hasMoreThanOneAffectedRows()) {
long[] data;
if (executionResult.isSingleExecutionResult()) {
int updateCount = executionResult.getFirstAffectedRows();
data = new long[updateCount];
for (int i = 0; i < updateCount; i++) {
data[i] = ((SingleExecutionResult) executionResult).getInsertId() + i * autoIncrementIncrement;
}
} else {
if (options.rewriteBatchedStatements && prepareResult.isQueryMultiValuesRewritable()) {
MultiVariableIntExecutionResult multiExecution = (MultiVariableIntExecutionResult) executionResult;
data = multiExecution.getInsertIdsForRewrite(autoIncrementIncrement);
} else {
MultiExecutionResult multiExecution = (MultiExecutionResult) executionResult;
int size = 0;
int affectedRowsLength = multiExecution.getAffectedRows().length;
for (int i = 0; i < affectedRowsLength; i++) {
int affectedRows = multiExecution.getAffectedRows()[i];
if (affectedRows >= 0) {
size += multiExecution.getAffectedRows()[i];
} else {
size += 1;
}
}

data = new long[(size < 0) ? 0 : size];
int insertIdCounter = 0;
for (int affectedRowsCounter = 0; affectedRowsCounter < affectedRowsLength; affectedRowsCounter++) {
int affectedRows = multiExecution.getAffectedRows()[affectedRowsCounter];
if (affectedRows > 0) {
for (int i = 0; i < affectedRows; i++) {
data[insertIdCounter++] = multiExecution.getInsertIds()[affectedRowsCounter] + i * autoIncrementIncrement;
}
} else {
data[insertIdCounter++] = multiExecution.getInsertIds()[affectedRowsCounter];
}
}
}
}
return MariaSelectResultSet.createGeneratedData(data, connection.getProtocol(), true);
}
return MariaSelectResultSet.createGeneratedData(executionResult.getInsertIds(), connection.getProtocol(), true);
}
return MariaSelectResultSet.EMPTY;
}


/**
* Retrieves a <code>ResultSetMetaData</code> object that contains information about the columns of the
* <code>ResultSet</code> object that will be returned when this <code>PreparedStatement</code> object is executed.
Expand Down
37 changes: 4 additions & 33 deletions src/main/java/org/mariadb/jdbc/MariaDbStatement.java
Original file line number Diff line number Diff line change
Expand Up @@ -769,34 +769,9 @@ public Connection getConnection() throws SQLException {
* @since 1.4
*/
public ResultSet getGeneratedKeys() throws SQLException {
if (executionResult != null && executionResult.getResultSet() == null) {
if (executionResult != null) {
int autoIncrementIncrement = connection.getAutoIncrementIncrement();
//multi insert in one execution. will create result based on autoincrement
if (executionResult.hasMoreThanOneAffectedRows()) {
long[] data;
if (executionResult.isSingleExecutionResult()) {
int updateCount = executionResult.getFirstAffectedRows();
data = new long[updateCount];
for (int i = 0; i < updateCount; i++) {
data[i] = ((SingleExecutionResult) executionResult).getInsertId() + i * autoIncrementIncrement;
}
} else {
MultiExecutionResult multiExecution = (MultiExecutionResult) executionResult;
int size = 0;
int affectedRowsLength = multiExecution.getAffectedRows().length;
for (int i = 0; i < affectedRowsLength; i++) {
size += multiExecution.getAffectedRows()[i];
}
data = new long[size];
for (int affectedRows = 0; affectedRows < affectedRowsLength; affectedRows++) {
for (int i = 0; i < multiExecution.getAffectedRows()[affectedRows]; i++) {
data[i] = multiExecution.getInsertIds()[affectedRows] + i * autoIncrementIncrement;
}
}
}
return MariaSelectResultSet.createGeneratedData(data, connection.getProtocol(), true);
}
return MariaSelectResultSet.createGeneratedData(executionResult.getInsertIds(), connection.getProtocol(), true);
return executionResult.getGeneratedKeys(autoIncrementIncrement, protocol);
}
return MariaSelectResultSet.EMPTY;
}
Expand Down Expand Up @@ -878,11 +853,7 @@ public int getUpdateCount() throws SQLException {
if (executionResult == null || executionResult.getResultSet() != null) {
return -1; /* Result comes from SELECT , or there are no more results */
}
if (executionResult.isSingleExecutionResult()) {
return (int) ((SingleExecutionResult) executionResult).getAffectedRows();
} else {
return executionResult.getFirstAffectedRows();
}
return executionResult.getFirstAffectedRows();
}

protected void skipMoreResults() throws SQLException {
Expand Down Expand Up @@ -1145,8 +1116,8 @@ public int[] executeBatch() throws SQLException {
}
} catch (QueryException e) {
exception = e;
} finally {
internalExecutionResult.fixStatsError(batchQueries.size());
} finally {
if (exception != null && multipleExecution) {
internalExecutionResult.updateResultsMultiple(batchQueries.size(), true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWIS
import org.mariadb.jdbc.MariaDbStatement;
import org.mariadb.jdbc.UrlParser;
import org.mariadb.jdbc.internal.MariaDbType;
import org.mariadb.jdbc.internal.logging.Logger;
import org.mariadb.jdbc.internal.logging.LoggerFactory;
import org.mariadb.jdbc.internal.packet.*;
import org.mariadb.jdbc.internal.packet.dao.ColumnInformation;
import org.mariadb.jdbc.internal.packet.dao.parameters.ParameterHolder;
Expand Down Expand Up @@ -1161,7 +1159,7 @@ public ExecutionResult getResult(ExecutionResult executionResult, int resultSetS
if (bufferEof.getByteAt(0) != Packet.EOF) {
throw new QueryException("Packets out of order when reading field packets, expected was EOF stream. "
+ "Packet contents (hex) = " + Utils.hexdump(bufferEof.buf, options.maxQuerySizeToLog, 0, buffer.position));
} else if (executionResult.isCanHaveCallableResultset() || checkCallableResultSet) {
} else if (executionResult.isCanHaveCallableResultSet() || checkCallableResultSet) {
//Identify if this is a "callable OUT packet" (callableResult=true)
//needed because :
// - will permit for callableStatement to identify the output result packet
Expand All @@ -1183,7 +1181,7 @@ public ExecutionResult getResult(ExecutionResult executionResult, int resultSetS
} catch (QueryException e) {
}
}
if (!executionResult.isCanHaveCallableResultset()) {
if (!executionResult.isCanHaveCallableResultSet()) {
throw new QueryException("Select command are not permitted via executeBatch() command");
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package org.mariadb.jdbc.internal.queryresults;

import org.mariadb.jdbc.MariaDbStatement;
import org.mariadb.jdbc.internal.protocol.Protocol;
import org.mariadb.jdbc.internal.queryresults.resultset.MariaSelectResultSet;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Deque;

public interface ExecutionResult {
String INSERT_ID_ROW_NAME = "insert_id";

MariaSelectResultSet getResultSet();

Expand All @@ -33,18 +36,14 @@ public interface ExecutionResult {

void fixStatsError(int sendCommand);

long[] getInsertIds();

boolean hasMoreThanOneAffectedRows();

int getFirstAffectedRows();

boolean isSelectPossible();

boolean isCanHaveCallableResultset();
boolean isCanHaveCallableResultSet();

Deque<ExecutionResult> getCachedExecutionResults();

boolean isSingleExecutionResult();
ResultSet getGeneratedKeys(int autoIncrementIncrement, Protocol protocol);
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ public interface MultiExecutionResult extends ExecutionResult {
int[] getAffectedRows();

int getFirstAffectedRows();

}

Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package org.mariadb.jdbc.internal.queryresults;

import org.mariadb.jdbc.MariaDbStatement;
import org.mariadb.jdbc.internal.MariaDbType;
import org.mariadb.jdbc.internal.packet.dao.ColumnInformation;
import org.mariadb.jdbc.internal.protocol.Protocol;
import org.mariadb.jdbc.internal.queryresults.resultset.MariaSelectResultSet;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.*;

public class MultiFixedIntExecutionResult implements MultiExecutionResult {

Expand All @@ -16,9 +18,9 @@ public class MultiFixedIntExecutionResult implements MultiExecutionResult {
private boolean moreResultAvailable;
private int fetchSize;
private boolean selectPossible;
private boolean canHaveCallableResultset;
private boolean canHaveCallableResultSet;
private MariaSelectResultSet resultSet = null;
private long[] insertId;
private long[] insertIds;
private int[] affectedRows;
private int currentStat = 0;

Expand All @@ -34,13 +36,13 @@ public MultiFixedIntExecutionResult(MariaDbStatement statement, int size, int fe
this.statement = statement;
this.fetchSize = fetchSize;
this.selectPossible = selectPossible;
this.canHaveCallableResultset = false;
this.canHaveCallableResultSet = false;
this.cachedExecutionResults = new ArrayDeque<>();

affectedRows = new int[size];
insertId = new long[size];
insertIds = new long[size];
Arrays.fill(affectedRows, Statement.EXECUTE_FAILED);
Arrays.fill(insertId, Statement.EXECUTE_FAILED);
Arrays.fill(insertIds, 0);
}

/**
Expand All @@ -51,7 +53,7 @@ public MultiFixedIntExecutionResult(MariaDbStatement statement, int size, int fe
*/
public void addResultSet(MariaSelectResultSet result, boolean moreResultAvailable) {
this.resultSet = result;
this.insertId[currentStat] = Statement.SUCCESS_NO_INFO;
this.insertIds[currentStat] = 0;
this.affectedRows[currentStat++] = -1;
this.setMoreResultAvailable(moreResultAvailable);
}
Expand All @@ -64,23 +66,15 @@ public void addResultSet(MariaSelectResultSet result, boolean moreResultAvailabl
* @param moreResultAvailable is there additional packet
*/
public void addStats(long affectedRows, long insertId, boolean moreResultAvailable) {
this.insertId[currentStat] = insertId;
this.insertIds[currentStat] = insertId;
this.affectedRows[currentStat++] = (int) affectedRows;
setMoreResultAvailable(moreResultAvailable);
}

public long[] getInsertIds() {
return insertId;
}

public int[] getAffectedRows() {
return affectedRows;
}

public boolean hasMoreThanOneAffectedRows() {
return affectedRows.length > 0 && affectedRows[0] > 1;
}

public int getFirstAffectedRows() {
return affectedRows[0];
}
Expand Down Expand Up @@ -175,23 +169,57 @@ public boolean isSelectPossible() {
return selectPossible;
}

public boolean isCanHaveCallableResultset() {
return canHaveCallableResultset;
public boolean isCanHaveCallableResultSet() {
return canHaveCallableResultSet;
}

public Deque<ExecutionResult> getCachedExecutionResults() {
return cachedExecutionResults;
}

public void addResult(ExecutionResult executionResult) {
cachedExecutionResults.add(executionResult);
}

public boolean isSingleExecutionResult() {
return false;
}

public int getCurrentStat() {
return currentStat;
}

/**
* Return auto_increment keys in resultSet.
*
* @param autoIncrementIncrement connection autoIncrementIncrement variable value
* @param protocol current protocol
* @return resultSet
*/
public ResultSet getGeneratedKeys(int autoIncrementIncrement, Protocol protocol) {
if (resultSet == null) {
ColumnInformation[] columns = new ColumnInformation[1];
columns[0] = ColumnInformation.create(INSERT_ID_ROW_NAME, MariaDbType.BIGINT);

List<byte[][]> rows = new ArrayList<>();

//multi insert in one execution. will create result based on autoincrement
if (affectedRows.length > 0 && affectedRows[0] > 1) {
for (int affectedRow : affectedRows) {
for (int counter = 0; counter < affectedRow; counter++) {
for (long insertId : insertIds) {
byte[][] row = {String.valueOf(insertId + counter * autoIncrementIncrement).getBytes()};
rows.add(row);
}
}
}
} else {
for (long insertId : insertIds) {
if (insertId != 0) {
byte[][] row = {String.valueOf(insertId).getBytes()};
rows.add(row);
}
}
}
return new MariaSelectResultSet(columns, rows, protocol, ResultSet.TYPE_SCROLL_SENSITIVE) {
@Override
public int findColumn(String name) {
return 1;
}
};
}
return MariaSelectResultSet.EMPTY;
}
}
Loading

0 comments on commit 6295808

Please sign in to comment.