Esempio n. 1
0
        /// <summary>
        /// Visits the children of the given payload element and replaces it with a copy if any child changes
        /// </summary>
        /// <param name="payloadElement">The payload element to potentially replace</param>
        /// <returns>The original element or a copy to replace it with</returns>
        public virtual ODataPayloadElement Visit(EmptyPayload payloadElement)
        {
            ExceptionUtilities.CheckArgumentNotNull(payloadElement, "payloadElement");
            if (this.alwaysReplace)
            {
                return(payloadElement.ReplaceWith(new EmptyPayload()));
            }

            return(payloadElement);
        }
            /// <summary>
            /// Visits a payload element whose root is an EmptyPayload.
            /// </summary>
            /// <param name="expected">The root node of payload element being visited.</param>
            public void Visit(EmptyPayload expected)
            {
                ExceptionUtilities.CheckArgumentNotNull(expected, "expected");
                var observed = this.GetNextObservedElement <EmptyPayload>();

                using (this.Assert.WithMessage("Empty payload did not match expectation"))
                {
                    this.CompareAnnotations(expected.Annotations, observed.Annotations);
                }
            }
Esempio n. 3
0
 /// <summary>
 /// Calls the base class method to process an empty payload.
 /// </summary>
 /// <param name="payloadElement">The empty payload to process.</param>
 public override void Visit(EmptyPayload payloadElement)
 {
     base.Visit(payloadElement);
 }
