Exemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PrimaryKeyFunctionField"/> class.
        /// </summary>
        /// <param name="uniqueKeyElement">The table element.</param>
        public PrimaryKeyFunctionField(UniqueKeyElement uniqueKeyElement)
        {
            // Initialize the object.
            this.Name             = "primaryKeyFunction";
            this.uniqueKeyElement = uniqueKeyElement ?? throw new ArgumentNullException(nameof(uniqueKeyElement));

            //        /// <summary>
            //        /// Used to get the primary key from the record.
            //        /// </summary>
            //        private Func<Buyer, object> primaryKeyFunction = ((Expression<Func<Buyer, object>>)(b => b.BuyerId)).Compile();
            this.Syntax = SyntaxFactory.FieldDeclaration(
                SyntaxFactory.VariableDeclaration(
                    SyntaxFactory.GenericName(
                        SyntaxFactory.Identifier("Func"))
                    .WithTypeArgumentList(
                        SyntaxFactory.TypeArgumentList(
                            SyntaxFactory.SeparatedList <TypeSyntax>(
                                new SyntaxNodeOrToken[]
            {
                SyntaxFactory.IdentifierName(this.uniqueKeyElement.Table.Name),
                SyntaxFactory.Token(SyntaxKind.CommaToken),
                SyntaxFactory.PredefinedType(
                    SyntaxFactory.Token(SyntaxKind.ObjectKeyword)),
            }))))
                .WithVariables(
                    SyntaxFactory.SingletonSeparatedList <VariableDeclaratorSyntax>(
                        SyntaxFactory.VariableDeclarator(
                            SyntaxFactory.Identifier("primaryKeyFunction"))
                        .WithInitializer(this.Initializer))))
                          .WithModifiers(PrimaryKeyFunctionField.Modifiers)
                          .WithLeadingTrivia(PrimaryKeyFunctionField.DocumentationComment);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates the element that describes a unique constraint.
        /// </summary>
        /// <param name="uniqueConstraintSchema">A description of a unique constraint.</param>
        /// <returns>An element that can be used in an XML Schema document to describe a unique constraint.</returns>
        private static XElement CreateUniqueKey(UniqueKeyElement uniqueConstraintSchema)
        {
            //    <xs:unique name="AccessControlKey" msdata:PrimaryKey="true">
            //      <xs:selector xpath=".//mstns:AccessControl" />
            //      <xs:field xpath="mstns:UserId" />
            //      <xs:field xpath="mstns:EntityId" />
            //    </xs:unique>
            XElement uniqueElement = new XElement(
                ScrubXsdCommand.xs + "unique",
                new XAttribute("name", uniqueConstraintSchema.Name));

            if (uniqueConstraintSchema.IsPrimaryKey)
            {
                uniqueElement.Add(new XAttribute(XmlSchemaDocument.IsPrimaryKeyName, true));
            }

            uniqueElement.Add(
                new XElement(
                    ScrubXsdCommand.xs + "selector",
                    new XAttribute("xpath", string.Format(".//mstns:{0}", uniqueConstraintSchema.Table.Name))));

            foreach (ColumnReferenceElement columnReferenceElement in uniqueConstraintSchema.Columns.OrderBy(c => c.Name))
            {
                ColumnElement columnElement = columnReferenceElement.Column;
                uniqueElement.Add(
                    new XElement(
                        ScrubXsdCommand.xs + "field",
                        new XAttribute("xpath", string.Format("mstns:{0}", columnElement.Name))));
            }

            // This describes a unique constraint on a table.
            return(uniqueElement);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="GetMethod"/> class.
        /// </summary>
        /// <param name="uniqueKeyElement">The unique constraint schema.</param>
        public GetMethod(UniqueKeyElement uniqueKeyElement)
        {
            // Initialize the object.  Note that we decorate the name of every method that's not the primary key to prevent ambiguous signatures.
            this.uniqueKeyElement = uniqueKeyElement ?? throw new ArgumentNullException(nameof(uniqueKeyElement));
            this.Name             = $"Get{this.uniqueKeyElement.Table.Name}" + (uniqueKeyElement.IsPrimaryKey ? string.Empty : "By" + uniqueKeyElement.Name);

            //        /// <summary>
            //        /// Gets a specific <see cref="Province"/> record.
            //        /// </summary>
            //        /// <param name="provinceId">The primary key identifier.</param>
            //        /// <returns>The result of the GET verb.</returns>
            //        [HttpGet("{provinceId}")]
            //        public async Task<IActionResult> GetProvince([FromRoute] int provinceId)
            //        {
            //            <Body>
            //        }
            this.Syntax = SyntaxFactory.MethodDeclaration(
                SyntaxFactory.GenericName(
                    SyntaxFactory.Identifier("Task"))
                .WithTypeArgumentList(
                    SyntaxFactory.TypeArgumentList(
                        SyntaxFactory.SingletonSeparatedList <TypeSyntax>(
                            SyntaxFactory.IdentifierName("IActionResult")))),
                SyntaxFactory.Identifier(this.Name))
                          .WithAttributeLists(this.Attributes)
                          .WithModifiers(GetMethod.Modifiers)
                          .WithParameterList(this.Parameters)
                          .WithBody(this.Body)
                          .WithLeadingTrivia(this.DocumentationComment);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates an argument that creates a lambda expression for extracting the key from a class.
        /// </summary>
        /// <param name="uniqueKeyElement">The unique key element.</param>
        /// <returns>An expression that creates a filter for an index.</returns>
        public static ExpressionSyntax GetNullableKeyFilter(UniqueKeyElement uniqueKeyElement)
        {
            // Validate the parameter
            if (uniqueKeyElement == null)
            {
                throw new ArgumentNullException(nameof(uniqueKeyElement));
            }

            // Used as a variable when constructing the lambda expression.
            string abbreviation = uniqueKeyElement.Table.Name[0].ToString(CultureInfo.InvariantCulture).ToLowerInvariant();

            // This will create an expression for extracting the key from record.
            CSharpSyntaxNode syntaxNode = null;

            if (uniqueKeyElement.Columns.Count == 1)
            {
                // A simple key can be used like a value type.
                syntaxNode = SyntaxFactory.BinaryExpression(
                    SyntaxKind.NotEqualsExpression,
                    SyntaxFactory.MemberAccessExpression(
                        SyntaxKind.SimpleMemberAccessExpression,
                        SyntaxFactory.IdentifierName(abbreviation),
                        SyntaxFactory.IdentifierName(uniqueKeyElement.Columns[0].Column.Name)),
                    SyntaxFactory.LiteralExpression(
                        SyntaxKind.NullLiteralExpression));
            }
            else
            {
                // A Compound key must be constructed from an anomymous type.
                List <SyntaxNodeOrToken> keyElements = new List <SyntaxNodeOrToken>();
                foreach (ColumnReferenceElement columnReferenceElement in uniqueKeyElement.Columns)
                {
                    if (keyElements.Count != 0)
                    {
                        keyElements.Add(SyntaxFactory.Token(SyntaxKind.CommaToken));
                    }

                    keyElements.Add(
                        SyntaxFactory.Argument(
                            SyntaxFactory.MemberAccessExpression(
                                SyntaxKind.SimpleMemberAccessExpression,
                                SyntaxFactory.IdentifierName(abbreviation),
                                SyntaxFactory.IdentifierName(columnReferenceElement.Column.Name))));
                }

                // b => b.BuyerId or p => ValueTuple.Create(p.Name, p.CountryCode)
                syntaxNode = SyntaxFactory.InvocationExpression(
                    SyntaxFactory.MemberAccessExpression(
                        SyntaxKind.SimpleMemberAccessExpression,
                        SyntaxFactory.IdentifierName("ValueTuple"),
                        SyntaxFactory.IdentifierName("Create")))
                             .WithArgumentList(
                    SyntaxFactory.ArgumentList(
                        SyntaxFactory.SeparatedList <ArgumentSyntax>(keyElements.ToArray())));
            }

            //            this.BuyerKey = new SimpleUniqueKeyIndex<Buyer>("BuyerKey").HasIndex(b => b.BuyerId);
            return(SyntaxFactory.SimpleLambdaExpression(SyntaxFactory.Parameter(SyntaxFactory.Identifier(abbreviation)), syntaxNode));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Creates an argument that creates a lambda expression for extracting the key from a class.
        /// </summary>
        /// <param name="uniqueKeyElement">The unique key element.</param>
        /// <returns>An expression that creates a filter for an index.</returns>
        public static ExpressionSyntax GetNullableKeyFilter(UniqueKeyElement uniqueKeyElement)
        {
            // Validate the parameter
            if (uniqueKeyElement == null)
            {
                throw new ArgumentNullException(nameof(uniqueKeyElement));
            }

            // Used as a variable when constructing the lambda expression.
            string abbreviation = uniqueKeyElement.Table.Name[0].ToString(CultureInfo.InvariantCulture).ToLowerInvariant();

            // .HasFilter(s => s.Figi != null)
            // .HasFilter(p => p.BaseCurrencyCode != null && p.CurrencyCode != null)
            BinaryExpressionSyntax syntaxNode = default;

            for (int index = 0; index < uniqueKeyElement.Columns.Count; index++)
            {
                if (index == 0)
                {
                    syntaxNode = SyntaxFactory.BinaryExpression(
                        SyntaxKind.NotEqualsExpression,
                        SyntaxFactory.MemberAccessExpression(
                            SyntaxKind.SimpleMemberAccessExpression,
                            SyntaxFactory.IdentifierName(abbreviation),
                            SyntaxFactory.IdentifierName(uniqueKeyElement.Columns[0].Column.Name)),
                        SyntaxFactory.LiteralExpression(
                            SyntaxKind.NullLiteralExpression));
                }
                else
                {
                    BinaryExpressionSyntax oldNode = syntaxNode;
                    syntaxNode = SyntaxFactory.BinaryExpression(
                        SyntaxKind.LogicalAndExpression,
                        oldNode,
                        SyntaxFactory.BinaryExpression(
                            SyntaxKind.NotEqualsExpression,
                            SyntaxFactory.MemberAccessExpression(
                                SyntaxKind.SimpleMemberAccessExpression,
                                SyntaxFactory.IdentifierName(abbreviation),
                                SyntaxFactory.IdentifierName(uniqueKeyElement.Columns[index].Column.Name)),
                            SyntaxFactory.LiteralExpression(
                                SyntaxKind.NullLiteralExpression)));
                }
            }

            //            this.BuyerKey = new UniqueKeyIndex<Buyer>("BuyerKey").HasIndex(b => b.BuyerId);
            return(SyntaxFactory.SimpleLambdaExpression(SyntaxFactory.Parameter(SyntaxFactory.Identifier(abbreviation)), syntaxNode));
        }
Exemplo n.º 6
0
        /// <summary>
        /// Gets the syntax for the creation of an anonymous type.
        /// </summary>
        /// <param name="uniqueKeyElement">The description of a unique key.</param>
        /// <param name="isDecorated">Indicates that the argument name should be prefixed with the index name.</param>
        /// <returns>An expression that builds an anonymous type from a table description.</returns>
        public static SeparatedSyntaxList <ArgumentSyntax> GetSyntax(UniqueKeyElement uniqueKeyElement, bool isDecorated = false)
        {
            // Validate the argument.
            if (uniqueKeyElement == null)
            {
                throw new ArgumentNullException(nameof(uniqueKeyElement));
            }

            //                    country = this.domain.Countries.CountryCountryCodeKey.Find(countryCountryCodeKeyCountryCode);
            //                    region = this.domain.Regions.RegionExternalKey.Find((regionExternalKeyName, regionExternalKeyCountryCode));
            SeparatedSyntaxList <ArgumentSyntax> findParameters;

            if (uniqueKeyElement.Columns.Count == 1)
            {
                ColumnElement columnElement = uniqueKeyElement.Columns[0].Column;
                string        attributeName = isDecorated ? $"{uniqueKeyElement.Name.ToCamelCase()}{columnElement.Name}" : columnElement.Name.ToVariableName();
                findParameters = SyntaxFactory.SingletonSeparatedList <ArgumentSyntax>(
                    SyntaxFactory.Argument(
                        SyntaxFactory.IdentifierName(attributeName)));
            }
            else
            {
                List <SyntaxNodeOrToken> keys = new List <SyntaxNodeOrToken>();
                foreach (ColumnReferenceElement columnReferenceElement in uniqueKeyElement.Columns)
                {
                    if (keys.Count != 0)
                    {
                        keys.Add(SyntaxFactory.Token(SyntaxKind.CommaToken));
                    }

                    ColumnElement columnElement = columnReferenceElement.Column;
                    string        attributeName = isDecorated ? $"{uniqueKeyElement.Name.ToCamelCase()}{columnElement.Name}" : columnElement.Name.ToVariableName();
                    keys.Add(
                        SyntaxFactory.Argument(
                            SyntaxFactory.IdentifierName(attributeName)));
                }

                findParameters = SyntaxFactory.SingletonSeparatedList <ArgumentSyntax>(
                    SyntaxFactory.Argument(
                        SyntaxFactory.TupleExpression(
                            SyntaxFactory.SeparatedList <ArgumentSyntax>(keys))));
            }

            // This is either the parameter or a tuple that can be used as parameters to a 'Find' operation.
            return(findParameters);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DictionaryField"/> class.
        /// </summary>
        /// <param name="uniqueKeyElement">The table schema.</param>
        public DictionaryField(UniqueKeyElement uniqueKeyElement)
        {
            // Initialize the object.
            this.uniqueKeyElement = uniqueKeyElement;

            // This is the name of the field.
            this.Name = "dictionary";

            //        /// <summary>
            //        /// The dictionary containing the index.
            //        /// </summary>
            //        private Dictionary<Guid, ProvinceRow> dictionary = new Dictionary<Guid, ProvinceRow>();
            this.Syntax = SyntaxFactory.FieldDeclaration(
                SyntaxFactory.VariableDeclaration(this.Type)
                .WithVariables(
                    SyntaxFactory.SingletonSeparatedList <VariableDeclaratorSyntax>(
                        SyntaxFactory.VariableDeclarator(
                            SyntaxFactory.Identifier(this.Name))
                        .WithInitializer(this.Initializer))))
                          .WithModifiers(DictionaryField.Modifiers)
                          .WithLeadingTrivia(DictionaryField.DocumentationComment);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SimpleUniqueKeyIndexProperty"/> class.
        /// </summary>
        /// <param name="uniqueKeyElement">The unique key element.</param>
        public SimpleUniqueKeyIndexProperty(UniqueKeyElement uniqueKeyElement)
        {
            // Initialize the object.
            this.uniqueKeyElement = uniqueKeyElement ?? throw new ArgumentNullException(nameof(uniqueKeyElement));
            this.Name             = this.uniqueKeyElement.Name;

            //        /// <summary>
            //        /// Gets the BuyerExternalId0Key unique index.
            //        /// </summary>
            //        public SimpleUniqueKeyIndex<Buyer> BuyerExternalId0Key { get; } = new SimpleUniqueKeyIndex<Buyer>("BuyerExternalId0Key").HasIndex(b => b.ExternalId0);
            this.Syntax = SyntaxFactory.PropertyDeclaration(
                SyntaxFactory.GenericName(
                    SyntaxFactory.Identifier("SimpleUniqueKeyIndex"))
                .WithTypeArgumentList(
                    SyntaxFactory.TypeArgumentList(
                        SyntaxFactory.SingletonSeparatedList <TypeSyntax>(
                            SyntaxFactory.IdentifierName(this.uniqueKeyElement.Table.Name)))),
                SyntaxFactory.Identifier(this.uniqueKeyElement.Name))
                          .WithModifiers(SimpleUniqueKeyIndexProperty.Modifiers)
                          .WithAccessorList(SimpleUniqueKeyIndexProperty.AccessorList)
                          .WithInitializer(this.Initializer)
                          .WithLeadingTrivia(this.DocumentationComment)
                          .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken));
        }