Пример #1
0
        public static IProjectionAttribute ValList(this IProjection projection, Func <IProjectionAttribute, IProjectionAttribute> itemFactory)
        {
            if (projection.Data == null)
            {
                throw ProjectionException.ValueNotFound(projection.Metadata);
            }

            IProjectionMetadata itemMetadata = projection.Metadata?.Item ?? projection.Metadata;

            using ProjectionReader reader = new ProjectionReader(projection.Data.Source, new[] { itemMetadata });

            IProjectionAttribute attribute = new ProjectionAttribute(projection.Identity, projection.Context, itemMetadata, data: null);

            if (reader.Read())
            {
                IProjectionData data = reader.GetData().First();

                attribute = itemFactory(attribute.With(data: data));
            }

            while (reader.Read())
            {
                IProjectionData data = reader.GetData().First();

                attribute = attribute.Append(", ");
                attribute = itemFactory(attribute.With(data: data));
            }

            return(attribute);
        }
Пример #2
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));
                    }
                }
            }
        }
Пример #3
0
        public static IField GetFieldValue(IProjectionAttribute attribute)
        {
            if (attribute.Field == null)
            {
                throw ProjectionException.ValueNotFound(attribute);
            }

            return(attribute.Field());
        }
Пример #4
0
        public static IProjection Val(this IProjection projection)
        {
            IProjection value = projection.Vals().FirstOrDefault();

            if (value == null)
            {
                throw ProjectionException.ValueNotFound(projection);
            }

            return(value);
        }
Пример #5
0
        /// <summary>
        /// Appends the current variable name, e.g. <c>@V0</c>, to the attribute buffer.
        /// </summary>
        /// <param name="attribute">The current attribute.</param>
        /// <returns>A new attribute containing the appended buffer.</returns>
        public static IProjectionAttribute Var(this IProjectionAttribute attribute)
        {
            if (attribute.Data == null)
            {
                throw ProjectionException.ValueNotFound(attribute.Metadata);
            }

            string variableName = attribute.Context.Lookup.Variable(attribute.Identity, attribute.Data.Input);
            string dialectName  = attribute.Context.Domain.Dialect.Variable(variableName);

            return(attribute.Append(dialectName));
        }
Пример #6
0
        /// <summary>
        /// Appends the current variable name, e.g. <c>@V0</c>, to the attribute buffer.
        /// </summary>
        /// <param name="attribute">The current attribute.</param>
        /// <returns>A new attribute containing the appended buffer.</returns>
        public static IProjectionAttribute Var(this IProjectionAttribute attribute)
        {
            if (attribute.Field == null)
            {
                throw ProjectionException.ValueNotFound(attribute);
            }

            IField field = ProjectionHelper.GetFieldValue(attribute);

            string variableName = attribute.Context.Lookup.Variable(attribute.Identity, field);
            string dialectName  = attribute.Context.Domain.Dialect.Variable(variableName);

            return(attribute.Append(dialectName));
        }
Пример #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));
        }
Пример #8
0
        /// <summary>
        /// Appends a table-valued parameter from the current values, e.g. <c>@TP0</c>, to the projection buffer.
        /// </summary>
        /// <param name="projection">The current projection.</param>
        /// <returns>A new attribute containing the appended buffer.</returns>
        public static IProjectionAttribute TvpName(this IProjection projection)
        {
            if (projection.Source == null)
            {
                throw ProjectionException.ValueNotFound(projection);
            }
            else if (!projection.Attributes.Any())
            {
                throw ProjectionException.FromProjection(projection, "No attributes found.");
            }

            Relation relation = new Relation(projection.Source, projection.Attributes.Select(a => a.Metadata.Identity));

            string paramName   = projection.Context.Lookup.Custom("TP", projection.Identity, field: projection.Source);
            string dialectName = projection.Context.Domain.Dialect.Parameter(paramName);

            return(projection.Attr().Append(dialectName).Append(new TableValuedParameter(paramName, relation)));
        }
Пример #9
0
        /// <summary>
        /// Appends a table-valued parameter from the current values, e.g. <c>@TP0</c>, to the projection buffer.
        /// </summary>
        /// <param name="projection">The current projection.</param>
        /// <returns>A new attribute containing the appended buffer.</returns>
        public static IProjectionAttribute TvpName(this IProjection projection)
        {
            if (projection.Source == null)
            {
                throw ProjectionException.ValueNotFound(projection);
            }
            else if (!projection.Attributes.Any())
            {
                throw ProjectionException.FromProjection(projection, "No attributes found.");
            }

            IProjectionMetadata metadata = TvpHelper.GetPreferredTvpMetadata(projection);
            IField source = new Relation(projection.Source, metadata.Identity.Name).Scalar();

            RelationIdentity identity = new RelationIdentity(metadata.Identity.Schema, projection.Attributes.Select(a => a.Metadata.Identity));

            IBindingParameterContract contract = TvpCache.GetParameterContract(identity);

            string paramName   = projection.Context.Lookup.Custom("TP", projection.Identity, field: source);
            string dialectName = projection.Context.Domain.Dialect.Parameter(paramName);

            return(projection.Attr().Append(dialectName).Append(new Parameter(paramName, source, contract)));
        }
Пример #10
0
        public static IProjectionAttribute ValList(this IProjection projection, Func <IProjectionAttribute, IProjectionAttribute> itemFactory)
        {
            if (projection.Source == null)
            {
                throw ProjectionException.ValueNotFound(projection);
            }

            IField[]             items     = new Relation(projection.Source, projection.Metadata.Identity.Name).Column().ToArray();
            IProjectionAttribute attribute = projection.Attr();

            if (items.Length == 0)
            {
                return(attribute);
            }

            attribute = itemFactory(attribute.With(metadata: attribute.Metadata, field: () => items[0]));

            foreach (IField item in items.Skip(1))
            {
                attribute = itemFactory(attribute.With(field: () => item).Append(", "));
            }

            return(attribute);
        }