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();
            }
            //--------------------------------------

        }
 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;
     }
 }
        //----------------------------
        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;
                }
            }
        }