Example #1
0
        public static string ConvertToSqlFormat(MySqlDataReader rdr, int colIndex, bool wrapStringWithSingleQuote,
                                                bool escapeStringSequence, MySqlColumn col)
        {
            var ob = rdr[colIndex];

            var sb = new StringBuilder();

            if (ob == null || ob is DBNull)
            {
                sb.AppendFormat("NULL");
            }
            else if (ob is string)
            {
                var str = (string)ob;

                if (escapeStringSequence)
                {
                    str = EscapeStringSequence(str);
                }

                if (wrapStringWithSingleQuote)
                {
                    sb.AppendFormat("'");
                }

                sb.Append(str);

                if (wrapStringWithSingleQuote)
                {
                    sb.AppendFormat("'");
                }
            }
            else if (ob is bool)
            {
                sb.AppendFormat(Convert.ToInt32(ob).ToString());
            }
            else if (ob is byte[])
            {
                if (((byte[])ob).Length == 0)
                {
                    if (wrapStringWithSingleQuote)
                    {
                        return("''");
                    }
                    else
                    {
                        return("");
                    }
                }
                sb.AppendFormat(CryptoExpress.ConvertByteArrayToHexString((byte[])ob));
            }
            else if (ob is short)
            {
                sb.AppendFormat(((short)ob).ToString(MySqlNumberFormat));
            }
            else if (ob is int)
            {
                sb.AppendFormat(((int)ob).ToString(MySqlNumberFormat));
            }
            else if (ob is long)
            {
                sb.AppendFormat(((long)ob).ToString(MySqlNumberFormat));
            }
            else if (ob is ushort)
            {
                sb.AppendFormat(((ushort)ob).ToString(MySqlNumberFormat));
            }
            else if (ob is uint)
            {
                sb.AppendFormat(((uint)ob).ToString(MySqlNumberFormat));
            }
            else if (ob is ulong)
            {
                sb.AppendFormat(((ulong)ob).ToString(MySqlNumberFormat));
            }
            else if (ob is double)
            {
                sb.AppendFormat(((double)ob).ToString(MySqlNumberFormat));
            }
            else if (ob is decimal)
            {
                sb.AppendFormat(((decimal)ob).ToString(MySqlNumberFormat));
            }
            else if (ob is float)
            {
                sb.AppendFormat(((float)ob).ToString(MySqlNumberFormat));
            }
            else if (ob is byte)
            {
                sb.AppendFormat(((byte)ob).ToString(MySqlNumberFormat));
            }
            else if (ob is sbyte)
            {
                sb.AppendFormat(((sbyte)ob).ToString(MySqlNumberFormat));
            }
            else if (ob is TimeSpan)
            {
                var ts = (TimeSpan)ob;

                if (wrapStringWithSingleQuote)
                {
                    sb.AppendFormat("'");
                }

                sb.AppendFormat(ts.Hours.ToString().PadLeft(2, '0'));
                sb.AppendFormat(":");
                sb.AppendFormat(ts.Minutes.ToString().PadLeft(2, '0'));
                sb.AppendFormat(":");
                sb.AppendFormat(ts.Seconds.ToString().PadLeft(2, '0'));

                if (wrapStringWithSingleQuote)
                {
                    sb.AppendFormat("'");
                }
            }
            else if (ob is DateTime)
            {
                if (wrapStringWithSingleQuote)
                {
                    sb.AppendFormat("'");
                }

                sb.AppendFormat(((DateTime)ob).ToString("yyyy-MM-dd HH:mm:ss", MySqlDateTimeFormat));

                if (col.TimeFractionLength > 0)
                {
                    sb.Append(".");
                    var microsecond = rdr.GetMySqlDateTime(colIndex).Microsecond.ToString();
                    if (microsecond.Length < col.TimeFractionLength)
                    {
                        microsecond = microsecond.PadLeft(col.TimeFractionLength, '0');
                    }
                    else if (microsecond.Length > col.TimeFractionLength)
                    {
                        microsecond = microsecond.Substring(0, col.TimeFractionLength);
                    }
                    sb.Append(microsecond.PadLeft(col.TimeFractionLength, '0'));
                }

                if (wrapStringWithSingleQuote)
                {
                    sb.AppendFormat("'");
                }
            }
            else if (ob is MySqlDateTime)
            {
                var mdt = (MySqlDateTime)ob;

                if (mdt.IsNull)
                {
                    sb.AppendFormat("NULL");
                }
                else
                {
                    if (mdt.IsValidDateTime)
                    {
                        var dtime = mdt.Value;

                        if (wrapStringWithSingleQuote)
                        {
                            sb.AppendFormat("'");
                        }

                        if (col.MySqlDataType == "datetime")
                        {
                            sb.AppendFormat(dtime.ToString("yyyy-MM-dd HH:mm:ss", MySqlDateTimeFormat));
                        }
                        else if (col.MySqlDataType == "date")
                        {
                            sb.AppendFormat(dtime.ToString("yyyy-MM-dd", MySqlDateTimeFormat));
                        }
                        else if (col.MySqlDataType == "time")
                        {
                            sb.AppendFormat(dtime.ToString("HH:mm:ss", MySqlDateTimeFormat));
                        }
                        else
                        {
                            sb.AppendFormat(dtime.ToString("yyyy-MM-dd HH:mm:ss", MySqlDateTimeFormat));
                        }

                        if (col.TimeFractionLength > 0)
                        {
                            sb.Append(".");
                            sb.Append(((MySqlDateTime)ob).Microsecond.ToString().PadLeft(col.TimeFractionLength, '0'));
                        }

                        if (wrapStringWithSingleQuote)
                        {
                            sb.AppendFormat("'");
                        }
                    }
                    else
                    {
                        if (wrapStringWithSingleQuote)
                        {
                            sb.AppendFormat("'");
                        }

                        if (col.MySqlDataType == "datetime")
                        {
                            sb.AppendFormat("0000-00-00 00:00:00");
                        }
                        else if (col.MySqlDataType == "date")
                        {
                            sb.AppendFormat("0000-00-00");
                        }
                        else if (col.MySqlDataType == "time")
                        {
                            sb.AppendFormat("00:00:00");
                        }
                        else
                        {
                            sb.AppendFormat("0000-00-00 00:00:00");
                        }

                        if (col.TimeFractionLength > 0)
                        {
                            sb.Append(".".PadRight(col.TimeFractionLength, '0'));
                        }

                        if (wrapStringWithSingleQuote)
                        {
                            sb.AppendFormat("'");
                        }
                    }
                }
            }
            else if (ob is Guid)
            {
                switch (col.MySqlDataType)
                {
                case "binary(16)":
                    sb.Append(CryptoExpress.ConvertByteArrayToHexString(((Guid)ob).ToByteArray()));
                    break;

                case "char(36)":
                    if (wrapStringWithSingleQuote)
                    {
                        sb.AppendFormat("'");
                    }

                    sb.Append(ob);

                    if (wrapStringWithSingleQuote)
                    {
                        sb.AppendFormat("'");
                    }
                    break;

                default:
                    if (wrapStringWithSingleQuote)
                    {
                        sb.AppendFormat("'");
                    }

                    sb.Append(ob);

                    if (wrapStringWithSingleQuote)
                    {
                        sb.AppendFormat("'");
                    }
                    break;
                }
            }
            else
            {
                throw new Exception("Unhandled data type. Current processing data type: " + ob.GetType() +
                                    ". Please report this bug with this message to the development team.");
            }
            return(sb.ToString());
        }
