Esempio n. 1
0
 internal void Serialize(MySqlPacket packet, bool binary, MySqlConnectionStringBuilder settings)
 {
     if (!binary && (paramValue == null || paramValue == DBNull.Value))
     packet.WriteStringNoNull("NULL");
       else
       {
     if (ValueObject.MySqlDbType == MySqlDbType.Guid)
     {
       MySqlGuid g = (MySqlGuid)ValueObject;
       g.OldGuids = settings.OldGuids;
       valueObject = g;
     }
     ValueObject.WriteValue(packet, binary, paramValue, size);
       }
 }
Esempio n. 2
0
        void IMySqlValue.WriteValue(MySqlPacket packet, bool binary, object val, int length)
        {
            byte[] buffToWrite = (val as byte[]);
              if (buffToWrite == null)
              {
            char[] valAsChar = (val as Char[]);
            if (valAsChar != null)
              buffToWrite = packet.Encoding.GetBytes(valAsChar);
            else
            {
              string s = val.ToString();
              if (length == 0)
            length = s.Length;
              else
            s = s.Substring(0, length);
              buffToWrite = packet.Encoding.GetBytes(s);
            }
              }

              // we assume zero length means write all of the value
              if (length == 0)
            length = buffToWrite.Length;

              if (buffToWrite == null)
            throw new MySqlException("Only byte arrays and strings can be serialized by MySqlBinary");

              if (binary)
              {
            packet.WriteLength(length);
            packet.Write(buffToWrite, 0, length);
              }
              else
              {
            packet.WriteStringNoNull("_binary ");
            packet.WriteByte((byte)'\'');
            EscapeByteArray(buffToWrite, length, packet);
            packet.WriteByte((byte)'\'');
              }
        }
Esempio n. 3
0
 void IMySqlValue.WriteValue(MySqlPacket packet, bool binary, object val, int length)
 {
     Single v = (val is Single) ? (Single)val : Convert.ToSingle(val);
       if (binary)
     packet.Write(BitConverter.GetBytes(v));
       else
     packet.WriteStringNoNull(v.ToString("R",
        CultureInfo.InvariantCulture));
 }
Esempio n. 4
0
 void IMySqlValue.WriteValue(MySqlPacket packet, bool binary, object val, int length)
 {
     int v = (val is Int32) ? (int)val : Convert.ToInt32(val);
       if (binary)
     packet.WriteInteger((long)v, 2);
       else
     packet.WriteStringNoNull(v.ToString());
 }
Esempio n. 5
0
 void IMySqlValue.WriteValue(MySqlPacket packet, bool binary, object v, int length)
 {
     uint val = (v is uint) ? (uint)v : Convert.ToUInt32(v);
       if (binary)
     packet.WriteInteger((long)val, is24Bit ? 3 : 4);
       else
     packet.WriteStringNoNull(val.ToString());
 }
Esempio n. 6
0
        void IMySqlValue.WriteValue(MySqlPacket packet, bool binary, object val, int length)
        {
            Guid guid = Guid.Empty;
              string valAsString = val as string;
              byte[] valAsByte = val as byte[];

              if (val is Guid)
            guid = (Guid)val;
              else
              {
            try
            {
              if (valAsString != null)
            guid = new Guid(valAsString);
              else if (valAsByte != null)
            guid = new Guid(valAsByte);
            }
            catch (Exception ex)
            {
              throw new MySqlException(Resources.DataNotInSupportedFormat, ex);
            }
              }

              if (OldGuids)
            WriteOldGuid(packet, guid, binary);
              else
              {
            guid.ToString("D");

            if (binary)
              packet.WriteLenString(guid.ToString("D"));
            else
              packet.WriteStringNoNull("'" + MySqlHelper.EscapeString(guid.ToString("D")) + "'");
              }
        }
