2
0

ComparerFactory.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. using System;
  24. using System.Collections;
  25. namespace System.Data.Common
  26. {
  27. /// <summary>
  28. /// Summary description for ComparerFactory.
  29. /// </summary>
  30. internal class DBComparerFactory
  31. {
  32. private static IComparer comparableComparer = new ComparebleComparer();
  33. private static IComparer ignoreCaseComparer = new IgnoreCaseComparer();
  34. private static IComparer caseComparer = new CaseComparer();
  35. private static IComparer byteArrayComparer = new ByteArrayComparer();
  36. private static Type icomparerType = typeof (IComparable);
  37. public static IComparer GetComparer (Type type, bool ignoreCase)
  38. {
  39. if (type == typeof (string)) {
  40. if (ignoreCase)
  41. return ignoreCaseComparer;
  42. return caseComparer;
  43. }
  44. if (icomparerType.IsAssignableFrom(type))
  45. return comparableComparer;
  46. if (type == typeof (byte[]))
  47. return byteArrayComparer;
  48. return null;
  49. }
  50. class ComparebleComparer :IComparer
  51. {
  52. #region IComparer Members
  53. public int Compare(object x, object y)
  54. {
  55. if (x == DBNull.Value) {
  56. if (y == DBNull.Value)
  57. return 0;
  58. return -1;
  59. }
  60. if (y == DBNull.Value)
  61. return 1;
  62. return ((IComparable)x).CompareTo (y);
  63. }
  64. #endregion
  65. }
  66. class CaseComparer : IComparer
  67. {
  68. #region IComparer Members
  69. public int Compare(object x, object y)
  70. {
  71. if (x == DBNull.Value) {
  72. if (y == DBNull.Value)
  73. return 0;
  74. return -1;
  75. }
  76. if (y == DBNull.Value)
  77. return 1;
  78. return String.Compare ((string)x, (string)y, false);
  79. }
  80. #endregion
  81. }
  82. class IgnoreCaseComparer : IComparer
  83. {
  84. #region IComparer Members
  85. public int Compare(object x, object y)
  86. {
  87. if (x == DBNull.Value) {
  88. if (y == DBNull.Value)
  89. return 0;
  90. return -1;
  91. }
  92. if (y == DBNull.Value)
  93. return 1;
  94. return String.Compare ((string)x, (string)y, true);
  95. }
  96. #endregion
  97. }
  98. class ByteArrayComparer : IComparer
  99. {
  100. #region IComparer Members
  101. public int Compare(object x, object y)
  102. {
  103. if (x == DBNull.Value) {
  104. if (y == DBNull.Value)
  105. return 0;
  106. return -1;
  107. }
  108. if (y == DBNull.Value)
  109. return 1;
  110. byte[] o1 = (byte[])x;
  111. byte[] o2 = (byte[])y;
  112. int len = o1.Length;
  113. int lenb = o2.Length;
  114. for (int i = 0; ; i++) {
  115. int a = 0;
  116. int b = 0;
  117. if (i < len) {
  118. a = o1[i];
  119. }
  120. else if (i >= lenb) {
  121. return 0;
  122. }
  123. if (i < lenb) {
  124. b = o2[i];
  125. }
  126. if (a > b) {
  127. return 1;
  128. }
  129. if (b > a) {
  130. return -1;
  131. }
  132. }
  133. }
  134. #endregion
  135. }
  136. }
  137. }