private void executeSqlFiles(String[] pFiles, boolean pStopAtError)
throws RepositoryException {
// XXX: again with this bullshit
SQLProcessor sp = new SQLProcessor(getTransactionManager(), getDataSource());
boolean success = false;
TransactionDemarcation td = new TransactionDemarcation();
try {
td.begin(
(TransactionManager) Nucleus.getGlobalNucleus().resolveName(
"/atg/dynamo/transaction/TransactionManager"
)
);
// for sql server auto-commit must be true
// adamb: Hmm Marty added this, but it
// breaks against MSSQL 8
// if (getDatabaseType().equals(MICROSOFT))
// sp.setAutoCommit(true);
SQLFileParser parser = new SQLFileParser();
for (String file : pFiles) {
// switch the file path so everything is forward slashes
file = file.replace('\\', '/');
String cmd;
Iterator cmds;
if (isLoggingInfo()) {
logInfo("Executing SQL file: " + file);
}
if (!new File(file).exists()) {
throw new RepositoryException("SQL file " + file + " does not exist.");
}
// parse the file to get commands...
try {
Collection c = parser.parseSQLFile(file);
if (isLoggingDebug()) {
logDebug("Parsed " + c.size() + " SQL command(s) from file.");
}
cmds = c.iterator();
} catch (Exception e) {
// an error parsing the file indicates something very wrong, so bail
throw new RepositoryException(
"Error encountered parsing SQL file " + file, e
);
}
// then execute the commands...
while (cmds.hasNext()) {
cmd = (String) cmds.next();
if (cmd.trim().length() == 0) {
continue;
}
if (isLoggingDebug() || isLoggingCreateTables()) {
logDebug("Executing SQL cmd [" + cmd + "]");
}
try {
sp.executeSQL(cmd);
} catch (Exception e) {
if (pStopAtError) {
throw new RepositoryException(
"Error received executing command ["
+ cmd
+ "] from SQL file "
+ file, e
);
}
else {
if (isLoggingWarning()) {
logWarning(
"Error received executing command ["
+ cmd
+ "] from SQL file "
+ file
+ ": "
+ e.getMessage()
);
}
}
}
}
}
success = true;
} catch (TransactionDemarcationException e) {
logError(e);
} finally {
try {
td.end(!success);
} catch (TransactionDemarcationException e) {
logError(e);
}
}
}