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 handler is the result handler
* @param stmt is the query to run
* @param handler is the result handler that will be called with the output of the execution
* @return the object from the handler
*/
public static <T> T exec(PreparedStatement stmt, SQLResultHandler<T> handler) throws SQLException{
@ -228,28 +228,28 @@ public class DBConnection implements Closeable{
}
else
return null;
}catch(SQLException sqlex){
logger.log(Level.WARNING, null, sqlex);
}catch(SQLException e){
logger.log(Level.WARNING, null, e);
}finally{
if(result != null){
try {
result.close();
} catch (SQLException sqlex) {
logger.log(Level.WARNING, null, sqlex);
} catch (SQLException e) {
logger.log(Level.WARNING, null, e);
}
result = null;
}
}
}
}catch(SQLException sqlex){
logger.log(Level.WARNING, null, sqlex);
}catch(SQLException e){
logger.log(Level.WARNING, null, e);
// Cleanup
} finally {
if (stmt != null) {
try {
stmt.close();
} catch (SQLException sqlex) {
logger.log(Level.WARNING, null, sqlex);
} catch (SQLException e) {
logger.log(Level.WARNING, null, e);
}
stmt = null;
}
@ -257,6 +257,32 @@ public class DBConnection implements Closeable{
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
*

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