Added Batch Execution for PreperadeStatments

This commit is contained in:
Ziver Koc 2015-12-08 22:24:48 +01:00
parent 3f3333a72d
commit c33ce0e60f
3 changed files with 42 additions and 18 deletions

BIN
Zutil.jar

Binary file not shown.

View file

@ -207,10 +207,10 @@ public class DBConnection implements Closeable{
} }
/** /**
* Executes an query and cleans up after itself. * Executes a query and cleans up after itself.
* *
* @param stmt is the query * @param stmt is the query to run
* @param handler is the result handler * @param handler is the result handler that will be called with the output of the execution
* @return the object from the handler * @return the object from the handler
*/ */
public static <T> T exec(PreparedStatement stmt, SQLResultHandler<T> handler) throws SQLException{ public static <T> T exec(PreparedStatement stmt, SQLResultHandler<T> handler) throws SQLException{
@ -228,28 +228,28 @@ public class DBConnection implements Closeable{
} }
else else
return null; return null;
}catch(SQLException sqlex){ }catch(SQLException e){
logger.log(Level.WARNING, null, sqlex); logger.log(Level.WARNING, null, e);
}finally{ }finally{
if(result != null){ if(result != null){
try { try {
result.close(); result.close();
} catch (SQLException sqlex) { } catch (SQLException e) {
logger.log(Level.WARNING, null, sqlex); logger.log(Level.WARNING, null, e);
} }
result = null; result = null;
} }
} }
} }
}catch(SQLException sqlex){ }catch(SQLException e){
logger.log(Level.WARNING, null, sqlex); logger.log(Level.WARNING, null, e);
// Cleanup // Cleanup
} finally { } finally {
if (stmt != null) { if (stmt != null) {
try { try {
stmt.close(); stmt.close();
} catch (SQLException sqlex) { } catch (SQLException e) {
logger.log(Level.WARNING, null, sqlex); logger.log(Level.WARNING, null, e);
} }
stmt = null; stmt = null;
} }
@ -257,6 +257,32 @@ public class DBConnection implements Closeable{
return null; return null;
} }
/**
* Executes an query and cleans up after itself.
*
* @param stmt is the query to run
* @return a array of ints representing the number of updates for each batch statements
*/
public static int[] execBatch(PreparedStatement stmt) throws SQLException{
try{
// Execute
return stmt.executeBatch();
}catch(SQLException e){
logger.log(Level.WARNING, null, e);
// Cleanup
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
logger.log(Level.WARNING, null, e);
}
stmt = null;
}
}
return new int[0];
}
/** /**
* Sets the pool that this connection belongs to * Sets the pool that this connection belongs to
* *

View file

@ -107,17 +107,15 @@ public class DBUpgradeHandler {
List<String> refTables = reference.exec("SELECT name FROM sqlite_master WHERE type='table';", new ListSQLResult<String>()); List<String> refTables = reference.exec("SELECT name FROM sqlite_master WHERE type='table';", new ListSQLResult<String>());
List<String> targetTables = reference.exec("SELECT name FROM sqlite_master WHERE type='table';", new ListSQLResult<String>()); List<String> targetTables = reference.exec("SELECT name FROM sqlite_master WHERE type='table';", new ListSQLResult<String>());
PreparedStatement stmt;
for(String table : targetTables){ for(String table : targetTables){
if(refTables.contains(table)){ if(refTables.contains(table)){
// Get reference structure // Get reference structure
PreparedStatement stmt = reference.getPreparedStatement("PRAGMA table_info(?)"); List<DBColumn> refStruct = reference.exec("PRAGMA table_info("+table+")",
stmt.setString(1, table); new TableStructureResultHandler());
List<DBColumn> refStruct = DBConnection.exec(stmt, new TableStructureResultHandler());
// Get target structure // Get target structure
stmt = target.getPreparedStatement("PRAGMA table_info(?)"); List<DBColumn> targetStruct = target.exec("PRAGMA table_info("+table+")",
stmt.setString(1, table); new TableStructureResultHandler());
List<DBColumn> targetStruct = DBConnection.exec(stmt, new TableStructureResultHandler());
// Check existing columns // Check existing columns
for(DBColumn column : refStruct) { for(DBColumn column : refStruct) {