StaticContext.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Dotnetfx_Vista_SP2 / Dotnetfx_Vista_SP2 / 8.0.50727.4016 / DEVDIV / depot / DevDiv / releases / Orcas / QFE / ndp / fx / src / DataEntity / System / Data / Common / EntitySql / StaticContext.cs / 2 / StaticContext.cs

                            //---------------------------------------------------------------------- 
// 
//      Copyright ( c ) Microsoft Corporation.  All rights reserved.
// 
// 
// @owner  [....]
// @backup [....] 
//--------------------------------------------------------------------- 

 
namespace System.Data.Common.EntitySql
{
    using System;
    using System.Collections.Generic; 
    using System.Text;
    using System.Diagnostics; 
 
    using System.Data.Metadata.Edm;
    using System.Data.Common.CommandTrees; 
    using System.Data.Entity;

    /// 
    /// Represents generic stacked scopes of Key-Value pairs. 
    /// 
    /// Thrown when trying to add a key that alredy exisits in the current scope 
    internal class ScopeManager 
    {
        IEqualityComparer _keyComparer; 
        List _stackedScopes = new List();


        ///  
        /// initializes scope manager using given key-string comparer
        ///  
        ///  
        internal ScopeManager( IEqualityComparer keyComparer )
        { 
            _keyComparer = keyComparer;
        }

 
        /// 
        /// Pushes new scope 
        ///  
        /// 
        internal ScopeManager EnterScope() 
        {
            _stackedScopes.Add(new Scope(_keyComparer));
            return this;
        } 

 
        ///  
        /// Pops the current scope
        ///  
        internal void LeaveScope()
        {
            Debug.Assert(CurrentScopeIndex >= 0);
            _stackedScopes.RemoveAt(CurrentScopeIndex); 
        }
 
 
        /// 
        /// Returns current scope index 
        /// 
        internal int CurrentScopeIndex
        {
            get 
            {
                return (_stackedScopes.Count - 1); 
            } 
        }
 

        /// 
        /// returns current scope
        ///  
        internal Scope CurrentScope
        { 
            get 
            {
                return _stackedScopes[CurrentScopeIndex]; 
            }
        }

        ///  
        /// Get a given scope by its index
        ///  
        ///  
        /// 
        internal Scope GetScopeByIndex( int scopeIndex ) 
        {
            Debug.Assert(scopeIndex >= 0,"scopeIndex >= 0");
            Debug.Assert(scopeIndex <= CurrentScopeIndex, "scopeIndex <= CurrentScopeIndex");
            if (0 > scopeIndex || scopeIndex > CurrentScopeIndex) 
            {
                throw EntityUtil.EntitySqlError(Strings.InvalidScopeIndex); 
            } 
            return _stackedScopes[scopeIndex];
        } 


        /// 
        /// removes a entry from current scope 
        /// 
        ///  
        internal void RemoveFromScope( string key ) 
        {
            CurrentScope.Remove(key); 
        }


        ///  
        /// Add new item to the Current Scope
        ///  
        ///  
        /// 
        internal void Add( string key, ScopeEntry value ) 
        {
            CurrentScope.Add(key, value);
        }
 
