Пример #1
0
        protected override Expression VisitDeclaration(DeclarationCommand decl)
        {
            if (!this.Dialect.SupportMultipleCommands)
            {
                return base.VisitDeclaration(decl);
            }

            for (int i = 0, n = decl.Variables.Count; i < n; i++)
            {
                var v = decl.Variables[i];
                if (i > 0)
                    this.AppendLine(Indentation.Same);
                this.Append("DECLARE @");
                this.Append(v.Name);
                this.Append(" ");
                this.Append(this.GetVariableDeclaration(v.SqlType, false, null));
            }
            if (decl.Source != null)
            {
                this.AppendLine(Indentation.Same);
                this.Append("SELECT ");
                for (int i = 0, n = decl.Variables.Count; i < n; i++)
                {
                    if (i > 0)
                        this.Append(", ");
                    this.Append("@");
                    this.Append(decl.Variables[i].Name);
                    this.Append(" = ");
                    this.Visit(decl.Source.Columns[i].Expression);
                }
                if (decl.Source.From != null)
                {
                    this.AppendLine(Indentation.Same);
                    this.Append("FROM ");
                    this.VisitSource(decl.Source.From);
                }
                if (decl.Source.Where != null)
                {
                    this.AppendLine(Indentation.Same);
                    this.Append("WHERE ");
                    this.Visit(decl.Source.Where);
                }
            }
            else
            {
                for (int i = 0, n = decl.Variables.Count; i < n; i++)
                {
                    var v = decl.Variables[i];
                    if (v.Expression != null)
                    {
                        this.AppendLine(Indentation.Same);
                        this.Append("SET @");
                        this.Append(v.Name);
                        this.Append(" = ");
                        this.Visit(v.Expression);
                    }
                }
            }
            return decl;
        }
Пример #2
0
 protected DeclarationCommand UpdateDeclaration(DeclarationCommand decl, IEnumerable<VariableDeclaration> variables, SelectExpression source)
 {
     if (variables != decl.Variables || source != decl.Source)
     {
         return new DeclarationCommand(variables, source);
     }
     return decl;
 }
Пример #3
0
        // make a variable declaration / initialization for dependent generated values
        private CommandExpression GetDependentGeneratedVariableDeclaration(MappingEntity entity, MappingTable table, List <MemberInfo> members, Expression instance, Dictionary <MemberInfo, Expression> map)
        {
            // first make command that retrieves the generated ids if any
            DeclarationCommand genIdCommand = null;
            var generatedIds = this.mapping.GetMappedMembers(entity).Where(m => this.mapping.IsPrimaryKey(entity, m) && this.mapping.IsGenerated(entity, m)).ToList();

            if (generatedIds.Count > 0)
            {
                genIdCommand = this.GetGeneratedIdCommand(entity, members, map);

                // if that's all there is then just return the generated ids
                if (members.Count == generatedIds.Count)
                {
                    return(genIdCommand);
                }
            }

            // next make command that retrieves the generated members
            // only consider members that were not generated ids
            members = members.Except(generatedIds).ToList();

            var tableAlias = new TableAlias();
            var tex        = new TableExpression(tableAlias, entity, this.mapping.GetTableName(table));

            Expression where = null;
            if (generatedIds.Count > 0)
            {
                where = generatedIds.Select((m, i) =>
                                            this.GetMemberExpression(tex, entity, m).Equal(map[m]))
                        .Aggregate((x, y) => x.And(y));
            }
            else
            {
                where = this.GetIdentityCheck(tex, entity, instance);
            }

            TableAlias selectAlias = new TableAlias();
            var        columns     = new List <ColumnDeclaration>();
            var        variables   = new List <VariableDeclaration>();

            foreach (var mi in members)
            {
                ColumnExpression col = (ColumnExpression)this.GetMemberExpression(tex, entity, mi);
                columns.Add(new ColumnDeclaration(this.mapping.GetColumnName(entity, mi), col, col.QueryType));
                ColumnExpression vcol = new ColumnExpression(col.Type, col.QueryType, selectAlias, col.Name);
                variables.Add(new VariableDeclaration(mi.Name, col.QueryType, vcol));
                map.Add(mi, new VariableExpression(mi.Name, col.Type, col.QueryType));
            }

            var genMembersCommand = new DeclarationCommand(variables, new SelectExpression(selectAlias, columns, tex, where));

            if (genIdCommand != null)
            {
                return(new BlockCommand(genIdCommand, genMembersCommand));
            }

            return(genMembersCommand);
        }
