public void AddWithValue(string key, long value)
 {
     var data = new MyStructData();
     data.myInt64 = value;
     data.type = MySqlDataType.LONGLONG;
     _values[key] = data;
 }
 public void AddWithValue(string key, short value)
 {
     var data = new MyStructData();
     data.myInt32 = value;
     data.type = MySqlDataType.SHORT;
     _values[key] = data;
 }
 public void AddWithValue(string key, float value)
 {
     var data = new MyStructData();
     data.myDouble = value;
     data.type = MySqlDataType.FLOAT;
     _values[key] = data;
 }
 public void AddWithValue(string key, decimal value)
 {
     var data = new MyStructData();
     data.myString = value.ToString();
     data.type = MySqlDataType.STRING;
     _values[key] = data;
 }
 public void AddWithValue(string key, double value)
 {
     var data = new MyStructData();
     data.myDouble = value;
     data.type = MySqlDataType.DOUBLE;
     _values[key] = data;
 }
 public void AddWithValue(string key, byte value)
 {
     var data = new MyStructData();
     data.myString = value.ToString();
     data.type = MySqlDataType.STRING;
     //data.myInt32 = value;
     //data.type = Types.TINY;
     _values[key] = data;
 }
        public void AddWithValue(string key, int value)
        {
            //INT 4       min        max
            //signed -2147483648 2147483647
            //unsigned     0     4294967295
            //---------------------------

            var data = new MyStructData();
            data.myInt32 = value;
            data.type = MySqlDataType.LONG;//Types.LONG = int32
            _values[key] = data;
        }
Exemplo n.º 8
0
        static void WriteValueByType(PacketWriter writer, ref MyStructData dataTemp)
        {
            switch (dataTemp.type)
            {
            case Types.VARCHAR:
            case Types.VAR_STRING:
            case Types.STRING:
                writer.WriteLengthCodedString(dataTemp.myString);
                break;

            case Types.TINY:
                writer.WriteUnsignedNumber(1, dataTemp.myUInt32);
                break;

            case Types.SHORT:
                var a = dataTemp.myInt32;
                writer.WriteUnsignedNumber(2, dataTemp.myUInt32);
                break;

            case Types.LONG:
                //writer.WriteUnsignedNumber(4, (uint)dataTemp.myInt32);
                writer.WriteUnsignedNumber(4, dataTemp.myUInt32);
                break;

            case Types.LONGLONG:
                writer.WriteInt64(dataTemp.myInt64);
                break;

            case Types.FLOAT:
                writer.WriteFloat((float)dataTemp.myDouble);
                break;

            case Types.DOUBLE:
                writer.WriteDouble(dataTemp.myDouble);
                break;

            case Types.BIT:
            case Types.BLOB:
            case Types.MEDIUM_BLOB:
            case Types.LONG_BLOB:
                writer.WriteLengthCodedBuffer(dataTemp.myBuffer);
                break;

            default:
                //TODO: review here
                throw new NotSupportedException();
                //writer.WriteLengthCodedNull();
            }
        }
 public void AddWithValue(string key, string value)
 {
     var data = new MyStructData();
     if (value != null)
     {
         data.myString = value;
         data.type = MySqlDataType.VAR_STRING;
     }
     else
     {
         data.myString = null;
         data.type = MySqlDataType.NULL;
     }
     _values[key] = data;
 }
