Пример #1
0
        private SelectBuilder Append(Expression property, FunctionOperatorType op, object value, string splitValue)
        {
            var fuctionName = string.Empty;

            switch (op)
            {
            case FunctionOperatorType.Sum:
                fuctionName = "SUM";
                break;

            case FunctionOperatorType.Count:
                fuctionName = "COUNT";
                break;
            }

            if (property != null && value != null)
            {
                if (!__sql.IsEmpty_())
                {
                    __sql.Append(splitValue);
                }

                __sql.Append(fuctionName + "(");
                __sql.Append(__db.GetTableAndColumnName(property));
                __sql.Append(")");
            }

            return(this);
        }
Пример #2
0
        private static FunctionOperator createOperandProperty(string property, FunctionOperatorType operatorType)
        {
            CriteriaOperatorCollection operands        = new CriteriaOperatorCollection();
            OperandProperty            propertyOperand = new OperandProperty(property);

            operands.Add(propertyOperand);
            return(new FunctionOperator(operatorType, operands));
        }
Пример #3
0
        private static FunctionOperator createOperandValue(object requiredValue, FunctionOperatorType operatorType)
        {
            CriteriaOperatorCollection operands = new CriteriaOperatorCollection();
            OperandValue operandValue           = new OperandValue(requiredValue);

            operands.Add(operandValue);
            return(new FunctionOperator(operatorType, operands));
        }
Пример #4
0
        static string GetFunctionName(FunctionOperatorType operatorType)
        {
            switch (operatorType)
            {
            case FunctionOperatorType.Concat: return("Concat");

            case FunctionOperatorType.Iif: return("Iif");

            case FunctionOperatorType.IsNull: return("Is null");

            default: return(operatorType.ToString());
            }
        }
Пример #5
0
 protected override String GetFunctionText(FunctionOperatorType operatorType)
 {
     if (operatorType == FunctionOperatorType.Upper)
     {
         return "ToUpper";
     }
     else if (operatorType == FunctionOperatorType.Lower)
     {
         return "ToLower";
     }
     else
     {
         return base.GetFunctionText(operatorType);
     }
 }
        private static FunctionOperatorType GetOperatorTypeByMethodName(string methodName)
        {
            FunctionOperatorType result = FunctionOperatorType.Custom;

            if (CriteriaOperator.GetCustomFunction(methodName) != null)
            {
                return(result);
            }
            if (Enum.TryParse <FunctionOperatorType>(methodName, out result))
            {
                return(result);
            }
            string msg = string.Format(CultureInfo.CurrentCulture, Resources.MethodNotSupportedException, methodName);

            throw new NotSupportedException(msg);
        }
        //public override string FormatSelect(string selectedPropertiesSql, string fromSql, string whereSql, string orderBySql, string groupBySql, string havingSql, int topSelectedRecords)
        //{
        //    return base.FormatSelect(selectedPropertiesSql, fromSql, whereSql, orderBySql, groupBySql, havingSql, topSelectedRecords);
        //}

        //public override string FormatSelect(string selectedPropertiesSql, string fromSql, string whereSql, string orderBySql, string groupBySql, string havingSql, int skipSelectedRecords, int topSelectedRecords)
        //{
        //    return base.FormatSelect(selectedPropertiesSql, fromSql, whereSql, orderBySql, groupBySql, havingSql, skipSelectedRecords, topSelectedRecords);
        //}

        public override string FormatFunction(ProcessParameter processParameter, FunctionOperatorType operatorType, params object[] operands)
        {
            switch (operatorType)
            {
            case FunctionOperatorType.StartsWith:
            case FunctionOperatorType.Contains:
                var secondOperand = operands[1];
                if (((OperandValue)secondOperand)?.Value is string)
                {
                    var operandString = (string)((OperandValue)secondOperand).Value;
                    var prefix        = operatorType == FunctionOperatorType.Contains ? "%" : "";
                    return(string.Format(CultureInfo.InvariantCulture, "({0} ilike {1})", processParameter(operands[0]), processParameter(new ConstantValue($"{prefix}{operandString}%"))));;
                }
                break;
            }
            return(base.FormatFunction(processParameter, operatorType, operands));
        }
Пример #8
0
 public override string FormatFunction(FunctionOperatorType operatorType, params string[] operands) {
     if (operatorType == FunctionOperatorType.Custom) {
         if (operands[0] == "GetDayAndMonth") {
             const string format = "reverse(substring(reverse('0'+rtrim(cast(DATEPART({1}, {0}) as CHAR(2)))),0,3))";
             string formatMonth = string.Format(format, operands[1], "month");
             string formatDay = string.Format(format, operands[1], "day");
             return string.Format("{0}+'/'+{1}", formatDay, formatMonth);
         }
         if (operands.Length == 3) {
             string format = String.Format("{0}({1}, {2})", operands[0], operands[1], operands[2]);
             return format;
         }
         if (operands.Length == 2) {
             string format = String.Format("{0}({1})", operands[0], operands[1]);
             return format;
         }
     }
     return base.FormatFunction(operatorType, operands);
 }