        /// 
        /// Restores the Context to a previously created SavePoint 
        ///  
        /// 
        ///  
        /// 
        /// 
        internal void RollbackToSavepoint( SavePoint savePoint )
        { 
            //
            // assert preconditions 
            // 
            Debug.Assert(savePoint.ScopeIndex >= 0, "[PRE] savePoint.ScopeIndex >= 0");
            Debug.Assert(savePoint.ScopeIndex <= CurrentScopeIndex, "[PRE] savePoint.ScopeIndex <= CurrentScopeIndex"); 
            Debug.Assert(CurrentScopeIndex >= 0, "[PRE] CurrentScopeIndex >= 0");

            if (savePoint.ScopeIndex > CurrentScopeIndex || savePoint.ScopeIndex < 0 || CurrentScopeIndex < 0)
            { 
                throw EntityUtil.EntitySqlError(Strings.InvalidSavePoint);
            } 
 
            int delta = CurrentScopeIndex - savePoint.ScopeIndex;
            if (delta > 0) 
            {
                _stackedScopes.RemoveRange(savePoint.ScopeIndex + 1, CurrentScopeIndex - savePoint.ScopeIndex);
            }
 
            //
            // make sure invariants are preserved 
            // 
            Debug.Assert(savePoint.ScopeIndex == CurrentScopeIndex, "[POST] savePoint.ScopeIndex == CurrentScopeIndex");
            Debug.Assert(CurrentScopeIndex >= 0, "[POST] CurrentScopeIndex >= 0"); 

        }

 
        /// 
        /// Returns true if key exists in current scope. 
        ///  
        /// 
        ///  
        internal bool IsInCurrentScope( string key )
        {
            return CurrentScope.Contains(key);
        } 

    } 
 

    ///  
    /// Defines scope entry Kind
    /// 
    enum ScopeEntryKind
    { 
        WithSourceVar,
        SourceVar, 
        JoinSourceVar, 
        ApplySourceVar,
        ProjectList, 
        DummyGroupKey
    }

 
    /// 
    /// Represents a scope of key-value pairs. 
    ///  
    internal sealed class Scope : IEnumerable>
    { 
        Dictionary scopeEntries;

        /// 
        /// initializes using a given key comparer 
        /// 
        ///  
        internal Scope( IEqualityComparer keyComparer ) 
        {
            scopeEntries = new Dictionary(keyComparer); 
        }

        /// 
        /// add new key to the scope. if key already exists throws key already exists exception. 
        /// 
        ///  
        ///  
        /// 
        internal Scope Add( string key, ScopeEntry value ) 
        {
            scopeEntries.Add(key, value);
            return this;
        } 

        ///  
        /// removes a entry from the scope 
        /// 
        ///  
        internal void Remove( string key )
        {
            scopeEntries.Remove(key);
        } 

        ///  
        /// searches item by key. returns true in case of success and false otherwise. 
        /// 
        ///  
        /// 
        /// 
        internal bool TryLookup( string key, out ScopeEntry value )
        { 
            return (scopeEntries.TryGetValue(key, out value));
        } 
 
        /// 
        /// returns if a given key belongs to the scope 
        /// 
        /// 
        /// 
        internal bool Contains( string key ) 
        {
            return scopeEntries.ContainsKey(key); 
        } 

        #region GetEnumerator 
        public Dictionary.Enumerator GetEnumerator()
        {
            return scopeEntries.GetEnumerator();
        } 