Exemplo n.º 10
0
        void WriteValueByType(PacketWriter writer, MyStructData dataTemp)
        {
            switch (dataTemp.type)
            {
            case Types.VARCHAR:
            case Types.VAR_STRING:
            case Types.STRING:
                writer.WriteLengthCodedString(dataTemp.myString);
                break;

            case Types.LONG:
                writer.WriteUnsignedNumber(4, (uint)dataTemp.myInt32);
                break;

            case Types.LONGLONG:
                writer.WriteInt64(dataTemp.myInt64);
                break;

            case Types.FLOAT:
                writer.WriteFloat((float)dataTemp.myDouble);
                break;

            case Types.DOUBLE:
                writer.WriteDouble(dataTemp.myDouble);
                break;

            case Types.BIT:
            case Types.BLOB:
            case Types.MEDIUM_BLOB:
            case Types.LONG_BLOB:
                writer.WriteLengthCodedBuffer(dataTemp.myBuffer);
                break;

            default:
                writer.WriteLengthCodedNull();
                break;
            }
        }
 public void AddWithValue(string key, char value)
 {
     //1 unicode char => 2 bytes store
     var data = new MyStructData();
     data.myUInt32 = value;
     data.type = MySqlDataType.LONGLONG;
     _values[key] = data;
 }
        MyStructData ReadCurrentRowTextProtocol(MySqlFieldDefinition f)
        {

            BufferReader r = this.bufferReader;
            MyStructData data = new MyStructData();
            MySqlDataType type = (MySqlDataType)f.FieldType;
            string numberString = null;
            switch (type)
            {

                case MySqlDataType.TIMESTAMP:
                case MySqlDataType.DATE:
                case MySqlDataType.DATETIME:
                case MySqlDataType.NEWDATE:
                    {

                        QueryParsingConfig qparsingConfig = _queryParsingConf;
                        tmpStringBuilder.Length = 0;//clear 
                        data.myString = r.ReadLengthCodedString(this.StringConverter);
                        data.type = type;
                        if (data.myString == null)
                        {
                            data.type = MySqlDataType.NULL;
                            return data;
                        }
                        if (qparsingConfig.DateStrings)
                        {
                            return data;
                        }

                        //-------------------------------------------------------------
                        //    var originalString = dateString;
                        //    if (field.type === Types.DATE) {
                        //      dateString += ' 00:00:00';
                        //    }
                        tmpStringBuilder.Append(data.myString);
                        //string originalString = dateString;
                        if (type == MySqlDataType.DATE)
                        {
                            tmpStringBuilder.Append(" 00:00:00");
                        }
                        //    if (timeZone !== 'local') {
                        //      dateString += ' ' + timeZone;
                        //    }

                        if (!qparsingConfig.UseLocalTimeZone)
                        {
                            tmpStringBuilder.Append(' ' + qparsingConfig.TimeZone);
                        }
                        //var dt;
                        //    dt = new Date(dateString);
                        //    if (isNaN(dt.getTime())) {
                        //      return originalString;
                        //    }

                        data.myDateTime = DateTime.Parse(tmpStringBuilder.ToString(),
                            System.Globalization.CultureInfo.InvariantCulture);
                        data.type = type;
                        tmpStringBuilder.Length = 0;//clear 
                    }
                    return data;
                case MySqlDataType.TINY:
                case MySqlDataType.SHORT:
                case MySqlDataType.LONG:
                case MySqlDataType.INT24:
                case MySqlDataType.YEAR:

                    //TODO: review here,                    
                    data.myString = numberString = r.ReadLengthCodedString(this.StringConverter);
                    if (numberString == null ||
                        (f.IsZeroFill && numberString[0] == '0') ||
                        numberString.Length == 0)
                    {
                        data.type = MySqlDataType.NULL;
                    }
                    else
                    {
                        data.myInt32 = Convert.ToInt32(numberString);
                        data.type = type;
                    }
                    return data;
                case MySqlDataType.FLOAT:
                case MySqlDataType.DOUBLE:
                    data.myString = numberString = r.ReadLengthCodedString(this.StringConverter);
                    if (numberString == null || (f.IsZeroFill && numberString[0] == '0'))
                    {
                        data.type = MySqlDataType.NULL;
                    }
                    else
                    {
                        data.myDouble = Convert.ToDouble(numberString);
                        data.type = type;
                    }
                    return data;
                //    return (numberString === null || (field.zeroFill && numberString[0] == "0"))
                //      ? numberString : Number(numberString);
                case MySqlDataType.NEWDECIMAL:
                case MySqlDataType.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));

                    QueryParsingConfig config = _queryParsingConf;
                    data.myString = numberString = r.ReadLengthCodedString(this.StringConverter);
                    if (numberString == null || (f.IsZeroFill && numberString[0] == '0'))
                    {
                        data.type = MySqlDataType.NULL;
                    }
                    else if (config.SupportBigNumbers &&
                        (config.BigNumberStrings || (Convert.ToInt64(numberString) > Packet.IEEE_754_BINARY_64_PRECISION)))
                    {
                        //store as string ?
                        //TODO: review here  again
                        data.myString = numberString;
                        data.type = type;
                        throw new NotSupportedException();
                    }
                    else if (type == MySqlDataType.LONGLONG)
                    {
                        data.myInt64 = Convert.ToInt64(numberString);
                        data.type = type;
                    }
                    else//decimal
                    {
                        data.myDecimal = Convert.ToDecimal(numberString);
                        data.type = type;
                    }
                    return data;
                case MySqlDataType.BIT:

                    data.myBuffer = r.ReadLengthCodedBuffer();
                    data.type = type;
                    return data;
                //    return parser.parseLengthCodedBuffer();
                case MySqlDataType.STRING:
                case MySqlDataType.VAR_STRING:

                case MySqlDataType.TINY_BLOB:
                case MySqlDataType.MEDIUM_BLOB:
                case MySqlDataType.LONG_BLOB:
                case MySqlDataType.BLOB:
                    if (f.MarkedAsBinary)
                    {
                        data.myBuffer = r.ReadLengthCodedBuffer();
                        data.type = (data.myBuffer != null) ? type : MySqlDataType.NULL;
                    }
                    else
                    {
                        data.myString = r.ReadLengthCodedString(this.StringConverter);
                        data.type = (data.myString != null) ? type : MySqlDataType.NULL;
                    }
                    return data;
                //    return (field.charsetNr === Charsets.BINARY)
                //      ? parser.parseLengthCodedBuffer()
                //      : parser.parseLengthCodedString();
                case MySqlDataType.GEOMETRY:
                    //TODO: unfinished
                    data.type = MySqlDataType.GEOMETRY;
                    return data;
                default:
                    data.myString = r.ReadLengthCodedString(this.StringConverter);
                    data.type = type;
                    return data;
            }
        }
