コード例 #1
0
 public AggregateFunctionModel(SqlObjectName name, MethodModel method, FunctionMetadata metadata, IType returnType)
     : base(name, method, metadata)
 {
     ReturnType = returnType;
 }
コード例 #2
0
        /// <summary>
        /// Generates association extension method in extensions class for one side of association.
        /// </summary>
        /// <param name="extensionEntityAssociations">Association methods groupsfor each entity.</param>
        /// <param name="extensionAssociations">Association extensions region provider.</param>
        /// <param name="thisEntity">Entity class for this side of assocation (used for extension <c>this</c> parameter).</param>
        /// <param name="resultEntity">Entity class for other side of assocation (used for extension result type).</param>
        /// <param name="type">Association result type.</param>
        /// <param name="extensionModel">Extension method model.</param>
        /// <param name="metadata">Association methodo metadata.</param>
        /// <param name="associationModel">Association model.</param>
        /// <param name="backReference">Identifies current side of assocation.</param>
        private void BuildAssociationExtension(
            Dictionary <EntityModel, MethodGroup> extensionEntityAssociations,
            Func <RegionGroup> extensionAssociations,
            ClassBuilder thisEntity,
            ClassBuilder resultEntity,
            IType type,
            MethodModel extensionModel,
            AssociationMetadata metadata,
            AssociationModel associationModel,
            bool backReference)
        {
            // create (if missing) assocations region for specific owner (this) entity
            var key = backReference ? associationModel.Target : associationModel.Source;

            if (!extensionEntityAssociations.TryGetValue(key, out var associations))
            {
                extensionEntityAssociations.Add(
                    key,
                    associations = extensionAssociations()
                                   .New(string.Format(EXTENSIONS_ENTITY_ASSOCIATIONS_REGION, thisEntity.Type.Name.Name))
                                   .Methods(false));
            }

            // define extension method
            var methodBuilder = DefineMethod(associations, extensionModel).Returns(type);

            // and it's metadata
            _metadataBuilder.BuildAssociationMetadata(metadata, methodBuilder);

            // build method parameters...
            var thisParam = AST.Parameter(thisEntity.Type.Type, AST.Name(EXTENSIONS_ENTITY_THIS_PARAMETER), CodeParameterDirection.In);
            var ctxParam  = AST.Parameter(WellKnownTypes.LinqToDB.IDataContext, AST.Name(EXTENSIONS_ENTITY_CONTEXT_PARAMETER), CodeParameterDirection.In);

            methodBuilder.Parameter(thisParam);
            methodBuilder.Parameter(ctxParam);

            // ... and body
            if (associationModel.FromColumns == null || associationModel.ToColumns == null)
            {
                // association doesn't specify relation columns (e.g. defined usign expression method)
                // so we should generate exception for non-query execution
                methodBuilder
                .Body()
                .Append(
                    AST.Throw(
                        AST.New(
                            WellKnownTypes.System.InvalidOperationException,
                            AST.Constant(EXCEPTION_QUERY_ONLY_ASSOCATION_CALL, true))));
            }
            else
            {
                // generate association query for non-query invocation

                // As method body here could conflict with custom return type for many-to-one assocation
                // we forcebly override return type here to IQueryable<T>
                if (associationModel.ManyToOne && backReference)
                {
                    methodBuilder.Returns(WellKnownTypes.System.Linq.IQueryable(resultEntity.Type.Type));
                }

                var lambdaParam = AST.LambdaParameter(AST.Name(EXTENSIONS_ASSOCIATION_FILTER_PARAMETER), resultEntity.Type.Type);

                // generate assocation key columns filter, which compare
                // `this` entity parameter columns with return table entity columns
                var fromObject = backReference ? lambdaParam : thisParam;
                var toObject   = !backReference ? lambdaParam : thisParam;

                ICodeExpression?filter = null;

                for (var i = 0; i < associationModel.FromColumns.Length; i++)
                {
                    var fromColumn = _columnProperties[associationModel.FromColumns[i]];
                    var toColumn   = _columnProperties[associationModel.ToColumns[i]];

                    var cond = AST.Equal(
                        AST.Member(fromObject.Reference, fromColumn.Reference),
                        AST.Member(toObject.Reference, toColumn.Reference));

                    filter = filter == null ? cond : AST.And(filter, cond);
                }

                // generate filter lambda function
                var filterLambda = AST
                                   .Lambda(
                    WellKnownTypes.System.Linq.Expressions.Expression(
                        WellKnownTypes.System.Func(WellKnownTypes.System.Boolean, resultEntity.Type.Type)),
                    true)
                                   .Parameter(lambdaParam);
                filterLambda.Body().Append(AST.Return(filter !));

                // ctx.GetTable<ResultEntity>()
                var body = AST.ExtCall(
                    WellKnownTypes.LinqToDB.DataExtensions,
                    WellKnownTypes.LinqToDB.DataExtensions_GetTable,
                    WellKnownTypes.LinqToDB.ITable(resultEntity.Type.Type),
                    new[] { resultEntity.Type.Type },
                    false,
                    ctxParam.Reference);

                // append First/FirstOrDefault (for optional association)
                // for non-many relation
                if (!backReference || !associationModel.ManyToOne)
                {
                    var optional = backReference ? associationModel.TargetMetadata.CanBeNull : associationModel.SourceMetadata.CanBeNull;

                    // .First(t => t.PK == thisEntity.PK)
                    // or
                    // .FirstOrDefault(t => t.PK == thisEntity.PK)
                    body = AST.ExtCall(
                        WellKnownTypes.System.Linq.Queryable,
                        optional ? WellKnownTypes.System.Linq.Queryable_FirstOrDefault : WellKnownTypes.System.Linq.Queryable_First,
                        resultEntity.Type.Type.WithNullability(optional),
                        new[] { resultEntity.Type.Type },
                        true,
                        body,
                        filterLambda.Method);
                }
                else
                {
                    // .Where(t => t.PK == thisEntity.PK)
                    body = AST.ExtCall(
                        WellKnownTypes.System.Linq.Queryable,
                        WellKnownTypes.System.Linq.Queryable_Where,
                        WellKnownTypes.System.Linq.IQueryable(resultEntity.Type.Type),
                        new [] { resultEntity.Type.Type },
                        true,
                        body,
                        filterLambda.Method);
                }

                methodBuilder.Body().Append(AST.Return(body));
            }
        }
コード例 #3
0
 protected TableFunctionModelBase(SqlObjectName name, MethodModel method)
     : base(name, method)
 {
     Name = name;
 }
コード例 #4
0
 protected FunctionModelBase(SqlObjectName name, MethodModel method)
 {
     Method = method;
 }
コード例 #5
0
 protected ScalarFunctionModelBase(SqlObjectName name, MethodModel method, FunctionMetadata metadata)
     : base(name, method)
 {
     Metadata = metadata;
 }
コード例 #6
0
 public StoredProcedureModel(SqlObjectName name, MethodModel method)
     : base(name, method)
 {
 }
コード例 #7
0
 public ScalarFunctionModel(SqlObjectName name, MethodModel method, FunctionMetadata metadata)
     : base(name, method, metadata)
 {
 }