Code:
                         / Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / ndp / fx / src / DLinq / Dlinq / SqlClient / Query / SqlLiftIndependentRowExpressions.cs / 1 / SqlLiftIndependentRowExpressions.cs
                        
                        
                            using System; 
using System.Collections.Generic;
using System.Text;
using System.Diagnostics.CodeAnalysis;
 
namespace System.Data.Linq.SqlClient {
 
    ///  
    /// Find projection expressions on the right side of CROSS APPLY that do not
    /// depend exclusively on right side productions and move them outside of the 
    /// CROSS APPLY by enclosing the CROSS APPLY with a new source.
    ///  
    class SqlLiftIndependentRowExpressions {
        internal static SqlNode Lift(SqlNode node) { 
            ColumnLifter cl = new ColumnLifter();
            node = cl.Visit(node); 
            return node; 
        }
 
        private class ColumnLifter : SqlVisitor {
            SelectScope expressionSink;
            SqlAggregateChecker aggregateChecker;
 
            internal ColumnLifter() {
                this.aggregateChecker = new SqlAggregateChecker(); 
            } 
            class SelectScope { 
                // Stack of projections lifted from the right to be pushed on the left.
                internal Stack> Lifted = new Stack>();
                internal IEnumerable LeftProduction;
                internal HashSet ReferencedExpressions = new HashSet(); 
            }
 
            internal override SqlSelect VisitSelect(SqlSelect select) { 
                SelectScope s = expressionSink;
 
                // Don't lift through a TOP.
                if (select.Top != null) {
                    expressionSink = null;
                } 
                // Don't lift through a GROUP BY (or implicit GROUP BY). 
                if (select.GroupBy.Count > 0 || this.aggregateChecker.HasAggregates(select)) { 
                    expressionSink = null;
                } 
                // Don't lift through DISTINCT
                if (select.IsDistinct) {
                    expressionSink = null; 
                }
 
                if (expressionSink != null) { 
                    List keep = new List();
                    List lift = new List(); 
                    foreach (SqlColumn sc in select.Row.Columns) {
                        bool referencesLeftsideAliases = SqlAliasesReferenced.ReferencesAny(sc.Expression, expressionSink.LeftProduction);
                        bool isLockedExpression = expressionSink.ReferencedExpressions.Contains(sc); 
                        if (referencesLeftsideAliases && !isLockedExpression) {
                            lift.Add(sc); 
                        } else { 
                            keep.Add(sc);
                        } 
                    }
                    select.Row.Columns.Clear();
                    select.Row.Columns.AddRange(keep);
                    if (lift.Count > 0) { 
                        expressionSink.Lifted.Push(lift);
                    } 
                } 
                SqlSelect sel = base.VisitSelect(select); 
                expressionSink = s;
                return sel;
            }
            internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { 
                if (expressionSink!=null) {
                    expressionSink.ReferencedExpressions.Add(cref.Column); 
                } 
                return cref;
            } 
            internal override SqlSource VisitJoin(SqlJoin join) {
                if (join.JoinType == SqlJoinType.CrossApply) {
                    // Visit the left side as usual.
                    join.Left = this.VisitSource(join.Left); 
                    // Visit the condition as usual. 
                    join.Condition = this.VisitExpression(join.Condition); 
                    // Visit the right, with the expressionSink set. 
                    SelectScope s = expressionSink;
                    expressionSink = new SelectScope();
                    expressionSink.LeftProduction = SqlGatherProducedAliases.Gather(join.Left); 
                    join.Right = this.VisitSource(join.Right);
 
                    // Were liftable expressions found? 
                    SqlSource newSource = join;
                    foreach (List cols in expressionSink.Lifted) { 
                        newSource = PushSourceDown(newSource, cols);
                    }
                    expressionSink = s;
                    return newSource; 
                }
                return base.VisitJoin(join); 
            } 
            [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification="Unknown reason.")]
            private SqlSource PushSourceDown(SqlSource sqlSource, List cols) { 
                SqlSelect ns = new SqlSelect(new SqlNop(cols[0].ClrType, cols[0].SqlType, sqlSource.SourceExpression), sqlSource, sqlSource.SourceExpression);
                ns.Row.Columns.AddRange(cols);
                return new SqlAlias(ns);
            } 
        }
    } 
} 
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
using System; 
using System.Collections.Generic;
using System.Text;
using System.Diagnostics.CodeAnalysis;
 