Пример #9
0
        static string GetFunctionName(FunctionOperatorType operatorType)
        {
            switch (operatorType)
            {
            case FunctionOperatorType.IsNull:
                return("is null");

            case FunctionOperatorType.StartsWith:
                return("StartsWith");

            case FunctionOperatorType.EndsWith:
                return("EndsWith");

            case FunctionOperatorType.Contains:
                return("Contains");

            default:
                return(operatorType.ToString());
            }
        }
Пример #10
0
 public override string FormatFunction(FunctionOperatorType operatorType, params string[] operands)
 {
     if (operatorType == FunctionOperatorType.Custom)
     {
         if (operands[0] == "GetDayAndMonth")
         {
             const string format      = "reverse(substring(reverse('0'+rtrim(cast(DATEPART({1}, {0}) as CHAR(2)))),0,3))";
             string       formatMonth = string.Format(format, operands[1], "month");
             string       formatDay   = string.Format(format, operands[1], "day");
             return(string.Format("{0}+'/'+{1}", formatDay, formatMonth));
         }
         if (operands.Length == 3)
         {
             string format = String.Format("{0}({1}, {2})", operands[0], operands[1], operands[2]);
             return(format);
         }
         if (operands.Length == 2)
         {
             string format = String.Format("{0}({1})", operands[0], operands[1]);
             return(format);
         }
     }
     return(base.FormatFunction(operatorType, operands));
 }
Пример #11
0
 static CriteriaOperator MakeTypicalOutlookInterval(CriteriaOperator op, FunctionOperatorType lowerBound, FunctionOperatorType upperBound)
 {
     return op >= new FunctionOperator(lowerBound) & op < new FunctionOperator(upperBound);
 }
Пример #12
0
 void TestParseFunctionArgs(string criteria, FunctionOperatorType type, CriteriaOperator[] operands)
 {
     FunctionOperator fo = (FunctionOperator)CriteriaOperator.Parse(criteria);
     Assert.AreEqual(type, fo.OperatorType);
     Assert.AreEqual(operands.Length, fo.Operands.Count);
     for (int i = 0; i < operands.Length; i++) {
         Assert.AreEqual(operands[i], fo.Operands[i]);
     }
 }
Пример #13
0
 public static DateTime EvaluateLocalDateTime(FunctionOperatorType type)
 {
     DateTime now = DateTime.Now;
     switch(type) {
         case FunctionOperatorType.LocalDateTimeThisYear:
             return new DateTime(now.Year, 1, 1);
         case FunctionOperatorType.LocalDateTimeThisMonth:
             return new DateTime(now.Year, now.Month, 1);
         case FunctionOperatorType.LocalDateTimeLastWeek:
             return GetWeekStart(now).AddDays(-7);
         case FunctionOperatorType.LocalDateTimeThisWeek:
             return GetWeekStart(now);
         case FunctionOperatorType.LocalDateTimeYesterday:
             return now.Date.AddDays(-1);
         case FunctionOperatorType.LocalDateTimeToday:
             return now.Date;
         case FunctionOperatorType.LocalDateTimeNow:
             return now;
         case FunctionOperatorType.LocalDateTimeTomorrow:
             return now.Date.AddDays(1);
         case FunctionOperatorType.LocalDateTimeDayAfterTomorrow:
             return now.Date.AddDays(2);
         case FunctionOperatorType.LocalDateTimeNextWeek:
             return GetWeekStart(now).AddDays(7);
         case FunctionOperatorType.LocalDateTimeTwoWeeksAway:
             return GetWeekStart(now).AddDays(14);
         case FunctionOperatorType.LocalDateTimeNextMonth:
             return new DateTime(now.Year, now.Month, 1).AddMonths(1);
         case FunctionOperatorType.LocalDateTimeNextYear:
             return new DateTime(now.Year + 1, 1, 1);
         default:
             throw new InvalidOperationException("theOperator.OperatorType is not LocalDateTime* or internal error");
     }
 }
