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 public sealed class SqlChars : INullable, IXmlSerializable, ISerializable
53 StorageState storage = StorageState.UnmanagedBuffer;
65 public SqlChars (char[] buffer)
73 storage = StorageState.Buffer;
77 public SqlChars (SqlString value)
84 buffer = value.Value.ToCharArray ();
85 storage = StorageState.Buffer;
93 public char [] Buffer {
94 get { return buffer; }
98 get { return !notNull; }
101 public char this [long offset] {
103 if (notNull && offset >= 0 && offset < buffer.Length)
104 buffer [offset] = value;
108 throw new SqlNullValueException ("Data is Null");
109 if (offset < 0 || offset >= buffer.Length)
110 throw new ArgumentOutOfRangeException ("Parameter name: offset");
111 return buffer [offset];
117 if (!notNull || buffer == null)
118 throw new SqlNullValueException ("Data is Null");
119 if (buffer.Length < 0)
121 return buffer.Length;
125 public long MaxLength {
127 if (!notNull || buffer == null || storage == StorageState.Stream)
129 return buffer.Length;
133 public static SqlChars Null {
135 return new SqlChars ();
139 public StorageState Storage {
141 if (storage == StorageState.UnmanagedBuffer)
142 throw new SqlNullValueException ("Data is Null");
147 public char [] Value {
151 return (char []) buffer.Clone ();
159 public void SetLength (long value)
162 throw new SqlTypeException ("There is no buffer");
163 if (value < 0 || value > buffer.Length)
164 throw new ArgumentOutOfRangeException ("Specified argument was out of the range of valid values.");
165 Array.Resize (ref buffer, (int) value);
168 public void SetNull ()
174 public static explicit operator SqlString (SqlChars value)
177 return SqlString.Null;
179 return new SqlString (new String (value.Value));
183 public static explicit operator SqlChars (SqlString value)
188 return new SqlChars (value.Value);
191 public SqlString ToSqlString ()
193 if (buffer == null) {
194 return SqlString.Null;
196 return new SqlString (buffer.ToString ());
200 public long Read (long offset, char [] buffer, int offsetInBuffer, int count)
203 throw new ArgumentNullException ("buffer");
206 throw new SqlNullValueException ("There is no buffer. Read or write operation failed");
208 if (count > MaxLength || count > buffer.Length ||
209 count < 0 || ((offsetInBuffer + count) > buffer.Length))
210 throw new ArgumentOutOfRangeException ("count");
212 if (offset < 0 || offset > MaxLength)
213 throw new ArgumentOutOfRangeException ("offset");
215 if (offsetInBuffer < 0 || offsetInBuffer > buffer.Length)
216 throw new ArgumentOutOfRangeException ("offsetInBuffer");
218 /* LAMESPEC: If count specifies more characters than what is available from
219 offset to the Length of the SqlChars instance, only the available
220 characters are copied
223 /* Final count of what will be copied */
224 long actualCount = count;
225 if (count + offset > Length)
226 actualCount = Length - offset;
228 Array.Copy (this.buffer, offset, buffer, offsetInBuffer, actualCount);
233 public void Write (long offset, char [] buffer, int offsetInBuffer, int count)
236 throw new ArgumentNullException ("buffer");
239 throw new SqlTypeException ("There is no buffer. Read or write operation failed.");
242 throw new ArgumentOutOfRangeException ("offset");
244 if (offsetInBuffer < 0 || offsetInBuffer > buffer.Length
245 || offsetInBuffer > Length
246 || offsetInBuffer + count > Length
247 || offsetInBuffer + count > buffer.Length)
248 throw new ArgumentOutOfRangeException ("offsetInBuffer");
250 if (count < 0 || count > MaxLength)
251 throw new ArgumentOutOfRangeException ("count");
253 if (offset > MaxLength || offset+count > MaxLength)
254 throw new SqlTypeException ("The buffer is insufficient. Read or write operation failed.");
256 if (count + offset > Length &&
257 count + offset <= MaxLength)
260 Array.Copy (buffer, offsetInBuffer, this.buffer, offset, count);
263 public static XmlQualifiedName GetXsdType (XmlSchemaSet schemaSet)
265 if (schemaSet != null && schemaSet.Count == 0) {
266 XmlSchema xs = new XmlSchema ();
267 XmlSchemaComplexType ct = new XmlSchemaComplexType ();
272 return new XmlQualifiedName ("string", "http://www.w3.org/2001/XMLSchema");
275 XmlSchema IXmlSerializable.GetSchema ()
280 void IXmlSerializable.ReadXml (XmlReader reader)
285 switch (reader.ReadState) {
286 case ReadState.EndOfFile:
287 case ReadState.Error:
288 case ReadState.Closed:
291 // Skip XML declaration and prolog
292 // or do I need to validate for the <string> tag?
293 reader.MoveToContent ();
298 if (reader.NodeType == XmlNodeType.EndElement)
301 if (reader.Value.Length > 0) {
302 if (String.Compare ("Null", reader.Value) == 0) {
303 // means a null reference/invalid value
307 // FIXME: Validate the value for expected format
308 this.buffer = reader.Value.ToCharArray ();
310 this.storage = StorageState.Buffer;
314 void IXmlSerializable.WriteXml (XmlWriter writer)
316 writer.WriteString (this.buffer.ToString ());
320 void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
322 throw new NotImplementedException ();