DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
XmlFunctions.cs
Go to the documentation of this file.
1 using System;
2 using System.IO;
3 using System.Text;
4 
5 using Deveel.Data.Routines;
6 using Deveel.Data.Sql.Objects;
7 using Deveel.Data.Types;
8 
9 namespace Deveel.Data.Xml {
10  public static class XmlFunctions {
11  public static IRoutineResolver Resolver {
12  get { return new XmlFunctionProvider(); }
13  }
14 
15  private static string GetXPath(DataObject xpath) {
16  if (!(xpath.Type is StringType))
17  throw new ArgumentException();
18 
19  return xpath.AsVarChar().Value.ToString();
20  }
21 
22  private static SqlXmlNode GetXmlNode(DataObject node) {
23  if (!(node.Type is XmlNodeType))
24  throw new ArgumentException();
25 
26  return node.Value as SqlXmlNode;
27  }
28 
29  public static DataObject XmlType(DataObject obj) {
30  if (obj.IsNull)
32 
33  var value = obj.Value;
34  SqlXmlNode xmlNode;
35 
36  if (value is ISqlBinary) {
37  xmlNode = XmlType((ISqlBinary) value);
38  } else if (value is ISqlString) {
39  xmlNode = XmlType((ISqlString) value);
40  } else {
41  throw new NotSupportedException();
42  }
43 
44  return new DataObject(XmlNodeType.XmlType, xmlNode);
45  }
46 
47  public static SqlXmlNode XmlType(ISqlBinary binary) {
48  var len = binary.Length;
49  var content = new byte[len];
50  var offset = 0;
51 
52  const int bufferSize = 1024 * 10;
53 
54  using (var stream = binary.GetInput()) {
55  using (var reader = new BinaryReader(stream)) {
56  while (true) {
57  var buffer = new byte[bufferSize];
58  var readCount = reader.Read(buffer, 0, bufferSize);
59 
60  Array.Copy(buffer, 0, content, offset, readCount);
61 
62  if (readCount == 0)
63  break;
64 
65  offset += readCount;
66  }
67  }
68  }
69 
70  return new SqlXmlNode(content);
71  }
72 
73  public static SqlXmlNode XmlType(ISqlString s) {
74  var len = s.Length;
75  var content = new char[len];
76  var offset = 0;
77 
78  const int bufferSize = 1024*10;
79 
80  using (var reader = s.GetInput(s.Encoding)) {
81  while (true) {
82  var buffer = new char[bufferSize];
83  var readCount = reader.Read(buffer, 0, bufferSize);
84 
85  if (readCount == 0)
86  break;
87 
88  Array.Copy(buffer, 0, content, offset, readCount);
89 
90  offset += readCount;
91  }
92  }
93 
94  var bytes = s.Encoding.GetBytes(content);
95  if (!s.Encoding.Equals(Encoding.UTF8))
96  bytes = Encoding.Convert(s.Encoding, Encoding.UTF8, bytes);
97 
98  return new SqlXmlNode(bytes);
99  }
100 
101  public static DataObject AppendChild(DataObject obj, DataObject xpath, DataObject value) {
102  var result = AppendChild(GetXmlNode(obj), GetXPath(xpath), GetXmlNode(value));
103  return new DataObject(XmlNodeType.XmlType, result);
104  }
105 
106 
107  public static SqlXmlNode AppendChild(SqlXmlNode node, string xpath, SqlXmlNode value) {
108  return node.AppendChild(xpath, value);
109  }
110 
111  public static DataObject Extract(DataObject obj, DataObject xpath) {
112  var result = Extract(GetXmlNode(obj), GetXPath(xpath));
113  return new DataObject(XmlNodeType.XmlType, result);
114  }
115 
116  public static SqlXmlNode Extract(SqlXmlNode node, string xpath) {
117  return node.Extract(xpath);
118  }
119 
120  public static DataObject ExtractValue(DataObject obj, DataObject xpath) {
121  var result = ExtractValue(GetXmlNode(obj), GetXPath(xpath));
122 
123  SqlType resultType = PrimitiveTypes.String();
124  if (result is ISqlBinary)
125  resultType = PrimitiveTypes.Binary();
126  else if (result is SqlNumber)
127  resultType = PrimitiveTypes.Numeric();
128 
129  // TODO: Support more types
130 
131  return new DataObject(resultType, result);
132  }
133 
134  public static ISqlObject ExtractValue(SqlXmlNode node, string xpath) {
135  return node.ExtractValue(xpath);
136  }
137 
138  public static DataObject Delete(DataObject obj, DataObject xpath) {
139  var result = Delete(GetXmlNode(obj), GetXPath(xpath));
140  return new DataObject(XmlNodeType.XmlType, result);
141  }
142 
143  public static SqlXmlNode Delete(SqlXmlNode node, string xpath) {
144  return node.Delete(xpath);
145  }
146 
147  public static DataObject Exists(DataObject obj, DataObject xpath) {
148  return Exists(obj, GetXPath(xpath));
149  }
150 
151  public static DataObject Exists(DataObject obj, string xpath) {
152  throw new NotImplementedException();
153  }
154 
155  public static DataObject InsertChild(DataObject obj, DataObject xpath, DataObject child, DataObject value) {
156  throw new NotImplementedException();
157  }
158 
159  public static SqlXmlNode InsertChild(SqlXmlNode node, string xpath, SqlXmlNode child, SqlXmlNode value) {
160  return node.InsertChild(xpath, child, value);
161  }
162 
163  public static DataObject InsertBefore(DataObject obj, DataObject xpath, DataObject value) {
164  return InsertBefore(obj, GetXPath(xpath), value);
165  }
166 
167  public static DataObject InsertBefore(DataObject obj, string xpath, DataObject value) {
168  throw new NotImplementedException();
169  }
170 
171  public static SqlXmlNode Update(SqlXmlNode node, string xpath, DataObject value) {
172  return node.Update(xpath, value.Value);
173  }
174 
175  public static DataObject Update(DataObject obj, DataObject xpath, DataObject value) {
176  var result = Update(GetXmlNode(obj), GetXPath(xpath), value);
177  return new DataObject(XmlNodeType.XmlType, result);
178  }
179  }
180 }
Provides some helper functions for resolving and creating SqlType instances that are primitive to the...
SqlXmlNode Extract(string xpath)
Definition: SqlXmlNode.cs:121
The system uses instances of this interface to resolve routines given a user invocation.
static DataObject InsertChild(DataObject obj, DataObject xpath, DataObject child, DataObject value)
bool IsNull
Gets a value that indicates if this object is materialized as null.
Definition: DataObject.cs:91
static DataObject Extract(DataObject obj, DataObject xpath)
SqlType Type
Gets the SqlType that defines the object properties
Definition: DataObject.cs:78
static DataObject InsertBefore(DataObject obj, DataObject xpath, DataObject value)
static SqlXmlNode InsertChild(SqlXmlNode node, string xpath, SqlXmlNode child, SqlXmlNode value)
static SqlXmlNode XmlType(ISqlString s)
Definition: XmlFunctions.cs:73
static DataObject InsertBefore(DataObject obj, string xpath, DataObject value)
SqlXmlNode InsertChild(string xpath, SqlXmlNode child, SqlXmlNode value)
Definition: SqlXmlNode.cs:206
SqlXmlNode AppendChild(string xpath, SqlXmlNode value)
Definition: SqlXmlNode.cs:190
static BinaryType Binary(int maxSize)
static DataObject Update(DataObject obj, DataObject xpath, DataObject value)
ISqlObject Value
Gets the underlined value that is handled.
Definition: DataObject.cs:84
long Length
Gets the raw length of the binary object.
Definition: ISqlBinary.cs:29
static DataObject ExtractValue(DataObject obj, DataObject xpath)
static SqlXmlNode AppendChild(SqlXmlNode node, string xpath, SqlXmlNode value)
static readonly SqlNull Value
Definition: SqlNull.cs:24
static DataObject Exists(DataObject obj, DataObject xpath)
Defines the contract for a valid SQL Object
Definition: ISqlObject.cs:23
static string GetXPath(DataObject xpath)
Definition: XmlFunctions.cs:15
DataObject AsVarChar()
Definition: DataObject.cs:528
static SqlXmlNode Extract(SqlXmlNode node, string xpath)
static NumericType Numeric()
static SqlXmlNode GetXmlNode(DataObject node)
Definition: XmlFunctions.cs:22
static DataObject XmlType(DataObject obj)
Definition: XmlFunctions.cs:29
static SqlXmlNode Delete(SqlXmlNode node, string xpath)
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
static SqlXmlNode XmlType(ISqlBinary binary)
Definition: XmlFunctions.cs:47
SqlXmlNode Delete(string xpath)
Definition: SqlXmlNode.cs:194
static ISqlObject ExtractValue(SqlXmlNode node, string xpath)
Defines the properties of a specific SQL Type and handles the values compatible.
Definition: SqlType.cs:33
TextReader GetInput(Encoding encoding)
ISqlObject ExtractValue(string xpath)
Definition: SqlXmlNode.cs:137
static DataObject Delete(DataObject obj, DataObject xpath)
Stream GetInput()
Gets an object used to read the contents of the binary
static SqlXmlNode Update(SqlXmlNode node, string xpath, DataObject value)
bool Update(string xpath, object value, string xmlNs, out byte[] updated)
Definition: SqlXmlNode.cs:105
Defines the required contract of a SQL BINARY object
Definition: ISqlBinary.cs:25
static DataObject Exists(DataObject obj, string xpath)
static DataObject AppendChild(DataObject obj, DataObject xpath, DataObject value)