public virtual async Task <DbDataReader> ExecuteReaderAsync(string commandText, MySqlParameterCollection parameterCollection,
                                                                    CommandBehavior behavior, IOBehavior ioBehavior, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            using (m_command.RegisterCancel(cancellationToken))
            {
                m_command.Connection.Session.StartQuerying(m_command);
                m_command.LastInsertedId = -1;
                var statementPreparerOptions = StatementPreparerOptions.None;
                if (m_command.Connection.AllowUserVariables || m_command.CommandType == CommandType.StoredProcedure)
                {
                    statementPreparerOptions |= StatementPreparerOptions.AllowUserVariables;
                }
                if (m_command.Connection.OldGuids)
                {
                    statementPreparerOptions |= StatementPreparerOptions.OldGuids;
                }
                if (m_command.CommandType == CommandType.StoredProcedure)
                {
                    statementPreparerOptions |= StatementPreparerOptions.AllowOutputParameters;
                }
                var preparer = new MySqlStatementPreparer(commandText, parameterCollection, statementPreparerOptions);
                var payload  = new PayloadData(preparer.ParseAndBindParameters());
                try
                {
                    await m_command.Connection.Session.SendAsync(payload, ioBehavior, CancellationToken.None).ConfigureAwait(false);

                    return(await MySqlDataReader.CreateAsync(m_command, behavior, ioBehavior).ConfigureAwait(false));
                }
                catch (MySqlException ex) when(ex.Number == (int)MySqlErrorCode.QueryInterrupted && cancellationToken.IsCancellationRequested)
                {
                    throw new OperationCanceledException(cancellationToken);
                }
                catch (Exception ex) when(payload.ArraySegment.Count > 4_194_304 && (ex is SocketException || ex is IOException || ex is MySqlProtocolException))
                {
                    // the default MySQL Server value for max_allowed_packet (in MySQL 5.7) is 4MiB: https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_max_allowed_packet
                    // use "decimal megabytes" (to round up) when creating the exception message
                    int megabytes = payload.ArraySegment.Count / 1_000_000;

                    throw new MySqlException("Error submitting {0}MB packet; ensure 'max_allowed_packet' is greater than {0}MB.".FormatInvariant(megabytes), ex);
                }
            }
        }
Ejemplo n.º 2
0
        private PayloadData CreateQueryPayload(string commandText, MySqlParameterCollection parameterCollection)
        {
            var statementPreparerOptions = StatementPreparerOptions.None;

            if (m_command.Connection.AllowUserVariables || m_command.CommandType == CommandType.StoredProcedure)
            {
                statementPreparerOptions |= StatementPreparerOptions.AllowUserVariables;
            }
            if (m_command.Connection.OldGuids)
            {
                statementPreparerOptions |= StatementPreparerOptions.OldGuids;
            }
            if (m_command.CommandType == CommandType.StoredProcedure)
            {
                statementPreparerOptions |= StatementPreparerOptions.AllowOutputParameters;
            }
            var preparer = new MySqlStatementPreparer(commandText, parameterCollection, statementPreparerOptions);

            return(new PayloadData(preparer.ParseAndBindParameters()));
        }
        public virtual async Task <DbDataReader> ExecuteReaderAsync(string commandText, MySqlParameterCollection parameterCollection,
                                                                    CommandBehavior behavior, IOBehavior ioBehavior, CancellationToken cancellationToken)
        {
            m_command.VerifyValid();
            var connection = m_command.Connection;

            connection.HasActiveReader = true;

            MySqlDataReader reader = null;

            try
            {
                m_command.LastInsertedId = -1;
                var statementPreparerOptions = StatementPreparerOptions.None;
                if (connection.AllowUserVariables || m_command.CommandType == CommandType.StoredProcedure)
                {
                    statementPreparerOptions |= StatementPreparerOptions.AllowUserVariables;
                }
                if (connection.OldGuids)
                {
                    statementPreparerOptions |= StatementPreparerOptions.OldGuids;
                }
                var preparer = new MySqlStatementPreparer(commandText, parameterCollection, statementPreparerOptions);
                var payload  = new PayloadData(preparer.ParseAndBindParameters());
                await connection.Session.SendAsync(payload, ioBehavior, cancellationToken).ConfigureAwait(false);

                reader = await MySqlDataReader.CreateAsync(m_command, behavior, ioBehavior, cancellationToken).ConfigureAwait(false);

                return(reader);
            }
            finally
            {
                if (reader == null)
                {
                    // received an error from MySQL and never created an active reader
                    connection.HasActiveReader = false;
                }
            }
        }