bool Parse_PrepareOk_Header(MySqlStreamReader reader)
        {
            //this is first step ***
            if (!reader.Ensure(PACKET_HEADER_LENGTH + 1))
            {
                return(_needMoreData = true);
            }
            _currentHeader = reader.ReadPacketHeader();
            byte type = reader.PeekByte();

            switch (type)
            {
            case ERROR_CODE:
                _parsingState = PrepareResponseParseState.Error_Content;
                break;

            case EOF_CODE:
            case OK_CODE:
                _parsingState = PrepareResponseParseState.OkPrepare_Content;
                break;

            default:
                throw new NotSupportedException("Packet type don't match!!");
            }
            return(false);
        }
        void StoreBuffer(MySqlStreamReader reader, int length)
        {
            if (ms == null)
            {
                ms = new MemoryStream();
                //buffer data to this
            }
            ms.Write(reader.ReadBuffer(length), 0, length);

            //reader.SkipForward(length);
            // throw new NotSupportedException();
            ////TODO: review buffer mx here ****
            //byte[] dataTemp = _mysqlStreamReader.ReadBuffer((int)length);
            //int existingLargeDataBufferLen = (largeDataBuffer == null) ?
            //    0 :
            //    largeDataBuffer.Length;
            //if (existingLargeDataBufferLen > 0)
            //{
            //    //merge ...
            //    byte[] newData = new byte[existingLargeDataBufferLen + dataTemp.Length];
            //    Buffer.BlockCopy(largeDataBuffer, 0, newData, 0, largeDataBuffer.Length);
            //    Buffer.BlockCopy(dataTemp, 0, newData, largeDataBuffer.Length, dataTemp.Length);
            //    largeDataBuffer = newData;
            //}
            //else
            //{
            //    largeDataBuffer = dataTemp;
            //}
        }
 bool Parse_Ok_Content(MySqlStreamReader reader)
 {
     if (!reader.Ensure(_currentHeader.ContentLength)) //check if length is enough to parse
     {
         return(_needMoreData = true);
     }
     if (_currentMultResultSet != null)
     {
         //in multiple result set mode ***
         //see https://dev.mysql.com/doc/internals/en/multi-resultset.html
         //
         var okPacket = new OkPacket(_currentHeader, _isProtocol41);
         okPacket.ParsePacketContent(reader);
         _parseResult  = _currentMultResultSet;
         _parsingState = ResultPacketState.ShouldEnd; //*
         //
         _currentMultResultSet = null;                //reset
     }
     else
     {
         var okPacket = new OkPacket(_currentHeader, _isProtocol41);
         okPacket.ParsePacketContent(reader);
         _parseResult  = new MySqlOkResult(okPacket);
         _parsingState = ResultPacketState.ShouldEnd; //*
     }
     return(true);
 }
        //----------------------------------------------------
        bool Parse_BindingField_Header(MySqlStreamReader reader)
        {
            if (!reader.Ensure(PACKET_HEADER_LENGTH + 1))
            {
                return(_needMoreData = true); //need more
            }
            _currentHeader = reader.ReadPacketHeader();
            byte type = reader.PeekByte();

            switch (type)
            {
            case ERROR_CODE:
                _parsingState = PrepareResponseParseState.Error_Content;
                break;

            case EOF_CODE:
                _parsingState = PrepareResponseParseState.BindingField_EOF;
                break;

            default:
                _parsingState = PrepareResponseParseState.BindingField_Content;
                break;
            }
            return(false);
        }
        bool Parse_Field_Content(MySqlStreamReader reader)
        {
            if (!reader.Ensure(_currentHeader.ContentLength)) //check if length is enough to parse
            {
#if DEBUG
                reader.dbugMonitorData1 = true;
#endif
                return(_needMoreData = true);
            }

#if DEBUG
            if (reader.dbugMonitorData1)
            {
            }
#endif
            var fieldPacket = new FieldPacket(_currentHeader, _isProtocol41);
            fieldPacket.ParsePacketContent(reader);
            fieldPacket.FieldIndex = _tableHeader.ColumnCount; //set this before  add to field list
            _tableHeader.AddField(fieldPacket);

#if DEBUG
            //TODO:review here
            if (fieldPacket.dbugFailure)
            {
                throw new NotSupportedException();
            }
#endif

            //next state => field header of next field
            _parsingState = ResultPacketState.Field_Header;
            return(false);
        }
        bool Parse_Field_Header(MySqlStreamReader reader)
        {
            if (!reader.Ensure(PACKET_HEADER_LENGTH + 1)) //check if length is enough to parse
            {
                return(_needMoreData = true);
            }

            _currentHeader = reader.ReadPacketHeader();
            byte packetType = reader.PeekByte();

            switch (packetType)
            {
            case ERROR_CODE:
                _parsingState = ResultPacketState.Error_Content;
                break;

            case EOF_CODE:
            case OK_CODE:
                _parsingState = ResultPacketState.Field_EOF;
                break;

            default:
                //next state => field content of this field
                _parsingState = ResultPacketState.Field_Content;
                break;
            }
            return(false);
        }
