New tests.
[mono.git] / mcs / class / System.Data / System.Data.SqlTypes / SqlChars.cs
1 //
2 // System.Data.SqlTypes.SqlChars
3 //
4 // Author:
5 //   Tim Coleman <tim@timcoleman.com>
6 //
7 // Copyright (C) Tim Coleman, 2003
8 //
9
10 //
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32
33 #if NET_2_0
34
35 using System;
36 using System.Globalization;
37 using System.Xml;
38 using System.Text;
39 using System.Xml.Schema;
40 using System.Xml.Serialization;
41 using System.Runtime.Serialization;
42
43 namespace System.Data.SqlTypes
44 {
45         [SerializableAttribute]
46         [XmlSchemaProvider ("GetXsdType")]
47         [XmlRootAttribute ("string")]
48         public sealed class SqlChars : INullable, IXmlSerializable, ISerializable
49         {
50                 #region Fields
51
52                 bool notNull;
53                 char [] buffer;
54                 StorageState storage = StorageState.UnmanagedBuffer;
55
56                 #endregion
57
58                 #region Constructors
59
60                 public SqlChars ()
61                 {
62                         notNull = false;
63                         buffer = null;
64                 }
65
66                 public SqlChars (char[] buffer)
67                 {
68                         if (buffer == null) {
69                                 notNull = false;
70                                 this.buffer = null;
71                         } else {
72                                 notNull = true;
73                                 this.buffer = buffer;
74                                 storage = StorageState.Buffer;
75                         }
76                 }
77
78                 public SqlChars (SqlString value)
79                 {
80                         if (value.IsNull) {
81                                 notNull = false;
82                                 buffer = null;
83                         } else {
84                                 notNull = true;
85                                 buffer = value.Value.ToCharArray ();
86                                 storage = StorageState.Buffer;
87                         }
88                 }
89
90                 #endregion
91
92                 #region Properties
93
94                 public char [] Buffer {
95                         get { return buffer; }
96                 }
97
98                 public bool IsNull {
99                         get { return !notNull; }
100                 }
101
102                 public char this [long offset] {
103                         set {
104                                 if (notNull && offset >= 0 && offset < buffer.Length)
105                                         buffer [offset] = value;
106                         }
107                         get {
108                                 if (buffer == null)
109                                         throw new SqlNullValueException ("Data is Null");
110                                 if (offset < 0 || offset >= buffer.Length)
111                                         throw new ArgumentOutOfRangeException ("Parameter name: offset");
112                                 return buffer [offset];
113                         }
114                 }
115
116                 public long Length {
117                         get {
118                                 if (!notNull || buffer == null)
119                                         throw new SqlNullValueException ("Data is Null");
120                                 if (buffer.Length < 0)
121                                         return -1;
122                                 return buffer.Length;
123                         }
124                 }
125
126                 public long MaxLength {
127                         get {
128                                 if (!notNull || buffer == null || storage == StorageState.Stream)
129                                         return -1;
130                                 return buffer.Length;
131                         }
132                 }
133
134                 public static SqlChars Null {
135                         get {
136                                 return new SqlChars ();
137                         }
138                 }
139
140                 public StorageState Storage {
141                         get {
142                                 if (storage == StorageState.UnmanagedBuffer)
143                                         throw new SqlNullValueException ("Data is Null");
144                                 return storage;
145                         }
146                 }
147
148                 public char [] Value {
149                         get {
150                                 if (buffer == null)
151                                         return buffer;
152                                 return (char []) buffer.Clone ();
153                         }
154                 }
155
156                 #endregion
157
158                 #region Methods
159
160                 public void SetLength (long value)
161                 {
162                         if (buffer == null)
163                                 throw new SqlTypeException ("There is no buffer");
164                         if (value < 0 || value > buffer.Length)
165                                 throw new ArgumentOutOfRangeException ("Specified argument was out of the range of valid values.");
166                         Array.Resize (ref buffer, (int) value);
167                 }
168                                                                                 
169                 public void SetNull ()
170                 {
171                         buffer = null;
172                         notNull = false;
173                 }
174
175                 public static explicit operator SqlString (SqlChars x)
176                 {
177                         if (x.IsNull)
178                                 return SqlString.Null;
179                         else {
180                                 return new SqlString (new String (x.Value));
181                         }
182                 }
183
184                 public static explicit operator SqlChars (SqlString x)
185                 {
186                         if (x.IsNull)
187                                 return Null;
188                         else
189                                 return new SqlChars (x.Value);
190                 }
191
192                 public SqlString ToSqlString ()
193                 {
194                         if (buffer == null) {
195                                 return SqlString.Null;
196                         }
197                         else {
198                                 return new SqlString (buffer.ToString ());
199                         }
200                 }
201                                                               
202                 [MonoTODO]
203                 public long Read (long offset, char [] buffer, int offsetInBuffer, int count)
204                 {
205                         throw new NotImplementedException ();
206                 }
207
208                 [MonoNotSupported("")]
209                 public void Write (long offset, char [] buffer, int offsetInBuffer, int count)
210                 {
211                         throw new NotImplementedException ();
212                 }
213
214                 public static XmlQualifiedName GetXsdType (XmlSchemaSet schemaSet)
215                 {
216                         XmlQualifiedName qualifiedName = new XmlQualifiedName ("string", "http://www.w3.org/2001/XMLSchema");
217                         return qualifiedName;
218                 }
219                 
220                 [MonoTODO]
221                 XmlSchema IXmlSerializable.GetSchema ()
222                 {
223                         throw new NotImplementedException ();
224                 }
225                 
226                 void IXmlSerializable.ReadXml (XmlReader reader)
227                 {
228                         if (reader == null)
229                                 return;
230
231                         switch (reader.ReadState) {
232                         case ReadState.EndOfFile:
233                         case ReadState.Error:
234                         case ReadState.Closed:
235                                 return;
236                         }
237                         // Skip XML declaration and prolog
238                         // or do I need to validate for the <string> tag?
239                         reader.MoveToContent ();
240                         if (reader.EOF)
241                                 return;
242                         
243                         reader.Read ();
244                         if (reader.NodeType == XmlNodeType.EndElement)
245                                 return;
246
247                         if (reader.Value.Length > 0) {
248                                 if (String.Compare ("Null", reader.Value) == 0) {
249                                         // means a null reference/invalid value
250                                         notNull = false;
251                                         return; 
252                                 }
253                                 // FIXME: Validate the value for expected format
254                                 this.buffer = reader.Value.ToCharArray ();
255                                 this.notNull = true;
256                                 this.storage = StorageState.Buffer;
257                         }
258                 }
259                 
260                 void IXmlSerializable.WriteXml (XmlWriter writer) 
261                 {
262                         writer.WriteString (this.buffer.ToString ());
263                 }
264
265                 [MonoTODO]
266                 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
267                 {
268                         throw new NotImplementedException ();
269                 }
270                                                                                 
271                 #endregion
272         }
273 }
274
275 #endif