tijian_tieying/web/cyqdata-master/Table/MDataCell.cs

586 lines
18 KiB
C#
Raw Permalink Normal View History

2025-02-20 12:14:39 +08:00
using System;
using System.Data;
using System.Collections.Generic;
using CYQ.Data.SQL;
using System.ComponentModel;
using System.Data.SqlTypes;
using System.Collections;
using CYQ.Data.Tool;
namespace CYQ.Data.Table
{
/// <summary>
/// <20><>Ԫ<EFBFBD><EFBFBD><E1B9B9>ֵ
/// </summary>
internal partial class MCellValue
{
internal bool IsNull = true;
/// <summary>
/// ״̬<D7B4>ı<EFBFBD>:0;δ<><CEB4>,1;<3B><><EFBFBD>и<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>[<5B><>ֵ<EFBFBD><D6B5>ͬ],2:<3A><>ֵ,ֵ<><D6B5>ͬ<EFBFBD>ı<EFBFBD><C4B1><EFBFBD>
/// </summary>
internal int State = 0;
internal object Value = null;
}
/// <summary>
/// <20><>Ԫ<EFBFBD><D4AA>
/// </summary>
public partial class MDataCell
{
private MCellValue _CellValue;
private MCellValue CellValue
{
get
{
if (_CellValue == null)
{
_CellValue = new MCellValue();
}
return _CellValue;
}
}
private MCellStruct _CellStruct;
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// <summary>
/// ԭ<><D4AD>ģʽ<C4A3><CABD>Prototype Method<6F><64>
/// </summary>
/// <param name="dataStruct"></param>
internal MDataCell(ref MCellStruct dataStruct)
{
Init(dataStruct, null);
}
internal MDataCell(ref MCellStruct dataStruct, object value)
{
Init(dataStruct, value);
}
#endregion
#region <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
private void Init(MCellStruct dataStruct, object value)
{
_CellStruct = dataStruct;
if (value != null)
{
_CellValue = new MCellValue();
Value = value;
}
}
#endregion
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
private string _StringValue = null;
/// <summary>
/// <20>ַ<EFBFBD><D6B7><EFBFBD>ֵ
/// </summary>
public string StringValue
{
get
{
CheckNewValue();
return _StringValue;
}
internal set
{
_StringValue = value;
}
}
private object newValue = null;
private bool isNewValue = false;
/// <summary>
/// ֵ
/// </summary>
public object Value
{
get
{
CheckNewValue();
return CellValue.Value;
}
set
{
//ֻ<>Ǹ<EFBFBD>ֵ<EFBFBD><D6B5>ֵ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>
newValue = value;
isNewValue = true;
isAllowChangeState = true;
}
}
internal object SourceValue
{
set
{
CellValue.Value = value;
}
}
/// <summary>
/// <20><>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
private void CheckNewValue()
{
if (isNewValue)
{
isNewValue = false;
FixValue(newValue);
newValue = null;
isAllowChangeState = true;//<2F>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><CCAC>
}
}
private void FixValue(object value)
{
#region CheckValue
bool valueIsNull = value == null || value == DBNull.Value;
if (valueIsNull)
{
if (CellValue.IsNull)
{
CellValue.State = (value == DBNull.Value) ? 2 : 1;
}
else
{
if (isAllowChangeState)
{
CellValue.State = 2;
}
CellValue.Value = null;
CellValue.IsNull = true;
StringValue = string.Empty;
}
}
else
{
StringValue = value.ToString();
int groupID = DataType.GetGroup(_CellStruct.SqlType);
if (_CellStruct.SqlType != SqlDbType.Variant)
{
if (StringValue == "" && groupID > 0)
{
CellValue.Value = null;
CellValue.IsNull = true;
return;
}
value = ChangeValue(value, _CellStruct.ValueType, groupID);
if (value == null)
{
return;
}
}
if (!CellValue.IsNull && (CellValue.Value.Equals(value) || (groupID != 999 && CellValue.Value.ToString() == StringValue)))//<2F><><EFBFBD><EFBFBD><EFBFBD>ıȽ<C4B1>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>==<3D><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>ַ<EFBFBD><D6B7>
{
if (isAllowChangeState)
{
CellValue.State = 1;
}
}
else
{
CellValue.Value = value;
CellValue.IsNull = false;
if (isAllowChangeState)
{
CellValue.State = 2;
}
}
}
#endregion
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͱ<EFBFBD><CDB1>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD>͡<EFBFBD>
/// </summary>
public bool FixValue()
{
Exception err = null;
return FixValue(out err);
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͱ<EFBFBD><CDB1>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD>͡<EFBFBD>
/// </summary>
public bool FixValue(out Exception ex)
{
ex = null;
if (!IsNull)
{
CellValue.Value = ChangeValue(CellValue.Value, _CellStruct.ValueType, DataType.GetGroup(_CellStruct.SqlType), out ex);
}
return ex == null;
}
internal object ChangeValue(object value, Type convertionType, int groupID)
{
Exception err;
return ChangeValue(value, convertionType, groupID, out err);
}
/// <summary>
/// ֵ<><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>
/// </summary>
/// <param name="value">Ҫ<><D2AA>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>ֵ</param>
/// <param name="convertionType">Ҫת<D2AA><D7AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
/// <param name="groupID"><3E><><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD>͵<EFBFBD><CDB5><EFBFBD><EFBFBD><EFBFBD></param>
/// <returns></returns>
internal object ChangeValue(object value, Type convertionType, int groupID, out Exception ex)
{
ex = null;
StringValue = Convert.ToString(value);
if (value == null)
{
CellValue.IsNull = true;
return value;
}
if (groupID > 0 && StringValue == "")
{
CellValue.IsNull = true;
return null;
}
try
{
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD>
if (groupID == 1)
{
switch (StringValue)
{
case "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>":
StringValue = "Infinity";
break;
case "<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>":
StringValue = "-Infinity";
break;
}
}
if (value.GetType() != convertionType)
{
#region <EFBFBD>۵<EFBFBD>
switch (groupID)
{
case 0:
if (_CellStruct.SqlType == SqlDbType.Time)//time<6D><65><EFBFBD>͵<EFBFBD><CDB5><EFBFBD><EFBFBD><EFBFBD><E2B4A6><EFBFBD><EFBFBD>
{
string[] items = StringValue.Split(' ');
if (items.Length > 1)
{
StringValue = items[1];
}
}
value = StringValue;
break;
case 1:
switch (StringValue.ToLower())
{
case "true":
value = 1;
break;
case "false":
value = 0;
break;
case "infinity":
value = double.PositiveInfinity;
break;
case "-infinity":
value = double.NegativeInfinity;
break;
default:
goto err;
}
break;
case 2:
switch (StringValue.ToLower().TrimEnd(')', '('))
{
case "now":
case "getdate":
case "current_timestamp":
value = DateTime.Now;
break;
default:
DateTime dt = DateTime.Parse(StringValue);
value = dt == DateTime.MinValue ? (DateTime)SqlDateTime.MinValue : dt;
break;
}
break;
case 3:
switch (StringValue.ToLower())
{
case "yes":
case "true":
case "1":
case "on":
case "<22><>":
value = true;
break;
case "no":
case "false":
case "0":
case "":
case "<22><>":
default:
value = false;
break;
}
break;
case 4:
if (StringValue == SqlValue.Guid || StringValue.StartsWith("newid"))
{
value = Guid.NewGuid();
}
else
{
value = new Guid(StringValue);
}
break;
default:
err:
if (convertionType.Name.EndsWith("[]"))
{
value = Convert.FromBase64String(StringValue);
StringValue = "System.Byte[]";
}
else
{
value = StaticTool.ChangeType(value, convertionType);
}
break;
}
#endregion
}
//else if (groupID == 2 && strValue.StartsWith("000"))
//{
// value = SqlDateTime.MinValue;
//}
#endregion
}
catch (Exception err)
{
value = null;
CellValue.Value = null;
CellValue.IsNull = true;
ex = err;
string msg = string.Format("ChangeType Error<6F><72>ColumnName<6D><65>{0}<7D><>({1}) <20><> Value<75><65><EFBFBD><EFBFBD>{2}<7D><>\r\n", _CellStruct.ColumnName, _CellStruct.ValueType.FullName, StringValue);
StringValue = null;
if (AppConfig.Log.IsWriteLog)
{
Log.WriteLog(true, msg);
}
}
return value;
}
internal T Get<T>()
{
if (CellValue.IsNull)
{
return default(T);
}
Type t = typeof(T);
return (T)ChangeValue(CellValue.Value, t, DataType.GetGroup(DataType.GetSqlType(t)));
}
/// <summary>
/// ֵ<>Ƿ<EFBFBD>ΪNullֵ[ֻ<><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>]
/// </summary>
public bool IsNull
{
get
{
CheckNewValue();
return CellValue.IsNull;
}
internal set
{
CellValue.IsNull = value;
}
}
/// <summary>
/// ֵ<>Ƿ<EFBFBD>ΪNull<6C><6C>Ϊ<EFBFBD><CEAA><><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>]
/// </summary>
public bool IsNullOrEmpty
{
get
{
CheckNewValue();
return CellValue.IsNull || StringValue.Length == 0;
}
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><><D6BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>]
/// </summary>
public string ColumnName
{
get
{
return _CellStruct.ColumnName;
}
}
/// <summary>
/// <20><>Ԫ<EFBFBD><D4AA><EFBFBD>Ľṹ
/// </summary>
public MCellStruct Struct
{
get
{
return _CellStruct;
}
}
private bool isAllowChangeState = true;
/// <summary>
/// Value<75><65>״̬:0;δ<><CEB4>,1;<3B><><EFBFBD>и<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>[<5B><>ֵ<EFBFBD><D6B5>ͬ],2:<3A><>ֵ,ֵ<><D6B5>ͬ<EFBFBD>ı<EFBFBD><C4B1><EFBFBD>
/// </summary>
public int State
{
get
{
CheckNewValue();
return CellValue.State;
}
set
{
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>أ<EFBFBD><D8A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״̬<D7B4><CCAC><EFBFBD>ڻ<EFBFBD>ȡʱ<C8A1><CAB1><EFBFBD>õ<EFBFBD>״̬<D7B4><CCAC>ʧЧ<CAA7><D0A7>
if (isNewValue) { isAllowChangeState = false; }
CellValue.State = value;
}
}
#endregion
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// <summary>
/// <20><>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA>
/// </summary>
public void Clear()
{
isNewValue = false;
newValue = null;
CellValue.Value = null;
CellValue.State = 0;
CellValue.IsNull = true;
StringValue = null;
}
internal void LoadValue(MDataCell cell)
{
StringValue = cell.StringValue;
CellValue.Value = cell.Value;
CellValue.State = cell.State;
CellValue.IsNull = cell.IsNull;
}
/// <summary>
/// <20><><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC>ֵ<EFBFBD><D6B5>
/// </summary>
internal void SetDefaultValueToValue()
{
if (Convert.ToString(_CellStruct.DefaultValue).Length > 0)
{
switch (DataType.GetGroup(_CellStruct.SqlType))
{
case 2:
Value = DateTime.Now;
break;
case 4:
if (_CellStruct.DefaultValue.ToString().Length == 36)
{
Value = new Guid(_CellStruct.DefaultValue.ToString());
}
else
{
Value = Guid.NewGuid();
}
break;
default:
Value = _CellStruct.DefaultValue;
break;
}
}
}
/// <summary>
/// <20>ѱ<EFBFBD><D1B1><EFBFBD><EFBFBD>أ<EFBFBD>Ĭ<EFBFBD>Ϸ<EFBFBD><CFB7><EFBFBD>Valueֵ<65><D6B5>
/// </summary>
/// <returns></returns>
public override string ToString()
{
return StringValue ?? "";
}
/// <summary>
/// <20>Ƿ<EFBFBD>ֵ<EFBFBD><D6B5>ͬ[<5B><><EFBFBD><EFBFBD>д<EFBFBD>÷<EFBFBD><C3B7><EFBFBD>]
/// </summary>
public override bool Equals(object value)
{
bool valueIsNull = (value == null || value == DBNull.Value);
if (CellValue.IsNull)
{
return valueIsNull;
}
if (valueIsNull)
{
return CellValue.IsNull;
}
return StringValue.ToLower() == Convert.ToString(value).ToLower();
}
/// <summary>
/// ת<><D7AA><EFBFBD><EFBFBD>
/// </summary>
internal MDataRow ToRow()
{
MDataRow row = new MDataRow();
row.Add(this);
return row;
}
#endregion
}
//<2F><>չ<EFBFBD><D5B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
public partial class MDataCell
{
internal string ToXml(bool isConvertNameToLower)
{
string text = StringValue??"";
switch (DataType.GetGroup(_CellStruct.SqlType))
{
case 999:
MDataRow row = null;
MDataTable table = null;
Type t = Value.GetType();
if (!t.FullName.StartsWith("System."))//<2F><>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
row = new MDataRow(TableSchema.GetColumns(t));
row.LoadFrom(Value);
}
else if (Value is IEnumerable)
{
int len = StaticTool.GetArgumentLength(ref t);
if (len == 1)
{
table = MDataTable.CreateFrom(Value);
}
else if (len == 2)
{
row = MDataRow.CreateFrom(Value);
}
}
if (row != null)
{
text = row.ToXml(isConvertNameToLower);
}
else if (table != null)
{
text = string.Empty;
foreach (MDataRow r in table.Rows)
{
text += r.ToXml(isConvertNameToLower);
}
text += "\r\n ";
}
return string.Format("\r\n <{0}>{1}</{0}>", isConvertNameToLower ? ColumnName.ToLower() : ColumnName, text);
default:
if (text.LastIndexOfAny(new char[] { '<', '>', '&' }) > -1 && !text.StartsWith("<![CDATA["))
{
text = "<![CDATA[" + text.Trim() + "]]>";
}
return string.Format("\r\n <{0}>{1}</{0}>", isConvertNameToLower ? ColumnName.ToLower() : ColumnName, text);
}
}
}
}