Example #2
0
        public static string ConvertToSqlFormat(MySqlDataReader rdr, int colIndex, bool wrapStringWithSingleQuote, bool escapeStringSequence, MySqlColumn col)
        {
            object ob = rdr[colIndex];

            StringBuilder sb = new StringBuilder();

            if (ob == null || ob is System.DBNull)
            {
                sb.AppendFormat("NULL");
            }
            else if (ob is System.String)
            {
                string str = (string)ob;

                if (escapeStringSequence)
                    str = QueryExpress.EscapeStringSequence(str);

                if (wrapStringWithSingleQuote)
                    sb.AppendFormat("'");

                sb.Append(str);

                if (wrapStringWithSingleQuote)
                    sb.AppendFormat("'");
            }
            else if (ob is System.Boolean)
            {
                sb.AppendFormat(Convert.ToInt32(ob).ToString());
            }
            else if (ob is System.Byte[])
            {
                if (((byte[])ob).Length == 0)
                {
                    if (wrapStringWithSingleQuote)
                        return "''";
                    else
                        return "";
                }
                else
                {
                    sb.AppendFormat(CryptoExpress.ConvertByteArrayToHexString((byte[])ob));
                }
            }
            else if (ob is short)
            {
                sb.AppendFormat(((short)ob).ToString(_numberFormatInfo));
            }
            else if (ob is int)
            {
                sb.AppendFormat(((int)ob).ToString(_numberFormatInfo));
            }
            else if (ob is long)
            {
                sb.AppendFormat(((long)ob).ToString(_numberFormatInfo));
            }
            else if (ob is ushort)
            {
                sb.AppendFormat(((ushort)ob).ToString(_numberFormatInfo));
            }
            else if (ob is uint)
            {
                sb.AppendFormat(((uint)ob).ToString(_numberFormatInfo));
            }
            else if (ob is ulong)
            {
                sb.AppendFormat(((ulong)ob).ToString(_numberFormatInfo));
            }
            else if (ob is double)
            {
                sb.AppendFormat(((double)ob).ToString(_numberFormatInfo));
            }
            else if (ob is decimal)
            {
                sb.AppendFormat(((decimal)ob).ToString(_numberFormatInfo));
            }
            else if (ob is float)
            {
                sb.AppendFormat(((float)ob).ToString(_numberFormatInfo));
            }
            else if (ob is byte)
            {
                sb.AppendFormat(((byte)ob).ToString(_numberFormatInfo));
            }
            else if (ob is sbyte)
            {
                sb.AppendFormat(((sbyte)ob).ToString(_numberFormatInfo));
            }
            else if (ob is TimeSpan)
            {
                TimeSpan ts = (TimeSpan)ob;

                if (wrapStringWithSingleQuote)
                    sb.AppendFormat("'");

                sb.AppendFormat(ts.Hours.ToString().PadLeft(2, '0'));
                sb.AppendFormat(":");
                sb.AppendFormat(ts.Minutes.ToString().PadLeft(2, '0'));
                sb.AppendFormat(":");
                sb.AppendFormat(ts.Seconds.ToString().PadLeft(2, '0'));

                if (wrapStringWithSingleQuote)
                    sb.AppendFormat("'");
            }
            else if (ob is System.DateTime)
            {
                if (wrapStringWithSingleQuote)
                    sb.AppendFormat("'");

                sb.AppendFormat(((DateTime)ob).ToString("yyyy-MM-dd HH:mm:ss", _dateFormatInfo));

                if (col.TimeFractionLength > 0)
                {
                    sb.Append(".");
                    string _microsecond = rdr.GetMySqlDateTime(colIndex).Microsecond.ToString();
                    if (_microsecond.Length < col.TimeFractionLength)
                    {
                        _microsecond = _microsecond.PadLeft(col.TimeFractionLength, '0');
                    }
                    else if (_microsecond.Length > col.TimeFractionLength)
                    {
                        _microsecond = _microsecond.Substring(0, col.TimeFractionLength);
                    }
                    sb.Append(_microsecond.ToString().PadLeft(col.TimeFractionLength, '0'));
                }

                if (wrapStringWithSingleQuote)
                    sb.AppendFormat("'");
            }
            else if (ob is MySql.Data.Types.MySqlDateTime)
            {
                MySql.Data.Types.MySqlDateTime mdt = (MySql.Data.Types.MySqlDateTime)ob;

                if (mdt.IsNull)
                {
                    sb.AppendFormat("NULL");
                }
                else
                {
                    if (mdt.IsValidDateTime)
                    {
                        DateTime dtime = mdt.Value;

                        if (wrapStringWithSingleQuote)
                            sb.AppendFormat("'");

                        if (col.MySqlDataType == "datetime")
                            sb.AppendFormat(dtime.ToString("yyyy-MM-dd HH:mm:ss", _dateFormatInfo));
                        else if (col.MySqlDataType == "date")
                            sb.AppendFormat(dtime.ToString("yyyy-MM-dd", _dateFormatInfo));
                        else if (col.MySqlDataType == "time")
                            sb.AppendFormat(dtime.ToString("HH:mm:ss", _dateFormatInfo));
                        else
                            sb.AppendFormat(dtime.ToString("yyyy-MM-dd HH:mm:ss", _dateFormatInfo));

                        if(col.TimeFractionLength > 0)
                        {
                            sb.Append(".");
                            sb.Append(((MySql.Data.Types.MySqlDateTime)ob).Microsecond.ToString().PadLeft(col.TimeFractionLength, '0'));
                        }

                        if (wrapStringWithSingleQuote)
                            sb.AppendFormat("'");
                    }
                    else
                    {
                        if (wrapStringWithSingleQuote)
                            sb.AppendFormat("'");

                        if (col.MySqlDataType == "datetime")
                            sb.AppendFormat("0000-00-00 00:00:00");
                        else if (col.MySqlDataType == "date")
                            sb.AppendFormat("0000-00-00");
                        else if (col.MySqlDataType == "time")
                            sb.AppendFormat("00:00:00");
                        else
                            sb.AppendFormat("0000-00-00 00:00:00");

                        if (col.TimeFractionLength > 0)
                        {
                            sb.Append(".".PadRight(col.TimeFractionLength, '0'));
                        }

                        if (wrapStringWithSingleQuote)
                            sb.AppendFormat("'");
                    }
                }
            }
            else if (ob is System.Guid)
            {
                if (col.MySqlDataType == "binary(16)")
                {
                    sb.Append(CryptoExpress.ConvertByteArrayToHexString(((Guid)ob).ToByteArray()));
                }
                else if (col.MySqlDataType == "char(36)")
                {
                    if (wrapStringWithSingleQuote)
                        sb.AppendFormat("'");

                    sb.Append(ob);

                    if (wrapStringWithSingleQuote)
                        sb.AppendFormat("'");
                }
                else
                {
                    if (wrapStringWithSingleQuote)
                        sb.AppendFormat("'");

                    sb.Append(ob);

                    if (wrapStringWithSingleQuote)
                        sb.AppendFormat("'");
                }
            }
            else
            {
                throw new Exception("Unhandled data type. Current processing data type: " + ob.GetType().ToString() + ". Please report this bug with this message to the development team.");
            }
            return sb.ToString();
        }
