/// <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> /// 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> /// 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="UpdateQuery{TMappedClass}"/> class. /// </summary> /// <param name="mappingInformation">The mapping information.</param> /// <param name="objectPool">The object pool.</param> public UpdateQuery(IMappingSource mappingInformation, ObjectPool <StringBuilder> objectPool) : base(mappingInformation, objectPool) { var ParentMappings = MappingInformation.GetChildMappings(typeof(TMappedClass)) .SelectMany(x => mappingInformation.GetParentMapping(x.ObjectType)) .Distinct(); IDProperties = ParentMappings.SelectMany(x => x.IDProperties); ReferenceProperties = ParentMappings.SelectMany(x => x.ReferenceProperties); }
/// <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="GeneratorBaseClass{TMappedClass}"/> class. /// </summary> /// <param name="mappingInformation">The mapping information.</param> /// <param name="queryGenerators">The query generators.</param> /// <exception cref="ArgumentNullException"> /// linqQueryGenerator or mappingInformation or queryGenerators /// </exception> /// <exception cref="ArgumentException">Mapping not found for type: AssociatedType</exception> protected GeneratorBaseClass(IMappingSource mappingInformation, IEnumerable <IQueryGenerator <TMappedClass> > queryGenerators) { MappingInformation = mappingInformation ?? throw new ArgumentNullException(nameof(mappingInformation)); if (!MappingInformation.GetChildMappings(AssociatedType).Any()) { throw new ArgumentException("Mapping not found for type: " + AssociatedType); } QueryGenerators = queryGenerators?.ToDictionary(x => x.QueryType) ?? throw new ArgumentNullException(nameof(queryGenerators)); LinqQueryGenerator = (ILinqQueryGenerator <TMappedClass>)queryGenerators.FirstOrDefault(x => x.QueryType == QueryType.LinqQuery); DataQueryGenerator = (IDataQueryGenerator <TMappedClass>)queryGenerators.FirstOrDefault(x => x.QueryType == QueryType.LoadData); }
/// <summary> /// Generates the query. /// </summary> /// <param name="data">The data.</param> /// <returns>The resulting query</returns> public override IQuery[] GenerateQueries(QueryData <TMappedClass> data) { if (data is null) { return(Array.Empty <IQuery>()); } var ReturnValue = new List <IQuery>(); foreach (var ChildMapping in MappingInformation.GetChildMappings(typeof(TMappedClass))) { var TypeGraph = MappingInformation.TypeGraphs[ChildMapping.ObjectType]; ReturnValue.Add(new Query(ChildMapping.ObjectType, CommandType.Text, GenerateSelectQuery(TypeGraph?.Root, data), QueryType, data.Parameters.ToArray())); } return(ReturnValue.ToArray()); }
/// <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 query. /// </summary> /// <param name="ids"></param> /// <returns>The resulting query</returns> public IQuery[] GenerateQueries(Dynamo[] ids) { if (ids is null || ids.Length == 0) { return(Array.Empty <IQuery>()); } var ReturnValue = new List <IQuery>(); var ItemSize = ids.FirstOrDefault()?.Count ?? 1; foreach (var ChildMapping in MappingInformation.GetChildMappings(MappedClassType)) { var TypeGraph = MappingInformation.TypeGraphs[ChildMapping.ObjectType]; foreach (var Split in SplitList(ids, 1000 / ItemSize)) { ReturnValue.Add(new Query(ChildMapping.ObjectType, CommandType.Text, GenerateSelectQuery(TypeGraph?.Root, Split), QueryType, GetParameters(Split, IDColumnInfo))); } } return(ReturnValue.ToArray()); }
/// <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); }