private void InsertDefaultLanguages(IDbConnection conn, IDbTransaction tran)
        {
            var profiles          = GetLanguageProfiles(conn, tran);
            var languageConverter = new EmbeddedDocumentConverter(new LanguageIntConverter());

            foreach (var profile in profiles.OrderBy(p => p.Id))
            {
                using (IDbCommand insertNewLanguageProfileCmd = conn.CreateCommand())
                {
                    var itemsJson = languageConverter.ToDB(profile.Languages);
                    insertNewLanguageProfileCmd.Transaction = tran;
                    insertNewLanguageProfileCmd.CommandText = "INSERT INTO LanguageProfiles (Id, Name, Cutoff, Languages) VALUES (?, ?, ?, ?)";
                    insertNewLanguageProfileCmd.AddParameter(profile.Id);
                    insertNewLanguageProfileCmd.AddParameter(profile.Name);
                    insertNewLanguageProfileCmd.AddParameter(profile.Cutoff.Id);
                    insertNewLanguageProfileCmd.AddParameter(itemsJson);

                    insertNewLanguageProfileCmd.ExecuteNonQuery();
                }

                using (IDbCommand updateSeriesCmd = conn.CreateCommand())
                {
                    foreach (var profileId in profile.ProfileIds)
                    {
                        updateSeriesCmd.Transaction = tran;
                        updateSeriesCmd.CommandText = "UPDATE Series SET LanguageProfileId = ? WHERE ProfileId = ?";
                        updateSeriesCmd.AddParameter(profile.Id);
                        updateSeriesCmd.AddParameter(profileId);
                        updateSeriesCmd.ExecuteNonQuery();
                    }
                }
            }
        }
示例#2
0
        private void ConvertQualityModel(IDbConnection conn, IDbTransaction tran, string tableName)
        {
            var qualityModelConverter = new EmbeddedDocumentConverter(new QualityIntConverter());

            using (IDbCommand qualityModelCmd = conn.CreateCommand())
            {
                qualityModelCmd.Transaction = tran;
                qualityModelCmd.CommandText = @"SELECT Distinct Quality FROM " + tableName;
                using (IDataReader qualityModelReader = qualityModelCmd.ExecuteReader())
                {
                    while (qualityModelReader.Read())
                    {
                        var qualityJson = qualityModelReader.GetString(0);

                        SourceQualityModel036 sourceQuality;

                        if (!Json.TryDeserialize <SourceQualityModel036>(qualityJson, out sourceQuality))
                        {
                            continue;
                        }

                        var qualityNewJson = qualityModelConverter.ToDB(new DestinationQualityModel036
                        {
                            Quality = sourceQuality.Quality.Id,
                            Proper  = sourceQuality.Proper
                        });

                        using (IDbCommand updateCmd = conn.CreateCommand())
                        {
                            updateCmd.Transaction = tran;
                            updateCmd.CommandText = "UPDATE " + tableName + " SET Quality = ? WHERE Quality = ?";
                            updateCmd.AddParameter(qualityNewJson);
                            updateCmd.AddParameter(qualityJson);

                            updateCmd.ExecuteNonQuery();
                        }
                    }
                }
            }
        }