Example #3
0
        public static string ConvertToSqlFormat(object ob, bool wrapStringWithSingleQuote, bool escapeStringSequence, MySqlColumn col)
        {
            StringBuilder sb = new StringBuilder();

            if (ob == null || ob is System.DBNull)
            {
                sb.AppendFormat("NULL");
            }
            else if (ob is System.String)
            {
                string str = (string)ob;

                if (escapeStringSequence)
                {
                    str = QueryExpress.EscapeStringSequence(str);
                }

                if (wrapStringWithSingleQuote)
                {
                    sb.AppendFormat("'");
                }

                sb.Append(str);

                if (wrapStringWithSingleQuote)
                {
                    sb.AppendFormat("'");
                }
            }
            else if (ob is System.Boolean)
            {
                sb.AppendFormat(Convert.ToInt32(ob).ToString());
            }
            else if (ob is System.Byte[])
            {
                if (((byte[])ob).Length == 0)
                {
                    if (wrapStringWithSingleQuote)
                    {
                        return("''");
                    }
                    else
                    {
                        return("");
                    }
                }
                else
                {
                    sb.AppendFormat(CryptoExpress.ConvertByteArrayToHexString((byte[])ob));
                }
            }
            else if (ob is short)
            {
                sb.AppendFormat(((short)ob).ToString(_numberFormatInfo));
            }
            else if (ob is int)
            {
                sb.AppendFormat(((int)ob).ToString(_numberFormatInfo));
            }
            else if (ob is long)
            {
                sb.AppendFormat(((long)ob).ToString(_numberFormatInfo));
            }
            else if (ob is ushort)
            {
                sb.AppendFormat(((ushort)ob).ToString(_numberFormatInfo));
            }
            else if (ob is uint)
            {
                sb.AppendFormat(((uint)ob).ToString(_numberFormatInfo));
            }
            else if (ob is ulong)
            {
                sb.AppendFormat(((ulong)ob).ToString(_numberFormatInfo));
            }
            else if (ob is double)
            {
                sb.AppendFormat(((double)ob).ToString(_numberFormatInfo));
            }
            else if (ob is decimal)
            {
                sb.AppendFormat(((decimal)ob).ToString(_numberFormatInfo));
            }
            else if (ob is float)
            {
                sb.AppendFormat(((float)ob).ToString(_numberFormatInfo));
            }
            else if (ob is byte)
            {
                sb.AppendFormat(((byte)ob).ToString(_numberFormatInfo));
            }
            else if (ob is sbyte)
            {
                sb.AppendFormat(((sbyte)ob).ToString(_numberFormatInfo));
            }
            else if (ob is TimeSpan)
            {
                TimeSpan ts = (TimeSpan)ob;

                if (wrapStringWithSingleQuote)
                {
                    sb.AppendFormat("'");
                }

                sb.AppendFormat(ts.Hours.ToString().PadLeft(2, '0'));
                sb.AppendFormat(":");
                sb.AppendFormat(ts.Minutes.ToString().PadLeft(2, '0'));
                sb.AppendFormat(":");
                sb.AppendFormat(ts.Seconds.ToString().PadLeft(2, '0'));

                if (wrapStringWithSingleQuote)
                {
                    sb.AppendFormat("'");
                }
            }
            else if (ob is System.DateTime)
            {
                if (wrapStringWithSingleQuote)
                {
                    sb.AppendFormat("'");
                }

                sb.AppendFormat(((DateTime)ob).ToString("yyyy-MM-dd HH:mm:ss", _dateFormatInfo));

                if (wrapStringWithSingleQuote)
                {
                    sb.AppendFormat("'");
                }
            }
            else if (ob is MySql.Data.Types.MySqlDateTime)
            {
                MySql.Data.Types.MySqlDateTime mdt = (MySql.Data.Types.MySqlDateTime)ob;

                if (mdt.IsNull)
                {
                    sb.AppendFormat("NULL");
                }
                else
                {
                    if (mdt.IsValidDateTime)
                    {
                        DateTime dtime = mdt.Value;

                        if (wrapStringWithSingleQuote)
                        {
                            sb.AppendFormat("'");
                        }

                        if (col.MySqlDataType == "datetime")
                        {
                            sb.AppendFormat(dtime.ToString("yyyy-MM-dd HH:mm:ss", _dateFormatInfo));
                        }
                        else if (col.MySqlDataType == "date")
                        {
                            sb.AppendFormat(dtime.ToString("yyyy-MM-dd", _dateFormatInfo));
                        }
                        else if (col.MySqlDataType == "time")
                        {
                            sb.AppendFormat(dtime.ToString("HH:mm:ss", _dateFormatInfo));
                        }
                        else
                        {
                            sb.AppendFormat(dtime.ToString("yyyy-MM-dd HH:mm:ss", _dateFormatInfo));
                        }

                        if (dtime.Millisecond > 0)
                        {
                            sb.Append(".");
                            sb.Append(dtime.Millisecond);
                        }

                        if (wrapStringWithSingleQuote)
                        {
                            sb.AppendFormat("'");
                        }
                    }
                    else
                    {
                        if (wrapStringWithSingleQuote)
                        {
                            sb.AppendFormat("'");
                        }

                        if (col.MySqlDataType == "datetime")
                        {
                            sb.AppendFormat("0000-00-00 00:00:00");
                        }
                        else if (col.MySqlDataType == "date")
                        {
                            sb.AppendFormat("0000-00-00");
                        }
                        else if (col.MySqlDataType == "time")
                        {
                            sb.AppendFormat("00:00:00");
                        }
                        else
                        {
                            sb.AppendFormat("0000-00-00 00:00:00");
                        }

                        if (wrapStringWithSingleQuote)
                        {
                            sb.AppendFormat("'");
                        }
                    }
                }
            }
            else if (ob is System.Guid)
            {
                if (col.MySqlDataType == "binary(16)")
                {
                    sb.Append(CryptoExpress.ConvertByteArrayToHexString(((Guid)ob).ToByteArray()));
                }
                else if (col.MySqlDataType == "char(36)")
                {
                    if (wrapStringWithSingleQuote)
                    {
                        sb.AppendFormat("'");
                    }

                    sb.Append(ob);

                    if (wrapStringWithSingleQuote)
                    {
                        sb.AppendFormat("'");
                    }
                }
                else
                {
                    if (wrapStringWithSingleQuote)
                    {
                        sb.AppendFormat("'");
                    }

                    sb.Append(ob);

                    if (wrapStringWithSingleQuote)
                    {
                        sb.AppendFormat("'");
                    }
                }
            }
            else
            {
                throw new Exception("Unhandled data type. Current processing data type: " + ob.GetType().ToString() + ". Please report this bug with this message to the development team.");
            }
            return(sb.ToString());
        }