Beispiel #1
0
        public override void ParsePacket(PacketParser parser)
        {
            ParsePacketHeader(parser);

            _fieldCount = parser.ParseByte();
            _errno      = parser.ParseUnsigned2();//2

            if (parser.Peak() == 0x23)
            {
                _sqlStateMarker = parser.ParseChar();
                _sqlState       = parser.ParseString(5);
            }

            message = parser.ParsePacketTerminatedString();
#if DEBUG
            throw new Exception(_sqlStateMarker + _sqlState + " " + message);
#endif
        }
Beispiel #2
0
        public override void ParsePacket(PacketParser parser)
        {
            ParsePacketHeader(parser);
            _fieldCount = parser.ParseLengthCodedNumber();

            if (parser.ReachedPacketEnd())
            {
                return;
            }

            if (_fieldCount == 0)
            {
                _extraStr = parser.ParsePacketTerminatedString();
            }
            else
            {
                _extraNumber = parser.ParseLengthCodedNumber();
                _extraStr    = parser.ParsePacketTerminatedString();//null;
            }
        }
Beispiel #3
0
        public override void ParsePacket(PacketParser parser)
        {
            ParsePacketHeader(parser);

            protocolVersion     = parser.ParseUnsigned1(); //1
            serverVertion       = parser.ParseNullTerminatedString();
            threadId            = parser.ParseUnsigned4(); //4
            scrambleBuff1       = parser.ParseBuffer(8);
            filler1             = parser.ParseByte();
            serverCapabilities1 = parser.ParseUnsigned2(); //2
            serverLanguage      = parser.ParseByte();
            serverStatus        = parser.ParseUnsigned2(); //2

            protocol41 = (serverCapabilities1 & (1 << 9)) > 0;
            if (protocol41)
            {
                serverCapabilities2 = parser.ParseUnsigned2();
                scrambleLength      = parser.ParseByte();
                filler2             = parser.ParseBuffer(10);

                scrambleBuff2 = parser.ParseBuffer(12);
                filler3       = parser.ParseByte();
            }
            else
            {
                filler2 = parser.ParseBuffer(13);
            }

            if (parser.Position == parser.Length)
            {
                return;
            }

            pluginData = parser.ParsePacketTerminatedString();
            var last = pluginData.Length - 1;

            if (pluginData[last] == '\0')
            {
                pluginData = pluginData.Substring(0, last);
            }
        }
Beispiel #4
0
        public override void ParsePacket(PacketParser parser)
        {
            ParsePacketHeader(parser);

            _fieldCount  = parser.ParseUnsigned1();
            affectedRows = parser.ParseLengthCodedNumber();
            insertId     = parser.ParseLengthCodedNumber();

            if (_protocol41)
            {
                _serverStatus = parser.ParseUnsigned2();
                _warningCount = parser.ParseUnsigned2();
            }
            _message = parser.ParsePacketTerminatedString();
            //var m = this.message.match(/\schanged:\s * (\d +) / i);

            //if (m !== null)
            //{
            //    this.changedRows = parseInt(m[1], 10);
            //}
        }
Beispiel #5
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();
            }
        }
Beispiel #6
0
        const int MAX_PACKET_LENGTH   = (1 << 24) - 1;//(int)Math.Pow(2, 24) - 1;


        public Query(Connection conn, string sql, CommandParams cmdParams)//testing
        {
            if (sql == null)
            {
                throw new Exception("Sql command can not null.");
            }
            this._conn = conn;

            this._cmdParams = cmdParams;

            typeCast   = conn.config.typeCast;
            nestTables = false;

            //index = 0;
            LoadError = null;

            //*** query use conn resource such as parser,writer
            //so 1 query 1 connection
            _parser        = conn.PacketParser;
            _writer        = conn.PacketWriter;
            _receiveBuffer = null;

            _sqlStrTemplate = new SqlStringTemplate(sql);
        }
Beispiel #7
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;
            }
        }
Beispiel #8
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;
                    //    }
                    //  }
                    //}
                }
            }
        }
Beispiel #9
0
 public abstract void ParsePacket(PacketParser parser);
Beispiel #10
0
 public override void ParsePacket(PacketParser parser)
 {
     throw new NotImplementedException();
 }
Beispiel #11
0
 //const byte QUIT_CMD = (byte)Command.QUIT;//0x01
 public override void ParsePacket(PacketParser parser)
 {
     throw new NotImplementedException();
     //ParsePacketHeader(parser);
     //this.command = parser.ParseByte();
 }
Beispiel #12
0
 public override void ParsePacket(PacketParser parser)
 {
     ParsePacketHeader(parser);
     _QUERY_CMD = parser.ParseUnsigned1();//1
     _sql       = parser.ParsePacketTerminatedString();
 }
Beispiel #13
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;
            }
        }