3 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 using System
.Collections
;
27 namespace System
.Data
.Common
30 /// Summary description for ComparerFactory.
32 internal class DBComparerFactory
34 private static IComparer comparableComparer
= new ComparebleComparer();
35 private static IComparer ignoreCaseComparer
= new IgnoreCaseComparer();
36 private static IComparer caseComparer
= new CaseComparer();
37 private static IComparer byteArrayComparer
= new ByteArrayComparer();
38 private static Type icomparerType
= typeof (IComparable
);
40 public static IComparer
GetComparer (Type type
, bool ignoreCase
)
42 if (type
== typeof (string)) {
44 return ignoreCaseComparer
;
47 if (icomparerType
.IsAssignableFrom(type
))
48 return comparableComparer
;
49 if (type
== typeof (byte[]))
50 return byteArrayComparer
;
54 class ComparebleComparer
:IComparer
56 #region IComparer Members
58 public int Compare(object x
, object y
)
60 if (x
== DBNull
.Value
) {
61 if (y
== DBNull
.Value
)
67 if (y
== DBNull
.Value
)
70 return ((IComparable
)x
).CompareTo (y
);
76 class CaseComparer
: IComparer
78 #region IComparer Members
80 public int Compare(object x
, object y
)
82 if (x
== DBNull
.Value
) {
83 if (y
== DBNull
.Value
)
89 if (y
== DBNull
.Value
)
92 return String
.Compare ((string)x
, (string)y
, false);
98 class IgnoreCaseComparer
: IComparer
100 #region IComparer Members
102 public int Compare(object x
, object y
)
104 if (x
== DBNull
.Value
) {
105 if (y
== DBNull
.Value
)
111 if (y
== DBNull
.Value
)
114 return String
.Compare ((string)x
, (string)y
, true);
120 class ByteArrayComparer
: IComparer
122 #region IComparer Members
124 public int Compare(object x
, object y
)
126 if (x
== DBNull
.Value
) {
127 if (y
== DBNull
.Value
)
133 if (y
== DBNull
.Value
)
136 byte[] o1
= (byte[])x
;
137 byte[] o2
= (byte[])y
;
139 int lenb
= o2
.Length
;
141 for (int i
= 0; ; i
++) {
148 else if (i
>= lenb
) {