Esempio n. 7
0
 public override void Parse(MySqlStreamReader reader)
 {
     _finalResult = null;
     while (!StepParse(reader))
     {
         ;
     }
     //StepParse() return true if
     //1. need more data or
     //2. finish
 }
Esempio n. 8
0
        bool Parse_Field_EOF(MySqlStreamReader reader)
        {
            if (!reader.Ensure(_currentHeader.ContentLength)) //check if length is enough to parse
            {
                return(_needMoreData = true);
            }
            var eofPacket = new EofPacket(_currentHeader, this._isProtocol41);

            eofPacket.ParsePacketContent(reader);
            _parsingState = ResultPacketState.Row_Header;
            return(false);
        }
Esempio n. 9
0
        public override void Parse(MySqlStreamReader reader)
        {
            _finalResult = null;
            //1.create connection frame
            //_writer.Reset();
            PacketHeader header = reader.ReadPacketHeader();

            _handshake = new HandshakePacket(header);
            //check if
            _handshake.ParsePacketContent(reader);
            _finalResult = new MySqlHandshakeResult(_handshake);
        }
Esempio n. 10
0
        bool Parse_Error_Content(MySqlStreamReader reader)
        {
            if (!reader.Ensure(_currentHeader.ContentLength)) //check if length is enough to parse
            {
                return(_needMoreData = true);
            }
            var errPacket = new ErrPacket(_currentHeader);

            errPacket.ParsePacketContent(reader);
            //------------------------
            _parseResult  = new MySqlErrorResult(errPacket);
            _parsingState = ResultPacketState.ShouldEnd;
            return(true);//finished
        }
Esempio n. 11
0
        bool Parse_ColumnField_Content(MySqlStreamReader reader)
        {
            if (!reader.Ensure(_currentHeader.ContentLength))
            {
                return(_needMoreData = true);
            }
            var field = new FieldPacket(_currentHeader, _isProtocol41);

            field.ParsePacketContent(reader);
            field.FieldIndex = _tableHeader.ColumnCount; //set this before  add to field list
            _tableHeader.AddField(field);
            //back to field header
            _parsingState = PrepareResponseParseState.ColumnField_Header;
            return(false);
        }
Esempio n. 12
0
        //----------------------------------------------------
        bool ParseErrorPacket(MySqlStreamReader reader)
        {
            if (!reader.Ensure(_currentHeader.ContentLength))
            {
                return(_needMoreData = true);
            }
            var errPacket = new ErrPacket(_currentHeader);

            errPacket.ParsePacketContent(reader);
            //
            _finalResult  = new MySqlErrorResult(errPacket);
            _parsingState = PrepareResponseParseState.ShouldEnd;
            reader.Reset();
            return(true);
        }
Esempio n. 13
0
        /// <summary>
        /// parse header part of the result set
        /// </summary>
        bool Parse_Header_Content(MySqlStreamReader reader)
        {
            if (!reader.Ensure(_currentHeader.ContentLength))
            {
                return(_needMoreData = true);
            }
            //can parse
            var resultSetHeaderPacket = new ResultSetHeaderPacket(_currentHeader);

            resultSetHeaderPacket.ParsePacketContent(reader);
            _tableHeader = new TableHeader(this.ForPrepareResult);

            _parsingState = ResultPacketState.Field_Header;
            _rows         = new List <DataRowPacket>();
            return(false);
        }
Esempio n. 14
0
        bool Parse_ColumnField_EOF(MySqlStreamReader reader)
        {
            if (!reader.Ensure(_currentHeader.ContentLength))
            {
                return(_needMoreData = true);
            }

            var eofPacket = new EofPacket(_currentHeader, this._isProtocol41);

            eofPacket.ParsePacketContent(reader);
            //
            _finalResult  = new MySqlPrepareResponseResult(_okPrepare, _tableHeader);
            _parsingState = PrepareResponseParseState.ShouldEnd;
            reader.Reset();
            return(true);//finish
        }
