[System.Data] SqlTypes and DataStorage from corefx (#4291)
[mono.git] / mcs / class / referencesource / System.Data / System / Data / Common / SQLTypes / SqlXmlStorage.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="SqlXmlStorage.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>                                                                
5 // <owner current="true" primary="true">[....]</owner>
6 // <owner current="true" primary="false">[....]</owner>
7 // <owner current="false" primary="false">[....]</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.Text;
18     using System.Collections;
19     internal sealed class SqlXmlStorage : DataStorage {
20
21         private SqlXml[] values;
22
23         public SqlXmlStorage(DataColumn column)
24         : base(column, typeof(SqlXml), SqlXml.Null, SqlXml.Null, StorageType.Empty) {
25         }
26
27         override public Object Aggregate(int[] records, AggregateType kind) {
28             try {
29                 switch (kind) {
30                     case AggregateType.First:
31                         if (records.Length > 0) {
32                             return values[records[0]];
33                         }
34                         return null;// no data => null
35
36                     case AggregateType.Count:
37                         int count = 0;
38                         for (int i = 0; i < records.Length; i++) {
39                             if (!IsNull(records[i]))
40                                 count++;
41                         }
42                         return count;
43                 }
44             }
45             catch (OverflowException) {
46                 throw ExprException.Overflow(typeof(SqlXml));
47             }
48             throw ExceptionBuilder.AggregateException(kind, _dataType);
49         }
50
51         override public int Compare(int recordNo1, int recordNo2) {
52             //return values[recordNo1].CompareTo(values[recordNo2]);
53             return 0;
54         }
55
56         override public int CompareValueTo(int recordNo, Object value) {
57             // SqlXml valueNo2 = ((value == null)||(value == DBNull.Value))? SqlXml.Null : (SqlXml)value;
58             // return values[recordNo].CompareTo(valueNo2);
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] = SqlXml.Null;
77             }
78             else {
79                 values[record] = (SqlXml)value;            
80             }
81         }
82
83         override public void SetCapacity(int capacity) {
84             SqlXml[] newValues = new SqlXml[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             XmlTextReader reader =  new XmlTextReader(s, XmlNodeType.Element, null) ;
93             return (new SqlXml(reader));
94             
95
96 /*            SqlXml newValue = new SqlXml();
97             
98             StringReader strReader = new  StringReader(s);
99             XmlTextReader xmlTextReader = new XmlTextReader(strReader);
100             ((IXmlSerializable)newValue).ReadXml(xmlTextReader);
101             xmlTextReader.Close();
102             return newValue;
103 */
104         }
105
106         override public string ConvertObjectToXml(object value) {
107             SqlXml reader = (SqlXml) value;
108             if (reader.IsNull)
109                 return ADP.StrEmpty;
110             else
111                 return reader.Value;        // SqlXml.Value returns string
112
113         }
114         
115         override protected object GetEmptyStorage(int recordCount) {
116             return new SqlXml[recordCount];
117         }
118         
119         override protected void CopyValue(int record, object store, BitArray nullbits, int storeIndex) {
120             SqlXml[] typedStore = (SqlXml[]) store; 
121             typedStore[storeIndex] = values[record];
122             nullbits.Set(storeIndex, IsNull(record));
123         }
124         
125         override protected void SetStorage(object store, BitArray nullbits) {
126             values = (SqlXml[]) store; 
127             //SetNullStorage(nullbits);
128         }         
129     }
130 }