Esempio n. 7
0
        private void WriteOldGuid(MySqlPacket packet, Guid guid, bool binary)
        {
            byte[] bytes = guid.ToByteArray();

              if (binary)
              {
            packet.WriteLength(bytes.Length);
            packet.Write(bytes);
              }
              else
              {
            packet.WriteStringNoNull("_binary ");
            packet.WriteByte((byte)'\'');
            EscapeByteArray(bytes, bytes.Length, packet);
            packet.WriteByte((byte)'\'');
              }
        }
Esempio n. 8
0
        void IMySqlValue.WriteValue(MySqlPacket packet, bool binary, object val, int length)
        {
            if (!(val is TimeSpan))
            throw new MySqlException("Only TimeSpan objects can be serialized by MySqlTimeSpan");

              TimeSpan ts = (TimeSpan)val;
              bool negative = ts.TotalMilliseconds < 0;
              ts = ts.Duration();

              if (binary)
              {
            if (ts.Milliseconds > 0)
              packet.WriteByte(12);
            else
              packet.WriteByte(8);

            packet.WriteByte((byte)(negative ? 1 : 0));
            packet.WriteInteger(ts.Days, 4);
            packet.WriteByte((byte)ts.Hours);
            packet.WriteByte((byte)ts.Minutes);
            packet.WriteByte((byte)ts.Seconds);
            if (ts.Milliseconds > 0)
            {
              long mval = ts.Milliseconds*1000;
              packet.WriteInteger(mval, 4);
            }
              }
              else
              {
            String s = String.Format("'{0}{1} {2:00}:{3:00}:{4:00}.{5}'",
            negative ? "-" : "", ts.Days, ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds);

            packet.WriteStringNoNull(s);
              }
        }
Esempio n. 9
0
 void IMySqlValue.WriteValue(MySqlPacket packet, bool binary, object val, int length)
 {
     ulong v = (val is ulong) ? (ulong)val : Convert.ToUInt64(val);
       if (binary)
     packet.WriteInteger((long)v, 8);
       else
     packet.WriteStringNoNull(v.ToString());
 }
Esempio n. 10
0
 void IMySqlValue.WriteValue(MySqlPacket packet, bool binary, object val, int length)
 {
     byte v = (val is byte) ? (byte)val : Convert.ToByte(val);
       if (binary)
     packet.WriteByte(v);
       else
     packet.WriteStringNoNull(v.ToString());
 }
Esempio n. 11
0
        void IMySqlValue.WriteValue(MySqlPacket packet, bool binary, object val, int length)
        {
            string v = val.ToString();
              if (length > 0)
              {
            length = Math.Min(length, v.Length);
            v = v.Substring(0, length);
              }

              if (binary)
            packet.WriteLenString(v);
              else
            packet.WriteStringNoNull("'" + MySqlHelper.EscapeString(v) + "'");
        }
Esempio n. 12
0
 void IMySqlValue.WriteValue(MySqlPacket packet, bool binary, object val, int length)
 {
     decimal v = (val is decimal) ? (decimal)val : Convert.ToDecimal(val);
       string valStr = v.ToString(CultureInfo.InvariantCulture);
       if (binary)
     packet.WriteLenString(valStr);
       else
     packet.WriteStringNoNull(valStr);
 }
Esempio n. 13
0
        private void SerializeText(MySqlPacket packet, MySqlDateTime value)
        {
            string val = String.Empty;

              val = String.Format("{0:0000}-{1:00}-{2:00}",
                value.Year, value.Month, value.Day);
              if (type != MySqlDbType.Date)
              {
            val = value.Millisecond > 0 ? String.Format("{0} {1:00}:{2:00}:{3:00}.{4:000}", val,
              value.Hour, value.Minute, value.Second, value.Millisecond) : String.Format("{0} {1:00}:{2:00}:{3:00} ", val,
              value.Hour, value.Minute, value.Second);
              }

              packet.WriteStringNoNull("'" + val + "'");
        }
Esempio n. 14
0
 public void WriteValue(MySqlPacket packet, bool binary, object value, int length)
 {
     ulong v = (value is UInt64) ? (UInt64)value : Convert.ToUInt64(value);
       if (binary)
     packet.WriteInteger((long)v, 8);
       else
     packet.WriteStringNoNull(v.ToString());
 }