Exemplo n.º 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;
            }
        }
Exemplo n.º 14
0
 public ComStmtSendLongData(uint statement_id, int param_id, MyStructData data)
 {
     _statement_id = statement_id;
     _param_id     = param_id;
     _data         = data;
 }
 MyStructData ReadCurrentRowBinaryProtocol(MySqlFieldDefinition f)
 {
     MySqlDataType fieldType = (MySqlDataType)f.FieldType;
     MyStructData myData = new MyStructData();
     BufferReader r = this.bufferReader;
     switch (fieldType)
     {
         case MySqlDataType.TIMESTAMP://
         case MySqlDataType.DATE://
         case MySqlDataType.DATETIME://
         case MySqlDataType.NEWDATE://
             r.ReadLengthCodedDateTime(out myData.myDateTime);
             myData.type = fieldType;
             return myData;
         case MySqlDataType.TINY://length = 1;
             myData.myInt32 = r.U1();
             myData.type = fieldType;
             return myData;
         case MySqlDataType.SHORT://length = 2;
         case MySqlDataType.YEAR://length = 2;
             myData.myInt32 = (int)r.U2();
             myData.type = fieldType;
             return myData;
         case MySqlDataType.INT24:
         case MySqlDataType.LONG://length = 4;
             myData.myInt32 = (int)r.U4();
             myData.type = fieldType;
             return myData;
         case MySqlDataType.FLOAT:
             myData.myDouble = r.ReadFloat();
             myData.type = fieldType;
             return myData;
         case MySqlDataType.DOUBLE:
             myData.myDouble = r.ReadDouble();
             myData.type = fieldType;
             return myData;
         case MySqlDataType.NEWDECIMAL:
             myData.myDecimal = r.ReadDecimal();
             myData.type = fieldType;
             return myData;
         case MySqlDataType.LONGLONG:
             myData.myInt64 = r.ReadInt64();
             myData.type = fieldType;
             return myData;
         case MySqlDataType.STRING:
         case MySqlDataType.VARCHAR:
         case MySqlDataType.VAR_STRING:
             myData.myString = r.ReadLengthCodedString(this.StringConverter);
             myData.type = fieldType;
             return myData;
         case MySqlDataType.TINY_BLOB:
         case MySqlDataType.MEDIUM_BLOB:
         case MySqlDataType.LONG_BLOB:
         case MySqlDataType.BLOB:
         case MySqlDataType.BIT:
             myData.myBuffer = r.ReadLengthCodedBuffer();
             myData.type = fieldType;
             return myData;
         case MySqlDataType.GEOMETRY:
             throw new NotSupportedException();
         default:
             myData.myBuffer = r.ReadLengthCodedBuffer();
             myData.type = MySqlDataType.NULL;
             return myData;
     }
 }
