Beispiel #1
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));
        }
        /// <summary>
        ///     See comments in <see cref="UpdateCommand" />.
        /// </summary>
        internal override async Task <long> ExecuteAsync(
            Dictionary <int, object> identifierValues,
            List <KeyValuePair <PropagatorResult, object> > generatedValues, CancellationToken cancellationToken)
        {
            var connection = Translator.Connection;

            // configure command to use the connection and transaction for this session
            _dbCommand.Transaction = ((null == connection.CurrentTransaction)
                                          ? null
                                          : connection.CurrentTransaction.StoreTransaction);
            _dbCommand.Connection = connection.StoreConnection;
            if (Translator.CommandTimeout.HasValue)
            {
                _dbCommand.CommandTimeout = Translator.CommandTimeout.Value;
            }

            SetInputIdentifiers(identifierValues);

            // Execute the query
            long rowsAffected;

            if (null != ResultColumns)
            {
                // If there are result columns, read the server gen results
                rowsAffected = 0;
                var members = TypeHelpers.GetAllStructuralMembers(CurrentValues.StructuralType);
                using (
                    var reader =
                        await
                        _dbCommand.ExecuteReaderAsync(CommandBehavior.SequentialAccess, cancellationToken).ConfigureAwait(
                            continueOnCapturedContext: false))
                {
                    // Retrieve only the first row from the first result set
                    if (await reader.ReadAsync(cancellationToken).ConfigureAwait(continueOnCapturedContext: false))
                    {
                        rowsAffected++;

                        foreach (var resultColumn in 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
                        {
                            var    columnOrdinal = resultColumn.Key;
                            var    columnType    = members[resultColumn.Value.RecordOrdinal].TypeUsage;
                            object value;

                            if (Helper.IsSpatialType(columnType)
                                &&
                                !await
                                reader.IsDBNullAsync(columnOrdinal, cancellationToken).ConfigureAwait(continueOnCapturedContext: false))
                            {
                                value =
                                    await
                                    SpatialHelpers.GetSpatialValueAsync(
                                        Translator.MetadataWorkspace, reader, columnType, columnOrdinal, cancellationToken).ConfigureAwait(
                                        continueOnCapturedContext: false);
                            }
                            else
                            {
                                value =
                                    await
                                    reader.GetFieldValueAsync <object>(columnOrdinal, cancellationToken).ConfigureAwait(
                                        continueOnCapturedContext: false);
                            }

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

                            // register identifier if it exists
                            var 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
                    await CommandHelper.ConsumeReaderAsync(reader, cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
                }
            }
            else
            {
                rowsAffected = await _dbCommand.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(continueOnCapturedContext: false);
            }

            return(GetRowsAffected(rowsAffected, Translator));
        }
Beispiel #3
0
        // <summary>
        // See comments in <see cref="UpdateCommand" />.
        // </summary>
        internal override async Task <long> ExecuteAsync(
            Dictionary <int, object> identifierValues,
            List <KeyValuePair <PropagatorResult, object> > generatedValues, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            // Compile command
            using (var command = CreateCommand(identifierValues))
            {
                var connection = Translator.Connection;
                // configure command to use the connection and transaction for this session
                command.Transaction = ((null == connection.CurrentTransaction)
                                           ? null
                                           : connection.CurrentTransaction.StoreTransaction);
                command.Connection = connection.StoreConnection;

                // FIX Klaus 12/09/2018: Prevents the previously assigned transaction from being deleted due to collateral effects during connection assignment.
                command.Transaction = command.Transaction
                                      ?? (connection.CurrentTransaction == null ? null : connection.CurrentTransaction.StoreTransaction);

                if (Translator.CommandTimeout.HasValue)
                {
                    command.CommandTimeout = Translator.CommandTimeout.Value;
                }

                // Execute the query
                int rowsAffected;
                if (_modificationCommandTree.HasReader)
                {
                    // retrieve server gen results
                    rowsAffected = 0;
                    using (
                        var reader =
                            await
                            command.ExecuteReaderAsync(CommandBehavior.SequentialAccess, cancellationToken).WithCurrentCulture())
                    {
                        if (await reader.ReadAsync(cancellationToken).WithCurrentCulture())
                        {
                            rowsAffected++;

                            var members = TypeHelpers.GetAllStructuralMembers(CurrentValues.StructuralType);

                            for (var ordinal = 0; ordinal < reader.FieldCount; ordinal++)
                            {
                                // column name of result corresponds to column name of table
                                var    columnName = reader.GetName(ordinal);
                                var    member     = members[columnName];
                                object value;
                                if (Helper.IsSpatialType(member.TypeUsage)
                                    &&
                                    !await reader.IsDBNullAsync(ordinal, cancellationToken).WithCurrentCulture())
                                {
                                    value =
                                        await
                                        SpatialHelpers.GetSpatialValueAsync(
                                            Translator.MetadataWorkspace, reader, member.TypeUsage, ordinal, cancellationToken).
                                        WithCurrentCulture();
                                }
                                else
                                {
                                    value =
                                        await reader.GetFieldValueAsync <object>(ordinal, cancellationToken).WithCurrentCulture();
                                }

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

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

                                // register identifier if it exists
                                var 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
                        await CommandHelper.ConsumeReaderAsync(reader, cancellationToken).WithCurrentCulture();
                    }
                }
                else
                {
                    rowsAffected = await command.ExecuteNonQueryAsync(cancellationToken).WithCurrentCulture();
                }

                return(rowsAffected);
            }
        }
Beispiel #4
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);
        }