ComparerFactory.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using System;
  2. using System.Collections;
  3. namespace System.Data.Common
  4. {
  5. /// <summary>
  6. /// Summary description for ComparerFactory.
  7. /// </summary>
  8. public class DBComparerFactory
  9. {
  10. private static IComparer comparableComparer = new ComparebleComparer();
  11. private static IComparer ignoreCaseComparer = new IgnoreCaseComparer();
  12. private static IComparer caseComparer = new CaseComparer();
  13. private static IComparer byteArrayComparer = new ByteArrayComparer();
  14. private static Type icomparerType = typeof (IComparable);
  15. public static IComparer GetComparer (Type type, bool ignoreCase)
  16. {
  17. if (type == typeof (string)) {
  18. if (ignoreCase)
  19. return ignoreCaseComparer;
  20. return caseComparer;
  21. }
  22. if (icomparerType.IsAssignableFrom(type))
  23. return comparableComparer;
  24. if (type == typeof (byte[]))
  25. return byteArrayComparer;
  26. return null;
  27. }
  28. class ComparebleComparer :IComparer
  29. {
  30. #region IComparer Members
  31. public int Compare(object x, object y)
  32. {
  33. if (x == DBNull.Value) {
  34. if (y == DBNull.Value)
  35. return 0;
  36. return -1;
  37. }
  38. if (y == DBNull.Value)
  39. return 1;
  40. return ((IComparable)x).CompareTo (y);
  41. }
  42. #endregion
  43. }
  44. class CaseComparer : IComparer
  45. {
  46. #region IComparer Members
  47. public int Compare(object x, object y)
  48. {
  49. if (x == DBNull.Value) {
  50. if (y == DBNull.Value)
  51. return 0;
  52. return -1;
  53. }
  54. if (y == DBNull.Value)
  55. return 1;
  56. return String.Compare ((string)x, (string)y, false);
  57. }
  58. #endregion
  59. }
  60. class IgnoreCaseComparer : IComparer
  61. {
  62. #region IComparer Members
  63. public int Compare(object x, object y)
  64. {
  65. if (x == DBNull.Value) {
  66. if (y == DBNull.Value)
  67. return 0;
  68. return -1;
  69. }
  70. if (y == DBNull.Value)
  71. return 1;
  72. return String.Compare ((string)x, (string)y, true);
  73. }
  74. #endregion
  75. }
  76. class ByteArrayComparer : IComparer
  77. {
  78. #region IComparer Members
  79. public int Compare(object x, object y)
  80. {
  81. if (x == DBNull.Value) {
  82. if (y == DBNull.Value)
  83. return 0;
  84. return -1;
  85. }
  86. if (y == DBNull.Value)
  87. return 1;
  88. byte[] o1 = (byte[])x;
  89. byte[] o2 = (byte[])y;
  90. int len = o1.Length;
  91. int lenb = o2.Length;
  92. for (int i = 0; ; i++) {
  93. int a = 0;
  94. int b = 0;
  95. if (i < len) {
  96. a = o1[i];
  97. }
  98. else if (i >= lenb) {
  99. return 0;
  100. }
  101. if (i < lenb) {
  102. b = o2[i];
  103. }
  104. if (a > b) {
  105. return 1;
  106. }
  107. if (b > a) {
  108. return -1;
  109. }
  110. }
  111. }
  112. #endregion
  113. }
  114. }
  115. }