Пример #1
0
        /// <summary>
        /// Appends the current property name, e.g. <c>"Item.MyValue"</c>, to the attribute buffer.
        /// </summary>
        /// <param name="attribute">The current attribute</param>
        /// <param name="tblAlias">An alias to qualify the property name with.</param>
        /// <param name="propName">A name to suffix the property name with.</param>
        /// <returns>A new attribute containing the appended buffer.</returns>
        public static IProjectionAttribute Prop(this IProjectionAttribute attribute, string tblAlias = null, string propName = null)
        {
            if ((propName == null || attribute.Field != null) && attribute.Metadata.HasFlag(RelationMetadataFlags.Model))
            {
                throw ProjectionException.FromProjection(attribute, "Cannot reference model directly.");
            }

            if (attribute.Field != null)
            {
                IField field = attribute.Field();

                ColumnBinding binding = new ColumnBinding(field.Identity.Name, field);

                propName = attribute.Context.Domain.Dialect.Identifier(binding.ColumnName);

                return(attribute.Append(propName).Append(binding));
            }
            else
            {
                string fullName = attribute.Metadata.Identity.Name;

                if (propName != null)
                {
                    fullName = attribute.Identity.Schema.Notation.Combine(fullName, propName);
                }

                if (tblAlias != null)
                {
                    attribute = attribute.Append(attribute.Context.Domain.Dialect.Identifier(tblAlias));
                    attribute = attribute.Append(attribute.Context.Domain.Dialect.Qualifier);
                }

                return(attribute.Append(attribute.Context.Domain.Dialect.Identifier(fullName)));
            }
        }
Пример #2
0
        /// <summary>
        /// Appends the current parameter name and value, e.g. <c>@P0</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 Par(this IProjectionAttribute attribute)
        {
            if (!attribute.Context.Domain.Dialect.Support.HasFlag(DialectSupport.InputParameters))
            {
                throw ProjectionException.FromProjection(attribute, $"Dialect '{attribute.Context.Domain.Dialect.GetType().Name}' does not support input parameters.");
            }

            IField value = attribute.Field?.Invoke();

            if (value != null)
            {
                string paramName   = attribute.Context.Lookup.Parameter(attribute.Identity, value);
                string dialectName = attribute.Context.Domain.Dialect.Parameter(paramName);

                Parameter param = new Parameter(paramName, value);

                IProjectionAttribute result = attribute.Append(dialectName).Append(param);

                if (attribute.Metadata.HasFlag(ProjectionMetadataFlags.Output) && attribute.Context.Domain.Dialect.Support.HasFlag(DialectSupport.OutputParameters))
                {
                    ParameterBinding binding = new ParameterBinding(param);

                    result = result.Append(binding);
                }

                return(result);
            }
            else
            {
                string paramName   = attribute.Context.Lookup.Parameter(attribute.Identity, attribute.Metadata.Identity);
                string dialectName = attribute.Context.Domain.Dialect.Parameter(paramName);

                return(attribute.Append(dialectName));
            }
        }
Пример #3
0
        public static IProjectionMetadata GetPreferredTvpMetadata(IProjection projection)
        {
            if (projection.Metadata.List == null && projection.Metadata.Item == null)
            {
                throw ProjectionException.FromProjection(projection, "No table information found.");
            }

            return(projection.Metadata.List ?? projection.Metadata.Item?.List);
        }
Пример #4
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));
        }
Пример #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());
        }
Пример #6
0
        /// <summary>
        /// Appends the current JSON path literal, e.g. <c>'$.my.value'</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 JsonPath(this IProjectionAttribute attribute)
        {
            IJsonMetadata metadata = attribute.Metadata.Identity.GetMetadata <IJsonMetadata>();

            if (metadata == null)
            {
                throw ProjectionException.FromProjection(attribute, "JSON metadata not found.");
            }

            string literal = attribute.Context.Domain.Dialect.String(metadata.Path);

            return(attribute.Append(literal));
        }
Пример #7
0
        public static ITableMetadata GetPreferredColumnMetadata(IProjectionAttribute attribute)
        {
            if (attribute.Metadata.HasFlag(TableMetadataFlags.Column))
            {
                return(attribute.Metadata.Table);
            }
            else if (attribute.Metadata.Item != null && attribute.Metadata.Item.HasFlag(TableMetadataFlags.Column))
            {
                return(attribute.Metadata.Item.Table);
            }

            throw ProjectionException.FromProjection(attribute, "No column information found.");
        }
Пример #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 call to <c>JSON_VALUE</c> from the current column and JSON path, e.g. <c>JSON_VALUE(T0."MyJson", '$.my.value')</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 Json(this IProjectionAttribute attribute)
        {
            IJsonMetadata json = ProjectionHelper.GetJsonMetadata(attribute);

            IProjectionMetadata metadata     = attribute.Metadata;
            IProjectionMetadata rootMetadata = json.MemberOf.Identity.GetMetadata <IProjectionMetadata>();

            attribute = attribute.Append("JSON_VALUE(");
            try
            {
                attribute = attribute.With(metadata: rootMetadata).Col();
            }
            catch (ProjectionException ex)
            {
                throw ProjectionException.FromProjection(attribute, "JSON value requires a column to read from.", innerException: ex);
            }
            attribute = attribute.Append(",");
            attribute = attribute.With(metadata: metadata).JsonPath();
            attribute = attribute.Append(")");

            return(attribute);
        }
Пример #10
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)));
        }
Пример #11
0
 public static IJsonMetadata GetJsonMetadata(IProjectionAttribute attribute) => attribute.Metadata.Identity.GetMetadata <IJsonMetadata>() ??
 throw ProjectionException.FromProjection(attribute, "JSON metadata not found.");
Пример #12
0
        public static IProjectionMetadata GetMetadataFromRelativeLambda(IProjection projection, LambdaExpression expression)
        {
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }

            string name     = projection.Metadata.Identity.Notation.Lambda(expression);
            string fullName = projection.Metadata.Identity.Notation.Combine(projection.Metadata.Identity.Name, name);

            return(projection.Metadata.Identity.Schema.GetMetadata <IProjectionMetadata>(fullName) ?? throw ProjectionException.FromProjection(projection, "Metadata not found."));
        }
Пример #13
0
 public static ITableMetadata GetPreferredTableMetadata(IProjectionAttribute attribute) => GetPreferredTableMetadata(attribute.Metadata) ??
 throw ProjectionException.FromProjection(attribute, "No table information found.");
Пример #14
0
 public static ITableMetadata GetPreferredTableMetadata(IProjection projection) => GetPreferredTableMetadata(projection.Metadata) ??
 throw ProjectionException.FromProjection(projection, "No table information found.");