Пример #4
0
 protected override Expression VisitDeclaration(DeclarationCommand decl)
 {
     if (decl.Source != null)
     {
         this.Visit(decl.Source);
         return(decl);
     }
     return(base.VisitDeclaration(decl));
 }
Пример #5
0
        protected override Expression VisitDeclaration(DeclarationCommand decl)
        {
            if (decl.Source != null)
            {
                // make query that returns all these declared values as an object[]
                var projection = new ProjectionExpression(
                    decl.Source,
                    Expression.NewArrayInit(
                        typeof(object),
                        decl.Variables.Select(v => v.Expression.Type.IsValueType
                            ? Expression.Convert(v.Expression, typeof(object))
                            : v.Expression).ToArray()
                        ),
                    Aggregator.GetAggregator(typeof(object[]), typeof(IEnumerable <object[]>))
                    );

                // create execution variable to hold the array of declared variables
                var vars = Expression.Parameter(typeof(object[]), "vars");
                this.variables.Add(vars);
                this.initializers.Add(Expression.Constant(null, typeof(object[])));

                // create subsitution for each variable (so it will find the variable value in the new vars array)
                for (int i = 0, n = decl.Variables.Count; i < n; i++)
                {
                    var v = decl.Variables[i];
                    NamedValueExpression nv = new NamedValueExpression(
                        v.Name, v.SqlType,
                        Expression.Convert(Expression.ArrayIndex(vars, Expression.Constant(i)), v.Expression.Type)
                        );
                    this.variableMap.Add(v.Name, nv);
                }

                // make sure the execution of the select stuffs the results into the new vars array
                return(MakeAssign(vars, this.Visit(projection)));
            }

            // probably bad if we get here since we must not allow mulitple commands
            throw new InvalidOperationException(Res.DeclarationQueryInvalid);
        }
Пример #6
0
 protected override Expression VisitDeclaration(DeclarationCommand decl)
 {
     throw new NotSupportedException();
 }
Пример #7
0
 protected override Expression VisitDeclaration(DeclarationCommand decl)
 {
     if (decl.Source != null)
     {
         this.Visit(decl.Source);
         return decl;
     }
     return base.VisitDeclaration(decl);
 }
Пример #8
0
        // make a variable declaration / initialization for dependent generated values
        private CommandExpression GetDependentGeneratedVariableDeclaration(MappingEntity entity, MappingTable table, List<MemberInfo> members, Expression instance, Dictionary<MemberInfo, Expression> map)
        {
            // first make command that retrieves the generated ids if any
            DeclarationCommand genIdCommand = null;
            var generatedIds = this.mapping.GetMappedMembers(entity).Where(m => this.mapping.IsPrimaryKey(entity, m) && this.mapping.IsGenerated(entity, m)).ToList();
            if (generatedIds.Count > 0)
            {
                genIdCommand = this.GetGeneratedIdCommand(entity, members, map);

                // if that's all there is then just return the generated ids
                if (members.Count == generatedIds.Count)
                {
                    return genIdCommand;
                }
            }

            // next make command that retrieves the generated members
            // only consider members that were not generated ids
            members = members.Except(generatedIds).ToList();

            var tableAlias = new TableAlias();
            var tex = new TableExpression(tableAlias, entity, this.mapping.GetTableName(table));

            Expression where = null;
            if (generatedIds.Count > 0)
            {
                where = generatedIds.Select((m, i) =>
                    this.GetMemberExpression(tex, entity, m).Equal(map[m])
                    ).Aggregate((x, y) => x.And(y));
            }
            else
            {
                where = this.GetIdentityCheck(tex, entity, instance);
            }

            TableAlias selectAlias = new TableAlias();
            var columns = new List<ColumnDeclaration>();
            var variables = new List<VariableDeclaration>();
            foreach (var mi in members)
            {
                ColumnExpression col = (ColumnExpression)this.GetMemberExpression(tex, entity, mi);
                columns.Add(new ColumnDeclaration(this.mapping.GetColumnName(entity, mi), col, col.QueryType));
                ColumnExpression vcol = new ColumnExpression(col.Type, col.QueryType, selectAlias, col.Name);
                variables.Add(new VariableDeclaration(mi.Name, col.QueryType, vcol));
                map.Add(mi, new VariableExpression(mi.Name, col.Type, col.QueryType));
            }

            var genMembersCommand = new DeclarationCommand(variables, new SelectExpression(selectAlias, columns, tex, where));

            if (genIdCommand != null)
            {
                return new BlockCommand(genIdCommand, genMembersCommand);
            }

            return genMembersCommand;
        }
