Exemplo n.º 1
0
            /// <summary>
            /// Given default values for children members, produces a new default expression for the requested (parent) member.
            /// </summary>
            /// <param name="node">Parent member</param>
            /// <returns>Default value for parent member</returns>
            internal PropagatorResult Visit(EdmMember node)
            {
                PropagatorResult result;
                TypeUsage        nodeType = Helper.GetModelTypeUsage(node);

                if (Helper.IsScalarType(nodeType.EdmType))
                {
                    GetPropagatorResultForPrimitiveType(Helper.AsPrimitive(nodeType.EdmType), out result);
                }
                else
                {
                    // Construct a new 'complex type' (really any structural type) member.
                    StructuralType        structuralType = (StructuralType)nodeType.EdmType;
                    IBaseList <EdmMember> members        = TypeHelpers.GetAllStructuralMembers(structuralType);

                    PropagatorResult[] args = new PropagatorResult[members.Count];
                    for (int ordinal = 0; ordinal < members.Count; ordinal++)
                    //                    foreach (EdmMember member in members)
                    {
                        args[ordinal] = Visit(members[ordinal]);
                    }

                    result = PropagatorResult.CreateStructuralValue(args, structuralType, false);
                }

                return(result);
            }
Exemplo n.º 2
0
        private static List <TypeUsage> GetStructuralMemberTypes(TypeUsage instanceType)
        {
            StructuralType edmType = instanceType.EdmType as StructuralType;

            if (edmType == null)
            {
                throw new ArgumentException(Strings.Cqt_NewInstance_StructuralTypeRequired, nameof(instanceType));
            }
            if (edmType.Abstract)
            {
                throw new ArgumentException(Strings.Cqt_NewInstance_CannotInstantiateAbstractType((object)instanceType.ToString()), nameof(instanceType));
            }
            IBaseList <EdmMember> structuralMembers = TypeHelpers.GetAllStructuralMembers((EdmType)edmType);

            if (structuralMembers == null || structuralMembers.Count < 1)
            {
                throw new ArgumentException(Strings.Cqt_NewInstance_CannotInstantiateMemberlessType((object)instanceType.ToString()), nameof(instanceType));
            }
            List <TypeUsage> typeUsageList = new List <TypeUsage>(structuralMembers.Count);

            for (int index = 0; index < structuralMembers.Count; ++index)
            {
                typeUsageList.Add(Helper.GetModelTypeUsage(structuralMembers[index]));
            }
            return(typeUsageList);
        }
        /// <summary>
        /// Initializes a new <see cref="T:System.Data.Common.DbDataRecord" /> object for a specific type with an enumerable collection of data fields.
        /// </summary>
        /// <param name="metadata">
        /// The metadata for the type represented by this object, supplied by
        /// <see cref="T:System.Data.Entity.Core.Metadata.Edm.TypeUsage" />
        /// .
        /// </param>
        /// <param name="memberInfo">
        /// An enumerable collection of <see cref="T:System.Data.Entity.Core.Metadata.Edm.EdmMember" /> objects that represent column information.
        /// </param>
        public DataRecordInfo(TypeUsage metadata, IEnumerable <EdmMember> memberInfo)
        {
            Check.NotNull <TypeUsage>(metadata, nameof(metadata));
            IBaseList <EdmMember> structuralMembers = TypeHelpers.GetAllStructuralMembers(metadata.EdmType);
            List <System.Data.Entity.Core.Common.FieldMetadata> fieldMetadataList = new List <System.Data.Entity.Core.Common.FieldMetadata>(structuralMembers.Count);

            if (memberInfo != null)
            {
                foreach (EdmMember fieldType in memberInfo)
                {
                    if (fieldType == null || 0 > structuralMembers.IndexOf(fieldType) || BuiltInTypeKind.EdmProperty != fieldType.BuiltInTypeKind && fieldType.BuiltInTypeKind != BuiltInTypeKind.AssociationEndMember)
                    {
                        throw Error.InvalidEdmMemberInstance();
                    }
                    if (fieldType.DeclaringType != metadata.EdmType && !fieldType.DeclaringType.IsBaseTypeOf(metadata.EdmType))
                    {
                        throw new ArgumentException(Strings.EdmMembersDefiningTypeDoNotAgreeWithMetadataType);
                    }
                    fieldMetadataList.Add(new System.Data.Entity.Core.Common.FieldMetadata(fieldMetadataList.Count, fieldType));
                }
            }
            if (Helper.IsStructuralType(metadata.EdmType) != 0 < fieldMetadataList.Count)
            {
                throw Error.InvalidEdmMemberInstance();
            }
            this._fieldMetadata = new ReadOnlyCollection <System.Data.Entity.Core.Common.FieldMetadata>((IList <System.Data.Entity.Core.Common.FieldMetadata>)fieldMetadataList);
            this._metadata      = metadata;
        }
        internal ExtractorMetadata(
            EntitySetBase entitySetBase,
            StructuralType type,
            UpdateTranslator translator)
        {
            this.m_type       = type;
            this.m_translator = translator;
            EntityType      entityType = (EntityType)null;
            Set <EdmMember> set1;
            Set <EdmMember> set2;

            switch (type.BuiltInTypeKind)
            {
            case BuiltInTypeKind.EntityType:
                entityType = (EntityType)type;
                set1       = new Set <EdmMember>((IEnumerable <EdmMember>)entityType.KeyMembers).MakeReadOnly();
                set2       = new Set <EdmMember>((IEnumerable <EdmMember>)((EntitySet)entitySetBase).ForeignKeyDependents.SelectMany <Tuple <AssociationSet, ReferentialConstraint>, EdmProperty>((Func <Tuple <AssociationSet, ReferentialConstraint>, IEnumerable <EdmProperty> >)(fk => (IEnumerable <EdmProperty>)fk.Item2.ToProperties))).MakeReadOnly();
                break;

            case BuiltInTypeKind.RowType:
                set1 = new Set <EdmMember>((IEnumerable <EdmMember>)((RowType)type).Properties).MakeReadOnly();
                set2 = Set <EdmMember> .Empty;
                break;

            default:
                set1 = Set <EdmMember> .Empty;
                set2 = Set <EdmMember> .Empty;
                break;
            }
            IBaseList <EdmMember> structuralMembers = TypeHelpers.GetAllStructuralMembers((EdmType)type);

            this.m_memberMap = new ExtractorMetadata.MemberInformation[structuralMembers.Count];
            for (int ordinal = 0; ordinal < structuralMembers.Count; ++ordinal)
            {
                EdmMember       edmMember        = structuralMembers[ordinal];
                PropagatorFlags flags            = PropagatorFlags.NoFlags;
                int?            entityKeyOrdinal = new int?();
                if (set1.Contains(edmMember))
                {
                    flags |= PropagatorFlags.Key;
                    if (entityType != null)
                    {
                        entityKeyOrdinal = new int?(entityType.KeyMembers.IndexOf(edmMember));
                    }
                }
                if (set2.Contains(edmMember))
                {
                    flags |= PropagatorFlags.ForeignKey;
                }
                if (MetadataHelper.GetConcurrencyMode(edmMember) == ConcurrencyMode.Fixed)
                {
                    flags |= PropagatorFlags.ConcurrencyValue;
                }
                bool isServerGenerated     = this.m_translator.ViewLoader.IsServerGen(entitySetBase, this.m_translator.MetadataWorkspace, edmMember);
                bool isNullConditionMember = this.m_translator.ViewLoader.IsNullConditionMember(entitySetBase, this.m_translator.MetadataWorkspace, edmMember);
                this.m_memberMap[ordinal] = new ExtractorMetadata.MemberInformation(ordinal, entityKeyOrdinal, flags, edmMember, isServerGenerated, isNullConditionMember);
            }
        }
