using System; using System.Collections; using IwbZero.IwbDataQuery; using IwbZero.ToolCommon.StringModel; namespace IwbZero.IwbBase { public class Condition { private static ArrayList _supportedOps; public string Op; public string Source; public string Target; public Condition(string pcSource, string pcOp, string pcTarget) { Source = pcSource; Op = pcOp; Target = pcTarget; } public static Condition ConvertFrom(string pcString) { Condition condition = new Condition("", "", ""); ArrayList supportedOps = GetSupportedOps(); Array array = pcString.StrToArrayEx( "="); if (array.Length == 2) { string str = array.GetValue(1).ToString(); if (str.IndexOf('*') >= 0) { pcString = array.GetValue(0) + " like " + str; } else { char ch = ' '; if (str.Length > 0) { ch = str[0]; } if (condition.ReservedChars.IndexOf(ch) < 0) { pcString = array.GetValue(0) + "=" + str; } else { pcString = array.GetValue(0) + array.GetValue(1).ToString(); } } } foreach (IwbIdName name in supportedOps) { int index = pcString.IndexOf(name.Id, StringComparison.Ordinal); if (index > 0) { condition.Source = pcString.Substring(0, index).Trim(); condition.Op = name.Id; if (pcString.Length > (index + name.Id.Length)) { condition.Target = pcString.Substring(index + name.Id.Length).Trim(); } return condition; } } return condition; } public static ArrayList GetSupportedOps() { return _supportedOps ?? (_supportedOps = new ArrayList { new IwbIdName(">=", "GreaterEqual"), new IwbIdName("<=", "SmallerEqual"), new IwbIdName("!=", "Not Equal"), new IwbIdName("=", "Equal"), new IwbIdName(">", "Greater"), new IwbIdName("<", "Smaller"), new IwbIdName(" like ", "Like") }); } public string ToMappingString() { if ((Op == "like") && (Target.IndexOf('*') < 0)) { return ("[" + Source + "]=[*" + Target + "*]"); } if ((Op == "=") || ((Op == "like") && (Target.IndexOf('*') >= 0))) { return ("[" + Source + "]=[" + Target + "]"); } return ("[" + Source + "]=[" + Op + Target + "]"); } public override string ToString() { return (Source + Op + Target); } public string TranslateIntoSql(string pcTableId, int piConnectionId) { return ""; } public string TranslateIntoSql(string pcTableId, string pcServerType) { return ""; } public string ReservedChars { get; } = "<>=*"; public static string ConditionMappingListToMappingString(ArrayList poMappings) { string pcSource = ""; foreach (Condition condition in poMappings) { if (condition.Source != "") { pcSource = pcSource.AddStr( condition.ToMappingString()); } } return pcSource; } public static ArrayList ConditionMappingToArrayList(string pcMapping) { ArrayList list = new ArrayList(); Array array = pcMapping.IndexOf(" and ", StringComparison.Ordinal) > 0 ? pcMapping.StrToArrayEx( " and ") : pcMapping.StrToArray(); foreach (string str in array) { Condition condition = ConvertFrom(str); if ((condition.Source != "") && (condition.Op != "")) { list.Add(condition); } } return list; } public static string ConditionMappingToConditionString(string pcMapping) { string str = ""; foreach (Condition condition in ConditionMappingToArrayList(pcMapping)) { if (condition.Source != "") { string str2 = condition.ToString(); if (str2 != "") { str = str + ((str == "") ? "" : " and ") + $"({str2})"; } } } return str; } } }