Пример #9
0
        protected override Expression VisitDeclaration(DeclarationCommand decl)
        {
            if (!this.Language.AllowsMultipleCommands)
            {
                return base.VisitDeclaration(decl);
            }

            for (int i = 0, n = decl.Variables.Count; i < n; i++)
            {
                var v = decl.Variables[i];
                if (i > 0)
                    this.WriteLine(Indentation.Same);
                this.Write("DECLARE @");
                this.Write(v.Name);
                this.Write(" ");
                this.Write(this.Language.TypeSystem.GetVariableDeclaration(v.QueryType, false));
            }
            if (decl.Source != null)
            {
                this.WriteLine(Indentation.Same);
                this.Write("SELECT ");
                for (int i = 0, n = decl.Variables.Count; i < n; i++)
                {
                    if (i > 0)
                        this.Write(", ");
                    this.Write("@");
                    this.Write(decl.Variables[i].Name);
                    this.Write(" = ");
                    this.Visit(decl.Source.Columns[i].Expression);
                }
                if (decl.Source.From != null)
                {
                    this.WriteLine(Indentation.Same);
                    this.Write("FROM ");
                    this.VisitSource(decl.Source.From);
                }
                if (decl.Source.Where != null)
                {
                    this.WriteLine(Indentation.Same);
                    this.Write("WHERE ");
                    this.Visit(decl.Source.Where);
                }
            }
            else
            {
                for (int i = 0, n = decl.Variables.Count; i < n; i++)
                {
                    var v = decl.Variables[i];
                    if (v.Expression != null)
                    {
                        this.WriteLine(Indentation.Same);
                        this.Write("SET @");
                        this.Write(v.Name);
                        this.Write(" = ");
                        this.Visit(v.Expression);
                    }
                }
            }
            return decl;
        }
