public static string ConvertToSqlFormat(object ob, bool wrapStringWithSingleQuote, bool escapeStringSequence, MySqlColumn col, BlobDataExportMode blobExportMode) { 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 { if (blobExportMode == BlobDataExportMode.HexString) { sb.AppendFormat(CryptoExpress.ConvertByteArrayToHexString((byte[])ob)); } else if (blobExportMode == BlobDataExportMode.BinaryChar) { if (wrapStringWithSingleQuote) { sb.Append("'"); } foreach (byte b in ((byte[])ob)) { char ch = (char)b; escape_string(sb, ch); } if (wrapStringWithSingleQuote) { sb.Append("'"); } } } } 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(((int)ts.TotalHours).ToString().PadLeft(2, '0')); sb.AppendFormat(":"); sb.AppendFormat(ts.Duration().Minutes.ToString().PadLeft(2, '0')); sb.AppendFormat(":"); sb.AppendFormat(ts.Duration().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 = ((DateTime)ob).ToString("".PadLeft(col.TimeFractionLength, 'f')); sb.Append(_microsecond); } 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("{0}:{1}:{2}", mdt.Hour, mdt.Minute, mdt.Second); } //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()); }
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).Millisecond.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).Millisecond.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()); }
Dictionary <string, string> Export_RearrangeTableOrderForForeignKey(Dictionary <string, string> dic1) { System.Data.DataTable dtForeignKeyTables = QueryExpress.GetTable(Command, string.Format("select table_name, referenced_table_name from information_schema.key_column_usage where constraint_schema='{0}' and referenced_table_name IS NOT NULL;", QueryExpress.EscapeStringSequence(_database.Name))); if (dtForeignKeyTables.Rows.Count == 0) { return(dic1); } Dictionary <string, string> dic2 = new Dictionary <string, string>(); bool requireLoop = true; while (requireLoop) { requireLoop = false; foreach (var kv in dic1) { if (dic2.ContainsKey(kv.Key)) { continue; } bool handledInForeignKeySearch = false; for (int i = 0; i < dtForeignKeyTables.Rows.Count; i++) { string _tb_name = dtForeignKeyTables.Rows[i]["table_name"] + ""; string _fk_tb_name = dtForeignKeyTables.Rows[i]["referenced_table_name"] + ""; if (kv.Key == _tb_name) { handledInForeignKeySearch = true; if (dic2.ContainsKey(_fk_tb_name)) { dic2[kv.Key] = kv.Value; requireLoop = true; } continue; } } if (!handledInForeignKeySearch) { dic2[kv.Key] = kv.Value; requireLoop = true; } } } return(dic2); }