Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Data / System / Data / Common / SQLTypes / SQLCharsStorage.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="SQLCharsStorage.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.Xml;
13     using System.IO;
14     using System.Xml.Serialization;
15     using System.Data.SqlTypes;
16     using System.Diagnostics;
17     using System.Globalization;
18     using System.Collections;
19
20     internal sealed class SqlCharsStorage : DataStorage {
21
22         private SqlChars[] values;
23
24         public SqlCharsStorage(DataColumn column)
25         : base(column, typeof(SqlChars), SqlChars.Null, SqlChars.Null, StorageType.SqlChars) {
26         }
27
28         override public Object Aggregate(int[] records, AggregateType kind) {
29             try {
30                 switch (kind) {
31                     case AggregateType.First:
32                         if (records.Length > 0) {
33                             return values[records[0]];
34                         }
35                         return null;// no data => null
36
37                     case AggregateType.Count:
38                         int count = 0;
39                         for (int i = 0; i < records.Length; i++) {
40                             if (!IsNull(records[i]))
41                                 count++;
42                         }
43                         return count;
44                 }
45             }
46             catch (OverflowException) {
47                 throw ExprException.Overflow(typeof(SqlChars));
48             }
49             throw ExceptionBuilder.AggregateException(kind, DataType);
50         }
51
52         override public int Compare(int recordNo1, int recordNo2) {
53 //            throw ExceptionBuilder.IComparableNotDefined;
54             return 0;
55         }
56
57         override public int CompareValueTo(int recordNo, Object value) {
58 //            throw ExceptionBuilder.IComparableNotDefined;
59             return 0;
60         }
61
62         override public void Copy(int recordNo1, int recordNo2) {
63             values[recordNo2] = values[recordNo1];
64         }
65
66         override public Object Get(int record) {
67             return values[record];
68         }
69
70         override public bool IsNull(int record) {
71             return (values[record].IsNull);
72         }
73
74         override public void Set(int record, Object value) {
75             if ((value ==  DBNull.Value) || (value == null)){
76                 values[record] = SqlChars.Null;
77             }
78             else {
79                 values[record] = (SqlChars)value;            
80             }
81         }
82
83         override public void SetCapacity(int capacity) {
84             SqlChars[] newValues = new SqlChars[capacity];
85             if (null != values) {
86                 Array.Copy(values, 0, newValues, 0, Math.Min(capacity, values.Length));
87             }
88             values = newValues;
89         }
90
91         override public object ConvertXmlToObject(string s) {
92             SqlString newValue = new SqlString();
93
94             string tempStr =string.Concat("<col>", s, "</col>"); // this is done since you can give fragmet to reader, bug 98767
95             StringReader strReader = new  StringReader(tempStr);
96
97             IXmlSerializable tmp = newValue;
98             
99             using (XmlTextReader xmlTextReader = new XmlTextReader(strReader)) {
100                 tmp.ReadXml(xmlTextReader);
101             }
102             return (new SqlChars((SqlString)tmp));
103         }
104
105         override public string ConvertObjectToXml(object value) {
106             Debug.Assert(!DataStorage.IsObjectNull(value), "we shouldn't have null here");
107             Debug.Assert((value.GetType() == typeof(SqlChars)), "wrong input type");
108             
109             StringWriter strwriter = new StringWriter(FormatProvider);
110
111             using (XmlTextWriter xmlTextWriter = new XmlTextWriter (strwriter)) {
112                 ((IXmlSerializable)value).WriteXml(xmlTextWriter);
113             }
114             return (strwriter.ToString ());
115         }
116         
117         override protected object GetEmptyStorage(int recordCount) {
118             return new SqlChars[recordCount];
119         }
120         
121         override protected void CopyValue(int record, object store, BitArray nullbits, int storeIndex) {
122             SqlChars[] typedStore = (SqlChars[]) store; 
123             typedStore[storeIndex] = values[record];
124             nullbits.Set(storeIndex, IsNull(record));
125         }
126         
127         override protected void SetStorage(object store, BitArray nullbits) {
128             values = (SqlChars[]) store; 
129             //SetNullStorage(nullbits);
130         }        
131     }
132 }