Пример #10
0
        public virtual Expression GetInsertResult(IEntityMapping mapping, Expression instance, LambdaExpression selector, Dictionary <MemberInfo, Expression> map)
        {
            var tableAlias = new TableAlias();
            var tex        = new TableExpression(tableAlias, mapping);
            var aggregator = Aggregator.GetAggregator(selector.Body.Type, typeof(IEnumerable <>).MakeGenericType(selector.Body.Type));

            Expression where = null;
            DeclarationCommand genIdCommand = null;
            var generatedIds = mapping.PrimaryKeys.Where(m => m.IsPrimaryKey && m.IsGenerated).ToList();

            if (generatedIds.Count > 0)
            {
                if (map == null || !generatedIds.Any(m => map.ContainsKey(m.Member)))
                {
                    var localMap = new Dictionary <MemberInfo, Expression>();
                    genIdCommand = this.GetGeneratedIdCommand(mapping, generatedIds, localMap);
                    map          = localMap;
                }

                // is this just a retrieval of one generated id member?
                var mex = selector.Body as MemberExpression;
                if (mex != null)
                {
                    var id = mapping.Get(mex.Member);
                    if (id != null && id.IsPrimaryKey && id.IsGenerated)
                    {
                        if (genIdCommand != null)
                        {
                            // just use the select from the genIdCommand
                            return(new ProjectionExpression(
                                       genIdCommand.Source,
                                       new ColumnExpression(mex.Type, genIdCommand.Variables[0].SqlType, genIdCommand.Source.Alias, genIdCommand.Source.Columns[0].Name),
                                       aggregator
                                       ));
                        }
                        else
                        {
                            TableAlias alias   = new TableAlias();
                            var        colType = id.SqlType;
                            return(new ProjectionExpression(
                                       new SelectExpression(alias, new[] { new ColumnDeclaration("", map[mex.Member], colType) }, null, null),
                                       new ColumnExpression(mex.Member.GetMemberType(), colType, alias, ""),
                                       aggregator
                                       ));
                        }
                    }

                    where = generatedIds.Select((m, i) =>
                                                this.GetMemberExpression(tex, mapping, m.Member).Equal(map[m.Member])
                                                ).Aggregate((x, y) => x.And(y));
                }
            }
            else
            {
                where = this.GetIdentityCheck(tex, mapping, instance);
            }

            Expression typeProjector = this.GetEntityExpression(tex, mapping);
            Expression selection     = DbExpressionReplacer.Replace(selector.Body, selector.Parameters[0], typeProjector);
            TableAlias newAlias      = new TableAlias();
            var        pc            = ColumnProjector.ProjectColumns(selection, null, newAlias, tableAlias);
            var        pe            = new ProjectionExpression(
                new SelectExpression(newAlias, pc.Columns, tex, where),
                pc.Projector,
                aggregator
                );

            if (genIdCommand != null)
            {
                return(new BlockCommand(genIdCommand, pe));
            }
            return(pe);
        }
Пример #11
0
        protected override Expression VisitDeclaration(DeclarationCommand decl)
        {
            if (!this.Dialect.SupportMultipleCommands)
            {
                return(base.VisitDeclaration(decl));
            }

            for (int i = 0, n = decl.Variables.Count; i < n; i++)
            {
                var v = decl.Variables[i];
                if (i > 0)
                {
                    this.AppendLine(Indentation.Same);
                }
                this.Append("DECLARE @");
                this.Append(v.Name);
                this.Append(" ");
                this.Append(this.GetVariableDeclaration(v.SqlType, false, null));
            }
            if (decl.Source != null)
            {
                this.AppendLine(Indentation.Same);
                this.Append("SELECT ");
                for (int i = 0, n = decl.Variables.Count; i < n; i++)
                {
                    if (i > 0)
                    {
                        this.Append(", ");
                    }
                    this.Append("@");
                    this.Append(decl.Variables[i].Name);
                    this.Append(" = ");
                    this.Visit(decl.Source.Columns[i].Expression);
                }
                if (decl.Source.From != null)
                {
                    this.AppendLine(Indentation.Same);
                    this.Append("FROM ");
                    this.VisitSource(decl.Source.From);
                }
                if (decl.Source.Where != null)
                {
                    this.AppendLine(Indentation.Same);
                    this.Append("WHERE ");
                    this.Visit(decl.Source.Where);
                }
            }
            else
            {
                for (int i = 0, n = decl.Variables.Count; i < n; i++)
                {
                    var v = decl.Variables[i];
                    if (v.Expression != null)
                    {
                        this.AppendLine(Indentation.Same);
                        this.Append("SET @");
                        this.Append(v.Name);
                        this.Append(" = ");
                        this.Visit(v.Expression);
                    }
                }
            }
            return(decl);
        }
