public string ToString(IBaseDataSpecial cfg) { StringBuilder sql = new StringBuilder(); IUpatedataInfo _row = this.FilterRow; foreach (string f in _row.GetUpateFields()) { object v = _row.GetValueByName(f); if (v == null || v == DBNull.Value) { switch (cfg.FieldMask) { case EM_Safe_Field_MASK.MySQLMode: sql.AppendFormat(" and isNull(`{0}`)=1", f); continue; case EM_Safe_Field_MASK.MsSQLMode: sql.AppendFormat(" and [{0}] is null", f); continue; default: throw new Exception("未知的编码方式:" + cfg.FieldMask.ToString()); } } if (v is int) { sql.AppendFormat(" and {0}={1}", cfg.FieldEncode(f), v); } else { sql.AppendFormat(" and {0}='{1}'", cfg.FieldEncode(f), cfg.SqlEncode(v.ToString())); } } if (_andFilter != null && _andFilter.Count != 0) { sql.AppendFormat(" and {0}", DataFilter.FiltersToString(_andFilter, "and", cfg)); } if (_orFilter != null && _orFilter.Count != 0) { sql.AppendFormat("\n or {0}", DataFilter.FiltersToString(_orFilter, "or", cfg)); } if (sql.Length > 4) { string h = sql.ToString().Substring(0, 4); if (h == " and" || h == "\n or") { sql.Remove(0, 4); } } return(sql.ToString()); }
public virtual string ToString(IBaseDataSpecial cfg) { StringBuilder ret = new StringBuilder(); string value; if (_value is int || _value is long || _value is short || _value is byte) { value = _value.ToString(); } else if (_value is bool) { value = (bool)_value ? "1" : "0"; } else if (_value is Enum) { value = ((Enum)_value).ToString("D"); } else if (_value is DateTime) { value = "'" + _value.ToString() + "'"; } else if (_value is IEnumerable <int> || _value is IEnumerable <string> ) { StringBuilder t = new StringBuilder(); int arrType = 0; if (_value is IEnumerable <Enum> ) { arrType = 2; } else if (_value is IEnumerable <int> ) { arrType = 1; } else if (_value is string[]) { arrType = 3; } foreach (object s in (System.Collections.IEnumerable)_value) { switch (arrType) { case 0: t.AppendFormat(",'{0}'", s); break; //unkonw case 1: t.AppendFormat(",{0}", s); break; //digi case 2: t.AppendFormat(",{0}", (int)s); break; //enum case 3: t.AppendFormat(",'{0}'", cfg.SqlEncode((string)s)); break; //string default: throw new Exception("未知数据类别"); } } if (t.Length == 0) { value = string.Empty; } else { t.Remove(0, 1); value = t.ToString(); } } else { if (_value == null) { value = "''"; } else { value = "'" + cfg.SqlEncode(_value.ToString()) + "'"; } } string op; switch (_operator) { case EM_DataFiler_Operator.Equal: op = "="; break; case EM_DataFiler_Operator.In: op = "in"; break; case EM_DataFiler_Operator.Less: op = "<"; break; case EM_DataFiler_Operator.LessOrEqual: op = "<="; break; case EM_DataFiler_Operator.Like: op = "like"; break; case EM_DataFiler_Operator.More: op = ">"; break; case EM_DataFiler_Operator.MoreOrEqual: op = ">="; break; case EM_DataFiler_Operator.Not_In: op = "not in"; break; case EM_DataFiler_Operator.Not_Equal: op = "<>"; break; default: throw new Exception("未知操作符!"); } if (NullToValue != null) { if (_operator == EM_DataFiler_Operator.In || _operator == EM_DataFiler_Operator.Not_In) { ret.AppendFormat("{0} {1} ({2})", cfg.funcNullToValue(_field, NullToValue), op, value); } else { ret.AppendFormat("{0} {1} {2}", cfg.funcNullToValue(_field, NullToValue), op, value); } } else { if (_operator == EM_DataFiler_Operator.In || _operator == EM_DataFiler_Operator.Not_In) { ret.AppendFormat("{0} {1} ({2})", cfg.FieldEncode(_field), op, value); } else { ret.AppendFormat("{0} {1} {2}", cfg.FieldEncode(_field), op, value); } } if (_andFilter != null && _andFilter.Count != 0) { ret.Append("\n and "); ret.Append(FiltersToString(_andFilter, "and", cfg)); } if (_orFilter != null && _orFilter.Count != 0) { ret.Append("\n or "); ret.Append(FiltersToString(_orFilter, "or", cfg)); ret.Insert(0, "("); ret.Append(")"); } return(ret.ToString()); }