/// <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> /// Generates the insert query declarations. /// </summary> /// <returns>The resulting query</returns> private string[] GenerateInsertQueryDeclarations() { var Builder = new List <string>(); var ParentMappings = new List <IMapping>(); foreach (var ChildMapping in MappingInformation.GetChildMappings(typeof(TMappedClass))) { var TempParentMappings = MappingInformation.GetParentMapping(ChildMapping.ObjectType); ParentMappings.AddIfUnique(TempParentMappings); } for (int x = 0, ParentMappingsCount = ParentMappings.Count; x < ParentMappingsCount; x++) { var ParentMapping = ParentMappings[x]; //ID Properties to pass to the next set of queries foreach (var IDProperty in ParentMapping.IDProperties) { Builder.Add("DECLARE " + GetParentParameterName(IDProperty) + " AS " + GetParameterType(IDProperty) + ";"); } //Auto ID properties to pass to the next set of queries foreach (var AutoIDProperty in ParentMapping.AutoIDProperties) { Builder.Add("DECLARE " + GetParentParameterName(AutoIDProperty) + " AS " + GetParameterType(AutoIDProperty) + ";"); } } return(Builder.ToArray()); }
/// <summary> /// Initializes a new instance of the <see cref="SavePropertiesQuery{TMappedClass}"/> class. /// </summary> /// <param name="mappingInformation">Mapping information</param> /// <param name="objectPool">The object pool.</param> public DeletePropertiesQuery(IMappingSource mappingInformation, ObjectPool <StringBuilder> objectPool) : base(mappingInformation, objectPool) { IDProperties = MappingInformation.GetChildMappings(typeof(TMappedClass)) .SelectMany(x => MappingInformation.GetParentMapping(x.ObjectType)) .Distinct() .SelectMany(x => x.IDProperties); }
/// <summary> /// Initializes a new instance of the <see cref="DeleteQuery{TMappedClass}"/> class. /// </summary> /// <param name="mappingInformation">The mapping information.</param> /// <param name="objectPool">The object pool.</param> public DeleteQuery(IMappingSource mappingInformation, ObjectPool <StringBuilder> objectPool) : base(mappingInformation, objectPool) { ParentMappings = MappingInformation.GetChildMappings <TMappedClass>() .SelectMany(x => MappingInformation.GetParentMapping(x.ObjectType)) .Distinct(); IDProperties = ParentMappings.SelectMany(x => x.IDProperties); GenerateQuery(); }
/// <summary> /// Initializes a new instance of the <see cref="DataLoadQuery{TMappedClass}"/> class. /// </summary> /// <param name="mappingInformation">Mapping information</param> /// <param name="objectPool">The object pool.</param> public DataLoadQuery(IMappingSource mappingInformation, ObjectPool <StringBuilder> objectPool) : base(mappingInformation, objectPool) { IDProperties = MappingInformation.GetChildMappings(MappedClassType) .SelectMany(x => MappingInformation.GetParentMapping(x.ObjectType)) .Distinct() .SelectMany(x => x.IDProperties) .ToArray(); IDColumnInfo = IDProperties.Select(x => x.GetColumnInfo()[0]).ToArray(); }
/// <summary> /// Initializes a new instance of the <see cref="SavePropertiesQuery{TMappedClass}"/> class. /// </summary> /// <param name="mappingInformation">Mapping information</param> /// <param name="objectPool">The object pool.</param> public SavePropertiesQuery(IMappingSource mappingInformation, ObjectPool <StringBuilder> objectPool) : base(mappingInformation, objectPool) { IDProperties = MappingInformation.GetChildMappings(typeof(TMappedClass)) .SelectMany(x => MappingInformation.GetParentMapping(x.ObjectType)) .Distinct() .SelectMany(x => x.IDProperties); Queries = new ListMapping <string, QueryGeneratorData>(); SetupQueries(); }
/// <summary> /// Initializes a new instance of the <see cref="InsertQuery{TMappedClass}"/> class. /// </summary> /// <param name="mappingInformation">The mapping information.</param> /// <param name="objectPool">The object pool.</param> public InsertQuery(IMappingSource mappingInformation, ObjectPool <StringBuilder> objectPool) : base(mappingInformation, objectPool) { if (!MappingInformation.TypeGraphs.ContainsKey(AssociatedType)) { return; } var TypeGraph = MappingInformation.TypeGraphs[AssociatedType]; QueryDeclarationText = GenerateInsertQueryDeclarations(); QueryText = GenerateInsertQuery(TypeGraph?.Root); var ParentMappings = MappingInformation.GetParentMapping(typeof(TMappedClass)); IDProperties = ParentMappings.SelectMany(x => x.IDProperties); ReferenceProperties = ParentMappings.SelectMany(x => x.ReferenceProperties); }
/// <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); }