Skip to content
Permalink
Browse files

refactor: move implementation of Prepared and Callable statements to …

…PgPreparedStatement and PgCallableStatement

Note: the code is moved as is, no reformatting happens on the way for easier comparison

closes #459
  • Loading branch information
vlsi committed Dec 26, 2015
1 parent c10acf0 commit 8fca8b433b36002df472c5c064e3d886ee4394eb
@@ -0,0 +1,105 @@
package org.postgresql.jdbc;

import org.postgresql.core.*;
import org.postgresql.util.GT;
import org.postgresql.util.PSQLException;
import org.postgresql.util.PSQLState;

import java.sql.BatchUpdateException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.util.List;

class BatchResultHandler implements ResultHandler
{
private PgStatement pgStatement;
private BatchUpdateException batchException = null;
private int resultIndex = 0;

private final Query[] queries;
private final ParameterList[] parameterLists;
private final int[] updateCounts;
private final boolean expectGeneratedKeys;
private ResultSet generatedKeys;

BatchResultHandler(PgStatement pgStatement, Query[] queries, ParameterList[] parameterLists, int[] updateCounts, boolean expectGeneratedKeys) {
this.pgStatement = pgStatement;
this.queries = queries;
this.parameterLists = parameterLists;
this.updateCounts = updateCounts;
this.expectGeneratedKeys = expectGeneratedKeys;
}

public void handleResultRows(Query fromQuery, Field[] fields, List<byte[][]> tuples, ResultCursor cursor) {
if (!expectGeneratedKeys) {
handleError(new PSQLException(GT.tr("A result was returned when none was expected."),
PSQLState.TOO_MANY_RESULTS));
} else {
if (generatedKeys == null) {
try
{
generatedKeys = pgStatement.createResultSet(fromQuery, fields, tuples, cursor);
}
catch (SQLException e)
{
handleError(e);

}
} else {
((PgResultSet) generatedKeys).addRows(tuples);
}
}
}

public void handleCommandStatus(String status, int updateCount, long insertOID) {
if (resultIndex >= updateCounts.length)
{
handleError(new PSQLException(GT.tr("Too many update results were returned."),
PSQLState.TOO_MANY_RESULTS));
return ;
}

updateCounts[resultIndex++] = updateCount;
}

public void handleWarning(SQLWarning warning) {
pgStatement.addWarning(warning);
}

public void handleError(SQLException newError) {
if (batchException == null)
{
int[] successCounts;

if (resultIndex >= updateCounts.length)
successCounts = updateCounts;
else
{
successCounts = new int[resultIndex];
System.arraycopy(updateCounts, 0, successCounts, 0, resultIndex);
}

String queryString = "<unknown>";
if (resultIndex < queries.length)
queryString = queries[resultIndex].toString(parameterLists[resultIndex]);

batchException = new BatchUpdateException(GT.tr("Batch entry {0} {1} was aborted. Call getNextException to see the cause.",
new Object[]{resultIndex,
queryString}),
newError.getSQLState(),
successCounts);
}

batchException.setNextException(newError);
}

public void handleCompletion() throws SQLException {
if (batchException != null)
throw batchException;
}

public ResultSet getGeneratedKeys() {
return generatedKeys;
}
}
@@ -0,0 +1,21 @@
package org.postgresql.jdbc;

import org.postgresql.core.*;
import org.postgresql.util.GT;
import org.postgresql.util.PSQLException;
import org.postgresql.util.PSQLState;

import java.sql.*;
import java.util.List;

class CallableBatchResultHandler extends BatchResultHandler
{
CallableBatchResultHandler(PgStatement statement, Query[] queries, ParameterList[] parameterLists, int[] updateCounts) {
super(statement, queries, parameterLists, updateCounts, false);
}

public void handleResultRows(Query fromQuery, Field[] fields, List tuples, ResultCursor cursor)
{
/* ignore */
}
}

0 comments on commit 8fca8b4

Please sign in to comment.
You can’t perform that action at this time.