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 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(); } } } } }
private static void RegisterEmbeddedConverter() { var embeddedTypes = typeof(IEmbeddedDocument).Assembly.ImplementationsOf<IEmbeddedDocument>(); var embeddedConvertor = new EmbeddedDocumentConverter(); var genericListDefinition = typeof(List<>).GetGenericTypeDefinition(); foreach (var embeddedType in embeddedTypes) { var embeddedListType = genericListDefinition.MakeGenericType(embeddedType); MapRepository.Instance.RegisterTypeConverter(embeddedType, embeddedConvertor); MapRepository.Instance.RegisterTypeConverter(embeddedListType, embeddedConvertor); } }