Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Data / System / Data / Common / StringStorage.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="StringStorage.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 // <owner current="true" primary="true">Microsoft</owner>
6 // <owner current="true" primary="false">Microsoft</owner>
7 // <owner current="false" primary="false">Microsoft</owner>
8 //------------------------------------------------------------------------------
9
10 namespace System.Data.Common {
11     using System;
12     using System.Diagnostics;
13     using System.Globalization;
14     using System.Data.SqlTypes;
15     using System.Collections;
16
17     // The string storage does not use BitArrays in DataStorage
18     internal sealed class StringStorage : DataStorage {
19
20         private String[] values;
21
22         public StringStorage(DataColumn column)
23         : base(column, typeof(String), String.Empty, StorageType.String) {
24         }
25
26         override public Object Aggregate(int[] recordNos, AggregateType kind) {
27             int i;
28             switch (kind) {
29                 case AggregateType.Min:
30                     int min = -1;
31                     for (i = 0; i < recordNos.Length; i++) {
32                         if (IsNull(recordNos[i]))
33                             continue;
34                         min = recordNos[i];
35                         break;
36                     }
37                     if (min >= 0) {
38                         for (i = i+1; i < recordNos.Length; i++) {
39                             if (IsNull(recordNos[i]))
40                                 continue;
41                             if (Compare(min, recordNos[i]) > 0) {
42                                 min = recordNos[i];
43                             }
44                         }
45                         return Get(min);
46                     }
47                     return NullValue;
48
49                 case AggregateType.Max:
50                     int max = -1;
51                     for (i = 0; i < recordNos.Length; i++) {
52                         if (IsNull(recordNos[i]))
53                             continue;
54                         max = recordNos[i];
55                         break;
56                     }
57                     if (max >= 0) {
58                         for (i = i+1; i < recordNos.Length; i++) {
59                             if (Compare(max, recordNos[i]) < 0) {
60                                 max = recordNos[i];
61                             }
62                         }
63                         return Get(max);
64                     }
65                     return NullValue;
66
67                 case AggregateType.Count:
68                     int count = 0;
69                     for (i = 0; i < recordNos.Length; i++) {
70                         Object value = values[recordNos[i]];
71                         if (value != null)
72                             count++;
73                     }
74                     return count;
75             }
76             throw ExceptionBuilder.AggregateException(kind, DataType);
77         }
78
79         override public int Compare(int recordNo1, int recordNo2) {
80             string valueNo1 = values[recordNo1];
81             string valueNo2 = values[recordNo2];
82
83             if ((Object)valueNo1 == (Object)valueNo2)
84                 return 0;
85
86             if (valueNo1 == null)
87                 return -1;
88             if (valueNo2 == null)
89                 return 1;
90
91             return Table.Compare(valueNo1, valueNo2);
92         }
93
94         override public int CompareValueTo(int recordNo, Object value) {
95             Debug.Assert(recordNo != -1, "Invalid (-1) parameter: 'recordNo'");
96             Debug.Assert(null != value, "null value");
97             string valueNo1 = values[recordNo];
98
99             if (null == valueNo1) {
100                 if (NullValue == value) {
101                     return 0;
102                 }
103                 else {
104                     return -1;
105                 }
106             }
107             else if (NullValue == value) {
108                 return 1;
109             }
110             return Table.Compare(valueNo1, (string)value);
111         }
112
113         public override object ConvertValue(object value) {
114             if (NullValue != value) {
115                 if (null != value) {
116                     value = value.ToString();
117                 }
118                 else {
119                     value = NullValue;
120                 }
121             }
122             return value;
123         }
124
125         override public void Copy(int recordNo1, int recordNo2) {
126             values[recordNo2] = values[recordNo1];
127         }
128
129         override public Object Get(int recordNo) {
130             String value = values[recordNo];
131
132             if (null != value) {
133                 return value;
134             }
135             return NullValue;
136         }
137
138         override public int GetStringLength(int record) {
139             string value = values[record];
140             return ((null != value) ? value.Length : 0);
141         }
142
143         override public bool IsNull(int record) {
144             return (null == values[record]);
145         }
146
147         override public void Set(int record, Object value) {
148             System.Diagnostics.Debug.Assert(null != value, "null value");
149             if (NullValue == value) {
150                 values[record] = null;
151             }
152             else {
153                 values[record] = value.ToString();
154             }
155         }
156
157         override public void SetCapacity(int capacity) {
158             string[] newValues = new string[capacity];
159             if (values != null) {
160                 Array.Copy(values, 0, newValues, 0, Math.Min(capacity, values.Length));
161             }
162             values = newValues;
163         }
164
165         override public object ConvertXmlToObject(string s) {
166             return s;
167         }
168
169         override public string ConvertObjectToXml(object value) {
170             return (string)value;
171         }
172
173         override protected object GetEmptyStorage(int recordCount) {
174             return new String[recordCount];
175         }
176
177         override protected void CopyValue(int record, object store, BitArray nullbits, int storeIndex) {
178             String[] typedStore = (String[]) store;
179             typedStore[storeIndex] = values[record];
180             nullbits.Set(storeIndex, IsNull(record));
181         }
182
183         override protected void SetStorage(object store, BitArray nullbits) {
184             values = (String[]) store;
185  //           SetNullStorage(nullbits);
186         }
187     }
188 }