public static String BuildSql(FilterEntity entity) { if ((entity.LeftPart == null) || (entity.RightPart == null)) { return(String.Empty); } StringBuilder builder = new StringBuilder(); if ((entity.Operate == CommonEnumerators.FilterOperate.And) || (entity.Operate == CommonEnumerators.FilterOperate.Or)) { builder.Append("("); FilterEntity leftPart = entity.LeftPart as FilterEntity; if (leftPart != null) { builder.Append(BuildSql(leftPart)); } FilterEntity rightPart = entity.RightPart as FilterEntity; if (rightPart != null) { builder.Append(CommonEnumerators.GetOperate(entity.Operate)); builder.Append(BuildSql(rightPart)); } builder.Append(")"); } else { builder.Append(BuildSql(entity.LeftPart.ToString(), entity.RightPart, entity.Operate)); } return(builder.ToString()); }
private static String BuildSql(String leftPart, object rightPart, CommonEnumerators.FilterOperate operate) { String format = " [{0}] {1} {2} "; String str2 = CommonEnumerators.GetOperate(operate); switch (rightPart.GetType().FullName) { case "System.String": { String str3 = rightPart.ToString().Replace("'", "").Replace("%", "").Trim(); if (operate == CommonEnumerators.FilterOperate.StartWith) { return(String.Format(format, leftPart, str2, String.Format("'{0}%'", str3))); } if (operate == CommonEnumerators.FilterOperate.EndWith) { return(String.Format(format, leftPart, str2, String.Format("'%{0}'", str3))); } if ((operate == CommonEnumerators.FilterOperate.Contains) || (operate == CommonEnumerators.FilterOperate.NotContains)) { return(String.Format(format, leftPart, str2, String.Format("'%{0}%'", str3))); } if ((operate == CommonEnumerators.FilterOperate.GtIndicator) || (operate == CommonEnumerators.FilterOperate.LtIndicator)) { return(String.Format(format, leftPart, str2, String.Format("[{0}]", rightPart))); } return(String.Format(format, leftPart, str2, String.Format("'{0}'", str3))); } case "System.DateTime": { DateTime time = Convert.ToDateTime(rightPart); return(String.Format(format, leftPart, str2, String.Format("'{0}'", time.ToString("yyyy-MM-dd")))); } } return(String.Format(format, leftPart, str2, rightPart)); }