2 // System.Data.SqlTypes.SqlInt16
5 // Tim Coleman <tim@timcoleman.com>
6 // Ville Palo <vi64pa@koti.soon.fi>
8 // (C) Copyright 2002 Tim Coleman
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.Xml.Schema;
37 using System.Globalization;
38 using System.Xml.Serialization;
40 namespace System.Data.SqlTypes
43 [SerializableAttribute]
44 [XmlSchemaProvider ("GetXsdType")]
46 public struct SqlInt16 : INullable, IComparable
56 public static readonly SqlInt16 MaxValue = new SqlInt16 (32767);
57 public static readonly SqlInt16 MinValue = new SqlInt16 (-32768);
58 public static readonly SqlInt16 Null;
59 public static readonly SqlInt16 Zero = new SqlInt16 (0);
65 public SqlInt16 (short value)
76 get { return !notNull; }
82 throw new SqlNullValueException ();
92 public static SqlInt16 Add (SqlInt16 x, SqlInt16 y)
97 public static SqlInt16 BitwiseAnd (SqlInt16 x, SqlInt16 y)
102 public static SqlInt16 BitwiseOr (SqlInt16 x, SqlInt16 y)
107 public int CompareTo (object value)
111 else if (!(value is SqlInt16))
112 throw new ArgumentException (Locale.GetText ("Value is not a System.Data.SqlTypes.SqlInt16"));
113 return CompareSqlInt16 ((SqlInt16) value);
117 public int CompareTo (SqlInt16 value)
119 return CompareSqlInt16 (value);
123 private int CompareSqlInt16 (SqlInt16 value)
125 if (((SqlInt16)value).IsNull)
128 return this.value.CompareTo (value.Value);
131 public static SqlInt16 Divide (SqlInt16 x, SqlInt16 y)
136 public override bool Equals (object value)
138 if (!(value is SqlInt16))
140 else if (this.IsNull)
141 return ((SqlInt16)value).IsNull;
142 else if (((SqlInt16)value).IsNull)
145 return (bool)(this == (SqlInt16)value);
148 public static SqlBoolean Equals (SqlInt16 x, SqlInt16 y)
153 public override int GetHashCode ()
158 public static SqlBoolean GreaterThan (SqlInt16 x, SqlInt16 y)
163 public static SqlBoolean GreaterThanOrEqual (SqlInt16 x, SqlInt16 y)
168 public static SqlBoolean LessThan (SqlInt16 x, SqlInt16 y)
173 public static SqlBoolean LessThanOrEqual (SqlInt16 x, SqlInt16 y)
178 public static SqlInt16 Mod (SqlInt16 x, SqlInt16 y)
184 public static SqlInt16 Modulus (SqlInt16 x, SqlInt16 y)
190 public static SqlInt16 Multiply (SqlInt16 x, SqlInt16 y)
195 public static SqlBoolean NotEquals (SqlInt16 x, SqlInt16 y)
200 public static SqlInt16 OnesComplement (SqlInt16 x)
208 public static SqlInt16 Parse (string s)
211 return new SqlInt16 (Int16.Parse (s));
215 public static SqlInt16 Subtract (SqlInt16 x, SqlInt16 y)
220 public SqlBoolean ToSqlBoolean ()
222 return ((SqlBoolean) this);
225 public SqlByte ToSqlByte ()
227 return ((SqlByte) this);
230 public SqlDecimal ToSqlDecimal ()
232 return ((SqlDecimal) this);
235 public SqlDouble ToSqlDouble ()
237 return ((SqlDouble) this);
240 public SqlInt32 ToSqlInt32 ()
242 return ((SqlInt32) this);
245 public SqlInt64 ToSqlInt64 ()
247 return ((SqlInt64) this);
250 public SqlMoney ToSqlMoney ()
252 return ((SqlMoney) this);
255 public SqlSingle ToSqlSingle ()
257 return ((SqlSingle) this);
260 public SqlString ToSqlString ()
262 return ((SqlString) this);
265 public override string ToString ()
270 return value.ToString ();
273 public static SqlInt16 Xor (SqlInt16 x, SqlInt16 y)
278 public static SqlInt16 operator + (SqlInt16 x, SqlInt16 y)
281 return new SqlInt16 ((short) (x.Value + y.Value));
285 public static SqlInt16 operator & (SqlInt16 x, SqlInt16 y)
287 return new SqlInt16 ((short) (x.value & y.Value));
290 public static SqlInt16 operator | (SqlInt16 x, SqlInt16 y)
292 return new SqlInt16 ((short) ( x.Value | y.Value));
295 public static SqlInt16 operator / (SqlInt16 x, SqlInt16 y)
298 return new SqlInt16 ((short) (x.Value / y.Value));
302 public static SqlBoolean operator == (SqlInt16 x, SqlInt16 y)
304 if (x.IsNull || y.IsNull)
305 return SqlBoolean.Null;
307 return new SqlBoolean (x.Value == y.Value);
310 public static SqlInt16 operator ^ (SqlInt16 x, SqlInt16 y)
312 return new SqlInt16 ((short) (x.Value ^ y.Value));
315 public static SqlBoolean operator > (SqlInt16 x, SqlInt16 y)
317 if (x.IsNull || y.IsNull)
318 return SqlBoolean.Null;
320 return new SqlBoolean (x.Value > y.Value);
323 public static SqlBoolean operator >= (SqlInt16 x, SqlInt16 y)
325 if (x.IsNull || y.IsNull)
326 return SqlBoolean.Null;
328 return new SqlBoolean (x.Value >= y.Value);
331 public static SqlBoolean operator != (SqlInt16 x, SqlInt16 y)
333 if (x.IsNull || y.IsNull)
334 return SqlBoolean.Null;
336 return new SqlBoolean (!(x.Value == y.Value));
339 public static SqlBoolean operator < (SqlInt16 x, SqlInt16 y)
341 if (x.IsNull || y.IsNull)
342 return SqlBoolean.Null;
344 return new SqlBoolean (x.Value < y.Value);
347 public static SqlBoolean operator <= (SqlInt16 x, SqlInt16 y)
349 if (x.IsNull || y.IsNull)
350 return SqlBoolean.Null;
352 return new SqlBoolean (x.Value <= y.Value);
355 public static SqlInt16 operator % (SqlInt16 x, SqlInt16 y)
357 return new SqlInt16 ((short) (x.Value % y.Value));
360 public static SqlInt16 operator * (SqlInt16 x, SqlInt16 y)
363 return new SqlInt16 ((short) (x.Value * y.Value));
367 public static SqlInt16 operator ~ (SqlInt16 x)
372 return new SqlInt16 ((short) (~x.Value));
375 public static SqlInt16 operator - (SqlInt16 x, SqlInt16 y)
378 return new SqlInt16 ((short) (x.Value - y.Value));
382 public static SqlInt16 operator - (SqlInt16 x)
385 return new SqlInt16 ((short) (-x.Value));
389 public static explicit operator SqlInt16 (SqlBoolean x)
394 return new SqlInt16 ((short) x.ByteValue);
397 public static explicit operator SqlInt16 (SqlDecimal x)
403 return new SqlInt16 ((short) x.Value);
407 public static explicit operator SqlInt16 (SqlDouble x)
412 return new SqlInt16 (checked ((short) x.Value));
415 public static explicit operator short (SqlInt16 x)
420 public static explicit operator SqlInt16 (SqlInt32 x)
426 return new SqlInt16 ((short) x.Value);
430 public static explicit operator SqlInt16 (SqlInt64 x)
436 return new SqlInt16 ((short) x.Value);
441 public static explicit operator SqlInt16 (SqlMoney x)
447 return new SqlInt16 ((short) Math.Round (x.Value));
452 public static explicit operator SqlInt16 (SqlSingle x)
458 return new SqlInt16 ((short) x.Value);
463 public static explicit operator SqlInt16 (SqlString x)
468 return SqlInt16.Parse (x.Value);
471 public static implicit operator SqlInt16 (short x)
473 return new SqlInt16 (x);
476 public static implicit operator SqlInt16 (SqlByte x)
478 return new SqlInt16 ((short) x.Value);
482 public static XmlQualifiedName GetXsdType (XmlSchemaSet schemaSet)
484 if (schemaSet != null && schemaSet.Count == 0) {
485 XmlSchema xs = new XmlSchema ();
486 XmlSchemaComplexType ct = new XmlSchemaComplexType ();
491 return new XmlQualifiedName ("short", "http://www.w3.org/2001/XMLSchema");
494 XmlSchema IXmlSerializable.GetSchema ()
499 void IXmlSerializable.ReadXml (XmlReader reader)
504 switch (reader.ReadState) {
505 case ReadState.EndOfFile:
506 case ReadState.Error:
507 case ReadState.Closed:
511 // Skip XML declaration and prolog
512 // or do I need to validate for the <SqlInt16> tag?
513 reader.MoveToContent ();
518 if (reader.NodeType == XmlNodeType.EndElement)
521 if (reader.Value.Length > 0) {
522 if (String.Compare ("Null", reader.Value) == 0) {
523 // means a null reference/invalid value
527 // FIXME: Validate the value for expected format
528 this.value = Int16.Parse (reader.Value);
533 void IXmlSerializable.WriteXml (XmlWriter writer)
535 writer.WriteString (this.ToString ());