Ejemplo n.º 1
0
        /// <summary>
        /// Creates an argument that creates a lambda expression for extracting the key from a class.
        /// </summary>
        /// <param name="foreignKeyElement">The unique key element.</param>
        /// <returns>An expression that creates a filter for an index.</returns>
        public static ExpressionSyntax GetNullableKeyFilter(ForeignElement foreignKeyElement)
        {
            // Used as a variable when constructing the lambda expression.
            string abbreviation = foreignKeyElement.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 < foreignKeyElement.Columns.Count; index++)
            {
                // We can only filter nullable items.
                if (foreignKeyElement.Columns[0].Column.ColumnType.IsNullable)
                {
                    if (index == 0)
                    {
                        syntaxNode = SyntaxFactory.BinaryExpression(
                            SyntaxKind.NotEqualsExpression,
                            SyntaxFactory.MemberAccessExpression(
                                SyntaxKind.SimpleMemberAccessExpression,
                                SyntaxFactory.IdentifierName(abbreviation),
                                SyntaxFactory.IdentifierName(foreignKeyElement.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(foreignKeyElement.Columns[index].Column.Name)),
                                SyntaxFactory.LiteralExpression(
                                    SyntaxKind.NullLiteralExpression)));
                    }
                }
            }

            //            this.BuyerKey = new UniqueIndex<Buyer>("BuyerKey").HasIndex(b => b.BuyerId);
            return(SyntaxFactory.SimpleLambdaExpression(SyntaxFactory.Parameter(SyntaxFactory.Identifier(abbreviation)), syntaxNode));
        }
        /// <summary>
        /// Creates an argument that creates a lambda expression for extracting the key from a class.
        /// </summary>
        /// <param name="foreignKeyElement">The unique key element.</param>
        /// <returns>An argument that extracts a key from an object.</returns>
        public static ExpressionSyntax GetForeignKey(ForeignElement foreignKeyElement)
        {
            // Used as a variable when constructing the lambda expression.
            string abbreviation = foreignKeyElement.Table.Name[0].ToString(CultureInfo.InvariantCulture).ToLowerInvariant();

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

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

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

                // b => b.BuyerId or b => new { b.BuyerId, b.ExternalId0 }
                syntaxNode = SyntaxFactory.AnonymousObjectCreationExpression(
                    SyntaxFactory.SeparatedList <AnonymousObjectMemberDeclaratorSyntax>(keyElements.ToArray()));
            }

            //            this.BuyerKey = new ForeignIndex<Buyer>("BuyerKey").HasIndex(b => b.BuyerId);
            return(SyntaxFactory.SimpleLambdaExpression(SyntaxFactory.Parameter(SyntaxFactory.Identifier(abbreviation)), syntaxNode));
        }