public void Run(ProgressReporter progressReporter = null, ErrorReporter errorReporter = null) { if (false == File.Exists(Config.BigSqlFilePath)) { throw new ArgumentException($"specified big sql file not exist: {Config.BigSqlFilePath}", nameof(Config.BigSqlFilePath)); } // reset the stop flag if (Stop) { return; } Stop = false; var sessionLevelDbPath = GetSessionLevelDbPath(Config.BigSqlFilePath); if (false == Config.ContinueFromLastSessionWhenStarted && Directory.Exists(sessionLevelDbPath)) { var sessionBackupPath = GetSessionBackupPath(sessionLevelDbPath); Directory.Move(sessionLevelDbPath, sessionBackupPath); } using (var streamReader = new StreamReader(Config.BigSqlFilePath)) using (var sessionDb = new SessionLevelDb(sessionLevelDbPath, Config.SessionSaveType)) { int sqlUnitIndex = 0; int affectedRows = 0; while (true) { // stop when requested if (Stop) { StopCallBack?.Invoke(); return; } // read from file var sqlUnitList = ReadBatchSqlUnits(streamReader, Config.BatchSize, Config.SqlUnitEndingLine, sessionDb, ref sqlUnitIndex); if (false == sqlUnitList.Any()) { break; } // prepare sql var batchSql = SqlUnit.CombineSqlUnitList(sqlUnitList); if (false == string.IsNullOrWhiteSpace(batchSql)) { // batch execute sql HelperFns.TryThenException( () => { var thisAffectedRows = RunSql(Config.ConnectionString, batchSql); if (thisAffectedRows > 0) { affectedRows += thisAffectedRows; } }, Config.RetryIntervalWhenError, true, Config.RetryNumberWhenError + 1, (e, i) => errorReporter?.Invoke($"{e.Message}; retry in {Config.RetryIntervalWhenError.TotalSeconds} seconds...") ); // set executing status foreach (var sqlUnit in sqlUnitList) { sessionDb.SetSqlUnitExecuteStatus(sqlUnit.Index, sqlUnit.Sql, true); } // report progress progressReporter?.Invoke(sqlUnitIndex, affectedRows); } } } }
public IEnumerable <SqlUnit> ReadBatchSqlUnits(TextReader textReader, int batchSize, string sqlUnitEndingLine, SessionLevelDb sessionDb, ref int sqlUnitIndex) { var unitList = new List <SqlUnit>(); for (int i = 0; i < batchSize;) { var thisUnit = ReadSqlUnit(textReader, sqlUnitEndingLine); if (null == thisUnit) { break; } var unit = new SqlUnit(sqlUnitIndex++, thisUnit); if (sessionDb.IsSqlUnitAlreadyExecuted(unit.Index, unit.Sql)) { continue; } unitList.Add(unit); i++; } return(unitList); }