Exemplo n.º 16
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.º 17
0
 public ComStmtSendLongDataPacket(PacketHeader header, uint statement_id, int param_id, MyStructData data)
     : base(header)
 {
     _statement_id = statement_id;
     _param_id = param_id;
     _data = data;
 }
Exemplo n.º 18
0
        //----------------------------
        static bool WriteValueByType(MySqlStreamWriter writer,
            ref MyStructData dataTemp,
            ref TempSingleValueHolder singleValueHolder)
        {
            int availableContentSpace = Packet.MAX_PACKET_LENGTH - (int)writer.OnlyPacketContentLength;
            if (singleValueHolder.round == 0)
            {
                //---------------------------
                //this is first round *** 
                //---------------------------
                switch (dataTemp.type)
                {
                    case MySqlDataType.NULL:
                        //we not write null data
                        return true;
                    case MySqlDataType.BIT:
                    case MySqlDataType.BLOB:
                    case MySqlDataType.MEDIUM_BLOB:
                    case MySqlDataType.LONG_BLOB:

                        //writer.WriteLengthCodedBuffer(dataTemp.myBuffer);
                        //this is first round
                        singleValueHolder.isBufferOrString = true;
                        singleValueHolder.bufferContent = dataTemp.myBuffer;
                        singleValueHolder.contentLen = singleValueHolder.bufferContent.Length;
                        //generate output
                        singleValueHolder.encodedLengthLen = writer.GenerateEncodeLengthNumber(singleValueHolder.contentLen, singleValueHolder.headerLenBuffer);
                        if (availableContentSpace > singleValueHolder.TotalLength)
                        {
                            //write in first round
                            writer.WriteLengthCodedBuffer(singleValueHolder.bufferContent);
                            return true;
                        }
                        //go below

                        break;
                    case MySqlDataType.VARCHAR:
                    case MySqlDataType.VAR_STRING:
                    case MySqlDataType.STRING:
                        {
                            //this is first round
                            singleValueHolder.isBufferOrString = true;
                            singleValueHolder.bufferContent = Encoding.UTF8.GetBytes(dataTemp.myString);
                            singleValueHolder.contentLen = singleValueHolder.bufferContent.Length;
                            //generate output
                            singleValueHolder.encodedLengthLen = writer.GenerateEncodeLengthNumber(singleValueHolder.contentLen, singleValueHolder.headerLenBuffer);
                            if (availableContentSpace > singleValueHolder.TotalLength)
                            {
                                //write in first round
                                writer.WriteLengthCodedBuffer(singleValueHolder.bufferContent);
                                return true;
                            }
                            //go below 
                        }
                        break;
                    case MySqlDataType.TINY:
                        //if we can write in 1st round 
                        if (availableContentSpace > 1)
                        {
                            writer.WriteUnsignedNumber(1, dataTemp.myUInt32);
                            return true;
                        }
                        else
                        {
                            //go below 
                            MySqlStreamWriter.WriteUnsignedNumber(1, dataTemp.myUInt32, singleValueHolder.generalContent);
                        }
                        break;
                    case MySqlDataType.SHORT:
                        if (availableContentSpace > 2)
                        {
                            writer.WriteUnsignedNumber(2, dataTemp.myUInt32);
                            return true;
                        }
                        else
                        {
                            MySqlStreamWriter.WriteUnsignedNumber(2, dataTemp.myUInt32, singleValueHolder.generalContent);
                        }
                        break;
                    case MySqlDataType.LONG:
                        if (availableContentSpace > 4)
                        {
                            writer.WriteUnsignedNumber(4, dataTemp.myUInt32);
                            return true;
                        }
                        else
                        {
                            MySqlStreamWriter.WriteUnsignedNumber(4, dataTemp.myUInt32, singleValueHolder.generalContent);
                            //go below 
                        }
                        break;
                    case MySqlDataType.LONGLONG:
                        if (availableContentSpace > 8)
                        {
                            writer.WriteInt64(dataTemp.myInt64);
                            return true;
                        }
                        else
                        {
                            //go below 
                            MySqlStreamWriter.Write(dataTemp.myInt64, singleValueHolder.generalContent);
                        }

                        break;
                    case MySqlDataType.FLOAT:
                        if (availableContentSpace > 4)
                        {
                            writer.WriteFloat((float)dataTemp.myDouble);
                            return true;
                        }
                        else
                        {
                            //go below 
                            MySqlStreamWriter.Write((float)dataTemp.myDouble, singleValueHolder.generalContent);
                        }
                        break;
                    case MySqlDataType.DOUBLE:
                        if (availableContentSpace > 8)
                        {
                            writer.WriteDouble(dataTemp.myDouble);
                            return true;
                        }
                        else
                        {
                            //go below 
                            MySqlStreamWriter.Write(dataTemp.myDouble, singleValueHolder.generalContent);
                        }
                        break;
                    default:
                        //TODO: review here
                        throw new NotSupportedException();
                        //writer.WriteLengthCodedNull();
                }
            }
            //----------------------------------------------------------------------------------------------------------
            if (singleValueHolder.isBufferOrString)
            {
                //write encoded len & data
                //check if we write complete header or not   
                if (singleValueHolder.encodedLengthBufferWriteIndex < singleValueHolder.encodedLengthLen)
                {
                    //can we write complete encoded length header here?
                    int remaining = singleValueHolder.encodedLengthLen - singleValueHolder.encodedLengthBufferWriteIndex;
                    if (availableContentSpace > remaining)
                    {
                        for (int i = 0; i < remaining; ++i)
                        {
                            writer.WriteByte(singleValueHolder.headerLenBuffer[singleValueHolder.encodedLengthBufferWriteIndex]);
                            singleValueHolder.encodedLengthBufferWriteIndex++;
                        }
                        availableContentSpace -= remaining;
                    }
                    else if (availableContentSpace == remaining)
                    {
                        //TODO: review here
                        throw new NotSupportedException();
                        //we can write only header ***
                        for (int i = 0; i < remaining; ++i)
                        {
                            writer.WriteByte(singleValueHolder.headerLenBuffer[singleValueHolder.encodedLengthBufferWriteIndex]);
                            singleValueHolder.encodedLengthBufferWriteIndex++;
                        }
                        availableContentSpace -= remaining;
                    }
                    else
                    {
                        //can't complete encoded length here in this step
                        for (int i = 0; i < availableContentSpace; ++i)
                        {
                            writer.WriteByte(singleValueHolder.headerLenBuffer[singleValueHolder.encodedLengthBufferWriteIndex]);
                            singleValueHolder.encodedLengthBufferWriteIndex++;
                        }
                        return false;
                    }
                }
                //---------------
                //write the body
                int remainBodyLen = singleValueHolder.contentLen - singleValueHolder.contentStart;
                if (availableContentSpace > remainBodyLen)
                {
                    //write buffer 
                    //we use bufferContent ****
                    writer.WriteBuffer(singleValueHolder.bufferContent, singleValueHolder.contentStart, remainBodyLen);
                    singleValueHolder.contentStart += remainBodyLen;
                    return true;

                }
                else if (availableContentSpace == remainBodyLen)
                { 
                    //TODO: review here
                    throw new NotSupportedException();
                    //this needs last packet with 0 byte content size ***
                    writer.WriteBuffer(singleValueHolder.generalContent, singleValueHolder.contentStart, remainBodyLen);
                    singleValueHolder.contentStart += remainBodyLen;
                    return false;
                }
                else
                {
                    //write buffer 
                    //we use bufferContent ****
                    writer.WriteBuffer(singleValueHolder.bufferContent, singleValueHolder.contentStart, availableContentSpace);
                    singleValueHolder.contentStart += availableContentSpace;
                    return false;
                }
            }
            else
            {
                //---------------
                //write the body
                int remainBodyLen = singleValueHolder.contentLen - singleValueHolder.contentStart;
                if (availableContentSpace > remainBodyLen)
                {
                    //write buffer 
                    //we use generalContent ****
                    writer.WriteBuffer(singleValueHolder.generalContent, singleValueHolder.contentStart, remainBodyLen);
                    singleValueHolder.contentStart += remainBodyLen;
                    return true;
                }
                else if (availableContentSpace == remainBodyLen)
                {   
                    
                    //TODO: review here
                    throw new NotSupportedException();
                    //this needs last packet with 0 byte content size ***
                    writer.WriteBuffer(singleValueHolder.generalContent, singleValueHolder.contentStart, remainBodyLen);
                    singleValueHolder.contentStart += remainBodyLen;
                    return false;
                }
                else
                {
                    //write buffer 
                    //we use generalContent ****
                    writer.WriteBuffer(singleValueHolder.generalContent, singleValueHolder.contentStart, availableContentSpace);
                    singleValueHolder.contentStart += availableContentSpace;
                    return false;
                }
            }
        }
        static void FormatAndAppendData(StringBuilder stbuilder, ref MyStructData data)
        {
            //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            //TODO: review here , data range
            //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            switch (data.type)
            {
                case MySqlDataType.BLOB:
                case MySqlDataType.LONG_BLOB:
                case MySqlDataType.MEDIUM_BLOB:
                case MySqlDataType.TINY_BLOB:
                    MySqlStringToHexUtils.ConvertByteArrayToHexWithMySqlPrefix(data.myBuffer, stbuilder);
                    break;
                case MySqlDataType.DATE:
                case MySqlDataType.NEWDATE:
                    stbuilder.Append('\'');
                    stbuilder.Append(data.myDateTime.ToString("yyyy-MM-dd"));
                    stbuilder.Append('\'');
                    break;
                case MySqlDataType.DATETIME:
                    stbuilder.Append('\'');
                    stbuilder.Append(data.myDateTime.ToString("yyyy-MM-dd hh:mm:ss"));
                    stbuilder.Append('\'');
                    break;
                case MySqlDataType.TIMESTAMP:
                case MySqlDataType.TIME:
                    //TODO: review here
                    stbuilder.Append('\'');
                    stbuilder.Append(data.myDateTime.ToString("hh:mm:ss"));
                    stbuilder.Append('\'');
                    break;
                case MySqlDataType.STRING:
                case MySqlDataType.VARCHAR:
                case MySqlDataType.VAR_STRING:

                    stbuilder.Append('\'');
                    //TODO: check /escape string here ****
                    stbuilder.Append(data.myString);
                    stbuilder.Append('\'');
                    break;
                case MySqlDataType.BIT:
                    stbuilder.Append(Encoding.ASCII.GetString(new byte[] { (byte)data.myInt32 }));
                    break;
                case MySqlDataType.DOUBLE:
                    stbuilder.Append(data.myDouble.ToString());
                    break;
                case MySqlDataType.FLOAT:
                    stbuilder.Append(((float)data.myDouble).ToString());
                    break;
                case MySqlDataType.TINY:
                case MySqlDataType.SHORT:
                case MySqlDataType.LONG:
                case MySqlDataType.INT24:
                case MySqlDataType.YEAR:
                    stbuilder.Append(data.myInt32.ToString());
                    break;
                case MySqlDataType.LONGLONG:
                    stbuilder.Append(data.myInt64.ToString());
                    break;
                case MySqlDataType.DECIMAL:
                    stbuilder.Append(data.myDecimal.ToString());
                    break;
                default:
                    stbuilder.Append(data.myUInt64.ToString());
                    break;
            }
        }
 public void AddWithValue(string key, DateTime value)
 {
     var data = new MyStructData();
     data.myDateTime = value;
     data.type = MySqlDataType.DATETIME;
     _values[key] = data;
 }
 public void AddWithValue(string key, sbyte value)
 {
     //tiny int signed (-128 to 127)
     var data = new MyStructData();
     data.myInt32 = value;
     data.type = MySqlDataType.TINY;
     _values[key] = data;
 }
 public void AddWithValue(string key, byte[] value)
 {
     var data = new MyStructData();
     if (value != null)
     {
         data.myBuffer = value;
         data.type = MySqlDataType.LONG_BLOB;
     }
     else
     {
         data.myBuffer = null;
         data.type = MySqlDataType.NULL;
     }
     _values[key] = data;
 }
        public void AddWithValue(string key, ushort value)
        {
            //INT 2       min        max
            //signed      -32768    32767
            //unsigned     0     65535
            //---------------------------

            var data = new MyStructData();
            data.myString = value.ToString();
            data.type = MySqlDataType.STRING;
            //data.myUInt32 = value;
            //data.type = Types.SHORT;
            _values[key] = data;
        }
