private void UpdateStatus(UpdateScriptRegisteredData script) { if (!script.Status.HasValue || string.IsNullOrEmpty(_settings.ScriptsDbTable.UpdateStatusField)) { return; } string statusText = string.Format("{0} = @{0}", _settings.ScriptsDbTable.UpdateStatusField); string updateTemplate = " Update {0} SET {1} where {2} = '{3}'"; string commandText = string.Format(updateTemplate, _settings.ScriptsDbTable.TableName, statusText, _settings.ScriptsDbTable.XmlMappedData.FileField, script.File); using (SqlConnection conn = new SqlConnection(_settings.ConnectionString)) { conn.Open(); SqlCommand command = new SqlCommand(commandText, conn); command.Parameters.AddWithValue("@" + _settings.ScriptsDbTable.UpdateStatusField, GetDbStatusValueByEnum(script.Status.Value)); command.ExecuteNonQuery(); command.Dispose(); } RaiseUpdateProgerss(String.Format("{0}, status updated to :{1}", script.File, script.Status)); }
private void RunScript(UpdateScript script) { UpdateScriptRegisteredData inDb = null; bool registerBeforeRun = false; try { inDb = GetRegistedScriptData(script.DoNotStoreFileExtensionInDatabase, script.File); if (!IsNeedToRun(inDb)) { return; } script.Status = UpdateScriptStatus.InProgress; registerBeforeRun = ShouldRegisterBeforeExecution(); if (registerBeforeRun) { if (inDb == null) { AddNewScript(script); } else { inDb.Status = UpdateScriptStatus.InProgress; UpdateStatus(inDb); } } ExecuteScript(script); inDb = GetRegistedScriptData(script.DoNotStoreFileExtensionInDatabase, script.File); script.Status = UpdateScriptStatus.Ok; if (inDb == null) { AddNewScript(script); } else { UpdateScriptInfo(script); } } catch (Exception ex) { var message = String.Format("Script execution file: {0} failed. {1}", script.FullFileName, ex.Message); var error = new ApplicationException(message, ex); script.Status = UpdateScriptStatus.Error; if (registerBeforeRun && (inDb != null)) { UpdateScriptInfo(script); } throw error; } }
private bool IsNeedToRun(UpdateScriptRegisteredData inDb) { if (inDb == null) { return(true); } if (inDb.Status.HasValue && inDb.Status != UpdateScriptStatus.Ok) { return(true); } return(false); }
private UpdateScriptRegisteredData GetRegistedScriptData(bool donotStoreFileExtension, string name) { UpdateScriptRegisteredData registeredData = null; string registeredName = GetRegisteredNameByScriptName(donotStoreFileExtension, name); string selectTemplate = "{0} As FileName, {1} as ModifiedDate, {2} as Status"; string dateTemplate = "NULL"; string statusTemplate = "NULL"; if (!(string.IsNullOrEmpty(_settings.ScriptsDbTable.UpdateStatusField))) { statusTemplate = _settings.ScriptsDbTable.UpdateStatusField; } selectTemplate = string.Format(selectTemplate, _settings.ScriptsDbTable.XmlMappedData.FileField, dateTemplate, statusTemplate); string commandText = string.Format(" SELECT {0} FROM {1} where {2} = '{3}' ", selectTemplate, _settings.ScriptsDbTable.TableName, _settings.ScriptsDbTable.XmlMappedData.FileField, registeredName); using (SqlConnection conn = new SqlConnection(_settings.ConnectionString)) { conn.Open(); SqlCommand command = new SqlCommand(commandText, conn); SqlDataReader reader = command.ExecuteReader(); if (reader.Read()) { registeredData = new UpdateScriptRegisteredData(); registeredData.File = reader.GetString(0); object dataObj = reader.GetValue(1); if (dataObj != null && dataObj != DBNull.Value) { registeredData.ModifiedDate = (DateTime)dataObj; } object statusObj = reader.GetValue(2); if (statusObj != null && statusObj != DBNull.Value) { registeredData.Status = GetStatusEnum(statusObj); } } reader.Close(); } return(registeredData); }
private bool IsNeedToRun(UpdateScriptRegisteredData inDb) { if (inDb == null) return true; if (inDb.Status.HasValue && inDb.Status != UpdateScriptStatus.Ok) return true; return false; }
private UpdateScriptRegisteredData GetRegistedScriptData(bool donotStoreFileExtension, string name) { UpdateScriptRegisteredData registeredData = null; string registeredName = GetRegisteredNameByScriptName(donotStoreFileExtension, name); string selectTemplate = "{0} As FileName, {1} as ModifiedDate, {2} as Status"; string dateTemplate = "NULL"; string statusTemplate = "NULL"; if (!(string.IsNullOrEmpty(_settings.ScriptsDbTable.UpdateStatusField))) statusTemplate = _settings.ScriptsDbTable.UpdateStatusField; selectTemplate = string.Format(selectTemplate, _settings.ScriptsDbTable.XmlMappedData.FileField, dateTemplate, statusTemplate); string commandText = string.Format(" SELECT {0} FROM {1} where {2} = '{3}' ", selectTemplate, _settings.ScriptsDbTable.TableName, _settings.ScriptsDbTable.XmlMappedData.FileField, registeredName); using (SqlConnection conn = new SqlConnection(_settings.ConnectionString)) { conn.Open(); SqlCommand command = new SqlCommand(commandText, conn); SqlDataReader reader = command.ExecuteReader(); if (reader.Read()) { registeredData = new UpdateScriptRegisteredData(); registeredData.File = reader.GetString(0); object dataObj = reader.GetValue(1); if (dataObj != null && dataObj != DBNull.Value) registeredData.ModifiedDate = (DateTime)dataObj; object statusObj = reader.GetValue(2); if (statusObj != null && statusObj != DBNull.Value) { registeredData.Status = GetStatusEnum(statusObj); } } reader.Close(); } return registeredData; }