Пример #14
0
 public void CheckFunctionArgumentsCount(FunctionOperatorType functionType, int argumentsCount)
 {
     if ((functionType == FunctionOperatorType.Custom || functionType == FunctionOperatorType.Concat) && argumentsCount > 0) return;
     switch (argumentsCount) {
         case 0:
             switch (functionType) {
                 case FunctionOperatorType.LocalDateTimeDayAfterTomorrow:
                 case FunctionOperatorType.LocalDateTimeLastWeek:
                 case FunctionOperatorType.LocalDateTimeNextMonth:
                 case FunctionOperatorType.LocalDateTimeNextWeek:
                 case FunctionOperatorType.LocalDateTimeNextYear:
                 case FunctionOperatorType.LocalDateTimeNow:
                 case FunctionOperatorType.LocalDateTimeThisMonth:
                 case FunctionOperatorType.LocalDateTimeThisWeek:
                 case FunctionOperatorType.LocalDateTimeThisYear:
                 case FunctionOperatorType.LocalDateTimeToday:
                 case FunctionOperatorType.LocalDateTimeTomorrow:
                 case FunctionOperatorType.LocalDateTimeTwoWeeksAway:
                 case FunctionOperatorType.LocalDateTimeYesterday:
                 case FunctionOperatorType.Now:
                 case FunctionOperatorType.UtcNow:
                 case FunctionOperatorType.Today:
                 case FunctionOperatorType.Rnd:
                     return;
             }
             break;
         case 1:
             switch (functionType) {
                 case FunctionOperatorType.IsNullOrEmpty:
                 case FunctionOperatorType.Trim:
                 case FunctionOperatorType.Upper:
                 case FunctionOperatorType.Lower:
                 case FunctionOperatorType.Len:
                 case FunctionOperatorType.IsOutlookIntervalBeyondThisYear:
                 case FunctionOperatorType.IsOutlookIntervalEarlierThisMonth:
                 case FunctionOperatorType.IsOutlookIntervalEarlierThisWeek:
                 case FunctionOperatorType.IsOutlookIntervalEarlierThisYear:
                 case FunctionOperatorType.IsOutlookIntervalLastWeek:
                 case FunctionOperatorType.IsOutlookIntervalLaterThisMonth:
                 case FunctionOperatorType.IsOutlookIntervalLaterThisWeek:
                 case FunctionOperatorType.IsOutlookIntervalLaterThisYear:
                 case FunctionOperatorType.IsOutlookIntervalNextWeek:
                 case FunctionOperatorType.IsOutlookIntervalPriorThisYear:
                 case FunctionOperatorType.IsOutlookIntervalToday:
                 case FunctionOperatorType.IsOutlookIntervalTomorrow:
                 case FunctionOperatorType.IsOutlookIntervalYesterday:
                 case FunctionOperatorType.Ascii:
                 case FunctionOperatorType.Char:
                 case FunctionOperatorType.ToStr:
                 case FunctionOperatorType.Reverse:
                 case FunctionOperatorType.Abs:
                 case FunctionOperatorType.Sqr:
                 case FunctionOperatorType.Cos:
                 case FunctionOperatorType.Sin:
                 case FunctionOperatorType.Atn:
                 case FunctionOperatorType.Exp:
                 case FunctionOperatorType.Log:
                 case FunctionOperatorType.Log10:
                 case FunctionOperatorType.Tan:
                 case FunctionOperatorType.Sign:
                 case FunctionOperatorType.Round:
                 case FunctionOperatorType.Ceiling:
                 case FunctionOperatorType.Floor:
                 case FunctionOperatorType.Asin:
                 case FunctionOperatorType.Acos:
                 case FunctionOperatorType.Cosh:
                 case FunctionOperatorType.Sinh:
                 case FunctionOperatorType.Tanh:
                 case FunctionOperatorType.GetDate:
                 case FunctionOperatorType.GetDay:
                 case FunctionOperatorType.GetDayOfWeek:
                 case FunctionOperatorType.GetDayOfYear:
                 case FunctionOperatorType.GetHour:
                 case FunctionOperatorType.GetMilliSecond:
                 case FunctionOperatorType.GetMinute:
                 case FunctionOperatorType.GetMonth:
                 case FunctionOperatorType.GetSecond:
                 case FunctionOperatorType.GetTimeOfDay:
                 case FunctionOperatorType.GetYear:
                     return;
             }
             break;
         case 2:
             switch (functionType) {
                 case FunctionOperatorType.Substring:
                 case FunctionOperatorType.Log:
                 case FunctionOperatorType.Power:
                 case FunctionOperatorType.Atn2:
                 case FunctionOperatorType.BigMul:
                 case FunctionOperatorType.PadLeft:
                 case FunctionOperatorType.PadRight:
                 case FunctionOperatorType.AddDays:
                 case FunctionOperatorType.AddHours:
                 case FunctionOperatorType.AddMilliSeconds:
                 case FunctionOperatorType.AddMinutes:
                 case FunctionOperatorType.AddMonths:
                 case FunctionOperatorType.AddSeconds:
                 case FunctionOperatorType.AddTicks:
                 case FunctionOperatorType.AddTimeSpan:
                 case FunctionOperatorType.AddYears:
                 case FunctionOperatorType.CharIndex:
                     return;
             }
             break;
         case 3:
             switch (functionType) {
                 case FunctionOperatorType.Substring:
                 case FunctionOperatorType.Iif:
                 case FunctionOperatorType.PadLeft:
                 case FunctionOperatorType.PadRight:
                 case FunctionOperatorType.CharIndex:
                 case FunctionOperatorType.Insert:
                 case FunctionOperatorType.Remove:
                 case FunctionOperatorType.Replace:
                     return;
             }
             break;
         case 4:
             if (functionType == FunctionOperatorType.CharIndex) return;
             break;
     }
     YYError("Wrong arguments count ({0}). Function - '{1}'.", argumentsCount, functionType.ToString());
 }
