Beispiel #1
0
        public static IProjectionValues <TModel> Vals <TModel>(this IProjection <TModel> projection, int batchIndex = -1)
        {
            if (projection.Data == null)
            {
                throw ProjectionException.ValueNotFound(projection.Metadata);
            }
            else if (projection.Data.Source.Snapshot == null)
            {
                IEnumerable <IProjection <TModel> > emptyItems = Array.Empty <IProjection <TModel> >();

                return(new ProjectionValues <TModel>(projection.Context, projection.Identity, emptyItems, batchIndex));
            }

            IProjectionMetadata[]  header = new[] { projection.Metadata }.Concat(projection.Header.Select(a => a.Metadata)).ToArray();
            IProjectionAttribute[] attributes = header.Skip(1).Select(m => new ProjectionAttribute(projection.Identity, projection.Context, m, data: null)).ToArray();

            return(new ProjectionValues <TModel>(projection.Context, projection.Identity, innerReader(), batchIndex));

            IEnumerable <IProjection <TModel> > innerReader()
            {
                using ProjectionReader reader = new ProjectionReader(projection.Data.Source, header);

                while (reader.Read())
                {
                    IProjectionData[] dataSet = reader.GetData().ToArray();

                    if (dataSet[0].Source.Snapshot != null)
                    {
                        IEnumerable <IProjectionAttribute> valueHeader = attributes.Zip(dataSet.Skip(1)).Select(t => t.First.With(data: t.Second));

                        yield return(projection.With(data: dataSet[0], header: valueHeader));
                    }
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Navigates the current projection to its default item target.
        /// </summary>
        /// <param name="projection">The current projection.</param>
        /// <returns>A new projection of the item target.</returns>
        public static IProjection Open(this IProjection projection)
        {
            if (!projection.Metadata.HasFlag(RelationMetadataFlags.List))
            {
                throw ProjectionException.InvalidProjection(projection.Metadata, "Attribute is not a list.");
            }

            return(projection.With(metadata: projection.Metadata.Item));
        }
Beispiel #3
0
        /// <summary>
        /// Navigates the current projection to its default item target.
        /// </summary>
        /// <param name="projection">The current projection.</param>
        /// <returns>A new projection of the item target.</returns>
        public static IProjection Open(this IProjection projection)
        {
            if (!projection.Metadata.HasFlag(RelationMetadataFlags.List))
            {
                throw ProjectionException.FromProjection(projection, "No metadata list contract found.");
            }

            return(projection.With(metadata: projection.Metadata.Item));
        }
Beispiel #4
0
        /// <summary>
        /// Appends mappings between the current qualified columns and their properties, e.g. <c>T0."MyColumn" AS "Item.MyValue"</c>, to the projection buffer. Identical to calling <c>Cols().As().Props()</c>.
        /// </summary>
        /// <param name="projection">The current projection</param>
        /// <param name="tblAlias">The table alias to qualify each column name with.</param>
        /// <returns>A new projection containing the appended buffer.</returns>
        public static IProjection Star(this IProjection projection, string tblAlias = null)
        {
            if (!projection.Any())
            {
                throw ProjectionException.AttributesNotFound(projection.Metadata);
            }

            IProjectionMetadata metadata = ProjectionHelper.GetPreferredTableMetadata(projection.Metadata);

            return(projection.With(metadata).Cols(tblAlias).As().Props());
        }
Beispiel #5
0
        /// <summary>
        /// Appends mappings between the current qualified columns and their properties, e.g. <c>T0."MyColumn" AS "Item.MyValue"</c>, to the projection buffer. Identical to calling <c>Cols().As().Props()</c>.
        /// </summary>
        /// <param name="projection">The current projection</param>
        /// <returns>A new projection containing the appended buffer.</returns>
        public static IProjection Star(this IProjection projection)
        {
            if (!projection.Any())
            {
                throw ProjectionException.FromProjection(projection, "No attributes found.");
            }

            IProjectionMetadata metadata = ProjectionHelper.GetPreferredTableMetadata(projection).Identity.GetMetadata <IProjectionMetadata>();

            return(projection.With(metadata).Cols().As().Props());
        }
 private IProjection GetProjectionForPartialResult(IProjection projection, Type resultType)
 {
     if (resultType.IsAssignableFrom(projection.Metadata.Type))
     {
         return(projection);
     }
     else if (projection.Metadata.Item != null && resultType.IsAssignableFrom(projection.Metadata.Item.Type))
     {
         return(projection.With(metadata: projection.Metadata.Item));
     }
     else
     {
         throw new InvalidOperationException($"No conversion found between page type '{resultType.GetSanitizedFullName()}' and requested type '{projection.Metadata.Type.GetSanitizedFullName()}'.");
     }
 }
Beispiel #7
0
        public static IProjection Val(this IProjection projection)
        {
            if (projection.Data == null)
            {
                throw ProjectionException.ValueNotFound(projection.Metadata);
            }

            IProjectionData newData = ProjectionData.Resolve(projection.Data, projection.Metadata);

            if (newData.Source.Snapshot == null)
            {
                throw ProjectionException.ValueNotFound(newData.Source);
            }

            return(projection.With(data: newData));
        }
        public static IProjection IsEq(this IProjection projection, IProjection other)
        {
            List <IProjectionAttribute> newAttrs = new List <IProjectionAttribute>();

            foreach (var(l, r) in projection.Attrs().Zip(other.Attrs()))
            {
                IProjectionAttribute newAttr = l;

                newAttr = newAttr.Eq();
                newAttr = newAttr.With(metadata: r.Metadata, field: r.Field).Par();
                newAttr = newAttr.With(metadata: l.Metadata, field: l.Field);

                newAttrs.Add(newAttr);
            }

            return(projection.With(attributes: newAttrs));
        }
Beispiel #9
0
        public static IProjection IsEq(this IProjection projection, IProjection other)
        {
            List <IProjectionAttribute> newHeader = new List <IProjectionAttribute>();

            foreach (var(l, r) in projection.Attrs().Zip(other.Attrs()))
            {
                IProjectionAttribute newAttr = l;

                newAttr = newAttr.Eq();
                newAttr = newAttr.With(data: r.Data).Par();
                newAttr = newAttr.With(data: l.Data);

                newHeader.Add(newAttr);
            }

            ProjectionOptions newOptions = new ProjectionOptions(projection.Options)
            {
                Separator = Environment.NewLine + "AND" + Environment.NewLine,
            };

            return(projection.With(header: newHeader, options: newOptions));
        }
        public static IProjection IsEq(this IProjection projection, IProjection other)
        {
            List <IProjectionAttribute> newAttrs = new List <IProjectionAttribute>();

            foreach (var(l, r) in projection.Attrs().Zip(other.Attrs()))
            {
                IProjectionAttribute newAttr = l;

                newAttr = newAttr.Eq();
                newAttr = newAttr.With(metadata: r.Metadata, field: r.Field).Par();
                newAttr = newAttr.With(metadata: l.Metadata, field: l.Field);

                newAttrs.Add(newAttr);
            }

            ProjectionOptions newOptions = new ProjectionOptions(projection.Options)
            {
                Separator = Environment.NewLine + "AND" + Environment.NewLine,
            };

            return(projection.With(attributes: newAttrs, options: newOptions));
        }
Beispiel #11
0
        public static IEnumerable <IProjection> Vals(this IProjection projection)
        {
            if (projection.Source == null)
            {
                yield break;
            }

            IEnumerable <MetadataIdentity> heading = new[] { projection.Attr().Metadata.Identity }.Concat(projection.Attributes.Select(a => a.Metadata.Identity));

            Relation relation = new Relation(projection.Source, heading);

            IProjectionAttribute[] attributes = projection.Attributes.ToArray();

            foreach (ITuple tuple in relation)
            {
                IField field = tuple[0];
                IProjectionAttribute[] newAttributes = attributes.Select((a, i) => a.With(field: () => tuple[i + 1])).ToArray();

                yield return(projection.With(attributes: newAttributes, field: field));

                projection.Context.Execution.Buffer.Mark();
            }
        }
Beispiel #12
0
 /// <summary>
 /// Filters the current projection to include only primary key attributes.
 /// </summary>
 /// <param name="projection">The current projection</param>
 /// <returns>A new projection containing the filtered attributes.</returns>
 public static IProjection NonKey(this IProjection projection) => projection.With(attributes: projection.Attributes.Where(a => !a.Metadata.HasAnyFlag(ReferenceMetadataFlags.Key)));
Beispiel #13
0
 /// <summary>
 /// Filters the current projection to include only primary key attributes.
 /// </summary>
 /// <param name="projection">The current projection</param>
 /// <returns>A new projection containing the filtered attributes.</returns>
 public static IProjection Key(this IProjection projection) => projection.With(attributes: projection.Attributes.Where(a => a.Metadata.HasFlag(ReferenceMetadataFlags.CandidateKey)));
Beispiel #14
0
 /// <summary>
 /// Filters the current projection to include only output attributes.
 /// </summary>
 /// <param name="projection">The current projection</param>
 /// <returns>A new projection containing the filtered attributes.</returns>
 public static IProjection Out(this IProjection projection) => projection.With(attributes: projection.Attributes.Where(a => a.Metadata.HasFlag(ProjectionMetadataFlags.Output)));
Beispiel #15
0
 /// <summary>
 /// Filters the header of the current projection.
 /// </summary>
 /// <param name="projection">The current projection</param>
 /// <param name="filterFunc">Predicate function to filter by.</param>
 /// <returns>A new projection containing the filtered attributes.</returns>
 public static IProjection Filter(this IProjection projection, Func <IProjectionAttribute, bool> filterFunc)
 => projection.With(header: projection.Header.Where(filterFunc ?? (a => true)));
Beispiel #16
0
        /// <summary>
        /// Navigates the current projection to a selected target.
        /// </summary>
        /// <param name="projection">The current projection.</param>
        /// <param name="expression">Expression selecting a projection target.</param>
        /// <returns>A new projection from the selected target.</returns>
        public static IProjection <TProperty> For <TModel, TProperty>(this IProjection <TModel> projection, Expression <Func <TModel, TProperty> > expression)
        {
            IProjectionMetadata metadata = ProjectionHelper.GetMetadataFromRelativeLambda(projection, expression);

            return(projection.With(metadata).Cast <TProperty>());
        }