Skip to content

Commit

Permalink
verifies num updated statements equals size of batch. properly remove…
Browse files Browse the repository at this point in the history
…s updateBy fields from update SET clause
  • Loading branch information
cwensel committed May 29, 2009
1 parent c165d1f commit f6149c5
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/java/cascading/jdbc/db/DBOutputFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -135,7 +138,15 @@ private void executeBatch() throws IOException
{
LOG.info( "executing update batch " + createBatchMessage( updateStatementsCurrent ) );

updateStatement.executeBatch();
int[] result = updateStatement.executeBatch();

int count = 0;

for( int value : result )
count += value;

if( count != updateStatementsCurrent )
throw new IOException( "update did not update same number of statements executed in batch, batch: " + updateStatementsCurrent + " updated: " + count );
}

updateStatementsCurrent = 0;
Expand Down Expand Up @@ -263,6 +274,9 @@ protected String constructUpdateQuery( String table, String[] fieldNames, String
if( fieldNames == null )
throw new IllegalArgumentException( "field names may not be null" );

Set<String> updateNamesSet = new HashSet<String>();
Collections.addAll( updateNamesSet, updateNames );

StringBuilder query = new StringBuilder();

query.append( "UPDATE " ).append( table );
Expand All @@ -271,13 +285,20 @@ protected String constructUpdateQuery( String table, String[] fieldNames, String

if( fieldNames.length > 0 && fieldNames[ 0 ] != null )
{
int count = 0;

for( int i = 0; i < fieldNames.length; i++ )
{
if( updateNamesSet.contains( fieldNames[ i ] ) )
continue;

if( count != 0 )
query.append( "," );

query.append( fieldNames[ i ] );
query.append( " = ?" );

if( i != fieldNames.length - 1 )
query.append( "," );
count++;
}
}

Expand Down

0 comments on commit f6149c5

Please sign in to comment.