Пример #12
0
        protected override Expression VisitDeclaration(DeclarationCommand decl)
        {
            VariableDeclaration declaration;

            if (!this.Language.AllowsMultipleCommands)
            {
                return(base.VisitDeclaration(decl));
            }
            int num   = 0;
            int count = decl.Variables.Count;

            while (num < count)
            {
                declaration = decl.Variables[num];
                if (num > 0)
                {
                    base.WriteLine(SqlFormatter.Indentation.Same);
                }
                base.Write("DECLARE @");
                base.Write(declaration.Name);
                base.Write(" ");
                base.Write(this.Language.TypeSystem.GetVariableDeclaration(declaration.QueryType, false));
                num++;
            }
            if (decl.Source != null)
            {
                base.WriteLine(SqlFormatter.Indentation.Same);
                base.Write("SELECT ");
                num   = 0;
                count = decl.Variables.Count;
                while (num < count)
                {
                    if (num > 0)
                    {
                        base.Write(", ");
                    }
                    base.Write("@");
                    base.Write(decl.Variables[num].Name);
                    base.Write(" = ");
                    this.Visit(decl.Source.Columns[num].Expression);
                    num++;
                }
                if (decl.Source.From != null)
                {
                    base.WriteLine(SqlFormatter.Indentation.Same);
                    base.Write("FROM ");
                    this.VisitSource(decl.Source.From);
                }
                if (decl.Source.Where != null)
                {
                    base.WriteLine(SqlFormatter.Indentation.Same);
                    base.Write("WHERE ");
                    this.Visit(decl.Source.Where);
                }
                return(decl);
            }
            num   = 0;
            count = decl.Variables.Count;
            while (num < count)
            {
                declaration = decl.Variables[num];
                if (declaration.Expression != null)
                {
                    base.WriteLine(SqlFormatter.Indentation.Same);
                    base.Write("SET @");
                    base.Write(declaration.Name);
                    base.Write(" = ");
                    this.Visit(declaration.Expression);
                }
                num++;
            }
            return(decl);
        }
Пример #13
0
 protected virtual Expression VisitDeclaration(DeclarationCommand decl)
 {
     var variables = this.VisitVariableDeclarations(decl.Variables);
     var source = (SelectExpression)this.Visit(decl.Source);
     return this.UpdateDeclaration(decl, variables, source);
 }
Пример #14
0
 public void addDeclaration(DeclarationCommand declarationCommand)
 {
     sequentialDeclaration.AddLast(declarationCommand);
 }
Пример #15
0
        protected override Expression VisitDeclaration(DeclarationCommand decl)
        {
            if (!this.Language.AllowsMultipleCommands)
            {
                return(base.VisitDeclaration(decl));
            }

            for (int i = 0, n = decl.Variables.Count; i < n; i++)
            {
                var v = decl.Variables[i];
                if (i > 0)
                {
                    this.WriteLine(Indentation.Same);
                }
                this.Write("DECLARE @");
                this.Write(v.Name);
                this.Write(" ");
                this.Write(this.Language.TypeSystem.Format(v.QueryType, false));
            }
            if (decl.Source != null)
            {
                this.WriteLine(Indentation.Same);
                this.Write("SELECT ");
                for (int i = 0, n = decl.Variables.Count; i < n; i++)
                {
                    if (i > 0)
                    {
                        this.Write(", ");
                    }
                    this.Write("@");
                    this.Write(decl.Variables[i].Name);
                    this.Write(" = ");
                    this.Visit(decl.Source.Columns[i].Expression);
                }
                if (decl.Source.From != null)
                {
                    this.WriteLine(Indentation.Same);
                    this.Write("FROM ");
                    this.VisitSource(decl.Source.From);
                }
                if (decl.Source.Where != null)
                {
                    this.WriteLine(Indentation.Same);
                    this.Write("WHERE ");
                    this.Visit(decl.Source.Where);
                }
            }
            else
            {
                for (int i = 0, n = decl.Variables.Count; i < n; i++)
                {
                    var v = decl.Variables[i];
                    if (v.Expression != null)
                    {
                        this.WriteLine(Indentation.Same);
                        this.Write("SET @");
                        this.Write(v.Name);
                        this.Write(" = ");
                        this.Visit(v.Expression);
                    }
                }
            }
            return(decl);
        }
Пример #16
0
 /// <summary>
 /// Visits the declaration.
 /// </summary>
 /// <param name="decl">The decl.</param>
 /// <returns></returns>
 protected override Expression VisitDeclaration(DeclarationCommand decl)
 {
     throw new NotSupportedException();
 }