Esempio n. 1
0
        internal string Validate(SqlType t)
        {
            if (ConditionGroup != null && LeftColumn != null)
            {
                return("You have specified a condition as both a grouping and a condition");
            }

            if (ConditionGroup == null)
            {
                if (LeftColumn == null)
                {
                    return("You must specify a left column for condition");
                }
                if (RightColumn != null && RightValue != null)
                {
                    return("You can not set a condition to be both column and literal based");
                }
                if (Operator != CompareOperator.In && RightColumn == null && RightValue == null)
                {
                    return("You must specify either a compare column or literal value");
                }
                // ReSharper disable once SwitchStatementMissingSomeCases
                switch (Operator)
                {
                case CompareOperator.Contains:
                case CompareOperator.NotContains:
                case CompareOperator.BeginsWith:
                case CompareOperator.EndsWith:
                    if (RightValue == null)
                    {
                        return("You must specify a literal value for a LIKE operation");
                    }
                    RightValue = RightValue
                                 .ToString()
                                 .Replace("|", "||")
                                 .Replace("%", "|%")
                                 .Replace("_", "|_");
                    if (t == SqlType.SqlServer)
                    {
                        RightValue = RightValue
                                     .ToString()
                                     .Replace("[", "|[")
                                     .Replace("]", "|]");
                    }
                    break;

                case CompareOperator.DateOffset:
                    if (!(RightValue is TimeSpan))
                    {
                        return("You must specify a time span for date offset");
                    }

                    var now = DateTime.UtcNow;
                    AdditionalParameterValues.Add(now.Add((TimeSpan)RightValue));
                    RightValue = now;
                    break;

                case CompareOperator.DateRange:
                    if (!(RightValue is DateTime))
                    {
                        return("Date range must use dates (first date)");
                    }
                    if (!(AdditionalParameterValues.FirstOrDefault() is DateTime))
                    {
                        return("Date range must use dates (second date)");
                    }
                    break;

                case CompareOperator.In:
                    if (RightValue != null)
                    {
                        return("For IN comparison, you must specify all values as AdditionalParameterValues");
                    }
                    if (!AdditionalParameterValues.Any())
                    {
                        return("For IN comparison, you must specify at least 1 AdditionalParameterValues");
                    }
                    break;
                }
            }

            else
            {
                var groupValidation = ConditionGroup.Validate(t);
                if (groupValidation != null)
                {
                    return(groupValidation);
                }
            }

            return(Sibling?.Validate(t));
        }