Esempio n. 15
0
        bool Parse_Row_EOF(MySqlStreamReader reader)
        {
            if (!reader.Ensure(_currentHeader.ContentLength)) //check if length is enough to parse
            {
                return(_needMoreData = true);
            }
            //finish all of each row
            var eofPacket = new EofPacket(_currentHeader, this._isProtocol41);

            eofPacket.ParsePacketContent(reader);

            if (((eofPacket.serverStatus & (int)MySqlServerStatus.SERVER_MORE_RESULTS_EXISTS)) != 0)
            {
                var tableResult = new MySqlTableResult(_tableHeader, _rows);
                _rows = null;//reset

                //more than one result table
                //mu
                if (_currentMultResultSet != null)
                {
                    _currentMultResultSet.AddTableResult(tableResult);
                }
                else
                {
                    //first time
                    _currentMultResultSet = new MySqlMultiTableResult();
                    _currentMultResultSet.AddTableResult(tableResult);;
                    //not set _parseResult*** because this not finish
                }
                //--------------------
                //see: https://dev.mysql.com/doc/internals/en/multi-resultset.html
                //may has more than 1 result
                _parsingState = ResultPacketState.Header_Header;
                return(false);
            }
            else
            {
                //after finish we create a result table
                //the move rows into the table
                _parseResult = new MySqlTableResult(_tableHeader, _rows);
                //not link to the rows anymore
                _rows = null;
                _currentMultResultSet = null;
                _parsingState         = ResultPacketState.ShouldEnd; //***
                return(true);                                        //end
            }
        }
        public override void Parse(MySqlStreamReader reader)
        {
            //reset final result
            _parseResult = null;
            while (!StepParse(reader))
            {
                ;
            }
            //StepParse() return true if
            //1. need more data or
            //2. finish

            reader.ClearReadBuffer();

            if (_needMoreData)
            {
                //at any state if need more buffer
                //then stop parsing and return
                if (_supportPartialRelease)
                {
                    if (_rows != null && _rows.Count > 0)
                    {
                        _parseResult = new MySqlTableResult(_tableHeader, _rows.ToArray())
                        {
                            HasFollower = true
                        };
                    }
                    if (_generateResultMode)
                    {
                        //generate new result
#if DEBUG
                        if (_rows != null)
                        {
                        }
#endif
                        _rows.Clear();
                    }
                }
            }
            else if (_parsingState == ResultPacketState.ShouldEnd)
            {
                //reset
                reader.Reset();
                _parsingState = ResultPacketState.Header_Header;
            }
        }
Esempio n. 17
0
        bool Parse_Row_Header(MySqlStreamReader reader)
        {
            if (!reader.Ensure(PACKET_HEADER_LENGTH + 1))
            {
                return(_needMoreData = true);
            }
            _currentHeader = reader.ReadPacketHeader();
            if (_currentHeader.ContentLength == Packet.MAX_PACKET_LENGTH)
            {
                //need more than 1 packet for row content
                _needMoreThan1Packet = true;
                _parsingState        = ResultPacketState.Row_Content;
                return(false);
            }
            else if (_currentHeader.ContentLength > Packet.MAX_PACKET_LENGTH)
            {
                throw new NotSupportedException("???");
            }

            if (_needMoreThan1Packet)
            {
                //switch to row content
                _parsingState = ResultPacketState.Row_Content;
            }
            else
            {
                byte packetType = reader.PeekByte();
                switch (packetType)
                {
                case ERROR_CODE:
                    _parsingState = ResultPacketState.Error_Content;
                    break;

                case EOF_CODE:    //0x00 or 0xfe the OK packet header
                    _parsingState = ResultPacketState.Row_EOF;
                    break;

                default:
                    _parsingState = ResultPacketState.Row_Content;
                    break;
                }
            }

            return(false);
        }
Esempio n. 18
0
        bool StepParse(MySqlStreamReader reader)
        {
            //reset everytime before actual parse
            _needMoreData = false;
            switch (_parsingState)
            {
            case ResultPacketState.Header_Header:
                return(Parse_Header_Header(reader));

            case ResultPacketState.Header_Content:
                return(Parse_Header_Content(reader));

            //---------------------------------------
            case ResultPacketState.Field_Header:
                return(Parse_Field_Header(reader));

            case ResultPacketState.Field_Content:
                return(Parse_Field_Content(reader));

            case ResultPacketState.Field_EOF:
                return(Parse_Field_EOF(reader));

            //---------------------------------------
            case ResultPacketState.Row_Header:
                return(Parse_Row_Header(reader));

            case ResultPacketState.Row_Content:
                return(Parse_Row_Content(reader));

            case ResultPacketState.Row_EOF:
                return(Parse_Row_EOF(reader));

            //---------------------------------------
            case ResultPacketState.Error_Content:
                return(Parse_Error_Content(reader));

            case ResultPacketState.Ok_Content:
                return(Parse_Ok_Content(reader));

            default:
                throw new Exception("unknown step");
            }
        }
Esempio n. 19
0
        /// <summary>
        /// return ***true*** if finish or need more data
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        bool StepParse(MySqlStreamReader reader)
        {
            switch (_parsingState)
            {
            case PrepareResponseParseState.OkPrepare_Header:
                return(Parse_PrepareOk_Header(reader));

            case PrepareResponseParseState.OkPrepare_Content:
                return(Parse_PrepareOk_Content(reader));

            //------------------------------------------------
            case PrepareResponseParseState.BindingField_Header:
                return(Parse_BindingField_Header(reader));

            case PrepareResponseParseState.BindingField_Content:
                return(Parse_BindingField_Content(reader));

            case PrepareResponseParseState.BindingField_EOF:
                return(Parse_BindingField_EOF(reader));

            //------------------------------------------------
            case PrepareResponseParseState.ColumnField_Header:
                return(Parse_ColumnField_Header(reader));

            case PrepareResponseParseState.ColumnField_Content:
                return(Parse_ColumnField_Content(reader));

            case PrepareResponseParseState.ColumnField_EOF:
                return(Parse_ColumnField_EOF(reader));

            //------------------------------------------------
            case PrepareResponseParseState.ShouldEnd:
                reader.Reset();
                return(true);

            case PrepareResponseParseState.Error_Content:
                return(ParseErrorPacket(reader));

            default:
                throw new Exception("unknown step?");
            }
        }
        bool Parse_BindingField_EOF(MySqlStreamReader reader)
        {
            if (!reader.Ensure(_currentHeader.ContentLength))
            {
                return(_needMoreData = true);
            }

            var eofPacket = new EofPacket(_currentHeader, _isProtocol41);

            eofPacket.ParsePacketContent(reader);

            if (_okPrepare.num_columns > 0)
            {
                _parsingState = PrepareResponseParseState.ColumnField_Header;
                return(false);
            }
            else
            {
                _finalResult  = new MySqlPrepareResponseResult(_okPrepare, _tableHeader);
                _parsingState = PrepareResponseParseState.ShouldEnd;
                reader.Reset();
                return(true);
            }
        }
Esempio n. 21
0
        bool Parse_PrepareOk_Content(MySqlStreamReader reader)
        {
            if (!reader.Ensure(_currentHeader.ContentLength))
            {
                return(_needMoreData = true);
            }
            var okPrepare = new OkPrepareStmtPacket(_currentHeader);

            okPrepare.ParsePacketContent(reader);
            _okPrepare = okPrepare;
            //----------------------------------------------------
            _tableHeader = new TableHeader(true);
            //----------------------------------------------------
            //*** 3 possible way after read prepare ok header***
            if (okPrepare.num_params == 0)
            {
                //if prepare stmt dosn't have binding parameters
                if (okPrepare.num_columns > 0)
                {
                    //has some column
                    _parsingState = PrepareResponseParseState.ColumnField_Header;
                }
                else
                {
                    _finalResult  = new MySqlPrepareResponseResult(okPrepare, _tableHeader);
                    _parsingState = PrepareResponseParseState.ShouldEnd;
                    reader.Reset();
                    return(true); //finish
                }
            }
            else
            {
                _parsingState = PrepareResponseParseState.BindingField_Header;
            }
            return(false);
        }
 public override void ParsePacketContent(MySqlStreamReader r)
 {
     throw new NotImplementedException();
 }
 public override void ParsePacketContent(MySqlStreamReader r)
 {
     throw new NotImplementedException();
     //ParsePacketHeader(parser);
     //this.command = parser.ParseByte();
 }
        public override void ParsePacketContent(MySqlStreamReader r)
        {

            if (protocol41)
            {
                clientFlags = r.U4(); //4
                maxPacketSize = r.U4(); //4 
                charsetNumber = r.ReadByte();
                r.ReadFiller(23);
                user = r.ReadNullTerminatedString();
                scrambleBuff = r.ReadLengthCodedBuffer();
                database = r.ReadNullTerminatedString();
            }
            else
            {
                clientFlags = r.U2();//2
                maxPacketSize = r.U3();//3
                user = r.ReadNullTerminatedString();
                scrambleBuff = r.ReadBuffer(8);
                database = r.ReadLengthCodedString();
            }
        }
        public override void ParsePacketContent(MySqlStreamReader r)
        {

            _fieldCount = r.ReadLengthCodedNumber();
            if (r.ReachedPacketEnd())
            {
                return;
            }
            if (_fieldCount == 0)
            {
                _extraStr = r.ReadPacketTerminatedString();
            }
            else
            {
                _extraNumber = r.ReadLengthCodedNumber();
                _extraStr = r.ReadPacketTerminatedString();//null;
            }
        }
        public override void ParsePacketContent(MySqlStreamReader r)
        {

            _fieldCount = r.ReadByte();
            affectedRows = r.ReadLengthCodedNumber();
            insertId = r.ReadLengthCodedNumber();
            if (_protocol41)
            {
                _serverStatus = r.U2();
                _warningCount = r.U2();
            }

            //TODO: review here again
            //https://dev.mysql.com/doc/internals/en/packet-OK_Packet.html

            _message = r.ReadPacketTerminatedString();
            //var m = this.message.match(/\schanged:\s * (\d +) / i);

            //if (m !== null)
            //{
            //    this.changedRows = parseInt(m[1], 10);
            //}
        }
        bool Parse_Row_Content(MySqlStreamReader reader)
        {
            if (!reader.Ensure(_currentHeader.ContentLength))
            {
                return _needMoreData = true;
            }
            if (_currentHeader.ContentLength > Packet.MAX_PACKET_LENGTH)
            {
                throw new NotSupportedException("???");
            }
            else if (_currentHeader.ContentLength >= Packet.MAX_PACKET_LENGTH)
            {
                //can't complete in this round 
                //so store data into temp extra large buffer 
                //and set isLargeData= true
                StoreBuffer(reader, (int)_currentHeader.ContentLength);
                isLargeData = true;
                //we still in the row content state
                _parsingState = ResultPacketState.Row_Header;
                return false;
            }
            //--------------------------------       

            if (_generateResultMode)
            {
                //this is normal mode (opposite to JustFlushOutMode)
                //in this mode we parse packet content 
                //and add it to the output rows 
                //----------------------------------
                //in  this version row buffer len must < int.MaxLength
                if (_currentHeader.ContentLength > int.MaxValue)
                {
                    throw new NotSupportedException("not support this length");
                }
                //------------------------------------  
                if (isLargeData)
                {
                    if (ms == null)
                    {   //it should not be null here
                        throw new NotSupportedException();//?   
                    }
                    ms.Write(reader.ReadBuffer((int)_currentHeader.ContentLength), 0,
                        (int)_currentHeader.ContentLength);
                    _rows.Add(new DataRowPacket(_currentHeader, ms.ToArray()));

                    ms.Close();
                    ms.Dispose();
                    ms = null;

                    isLargeData = false; //reset
                }
                else
                {
                    _rows.Add(new DataRowPacket(_currentHeader,
                    reader.ReadBuffer((int)_currentHeader.ContentLength)));
                }

            }
            else
            {
                //just flush data*** 
                //not create data row
                if (_currentHeader.ContentLength > int.MaxValue)
                {
                    throw new Exception("not support content length> int.MaxValue");
                }
                reader.SkipForward((int)_currentHeader.ContentLength);
            }
            //-----------------------------------------------------------------------
            //after this row, next state = next row header
            _parsingState = ResultPacketState.Row_Header;
            return false;
        }
 public abstract void Parse(MySqlStreamReader reader);
 bool Parse_PrepareOk_Content(MySqlStreamReader reader)
 {
     if (!reader.Ensure(_currentHeader.ContentLength))
     {
         return _needMoreData = true;
     }
     var okPrepare = new OkPrepareStmtPacket(_currentHeader);
     okPrepare.ParsePacketContent(reader);
     _okPrepare = okPrepare;
     //----------------------------------------------------
     _tableHeader = new TableHeader(true);
     //----------------------------------------------------
     //*** 3 possible way after read prepare ok header***
     if (okPrepare.num_params == 0)
     {
         //if prepare stmt dosn't have binding parameters
         if (okPrepare.num_columns > 0)
         {
             //has some column
             _parsingState = PrepareResponseParseState.ColumnField_Header;
         }
         else
         {
             _finalResult = new MySqlPrepareResponseResult(okPrepare, _tableHeader);
             _parsingState = PrepareResponseParseState.ShouldEnd;
             reader.Reset();
             return true; //finish
         }
     }
     else
     {
         _parsingState = PrepareResponseParseState.BindingField_Header;
     }
     return false;
 }
        bool Parse_Row_Header(MySqlStreamReader reader)
        {
            if (!reader.Ensure(PACKET_HEADER_LENGTH + 1))
            {
                return _needMoreData = true;
            }

            _currentHeader = reader.ReadPacketHeader();
            if (_currentHeader.ContentLength == Packet.MAX_PACKET_LENGTH)
            {
                //need more than 1 packet for row content 
                _parsingState = ResultPacketState.Row_Content;
                return false;
            }
            else if (_currentHeader.ContentLength > Packet.MAX_PACKET_LENGTH)
            {
                throw new NotSupportedException("???");
            }
            byte packetType = reader.PeekByte();
            switch (packetType)
            {
                case ERROR_CODE:
                    _parsingState = ResultPacketState.Error_Content;
                    break;
                case EOF_CODE://0x00 or 0xfe the OK packet header
                    _parsingState = ResultPacketState.Row_EOF;
                    break;
                default:
                    _parsingState = ResultPacketState.Row_Content;
                    break;
            }
            return false;
        }
        public override void ParsePacketContent(MySqlStreamReader r)
        {

            _fieldCount = r.ReadByte();
            _errno = r.U2();//2
            if (r.PeekByte() == 0x23)
            {
                _sqlStateMarker = r.ReadChar();
                _sqlState = r.ReadString(5);
            }

            message = r.ReadPacketTerminatedString();
#if DEBUG
            throw new Exception(_sqlStateMarker + _sqlState + " " + message);
#endif
        }
 //----------------------------------------------------
 bool Parse_ColumnField_Header(MySqlStreamReader reader)
 {
     if (!reader.Ensure(PACKET_HEADER_LENGTH + 1))
     {
         return _needMoreData = true;
     }
     _currentHeader = reader.ReadPacketHeader();
     byte type = reader.PeekByte();//1
     switch (type)
     {
         case ERROR_CODE:
             _parsingState = PrepareResponseParseState.Error_Content;
             break;
         case EOF_CODE:
             //?
             _parsingState = PrepareResponseParseState.ColumnField_EOF;
             break;
         default:
             _parsingState = PrepareResponseParseState.ColumnField_Content;
             break;
     }
     return false;
 }
 bool Parse_ColumnField_Content(MySqlStreamReader reader)
 {
     if (!reader.Ensure(_currentHeader.ContentLength))
     {
         return _needMoreData = true;
     }
     var field = new FieldPacket(_currentHeader, _isProtocol41);
     field.ParsePacketContent(reader);
     field.FieldIndex = _tableHeader.ColumnCount; //set this before  add to field list
     _tableHeader.AddField(field);
     //back to field header
     _parsingState = PrepareResponseParseState.ColumnField_Header;
     return false;
 }
        bool Parse_ColumnField_EOF(MySqlStreamReader reader)
        {
            if (!reader.Ensure(_currentHeader.ContentLength))
            {
                return _needMoreData = true;
            }

            var eofPacket = new EofPacket(_currentHeader, this._isProtocol41);
            eofPacket.ParsePacketContent(reader);
            //
            _finalResult = new MySqlPrepareResponseResult(_okPrepare, _tableHeader);
            _parsingState = PrepareResponseParseState.ShouldEnd;
            reader.Reset();
            return true;//finish
        }
 //----------------------------------------------------
 bool ParseErrorPacket(MySqlStreamReader reader)
 {
     if (!reader.Ensure(_currentHeader.ContentLength))
     {
         return _needMoreData = true;
     }
     var errPacket = new ErrPacket(_currentHeader);
     errPacket.ParsePacketContent(reader);
     // 
     _finalResult = new MySqlErrorResult(errPacket);
     _parsingState = PrepareResponseParseState.ShouldEnd;
     reader.Reset();
     return true;
 }
        void StoreBuffer(MySqlStreamReader reader, int length)
        {
            if (ms == null)
            {
                ms = new MemoryStream();
                //buffer data to this 
            }
            ms.Write(reader.ReadBuffer(length), 0, length);

            //reader.SkipForward(length);
            // throw new NotSupportedException();
            ////TODO: review buffer mx here ****
            //byte[] dataTemp = _mysqlStreamReader.ReadBuffer((int)length);
            //int existingLargeDataBufferLen = (largeDataBuffer == null) ?
            //    0 :
            //    largeDataBuffer.Length;
            //if (existingLargeDataBufferLen > 0)
            //{
            //    //merge ...
            //    byte[] newData = new byte[existingLargeDataBufferLen + dataTemp.Length];
            //    Buffer.BlockCopy(largeDataBuffer, 0, newData, 0, largeDataBuffer.Length);
            //    Buffer.BlockCopy(dataTemp, 0, newData, largeDataBuffer.Length, dataTemp.Length);
            //    largeDataBuffer = newData;
            //}
            //else
            //{
            //    largeDataBuffer = dataTemp;
            //}
        }
 bool Parse_PrepareOk_Header(MySqlStreamReader reader)
 {
     //this is first step ***
     if (!reader.Ensure(PACKET_HEADER_LENGTH + 1))
     {
         return _needMoreData = true;
     }
     _currentHeader = reader.ReadPacketHeader();
     byte type = reader.PeekByte();
     switch (type)
     {
         case ERROR_CODE:
             _parsingState = PrepareResponseParseState.Error_Content;
             break;
         case EOF_CODE:
         case OK_CODE:
             _parsingState = PrepareResponseParseState.OkPrepare_Content;
             break;
         default:
             throw new NotSupportedException("Packet type don't match!!");
     }
     return false;
 }
        bool Parse_Row_EOF(MySqlStreamReader reader)
        {
            if (!reader.Ensure(_currentHeader.ContentLength)) //check if length is enough to parse 
            {
                return _needMoreData = true;
            }
            //finish all of each row
            var eofPacket = new EofPacket(_currentHeader, this._isProtocol41);
            eofPacket.ParsePacketContent(reader);

            if (((eofPacket.serverStatus & (int)MySqlServerStatus.SERVER_MORE_RESULTS_EXISTS)) != 0)
            {
                var tableResult = new MySqlTableResult(_tableHeader, _rows);
                _rows = null;//reset

                //more than one result table
                //mu
                if (_currentMultResultSet != null)
                {
                    _currentMultResultSet.AddTableResult(tableResult);
                }
                else
                {
                    //first time 
                    _currentMultResultSet = new MySqlMultiTableResult();
                    _currentMultResultSet.AddTableResult(tableResult); ;
                    //not set _parseResult*** because this not finish
                }
                //--------------------
                //see: https://dev.mysql.com/doc/internals/en/multi-resultset.html
                //may has more than 1 result
                _parsingState = ResultPacketState.Header_Header;
                return false;
            }
            else
            {
                //after finish we create a result table 
                //the move rows into the table
                _parseResult = new MySqlTableResult(_tableHeader, _rows);
                //not link to the rows anymore
                _rows = null;
                _currentMultResultSet = null;
                _parsingState = ResultPacketState.ShouldEnd;//***  
                return true;//end
            }
        }
Esempio n. 39
0
        bool Parse_Row_Content(MySqlStreamReader reader)
        {
            if (!reader.Ensure(_currentHeader.ContentLength))
            {
                return(_needMoreData = true);
            }
            if (_currentHeader.ContentLength > Packet.MAX_PACKET_LENGTH)
            {
                throw new NotSupportedException("???");
            }
            else if (_currentHeader.ContentLength >= Packet.MAX_PACKET_LENGTH)
            {
                //can't complete in this round
                //so store data into temp extra large buffer
                //and set isLargeData= true
                StoreBuffer(reader, (int)_currentHeader.ContentLength);
                isLargeData = true;
                //we still in the row content state
                _parsingState = ResultPacketState.Row_Header;
                return(false);
            }
            //--------------------------------

            if (_generateResultMode)
            {
                //this is normal mode (opposite to JustFlushOutMode)
                //in this mode we parse packet content
                //and add it to the output rows
                //----------------------------------
                //in  this version row buffer len must < int.MaxLength
                if (_currentHeader.ContentLength > int.MaxValue)
                {
                    throw new NotSupportedException("not support this length");
                }
                //------------------------------------
                if (isLargeData)
                {
                    if (ms == null)
                    {                                      //it should not be null here
                        throw new NotSupportedException(); //?
                    }
                    ms.Write(reader.ReadBuffer((int)_currentHeader.ContentLength), 0,
                             (int)_currentHeader.ContentLength);
                    _rows.Add(new DataRowPacket(_currentHeader, ms.ToArray()));

#if NET20
                    ms.Close();
#endif
                    ms.Dispose();
                    ms = null;

                    isLargeData = false; //reset
                }
                else
                {
                    _rows.Add(new DataRowPacket(_currentHeader,
                                                reader.ReadBuffer((int)_currentHeader.ContentLength)));
                }
            }
            else
            {
                //just flush data***
                //not create data row
                if (_currentHeader.ContentLength > int.MaxValue)
                {
                    throw new Exception("not support content length> int.MaxValue");
                }
                reader.SkipForward((int)_currentHeader.ContentLength);
            }
            //-----------------------------------------------------------------------
            //after this row, next state = next row header
            _parsingState = ResultPacketState.Row_Header;
            return(false);
        }
        public override void ParsePacketContent(MySqlStreamReader r)
        {
            //we already have header ***
            protocolVersion = r.ReadByte();//1
            serverVertion = r.ReadNullTerminatedString();
            threadId = r.U4();//4
            scrambleBuff1 = r.ReadBuffer(8);
            filler1 = r.ReadByte();
            serverCapabilities1 = r.U2();//2
            serverLanguage = r.ReadByte();
            serverStatus = r.U2();//2
            protocol41 = (serverCapabilities1 & (1 << 9)) > 0;
            if (protocol41)
            {
                serverCapabilities2 = r.U2();
                scrambleLength = r.ReadByte();
                filler2 = r.ReadBuffer(10);
                scrambleBuff2 = r.ReadBuffer(12);
                filler3 = r.ReadByte();
            }
            else
            {
                filler2 = r.ReadBuffer(13);
            }

            if (r.ReadPosition == r.CurrentInputLength)
            {
                return;
            }

            pluginData = r.ReadPacketTerminatedString();
            var last = pluginData.Length - 1;
            if (pluginData[last] == '\0')
            {
                pluginData = pluginData.Substring(0, last);
            }
        }
Esempio n. 41
0
 public abstract void Parse(MySqlStreamReader reader);
        public override void ParsePacketContent(MySqlStreamReader r)
        {

            _status = r.ReadByte();//alway 0
            statement_id = r.ReadUnsigedNumber(4);
            num_columns = r.ReadUnsigedNumber(2);
            num_params = r.ReadUnsigedNumber(2);
            r.ReadFiller(1);//reserved_1
            waring_count = r.ReadUnsigedNumber(2);
        }
 /// <summary>
 /// return ***true*** if finish or need more data
 /// </summary>
 /// <param name="reader"></param>
 /// <returns></returns>
 bool StepParse(MySqlStreamReader reader)
 {
     switch (_parsingState)
     {
         case PrepareResponseParseState.OkPrepare_Header:
             return Parse_PrepareOk_Header(reader);
         case PrepareResponseParseState.OkPrepare_Content:
             return Parse_PrepareOk_Content(reader);
         //------------------------------------------------
         case PrepareResponseParseState.BindingField_Header:
             return Parse_BindingField_Header(reader);
         case PrepareResponseParseState.BindingField_Content:
             return Parse_BindingField_Content(reader);
         case PrepareResponseParseState.BindingField_EOF:
             return Parse_BindingField_EOF(reader);
         //------------------------------------------------
         case PrepareResponseParseState.ColumnField_Header:
             return Parse_ColumnField_Header(reader);
         case PrepareResponseParseState.ColumnField_Content:
             return Parse_ColumnField_Content(reader);
         case PrepareResponseParseState.ColumnField_EOF:
             return Parse_ColumnField_EOF(reader);
         //------------------------------------------------
         case PrepareResponseParseState.ShouldEnd:
             reader.Reset();
             return true;
         case PrepareResponseParseState.Error_Content:
             return ParseErrorPacket(reader);
         default:
             throw new Exception("unknown step?");
     }
 }
 public override void ParsePacketContent(MySqlStreamReader r)
 {
     //we 've set _rowDataBuffer from ctor
     throw new NotSupportedException();
 }
 public override void Parse(MySqlStreamReader reader)
 {
     _finalResult = null;
     while (!StepParse(reader)) ;
     //StepParse() return true if 
     //1. need more data or
     //2. finish
 }
        public override void ParsePacketContent(MySqlStreamReader r)
        {

            _QUERY_CMD = r.ReadByte();//1
            _sql = r.ReadPacketTerminatedString();
        }
 public override void Parse(MySqlStreamReader reader)
 {
     //reset final result 
     _parseResult = null;
     while (!StepParse(reader)) ;
     //StepParse() return true if 
     //1. need more data or
     //2. finish
     if (_needMoreData)
     {
         //at any state if need more buffer 
         //then stop parsing and return 
         if (_supportPartialRelease)
         {
             if (_rows != null && _rows.Count > 0)
             {
                 _parseResult = new MySqlTableResult(_tableHeader, _rows) { HasFollower = true };
             }
             if (_generateResultMode)
             {
                 //generate new result
                 _rows = new List<DataRowPacket>();
             }
         }
     }
     else if (_parsingState == ResultPacketState.ShouldEnd)
     {
         //reset
         reader.Reset();
         _parsingState = ResultPacketState.Header_Header;
     }
 }
 /// <summary>
 /// parse only body part, please ensure content length before parse
 /// </summary>
 /// <param name="r"></param>
 public abstract void ParsePacketContent(MySqlStreamReader r);
 bool Parse_Error_Content(MySqlStreamReader reader)
 {
     if (!reader.Ensure(_currentHeader.ContentLength)) //check if length is enough to parse 
     {
         return _needMoreData = true;
     }
     var errPacket = new ErrPacket(_currentHeader);
     errPacket.ParsePacketContent(reader);
     //------------------------
     _parseResult = new MySqlErrorResult(errPacket);
     _parsingState = ResultPacketState.ShouldEnd;
     return true;//finished
 }
 public override void ParsePacketContent(MySqlStreamReader r)
 {
     fieldCount = r.ReadByte();
     if (protocol41)
     {
         warningCount = r.U2();//2
         serverStatus = r.U2();//2
     }
 }
 public override void Parse(MySqlStreamReader reader)
 {
     _finalResult = null;
     //1.create connection frame  
     //_writer.Reset();        
     PacketHeader header = reader.ReadPacketHeader();
     _handshake = new HandshakePacket(header);
     //check if  
     _handshake.ParsePacketContent(reader);
     _finalResult = new MySqlHandshakeResult(_handshake);
 }
        public override void ParsePacketContent(MySqlStreamReader r)
        {

            if (protocol41)
            {
                catalog = r.ReadLengthCodedString();//3,100,101,102,103  (always "def")
                schema = r.ReadLengthCodedString();
                table = r.ReadLengthCodedString();
                orgTable = r.ReadLengthCodedString();
                name = r.ReadLengthCodedString();
                orgName = r.ReadLengthCodedString();

                //next_length (lenenc_int) -- length of the following fields (always 0x0c)  ***
                uint lengthCodedNumber = r.ReadLengthCodedNumber();
                if (lengthCodedNumber != 0x0c)
                {

                    //var err  = new TypeError('Received invalid field length');
                    //err.code = 'PARSER_INVALID_FIELD_LENGTH';
                    //throw err;
#if DEBUG
                    if (lengthCodedNumber == 0)
                    {
                        //error
                        //this package is error packet 
                        //server may send the correct one?
                        dbugFailure = true;
                        return;
                    }
#endif
                    throw new Exception("Received invalid field length");
                }

                charsetNr = r.U2();//2
                maxLengthOfField = r.U4();//4
                columnType = r.ReadByte();//1
                flags = r.U2();//2
                maxShownDecimalDigits = r.ReadByte();
                filler = r.ReadBuffer(2);
                if (filler[0] != 0x0 || filler[1] != 0x0)
                {
                    //var err  = new TypeError('Received invalid filler');
                    //err.code = 'PARSER_INVALID_FILLER';
                    //throw err;
                    throw new Exception("Received invalid filler");
                }
                // parsed flags
                //this.zeroFill = (this.flags & 0x0040 ? true : false);
                zeroFill = ((flags & 0x0040) == 0x0040 ? true : false);
                if (r.ReachedPacketEnd())
                {
                    return;
                }
                //----
                //if command was COM_FIELD_LIST {
                //   lenenc_int length of default- values
                //string[$len]   default values
                //}
                strDefault = r.ReadLengthCodedString();
            }
            else
            {
                table = r.ReadLengthCodedString();
                name = r.ReadLengthCodedString();
                maxLengthOfField = r.ReadUnsigedNumber(r.ReadByte());
                columnType = (int)r.ReadUnsigedNumber(r.ReadByte());
            }
        }
 bool Parse_Field_EOF(MySqlStreamReader reader)
 {
     if (!reader.Ensure(_currentHeader.ContentLength)) //check if length is enough to parse 
     {
         return _needMoreData = true;
     }
     var eofPacket = new EofPacket(_currentHeader, this._isProtocol41);
     eofPacket.ParsePacketContent(reader);
     _parsingState = ResultPacketState.Row_Header;
     return false;
 }