using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Diagnostics;
using System.Runtime;
using System.Runtime.Serialization;
using Tdo;
using Tdo.Common;
using Tdo.Common.Entities;
using Tdo.Common.Entities.Tables;
using Tdo.Common.Entities.Views;
using Tdo.Common.Helper;
using Tdo.Common.TdoSqlExpressionDom;
using Tdo.Common.TdoTypes;
namespace Tdo.Common.TdoTypes{
/// <summary>
/// TdoXml class. Allows to keep values of SqlXml types.
/// </summary>
[CLSCompliant(true)]
[Serializable()]
public sealed class TdoXml : TdoTypeBase<SqlXml>, ITdoColumn
{
#region Static Members
/// <summary>
/// TdoXml to SqlXml converter.
/// </summary>
/// <param name="xmlFragment">The XML fragment.</param>
/// <returns></returns>
public static SqlXml ToSqlXml(string xmlFragment)
{
XmlReader r = XmlReader.Create(new StringReader(xmlFragment));
return new SqlXml(r);
}
#endregion Static Members
#region Constructors
/// <summary>
/// Default constructor for TdoXml Class
/// </summary>
public TdoXml()
{
this.actualValue = SqlXml.Null;
}
/// <summary>
/// Constructor for TdoXml Class.
/// </summary>
/// <param name="value">Column constant value</param>
/// <param name="columnAlias">Alias column.</param>
public TdoXml(SqlXml value, string columnAlias)
{
this.Caption = this.ColumnName = columnAlias;
this.actualValue = value;
this.IsConstant = true;
}
/// <summary>
/// Constructor for TdoXml Class.
/// </summary>
/// <param name="xmlReader">An System.Xml.XmlReader-derived class instance to be used as the value of the new System.Data.SqlTypes.SqlXml instance.</param>
/// <param name="columnAlias">Alias column.</param>
public TdoXml(XmlReader xmlReader, string columnAlias)
{
this.actualValue = new SqlXml(xmlReader);
this.Caption = this.ColumnName = columnAlias;
this.IsConstant = true;
}
/// <summary>
/// Constructor for TdoXml Class.
/// </summary>
/// <param name="stream">A System.IO.Stream-derived instance (such as System.IO.FileStream) from which to load the System.Data.SqlTypes.SqlXml instance's Xml content.</param>
/// <param name="columnAlias">Alias column.</param>
public TdoXml(Stream stream, string columnAlias)
{
this.actualValue = new SqlXml(stream);
this.Caption = this.ColumnName = columnAlias;
this.IsConstant = true;
}
/// <summary>
/// Constructor for TdoXml Class.
/// </summary>
/// <param name="xmlFragment">The string containing the XML fragment to parse.</param>
/// <param name="conformanceLevel">The level of conformance which the System.Xml.XmlReader will comply.</param>
/// <param name="columnAlias">Alias column.</param>
public TdoXml(string xmlFragment, ConformanceLevel conformanceLevel, string columnAlias)
{
XmlReader reader = null;
XmlReaderSettings settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Fragment;
reader = XmlReader.Create(new StringReader(xmlFragment), settings);
this.actualValue = new SqlXml(reader);
this.Caption = this.ColumnName = columnAlias;
this.IsConstant = true;
}
/// <summary>
/// Constructor for TdoXml Class.
/// </summary>
/// <param name="xmlDocument">System.Xml.XmlDocument from wich read xml.</param>
/// <param name="columnAlias">Alias column.</param>
public TdoXml(XmlDocument xmlDocument, string columnAlias)
: this(xmlDocument.OuterXml, ConformanceLevel.Document, columnAlias)
{
}
/// <summary>
/// Constructor for TdoXml Class.
/// </summary>
/// <param name="xmlNodeList">System.Xml.XmlNodeList from wich read xml nodes.</param>
/// <param name="columnAlias">Alias column.</param>
public TdoXml(XmlNodeList xmlNodeList, string columnAlias)
{
StringBuilder sb = new StringBuilder();
foreach (XmlNode xmlNode in xmlNodeList)
{
sb = sb.Append(xmlNode.OuterXml);
}
this.actualValue = new TdoXml(sb.ToString(), ConformanceLevel.Fragment, String.Empty).Value;
this.Caption = this.ColumnName = columnAlias;
this.IsConstant = true;
}
/// <summary>
/// Constructor for TdoXml Class.
/// </summary>
/// <param name="xmlNode">System.Xml.XmlNode from wich read xml node.</param>
/// <param name="columnAlias">Alias column.</param>
public TdoXml(XmlNode xmlNode, string columnAlias)
: this(xmlNode.OuterXml, ConformanceLevel.Fragment, String.Empty)
{
}
/// <summary>
/// Constructor for TdoXml class
/// </summary>
/// <param name="allowDBNull">Boolean that indicates wether the field can be NULL</param>
/// <param name="autoIncrement">Boolean that indicates if the field is IDENTITY (auto-increase)</param>
/// <param name="autoIncrementSeed">Long that indicates how much is increasing the IDENTITY field</param>
/// <param name="autoIncrementStep">Long that indicates the start actualValue of the IDENTITY field </param>
/// <param name="caption">String that indicates the heading/columnAlias of the field</param>
/// <param name="columnName">String that indicates the name of the column (field)</param>
/// <param name="maxLength">Integer that indicates the maximum length of the field (-1,field without limit)</param>
/// <param name="readOnly">Boolean that indicates if the field is only reading</param>
/// <param name="unique">Boolean that indicates if a unique constraint exists on that field</param>
/// <param name="tdoEntity">TdoEntity referenced to the class that contains the field</param>
public TdoXml(bool allowDBNull, bool autoIncrement, long autoIncrementSeed,
long autoIncrementStep, string caption, string columnName,
int maxLength, bool readOnly,
bool unique, ITdoEntity tdoEntity)
:
base(allowDBNull, autoIncrement, autoIncrementSeed,
autoIncrementStep, caption, columnName, maxLength,
readOnly,
unique, tdoEntity)
{
this.actualValue = SqlXml.Null;
}
#endregion Constructors
#region Properties
/// <summary>
/// Get or Set Value property of Object type
/// </summary>
[XmlIgnore]
public override object ObjectValue
{
get
{
return this.actualValue;
}
set
{
if (value == null || value == DBNull.Value || value == SqlXml.Null)
{
this.actualValue = SqlXml.Null;
}
if (value as string != null)
{
this.actualValue = new TdoXml((string)value, ConformanceLevel.Auto, String.Empty).Value;
return;
}
if (value as SqlXml != null)
{
this.actualValue = TdoXml.ClonedValue((SqlXml)value);
return;
}
this.pModified = !this.ReadOnly;
}
}
/// <summary>
/// System.Data.SqlDbType of ITdoColumn
/// </summary>
[XmlAttribute()]
public override SqlDbType SqlDbType
{
get
{
return System.Data.SqlDbType.Xml;
}
}
#endregion Properties
#region Methods
/// <summary>
/// Comparison operator Equals
/// </summary>
/// <param name="obj">Object to compare</param>
public override bool Equals(object obj)
{
return base.Equals(obj);
}
/// <summary>
/// Returns Hash code
/// </summary>
public override int GetHashCode()
{
return base.GetHashCode();
}
internal override SqlXml ClonedValue(bool actualValue)
{
return (actualValue ? TdoXml.ClonedValue(this.actualValue) : TdoXml.ClonedValue(this.oldValue));
}
/// <summary>
/// Return a copy of SqlXml structure
/// </summary>
/// <param name="toClone">SqlXml to copy</param>
/// <returns>SqlXml structure copied</returns>
public static SqlXml ClonedValue(SqlXml toClone)
{
SqlXml res;
if (toClone.IsNull)
{
res = SqlXml.Null;
}
else
{
res = new TdoXml(toClone.Value, ConformanceLevel.Auto, String.Empty).Value;
}
return res;
}
/// <summary>
/// Creates a copy of this TdoXml object.
/// </summary>
/// <returns></returns>
public override ITdoType<SqlXml> Clone()
{
TdoXml clone = new TdoXml(this.pAllowDBNull, this.pAutoIncrement, this.pAutoIncrementSeed, this.pAutoIncrementStep, this.pCaption, this.pColumnName, this.pMaxLength, this.pReadOnly, this.pUnique, this.pTdoEntity);
clone.Value = this.ClonedValue();
return clone;
}
/// <summary>
/// Returns the string representation of the content of the field
/// </summary>
/// <returns>string</returns>
public override string ToString()
{
return this.Value.Value;
}
/// <summary>
/// Returns a System.Xml.XmlNode array if SqlXml.Value is not SqlXml.Null. Otherwise null.
/// </summary>
/// <returns>System.Xml.XmlNode array</returns>
public XmlNode[] GetXmlNodes()
{
if (this.Value == null || this.Value == SqlXml.Null)
return null;
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml("<TdoRoot>" + this.Value.Value + @"</TdoRoot>");
XmlNode[] result = new XmlNode[xmldoc.ChildNodes[0].ChildNodes.Count];
for (int i = 0; i < xmldoc.ChildNodes[0].ChildNodes.Count; i++)
{
result[i] = xmldoc.ChildNodes[0].ChildNodes[i];
}
return result;
}
/// <summary>
/// Returns a System.Xml.XmlDocument even if SqlXml.Value is an xml well-formed document otherwise null.
/// </summary>
/// <returns>System.Xml.XmlDocument</returns>
public XmlDocument GetXmlDocument()
{
if (this.Value==SqlXml.Null)
return null;
XmlDocument result = new XmlDocument();
try
{
result.LoadXml(this.Value.Value);
}
catch (XmlException)
{
//Just ignore
}
return result;
}
#endregion Methods
#region Operators
/// <summary>
/// Comparison operator Equal
/// </summary>
/// <param name="left">Parameter on the left of the operator</param>
/// <param name="right">Parameter on the right of the operator</param>
/// <returns>Returns an expression like TdoSqlExpression useful to convert the where clause to string</returns>
public static TdoSqlExpression operator ==(TdoXml left, string right)
{
return new TdoSqlExpression((ITdoColumn)left, SqlOperator.Equal, right);
}
/// <summary>
/// Comparison operator Equal
/// </summary>
/// <param name="left">Parameter on the left of the operator</param>
/// <param name="right">Parameter on the right of the operator</param>
/// <returns>Returns an expression like TdoSqlExpression useful to convert the where clause to string</returns>
public static TdoSqlExpression operator ==(TdoXml left, ITdoType<SqlXml> right)
{
return new TdoSqlExpression((ITdoColumn)left, SqlOperator.Equal, right);
}
/// <summary>
/// Comparison operator is (null)
/// </summary>
/// <param name="left">Parameter on the left of the operator</param>
/// <param name="right">Parameter on the right of the operator</param>
/// <returns>Returns an expression like TdoSqlExpression useful to convert the where clause to string</returns>
public static TdoSqlExpression operator ==(TdoXml left, DBNull right)
{
return new TdoSqlExpression((ITdoColumn)left, SqlOperator.Equal, right);
}
/// <summary>
/// Comparison operator NotEqual
/// </summary>
/// <param name="left">Parameter on the left of the operator</param>
/// <param name="right">Parameter on the right of the operator</param>
/// <returns>Returns an expression like TdoSqlExpression useful to convert the where clause to string</returns>
public static TdoSqlExpression operator !=(TdoXml left, ITdoType<SqlXml> right)
{
return new TdoSqlExpression((ITdoColumn)left, SqlOperator.NotEqual, right);
}
/// <summary>
/// Comparison operator NotEqual
/// </summary>
/// <param name="left">Parameter on the left of the operator</param>
/// <param name="right">Parameter on the right of the operator</param>
/// <returns>Returns an expression like TdoSqlExpression useful to convert the where clause to string</returns>
public static TdoSqlExpression operator !=(TdoXml left, string right)
{
return new TdoSqlExpression((ITdoColumn)left, SqlOperator.NotEqual, right);
}
/// <summary>
/// Comparison operator is not (null)
/// </summary>
/// <param name="left">Parameter on the left of the operator</param>
/// <param name="right">Parameter on the right of the operator</param>
/// <returns>Returns an expression like TdoSqlExpression useful to convert the where clause to string</returns>
public static TdoSqlExpression operator !=(TdoXml left, DBNull right)
{
return new TdoSqlExpression((ITdoColumn)left, SqlOperator.NotEqual, right);
}
#endregion Operators
}
}
|