Beispiel #1
0
        private void EscapeByteArray(byte[] bytes, int length, MySqlStream stream)
        {
            for (int i = 0; i < length; i++)
            {
                byte num2 = bytes[i];
                switch (num2)
                {
                case 0:
                    stream.WriteByte(0x5c);
                    stream.WriteByte(0x30);
                    break;

                case 0x5c:
                case 0x27:
                case 0x22:
                    stream.WriteByte(0x5c);
                    stream.WriteByte(num2);
                    break;

                default:
                    stream.WriteByte(num2);
                    break;
                }
            }
        }
Beispiel #2
0
        void IMySqlValue.WriteValue(MySqlStream stream, 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)
            {
                stream.WriteByte(8);
                stream.WriteByte((byte)(negative ? 1 : 0));
                stream.WriteInteger(ts.Days, 4);
                stream.WriteByte((byte)ts.Hours);
                stream.WriteByte((byte)ts.Minutes);
                stream.WriteByte((byte)ts.Seconds);
            }
            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);

                stream.WriteStringNoNull(s);
            }
        }
Beispiel #3
0
        private void EscapeByteArray(byte[] bytes, int length, MySqlStream stream)
        {
            //	System.IO.MemoryStream ms = (System.IO.MemoryStream)stream.Stream;
            //	ms.Capacity += (length * 2);

            for (int x = 0; x < length; x++)
            {
                byte b = bytes[x];
                if (b == '\0')
                {
                    stream.WriteByte((byte)'\\');
                    stream.WriteByte((byte)'0');
                }

                else if (b == '\\' || b == '\'' || b == '\"')
                {
                    stream.WriteByte((byte)'\\');
                    stream.WriteByte(b);
                }
                else
                {
                    stream.WriteByte(b);
                }
            }
        }
Beispiel #4
0
        void IMySqlValue.WriteValue(MySqlStream stream, bool binary, object val, int length)
        {
            if (!(val is TimeSpan))
            {
                throw new MySqlException("Only TimeSpan objects can be serialized by MySqlTimeSpan");
            }
            TimeSpan span = (TimeSpan)val;
            bool     flag = span.TotalMilliseconds < 0.0;

            span = span.Duration();
            if (binary)
            {
                stream.WriteByte(8);
                stream.WriteByte(flag ? ((byte)1) : ((byte)0));
                stream.WriteInteger((long)span.Days, 4);
                stream.WriteByte((byte)span.Hours);
                stream.WriteByte((byte)span.Minutes);
                stream.WriteByte((byte)span.Seconds);
            }
            else
            {
                string v = string.Format("'{0}{1} {2:00}:{3:00}:{4:00}.{5}'", new object[] { flag ? "-" : "", span.Days, span.Hours, span.Minutes, span.Seconds, span.Milliseconds });
                stream.WriteStringNoNull(v);
            }
        }
        void IMySqlValue.WriteValue(MySqlStream stream, bool binary, object value, int length)
        {
            MySqlDateTime dtValue;

            if (value is DateTime)
            {
                dtValue = new MySqlDateTime(type, (DateTime)value);
            }
            else if (value is string)
            {
                dtValue = new MySqlDateTime(type, DateTime.Parse((string)value,
                                                                 System.Globalization.CultureInfo.CurrentCulture));
            }
            else if (value is MySqlDateTime)
            {
                dtValue = (MySqlDateTime)value;
            }
            else
            {
                throw new MySqlException("Unable to serialize date/time value.");
            }

            if (!binary)
            {
                SerializeText(stream, dtValue);
                return;
            }

            if (type == MySqlDbType.Timestamp)
            {
                stream.WriteByte(11);
            }
            else
            {
                stream.WriteByte(7);
            }

            stream.WriteInteger(dtValue.Year, 2);
            stream.WriteByte((byte)dtValue.Month);
            stream.WriteByte((byte)dtValue.Day);
            if (type == MySqlDbType.Date)
            {
                stream.WriteByte(0);
                stream.WriteByte(0);
                stream.WriteByte(0);
            }
            else
            {
                stream.WriteByte((byte)dtValue.Hour);
                stream.WriteByte((byte)dtValue.Minute);
                stream.WriteByte((byte)dtValue.Second);
            }

            if (type == MySqlDbType.Timestamp)
            {
                stream.WriteInteger(dtValue.Millisecond, 4);
            }
        }
Beispiel #6
0
        void IMySqlValue.WriteValue(MySqlStream stream, bool binary, object val, int length)
        {
            byte[] buffToWrite = null;

            if (val is System.Byte[])
            {
                buffToWrite = (byte[])val;
            }
            else if (val is Char[])
            {
                buffToWrite = stream.Encoding.GetBytes(val as char[]);
            }
            else
            {
                string s = val.ToString();
                if (length == 0)
                {
                    length = s.Length;
                }
                else
                {
                    s = s.Substring(0, length);
                }
                buffToWrite = stream.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)
            {
                stream.WriteLength(length);
                stream.Write(buffToWrite, 0, length);
            }
            else
            {
                if (stream.Version.isAtLeast(4, 1, 0))
                {
                    stream.WriteStringNoNull("_binary ");
                }

                stream.WriteByte((byte)'\'');
                EscapeByteArray(buffToWrite, length, stream);
                stream.WriteByte((byte)'\'');
            }
        }
Beispiel #7
0
 void IMySqlValue.WriteValue(MySqlStream stream, bool binary, object val, int length)
 {
     byte[] bytes = null;
     if (val is byte[])
     {
         bytes = (byte[])val;
     }
     else if (val is char[])
     {
         bytes = stream.Encoding.GetBytes(val as char[]);
     }
     else
     {
         string s = val.ToString();
         if (length == 0)
         {
             length = s.Length;
         }
         else
         {
             s = s.Substring(0, length);
         }
         bytes = stream.Encoding.GetBytes(s);
     }
     if (length == 0)
     {
         length = bytes.Length;
     }
     if (bytes == null)
     {
         throw new MySqlException("Only byte arrays and strings can be serialized by MySqlBinary");
     }
     if (binary)
     {
         stream.WriteLength((long)length);
         stream.Write(bytes, 0, length);
     }
     else
     {
         if (stream.Version.isAtLeast(4, 1, 0))
         {
             stream.WriteStringNoNull("_binary ");
         }
         stream.WriteByte(0x27);
         this.EscapeByteArray(bytes, length, stream);
         stream.WriteByte(0x27);
     }
 }
Beispiel #8
0
        void IMySqlValue.WriteValue(MySqlStream stream, bool binary, object val, int length)
        {
            sbyte num = ((IConvertible)val).ToSByte(null);

            if (binary)
            {
                stream.WriteByte((byte)num);
            }
            else
            {
                stream.WriteStringNoNull(num.ToString());
            }
        }