public void RemovesIdsThatNoLongerExist() { var source = Substitute.For <ISource>(); source.Name.Returns(new SourceName("SourceName")); var sourceData = Substitute.For <ISourceData>(); sourceData.Source.Returns(source); MediaItem.GetAllSourceData().Returns(new[] { sourceData }); PropertyMappings.Apply(Arg.Any <IEnumerable <object> >(), Arg.Any <MetadataResult <Series> >(), Arg.Any <Action <string> >()) .Returns(m => { var r = m.Arg <MetadataResult <Series> >(); r.Item.Name = "Name"; r.Item.ProviderIds = new Dictionary <string, string> { { "SourceName", "3" } }; return(r); }); var result = MediaItemType.CreateMetadataFoundResult(PluginConfiguration, MediaItem, new ConsoleLogManager()); result.IsRight.Should().BeTrue(); result.IfRight(r => { r.EmbyMetadataResult.Item.ProviderIds.Should().NotContainKey("SourceName"); r.EmbyMetadataResult.Item.ProviderIds.Should().NotContainValue("3"); }); }
public void Setup() { PropertyMappings = Substitute.For <IPropertyMappingCollection>(); PropertyMappings.Apply(Arg.Any <IEnumerable <object> >(), Arg.Any <MetadataResult <Series> >(), Arg.Any <Action <string> >()) .Returns(x => x.Arg <MetadataResult <Series> >()); PropertyMappings.GetEnumerator() .Returns(new IPropertyMapping[] { new PropertyMapping <AniDbSeriesData, MetadataResult <Series>, string>("Name", s => s.Item.Name, (s, t) => t.Item.Name = "Name", SourceNames.AniDb) }.AsEnumerable().GetEnumerator()); PluginConfiguration = Substitute.For <IPluginConfiguration>(); var embyItemData = Substitute.For <IEmbyItemData>(); embyItemData.Language.Returns("en"); _sources = new TestSources(); var aniDbSourceData = new SourceData <AniDbSeriesData>(_sources.AniDb, 33, new ItemIdentifier(33, 1, "Name"), new AniDbSeriesData()); MediaItem = Substitute.For <IMediaItem>(); MediaItem.GetAllSourceData().Returns(new ISourceData[] { aniDbSourceData }); MediaItem.GetDataFromSource(null).ReturnsForAnyArgs(aniDbSourceData); MediaItem.EmbyData.Returns(embyItemData); MediaItemType = new MediaItemType <Series>("Series", (c, l) => PropertyMappings); }
public async Task <int> BulkUpdateAsync(IQueryable <TEntity> queryable, TEntity updatedEntity, List <string> updateProperties) { var alias = $"u_{TableName}"; var qryAlias = $"q_{TableName}"; var qrySql = queryable.ToSql(out IReadOnlyList <SqlParameter> parameters); var propertyMappings = PropertyMappings .Where(p => updateProperties.Contains(p.PropertyName)) .Select(p => new { PropertyMapping = p, SqlParam = new SqlParameter($"@{TableName}_{p.PropertyName}", p.GetPropertyValue(updatedEntity) ?? DBNull.Value) }).ToList(); var sqlParams = new List <SqlParameter>(); sqlParams.AddRange(parameters.Select(p => new SqlParameter(p.ParameterName, p.Value))); sqlParams.AddRange(propertyMappings.Select(p => p.SqlParam)); var columnSql = string.Join(",", propertyMappings.Select(prop => $@" {alias}.{prop.PropertyMapping.ColumnName} = {prop.SqlParam.ParameterName}")); var sqlCmd = $@" UPDATE {alias} SET {columnSql} FROM dbo.{TableName} {alias} JOIN ( {qrySql} ) AS {qryAlias} ON {string.Join(@" AND", PrimaryKey.Keys.Select(k => $@" {alias}.{k.ColumnName} = {qryAlias}.{k.ColumnName}"))}"; return(await DbContext.Database.ExecuteSqlCommandAsync(sqlCmd, sqlParams)); }
public TransformQueryable(IQueryable source, TypeMappings typeMappings, PropertyMappings propertyMappings, SelectParameterMappings selectParameterMappings, ContainerClassMappings containerClassMappings, Expression currentExpression) { if (source == null) { throw new ArgumentNullException("source"); } if (typeMappings == null) { throw new ArgumentNullException("typeMappings"); } if (propertyMappings == null) { throw new ArgumentNullException("propertyMappings"); } if (selectParameterMappings == null) { throw new ArgumentNullException("selectParameterMappings"); } if (containerClassMappings == null) { throw new ArgumentNullException("containerClassFieldMappings"); } if (currentExpression == null) { throw new ArgumentNullException("currentExpression"); } _source = source; _typeMappings = typeMappings; _propertyMappings = propertyMappings; _selectParameterMappings = selectParameterMappings; _containerClassMappings = containerClassMappings; _currentExpression = currentExpression; }
public async Task BulkAddAsync(List <TEntity> entities) { var tableVar = $"_ToAdd_{typeof(TEntity).Name}"; var props = PropertyMappings.Where(p => !p.IsGenerated).ToList(); var columnsSql = $@"{string.Join(",", props.Select(p => $@" {p.ColumnName}"))}"; var deltaSql = string.Join(",", entities.Select(entity => { return($@" ({string.Join(", ", props.Select(m => $"{m.GetDbValue(entity)}"))})"); })); var sqlCmd = $@" CREATE TEMP TABLE {tableVar} ({string.Join(", ", props.Select(m => $"{m.ColumnName} {m.ColumnType}"))}); INSERT INTO {tableVar} VALUES {deltaSql}; INSERT INTO {TableName} ({columnsSql}) SELECT {columnsSql} FROM {tableVar} RETURNING {string.Join(",", PrimaryKey.Keys.Select(k => $@" {TableName}.{k.ColumnName}"))}"; await ExecuteSqlCommandAsync(sqlCmd, entities); }
public async Task <int> BulkUpdateAsync(IQueryable <TEntity> queryable, TEntity updatedEntity, List <string> updateProperties) { var alias = GetAlias(queryable); var qrySql = queryable .Select(x => 1) .ToSql(out IReadOnlyList <SqlParameter> parameters) .Substring(8) .TrimStart(); var propertyMappings = PropertyMappings .Where(p => updateProperties.Contains(p.PropertyName)) .Select(p => new { PropertyMapping = p, SqlParam = new SqlParameter($"@{TableName}_{p.PropertyName}", p.GetPropertyValue(updatedEntity) ?? DBNull.Value) }).ToList(); var sqlParams = new List <SqlParameter>(); sqlParams.AddRange(parameters.Select(p => new SqlParameter(p.ParameterName, p.Value))); sqlParams.AddRange(propertyMappings.Select(p => p.SqlParam)); var columnSql = string.Join(",", propertyMappings.Select(prop => $@" {alias}.{prop.PropertyMapping.ColumnName} = {prop.SqlParam.ParameterName}")); var sqlCmd = $@" UPDATE {alias} SET {columnSql} {qrySql}"; return(await DbContext.Database.ExecuteSqlRawAsync(sqlCmd, sqlParams)); }
public async Task BulkAddAsync(List <TEntity> entities) { var tableVar = "@ToAdd"; var props = PropertyMappings.Where(p => !p.IsGenerated).ToList(); var columnsSql = $@"{string.Join(",", props.Select(p => $@" {p.ColumnName}"))}"; var records = entities.Select(entity => { return($@" ({string.Join(", ", props.Select(m => $"{m.GetDbValue(entity)}"))})"); }); var sqlCmd = $@" DECLARE {tableVar} TABLE ({string.Join(", ", props.Select(m => $"{m.ColumnName} {m.ColumnType}"))}) SET NOCOUNT ON { CreateBatchInsertStatement($"INSERT INTO {tableVar} VALUES", records) } SET NOCOUNT OFF INSERT INTO dbo.{TableName} ({columnsSql}) OUTPUT {string.Join(",", PrimaryKey.Keys.Select(k => $@" inserted.{k.ColumnName}"))} SELECT {columnsSql} FROM {tableVar}"; await ExecuteSqlCommandAsync(sqlCmd, entities); }
protected override string ResolvePropertyName(string propertyName) { string resolvedName; var resolved = PropertyMappings.TryGetValue(propertyName, out resolvedName); return(resolved ? resolvedName : base.ResolvePropertyName(propertyName)); }
public List <T> ToList <T>(PropertyMappings mappings) where T : new() { List <T> collection = new List <T>(); FillCollection(collection, mappings); return(collection); }
public Collection <T> ToCollection <T>(PropertyMappings mappings) where T : new() { Collection <T> collection = new Collection <T>(); FillCollection(collection, mappings); return(collection); }
public void AppliesPropertyMappings() { MediaItemType.CreateMetadataFoundResult(PluginConfiguration, MediaItem, new ConsoleLogManager()); PropertyMappings.Received(1) .Apply(Arg.Any <IEnumerable <object> >(), Arg.Any <MetadataResult <Series> >(), Arg.Any <Action <string> >()); }
/// <summary> /// Adds the properties from the parent mapping to the sub class. /// </summary> /// <param name="parentMapping"></param> protected SubClassMap(ClassMap <TSuper> parentMapping) { var mapping = (ClassMap <TSuper>)parentMapping.PerformMapping(); foreach (var property in mapping.PropertyMappings) { PropertyMappings.Add(property); } }
public void NoNameMappingForLibrarySourceData_ReturnsFailure() { PropertyMappings.GetEnumerator() .Returns(Enumerable.Empty <IPropertyMapping>().GetEnumerator()); var result = MediaItemType.CreateMetadataFoundResult(PluginConfiguration, MediaItem, new ConsoleLogManager()); result.IsLeft.Should().BeTrue(); result.IfLeft(r => r.Reason.Should().Be("No value for Name property mapped from library source")); }
private ICollection <string> UpdateErrorsInternal([NotNull] string propertyName, [CanBeNull] IEnumerable validatorErrors) { Should.NotBeNull(propertyName, "propertyName"); IList <object> errors; if (validatorErrors is string) { errors = new object[] { validatorErrors } } ; else { errors = validatorErrors == null ? Empty.Array <object>() : validatorErrors.OfType <object>().ToArray(); } var hasErrors = errors.Count != 0; if (hasErrors && IgnoreProperties.Contains(propertyName)) { return(Empty.Array <string>()); } ICollection <string> mappingProperties; PropertyMappings.TryGetValue(propertyName, out mappingProperties); if (mappingProperties == null) { if (hasErrors) { _errors[propertyName] = errors; } else { _errors.Remove(propertyName); } return(new[] { propertyName }); } foreach (string property in mappingProperties) { if (hasErrors) { _errors[property] = errors; } else { _errors.Remove(property); } } return(mappingProperties); }
public Dictionary <string, PropertyMappingValue> GetPropertyMapping <TSource, TDestination>() { var matchingMapping = PropertyMappings.OfType <PropertyMapping <TSource, TDestination> >(); if (matchingMapping.Count() == 1) { return(matchingMapping.First().MappingDictionary); } throw new Exception($"Cannot find exact property mapping instance for <{typeof(TSource)}>"); }
public async Task <int> BulkUpdateAsync(IQueryable <TEntity> queryable, List <object[]> keys, List <string> updateProperties, Func <object[], TEntity> updateFunc) { ValidateCompositeKeys(keys); var alias = GetAlias(queryable); var qryAlias = $"q_{TableName}"; var deltaAlias = $"d_{TableName}"; var tableVar = "@Delta"; var qrySql = queryable .Select(x => 1) .ToSql(out IReadOnlyList <SqlParameter> parameters) .Substring(8) .TrimStart(); var fromClause = Regex.Match(qrySql, $"^FROM .* \\{alias}").Value; var whereClause = Regex.Replace(qrySql, $"^FROM .* \\{alias}", string.Empty); qrySql = $@" {fromClause} JOIN {tableVar} {deltaAlias} ON {string.Join(@" AND", PrimaryKey.Keys.Select(k => $@" {alias}.{k.ColumnName} = {deltaAlias}.{k.ColumnName}"))} {whereClause}"; var propertyMappings = PropertyMappings .Where(p => updateProperties.Contains(p.PropertyName)) .ToDictionary(x => x.PropertyName); var records = keys.Select(key => { var entity = updateFunc(key); return($@" ({string.Join(", ", key.Select(keyVal => $"{StringifyKeyVal(keyVal)}"))}, {string.Join(", ", propertyMappings.Select(m => $"{m.Value.GetDbValue(entity)}"))})"); }); var sqlCmd = $@" DECLARE {tableVar} TABLE ({string.Join(", ", PrimaryKey.Keys.Select(k => $"{k.ColumnName} {k.ColumnType}"))}, {string.Join(", ", propertyMappings.Select(m => $"{m.Value.ColumnName} {m.Value.ColumnType}"))}) SET NOCOUNT ON { CreateBatchInsertStatement($"INSERT INTO {tableVar} VALUES", records) } SET NOCOUNT OFF UPDATE {alias} SET {string.Join(",", updateProperties.Select(prop => $@" {alias}.{propertyMappings[prop].ColumnName} = {deltaAlias}.{propertyMappings[prop].ColumnName}"))} {qrySql}"; return(await DbContext.Database.ExecuteSqlRawAsync(sqlCmd, parameters.Select(p => new SqlParameter(p.ParameterName, p.Value)))); }
/// <summary> /// Converts the mapping to functional object mapping. /// </summary> /// <returns></returns> public override IObjectMapping ToObjectMapping() { return(IsForAnonymousType ? new AnonymousObjectMapping <T>(GetNamingContext(), PropertyMappings.Select(pm => pm.ToPropertyMapping()), GetObjectCategory(), IncludeObjectCategory, GetObjectClass(), IncludeObjectClasses) : base.ToObjectMapping()); }
public IPropertyMapping GetMappingForType(PropertyInfo info) { var mapping = PropertyMappings.FirstOrDefault(x => x.PropertyInfo.GetMethod == info.GetMethod); if (mapping == null) { mapping = new PropertyMapping(info); PropertyMappings.Add(mapping); } return(mapping); }
public void AppliesPropertyMappingsForIdentifierOnlySourceData() { var identifierOnlySourceData = new IdentifierOnlySourceData(_sources.AniDb, 33, new ItemIdentifier(33, 1, "Name")); MediaItem = Substitute.For <IMediaItem>(); MediaItem.GetAllSourceData().Returns(new ISourceData[] { identifierOnlySourceData }); MediaItemType.CreateMetadataFoundResult(PluginConfiguration, MediaItem, new ConsoleLogManager()); PropertyMappings.Received(1) .Apply(Arg.Is <IEnumerable <object> >(e => e.Contains(identifierOnlySourceData)), Arg.Any <MetadataResult <Series> >(), Arg.Any <Action <string> >()); }
private IPropertyMapping DefineMapping(LambdaExpression lambda) { var info = (PropertyInfo)GetMemberInfo(lambda); var mapping = PropertyMappings.FirstOrDefault(x => x.PropertyInfo.GetMethod == info.GetMethod); if (mapping == null) { mapping = new PropertyMapping(info); PropertyMappings.Add(mapping); } return(mapping); }
protected void AddPropertyMapping <TViewModel>([NotNull] Func <Expression <Func <TViewModel, object> > > viewModelProperty, [NotNull] Func <Expression <Func <T, object> > > modelProperty) { var vmProperty = viewModelProperty.GetMemberName(); var mProperty = modelProperty.GetMemberName(); ICollection <string> value; if (!PropertyMappings.TryGetValue(vmProperty, out value)) { value = new HashSet <string>(); PropertyMappings[vmProperty] = value; } value.Add(mProperty); }
public async Task <int> BulkMergeAsync(IQueryable <TEntity> queryable, List <TEntity> current) { var deltaSql = string.Join(",", current.Select(entity => { return($@" ({string.Join(", ", PropertyMappings.Select(m => $"{m.GetDbValue(entity)}"))})"); })); var qrySql = queryable.ToSql(out IReadOnlyList <SqlParameter> parameters); var updateColumnSql = string.Join(",", PropertyMappings.Where(p => !p.IsGenerated).Select(p => $@" TARGET.{p.ColumnName} = SOURCE.{p.ColumnName}")); var insertColumnSql = string.Join(",", PropertyMappings.Where(p => !p.IsGenerated).Select(p => $@" {p.ColumnName}")); var insertSourceColumnSql = string.Join(",", PropertyMappings.Where(p => !p.IsGenerated).Select(p => $@" SOURCE.{p.ColumnName}")); var sqlCmd = $@" DECLARE @merge TABLE ({string.Join(", ", PropertyMappings.Select(m => $"{m.ColumnName} {m.ColumnType}"))}) SET NOCOUNT ON INSERT INTO @merge VALUES {deltaSql} SET NOCOUNT OFF ; WITH tgt_cte AS ( {qrySql} ) MERGE tgt_cte AS TARGET USING @merge AS SOURCE ON ( {string.Join(" AND ", PrimaryKey.Keys.Select(k => $@"TARGET.{k.ColumnName} = SOURCE.{k.ColumnName}"))} ) WHEN MATCHED THEN UPDATE SET {updateColumnSql} WHEN NOT MATCHED BY TARGET THEN INSERT ({insertColumnSql}) VALUES ({insertSourceColumnSql}) WHEN NOT MATCHED BY SOURCE THEN DELETE OUTPUT $action AS action, {string.Join(",", PrimaryKey.Keys.Select(k => $@" inserted.{k.ColumnName}"))};"; return(await ExecuteSqlCommandAsync(sqlCmd, current, parameters.Select(p => new SqlParameter(p.ParameterName, p.Value)))); }
/// <summary> /// Adds a property mapping to the <see cref="ValidatableViewModel.PropertyMappings" /> dictionary. /// </summary> protected void AddPropertyMapping <T1, T2>([NotNull] Expression <Func <T1> > viewModelProperty, [NotNull] Expression <Func <T, T2> > modelProperty) { var vmProperty = viewModelProperty.GetMemberInfo().Name; var mProperty = modelProperty.GetMemberInfo().Name; ICollection <string> value; if (!PropertyMappings.TryGetValue(vmProperty, out value)) { value = new HashSet <string>(); PropertyMappings[vmProperty] = value; } value.Add(mProperty); }
public void NameNotMapped_ReturnsFailure(string name) { PropertyMappings.GetEnumerator() .Returns(new IPropertyMapping[] { new PropertyMapping <AniDbSeriesData, MetadataResult <Series>, string>("Name", s => s.Item.Name, (s, t) => t.Item.Name = name, SourceNames.AniDb) }.AsEnumerable().GetEnumerator()); var result = MediaItemType.CreateMetadataFoundResult(PluginConfiguration, MediaItem, new ConsoleLogManager()); result.IsLeft.Should().BeTrue(); result.IfLeft(r => r.Reason.Should().Be("Property mapping failed for the Name property")); }
private void FillCollection <T>(ICollection <T> collection, PropertyMappings mappings) where T : new() { using (DbCommand command = commandFactory.CreateCommand()) { IEntityAdapter adapter = new EntityAdapter(command) { MissingPropertyMappingAction = MissingPropertyMappingAction.Ignore }; if (mappings != null && mappings.Count > 0) { adapter.Options.Mappings.Add(mappings); } adapter.FillCollection(collection); } }
public void ConvertWithMappingCollection() { List <Customer> customers = GetCustomers(); PropertyMappings collection = new PropertyMappings(); collection.Add("Telephone", "Phone"); EntityConverter <Customer, Contact> converter = new EntityConverter <Customer, Contact>(collection); IList <Contact> contacts = converter.Convert(customers); Assert.AreEqual(100, contacts.Count); Assert.AreEqual("TestCustomer1", contacts[0].Name); Assert.AreEqual("TestAddress1", contacts[0].Address); Assert.AreEqual("555-1212", contacts[0].Phone); }
public async Task <int> BulkUpdateAsync(IQueryable <TEntity> queryable, List <object[]> keys, List <string> updateProperties, Func <object[], TEntity> updateFunc) { ValidateCompositeKeys(keys); var alias = $"u_{TableName}"; var qryAlias = $"q_{TableName}"; var deltaAlias = $"d_{TableName}"; var tableVar = $"_delta_{typeof(TEntity).Name}"; var qrySql = queryable.ToSql(out IReadOnlyList <NpgsqlParameter> parameters); var propertyMappings = PropertyMappings .Where(p => updateProperties.Contains(p.PropertyName)) .ToDictionary(x => x.PropertyName); var keyList = keys.ToList(); var deltaSql = string.Join(",", keyList.Select(key => { var entity = updateFunc(key); return($@" ({string.Join(", ", key.Select(keyVal => $"{StringifyKeyVal(keyVal)}"))}, {string.Join(", ", propertyMappings.Select(m => $"{m.Value.GetDbValue(entity)}"))})"); })); var sqlCmd = $@" CREATE TEMP TABLE {tableVar} ({string.Join(", ", PrimaryKey.Keys.Select(k => $"{k.ColumnName} {k.ColumnType}"))}, {string.Join(", ", propertyMappings.Select(m => $"{m.Value.ColumnName} {m.Value.ColumnType}"))}); INSERT INTO {tableVar} VALUES {deltaSql}; UPDATE {TableName} AS {alias} SET {string.Join(",", updateProperties.Select(prop => $@" {propertyMappings[prop].ColumnName} = {deltaAlias}.{propertyMappings[prop].ColumnName}"))} FROM ( {qrySql} ) AS {qryAlias} JOIN {tableVar} {deltaAlias} ON {string.Join(@" AND", PrimaryKey.Keys.Select(k => $@" {qryAlias}.{k.ColumnName} = {deltaAlias}.{k.ColumnName}"))} WHERE {string.Join(@" AND", PrimaryKey.Keys.Select(k => $@" {alias}.{k.ColumnName} = {qryAlias}.{k.ColumnName}"))}"; var count = await DbContext.Database.ExecuteSqlRawAsync(sqlCmd, parameters.Select(p => new NpgsqlParameter(p.ParameterName, p.Value))); return(count - keyList.Count); }
public FaultContractExceptionHandlerNode(FaultContractExceptionHandlerData data) { if (null == data) { throw new ArgumentNullException("data"); } Rename(data.Name); faultContractType = data.FaultContractType; exceptionMessage = data.ExceptionMessage; foreach (FaultContractExceptionHandlerMappingData mappingData in data.PropertyMappings) { FaultContractPropertyMapping mapping = new FaultContractPropertyMapping(); mapping.Name = mappingData.Name; mapping.Source = mappingData.Source; PropertyMappings.Add(mapping); } }
/// <summary> /// Returns the mapping for an element of this class. /// </summary> /// <param name="name">The name of the element, may include the type suffix for choice elements.</param> /// <returns></returns> public PropertyMapping FindMappedElementByName(string name) { if (name == null) { throw Error.ArgumentNull(nameof(name)); } var normalizedName = name.ToUpperInvariant(); if (_propMappings.TryGetValue(normalizedName, out PropertyMapping prop)) { return(prop); } // Not found, maybe a polymorphic name return(PropertyMappings.SingleOrDefault(p => p.MatchesSuffixedName(name))); }
private void FillCollection <T>(ICollection <T> collection, PropertyMappings mappings) where T : new() { DbCommand command = commandBase.CreateCommand(); commandBase.Session.EnlistConnection(command); IEntityAdapter adapter = new EntityAdapter(command) { MissingPropertyMappingAction = MissingPropertyMappingAction.Ignore }; if (mappings != null && mappings.Count > 0) { adapter.Options.Mappings.Add(mappings); } adapter.FillCollection(collection); }