/// <summary> /// 条件比较 /// </summary> private static bool GetTFilterOk(string where, int quoteIndex, string sign, Op op, MDataColumn mdc, out TFilter tFilter) { bool result = false; tFilter = null; int index = where.ToLower().IndexOf(sign, 0, quoteIndex > 0 ? quoteIndex : where.Length); if (index > 0) { string columnAName = where.Substring(0, index).Trim(); int columnAIndex = mdc.GetIndex(columnAName); string valueB = where.Substring(index + sign.Length).Trim(' ', '\'').Replace("''", "'"); if (op == Op.In || op == Op.NotIn) { valueB = ',' + valueB.TrimStart('(', ')').Replace("'", "") + ",";//去除单引号。 } int columnBIndex = -1; if (quoteIndex == 0 && mdc.Contains(valueB)) //判断右侧的是否列名。 { columnBIndex = mdc.GetIndex(valueB); } tFilter = new TFilter(Ao.None, columnAName, columnAIndex, op, valueB, columnBIndex); if (columnBIndex == -1 && !string.IsNullOrEmpty(Convert.ToString(valueB))) //右侧是值类型,转换值的类型。 { if (columnAIndex > -1 && DataType.GetGroup(mdc[columnAIndex].SqlType) == 3) //bool型 { switch (Convert.ToString(tFilter._valueB).ToLower()) { case "true": case "1": case "on": tFilter._valueB = true; break; case "false": case "0": case "": tFilter._valueB = false; break; default: tFilter._valueB = null; break; } } else { try { tFilter._valueB = ConvertTool.ChangeType(tFilter._valueB, columnAIndex > -1 ? typeof(string) : mdc[columnAIndex].ValueType); } catch { } } } result = true; } return(result); }
/// <summary> /// 单个条件 /// </summary> private static TFilter GetSingleTFilter(string where, MDataColumn mdc) { //id like 'a>b=c' //id>'a like b' where = where.TrimStart('(').TrimEnd(')').Trim(); int quoteIndex = where.IndexOf('\''); quoteIndex = quoteIndex == -1 ? 0 : quoteIndex; TFilter tFilter = null; foreach (KeyValuePair <string, Op> opItem in Ops) { if (GetTFilterOk(where, quoteIndex, opItem.Key, opItem.Value, mdc, out tFilter)) { break; } } return(tFilter); }
/// <summary> /// 多个条件 /// </summary> private static TFilter[] GetTFilter(object whereObj, MDataColumn mdc) { List <TFilter> tFilterList = new List <TFilter>(); string whereStr = SqlFormat.GetIFieldSql(whereObj); whereStr = SqlCreate.FormatWhere(whereStr, mdc, DalType.None, null); string lowerWhere = whereStr.ToLower(); string andSign = " and "; string orSign = " or "; int andIndex = IndexOf(lowerWhere, andSign, 0);// lowerWhere.IndexOf(andSign); int orIndex = IndexOf(lowerWhere, orSign, 0); TFilter filter = null; if (andIndex == -1 && orIndex == -1)//仅有一个条件 { filter = GetSingleTFilter(whereStr, mdc); if (filter != null) { tFilterList.Add(filter); } } else if (orIndex == -1) // 只有and条件 { int andStartIndex = 0; while (andIndex > -1) { filter = GetSingleTFilter(whereStr.Substring(andStartIndex, andIndex - andStartIndex), mdc); if (filter != null) { if (andStartIndex > 0) { filter._Ao = Ao.And; } tFilterList.Add(filter); } andStartIndex = andIndex + andSign.Length; andIndex = IndexOf(lowerWhere, andSign, andStartIndex + 1); } filter = GetSingleTFilter(whereStr.Substring(andStartIndex), mdc); if (filter != null) { filter._Ao = Ao.And; tFilterList.Add(filter); } } else if (andIndex == -1) //只有or 条件 { int orStartIndex = 0; while (orIndex > -1) { filter = GetSingleTFilter(whereStr.Substring(orStartIndex, orIndex - orStartIndex), mdc); if (filter != null) { if (orStartIndex > 0) { filter._Ao = Ao.Or; } tFilterList.Add(filter); } orStartIndex = orIndex + orSign.Length; orIndex = IndexOf(lowerWhere, orSign, orStartIndex + 1); } filter = GetSingleTFilter(whereStr.Substring(orStartIndex), mdc); if (filter != null) { filter._Ao = Ao.Or; tFilterList.Add(filter); } } else //有and 又有 or { bool isAnd = andIndex < orIndex; bool lastAnd = isAnd; int andOrIndex = isAnd ? andIndex : orIndex;//最小的,前面的先处理 int andOrStartIndex = 0; while (andOrIndex > -1) { filter = GetSingleTFilter(whereStr.Substring(andOrStartIndex, andOrIndex - andOrStartIndex), mdc); if (filter != null) { if (andOrStartIndex > 0) { filter._Ao = lastAnd ? Ao.And : Ao.Or; } tFilterList.Add(filter); } andOrStartIndex = andOrIndex + (isAnd ? andSign.Length : orSign.Length); if (isAnd) { andIndex = IndexOf(lowerWhere, andSign, andOrStartIndex + 1); } else { orIndex = IndexOf(lowerWhere, orSign, andOrStartIndex + 1); } lastAnd = isAnd; if (andIndex == -1) { isAnd = false; andOrIndex = orIndex; } else if (orIndex == -1) { isAnd = true; andOrIndex = andIndex; } else { isAnd = andIndex < orIndex; andOrIndex = isAnd ? andIndex : orIndex;//最小的,前面的先处理 } } filter = GetSingleTFilter(whereStr.Substring(andOrStartIndex), mdc); if (filter != null) { filter._Ao = lastAnd ? Ao.And : Ao.Or; tFilterList.Add(filter); } } // string firstFilter=whereStr.su return(tFilterList.ToArray()); }
/// <summary> /// 多个条件 /// </summary> private static List <TFilter> GetTFilter(object whereObj, MDataColumn mdc, out List <TFilter> group2) { group2 = new List <TFilter>(); List <TFilter> tFilterList = new List <TFilter>(); string whereStr = SqlFormat.GetIFieldSql(whereObj); whereStr = SqlCreate.FormatWhere(whereStr, mdc, DataBaseType.None, null); string lowerWhere = whereStr.ToLower(); string andSign = " and "; string orSign = " or "; int andIndex = IndexOf(lowerWhere, andSign, 0);// lowerWhere.IndexOf(andSign); int orIndex = IndexOf(lowerWhere, orSign, 0); TFilter filter = null; if (andIndex == -1 && orIndex == -1)//仅有一个条件 { filter = GetSingleTFilter(whereStr, mdc); if (filter != null) { tFilterList.Add(filter); } } else if (orIndex == -1) // 只有and条件 { int andStartIndex = 0; while (andIndex > -1) { filter = GetSingleTFilter(whereStr.Substring(andStartIndex, andIndex - andStartIndex), mdc); if (filter != null) { if (andStartIndex > 0) { filter._Ao = Ao.And; } tFilterList.Add(filter); } andStartIndex = andIndex + andSign.Length; andIndex = IndexOf(lowerWhere, andSign, andStartIndex + 1); } filter = GetSingleTFilter(whereStr.Substring(andStartIndex), mdc); if (filter != null) { filter._Ao = Ao.And; tFilterList.Add(filter); } } else if (andIndex == -1) //只有or 条件 { int orStartIndex = 0; while (orIndex > -1) { filter = GetSingleTFilter(whereStr.Substring(orStartIndex, orIndex - orStartIndex), mdc); if (filter != null) { if (orStartIndex > 0) { filter._Ao = Ao.Or; } tFilterList.Add(filter); } orStartIndex = orIndex + orSign.Length; orIndex = IndexOf(lowerWhere, orSign, orStartIndex + 1); } filter = GetSingleTFilter(whereStr.Substring(orStartIndex), mdc); if (filter != null) { filter._Ao = Ao.Or; tFilterList.Add(filter); } } else //有and 又有 or { bool isAnd = andIndex < orIndex; bool lastAnd = isAnd; int andOrIndex = isAnd ? andIndex : orIndex;//最小的,前面的先处理 int andOrStartIndex = 0; bool needGroup2 = isAnd;//如果是and开头,则分成两组 while (andOrIndex > -1) { filter = GetSingleTFilter(whereStr.Substring(andOrStartIndex, andOrIndex - andOrStartIndex), mdc); if (filter != null) { if (andOrStartIndex > 0) { filter._Ao = lastAnd ? Ao.And : Ao.Or; } tFilterList.Add(filter); } andOrStartIndex = andOrIndex + (isAnd ? andSign.Length : orSign.Length); if (isAnd) { andIndex = IndexOf(lowerWhere, andSign, andOrStartIndex + 1); } else { orIndex = IndexOf(lowerWhere, orSign, andOrStartIndex + 1); } lastAnd = isAnd; if (andIndex == -1) { isAnd = false; andOrIndex = orIndex; } else if (orIndex == -1) { isAnd = true; andOrIndex = andIndex; } else { isAnd = andIndex < orIndex; andOrIndex = isAnd ? andIndex : orIndex;//最小的,前面的先处理 } } filter = GetSingleTFilter(whereStr.Substring(andOrStartIndex), mdc); if (filter != null) { filter._Ao = lastAnd ? Ao.And : Ao.Or; tFilterList.Add(filter); } if (tFilterList.Count > 2 && needGroup2) { int okflag = -1; for (int i = 0; i < tFilterList.Count; i++) { if (okflag == -1 && tFilterList[i]._Ao == Ao.Or) { i--;//返回上一个索引1,2,3 okflag = i; } if (okflag != -1) { group2.Add(tFilterList[i]); } } tFilterList.RemoveRange(okflag, tFilterList.Count - okflag); } } // string firstFilter=whereStr.su return(tFilterList); }
/// <summary> /// 条件比较 /// </summary> private static bool GetTFilterOk(string where, int quoteIndex, string sign, Op op, MDataColumn mdc, out TFilter tFilter) { bool result = false; tFilter = null; int index = where.ToLower().IndexOf(sign, 0, quoteIndex > 0 ? quoteIndex : where.Length); if (index > 0) { string columnAName = where.Substring(0, index).Trim(); int columnAIndex = mdc.GetIndex(columnAName); string valueB = where.Substring(index + sign.Length).Trim(' ', '\'').Replace("''", "'"); if (op == Op.In || op == Op.NotIn) { valueB = ',' + valueB.TrimStart('(', ')').Replace("'", "") + ",";//去除单引号。 } int columnBIndex = -1; if (quoteIndex == 0 && mdc.Contains(valueB)) //判断右侧的是否列名。 { columnBIndex = mdc.GetIndex(valueB); } tFilter = new TFilter(Ao.None, columnAName, columnAIndex, op, valueB, columnBIndex); if (columnBIndex == -1 && !string.IsNullOrEmpty(Convert.ToString(valueB)))//右侧是值类型,转换值的类型。 { if (columnAIndex > -1 && DataType.GetGroup(mdc[columnAIndex].SqlType) == 3)//bool型 { switch (Convert.ToString(tFilter._valueB).ToLower()) { case "true": case "1": case "on": tFilter._valueB = true; break; case "false": case "0": case "": tFilter._valueB = false; break; default: tFilter._valueB = null; break; } } else { try { tFilter._valueB = StaticTool.ChangeType(tFilter._valueB, columnAIndex > -1 ? typeof(string) : mdc[columnAIndex].ValueType); } catch { } } } result = true; } return result; }