namespace System.Data.Linq.SqlClient {
 
    ///  
    /// Find projection expressions on the right side of CROSS APPLY that do not
    /// depend exclusively on right side productions and move them outside of the 
    /// CROSS APPLY by enclosing the CROSS APPLY with a new source.
    ///  
    class SqlLiftIndependentRowExpressions {
        internal static SqlNode Lift(SqlNode node) { 
            ColumnLifter cl = new ColumnLifter();
            node = cl.Visit(node); 
            return node; 
        }
 
        private class ColumnLifter : SqlVisitor {
            SelectScope expressionSink;
            SqlAggregateChecker aggregateChecker;
 
            internal ColumnLifter() {
                this.aggregateChecker = new SqlAggregateChecker(); 
            } 
            class SelectScope { 
                // Stack of projections lifted from the right to be pushed on the left.
                internal Stack> Lifted = new Stack>();
                internal IEnumerable LeftProduction;
                internal HashSet ReferencedExpressions = new HashSet(); 
            }
 
            internal override SqlSelect VisitSelect(SqlSelect select) { 
                SelectScope s = expressionSink;
 
                // Don't lift through a TOP.
                if (select.Top != null) {
                    expressionSink = null;
                } 
                // Don't lift through a GROUP BY (or implicit GROUP BY). 
                if (select.GroupBy.Count > 0 || this.aggregateChecker.HasAggregates(select)) { 
                    expressionSink = null;
                } 
                // Don't lift through DISTINCT
                if (select.IsDistinct) {
                    expressionSink = null; 
                }
 
                if (expressionSink != null) { 
                    List keep = new List();
                    List lift = new List(); 
                    foreach (SqlColumn sc in select.Row.Columns) {
                        bool referencesLeftsideAliases = SqlAliasesReferenced.ReferencesAny(sc.Expression, expressionSink.LeftProduction);
                        bool isLockedExpression = expressionSink.ReferencedExpressions.Contains(sc); 
                        if (referencesLeftsideAliases && !isLockedExpression) {
                            lift.Add(sc); 
                        } else { 
                            keep.Add(sc);
                        } 
                    }
                    select.Row.Columns.Clear();
                    select.Row.Columns.AddRange(keep);
                    if (lift.Count > 0) { 
                        expressionSink.Lifted.Push(lift);
                    } 
                } 
                SqlSelect sel = base.VisitSelect(select); 
                expressionSink = s;
                return sel;
            }
            internal override SqlExpression VisitColumnRef(SqlColumnRef cref) { 
                if (expressionSink!=null) {
                    expressionSink.ReferencedExpressions.Add(cref.Column); 
                } 
                return cref;
            } 
            internal override SqlSource VisitJoin(SqlJoin join) {
                if (join.JoinType == SqlJoinType.CrossApply) {
                    // Visit the left side as usual.
                    join.Left = this.VisitSource(join.Left); 
                    // Visit the condition as usual. 
                    join.Condition = this.VisitExpression(join.Condition); 
                    // Visit the right, with the expressionSink set. 
                    SelectScope s = expressionSink;
                    expressionSink = new SelectScope();
                    expressionSink.LeftProduction = SqlGatherProducedAliases.Gather(join.Left); 
                    join.Right = this.VisitSource(join.Right);
 
                    // Were liftable expressions found? 
                    SqlSource newSource = join;
                    foreach (List cols in expressionSink.Lifted) { 
                        newSource = PushSourceDown(newSource, cols);
                    }
                    expressionSink = s;
                    return newSource; 
                }
                return base.VisitJoin(join); 
            } 
            [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification="Unknown reason.")]
            private SqlSource PushSourceDown(SqlSource sqlSource, List cols) { 
                SqlSelect ns = new SqlSelect(new SqlNop(cols[0].ClrType, cols[0].SqlType, sqlSource.SourceExpression), sqlSource, sqlSource.SourceExpression);
                ns.Row.Columns.AddRange(cols);
                return new SqlAlias(ns);
            } 
        }
    } 
} 
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                                 
         
                        
                        
                        
                    Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- DbMetaDataColumnNames.cs
