Пример #1
0
 /// <summary>
 /// Создать Состояние секции бд.
 /// </summary>
 /// <param name="dbState">Состояние бд. </param>
 /// <param name="dbSectionStatus">Статус секции бд.</param>
 /// <param name="message">Сообщение.</param>
 /// <param name="canResolve">Можно ли разрешить конфликт бд.</param>
 /// <param name="sectionVersion">Версия секции.</param>
 public DbSectionState(DbState dbState, DbSectionStatus dbSectionStatus, ScoutVersion? sectionVersion = null, bool canResolve = false, string message = null)
     : this()
 {
     DbState = dbState;
     DbSectionStatus = dbSectionStatus;
     Message = message;
     CanResolve = canResolve;
     SectionVersion = sectionVersion;
 }
        public DbState CheckDbState(ScoutVersion requiredVersion, bool newerVersionIsValid = false)
        {
            try
            {
                if (!IsDbExist(_connectionString))
                    return new DbState(DbStatus.NotExists, message: "БД не существует.");


                using (var connection = GetConnection(_connectionString))
                {
                    connection.Open();

                    var checkDbTablesCommand = connection.CreateCommand();
                    checkDbTablesCommand.CommandText = string.Format(CheckDbTablesCommandFormat, "DbVersion");
                    checkDbTablesCommand.CommandType = CommandType.Text;

                    using (var reader = checkDbTablesCommand.ExecuteReader())
                    {
                        if (reader == null || !reader.Read())
                            return new DbState(DbStatus.NoSchema, message: "Схема бд невалидна.");
                    }

                    var versionInfoCommand = connection.CreateCommand();
                    versionInfoCommand.CommandText = GetDbVersionCommand;
                    versionInfoCommand.CommandType = CommandType.Text;

                    using (var reader = versionInfoCommand.ExecuteReader())
                    {
                        if (reader != null && reader.Read())
                        {
                            var currentDbVersion = new ScoutVersion(new Version(reader.GetString(0)));
                            var dbState = GetDbState(requiredVersion, currentDbVersion, newerVersionIsValid);
                            return dbState;
                        }
                        return new DbState(DbStatus.Unknown, message: "Весрию бд не удалось определить.");
                    }
                }
            }
            catch (SqlException ex)
            {
                return GetDbstateForError(ex);
            }
            catch (TimeoutException)
            {
                return new DbState(DbStatus.NotAvailable);
            }
            catch (InvalidOperationException)
            {
                return new DbState(DbStatus.NotAvailable);
            }
            catch (Exception ex)
            {
                LogManager.ExceptionLog.ErrorException(ex);
                return new DbState(DbStatus.Unknown);
            }
        }
Пример #3
0
 private static DbScript ExtractCreateScript(string createScriptName, 
                                             Assembly packageAssembly, 
                                             IReadOnlyDictionary<string, string> replaceAliases )
 {
     if (createScriptName == null) return null;
     var stringVersion = createScriptName.Split(new[] { '_' })[1].Replace(".sql", string.Empty);
     var version = Version.Parse(stringVersion);
     var scoutVersion = new ScoutVersion(version);
     var stream = packageAssembly.GetManifestResourceStream(createScriptName);
     string createScriptText = GetScript(stream, replaceAliases);
     return new DbScript(createScriptText, scoutVersion);
 }
Пример #4
0
 private DbSectionState GetNotExistDbSectionState(DbState dbState, ScoutVersion? requiredVersion)
 {
     return new DbSectionState(dbState, DbSectionStatus.NotExists, requiredVersion, true);
 }
Пример #5
0
        private DbSectionState GetSectionState(DbState dbState, ScoutVersion? currentSectionVersion, ScoutVersion? requiredVersion)
        {
            if (dbState.Status == DbStatus.Unknown) return GetDefaultDbSectionState();

            if (currentSectionVersion == null && requiredVersion != null)
            {
                return GetNotExistDbSectionState(dbState, requiredVersion);
            }

            if (currentSectionVersion == null || requiredVersion == null)
            {
                return new DbSectionState(dbState, DbSectionStatus.Error, currentSectionVersion, message: "Не удалось определить версию секции бд.");
            }

            try
            {
                if (currentSectionVersion.Value < requiredVersion.Value)
                {
                    return new DbSectionState(dbState, DbSectionStatus.Outdated, currentSectionVersion, true, "Версия секции базы данных ниже требуемой.");
                }
                if (currentSectionVersion.Value > requiredVersion.Value)
                {
                    return new DbSectionState(dbState, DbSectionStatus.Newer, currentSectionVersion, message: "Версия секции базы данных не совместима для работы расширения.");
                }
                return new DbSectionState(dbState, DbSectionStatus.Ok, currentSectionVersion, message: "Версия секции базы данных корректна");


            }
            catch (Exception)
            {
                return new DbSectionState(dbState, DbSectionStatus.Error, currentSectionVersion, message: "Не удалось определить версию секции бд.");
            }
        }
Пример #6
0
        private Dictionary<Guid, ScoutVersion> GetSectionsVersions()
        {
            try
            {
                using (var connection = new SqlConnection(_connectionString))
                {
                    connection.Open();

                    var sessionInfoCommand = new SqlCommand
                    {
                        CommandText = string.Format(GetSectionsInfoFormat, _dbName),
                        CommandType = CommandType.Text,
                        Connection = connection
                    };

                    using (var reader = sessionInfoCommand.ExecuteReader())
                    {
                        var sectionsInfo = new Dictionary<Guid, ScoutVersion>();
                        while (reader.Read())
                        {
                            var id = reader.GetGuid(0);
                            var version = new ScoutVersion(new Version(reader.GetString(1)));
                            sectionsInfo.Add(id, version);
                        }
                        return sectionsInfo;
                    }
                }
            }

            catch (Exception ex)
            {
                LogManager.ExceptionLog.Error(ex);
            }
            return new Dictionary<Guid, ScoutVersion>();
        }
Пример #7
0
 private string GetSetVersionCommand(Guid dbSectionId, ScoutVersion requiredVersion)
 {
     return String.Format(InsertSectionVersionFormat, _dbName, dbSectionId, requiredVersion);
 }
Пример #8
0
 private string GetUpdateVersionCommand(Guid dbSectionId, ScoutVersion requiredVersion)
 {
     return String.Format(UpdateSectionVersionFormat, _dbName, requiredVersion, dbSectionId);
 }
Пример #9
0
 private string GetUpdateVersionCommand(ScoutVersion requiredVersion, string dbName)
 {
     return String.Format(UpdateDbVersionCommandFormat, dbName, requiredVersion);
 }
Пример #10
0
 private static DbState GetDbState(ScoutVersion requieredVersion, ScoutVersion currentDbSectionVersion, bool newerVeresionIsValid = false)
 {
     try
     {
         if (currentDbSectionVersion.Version < requieredVersion.Version)
         {
             return new DbState(DbStatus.Outdated, currentDbSectionVersion, "Версия базы ниже требуемой.");
         }
         if ((currentDbSectionVersion.Version > requieredVersion.Version) && newerVeresionIsValid)
         {
             return new DbState(DbStatus.Invalid, currentDbSectionVersion, "Версия базы несовместима с требуемой версией.");
         }
         return new DbState(DbStatus.Ok, currentDbSectionVersion, "Версия базы корректна");
     }
     catch (Exception)
     {
         return new DbState(DbStatus.Unknown, message: "Весрию бд не удалось определить.");
     }
 }
Пример #11
0
        private static DbState GetDbState(ScoutVersion requieredVersion, ScoutVersion currentDbSectionVersion,
                                          bool newerVeresionIsValid = false)
        {

            if (currentDbSectionVersion.Version < requieredVersion.Version)
            {
                return new DbState(DbStatus.Outdated, currentDbSectionVersion, "Версия базы ниже требуемой.");
            }
            if ((currentDbSectionVersion.Version > requieredVersion.Version) && newerVeresionIsValid)
            {
                return new DbState(DbStatus.Invalid, currentDbSectionVersion,
                                   "Версия базы несовместима с требуемой версией.");
            }
            return new DbState(DbStatus.Ok, currentDbSectionVersion, "Версия базы корректна");
        }
Пример #12
0
 private static DbScript[] GetUpdateScripts(ScoutVersion requiredVersion, ScoutVersion? currentVersion, IEnumerable<DbScript> scripts)
 {
     var result = scripts.Where(dbScript => (currentVersion == null || dbScript.Version.Version > currentVersion.Value.Version) && dbScript.Version.Version <= requiredVersion.Version).ToList();
     result.Sort((x, y) => x.Version.CompareTo(y.Version));
     return result.ToArray();
 }
Пример #13
0
 public DbSectionInfo(Guid dbSectionId, ScoutVersion requiredVersion, DbScriptSet dbScripts)
 {
     DbSectionId = dbSectionId;
     RequiredVersion = requiredVersion;
     DbScripts = dbScripts;
 }
Пример #14
0
 /// <summary>
 /// Создать Sql скрипт.
 /// </summary>
 public DbScript(string sql, ScoutVersion version)
 {
     Sql = sql;
     Version = version;
 }