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); } } } }
protected string GetHash(string sqlUnit) => HelperFns.ComputeHash(sqlUnit?.Trim(), HashAlgorithm);