Exemplo n.º 5
0
        internal override long Execute(
            Dictionary <int, object> identifierValues,
            List <KeyValuePair <PropagatorResult, object> > generatedValues)
        {
            EntityConnection connection = this.Translator.Connection;

            this._dbCommand.Transaction = connection.CurrentTransaction == null ? (DbTransaction)null : connection.CurrentTransaction.StoreTransaction;
            this._dbCommand.Connection  = connection.StoreConnection;
            if (this.Translator.CommandTimeout.HasValue)
            {
                this._dbCommand.CommandTimeout = this.Translator.CommandTimeout.Value;
            }
            this.SetInputIdentifiers(identifierValues);
            long rowsAffected;

            if (this.ResultColumns != null)
            {
                rowsAffected = 0L;
                IBaseList <EdmMember> structuralMembers = TypeHelpers.GetAllStructuralMembers((EdmType)this.CurrentValues.StructuralType);
                using (DbDataReader reader = this._dbCommand.ExecuteReader(CommandBehavior.SequentialAccess))
                {
                    if (reader.Read())
                    {
                        ++rowsAffected;
                        foreach (KeyValuePair <int, PropagatorResult> keyValuePair in (IEnumerable <KeyValuePair <int, PropagatorResult> >) this.ResultColumns.Select <KeyValuePair <string, PropagatorResult>, KeyValuePair <int, PropagatorResult> >((Func <KeyValuePair <string, PropagatorResult>, KeyValuePair <int, PropagatorResult> >)(r => new KeyValuePair <int, PropagatorResult>(this.GetColumnOrdinal(this.Translator, reader, r.Key), r.Value))).OrderBy <KeyValuePair <int, PropagatorResult>, int>((Func <KeyValuePair <int, PropagatorResult>, int>)(r => r.Key)))
                        {
                            int key1 = keyValuePair.Key;
                            if (key1 != -1)
                            {
                                TypeUsage        typeUsage = structuralMembers[keyValuePair.Value.RecordOrdinal].TypeUsage;
                                object           obj       = !Helper.IsSpatialType(typeUsage) || reader.IsDBNull(key1) ? reader.GetValue(key1) : SpatialHelpers.GetSpatialValue(this.Translator.MetadataWorkspace, reader, typeUsage, key1);
                                PropagatorResult key2      = keyValuePair.Value;
                                generatedValues.Add(new KeyValuePair <PropagatorResult, object>(key2, obj));
                                int identifier = key2.Identifier;
                                if (-1 != identifier)
                                {
                                    identifierValues.Add(identifier, obj);
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    CommandHelper.ConsumeReader(reader);
                }
            }
            else
            {
                rowsAffected = (long)this._dbCommand.ExecuteNonQuery();
            }
            return(this.GetRowsAffected(rowsAffected, this.Translator));
        }
Exemplo n.º 6
0
        // requires: enumerations must have the same number of members
        // effects: returns paired enumeration values
        internal static IEnumerable <KeyValuePair <T, S> > PairEnumerations <T, S>(IBaseList <T> left, IEnumerable <S> right)
        {
            IEnumerator     leftEnumerator  = left.GetEnumerator();
            IEnumerator <S> rightEnumerator = right.GetEnumerator();

            while (leftEnumerator.MoveNext() && rightEnumerator.MoveNext())
            {
                yield return(new KeyValuePair <T, S>((T)leftEnumerator.Current, rightEnumerator.Current));
            }

            yield break;
        }
        internal DataRecordInfo(TypeUsage metadata)
        {
            IBaseList <EdmMember> structuralMembers = TypeHelpers.GetAllStructuralMembers(metadata);

            System.Data.Entity.Core.Common.FieldMetadata[] fieldMetadataArray = new System.Data.Entity.Core.Common.FieldMetadata[structuralMembers.Count];
            for (int ordinal = 0; ordinal < fieldMetadataArray.Length; ++ordinal)
            {
                EdmMember fieldType = structuralMembers[ordinal];
                fieldMetadataArray[ordinal] = new System.Data.Entity.Core.Common.FieldMetadata(ordinal, fieldType);
            }
            this._fieldMetadata = new ReadOnlyCollection <System.Data.Entity.Core.Common.FieldMetadata>((IList <System.Data.Entity.Core.Common.FieldMetadata>)fieldMetadataArray);
            this._metadata      = metadata;
        }
Exemplo n.º 8
0
        private static int[] InitializeKeyOrdinals(EntitySet table)
        {
            EntityType            elementType       = table.ElementType;
            IList <EdmMember>     keyMembers        = (IList <EdmMember>)elementType.KeyMembers;
            IBaseList <EdmMember> structuralMembers = TypeHelpers.GetAllStructuralMembers((EdmType)elementType);

            int[] numArray = new int[keyMembers.Count];
            for (int index = 0; index < keyMembers.Count; ++index)
            {
                EdmMember edmMember = keyMembers[index];
                numArray[index] = structuralMembers.IndexOf(edmMember);
            }
            return(numArray);
        }
Exemplo n.º 9
0
 internal override long Execute(
     Dictionary <int, object> identifierValues,
     List <KeyValuePair <PropagatorResult, object> > generatedValues)
 {
     using (DbCommand command = this.CreateCommand(identifierValues))
     {
         EntityConnection connection = this.Translator.Connection;
         command.Transaction = connection.CurrentTransaction == null ? (DbTransaction)null : connection.CurrentTransaction.StoreTransaction;
         command.Connection  = connection.StoreConnection;
         if (this.Translator.CommandTimeout.HasValue)
         {
             command.CommandTimeout = this.Translator.CommandTimeout.Value;
         }
         int num;
         if (this._modificationCommandTree.HasReader)
         {
             num = 0;
             using (DbDataReader reader = command.ExecuteReader(CommandBehavior.SequentialAccess))
             {
                 if (reader.Read())
                 {
                     ++num;
                     IBaseList <EdmMember> structuralMembers = TypeHelpers.GetAllStructuralMembers((EdmType)this.CurrentValues.StructuralType);
                     for (int index = 0; index < reader.FieldCount; ++index)
                     {
                         string           name        = reader.GetName(index);
                         EdmMember        edmMember   = structuralMembers[name];
                         object           obj         = !Helper.IsSpatialType(edmMember.TypeUsage) || reader.IsDBNull(index) ? reader.GetValue(index) : SpatialHelpers.GetSpatialValue(this.Translator.MetadataWorkspace, reader, edmMember.TypeUsage, index);
                         PropagatorResult memberValue = this.CurrentValues.GetMemberValue(structuralMembers.IndexOf(edmMember));
                         generatedValues.Add(new KeyValuePair <PropagatorResult, object>(memberValue, obj));
                         int identifier = memberValue.Identifier;
                         if (-1 != identifier)
                         {
                             identifierValues.Add(identifier, obj);
                         }
                     }
                 }
                 CommandHelper.ConsumeReader(reader);
             }
         }
         else
         {
             num = command.ExecuteNonQuery();
         }
         return((long)num);
     }
 }
Exemplo n.º 10
0
        /// <summary>
        /// Construct FieldMetadata for structuralType.Members from TypeUsage
        /// </summary>
        internal DataRecordInfo(TypeUsage metadata)
        {
            Debug.Assert(null != metadata, "invalid attempt to instantiate DataRecordInfo with null metadata information");

            IBaseList <EdmMember> structuralMembers = TypeHelpers.GetAllStructuralMembers(metadata);

            FieldMetadata[] fieldList = new FieldMetadata[structuralMembers.Count];
            for (int i = 0; i < fieldList.Length; ++i)
            {
                EdmMember member = structuralMembers[i];
                Debug.Assert((BuiltInTypeKind.EdmProperty == member.BuiltInTypeKind) ||
                             (BuiltInTypeKind.AssociationEndMember == member.BuiltInTypeKind),
                             "unexpected BuiltInTypeKind for member");
                fieldList[i] = new FieldMetadata(i, member);
            }
            _fieldMetadata = new System.Collections.ObjectModel.ReadOnlyCollection <FieldMetadata>(fieldList);
            _metadata      = metadata;
        }
Exemplo n.º 11
0
        /// <summary>
        /// Construct DataRecordInfo with list of EdmMembers.
        /// Each memberInfo must be a member of metadata.
        /// </summary>
        /// <param name="metadata"></param>
        /// <param name="memberInfo"></param>
        public DataRecordInfo(TypeUsage metadata, IEnumerable <EdmMember> memberInfo)
        {
            EntityUtil.CheckArgumentNull(metadata, "metadata");
            IBaseList <EdmMember> members = TypeHelpers.GetAllStructuralMembers(metadata.EdmType);

            List <FieldMetadata> fieldList = new List <FieldMetadata>(members.Count);

            if (null != memberInfo)
            {
                foreach (EdmMember member in memberInfo)
                {
                    if ((null != member) &&
                        (0 <= members.IndexOf(member)) &&
                        ((BuiltInTypeKind.EdmProperty == member.BuiltInTypeKind) ||         // for ComplexType, EntityType; BuiltTypeKind.NaviationProperty not allowed
                         (BuiltInTypeKind.AssociationEndMember == member.BuiltInTypeKind))) // for AssociationType
                    {                                                                       // each memberInfo must be non-null and be part of Properties or AssociationEndMembers
                        //validate that EdmMembers are from the same type or base type of the passed in metadata.
                        if ((member.DeclaringType != metadata.EdmType) &&
                            !member.DeclaringType.IsBaseTypeOf(metadata.EdmType))
                        {
                            throw EntityUtil.Argument(System.Data.Entity.Strings.EdmMembersDefiningTypeDoNotAgreeWithMetadataType);
                        }
                        fieldList.Add(new FieldMetadata(fieldList.Count, member));
                    }
                    else
                    {   // expecting empty memberInfo for non-structural && non-null member part of members if structural
                        throw EntityUtil.Argument("memberInfo");
                    }
                }
            }

            // expecting structural types to have something at least 1 property
            // (((null == structural) && (0 == fieldList.Count)) || ((null != structural) && (0 < fieldList.Count)))
            if (Helper.IsStructuralType(metadata.EdmType) == (0 < fieldList.Count))
            {
                _fieldMetadata = new System.Collections.ObjectModel.ReadOnlyCollection <FieldMetadata>(fieldList);
                _metadata      = metadata;
            }
            else
            {
                throw EntityUtil.Argument("memberInfo");
            }
        }
Exemplo n.º 12
0
        // Determines which column ordinals in the table are part of the key.
        private static int[] InitializeKeyOrdinals(EntitySet table)
        {
            EntityType            tableType  = table.ElementType;
            IList <EdmMember>     keyMembers = tableType.KeyMembers;
            IBaseList <EdmMember> members    = TypeHelpers.GetAllStructuralMembers(tableType);

            int[] keyOrdinals = new int[keyMembers.Count];

            for (int keyMemberIndex = 0; keyMemberIndex < keyMembers.Count; keyMemberIndex++)
            {
                EdmMember keyMember = keyMembers[keyMemberIndex];
                keyOrdinals[keyMemberIndex] = members.IndexOf(keyMember);

                Debug.Assert(keyOrdinals[keyMemberIndex] >= 0 && keyOrdinals[keyMemberIndex] < members.Count,
                             "an EntityType key member must also be a member of the entity type");
            }

            return(keyOrdinals);
        }
        private static ColumnMap[] GetColumnMapsForType(
            DbDataReader storeDataReader,
            EdmType edmType,
            Dictionary <string, FunctionImportReturnTypeStructuralTypeColumnRenameMapping> renameList)
        {
            IBaseList <EdmMember> structuralMembers = TypeHelpers.GetAllStructuralMembers(edmType);

            ColumnMap[] columnMapArray = new ColumnMap[structuralMembers.Count];
            int         index          = 0;

            foreach (EdmMember member in (IEnumerable)structuralMembers)
            {
                if (!Helper.IsScalarType(member.TypeUsage.EdmType))
                {
                    throw new InvalidOperationException(Strings.ADP_InvalidDataReaderUnableToMaterializeNonScalarType((object)member.Name, (object)member.TypeUsage.EdmType.FullName));
                }
                int ordinalFromReader = ColumnMapFactory.GetMemberOrdinalFromReader(storeDataReader, member, edmType, renameList);
                columnMapArray[index] = (ColumnMap) new ScalarColumnMap(member.TypeUsage, member.Name, 0, ordinalFromReader);
                ++index;
            }
            return(columnMapArray);
        }
Exemplo n.º 14
0
        /// <summary>
        /// For a given edmType, build an array of scalarColumnMaps that map to the columns
        /// in the store datareader provided.  Note that we're hooking things up by name, not
        /// by ordinal position.
        /// </summary>
        /// <param name="storeDataReader"></param>
        /// <param name="edmType"></param>
        /// <returns></returns>
        private static ColumnMap[] GetColumnMapsForType(DbDataReader storeDataReader, EdmType edmType, Dictionary <string, FunctionImportReturnTypeStructuralTypeColumnRenameMapping> renameList)
        {
            // First get the list of properties; NOTE: we need to hook up the column by name,
            // not by position.
            IBaseList <EdmMember> members = TypeHelpers.GetAllStructuralMembers(edmType);

            ColumnMap[] propertyColumnMaps = new ColumnMap[members.Count];

            int index = 0;

            foreach (EdmMember member in members)
            {
                if (!Helper.IsScalarType(member.TypeUsage.EdmType))
                {
                    throw EntityUtil.InvalidOperation(Strings.ADP_InvalidDataReaderUnableToMaterializeNonScalarType(member.Name, member.TypeUsage.EdmType.FullName));
                }

                int ordinal = GetMemberOrdinalFromReader(storeDataReader, member, edmType, renameList);

                propertyColumnMaps[index] = new ScalarColumnMap(member.TypeUsage, member.Name, 0, ordinal);
                index++;
            }
            return(propertyColumnMaps);
        }
Exemplo n.º 15
0
        internal override async Task <long> ExecuteAsync(
            Dictionary <int, object> identifierValues,
            List <KeyValuePair <PropagatorResult, object> > generatedValues,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            EntityConnection connection = this.Translator.Connection;

            this._dbCommand.Transaction = connection.CurrentTransaction == null ? (DbTransaction)null : connection.CurrentTransaction.StoreTransaction;
            this._dbCommand.Connection  = connection.StoreConnection;
            if (this.Translator.CommandTimeout.HasValue)
            {
                this._dbCommand.CommandTimeout = this.Translator.CommandTimeout.Value;
            }
            this.SetInputIdentifiers(identifierValues);
            long rowsAffected;

            if (this.ResultColumns != null)
            {
                rowsAffected = 0L;
                IBaseList <EdmMember> members = TypeHelpers.GetAllStructuralMembers((EdmType)this.CurrentValues.StructuralType);
                using (DbDataReader reader = await this._dbCommand.ExecuteReaderAsync(CommandBehavior.SequentialAccess, cancellationToken).WithCurrentCulture <DbDataReader>())
                {
                    if (await reader.ReadAsync(cancellationToken).WithCurrentCulture <bool>())
                    {
                        ++rowsAffected;
                        foreach (KeyValuePair <int, PropagatorResult> keyValuePair in (IEnumerable <KeyValuePair <int, PropagatorResult> >) this.ResultColumns.Select <KeyValuePair <string, PropagatorResult>, KeyValuePair <int, PropagatorResult> >((Func <KeyValuePair <string, PropagatorResult>, KeyValuePair <int, PropagatorResult> >)(r => new KeyValuePair <int, PropagatorResult>(this.GetColumnOrdinal(this.Translator, reader, r.Key), r.Value))).OrderBy <KeyValuePair <int, PropagatorResult>, int>((Func <KeyValuePair <int, PropagatorResult>, int>)(r => r.Key)))
                        {
                            int       columnOrdinal = keyValuePair.Key;
                            TypeUsage columnType    = members[keyValuePair.Value.RecordOrdinal].TypeUsage;
                            object    value;
                            if (Helper.IsSpatialType(columnType))
                            {
                                if (!await reader.IsDBNullAsync(columnOrdinal, cancellationToken).WithCurrentCulture <bool>())
                                {
                                    value = await SpatialHelpers.GetSpatialValueAsync(this.Translator.MetadataWorkspace, reader, columnType, columnOrdinal, cancellationToken).WithCurrentCulture <object>();

                                    goto label_14;
                                }
                            }
                            value = await reader.GetFieldValueAsync <object>(columnOrdinal, cancellationToken).WithCurrentCulture <object>();

label_14:
                            PropagatorResult result = keyValuePair.Value;
                            generatedValues.Add(new KeyValuePair <PropagatorResult, object>(result, value));
                            int identifier = result.Identifier;
                            if (-1 != identifier)
                            {
                                identifierValues.Add(identifier, value);
                            }
                        }
                    }
                    await CommandHelper.ConsumeReaderAsync(reader, cancellationToken).WithCurrentCulture();
                }
            }
            else
            {
                rowsAffected = (long)await this._dbCommand.ExecuteNonQueryAsync(cancellationToken).WithCurrentCulture <int>();
            }
            return(this.GetRowsAffected(rowsAffected, this.Translator));
        }
Exemplo n.º 16
0
        internal override long Execute(UpdateTranslator translator, EntityConnection connection, Dictionary <int, object> identifierValues, List <KeyValuePair <PropagatorResult, object> > generatedValues)
        {
            // Compile command
            using (DbCommand command = this.CreateCommand(translator, identifierValues))
            {
                // configure command to use the connection and transaction for this session
                command.Transaction = ((null != connection.CurrentTransaction) ? connection.CurrentTransaction.StoreTransaction : null);
                command.Connection  = connection.StoreConnection;
                if (translator.CommandTimeout.HasValue)
                {
                    command.CommandTimeout = translator.CommandTimeout.Value;
                }

                // Execute the query
                int rowsAffected;
                if (m_modificationCommandTree.HasReader)
                {
                    // retrieve server gen results
                    rowsAffected = 0;
                    using (DbDataReader reader = command.ExecuteReader(CommandBehavior.SequentialAccess))
                    {
                        if (reader.Read())
                        {
                            rowsAffected++;

                            IBaseList <EdmMember> members = TypeHelpers.GetAllStructuralMembers(this.CurrentValues.StructuralType);

                            for (int ordinal = 0; ordinal < reader.FieldCount; ordinal++)
                            {
                                // column name of result corresponds to column name of table
                                string    columnName = reader.GetName(ordinal);
                                EdmMember member     = members[columnName];
                                object    value;
                                if (Helper.IsSpatialType(member.TypeUsage) && !reader.IsDBNull(ordinal))
                                {
                                    value = SpatialHelpers.GetSpatialValue(translator.MetadataWorkspace, reader, member.TypeUsage, ordinal);
                                }
                                else
                                {
                                    value = reader.GetValue(ordinal);
                                }

                                // retrieve result which includes the context for back-propagation
                                int columnOrdinal       = members.IndexOf(member);
                                PropagatorResult result = this.CurrentValues.GetMemberValue(columnOrdinal);

                                // register for back-propagation
                                generatedValues.Add(new KeyValuePair <PropagatorResult, object>(result, value));

                                // register identifier if it exists
                                int identifier = result.Identifier;
                                if (PropagatorResult.NullIdentifier != identifier)
                                {
                                    identifierValues.Add(identifier, value);
                                }
                            }
                        }

                        // Consume the current reader (and subsequent result sets) so that any errors
                        // executing the command can be intercepted
                        CommandHelper.ConsumeReader(reader);
                    }
                }
                else
                {
                    rowsAffected = command.ExecuteNonQuery();
                }

                return(rowsAffected);
            }
        }
Exemplo n.º 17
0
        internal ExtractorMetadata(EntitySetBase entitySetBase, StructuralType type, UpdateTranslator translator)
        {
            EntityUtil.CheckArgumentNull(entitySetBase, "entitySetBase");
            m_type       = EntityUtil.CheckArgumentNull(type, "type");
            m_translator = EntityUtil.CheckArgumentNull(translator, "translator");

            EntityType      entityType = null;
            Set <EdmMember> keyMembers;
            Set <EdmMember> foreignKeyMembers;

            switch (type.BuiltInTypeKind)
            {
            case BuiltInTypeKind.RowType:
                // for row types (which are actually association end key records in disguise), all members
                // are keys
                keyMembers        = new Set <EdmMember>(((RowType)type).Properties).MakeReadOnly();
                foreignKeyMembers = Set <EdmMember> .Empty;
                break;

            case BuiltInTypeKind.EntityType:
                entityType        = (EntityType)type;
                keyMembers        = new Set <EdmMember>(entityType.KeyMembers).MakeReadOnly();
                foreignKeyMembers = new Set <EdmMember>(((EntitySet)entitySetBase).ForeignKeyDependents
                                                        .SelectMany(fk => fk.Item2.ToProperties)).MakeReadOnly();
                break;

            default:
                keyMembers        = Set <EdmMember> .Empty;
                foreignKeyMembers = Set <EdmMember> .Empty;
                break;
            }

            IBaseList <EdmMember> members = TypeHelpers.GetAllStructuralMembers(type);

            m_memberMap = new MemberInformation[members.Count];
            // for each member, cache expensive to compute metadata information
            for (int ordinal = 0; ordinal < members.Count; ordinal++)
            {
                EdmMember member = members[ordinal];
                // figure out flags for this member
                PropagatorFlags flags            = PropagatorFlags.NoFlags;
                int?            entityKeyOrdinal = default(int?);

                if (keyMembers.Contains(member))
                {
                    flags |= PropagatorFlags.Key;
                    if (null != entityType)
                    {
                        entityKeyOrdinal = entityType.KeyMembers.IndexOf(member);
                    }
                }
                if (foreignKeyMembers.Contains(member))
                {
                    flags |= PropagatorFlags.ForeignKey;
                }


                if (MetadataHelper.GetConcurrencyMode(member) == ConcurrencyMode.Fixed)
                {
                    flags |= PropagatorFlags.ConcurrencyValue;
                }

                // figure out whether this member is mapped to any server generated
                // columns in the store
                bool isServerGenerated = m_translator.ViewLoader.IsServerGen(entitySetBase, m_translator.MetadataWorkspace, member);

                // figure out whether member nullability is used as a condition in mapping
                bool isNullConditionMember = m_translator.ViewLoader.IsNullConditionMember(entitySetBase, m_translator.MetadataWorkspace, member);

                // add information about this member
                m_memberMap[ordinal] = new MemberInformation(ordinal, entityKeyOrdinal, flags, member, isServerGenerated, isNullConditionMember);
            }
        }
Exemplo n.º 18
0
 public NumberedList(IBaseList baseList)
     : base(baseList)
 {
 }
Exemplo n.º 19
0
        // efects: Executes the current function command in the given transaction and connection context.
        // All server-generated values are added to the generatedValues list. If those values are identifiers, they are
        // also added to the identifierValues dictionary, which associates proxy identifiers for keys in the session
        // with their actual values, permitting fix-up of identifiers across relationships.
        internal override long Execute(UpdateTranslator translator, EntityConnection connection, Dictionary <int, object> identifierValues,
                                       List <KeyValuePair <PropagatorResult, object> > generatedValues)
        {
            // configure command to use the connection and transaction for this session
            m_dbCommand.Transaction = ((null != connection.CurrentTransaction) ? connection.CurrentTransaction.StoreTransaction : null);
            m_dbCommand.Connection  = connection.StoreConnection;
            if (translator.CommandTimeout.HasValue)
            {
                m_dbCommand.CommandTimeout = translator.CommandTimeout.Value;
            }

            // set all identifier inputs (to support propagation of identifier values across relationship
            // boundaries)
            if (null != m_inputIdentifiers)
            {
                foreach (KeyValuePair <int, DbParameter> inputIdentifier in m_inputIdentifiers)
                {
                    object value;
                    if (identifierValues.TryGetValue(inputIdentifier.Key, out value))
                    {
                        // set the actual value for the identifier if it has been produced by some
                        // other command
                        inputIdentifier.Value.Value = value;
                    }
                }
            }

            // Execute the query
            long rowsAffected;

            if (null != m_resultColumns)
            {
                // If there are result columns, read the server gen results
                rowsAffected = 0;
                IBaseList <EdmMember> members = TypeHelpers.GetAllStructuralMembers(this.CurrentValues.StructuralType);
                using (DbDataReader reader = m_dbCommand.ExecuteReader(CommandBehavior.SequentialAccess))
                {
                    // Retrieve only the first row from the first result set
                    if (reader.Read())
                    {
                        rowsAffected++;

                        foreach (var resultColumn in m_resultColumns
                                 .Select(r => new KeyValuePair <int, PropagatorResult>(GetColumnOrdinal(translator, reader, r.Key), r.Value))
                                 .OrderBy(r => r.Key)) // order by column ordinal to avoid breaking SequentialAccess readers
                        {
                            int       columnOrdinal = resultColumn.Key;
                            TypeUsage columnType    = members[resultColumn.Value.RecordOrdinal].TypeUsage;
                            object    value;

                            if (Helper.IsSpatialType(columnType) && !reader.IsDBNull(columnOrdinal))
                            {
                                value = SpatialHelpers.GetSpatialValue(translator.MetadataWorkspace, reader, columnType, columnOrdinal);
                            }
                            else
                            {
                                value = reader.GetValue(columnOrdinal);
                            }

                            // register for back-propagation
                            PropagatorResult result = resultColumn.Value;
                            generatedValues.Add(new KeyValuePair <PropagatorResult, object>(result, value));

                            // register identifier if it exists
                            int identifier = result.Identifier;
                            if (PropagatorResult.NullIdentifier != identifier)
                            {
                                identifierValues.Add(identifier, value);
                            }
                        }
                    }

                    // Consume the current reader (and subsequent result sets) so that any errors
                    // executing the function can be intercepted
                    CommandHelper.ConsumeReader(reader);
                }
            }
            else
            {
                rowsAffected = m_dbCommand.ExecuteNonQuery();
            }

            // if an explicit rows affected parameter exists, use this value instead
            if (null != m_rowsAffectedParameter)
            {
                // by design, negative row counts indicate failure iff. an explicit rows
                // affected parameter is used
                if (DBNull.Value.Equals(m_rowsAffectedParameter.Value))
                {
                    rowsAffected = 0;
                }
                else
                {
                    try
                    {
                        rowsAffected = Convert.ToInt64(m_rowsAffectedParameter.Value, CultureInfo.InvariantCulture);
                    }
                    catch (Exception e)
                    {
                        if (UpdateTranslator.RequiresContext(e))
                        {
                            // wrap the exception
                            throw EntityUtil.Update(System.Data.Entity.Strings.Update_UnableToConvertRowsAffectedParameterToInt32(
                                                        m_rowsAffectedParameter.ParameterName, typeof(int).FullName), e, this.GetStateEntries(translator));
                        }
                        throw;
                    }
                }
            }

            return(rowsAffected);
        }
Exemplo n.º 20
0
 protected BaseList(IBaseList baseList)
 {
     BaseListImplementor = baseList;
 }
Exemplo n.º 21
0
        internal override async Task <long> ExecuteAsync(
            Dictionary <int, object> identifierValues,
            List <KeyValuePair <PropagatorResult, object> > generatedValues,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            long num;

            using (DbCommand command = this.CreateCommand(identifierValues))
            {
                EntityConnection connection = this.Translator.Connection;
                command.Transaction = connection.CurrentTransaction == null ? (DbTransaction)null : connection.CurrentTransaction.StoreTransaction;
                command.Connection  = connection.StoreConnection;
                if (this.Translator.CommandTimeout.HasValue)
                {
                    command.CommandTimeout = this.Translator.CommandTimeout.Value;
                }
                int rowsAffected;
                if (this._modificationCommandTree.HasReader)
                {
                    rowsAffected = 0;
                    using (DbDataReader reader = await command.ExecuteReaderAsync(CommandBehavior.SequentialAccess, cancellationToken).WithCurrentCulture <DbDataReader>())
                    {
                        if (await reader.ReadAsync(cancellationToken).WithCurrentCulture <bool>())
                        {
                            ++rowsAffected;
                            IBaseList <EdmMember> members = TypeHelpers.GetAllStructuralMembers((EdmType)this.CurrentValues.StructuralType);
                            for (int ordinal = 0; ordinal < reader.FieldCount; ++ordinal)
                            {
                                string    columnName = reader.GetName(ordinal);
                                EdmMember member     = members[columnName];
                                object    value;
                                if (Helper.IsSpatialType(member.TypeUsage))
                                {
                                    if (!await reader.IsDBNullAsync(ordinal, cancellationToken).WithCurrentCulture <bool>())
                                    {
                                        value = await SpatialHelpers.GetSpatialValueAsync(this.Translator.MetadataWorkspace, reader, member.TypeUsage, ordinal, cancellationToken).WithCurrentCulture <object>();

                                        goto label_14;
                                    }
                                }
                                value = await reader.GetFieldValueAsync <object>(ordinal, cancellationToken).WithCurrentCulture <object>();

label_14:
                                int columnOrdinal = members.IndexOf(member);
                                PropagatorResult result = this.CurrentValues.GetMemberValue(columnOrdinal);
                                generatedValues.Add(new KeyValuePair <PropagatorResult, object>(result, value));
                                int identifier = result.Identifier;
                                if (-1 != identifier)
                                {
                                    identifierValues.Add(identifier, value);
                                }
                            }
                        }
                        await CommandHelper.ConsumeReaderAsync(reader, cancellationToken).WithCurrentCulture();
                    }
                }
                else
                {
                    rowsAffected = await command.ExecuteNonQueryAsync(cancellationToken).WithCurrentCulture <int>();
                }
                num = (long)rowsAffected;
            }
            return(num);
        }
Exemplo n.º 22
0
 public OrnamentedList(IBaseList baseList)
     : base(baseList)
 {
 }