Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / DataEntity / System / Data / Map / ViewGeneration / Structures / ScalarRestriction.cs / 1305376 / ScalarRestriction.cs
//---------------------------------------------------------------------- //// Copyright (c) Microsoft Corporation. All rights reserved. // // // @owner [....] // @backupOwner [....] //--------------------------------------------------------------------- using System.Data.Common.Utils.Boolean; using System.Collections.Generic; using System.Text; using System.Diagnostics; using System.Data.Common.Utils; using System.Data.Metadata.Edm; using System.Linq; using System.Data.Entity; namespace System.Data.Mapping.ViewGeneration.Structures { using DomainBoolExpr = BoolExpr>; // A class that denotes the boolean expression: "scalarVar in values" // See the comments in OneOfConst for partially and fully-done objects internal class ScalarRestriction : MemberRestriction { #region Constructors // effects: Creates an expression of the form "node in scalar strings // corresponding to values". This constructor is needed for creating // discriminator type conditions internal ScalarRestriction(MemberPath node, object value, TypeUsage memberType) : this(node, new ScalarConstant(value)) { // CHANGE_[....]_IMPROVE: the memberType stuff has to be consumed } internal ScalarRestriction(MemberPath node, Constant value) : base(new MemberProjectedSlot(node), value) { Debug.Assert(value is ScalarConstant || value.IsNull() || value.IsNotNull(), "Scalar, NULL, or NOT NULL expected"); } // effects: Creates an expression of the form "var = value" internal ScalarRestriction(MemberPath node, IEnumerable values, IEnumerable possibleValues) : base(new MemberProjectedSlot(node), values, possibleValues) { } // effects: Creates an expression of the form "slot in domain" internal ScalarRestriction(MemberProjectedSlot slot, Domain domain) : base(slot, domain) { } #endregion #region Methods // requires: IsFullyDone is true // effects: Fixes the range of this in accordance with range internal override DomainBoolExpr FixRange(Set range, MemberDomainMap memberDomainMap) { Debug.Assert(IsFullyDone, "Ranges are fixed only for fully done OneOfConsts"); IEnumerable newPossibleValues = memberDomainMap.GetDomain(RestrictedMemberSlot.MemberPath); BoolLiteral newLiteral = new ScalarRestriction(RestrictedMemberSlot, new Domain(range, newPossibleValues)); return newLiteral.GetDomainBoolExpression(memberDomainMap); } // requires: there is at most one negated Constant internal override StringBuilder AsCql(StringBuilder builder, string blockAlias, bool canSkipIsNotNull) { return ToStringHelper(builder, blockAlias, canSkipIsNotNull, false); } internal override StringBuilder AsUserString(StringBuilder builder, string blockAlias, bool canSkipIsNotNull) { return ToStringHelper(builder, blockAlias, canSkipIsNotNull, true); } //Common code for AsCQL and TouserString methods private StringBuilder ToStringHelper(StringBuilder builder, string blockAlias, bool canSkipIsNotNull, bool userString) { Debug.Assert(RestrictedMemberSlot.MemberPath.IsScalarType(), "Expected scalar"); Set constants = new Set (Domain.Values, Constant.EqualityComparer); // * If no negated cell constant generate the normal list // * If nullable, add AND IS NOT NULL to the list of normal values // * If has a negated cell constant, come up with a simplified // version of the constants, e.g., 7, NOT(7, NULL) means NOT NULL // 7, 8, NOT(7, 8, 9, 10) Means NOT(9, 10) // Note: NOT(9, NULL) is NOT (a is 9 OR a is NULL) = // A IS NOT 9 AND A IS NOT NULL NegatedConstant negated = null; foreach (Constant constant in constants) { NegatedConstant tmpConstant = constant as NegatedConstant; if (tmpConstant != null) { Debug.Assert(negated == null, "Multiple negated constants?"); negated = tmpConstant; } } if (negated != null) { if (userString) { return negated.AsUserString(builder, blockAlias, constants, RestrictedMemberSlot.MemberPath, canSkipIsNotNull); } else { return negated.AsCql(builder, blockAlias, constants, RestrictedMemberSlot.MemberPath, canSkipIsNotNull); } } // We have only positive constants bool containsNull = constants.Contains(Constant.Null); constants.Remove(Constant.Null); //Constraint counter-example could contain undefined cellconstant. E.g for booleans (for int its optimized out due to negated constants) //we want to treat undefined as nulls if (constants.Contains(Constant.Undefined)) { constants.Remove(Constant.Undefined); containsNull = true; } if (containsNull) { if (constants.Count > 0) { builder.Append('('); } if (userString) { RestrictedMemberSlot.MemberPath.ToCompactString(builder, blockAlias); builder.Append(" is NULL"); } else { RestrictedMemberSlot.MemberPath.AsCql(builder, blockAlias); builder.Append(" IS NULL"); } if (constants.Count > 0) { builder.Append(" OR "); } } if (constants.Count == 0) { return builder; } bool isNullable = RestrictedMemberSlot.MemberPath.IsNullable; // Generate var IN {...} or var = ... // For nullable members, add (... AND var IS NOT NULL) // so the boolean _from variables never evaluate to null // This is needed because view generation assumes 2-valued boolean logic if (isNullable && canSkipIsNotNull == false) { builder.Append("("); // enclose AND var IS NOT NULL in brackets } if (userString) { RestrictedMemberSlot.MemberPath.ToCompactString(builder, blockAlias); } else { RestrictedMemberSlot.MemberPath.AsCql(builder, blockAlias); } if (constants.Count > 1) { // Multiple values builder.Append(" IN {"); bool isFirst = true; foreach (Constant constant in constants) { if (isFirst == false) { builder.Append(", "); } isFirst = false; if (userString) { constant.ToCompactString(builder); } else { constant.AsCql(builder, RestrictedMemberSlot.MemberPath, blockAlias); } } builder.Append("}"); } else { // Single value builder.Append(" = "); if (userString) { constants.Single().ToCompactString(builder); } else { constants.Single().AsCql(builder, RestrictedMemberSlot.MemberPath, blockAlias); } } if (isNullable && canSkipIsNotNull == false) { // add the AND var IS NOT NULL condition builder.Append(" AND "); if (userString) { RestrictedMemberSlot.MemberPath.ToCompactString(builder, Strings.ViewGen_EntityInstanceToken); builder.Append(" is not NULL)"); // plus the closing bracket } else { RestrictedMemberSlot.MemberPath.AsCql(builder, blockAlias); builder.Append(" IS NOT NULL)"); // plus the closing bracket } } if (containsNull) { // Close the OR builder.Append(')'); } return builder; } #endregion #region String methods internal override void ToCompactString(StringBuilder builder) { RestrictedMemberSlot.ToCompactString(builder); builder.Append(" IN ("); StringUtil.ToCommaSeparatedStringSorted(builder, Domain.Values); builder.Append(")"); } #endregion } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. //---------------------------------------------------------------------- // // Copyright (c) Microsoft Corporation. All rights reserved. // // // @owner [....] // @backupOwner [....] //--------------------------------------------------------------------- using System.Data.Common.Utils.Boolean; using System.Collections.Generic; using System.Text; using System.Diagnostics; using System.Data.Common.Utils; using System.Data.Metadata.Edm; using System.Linq; using System.Data.Entity; namespace System.Data.Mapping.ViewGeneration.Structures { using DomainBoolExpr = BoolExpr>; // A class that denotes the boolean expression: "scalarVar in values" // See the comments in OneOfConst for partially and fully-done objects internal class ScalarRestriction : MemberRestriction { #region Constructors // effects: Creates an expression of the form "node in scalar strings // corresponding to values". This constructor is needed for creating // discriminator type conditions internal ScalarRestriction(MemberPath node, object value, TypeUsage memberType) : this(node, new ScalarConstant(value)) { // CHANGE_[....]_IMPROVE: the memberType stuff has to be consumed } internal ScalarRestriction(MemberPath node, Constant value) : base(new MemberProjectedSlot(node), value) { Debug.Assert(value is ScalarConstant || value.IsNull() || value.IsNotNull(), "Scalar, NULL, or NOT NULL expected"); } // effects: Creates an expression of the form "var = value" internal ScalarRestriction(MemberPath node, IEnumerable values, IEnumerable possibleValues) : base(new MemberProjectedSlot(node), values, possibleValues) { } // effects: Creates an expression of the form "slot in domain" internal ScalarRestriction(MemberProjectedSlot slot, Domain domain) : base(slot, domain) { } #endregion #region Methods // requires: IsFullyDone is true // effects: Fixes the range of this in accordance with range internal override DomainBoolExpr FixRange(Set range, MemberDomainMap memberDomainMap) { Debug.Assert(IsFullyDone, "Ranges are fixed only for fully done OneOfConsts"); IEnumerable newPossibleValues = memberDomainMap.GetDomain(RestrictedMemberSlot.MemberPath); BoolLiteral newLiteral = new ScalarRestriction(RestrictedMemberSlot, new Domain(range, newPossibleValues)); return newLiteral.GetDomainBoolExpression(memberDomainMap); } // requires: there is at most one negated Constant internal override StringBuilder AsCql(StringBuilder builder, string blockAlias, bool canSkipIsNotNull) { return ToStringHelper(builder, blockAlias, canSkipIsNotNull, false); } internal override StringBuilder AsUserString(StringBuilder builder, string blockAlias, bool canSkipIsNotNull) { return ToStringHelper(builder, blockAlias, canSkipIsNotNull, true); } //Common code for AsCQL and TouserString methods private StringBuilder ToStringHelper(StringBuilder builder, string blockAlias, bool canSkipIsNotNull, bool userString) { Debug.Assert(RestrictedMemberSlot.MemberPath.IsScalarType(), "Expected scalar"); Set constants = new Set (Domain.Values, Constant.EqualityComparer); // * If no negated cell constant generate the normal list // * If nullable, add AND IS NOT NULL to the list of normal values // * If has a negated cell constant, come up with a simplified // version of the constants, e.g., 7, NOT(7, NULL) means NOT NULL // 7, 8, NOT(7, 8, 9, 10) Means NOT(9, 10) // Note: NOT(9, NULL) is NOT (a is 9 OR a is NULL) = // A IS NOT 9 AND A IS NOT NULL NegatedConstant negated = null; foreach (Constant constant in constants) { NegatedConstant tmpConstant = constant as NegatedConstant; if (tmpConstant != null) { Debug.Assert(negated == null, "Multiple negated constants?"); negated = tmpConstant; } } if (negated != null) { if (userString) { return negated.AsUserString(builder, blockAlias, constants, RestrictedMemberSlot.MemberPath, canSkipIsNotNull); } else { return negated.AsCql(builder, blockAlias, constants, RestrictedMemberSlot.MemberPath, canSkipIsNotNull); } } // We have only positive constants bool containsNull = constants.Contains(Constant.Null); constants.Remove(Constant.Null); //Constraint counter-example could contain undefined cellconstant. E.g for booleans (for int its optimized out due to negated constants) //we want to treat undefined as nulls if (constants.Contains(Constant.Undefined)) { constants.Remove(Constant.Undefined); containsNull = true; } if (containsNull) { if (constants.Count > 0) { builder.Append('('); } if (userString) { RestrictedMemberSlot.MemberPath.ToCompactString(builder, blockAlias); builder.Append(" is NULL"); } else { RestrictedMemberSlot.MemberPath.AsCql(builder, blockAlias); builder.Append(" IS NULL"); } if (constants.Count > 0) { builder.Append(" OR "); } } if (constants.Count == 0) { return builder; } bool isNullable = RestrictedMemberSlot.MemberPath.IsNullable; // Generate var IN {...} or var = ... // For nullable members, add (... AND var IS NOT NULL) // so the boolean _from variables never evaluate to null // This is needed because view generation assumes 2-valued boolean logic if (isNullable && canSkipIsNotNull == false) { builder.Append("("); // enclose AND var IS NOT NULL in brackets } if (userString) { RestrictedMemberSlot.MemberPath.ToCompactString(builder, blockAlias); } else { RestrictedMemberSlot.MemberPath.AsCql(builder, blockAlias); } if (constants.Count > 1) { // Multiple values builder.Append(" IN {"); bool isFirst = true; foreach (Constant constant in constants) { if (isFirst == false) { builder.Append(", "); } isFirst = false; if (userString) { constant.ToCompactString(builder); } else { constant.AsCql(builder, RestrictedMemberSlot.MemberPath, blockAlias); } } builder.Append("}"); } else { // Single value builder.Append(" = "); if (userString) { constants.Single().ToCompactString(builder); } else { constants.Single().AsCql(builder, RestrictedMemberSlot.MemberPath, blockAlias); } } if (isNullable && canSkipIsNotNull == false) { // add the AND var IS NOT NULL condition builder.Append(" AND "); if (userString) { RestrictedMemberSlot.MemberPath.ToCompactString(builder, Strings.ViewGen_EntityInstanceToken); builder.Append(" is not NULL)"); // plus the closing bracket } else { RestrictedMemberSlot.MemberPath.AsCql(builder, blockAlias); builder.Append(" IS NOT NULL)"); // plus the closing bracket } } if (containsNull) { // Close the OR builder.Append(')'); } return builder; } #endregion #region String methods internal override void ToCompactString(StringBuilder builder) { RestrictedMemberSlot.ToCompactString(builder); builder.Append(" IN ("); StringUtil.ToCommaSeparatedStringSorted(builder, Domain.Values); builder.Append(")"); } #endregion } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- HtmlMeta.cs
- InfoCardArgumentException.cs
- EntryPointNotFoundException.cs
- XmlSigningNodeWriter.cs
- PropertyValidationContext.cs
- BinaryUtilClasses.cs
- SqlDataSourceWizardForm.cs
- HttpException.cs
- Scene3D.cs
- DesignerActionPropertyItem.cs
- OperatingSystem.cs
- DirectoryNotFoundException.cs
- XsdDataContractImporter.cs
- XmlReaderSettings.cs
- ParallelTimeline.cs
- XmlSerializationReader.cs
- OciHandle.cs
- SequentialUshortCollection.cs
- MailAddress.cs
- ExtensionQuery.cs
- ServiceOperationUIEditor.cs
- SecUtil.cs
- EditorPartDesigner.cs
- NotifyCollectionChangedEventArgs.cs
- TreeNodeClickEventArgs.cs
- HttpCacheParams.cs
- FormViewInsertEventArgs.cs
- WeakReferenceList.cs
- QilReference.cs
- XmlDownloadManager.cs
- EntityDataSourceEntityTypeFilterItem.cs
- NullableIntMinMaxAggregationOperator.cs
- PostBackOptions.cs
- Subset.cs
- RawStylusInputCustomData.cs
- SchemaMapping.cs
- MsmqSecureHashAlgorithm.cs
- NativeMethods.cs
- CryptoKeySecurity.cs
- SqlUtils.cs
- SamlAudienceRestrictionCondition.cs
- BamlMapTable.cs
- AssemblyResourceLoader.cs
- ClockGroup.cs
- shaperfactory.cs
- ValuePattern.cs
- FilterEventArgs.cs
- UTF8Encoding.cs
- RegexParser.cs
- IImplicitResourceProvider.cs
- InvalidPropValue.cs
- RegexWriter.cs
- InertiaTranslationBehavior.cs
- GridViewRowEventArgs.cs
- SecurityIdentifierElement.cs
- CanonicalFontFamilyReference.cs
- XPathNodePointer.cs
- Int32Converter.cs
- TypeSystem.cs
- DataGridDetailsPresenterAutomationPeer.cs
- SoapIgnoreAttribute.cs
- HttpModulesSection.cs
- PasswordBoxAutomationPeer.cs
- UidManager.cs
- ThicknessAnimationBase.cs
- NativeMethods.cs
- IOException.cs
- WindowsGrip.cs
- DataGrid.cs
- ConfigXmlText.cs
- Link.cs
- StringAttributeCollection.cs
- DocumentScope.cs
- HttpConfigurationSystem.cs
- CodeSnippetStatement.cs
- UshortList2.cs
- CompositeDuplexBindingElement.cs
- Exceptions.cs
- QilInvokeLateBound.cs
- PenLineJoinValidation.cs
- Button.cs
- DynamicResourceExtensionConverter.cs
- webclient.cs
- CellConstantDomain.cs
- LoaderAllocator.cs
- XPathException.cs
- Transform3DGroup.cs
- CommandID.cs
- ChtmlTextWriter.cs
- BufferedGraphics.cs
- ListBindingHelper.cs
- Container.cs
- TextModifier.cs
- EntityRecordInfo.cs
- CompileLiteralTextParser.cs
- DiffuseMaterial.cs
- RSATokenProvider.cs
- DockProviderWrapper.cs
- ModelUtilities.cs
- AutoCompleteStringCollection.cs