- DynamicPropertyHolder.cs
- ExpandSegmentCollection.cs
- DbConnectionPoolGroupProviderInfo.cs
- util.cs
- CollectionType.cs
- VBIdentifierName.cs
- FormsAuthenticationConfiguration.cs
- SecurityDescriptor.cs
- TextRunProperties.cs
- LocalBuilder.cs
- _Rfc2616CacheValidators.cs
- BindingSourceDesigner.cs
- CodeCastExpression.cs
- TextParaLineResult.cs
- Int16AnimationBase.cs
- InvalidContentTypeException.cs
- DateTimeOffsetStorage.cs
- COM2PictureConverter.cs
- UncommonField.cs
- SettingsContext.cs
- DbDataSourceEnumerator.cs
- PopupControlService.cs
- TrustLevelCollection.cs
- RuntimeWrappedException.cs
- DataServiceKeyAttribute.cs
- InstanceHandleReference.cs
- AsyncCompletedEventArgs.cs
- __TransparentProxy.cs
- XsltFunctions.cs
- ApplicationServicesHostFactory.cs
- Assert.cs
- QuaternionAnimation.cs
- Predicate.cs
- Timeline.cs
- Vector.cs
- TransformGroup.cs
- DetailsViewUpdatedEventArgs.cs
- AsyncOperationContext.cs
- ScriptingSectionGroup.cs
- DebugView.cs
- XmlWriterSettings.cs
- WindowVisualStateTracker.cs
- SourceSwitch.cs
- COM2TypeInfoProcessor.cs
- WebException.cs
- CodeTypeMember.cs
- HostExecutionContextManager.cs
- CuspData.cs
- RowUpdatedEventArgs.cs
- PrintController.cs
- _SslSessionsCache.cs
- ConfigurationPermission.cs
- Stackframe.cs
- ThreadSafeList.cs
- TrustLevel.cs
- EdgeProfileValidation.cs
- TimeSpanParse.cs
- AnimationStorage.cs
- ConnectionsZone.cs
- DbProviderServices.cs
- ClientFormsIdentity.cs
- XmlToDatasetMap.cs
- SchemaTableOptionalColumn.cs
- IISMapPath.cs
- QueryStatement.cs
- CounterSample.cs
- EFTableProvider.cs
- FontSourceCollection.cs
- DecimalAnimation.cs
- TextEffect.cs
- SystemIPGlobalProperties.cs
- BookmarkEventArgs.cs
- Menu.cs
- ArgumentValidation.cs
- ButtonFieldBase.cs
- OneToOneMappingSerializer.cs
- ProtocolsConfigurationHandler.cs
- CharStorage.cs
- SamlDelegatingWriter.cs
- ProfilePropertyNameValidator.cs
- RepeatInfo.cs
- ToolStripMenuItem.cs
- XmlCharCheckingWriter.cs
- ModelItemDictionaryImpl.cs
- EntityProviderFactory.cs
- TemplateNameScope.cs
- IPGlobalProperties.cs
- Point.cs
- MediaPlayer.cs
- BitmapEncoder.cs
- RequestCachingSection.cs
- AutomationPattern.cs
- TransformerConfigurationWizardBase.cs
- RelatedEnd.cs
- ScopedKnownTypes.cs
- AQNBuilder.cs
- PerformanceCounterPermission.cs
- ReadOnlyDictionary.cs
- ProtocolElementCollection.cs