public int Execute(SqlTransaction transaction)
{
int recordsAffectedTotal = 0;
SetNoCountOff(transaction);
// the following reg exp will be used to determine if each script is an
// INSERT, UPDATE, or DELETE operation. The reg exp is also only looking
// for these actions on the SubtextData database. <- do we need this last part?
string regextStr = @"(INSERT\sINTO\s[\s\w\d\)\(\,\.\]\[\>\<]+)|(UPDATE\s[\s\w\d\)\(\,\.\]\[\>\<]+SET\s)|(DELETE\s[\s\w\d\)\(\,\.\]\[\>\<]+FROM\s[\s\w\d\)\(\,\.\]\[\>\<]+WHERE\s)";
Regex regex = new Regex(regextStr, RegexOptions.IgnorePatternWhitespace | RegexOptions.IgnoreCase | RegexOptions.Compiled | RegexOptions.Multiline);
scripts.ApplyTemplatesToScripts();
foreach(Script script in scripts)
{
int returnValue = script.Execute(transaction);
Match match = regex.Match(script.ScriptText);
if (match.Success)
{
/*
* For UPDATE, INSERT, and DELETE statements, the return value is the
* number of rows affected by the command. For all other types of statements,
* the return value is -1. If a rollback occurs, the return value is also -1.
*/
if (!IsCrudScript(script))
continue;
if (returnValue > -1)
{
recordsAffectedTotal += returnValue;
}
else
{
throw new SqlScriptExecutionException("An error occurred while executing the script.", script, returnValue);
}
}
}
return recordsAffectedTotal;
}