Esempio n. 4
0
        public async Task <ResultSet> ReadResultSetHeaderAsync(IOBehavior ioBehavior)
        {
            // ResultSet can be re-used, so initialize everything
            BufferState       = ResultSetState.None;
            ColumnDefinitions = null;
            ColumnTypes       = null;
            LastInsertId      = 0;
            RecordsAffected   = null;
            State             = ResultSetState.None;
            m_columnDefinitionPayloadUsedBytes = 0;
            m_dataLengths = null;
            m_dataOffsets = null;
            m_readBuffer.Clear();
            m_row         = null;
            m_rowBuffered = null;
            m_hasRows     = false;

            try
            {
                while (true)
                {
                    var payload = await Session.ReceiveReplyAsync(ioBehavior, CancellationToken.None).ConfigureAwait(false);

                    var firstByte = payload.HeaderByte;
                    if (firstByte == OkPayload.Signature)
                    {
                        var ok = OkPayload.Create(payload);
                        RecordsAffected = (RecordsAffected ?? 0) + ok.AffectedRowCount;
                        LastInsertId    = unchecked ((long)ok.LastInsertId);
                        if (ok.NewSchema != null)
                        {
                            Connection.Session.DatabaseOverride = ok.NewSchema;
                        }
                        ColumnDefinitions = null;
                        ColumnTypes       = null;
                        State             = (ok.ServerStatus & ServerStatus.MoreResultsExist) == 0
                                                        ? ResultSetState.NoMoreData
                                                        : ResultSetState.HasMoreData;
                        if (State == ResultSetState.NoMoreData)
                        {
                            break;
                        }
                    }
                    else if (firstByte == LocalInfilePayload.Signature)
                    {
                        try
                        {
                            var localInfile = LocalInfilePayload.Create(payload);
                            if (!IsHostVerified(Connection) &&
                                !localInfile.FileName.StartsWith(MySqlBulkLoader.StreamPrefix, StringComparison.Ordinal))
                            {
                                throw new NotSupportedException("Use SourceStream or SslMode >= VerifyCA for LOAD DATA LOCAL INFILE");
                            }

                            using (var stream = localInfile.FileName.StartsWith(MySqlBulkLoader.StreamPrefix, StringComparison.Ordinal) ?
                                                MySqlBulkLoader.GetAndRemoveStream(localInfile.FileName) :
                                                File.OpenRead(localInfile.FileName))
                            {
                                byte[] readBuffer = new byte[65536];
                                int    byteCount;
                                while ((byteCount = await stream.ReadAsync(readBuffer, 0, readBuffer.Length).ConfigureAwait(false)) > 0)
                                {
                                    payload = new PayloadData(new ArraySegment <byte>(readBuffer, 0, byteCount));
                                    await Session.SendReplyAsync(payload, ioBehavior, CancellationToken.None).ConfigureAwait(false);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            // store the exception, to be thrown after reading the response packet from the server
                            ReadResultSetHeaderException = new MySqlException("Error during LOAD DATA LOCAL INFILE", ex);
                        }

                        await Session.SendReplyAsync(EmptyPayload.Create(), ioBehavior, CancellationToken.None).ConfigureAwait(false);
                    }
                    else
                    {
                        int ReadColumnCount(ArraySegment <byte> arraySegment)
                        {
                            var reader       = new ByteArrayReader(arraySegment);
                            var columnCount_ = (int)reader.ReadLengthEncodedInteger();

                            if (reader.BytesRemaining != 0)
                            {
                                throw new MySqlException("Unexpected data at end of column_count packet; see https://github.com/mysql-net/MySqlConnector/issues/324");
                            }
                            return(columnCount_);
                        }
                        var columnCount = ReadColumnCount(payload.ArraySegment);

                        // reserve adequate space to hold a copy of all column definitions (but note that this can be resized below if we guess too small)
                        Array.Resize(ref m_columnDefinitionPayloads, columnCount * 96);

                        ColumnDefinitions = new ColumnDefinitionPayload[columnCount];
                        ColumnTypes       = new MySqlDbType[columnCount];
                        m_dataOffsets     = new int[columnCount];
                        m_dataLengths     = new int[columnCount];

                        for (var column = 0; column < ColumnDefinitions.Length; column++)
                        {
                            payload = await Session.ReceiveReplyAsync(ioBehavior, CancellationToken.None).ConfigureAwait(false);

                            var arraySegment = payload.ArraySegment;

                            // 'Session.ReceiveReplyAsync' reuses a shared buffer; make a copy so that the column definitions can always be safely read at any future point
                            if (m_columnDefinitionPayloadUsedBytes + arraySegment.Count > m_columnDefinitionPayloads.Length)
                            {
                                Array.Resize(ref m_columnDefinitionPayloads, Math.Max(m_columnDefinitionPayloadUsedBytes + arraySegment.Count, m_columnDefinitionPayloadUsedBytes * 2));
                            }
                            Buffer.BlockCopy(arraySegment.Array, arraySegment.Offset, m_columnDefinitionPayloads, m_columnDefinitionPayloadUsedBytes, arraySegment.Count);

                            var columnDefinition = ColumnDefinitionPayload.Create(new ArraySegment <byte>(m_columnDefinitionPayloads, m_columnDefinitionPayloadUsedBytes, arraySegment.Count));
                            ColumnDefinitions[column]           = columnDefinition;
                            ColumnTypes[column]                 = TypeMapper.ConvertToMySqlDbType(columnDefinition, treatTinyAsBoolean: Connection.TreatTinyAsBoolean, guidFormat: Connection.GuidFormat);
                            m_columnDefinitionPayloadUsedBytes += arraySegment.Count;
                        }

                        if (!Session.SupportsDeprecateEof)
                        {
                            payload = await Session.ReceiveReplyAsync(ioBehavior, CancellationToken.None).ConfigureAwait(false);

                            EofPayload.Create(payload);
                        }

                        LastInsertId = -1;
                        State        = ResultSetState.ReadResultSetHeader;
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                ReadResultSetHeaderException = ex;
            }
            finally
            {
                BufferState = State;
            }

            return(this);
        }
 /// <summary>
 /// Visits a payload element whose root is a EmptyPayload.
 /// </summary>
 /// <param name="payloadElement">The root node of payload element being visited.</param>
 public void Visit(EmptyPayload payloadElement)
 {
     throw new TaupoNotSupportedException("Json serialization does not allow element of type: " + payloadElement.ElementType.ToString());
 }
 /// <summary>
 /// Visits the payload element and annotates it with metadata
 /// </summary>
 /// <param name="payloadElement">The payload element to visit</param>
 public override void Visit(EmptyPayload payloadElement)
 {
     throw new TaupoNotSupportedException("Should have been fixed in normalizer");
 }
 /// <summary>
 /// Calls the base class method to process an empty payload.
 /// </summary>
 /// <param name="payloadElement">The empty payload to process.</param>
 public override void Visit(EmptyPayload payloadElement)
 {
     base.Visit(payloadElement);
 }
Esempio n. 8
0
        public async Task <ResultSet> ReadResultSetHeaderAsync(IOBehavior ioBehavior)
        {
            // ResultSet can be re-used, so initialize everything
            BufferState       = ResultSetState.None;
            ColumnDefinitions = null;
            LastInsertId      = 0;
            RecordsAffected   = 0;
            State             = ResultSetState.None;
            m_dataLengths     = null;
            m_dataOffsets     = null;
            m_readBuffer.Clear();
            m_row         = null;
            m_rowBuffered = null;

            try
            {
                while (true)
                {
                    var payload = await Session.ReceiveReplyAsync(ioBehavior, CancellationToken.None).ConfigureAwait(false);

                    var firstByte = payload.HeaderByte;
                    if (firstByte == OkPayload.Signature)
                    {
                        var ok = OkPayload.Create(payload);
                        RecordsAffected  += ok.AffectedRowCount;
                        LastInsertId      = ok.LastInsertId;
                        ColumnDefinitions = null;
                        State             = (ok.ServerStatus & ServerStatus.MoreResultsExist) == 0
                                                        ? ResultSetState.NoMoreData
                                                        : ResultSetState.HasMoreData;
                        if (State == ResultSetState.NoMoreData)
                        {
                            break;
                        }
                    }
                    else if (firstByte == LocalInfilePayload.Signature)
                    {
                        try
                        {
                            var localInfile = LocalInfilePayload.Create(payload);
                            using (var stream = localInfile.FileName.StartsWith(MySqlBulkLoader.StreamPrefix, StringComparison.Ordinal) ?
                                                MySqlBulkLoader.GetAndRemoveStream(localInfile.FileName) :
                                                File.OpenRead(localInfile.FileName))
                            {
                                byte[] readBuffer = new byte[65536];
                                int    byteCount;
                                while ((byteCount = await stream.ReadAsync(readBuffer, 0, readBuffer.Length).ConfigureAwait(false)) > 0)
                                {
                                    payload = new PayloadData(new ArraySegment <byte>(readBuffer, 0, byteCount));
                                    await Session.SendReplyAsync(payload, ioBehavior, CancellationToken.None).ConfigureAwait(false);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            // store the exception, to be thrown after reading the response packet from the server
                            ReadResultSetHeaderException = new MySqlException("Error during LOAD DATA LOCAL INFILE", ex);
                        }

                        await Session.SendReplyAsync(EmptyPayload.Create(), ioBehavior, CancellationToken.None).ConfigureAwait(false);
                    }
                    else
                    {
                        var reader      = new ByteArrayReader(payload.ArraySegment);
                        var columnCount = (int)reader.ReadLengthEncodedInteger();
                        ColumnDefinitions = new ColumnDefinitionPayload[columnCount];
                        m_dataOffsets     = new int[columnCount];
                        m_dataLengths     = new int[columnCount];

                        for (var column = 0; column < ColumnDefinitions.Length; column++)
                        {
                            payload = await Session.ReceiveReplyAsync(ioBehavior, CancellationToken.None).ConfigureAwait(false);

                            ColumnDefinitions[column] = ColumnDefinitionPayload.Create(payload);
                        }

                        payload = await Session.ReceiveReplyAsync(ioBehavior, CancellationToken.None).ConfigureAwait(false);

                        EofPayload.Create(payload);

                        LastInsertId = -1;
                        State        = ResultSetState.ReadResultSetHeader;
                        break;
                    }
                }
            }
            catch (Exception ex)
            {
                ReadResultSetHeaderException = ex;
            }
            finally
            {
                BufferState = State;
            }

            return(this);
        }
Esempio n. 9
0
 /// <summary>
 /// Visits a payload element whose root is a EmptyPayload.
 /// </summary>
 /// <param name="payloadElement">The root node of payload element being visited.</param>
 public void Visit(EmptyPayload payloadElement)
 {
     throw new TaupoNotSupportedException("Json serialization does not allow element of type: " + payloadElement.ElementType.ToString());
 }
Esempio n. 10
0
 /// <summary>
 /// Visits the payload element
 /// </summary>
 /// <param name="payloadElement">The payload element to visit</param>
 public virtual void Visit(EmptyPayload payloadElement)
 {
 }
 /// <summary>
 /// Visits a payload element whose root is an EmptyPayload.
 /// </summary>
 /// <param name="payloadElement">The root node of payload element being visited.</param>
 public void Visit(EmptyPayload payloadElement)
 {
     // using this.assert.Fail causes code coverage issue
     throw new AssertionFailedException("Payload should not have been empty");
 }
 /// <summary>
 /// Visits the payload element and annotates it with metadata
 /// </summary>
 /// <param name="payloadElement">The payload element to visit</param>
 public override void Visit(EmptyPayload payloadElement)
 {
     throw new TaupoNotSupportedException("Should have been fixed in normalizer");
 }