DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
XmlNodeType.cs
Go to the documentation of this file.
1 using System;
2 using System.IO;
3 using System.Xml;
4 
5 using Deveel.Data.Sql.Objects;
6 using Deveel.Data.Types;
7 
8 namespace Deveel.Data.Xml {
9  public sealed class XmlNodeType : SqlType {
10  public XmlNodeType()
11  : base("XMLNODE", SqlTypeCode.Type) {
12  }
13 
14  static XmlNodeType() {
15  XmlType = new XmlNodeType();
16  }
17 
18  public static SqlType XmlType { get; private set; }
19 
20  public override object ConvertTo(ISqlObject obj, Type destType) {
21  var xmlNode = obj as SqlXmlNode;
22  if (xmlNode == null || xmlNode.IsNull)
23  return null;
24 
25  if (destType == typeof (string))
26  return xmlNode.ToString();
27  if (destType == typeof (XmlNode))
28  return xmlNode.ToXmlNode();
29  if (destType == typeof (byte[]))
30  return xmlNode.ToBytes();
31 
32  return base.ConvertTo(obj, destType);
33  }
34 
35  public override ISqlObject DeserializeObject(Stream stream) {
36  return base.DeserializeObject(stream);
37  }
38 
39  public override void SerializeObject(Stream stream, ISqlObject obj) {
40  base.SerializeObject(stream, obj);
41  }
42 
43  public override bool CanCastTo(SqlType destType) {
44  return destType is StringType ||
45  destType is BinaryType;
46  }
47 
48  public override DataObject CastTo(DataObject value, SqlType destType) {
49  var xmlNode = value.Value as SqlXmlNode;
50  if (xmlNode == null)
51  return DataObject.Null(this);
52 
53  var destTypeCode = destType.TypeCode;
54  switch (destTypeCode) {
55  case SqlTypeCode.String:
56  case SqlTypeCode.VarChar:
57  case SqlTypeCode.LongVarChar:
58  // TODO: more advanced casting...
59  return DataObject.String(xmlNode.ToSqlString());
60  case SqlTypeCode.Binary:
61  case SqlTypeCode.LongVarBinary:
62  case SqlTypeCode.VarBinary:
63  // TODO: more advanced casting...
64  return DataObject.Binary(xmlNode.ToSqlBinary());
65  default:
66  throw new InvalidCastException(String.Format("Cannot cast XML node to type '{0}'.", destType));
67  }
68  }
69  }
70 }
static DataObject Binary(SqlBinary binary)
Definition: DataObject.cs:638
A long string in the system.
override DataObject CastTo(DataObject value, SqlType destType)
Converts the given object value to a SqlType specified.
Definition: XmlNodeType.cs:48
override void SerializeObject(Stream stream, ISqlObject obj)
Definition: XmlNodeType.cs:39
static DataObject Null(SqlType type)
Definition: DataObject.cs:630
ISqlObject Value
Gets the underlined value that is handled.
Definition: DataObject.cs:84
static DataObject String(string s)
Definition: DataObject.cs:592
SqlTypeCode TypeCode
Gets the kind of SQL type this data-type handles.
Definition: SqlType.cs:91
Defines the contract for a valid SQL Object
Definition: ISqlObject.cs:23
override object ConvertTo(ISqlObject obj, Type destType)
Definition: XmlNodeType.cs:20
override ISqlObject DeserializeObject(Stream stream)
Definition: XmlNodeType.cs:35
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
Defines the properties of a specific SQL Type and handles the values compatible.
Definition: SqlType.cs:33
SqlTypeCode
Enumerates the codes of all SQL types handled by the system.
Definition: SqlTypeCode.cs:23
override bool CanCastTo(SqlType destType)
Verifies if this type can cast any value to the given SqlType.
Definition: XmlNodeType.cs:43