示例#3
0
        private void ConvertQualityProfiles(IDbConnection conn, IDbTransaction tran)
        {
            var qualityProfileItemConverter = new EmbeddedDocumentConverter(new QualityIntConverter());

            // Convert 'Allowed' column in QualityProfiles from Json List<object> to Json List<int> (int = Quality)
            using (IDbCommand qualityProfileCmd = conn.CreateCommand())
            {
                qualityProfileCmd.Transaction = tran;
                qualityProfileCmd.CommandText = @"SELECT Id, Allowed FROM QualityProfiles";
                using (IDataReader qualityProfileReader = qualityProfileCmd.ExecuteReader())
                {
                    while (qualityProfileReader.Read())
                    {
                        var id          = qualityProfileReader.GetInt32(0);
                        var allowedJson = qualityProfileReader.GetString(1);

                        var allowed = Json.Deserialize <List <Quality> >(allowedJson);

                        var items = Quality.DefaultQualityDefinitions.OrderBy(v => v.Weight).Select(v => new ProfileQualityItem {
                            Quality = v.Quality, Allowed = allowed.Contains(v.Quality)
                        }).ToList();

                        var allowedNewJson = qualityProfileItemConverter.ToDB(items);

                        using (IDbCommand updateCmd = conn.CreateCommand())
                        {
                            updateCmd.Transaction = tran;
                            updateCmd.CommandText = "UPDATE QualityProfiles SET Items = ? WHERE Id = ?";
                            updateCmd.AddParameter(allowedNewJson);
                            updateCmd.AddParameter(id);

                            updateCmd.ExecuteNonQuery();
                        }
                    }
                }
            }
        }
        private void UpdateLanguage(IDbConnection conn, IDbTransaction tran)
        {
            var LanguageConverter = new EmbeddedDocumentConverter(new LanguageIntConverter());

            var profileLanguages = new Dictionary <int, int>();

            using (IDbCommand getProfileCmd = conn.CreateCommand())
            {
                getProfileCmd.Transaction = tran;
                getProfileCmd.CommandText = "SELECT Id, Language FROM Profiles";

                IDataReader profilesReader = getProfileCmd.ExecuteReader();
                while (profilesReader.Read())
                {
                    var profileId       = profilesReader.GetInt32(0);
                    var episodeLanguage = Language.English.Id;
                    try
                    {
                        episodeLanguage = profilesReader.GetInt32(1);
                    }
                    catch (InvalidCastException e)
                    {
                        _logger.Debug("Language field not found in Profiles, using English as default." + e.Message);
                    }

                    profileLanguages[profileId] = episodeLanguage;
                }
            }

            var seriesLanguages = new Dictionary <int, int>();

            using (IDbCommand getSeriesCmd = conn.CreateCommand())
            {
                getSeriesCmd.Transaction = tran;
                getSeriesCmd.CommandText = @"SELECT Id, ProfileId FROM Series";
                using (IDataReader seriesReader = getSeriesCmd.ExecuteReader())
                {
                    while (seriesReader.Read())
                    {
                        var seriesId        = seriesReader.GetInt32(0);
                        var seriesProfileId = seriesReader.GetInt32(1);

                        seriesLanguages[seriesId] = profileLanguages.GetValueOrDefault(seriesProfileId, Language.English.Id);
                    }
                }
            }

            foreach (var group in seriesLanguages.GroupBy(v => v.Value, v => v.Key))
            {
                var languageJson = LanguageConverter.ToDB(Language.FindById(group.Key));

                var seriesIds = group.Select(v => v.ToString()).Join(",");

                using (IDbCommand updateEpisodeFilesCmd = conn.CreateCommand())
                {
                    updateEpisodeFilesCmd.Transaction = tran;
                    updateEpisodeFilesCmd.CommandText = $"UPDATE EpisodeFiles SET Language = ? WHERE SeriesId IN ({seriesIds})";
                    updateEpisodeFilesCmd.AddParameter(languageJson);

                    updateEpisodeFilesCmd.ExecuteNonQuery();
                }

                using (IDbCommand updateHistoryCmd = conn.CreateCommand())
                {
                    updateHistoryCmd.Transaction = tran;
                    updateHistoryCmd.CommandText = $"UPDATE History SET Language = ? WHERE SeriesId IN ({seriesIds})";
                    updateHistoryCmd.AddParameter(languageJson);

                    updateHistoryCmd.ExecuteNonQuery();
                }

                using (IDbCommand updateBlacklistCmd = conn.CreateCommand())
                {
                    updateBlacklistCmd.Transaction = tran;
                    updateBlacklistCmd.CommandText = $"UPDATE Blacklist SET Language = ? WHERE SeriesId IN ({seriesIds})";
                    updateBlacklistCmd.AddParameter(languageJson);

                    updateBlacklistCmd.ExecuteNonQuery();
                }
            }
        }