        System.Collections.Generic.IEnumerator> System.Collections.Generic.IEnumerable>.GetEnumerator() 
        { 
            return scopeEntries.GetEnumerator();
        } 

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return scopeEntries.GetEnumerator(); 
        }
        #endregion 
    } 

    ///  
    /// Represents a context savepoint that can be restored later.
    /// 
    internal sealed class SavePoint
    { 
        private int _scopeIndex;
 
        ///  
        /// initializes a savepoint
        ///  
        /// 
        internal SavePoint( int scopeIndex )
        {
            _scopeIndex = scopeIndex; 
        }
 
        ///  
        /// returns save point start scope index
        ///  
        internal int ScopeIndex
        {
            get { return _scopeIndex; }
        } 

        ///  
        /// Returns true if given scope entry is in the save point 
        /// 
        ///  
        /// 
        internal bool ContainsScope( int scopeIndex )
        {
            return (scopeIndex >= _scopeIndex); 
        }
    } 
 
    internal enum SourceVarKind
    { 
        Input,
        GroupInput,
        GroupKey,
        GroupAggregate 
    }
 
    ///  
    /// Represents an entry in the scope
    ///  
    internal abstract class ScopeEntry
    {
        private ScopeEntryKind _scopeEntryKind;
        private bool _isHidden = false; 
        protected DbExpression _expression;
        protected SourceVarKind _varKind; 
 
        internal ScopeEntry( ScopeEntryKind scopeEntryKind, DbExpression expression )
        { 
            _scopeEntryKind = scopeEntryKind;
            _expression = expression;
            _varKind = SourceVarKind.Input;
        } 

        internal virtual DbExpression Expression 
        { 
            get { return _expression; }
        } 

        internal ScopeEntryKind Kind
        {
            get { return _scopeEntryKind; } 
            set { _scopeEntryKind = value; }
        } 
 
        internal bool IsHidden
        { 
            get { return _isHidden; }
        }

        public SourceVarKind VarKind 
        {
            get { return _varKind; } 
            set { _varKind = value; } 
        }
 
    }


    ///  
    /// Represents simple source var scope entry
    ///  
    internal class SourceScopeEntry : ScopeEntry 
    {
        private DbVariableReferenceExpression _varRef; 
        private List _prefixNames = new List();
        private string _varName;
        private bool _isSourceVarDirty = true;
        private DbVariableReferenceExpression _groupVar; 
        private DbExpression _aggregateExpression;
        private bool _isGroupVarDirty = true; 
        private string _varTag; 

        internal SourceScopeEntry( ScopeEntryKind scopeEntryKind, string varName, DbVariableReferenceExpression sourceVar ) 
            : base(scopeEntryKind, sourceVar)
        {
            _varName = varName;
            _varRef = sourceVar; 
        }
 
        internal SourceScopeEntry(ScopeEntryKind scopeEntryKind, string varName, DbVariableReferenceExpression sourceVar, SourceVarKind varKind) 
            : base(scopeEntryKind, sourceVar)
        { 
            _varName = varName;
            _varRef = sourceVar;
            _varKind = varKind;
        } 

        internal SourceScopeEntry( ScopeEntryKind scopeEntryKind, DbExpressionBinding sourceBinding, string varTag ) 
            : base(scopeEntryKind, sourceBinding.Variable) 
        {
            _varName = sourceBinding.VariableName; 
            _varRef = sourceBinding.Variable;
            _varTag = varTag;
        }
 
        /// 
        /// returns scope variable expression 
        ///  
        internal override DbExpression Expression
        { 
            get
            {
                if (_isSourceVarDirty)
                { 
                    _expression = RecalculateSourceVar(_varRef);
 
                    _isSourceVarDirty = false; 
                }
 
                return _expression;
            }
        }
 
        /// 
        /// adds new prefix to the 'property chain' 
        ///  
        /// 
        internal void AddBindingPrefix( string prefixName ) 
        {
            _isGroupVarDirty = true;
            _isSourceVarDirty = true;
            _prefixNames.Insert(0, prefixName); 
        }
 
        ///  
        /// sets new source binding var
        ///  
        /// 
        internal void SetNewSourceBinding( DbVariableReferenceExpression sourceVar )
        {
            _isGroupVarDirty = true; 
            _isSourceVarDirty = true;
            _varRef = sourceVar; 
        } 

        ///  
        /// recalculates var reference expression
        /// 
        private DbExpression RecalculateSourceVar( DbVariableReferenceExpression baseExpression )
        { 
            DbCommandTree cmdTree = baseExpression.CommandTree;
            DbExpression propExpr = baseExpression; 
            DbExpression result = null; 

            if (_prefixNames.Count > 0) 
            {
                for (int i = 1 ; i < _prefixNames.Count ; i++)
                {
                    propExpr = cmdTree.CreatePropertyExpression(_prefixNames[i], propExpr); 
                }
 
                result = cmdTree.CreatePropertyExpression(_varName, propExpr); 
            }
            else 
            {
                result = baseExpression;
            }
 
            return result;
        } 
 

        internal DbVariableReferenceExpression GroupVarExpression 
        {
            get { return _groupVar; }
            set { _groupVar = value; }
        } 

 
        internal DbExpression AggregateExpression 
        {
            get 
            {
                if (_isGroupVarDirty)
                {
                    _aggregateExpression = RecalculateSourceVar(_groupVar); 

                    _isGroupVarDirty = false; 
                } 

                return _aggregateExpression; 
            }
        }

        internal string VarTag 
        {
            get 
            { 
                return _varTag;
            } 
        }
    }

 
    /// 
    /// represents a projection expression scope entry 
    ///  
    internal class ProjectionScopeEntry : ScopeEntry
    { 
        private string _varName;

        internal ProjectionScopeEntry( string varName, DbExpression expression )
            : base(ScopeEntryKind.ProjectList, expression) 
        {
            _varName = varName; 
        } 

        ///  
        /// returns projection scope entry
        /// 
        internal override DbExpression Expression
        { 
            get { return _expression; }
        } 
    } 

    ///  
    /// represents Dummy group keys to be used during group aggregate search mode
    /// these entries will then be replaced by the real key expression once the group
    /// expression is built
    ///  
    internal class DummyGroupVarScopeEntry : ScopeEntry
    { 
        private DbExpression _aggreateExpression; 

        internal DummyGroupVarScopeEntry(DbExpression varBasedExpression, DbExpression groupVarBasedExpression) 
            : base(ScopeEntryKind.DummyGroupKey, varBasedExpression)
        {
            _aggreateExpression = groupVarBasedExpression;
            base.VarKind = SourceVarKind.GroupKey; 
        }
 
        ///  
        /// returns group var based expression
        ///  
        internal DbExpression AggregateExpression
        {
            get { return _aggreateExpression; }
        } 
    }
 
    ///  
    /// Represents static context for semantic analysis
    ///  
    internal sealed class StaticContext : ScopeManager
    {
        /// 
        /// initializes with specific keycomparer 
        /// 
        ///  
        internal StaticContext( IEqualityComparer keyComparer ) 
            : base(keyComparer)
        { 
        }

        /// 
        /// Adds current expression to scope according to curr scope kind 
        /// 
        ///  
        ///  
        /// 
        internal void AddSourceBinding( DbExpressionBinding sourceBinding, ScopeEntryKind scopeEntryKind , string varTag) 
        {
            Add(sourceBinding.VariableName, new SourceScopeEntry(scopeEntryKind, sourceBinding, varTag));
        }
 
        /// 
        /// adds a dummy group key to scope 
        ///  
        /// 
        ///  
        /// 
        internal void AddGroupDummyVar(string groupKey, DbExpression varBasedExpression, DbExpression groupVarBasedExpression)
        {
            Add(groupKey, new DummyGroupVarScopeEntry(varBasedExpression, groupVarBasedExpression)); 
        }
 
        ///  
        /// replaces dummy group keys added during the group aggregate search phase by the real group key expressions
        ///  
        /// 
        /// 
        internal void ReplaceGroupVarInScope( string groupVarName, DbVariableReferenceExpression groupSourceBinding )
        { 
            //
            // ensure the key exists 
            // 
            if (!IsInCurrentScope(groupVarName))
            { 
                throw EntityUtil.EntitySqlError(Strings.CouldNotFindAggregateKey);
            }

            // 
            // remove dummy key added previously
            // 
            RemoveFromScope(groupVarName); 

            // 
            // add real group key to scope
            //
            Add(groupVarName, new SourceScopeEntry(ScopeEntryKind.SourceVar, groupVarName, groupSourceBinding, SourceVarKind.GroupKey));
        } 

 
        ///  
        /// add group aggregate functions to the scope
        ///  
        /// 
        /// 
        internal void AddAggregateToScope( string aggregateName, DbVariableReferenceExpression sourceVar )
        { 
            Add(aggregateName, new SourceScopeEntry(ScopeEntryKind.SourceVar, aggregateName, sourceVar, SourceVarKind.GroupAggregate));
        } 
 
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//---------------------------------------------------------------------- 
// 
//      Copyright ( c ) Microsoft Corporation.  All rights reserved.
// 
// 
// @owner  [....]
// @backup [....] 
//--------------------------------------------------------------------- 

 
namespace System.Data.Common.EntitySql
{
    using System;
    using System.Collections.Generic; 
    using System.Text;
    using System.Diagnostics; 
 
    using System.Data.Metadata.Edm;
    using System.Data.Common.CommandTrees; 
    using System.Data.Entity;

    /// 
    /// Represents generic stacked scopes of Key-Value pairs. 
    /// 
    /// Thrown when trying to add a key that alredy exisits in the current scope 
    internal class ScopeManager 
    {
        IEqualityComparer _keyComparer; 
        List _stackedScopes = new List();


        ///  
        /// initializes scope manager using given key-string comparer
        ///  
        ///  
        internal ScopeManager( IEqualityComparer keyComparer )
        { 
            _keyComparer = keyComparer;
        }

 
        /// 
        /// Pushes new scope 
        ///  
        /// 
        internal ScopeManager EnterScope() 
        {
            _stackedScopes.Add(new Scope(_keyComparer));
            return this;
        } 

 
        ///  
        /// Pops the current scope
        ///  
        internal void LeaveScope()
        {
            Debug.Assert(CurrentScopeIndex >= 0);
            _stackedScopes.RemoveAt(CurrentScopeIndex); 
        }
 
 
        /// 
        /// Returns current scope index 
        /// 
        internal int CurrentScopeIndex
        {
            get 
            {
                return (_stackedScopes.Count - 1); 
            } 
        }
 

        /// 
        /// returns current scope
        ///  
        internal Scope CurrentScope
        { 
            get 
            {
                return _stackedScopes[CurrentScopeIndex]; 
            }
        }

        ///  
        /// Get a given scope by its index
        ///  
        ///  
        /// 
        internal Scope GetScopeByIndex( int scopeIndex ) 
        {
            Debug.Assert(scopeIndex >= 0,"scopeIndex >= 0");
            Debug.Assert(scopeIndex <= CurrentScopeIndex, "scopeIndex <= CurrentScopeIndex");
            if (0 > scopeIndex || scopeIndex > CurrentScopeIndex) 
            {
                throw EntityUtil.EntitySqlError(Strings.InvalidScopeIndex); 
            } 
            return _stackedScopes[scopeIndex];
        } 


        /// 
        /// removes a entry from current scope 
        /// 
        ///  
        internal void RemoveFromScope( string key ) 
        {
            CurrentScope.Remove(key); 
        }


        ///  
        /// Add new item to the Current Scope
        ///  
        ///  
        /// 
        internal void Add( string key, ScopeEntry value ) 
        {
            CurrentScope.Add(key, value);
        }
 
        /// 
        /// Restores the Context to a previously created SavePoint 
        ///  
        /// 
        ///  
        /// 
        /// 
        internal void RollbackToSavepoint( SavePoint savePoint )
        { 
            //
            // assert preconditions 
            // 
            Debug.Assert(savePoint.ScopeIndex >= 0, "[PRE] savePoint.ScopeIndex >= 0");
            Debug.Assert(savePoint.ScopeIndex <= CurrentScopeIndex, "[PRE] savePoint.ScopeIndex <= CurrentScopeIndex"); 
            Debug.Assert(CurrentScopeIndex >= 0, "[PRE] CurrentScopeIndex >= 0");

            if (savePoint.ScopeIndex > CurrentScopeIndex || savePoint.ScopeIndex < 0 || CurrentScopeIndex < 0)
            { 
                throw EntityUtil.EntitySqlError(Strings.InvalidSavePoint);
            } 
 
            int delta = CurrentScopeIndex - savePoint.ScopeIndex;
            if (delta > 0) 
            {
                _stackedScopes.RemoveRange(savePoint.ScopeIndex + 1, CurrentScopeIndex - savePoint.ScopeIndex);
            }
 
            //
            // make sure invariants are preserved 
            // 
            Debug.Assert(savePoint.ScopeIndex == CurrentScopeIndex, "[POST] savePoint.ScopeIndex == CurrentScopeIndex");
            Debug.Assert(CurrentScopeIndex >= 0, "[POST] CurrentScopeIndex >= 0"); 

        }

 
        /// 
        /// Returns true if key exists in current scope. 
        ///  
        /// 
        ///  
        internal bool IsInCurrentScope( string key )
        {
            return CurrentScope.Contains(key);
        } 

    } 
 

    ///  
    /// Defines scope entry Kind
    /// 
    enum ScopeEntryKind
    { 
        WithSourceVar,
        SourceVar, 
        JoinSourceVar, 
        ApplySourceVar,
        ProjectList, 
        DummyGroupKey
    }

 
    /// 
    /// Represents a scope of key-value pairs. 
    ///  
    internal sealed class Scope : IEnumerable>
    { 
        Dictionary scopeEntries;

        /// 
        /// initializes using a given key comparer 
        /// 
        ///  
        internal Scope( IEqualityComparer keyComparer ) 
        {
            scopeEntries = new Dictionary(keyComparer); 
        }

        /// 
        /// add new key to the scope. if key already exists throws key already exists exception. 
        /// 
        ///  
        ///  
        /// 
        internal Scope Add( string key, ScopeEntry value ) 
        {
            scopeEntries.Add(key, value);
            return this;
        } 

        ///  
        /// removes a entry from the scope 
        /// 
        ///  
        internal void Remove( string key )
        {
            scopeEntries.Remove(key);
        } 

        ///  
        /// searches item by key. returns true in case of success and false otherwise. 
        /// 
        ///  
        /// 
        /// 
        internal bool TryLookup( string key, out ScopeEntry value )
        { 
            return (scopeEntries.TryGetValue(key, out value));
        } 
 
        /// 
        /// returns if a given key belongs to the scope 
        /// 
        /// 
        /// 
        internal bool Contains( string key ) 
        {
            return scopeEntries.ContainsKey(key); 
        } 

        #region GetEnumerator 
        public Dictionary.Enumerator GetEnumerator()
        {
            return scopeEntries.GetEnumerator();
        } 

        System.Collections.Generic.IEnumerator> System.Collections.Generic.IEnumerable>.GetEnumerator() 
        { 
            return scopeEntries.GetEnumerator();
        } 

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return scopeEntries.GetEnumerator(); 
        }
        #endregion 
    } 

    ///  
    /// Represents a context savepoint that can be restored later.
    /// 
    internal sealed class SavePoint
    { 
        private int _scopeIndex;
 
        ///  
        /// initializes a savepoint
        ///  
        /// 
        internal SavePoint( int scopeIndex )
        {
            _scopeIndex = scopeIndex; 
        }
 
        ///  
        /// returns save point start scope index
        ///  
        internal int ScopeIndex
        {
            get { return _scopeIndex; }
        } 

        ///  
        /// Returns true if given scope entry is in the save point 
        /// 
        ///  
        /// 
        internal bool ContainsScope( int scopeIndex )
        {
            return (scopeIndex >= _scopeIndex); 
        }
    } 
 
    internal enum SourceVarKind
    { 
        Input,
        GroupInput,
        GroupKey,
        GroupAggregate 
    }
 
    ///  
    /// Represents an entry in the scope
    ///  
    internal abstract class ScopeEntry
    {
        private ScopeEntryKind _scopeEntryKind;
        private bool _isHidden = false; 
        protected DbExpression _expression;
        protected SourceVarKind _varKind; 
 
        internal ScopeEntry( ScopeEntryKind scopeEntryKind, DbExpression expression )
        { 
            _scopeEntryKind = scopeEntryKind;
            _expression = expression;
            _varKind = SourceVarKind.Input;
        } 

        internal virtual DbExpression Expression 
        { 
            get { return _expression; }
        } 

        internal ScopeEntryKind Kind
        {
            get { return _scopeEntryKind; } 
            set { _scopeEntryKind = value; }
        } 
 
        internal bool IsHidden
        { 
            get { return _isHidden; }
        }

        public SourceVarKind VarKind 
        {
            get { return _varKind; } 
            set { _varKind = value; } 
        }
 
    }


    ///  
    /// Represents simple source var scope entry
    ///  
    internal class SourceScopeEntry : ScopeEntry 
    {
        private DbVariableReferenceExpression _varRef; 
        private List _prefixNames = new List();
        private string _varName;
        private bool _isSourceVarDirty = true;
        private DbVariableReferenceExpression _groupVar; 
        private DbExpression _aggregateExpression;
        private bool _isGroupVarDirty = true; 
        private string _varTag; 

        internal SourceScopeEntry( ScopeEntryKind scopeEntryKind, string varName, DbVariableReferenceExpression sourceVar ) 
            : base(scopeEntryKind, sourceVar)
        {
            _varName = varName;
            _varRef = sourceVar; 
        }
 
        internal SourceScopeEntry(ScopeEntryKind scopeEntryKind, string varName, DbVariableReferenceExpression sourceVar, SourceVarKind varKind) 
            : base(scopeEntryKind, sourceVar)
        { 
            _varName = varName;
            _varRef = sourceVar;
            _varKind = varKind;
        } 

        internal SourceScopeEntry( ScopeEntryKind scopeEntryKind, DbExpressionBinding sourceBinding, string varTag ) 
            : base(scopeEntryKind, sourceBinding.Variable) 
        {
            _varName = sourceBinding.VariableName; 
            _varRef = sourceBinding.Variable;
            _varTag = varTag;
        }
 
        /// 
        /// returns scope variable expression 
        ///  
        internal override DbExpression Expression
        { 
            get
            {
                if (_isSourceVarDirty)
                { 
                    _expression = RecalculateSourceVar(_varRef);
 
                    _isSourceVarDirty = false; 
                }
 
                return _expression;
            }
        }
 
        /// 
        /// adds new prefix to the 'property chain' 
        ///  
        /// 
        internal void AddBindingPrefix( string prefixName ) 
        {
            _isGroupVarDirty = true;
            _isSourceVarDirty = true;
            _prefixNames.Insert(0, prefixName); 
        }
 
        ///  
        /// sets new source binding var
        ///  
        /// 
        internal void SetNewSourceBinding( DbVariableReferenceExpression sourceVar )
        {
            _isGroupVarDirty = true; 
            _isSourceVarDirty = true;
            _varRef = sourceVar; 
        } 

        ///  
        /// recalculates var reference expression
        /// 
        private DbExpression RecalculateSourceVar( DbVariableReferenceExpression baseExpression )
        { 
            DbCommandTree cmdTree = baseExpression.CommandTree;
            DbExpression propExpr = baseExpression; 
            DbExpression result = null; 

            if (_prefixNames.Count > 0) 
            {
                for (int i = 1 ; i < _prefixNames.Count ; i++)
                {
                    propExpr = cmdTree.CreatePropertyExpression(_prefixNames[i], propExpr); 
                }
 
                result = cmdTree.CreatePropertyExpression(_varName, propExpr); 
            }
            else 
            {
                result = baseExpression;
            }
 
            return result;
        } 
 

        internal DbVariableReferenceExpression GroupVarExpression 
        {
            get { return _groupVar; }
            set { _groupVar = value; }
        } 

 
        internal DbExpression AggregateExpression 
        {
            get 
            {
                if (_isGroupVarDirty)
                {
                    _aggregateExpression = RecalculateSourceVar(_groupVar); 

                    _isGroupVarDirty = false; 
                } 

                return _aggregateExpression; 
            }
        }

        internal string VarTag 
        {
            get 
            { 
                return _varTag;
            } 
        }
    }

 
    /// 
    /// represents a projection expression scope entry 
    ///  
    internal class ProjectionScopeEntry : ScopeEntry
    { 
        private string _varName;

        internal ProjectionScopeEntry( string varName, DbExpression expression )
            : base(ScopeEntryKind.ProjectList, expression) 
        {
            _varName = varName; 
        } 

        ///  
        /// returns projection scope entry
        /// 
        internal override DbExpression Expression
        { 
            get { return _expression; }
        } 
    } 

    ///  
    /// represents Dummy group keys to be used during group aggregate search mode
    /// these entries will then be replaced by the real key expression once the group
    /// expression is built
    ///  
    internal class DummyGroupVarScopeEntry : ScopeEntry
    { 
        private DbExpression _aggreateExpression; 

        internal DummyGroupVarScopeEntry(DbExpression varBasedExpression, DbExpression groupVarBasedExpression) 
            : base(ScopeEntryKind.DummyGroupKey, varBasedExpression)
        {
            _aggreateExpression = groupVarBasedExpression;
            base.VarKind = SourceVarKind.GroupKey; 
        }
 
        ///  
        /// returns group var based expression
        ///  
        internal DbExpression AggregateExpression
        {
            get { return _aggreateExpression; }
        } 
    }
 
    ///  
    /// Represents static context for semantic analysis
    ///  
    internal sealed class StaticContext : ScopeManager
    {
        /// 
        /// initializes with specific keycomparer 
        /// 
        ///  
        internal StaticContext( IEqualityComparer keyComparer ) 
            : base(keyComparer)
        { 
        }

        /// 
        /// Adds current expression to scope according to curr scope kind 
        /// 
        ///  
        ///  
        /// 
        internal void AddSourceBinding( DbExpressionBinding sourceBinding, ScopeEntryKind scopeEntryKind , string varTag) 
        {
            Add(sourceBinding.VariableName, new SourceScopeEntry(scopeEntryKind, sourceBinding, varTag));
        }
 
        /// 
        /// adds a dummy group key to scope 
        ///  
        /// 
        ///  
        /// 
        internal void AddGroupDummyVar(string groupKey, DbExpression varBasedExpression, DbExpression groupVarBasedExpression)
        {
            Add(groupKey, new DummyGroupVarScopeEntry(varBasedExpression, groupVarBasedExpression)); 
        }
 
        ///  
        /// replaces dummy group keys added during the group aggregate search phase by the real group key expressions
        ///  
        /// 
        /// 
        internal void ReplaceGroupVarInScope( string groupVarName, DbVariableReferenceExpression groupSourceBinding )
        { 
            //
            // ensure the key exists 
            // 
            if (!IsInCurrentScope(groupVarName))
            { 
                throw EntityUtil.EntitySqlError(Strings.CouldNotFindAggregateKey);
            }

            // 
            // remove dummy key added previously
            // 
            RemoveFromScope(groupVarName); 

            // 
            // add real group key to scope
            //
            Add(groupVarName, new SourceScopeEntry(ScopeEntryKind.SourceVar, groupVarName, groupSourceBinding, SourceVarKind.GroupKey));
        } 

 
        ///  
        /// add group aggregate functions to the scope
        ///  
        /// 
        /// 
        internal void AddAggregateToScope( string aggregateName, DbVariableReferenceExpression sourceVar )
        { 
            Add(aggregateName, new SourceScopeEntry(ScopeEntryKind.SourceVar, aggregateName, sourceVar, SourceVarKind.GroupAggregate));
        } 
 
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.

                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK