Exemplo n.º 1
0
        public override void ParsePacket(PacketParser parser)
        {
            ParsePacketHeader(parser);
            if (protocol41)
            {
                catalog  = parser.ParseLengthCodedString();
                db       = parser.ParseLengthCodedString();
                table    = parser.ParseLengthCodedString();
                orgTable = parser.ParseLengthCodedString();
                name     = parser.ParseLengthCodedString();
                orgName  = parser.ParseLengthCodedString();

                if (parser.ParseLengthCodedNumber() != 0x0c)
                {
                    //var err  = new TypeError('Received invalid field length');
                    //err.code = 'PARSER_INVALID_FIELD_LENGTH';
                    //throw err;
                    throw new Exception("Received invalid field length");
                }

                charsetNr = parser.ParseUnsigned2(); //2
                length    = parser.ParseUnsigned4(); //4
                type      = parser.ParseByte();
                flags     = parser.ParseUnsigned2(); //2
                decimals  = parser.ParseByte();

                filler = parser.ParseBuffer(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 (parser.reachedPacketEnd()) {
                //      return;
                //    }
                if (parser.ReachedPacketEnd())
                {
                    return;
                }
                strDefault = parser.ParseLengthCodedString();
            }
            else
            {
                table  = parser.ParseLengthCodedString();
                name   = parser.ParseLengthCodedString();
                length = parser.ParseUnsignedNumber(parser.ParseByte());
                type   = (int)parser.ParseUnsignedNumber(parser.ParseByte());
            }
        }
Exemplo n.º 2
0
        public override void ParsePacket(PacketParser parser)
        {
            ParsePacketHeader(parser);
            if (protocol41)
            {
                clientFlags   = parser.ParseUnsigned4(); //4
                maxPacketSize = parser.ParseUnsigned4(); //4
                charsetNumber = parser.ParseByte();

                parser.ParseFiller(23);
                user         = parser.ParseNullTerminatedString();
                scrambleBuff = parser.ParseLengthCodedBuffer();
                database     = parser.ParseNullTerminatedString();
            }
            else
            {
                clientFlags   = parser.ParseUnsigned2(); //2
                maxPacketSize = parser.ParseUnsigned3(); //3
                user          = parser.ParseNullTerminatedString();
                scrambleBuff  = parser.ParseBuffer(8);
                database      = parser.ParseLengthCodedString();
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// read a data cell with type cast
        /// </summary>
        /// <param name="parser"></param>
        /// <param name="fieldPacket"></param>
        /// <param name="data"></param>
        void ReadCellWithTypeCast(PacketParser parser, FieldPacket fieldPacket, ref MyStructData data)
        {
            string numberString;

            Types type = (Types)fieldPacket.type;

            switch (type)
            {
            case Types.TIMESTAMP:
            case Types.DATE:
            case Types.DATETIME:
            case Types.NEWDATE:

                _stbuilder.Length = 0;    //clear
                string dateString = parser.ParseLengthCodedString();
                data.myString = dateString;

                if (_config.dateStrings)
                {
                    //return new FieldData<string>(type, dateString);
                    //data.myString = dateString;
                    data.type = type;
                    return;
                }

                if (dateString == null)
                {
                    data.type = Types.NULL;
                    return;
                }

                //    var originalString = dateString;
                //    if (field.type === Types.DATE) {
                //      dateString += ' 00:00:00';
                //    }
                _stbuilder.Append(dateString);
                //string originalString = dateString;
                if (fieldPacket.type == (int)Types.DATE)
                {
                    _stbuilder.Append(" 00:00:00");
                }
                //    if (timeZone !== 'local') {
                //      dateString += ' ' + timeZone;
                //    }

                if (!_isLocalTimeZone)
                {
                    _stbuilder.Append(' ' + _config.timezone);
                }
                //var dt;
                //    dt = new Date(dateString);
                //    if (isNaN(dt.getTime())) {
                //      return originalString;
                //    }

                data.myDateTime = DateTime.Parse(_stbuilder.ToString());
                data.type       = type;
                return;

            case Types.TINY:
            case Types.SHORT:
            case Types.LONG:
            case Types.INT24:
            case Types.YEAR:

                //TODO: review here,
                data.myString = numberString = parser.ParseLengthCodedString();
                if (numberString == null || (fieldPacket.zeroFill && numberString[0] == '0') || numberString.Length == 0)
                {
                    data.type = Types.NULL;
                }
                else
                {
                    data.myInt32 = Convert.ToInt32(numberString);
                    data.type    = type;
                }
                return;

            case Types.FLOAT:
            case Types.DOUBLE:
                data.myString = numberString = parser.ParseLengthCodedString();
                if (numberString == null || (fieldPacket.zeroFill && numberString[0] == '0'))
                {
                    data.myString = numberString;
                    data.type     = Types.NULL;
                }
                else
                {
                    data.myDouble = Convert.ToDouble(numberString);
                    data.type     = type;
                }
                return;

            //    return (numberString === null || (field.zeroFill && numberString[0] == "0"))
            //      ? numberString : Number(numberString);
            case Types.NEWDECIMAL:
            case Types.LONGLONG:
                //    numberString = parser.parseLengthCodedString();
                //    return (numberString === null || (field.zeroFill && numberString[0] == "0"))
                //      ? numberString
                //      : ((supportBigNumbers && (bigNumberStrings || (Number(numberString) > IEEE_754_BINARY_64_PRECISION)))
                //        ? numberString
                //        : Number(numberString));

                data.myString = numberString = parser.ParseLengthCodedString();

                if (numberString == null || (fieldPacket.zeroFill && numberString[0] == '0'))
                {
                    data.myString = numberString;
                    data.type     = Types.NULL;
                }
                else if (_config.supportBigNumbers && (_config.bigNumberStrings || (Convert.ToInt64(numberString) > IEEE_754_BINARY_64_PRECISION)))
                {
                    //store as string ?
                    //TODO: review here  again
                    data.myString = numberString;
                    data.type     = type;
                    throw new NotSupportedException();
                }
                else if (type == Types.LONGLONG)
                {
                    data.myInt64 = Convert.ToInt64(numberString);
                    data.type    = type;
                }
                else    //decimal
                {
                    data.myDecimal = Convert.ToDecimal(numberString);
                    data.type      = type;
                }
                return;

            case Types.BIT:

                data.myBuffer = parser.ParseLengthCodedBuffer();
                data.type     = type;
                return;

            //    return parser.parseLengthCodedBuffer();
            case Types.STRING:
            case Types.VAR_STRING:
            case Types.TINY_BLOB:
            case Types.MEDIUM_BLOB:
            case Types.LONG_BLOB:
            case Types.BLOB:
                if (fieldPacket.charsetNr == (int)CharSets.BINARY)
                {
                    data.myBuffer = parser.ParseLengthCodedBuffer();     //CodedBuffer
                    data.type     = type;
                }
                else
                {
                    data.myString = parser.ParseLengthCodedString();    //codeString
                    data.type     = type;
                }
                return;

            //    return (field.charsetNr === Charsets.BINARY)
            //      ? parser.parseLengthCodedBuffer()
            //      : parser.parseLengthCodedString();
            case Types.GEOMETRY:
                //TODO: unfinished
                data.type = Types.GEOMETRY;
                return;

            default:
                data.myString = parser.ParseLengthCodedString();
                data.type     = type;
                return;
            }
        }
Exemplo n.º 4
0
        public override void ParsePacket(PacketParser parser)
        {
            //function parse(parser, fieldPackets, typeCast, nestTables, connection) {
            //  var self = this;
            //  var next = function () {
            //    return self._typeCast(fieldPacket, parser, connection.config.timezone, connection.config.supportBigNumbers, connection.config.bigNumberStrings, connection.config.dateStrings);
            //  };

            //  for (var i = 0; i < fieldPackets.length; i++) {
            //    var fieldPacket = fieldPackets[i];
            //    var value;


            //---------------------------------------------
            //danger!
            //please note that  ***
            //data in each slot is not completely cleared

            //because we don't want to copy entire MyStructData back and forth
            //we just replace some part of it ***
            //---------------------------------------------


            ParsePacketHeader(parser);

            var  fieldInfos = _tableHeader.GetFields();
            int  j          = _tableHeader.ColumnCount;
            bool typeCast   = _tableHeader.TypeCast;
            bool nestTables = _tableHeader.NestTables;

            if (!nestTables && typeCast)
            {
                for (int i = 0; i < j; i++)
                {
                    ReadCellWithTypeCast(parser, fieldInfos[i], ref _myDataList[i]);
                }
            }
            else
            {
                //may be nestTables or type cast
                //

                for (int i = 0; i < j; i++)
                {
                    // MyStructData value;
                    if (typeCast)
                    {
                        ReadCellWithTypeCast(parser, fieldInfos[i], ref _myDataList[i]);
                    }
                    else if (fieldInfos[i].charsetNr == (int)CharSets.BINARY)
                    {
                        _myDataList[i].myBuffer = parser.ParseLengthCodedBuffer();
                        _myDataList[i].type     = (Types)fieldInfos[i].type;

                        //value = new MyStructData();
                        //value.myBuffer = parser.ParseLengthCodedBuffer();
                        //value.type = (Types)fieldInfos[i].type;
                    }
                    else
                    {
                        _myDataList[i].myString = parser.ParseLengthCodedString();
                        _myDataList[i].type     = (Types)fieldInfos[i].type;

                        //value = new MyStructData();
                        //value.myString = parser.ParseLengthCodedString();
                        //value.type = (Types)fieldInfos[i].type;
                    }
                    //    if (typeof typeCast == "function") {
                    //      value = typeCast.apply(connection, [ new Field({ packet: fieldPacket, parser: parser }), next ]);
                    //    } else {
                    //      value = (typeCast)
                    //        ? this._typeCast(fieldPacket, parser, connection.config.timezone, connection.config.supportBigNumbers, connection.config.bigNumberStrings, connection.config.dateStrings)
                    //        : ( (fieldPacket.charsetNr === Charsets.BINARY)
                    //          ? parser.parseLengthCodedBuffer()
                    //          : parser.parseLengthCodedString() );
                    //    }


                    //TODO: review here
                    //nestTables=?


                    //if (nestTables)
                    //{
                    //    //      this[fieldPacket.table] = this[fieldPacket.table] || {};
                    //    //      this[fieldPacket.table][fieldPacket.name] = value;
                    //}
                    //else
                    //{
                    //    //      this[fieldPacket.name] = value;
                    //    myDataList[i] = value;
                    //}


                    //    if (typeof nestTables == "string" && nestTables.length) {
                    //      this[fieldPacket.table + nestTables + fieldPacket.name] = value;
                    //    } else if (nestTables) {
                    //      this[fieldPacket.table] = this[fieldPacket.table] || {};
                    //      this[fieldPacket.table][fieldPacket.name] = value;
                    //    } else {
                    //      this[fieldPacket.name] = value;
                    //    }
                    //  }
                    //}
                }
            }
        }
Exemplo n.º 5
0
        void ParseValues(PacketParser parser, FieldPacket fieldInfo, ref MyStructData myData)
        {
            Types fieldType = (Types)fieldInfo.type;

            switch (fieldType)
            {
            case Types.TIMESTAMP: //
            case Types.DATE:      //
            case Types.DATETIME:  //
            case Types.NEWDATE:   //
                myData.myDateTime = parser.ParseLengthCodedDateTime();
                myData.type       = fieldType;
                break;

            case Types.TINY:    //length = 1;
                myData.myInt32 = parser.ParseUnsigned1();
                myData.type    = fieldType;
                break;

            case Types.SHORT:   //length = 2;
            case Types.YEAR:    //length = 2;
                myData.myInt32 = (int)parser.ParseUnsigned2();
                myData.type    = fieldType;
                break;

            case Types.INT24:
            case Types.LONG:    //length = 4;
                myData.myInt32 = (int)parser.ParseUnsigned4();
                myData.type    = fieldType;
                break;

            case Types.FLOAT:
                myData.myDouble = parser.ParseFloat();
                myData.type     = fieldType;
                break;

            case Types.DOUBLE:
                myData.myDouble = parser.ParseDouble();
                myData.type     = fieldType;
                break;

            case Types.NEWDECIMAL:
                myData.myDecimal = parser.ParseDecimal();
                myData.type      = fieldType;
                break;

            case Types.LONGLONG:
                myData.myInt64 = parser.ParseInt64();
                myData.type    = fieldType;
                break;

            case Types.STRING:
            case Types.VARCHAR:
            case Types.VAR_STRING:
                myData.myString = parser.ParseLengthCodedString();
                myData.type     = fieldType;
                break;

            case Types.TINY_BLOB:
            case Types.MEDIUM_BLOB:
            case Types.LONG_BLOB:
            case Types.BLOB:
            case Types.BIT:
                myData.myBuffer = parser.ParseLengthCodedBuffer();
                myData.type     = fieldType;
                break;

            case Types.GEOMETRY:

            default:
                myData.myBuffer = parser.ParseLengthCodedBuffer();
                myData.type     = Types.NULL;
                break;
            }
        }