2 // System.Data.SqlTypes.SqlChars
5 // Tim Coleman <tim@timcoleman.com>
7 // Copyright (C) Tim Coleman, 2003
11 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
36 using System.Globalization;
39 using System.Xml.Schema;
40 using System.Xml.Serialization;
41 using System.Runtime.Serialization;
43 namespace System.Data.SqlTypes
45 [SerializableAttribute]
46 [XmlSchemaProvider ("GetXsdType")]
47 [XmlRootAttribute ("string")]
48 public sealed class SqlChars : INullable, IXmlSerializable, ISerializable
54 StorageState storage = StorageState.UnmanagedBuffer;
66 public SqlChars (char[] buffer)
74 storage = StorageState.Buffer;
78 public SqlChars (SqlString value)
85 buffer = value.Value.ToCharArray ();
86 storage = StorageState.Buffer;
94 public char [] Buffer {
95 get { return buffer; }
99 get { return !notNull; }
102 public char this [long offset] {
104 if (notNull && offset >= 0 && offset < buffer.Length)
105 buffer [offset] = value;
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];
118 if (!notNull || buffer == null)
119 throw new SqlNullValueException ("Data is Null");
120 if (buffer.Length < 0)
122 return buffer.Length;
126 public long MaxLength {
128 if (!notNull || buffer == null || storage == StorageState.Stream)
130 return buffer.Length;
134 public static SqlChars Null {
136 return new SqlChars ();
140 public StorageState Storage {
142 if (storage == StorageState.UnmanagedBuffer)
143 throw new SqlNullValueException ("Data is Null");
148 public char [] Value {
152 return (char []) buffer.Clone ();
160 public void SetLength (long value)
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);
169 public void SetNull ()
175 public static explicit operator SqlString (SqlChars x)
178 return SqlString.Null;
180 return new SqlString (new String (x.Value));
184 public static explicit operator SqlChars (SqlString x)
189 return new SqlChars (x.Value);
192 public SqlString ToSqlString ()
194 if (buffer == null) {
195 return SqlString.Null;
198 return new SqlString (buffer.ToString ());
203 public long Read (long offset, char [] buffer, int offsetInBuffer, int count)
205 throw new NotImplementedException ();
208 [MonoNotSupported("")]
209 public void Write (long offset, char [] buffer, int offsetInBuffer, int count)
211 throw new NotImplementedException ();
214 public static XmlQualifiedName GetXsdType (XmlSchemaSet schemaSet)
216 XmlQualifiedName qualifiedName = new XmlQualifiedName ("string", "http://www.w3.org/2001/XMLSchema");
217 return qualifiedName;
221 XmlSchema IXmlSerializable.GetSchema ()
223 throw new NotImplementedException ();
226 void IXmlSerializable.ReadXml (XmlReader reader)
231 switch (reader.ReadState) {
232 case ReadState.EndOfFile:
233 case ReadState.Error:
234 case ReadState.Closed:
237 // Skip XML declaration and prolog
238 // or do I need to validate for the <string> tag?
239 reader.MoveToContent ();
244 if (reader.NodeType == XmlNodeType.EndElement)
247 if (reader.Value.Length > 0) {
248 if (String.Compare ("Null", reader.Value) == 0) {
249 // means a null reference/invalid value
253 // FIXME: Validate the value for expected format
254 this.buffer = reader.Value.ToCharArray ();
256 this.storage = StorageState.Buffer;
260 void IXmlSerializable.WriteXml (XmlWriter writer)
262 writer.WriteString (this.buffer.ToString ());
266 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
268 throw new NotImplementedException ();