StringFunctions.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // StringFunctions.cs
  3. //
  4. // Author:
  5. // Juraj Skripsky ([email protected])
  6. //
  7. // (C) 2004 HotFeet GmbH (http://www.hotfeet.ch)
  8. // Copyright 2011 Xamarin Inc.
  9. //
  10. //
  11. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System;
  33. using System.Collections;
  34. using System.Data;
  35. namespace Mono.Data.SqlExpressions {
  36. internal abstract class StringFunction : UnaryExpression {
  37. protected StringFunction (IExpression e) : base (e) {}
  38. override public object Eval (DataRow row)
  39. {
  40. object val = expr.Eval (row);
  41. if(val == null)
  42. return null;
  43. if (!(val is string)) {
  44. string fnct = this.GetType ().ToString ();
  45. int start = fnct.LastIndexOf('.') + 1;
  46. fnct = fnct.Substring (start, fnct.Length - start - "Function".Length);
  47. throw new EvaluateException (String.Format ("'{0}' can be applied only to strings.", fnct));
  48. }
  49. return val;
  50. }
  51. }
  52. internal class SubstringFunction : StringFunction {
  53. IExpression start;
  54. IExpression len;
  55. public SubstringFunction (IExpression e, IExpression start, IExpression len) : base (e)
  56. {
  57. this.start = start;
  58. this.len = len;
  59. }
  60. public override bool Equals(object obj)
  61. {
  62. if (!base.Equals (obj))
  63. return false;
  64. if (!(obj is SubstringFunction))
  65. return false;
  66. SubstringFunction other = (SubstringFunction) obj;
  67. if (other.start != start)
  68. return false;
  69. if (other.len != len)
  70. return false;
  71. return true;
  72. }
  73. public override int GetHashCode()
  74. {
  75. int hashCode = base.GetHashCode ();
  76. hashCode ^= start.GetHashCode ();
  77. hashCode ^= len.GetHashCode ();
  78. return hashCode;
  79. }
  80. override public object Eval (DataRow row)
  81. {
  82. string str = (string)base.Eval (row);
  83. start.Eval (row);
  84. int istart = Convert.ToInt32 (start.Eval (row));
  85. int ilen = Convert.ToInt32 (len.Eval (row));
  86. if(str == null)
  87. return null;
  88. if (istart > str.Length)
  89. return String.Empty;
  90. return str.Substring (istart - 1, System.Math.Min (ilen, str.Length - (istart - 1)));
  91. }
  92. }
  93. internal class LenFunction : StringFunction {
  94. public LenFunction (IExpression e) : base (e) {}
  95. override public object Eval (DataRow row)
  96. {
  97. string str = (string)base.Eval (row);
  98. if(str == null)
  99. return 0;
  100. return str.Length;
  101. }
  102. }
  103. internal class TrimFunction : StringFunction {
  104. public TrimFunction (IExpression e) : base (e) {}
  105. override public object Eval (DataRow row)
  106. {
  107. string str = (string)base.Eval (row);
  108. if(str == null)
  109. return null;
  110. return str.Trim();
  111. }
  112. }
  113. }