StringFunctions.cs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. //
  2. // StringFunctions.cs
  3. //
  4. // Author:
  5. // Juraj Skripsky ([email protected])
  6. //
  7. // (C) 2004 HotFeet GmbH (http://www.hotfeet.ch)
  8. //
  9. using System;
  10. using System.Collections;
  11. using System.Data;
  12. namespace Mono.Data.SqlExpressions {
  13. public abstract class StringFunction : UnaryExpression {
  14. protected StringFunction (IExpression e) : base (e) {}
  15. override public object Eval (DataRow row)
  16. {
  17. object val = expr.Eval (row);
  18. if(val == null)
  19. return null;
  20. if (!(val is string)) {
  21. string fnct = this.GetType ().ToString ();
  22. int start = fnct.LastIndexOf('.') + 1;
  23. fnct = fnct.Substring (start, fnct.Length - start - "Function".Length);
  24. throw new EvaluateException (String.Format ("'{0}' can be applied only to strings.", fnct));
  25. }
  26. return val;
  27. }
  28. }
  29. public class SubstringFunction : StringFunction {
  30. int start, len;
  31. public SubstringFunction (IExpression e, int start, int len) : base (e)
  32. {
  33. this.start = start;
  34. this.len = len;
  35. }
  36. override public object Eval (DataRow row)
  37. {
  38. string str = (string)base.Eval (row);
  39. if(str == null)
  40. return null;
  41. if (start > str.Length)
  42. return String.Empty;
  43. return str.Substring (start - 1, System.Math.Min (len, str.Length - (start - 1)));
  44. }
  45. }
  46. public class LenFunction : StringFunction {
  47. public LenFunction (IExpression e) : base (e) {}
  48. override public object Eval (DataRow row)
  49. {
  50. string str = (string)base.Eval (row);
  51. if(str == null)
  52. return 0;
  53. return str.Length;
  54. }
  55. }
  56. public class TrimFunction : StringFunction {
  57. public TrimFunction (IExpression e) : base (e) {}
  58. override public object Eval (DataRow row)
  59. {
  60. string str = (string)base.Eval (row);
  61. if(str == null)
  62. return null;
  63. return str.Trim();
  64. }
  65. }
  66. }