/// <summary> /// Generates the parameters. /// </summary> /// <param name="queryObject">The query object.</param> /// <param name="property">The property.</param> /// <param name="propertyItem">The property item.</param> /// <returns>The parameters</returns> private IParameter[] GenerateParameters(TMappedClass queryObject, IManyToManyProperty property, object propertyItem) { var ItemList = propertyItem as IEnumerable; var ReturnValues = new List <IParameter>(); var ParentMappings = MappingInformation.GetChildMappings(property.ParentMapping.ObjectType).SelectMany(x => MappingInformation.GetParentMapping(x.ObjectType)).Distinct(); var ParentWithID = ParentMappings.FirstOrDefault(x => x.IDProperties.Count > 0); var Prefix = string.Empty; if (property.ForeignMapping.Any(TempMapping => ParentWithID == TempMapping)) { Prefix = "Parent_"; } var ParentIDs = ParentMappings.SelectMany(x => x.IDProperties); var ForeignIDs = MappingInformation.GetParentMapping(property.PropertyType).SelectMany(x => x.IDProperties); ReturnValues.AddRange(ParentIDs.ForEach <IIDProperty, IParameter>(x => { var Value = x.GetColumnInfo()[0].GetValue(queryObject); if (x.PropertyType == typeof(string)) { var TempParameter = Value as string; return(new StringParameter(Prefix + x.ParentMapping.TableName + x.ColumnName, TempParameter !)); } return(new Parameter <object>(Prefix + x.ParentMapping.TableName + x.ColumnName, x.PropertyType.To <Type, SqlDbType>(), Value)); })); return(ReturnValues.ToArray()); }
/// <summary> /// Copies the property. /// </summary> /// <param name="prop">The property.</param> public void CopyProperty(IManyToManyProperty prop) { if (prop is null) { return; } ManyToManyProperties.Add(prop.Convert <TClassType>(this)); }
/// <summary> /// Generates the query. /// </summary> /// <param name="queryObject">The object to generate the queries from.</param> /// <param name="property">The property.</param> /// <returns>The resulting query</returns> public override IQuery[] GenerateQueries(TMappedClass queryObject, IClassProperty property) { return(property switch { IMapProperty TempMapProperty => MapProperty(TempMapProperty, queryObject), IManyToManyProperty Property => ManyToManyProperty(Property, queryObject), IManyToOneListProperty ManyToOne => ManyToOneProperty(ManyToOne, queryObject), IManyToOneProperty ManyToOne => ManyToOneProperty(ManyToOne, queryObject), _ => Array.Empty <IQuery>(), });
/// <summary> /// Manies to many property. /// </summary> /// <param name="property">The property.</param> /// <param name="queryObject">The query object.</param> /// <returns>The queries</returns> private IQuery[] ManyToManyProperty(IManyToManyProperty property, TMappedClass queryObject) { var ItemList = property.GetValue(queryObject) as IEnumerable; var ForeignIDProperties = MappingInformation.GetChildMappings(property.PropertyType) .SelectMany(x => MappingInformation.GetParentMapping(x.ObjectType)) .Distinct() .SelectMany(x => x.IDProperties); return(new IQuery[] { new Query(property.PropertyType, CommandType.Text, GenerateJoinDeleteQuery(property), QueryType, GenerateParameters(queryObject, property, ItemList !)) });
/// <summary> /// Generates the join delete query. /// </summary> /// <param name="property">The property.</param> /// <returns>The join delete query</returns> private string GenerateJoinDeleteQuery(IManyToManyProperty property) { var Builder = ObjectPool.Get(); var PropertyNames = ObjectPool.Get(); var PropertyValues = ObjectPool.Get(); var ParametersList = ObjectPool.Get(); var ParentMappings = MappingInformation.GetChildMappings(property.ParentMapping.ObjectType).SelectMany(x => MappingInformation.GetParentMapping(x.ObjectType)).Distinct(); var ParentWithID = ParentMappings.FirstOrDefault(x => x.IDProperties.Count > 0); var Prefix = string.Empty; if (property.ForeignMapping.Any(TempMapping => ParentWithID == TempMapping)) { Prefix = "Parent_"; } var Splitter2 = string.Empty; foreach (var IDProperty in IDProperties) { ParametersList.Append(Splitter2).Append("([").Append(property.ParentMapping.SchemaName).Append("].[").Append(property.TableName).Append("].[").Append(Prefix).Append(IDProperty.ParentMapping.TableName).Append(IDProperty.ColumnName).Append("] = @").Append(Prefix).Append(IDProperty.ParentMapping.TableName).Append(IDProperty.ColumnName); if (!string.IsNullOrEmpty(Prefix)) { ParametersList.Append(Splitter2).Append(" OR [").Append(property.ParentMapping.SchemaName).Append("].[").Append(property.TableName).Append("].[").Append(IDProperty.ParentMapping.TableName).Append(IDProperty.ColumnName).Append("] = @").Append(Prefix).Append(IDProperty.ParentMapping.TableName).Append(IDProperty.ColumnName); } ParametersList.Append(")"); Splitter2 = " AND "; } Builder.Append("DELETE FROM ").Append(GetTableName(property)).Append(" WHERE ").Append(ParametersList).Append(";"); var Result = Builder.ToString(); ObjectPool.Return(Builder); ObjectPool.Return(PropertyNames); ObjectPool.Return(PropertyValues); ObjectPool.Return(ParametersList); return(Result); }
/// <summary> /// Deletes the joins if it needs to. /// </summary> /// <param name="object">The object.</param> /// <param name="source">The source.</param> /// <param name="batch">The batch.</param> /// <param name="ManyToManyProperty">The many to many property.</param> /// <param name="ManyToManyValueList">The many to many value list.</param> private void DeleteJoins(object @object, IMappingSource source, SQLHelper batch, IManyToManyProperty ManyToManyProperty, IList ManyToManyValueList) { if (ManyToManyProperty.DatabaseJoinsCascade) { return; } ManyToManyValueList.Clear(); var LinksGenerator = QueryProviderManager.CreateGenerator(ManyToManyProperty.ParentMapping.ObjectType, source); var TempQueries = LinksGenerator.GenerateQueries(QueryType.JoinsDelete, @object, ManyToManyProperty); for (int x = 0, TempQueriesLength = TempQueries.Length; x < TempQueriesLength; ++x) { var TempQuery = TempQueries[x]; batch.AddQuery(TempQuery.DatabaseCommandType, TempQuery.QueryString, TempQuery.Parameters !); } }
/// <summary> /// Checks if the properties are similar to one another /// </summary> /// <param name="secondProperty">The second property.</param> /// <returns>True if they are similar, false otherwise</returns> public bool Similar(IManyToManyProperty secondProperty) { return(!(secondProperty is null) && secondProperty.TableName == TableName && secondProperty.Name == Name); }
/// <summary> /// Gets the name of the table. /// </summary> /// <param name="property">The property.</param> /// <returns>The name of the table.</returns> protected string GetTableName(IManyToManyProperty property) => $"[{property?.ParentMapping.SchemaName}].[{property.TableName}]";