Exemplo n.º 24
0
 public ComExecPrepareStmtPacket(PacketHeader header, uint statementId, MyStructData[] filledValues)
     : base(header)
 {
     _statementId = statementId;
     _prepareValues = filledValues;
 }
 public void AddWithValue(string key, ulong value)
 {
     var data = new MyStructData();
     data.myString = value.ToString();
     data.type = MySqlDataType.STRING;
     //data.myUInt64 = value;
     //data.type = Types.LONGLONG;
     _values[key] = data;
 }
Exemplo n.º 26
0
        public static void Write(MySqlStreamWriter writer, uint stmtId, MyStructData[] _prepareValues)
        {
            //for those who don't want to alloc an new packet
            //just write it into a stream
            writer.ReserveHeader();
            writer.WriteByte((byte)Command.STMT_EXECUTE);
            writer.WriteUnsignedNumber(4, stmtId);
            writer.WriteByte((byte)CursorFlags.CURSOR_TYPE_NO_CURSOR);

            writer.WriteUnsignedNumber(4, 1);//iteration-count, always 1
                                             //write NULL-bitmap, length: (num-params+7)/8 
            MyStructData[] fillValues = _prepareValues;
            int paramNum = _prepareValues.Length;
            if (paramNum > 0)
            {
                uint bitmap = 0;
                uint bitValue = 1;
                for (int i = 0; i < paramNum; i++)
                {
                    MySqlDataType dataType = _prepareValues[i].type;
                    if (dataType == MySqlDataType.NULL)
                    {
                        bitmap += bitValue;
                    }
                    bitValue <<= 1; //shift to left 1 bit
                    if (bitValue == 256)
                    {
                        //end of 8 bits
                        //just store data
                        writer.WriteUnsigned1(bitmap);
                        bitmap = 0;
                        bitValue = 1;
                    }
                }
                if (bitValue != 1)
                {
                    writer.WriteUnsigned1(bitmap);
                }
            }
            //new-params-bound - flag
            writer.WriteByte(1);
            //-------------------------------------------------------
            //data types
            for (int i = 0; i < paramNum; i++)
            {
                writer.WriteUnsignedNumber(2, (byte)_prepareValues[i].type);
            }
            //--------------------------------------
            //actual data
            //--------------------------------------
            //stream of data may large than 1 packet
            var tempSingleValueHolder = new TempSingleValueHolder();
            tempSingleValueHolder.headerLenBuffer = new byte[9];
            tempSingleValueHolder.generalContent = new byte[16];
            for (int i = 0; i < paramNum; i++)
            {
                bool isComplete = WriteValueByType(writer, ref _prepareValues[i], ref tempSingleValueHolder);
                var header = new PacketHeader(writer.OnlyPacketContentLength, writer.IncrementPacketNumber());
                writer.WriteHeader(header);
                //--------------------------------------------------------------------------------------------------
                while (!isComplete)
                {
                    //write until complete
                    tempSingleValueHolder.round++;
                    writer.ReserveHeader();
                    isComplete = WriteValueByType(writer, ref _prepareValues[i], ref tempSingleValueHolder);
                    header = new PacketHeader(writer.OnlyPacketContentLength, writer.IncrementPacketNumber());
                    writer.WriteHeader(header);
                }
                //reset
                tempSingleValueHolder.Reset();
            }
            //--------------------------------------

        }
 internal bool TryGetData(string key, out MyStructData data)
 {
     return _values.TryGetValue(key, out data);
 }
