Example #1
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);
            }
        }
Example #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);
            }
        }
Example #3
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)'\'');
            }
        }
Example #4
0
        void IMySqlValue.WriteValue(MySqlStream stream, bool binary, object val, int length)
        {
            long num = Convert.ToInt64(val);

            if (binary)
            {
                stream.Write(BitConverter.GetBytes(num));
            }
            else
            {
                stream.WriteStringNoNull(num.ToString());
            }
        }
Example #5
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());
            }
        }
Example #6
0
        void IMySqlValue.WriteValue(MySqlStream stream, bool binary, object v, int length)
        {
            uint num = Convert.ToUInt32(v);

            if (binary)
            {
                stream.Write(BitConverter.GetBytes(num));
            }
            else
            {
                stream.WriteStringNoNull(num.ToString());
            }
        }
Example #7
0
        void IMySqlValue.WriteValue(MySqlStream stream, bool binary, object val, int length)
        {
            string s = Convert.ToDecimal(val).ToString(CultureInfo.InvariantCulture);

            if (binary)
            {
                stream.WriteLenString(s);
            }
            else
            {
                stream.WriteStringNoNull(s);
            }
        }
Example #8
0
        void IMySqlValue.WriteValue(MySqlStream stream, bool binary, object val, int length)
        {
            int v = Convert.ToUInt16(val);

            if (binary)
            {
                stream.Write(BitConverter.GetBytes(v));
            }
            else
            {
                stream.WriteStringNoNull(v.ToString());
            }
        }
Example #9
0
        public void WriteValue(MySqlStream stream, bool binary, object value, int length)
        {
            ulong v = Convert.ToUInt64(value);

            if (binary)
            {
                stream.Write(BitConverter.GetBytes(v));
            }
            else
            {
                stream.WriteStringNoNull(v.ToString());
            }
        }
Example #10
0
        void IMySqlValue.WriteValue(MySqlStream stream, bool binary, object val, int length)
        {
            double num = Convert.ToDouble(val);

            if (binary)
            {
                stream.Write(BitConverter.GetBytes(num));
            }
            else
            {
                stream.WriteStringNoNull(num.ToString("R", CultureInfo.InvariantCulture));
            }
        }
Example #11
0
        void IMySqlValue.WriteValue(MySqlStream stream, bool binary, object val, int length)
        {
            sbyte v = ((IConvertible)val).ToSByte(null);

            if (binary)
            {
                stream.Write(BitConverter.GetBytes(v));
            }
            else
            {
                stream.WriteStringNoNull(v.ToString());
            }
        }
Example #12
0
        void IMySqlValue.WriteValue(MySqlStream stream, bool binary, object val, int length)
        {
            Single v = Convert.ToSingle(val);

            if (binary)
            {
                stream.Write(BitConverter.GetBytes(v));
            }
            else
            {
                stream.WriteStringNoNull(v.ToString(
                                             CultureInfo.InvariantCulture));
            }
        }
Example #13
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);
     }
 }
        private void SerializeText(MySqlStream stream, MySqlDateTime value)
        {
            string val = String.Empty;

            if (type == MySqlDbType.Timestamp && !stream.Version.isAtLeast(4, 1, 0))
            {
                val = String.Format("{0:0000}{1:00}{2:00}{3:00}{4:00}{5:00}",
                                    value.Year, value.Month, value.Day, value.Hour, value.Minute, value.Second);
            }
            else
            {
                val = String.Format("{0:0000}-{1:00}-{2:00} {3:00}:{4:00}:{5:00}", value.Year, value.Month,
                                    value.Day, value.Hour, value.Minute, value.Second);
            }
            stream.WriteStringNoNull("'" + val + "'");
        }
Example #15
0
        private void SerializeText(MySqlStream stream, MySqlDateTime value)
        {
            string str = string.Empty;

            if ((this.type == MySqlDbType.Timestamp) && !stream.Version.isAtLeast(4, 1, 0))
            {
                str = string.Format("{0:0000}{1:00}{2:00}{3:00}{4:00}{5:00}", new object[] { value.Year, value.Month, value.Day, value.Hour, value.Minute, value.Second });
            }
            else
            {
                str = string.Format("{0:0000}-{1:00}-{2:00}", value.Year, value.Month, value.Day);
                if (this.type != MySqlDbType.Date)
                {
                    str = string.Format("{0}  {1:00}:{2:00}:{3:00}", new object[] { str, value.Hour, value.Minute, value.Second });
                }
            }
            stream.WriteStringNoNull("'" + str + "'");
        }
        void IMySqlValue.WriteValue(MySqlStream stream, bool binary, object val, int length)
        {
            string v = val.ToString();

            if (length > 0)
            {
                v = v.Substring(0, length);
            }

            if (binary)
            {
                stream.WriteLenString(v);
            }
            else
            {
                stream.WriteStringNoNull("'" + EscapeString(v) + "'");
            }
        }
Example #17
0
        void IMySqlValue.WriteValue(MySqlStream stream, bool binary, object val, int length)
        {
            string s = val.ToString();

            if (length > 0)
            {
                length = Math.Min(length, s.Length);
                s      = s.Substring(0, length);
            }
            if (binary)
            {
                stream.WriteLenString(s);
            }
            else
            {
                stream.WriteStringNoNull("'" + MySqlHelper.EscapeString(s) + "'");
            }
        }