SHARE
TWEET

Untitled

a guest Jun 20th, 2019 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public abstract class ConditionBuilder<TContext> : IConditionBuilder where TContext : FieldSearchContext
  2. {
  3.     public virtual bool QuotedValues { get; set; } = true;
  4.  
  5.     public abstract string OperatorSymbol { get; }
  6.  
  7.     public string BuildCondition(SearchCondition searchCondition)
  8.     {
  9.         var conditionBuilder = new StringBuilder();
  10.  
  11.         var context = searchCondition.GetContext<TContext>();
  12.  
  13.         conditionBuilder.Append(context.FieldId);
  14.         conditionBuilder.Append(OperatorSymbol);
  15.         conditionBuilder.Append(GetValue(context));
  16.  
  17.         return conditionBuilder.ToString();
  18.     }
  19.  
  20.     public abstract bool CanHandle(FilterAction filterAction);
  21.  
  22.     public abstract object GetValue(TContext context);
  23.  
  24. }
  25.  
  26. public class TextLikeConditionBuilder : ConditionBuilder<TextContext>
  27. {
  28.     public override string OperatorSymbol => " LIKE ";
  29.  
  30.     public override bool CanHandle(FilterAction action) => action == FilterAction.TextLike;
  31.  
  32.     public override object GetValue(TextContext context)
  33.     {
  34.         if (context.Text == null)
  35.         {
  36.             return null;
  37.         }
  38.  
  39.         return string.Concat("%", context.Text, "%");
  40.     }
  41. }
  42.  
  43. public class TextEqualsConditionBuilder : ConditionBuilder<TextContext>
  44. {
  45.     public override string OperatorSymbol => " = ";
  46.  
  47.     public override bool CanHandle(FilterAction action) => action == FilterAction.TextEqual;
  48.  
  49.     public override object GetValue(TextContext context)
  50.     {
  51.         if (context.Text == null)
  52.         {
  53.             return null;
  54.         }
  55.  
  56.         return "'" + context.Text + "'";
  57.     }
  58. }
  59.      
  60. public interface IDelimitedIdentifier
  61. {
  62.     string Delimit(string input);
  63. }
  64.  
  65. internal class SqlServerDelimitedIdentifier : IDelimitedIdentifier
  66. {
  67.     public string Delimit(string input)
  68.     {
  69.         return "[" + input.Replace("]", "]]") + "]";
  70.     }
  71. }
  72.  
  73. internal class OracleDelimitedIdentifier : IDelimitedIdentifier
  74. {
  75.     public string Delimit(string input)
  76.     {
  77.         return """ + input + """;
  78.     }
  79. }
  80.  
  81. public abstract class ConditionBuilder<TContext> : IConditionBuilder where TContext : FieldSearchContext
  82. {
  83.     public virtual bool QuotedValues { get; set; } = true;
  84.  
  85.     public abstract string OperatorSymbol { get; }
  86.  
  87.     public string BuildCondition(SearchCondition searchCondition)
  88.     {
  89.         var conditionBuilder = new StringBuilder();
  90.  
  91.         var context = searchCondition.GetContext<TContext>();
  92.  
  93.         conditionBuilder.Append(SanitizeFieldId(context.FieldId));
  94.         conditionBuilder.Append(OperatorSymbol);
  95.         conditionBuilder.Append(GetValue(context));
  96.  
  97.         return conditionBuilder.ToString();
  98.     }
  99.  
  100.     public abstract bool CanHandle(FilterAction filterAction);
  101.  
  102.     public abstract object GetValue(TContext context);
  103.  
  104.     protected virtual string SanitizeFieldId(string fieldId)
  105.     {
  106.         return _delimitedIdentifier.Delimit(fieldId);
  107.     }
  108.  
  109. }
  110.  
  111. public class SanitizedFieldConditionBuilder<TContext> : ConditionBuilder<TContext> where TContext : FieldSearchContextBase
  112. {
  113.     private readonly ConditionBuilder<TContext> _baseConditionBuilder;
  114.     private readonly IDelimitedIdentifier _delimitedIdentifier;
  115.  
  116.     public SanitizedFieldConditionBuilder(ConditionBuilder<TContext> baseConditionBuilder, IDelimitedIdentifier delimitedIdentifier)
  117.     {
  118.         QuotedValues = false;
  119.  
  120.         _baseConditionBuilder = baseConditionBuilder;
  121.         _delimitedIdentifier = delimitedIdentifier;
  122.     }
  123.  
  124.     public override string OperatorSymbol => _baseConditionBuilder.OperatorSymbol;
  125.  
  126.     public override bool CanHandle(SearchFilterAction action) => _baseConditionBuilder.CanHandle(action);
  127.  
  128.     public override object GetValue(TContext context) => _baseConditionBuilder.GetValue(context);
  129.  
  130.     protected override string SanitizeFieldId(string fieldId)
  131.     {
  132.         return _delimitedIdentifier.Delimit(fieldId);
  133.     }
  134. }
  135.  
  136. public static class ConditionBuilderExtensions
  137. {
  138.     public static SanitizedFieldConditionBuiler<TContext> SanitizeField<TContext>(this ConditionBuilder<TContext> source, IColumnSanitizer columnSanitizer) where TContext : FieldSearchContext
  139.     {
  140.         return new SanitizedFieldConditionBuiler<TContext>(source, columnSanitizer);
  141.     }
  142. }
  143.      
  144. class Program
  145. {
  146.   static void Main(string[] args)
  147.   {
  148.    var conditionBuilders = new List<IConditionBuilder>()
  149.         {
  150.             new TextEqualsConditionBuilder().SanitizeField(new SqlSanitizer()),
  151.             new TextLikeConditionBuilder().SanitizeField(new SqlSanitizer())
  152.         };
  153.         }
  154. }
  155.      
  156. public abstract class ConditionBuilder<TContext> : IConditionBuilder where TContext : FieldSearchContext
  157.     {
  158.         private readonly IDelimitedIdentifier DelimitedIdentifier;
  159.  
  160.         public virtual bool QuotedValues { get; set; } = true;
  161.  
  162.         public abstract string OperatorSymbol { get; }
  163.  
  164.         protected ConditionBuilder(IDelimitedIdentifier delimitedIdentifier)
  165.         {
  166.             DelimitedIdentifier = delimitedIdentifier;
  167.         }
  168.  
  169.         public string BuildCondition(SearchCondition searchCondition)
  170.         {
  171.             var conditionBuilder = new StringBuilder();
  172.  
  173.             var context = searchCondition.GetContext<TContext>();
  174.  
  175.             conditionBuilder.Append(DelimitedIdentifier.Delimit(context.FieldId));
  176.             conditionBuilder.Append(OperatorSymbol);
  177.             conditionBuilder.Append(GetValue(context));
  178.  
  179.             return conditionBuilder.ToString();
  180.         }
  181.  
  182.         public abstract bool CanHandle(FilterAction filterAction);
  183.  
  184.         public abstract object GetValue(TContext context);
  185.  
  186.     }
  187.  
  188.     public class TextLikeConditionBuilder : ConditionBuilder<TextContext>
  189.     {
  190.  
  191.         public TextLikeConditionBuilder(IDelimitedIdentifier delimitedIdentifier) : base(delimitedIdentifier)
  192.         {
  193.  
  194.         }
  195.  
  196.         public override string OperatorSymbol => " LIKE ";
  197.  
  198.         public override bool CanHandle(FilterAction action) => action == FilterAction.TextLike;
  199.  
  200.         public override object GetValue(TextContext context)
  201.         {
  202.             if (context.Text == null)
  203.             {
  204.                 return null;
  205.             }
  206.  
  207.             return string.Concat("%", context.Text, "%");
  208.         }
  209.     }
  210.  
  211.     public class TextEqualsConditionBuilder : ConditionBuilder<TextContext>
  212.     {
  213.  
  214.         public TextEqualsConditionBuilder(IDelimitedIdentifier delimitedIdentifier) : base(delimitedIdentifier)
  215.         {
  216.  
  217.         }
  218.  
  219.         public override string OperatorSymbol => " = ";
  220.  
  221.         public override bool CanHandle(FilterAction action) => action == FilterAction.TextEqual;
  222.  
  223.         public override object GetValue(TextContext context)
  224.         {
  225.             if (context.Text == null)
  226.             {
  227.                 return null;
  228.             }
  229.  
  230.             return "'" + context.Text + "'";
  231.         }
  232.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top