7 namespace Deveel.Data.Sql.Objects {
15 this.content = content;
18 navigator = CreateNavigator();
22 : this(content, false) {
25 int IComparable.CompareTo(
object obj) {
26 throw new NotSupportedException();
29 int IComparable<ISqlObject>.CompareTo(
ISqlObject other) {
30 throw new NotSupportedException();
33 public bool IsNull {
get;
private set; }
43 using (var stream =
new MemoryStream(content)) {
44 using (var xmlReader =
new StreamReader(stream, Encoding.UTF8)) {
45 var xmlDocument =
new XmlDocument();
46 xmlDocument.Load(xmlReader);
47 return xmlDocument.CreateNavigator();
53 XmlNamespaceManager nsManager = null;
54 if (!String.IsNullOrEmpty(xmlNs)) {
55 var nameTable =
new NameTable();
57 nsManager =
new XmlNamespaceManager(nameTable);
64 if (!navigator.CanEdit)
65 throw new NotSupportedException(
"The current node cannot be edited.");
69 var nodeNavigator = navigator.SelectSingleNode(xpath, NamespaceManager(xmlNs));
70 if (nodeNavigator == null)
73 return AsBinary(nodeNavigator);
76 private static byte[]
AsBinary(XPathNavigator navigator) {
77 using (var stream =
new MemoryStream()) {
78 using (var writer =
new StreamWriter(stream, Encoding.UTF8)) {
79 using (var xmlWriter =
new XmlTextWriter(writer)) {
80 xmlWriter.Formatting = Formatting.None;
82 navigator.WriteSubtree(xmlWriter);
86 return stream.ToArray();
92 private bool SelectSingleValue(
string xpath,
string xmlNs, out
object value, out Type valueType) {
96 var nodeNavigator = navigator.SelectSingleNode(xpath, NamespaceManager(xmlNs));
97 if (nodeNavigator == null)
100 valueType = nodeNavigator.ValueType;
101 value = nodeNavigator.TypedValue;
105 private bool Update(
string xpath,
object value,
string xmlNs, out byte[] updated) {
110 var rootNavigator = navigator.Clone();
111 var nodeNavigator = rootNavigator.SelectSingleNode(xpath, NamespaceManager(xmlNs));
112 if (nodeNavigator == null)
115 nodeNavigator.SetValue(value.ToString());
117 updated = AsBinary(rootNavigator);
122 return Extract(xpath, null);
129 var bytes = SelectSingle(xpath, xmlNs);
138 return ExtractValue(xpath, null);
147 if (!SelectSingleValue(xpath, xmlNs, out value, out valueType))
150 if (valueType == typeof(
string))
155 throw new NotSupportedException();
159 return Update(xpath, value, null);
164 if (!Update(xpath, value, xmlNs, out updated))
174 var bytes = Encoding.Convert(Encoding.UTF8, Encoding.Unicode, content);
191 throw new NotImplementedException();
195 throw new NotImplementedException();
199 throw new NotImplementedException();
207 throw new NotImplementedException();
SqlXmlNode Extract(string xpath)
static byte[] AsBinary(XPathNavigator navigator)
SqlXmlNode(byte[] content)
SqlXmlNode InsertChild(string xpath, SqlXmlNode child, SqlXmlNode value)
SqlXmlNode AppendChild(string xpath, SqlXmlNode value)
SqlXmlNode(byte[] content, bool isNull)
Implements a BINARY object that handles a limited number of bytes, not exceding MaxLength.
byte[] SelectSingle(string xpath, string xmlNs)
static readonly SqlNull Value
Defines the contract for a valid SQL Object
A user-defined TYPE that holds complex objects in a database column.
ISqlObject ExtractValue(string xpath, string xmlNs)
XPathNavigator CreateNavigator()
SqlXmlNode Delete(string xpath)
SqlXmlNode Extract(string xpath, string xmlNs)
bool IsComparableTo(ISqlObject other)
Checks if the current object is comparable with the given one.
ISqlObject ExtractValue(string xpath)
SqlXmlNode Update(string xpath, ISqlObject value, string xmlNs)
bool SelectSingleValue(string xpath, string xmlNs, out object value, out Type valueType)
bool Update(string xpath, object value, string xmlNs, out byte[] updated)
Deveel.Data.Sql.Objects.SqlString SqlString
SqlXmlNode Update(string xpath, ISqlObject value)
static readonly SqlBinary Null
XmlNamespaceManager NamespaceManager(string xmlNs)