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