Exemplo n.º 28
0
 void WriteValueByType(MySqlStreamWriter writer, MyStructData dataTemp)
 {
     switch (dataTemp.type)
     {
         case MySqlDataType.VARCHAR:
         case MySqlDataType.VAR_STRING:
         case MySqlDataType.STRING:
             writer.WriteLengthCodedString(dataTemp.myString);
             break;
         case MySqlDataType.LONG:
             writer.WriteUnsignedNumber(4, (uint)dataTemp.myInt32);
             break;
         case MySqlDataType.LONGLONG:
             writer.WriteInt64(dataTemp.myInt64);
             break;
         case MySqlDataType.FLOAT:
             writer.WriteFloat((float)dataTemp.myDouble);
             break;
         case MySqlDataType.DOUBLE:
             writer.WriteDouble(dataTemp.myDouble);
             break;
         case MySqlDataType.BIT:
         case MySqlDataType.BLOB:
         case MySqlDataType.MEDIUM_BLOB:
         case MySqlDataType.LONG_BLOB:
             writer.WriteLengthCodedBuffer(dataTemp.myBuffer);
             break;
         default:
             writer.WriteLengthCodedNull();
             break;
     }
 }
Exemplo n.º 29
0
        static void FormatAndAppendData(StringBuilder stbuilder, ref MyStructData data)
        {
            //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            //TODO: review here , data range
            //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
            switch (data.type)
            {
            case Types.BLOB:
            case Types.LONG_BLOB:
            case Types.MEDIUM_BLOB:
            case Types.TINY_BLOB:
                MySqlStringToHexUtils.ConvertByteArrayToHexWithMySqlPrefix(data.myBuffer, stbuilder);
                break;

            case Types.DATE:
            case Types.NEWDATE:
                stbuilder.Append('\'');
                stbuilder.Append(data.myDateTime.ToString("yyyy-MM-dd"));
                stbuilder.Append('\'');
                break;

            case Types.DATETIME:
                stbuilder.Append('\'');
                stbuilder.Append(data.myDateTime.ToString("yyyy-MM-dd hh:mm:ss"));
                stbuilder.Append('\'');
                break;

            case Types.TIMESTAMP:
            case Types.TIME:
                //TODO: review here
                stbuilder.Append('\'');
                stbuilder.Append(data.myDateTime.ToString("hh:mm:ss"));
                stbuilder.Append('\'');
                break;

            case Types.STRING:
            case Types.VARCHAR:
            case Types.VAR_STRING:

                stbuilder.Append('\'');
                //TODO: check /escape string here ****
                stbuilder.Append(data.myString);
                stbuilder.Append('\'');

                break;

            case Types.BIT:
                stbuilder.Append(Encoding.ASCII.GetString(new byte[] { (byte)data.myInt32 }));
                break;

            case Types.DOUBLE:
                stbuilder.Append(data.myDouble.ToString());
                break;

            case Types.FLOAT:
                stbuilder.Append(((float)data.myDouble).ToString());
                break;

            case Types.TINY:
            case Types.SHORT:
            case Types.LONG:
            case Types.INT24:
            case Types.YEAR:
                stbuilder.Append(data.myInt32.ToString());
                break;

            case Types.LONGLONG:
                stbuilder.Append(data.myInt64.ToString());
                break;

            case Types.DECIMAL:
                stbuilder.Append(data.myDecimal.ToString());
                break;

            default:
                stbuilder.Append(data.myUInt64.ToString());
                break;
            }
        }