Пример #15
0
 public FunctionOperator(CriteriaOperator[] fOperands, FunctionOperatorType fOperatorType)
 {
     Operands     = fOperands;
     OperatorType = fOperatorType;
 }
Пример #16
0
 void TestParseFunction(FunctionOperatorType type, int minArgs, int maxArgs)
 {
     TestParseFunction(type, minArgs, maxArgs, true);
 }
Пример #17
0
 void TestParseFunction(FunctionOperatorType type, int minArgs, int maxArgs, bool checkWrongArgs)
 {
     List<CriteriaOperator> operands = new List<CriteriaOperator>();
     string criteria = type.ToString() + "( {0} )";
     string args = string.Empty;
     for (int i = 0; i <= (maxArgs + 1); i++) {
         if (i >= minArgs - 1) {
             if (i < minArgs || i > maxArgs) {
                 if(checkWrongArgs)TestParseFunctionWrongArgs(string.Format(criteria, args));
             } else {
                 TestParseFunctionArgs(string.Format(criteria, args), type, operands.ToArray());
             }
         }
         if (!string.IsNullOrEmpty(args)) args = args + ", ";
         args = args + i.ToString();
         operands.Add(new OperandValue(i));
     }
 }
        private FunctionOperator MethodToFunction(MethodCallExpression node)
        {
            IList <CriteriaOperator> operands = new List <CriteriaOperator>();

            if (node.Object != null)
            {
                operands.Add(ExtractOperand(node.Object));
            }
            foreach (Expression arg in node.Arguments)
            {
                operands.Add(ExtractOperand(arg));
            }
            FunctionOperatorType operatorType = FunctionOperatorType.Custom;

            switch (node.Method.Name)
            {
            case "ToLower":
                operatorType = FunctionOperatorType.Lower;
                break;

            case "ToString":
                operatorType = FunctionOperatorType.ToStr;
                break;

            case "Trim":
                ThrowIfTrimArgumentsNotSupported(node);
                operatorType = FunctionOperatorType.Trim;
                break;

            case "ToUpper":
                operatorType = FunctionOperatorType.Upper;
                break;

            case "AddMilliseconds":
                operatorType = FunctionOperatorType.AddMilliSeconds;
                break;

            case "Add":
                operatorType = FunctionOperatorType.AddTimeSpan;
                break;

            case "Atan":
                operatorType = FunctionOperatorType.Atn;
                break;

            case "Atan2":
                operatorType = FunctionOperatorType.Atn2;
                break;

            case "ToChar":
                operatorType = FunctionOperatorType.Char;
                break;

            case "IndexOf":
                return(ExtractCharIndex(node));

            case "Pow":
                operatorType = FunctionOperatorType.Power;
                break;

            case "Sqrt":
                operatorType = FunctionOperatorType.Sqr;
                break;

            case "ToSingle":
                operatorType = FunctionOperatorType.ToFloat;
                break;

            case "ToInt32":
                operatorType = FunctionOperatorType.ToInt;
                break;

            case "ToInt64":
                operatorType = FunctionOperatorType.ToLong;
                break;

            default:
                operatorType = GetOperatorTypeByMethodName(node.Method.Name);
                if (operatorType == FunctionOperatorType.Custom)
                {
                    return(new FunctionOperator(node.Method.Name, operands));
                }
                break;
            }
            return(new FunctionOperator(operatorType, operands));
        }
Пример #19
0
 /// <summary>添加 WHERE 子句</summary>
 /// <param name="property">字段栏表达式</param>
 /// <param name="op">比较运算符</param>
 /// <param name="value">值</param>
 /// <returns>WHERE 构建器</returns>
 internal SelectBuilder Append(Expression property, FunctionOperatorType op, object value)
 {
     return(this.Append(property, op, value, ","));
 }