Permalink
Browse files

verifies num updated statements equals size of batch. properly remove…

…s updateBy fields from update SET clause
  • Loading branch information...
1 parent c165d1f commit f6149c5ffd4cafe61e805a4671d39d5c0218b94c @cwensel committed May 29, 2009
Showing with 24 additions and 3 deletions.
  1. +24 −3 src/java/cascading/jdbc/db/DBOutputFormat.java
@@ -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;
@@ -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;
@@ -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 );
@@ -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++;
}
}

0 comments on commit f6149c5

Please sign in to comment.