846 lines
26 KiB
C#
846 lines
26 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Data;
|
|||
|
|
using CYQ.Data.SQL;
|
|||
|
|
using CYQ.Data.Tool;
|
|||
|
|
using System.IO;
|
|||
|
|
using CYQ.Data.Json;
|
|||
|
|
|
|||
|
|
namespace CYQ.Data.Table
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// ͷ<>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public partial class MDataColumn
|
|||
|
|
{
|
|||
|
|
List<MCellStruct> structList;
|
|||
|
|
internal MDataTable _Table;
|
|||
|
|
internal MDataColumn(MDataTable table)
|
|||
|
|
{
|
|||
|
|
structList = new List<MCellStruct>();
|
|||
|
|
_Table = table;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public MDataColumn()
|
|||
|
|
{
|
|||
|
|
structList = new List<MCellStruct>();
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ҫˢ<D2AA><CBA2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
internal bool IsNeedRefleshIndex = false;
|
|||
|
|
|
|||
|
|
private int _CheckDuplicateState = -1;
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ظ<EFBFBD>(Ĭ<><C4AC>Ϊtrue)<29><>
|
|||
|
|
/// </summary>
|
|||
|
|
public bool CheckDuplicate
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
//return true;
|
|||
|
|
if (_CheckDuplicateState == -1)
|
|||
|
|
{
|
|||
|
|
return structList.Count < 100;//<2F>ж<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>Ӱ<EFBFBD><D3B0><EFBFBD><EFBFBD><EFBFBD>ܣ<EFBFBD>Ĭ<EFBFBD>ϳ<EFBFBD><CFB3><EFBFBD>100<30><30><EFBFBD><EFBFBD><F3A3ACB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD><D8B8>
|
|||
|
|
}
|
|||
|
|
return _CheckDuplicateState == 1;
|
|||
|
|
}
|
|||
|
|
set
|
|||
|
|
{
|
|||
|
|
_CheckDuplicateState = value ? 1 : 0;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ʽת<CABD><D7AA><EFBFBD><EFBFBD>ͷ
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="columns"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static implicit operator MDataColumn(DataColumnCollection columns)
|
|||
|
|
{
|
|||
|
|
if (columns == null)
|
|||
|
|
{
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
MDataColumn mColumns = new MDataColumn();
|
|||
|
|
|
|||
|
|
if (columns.Count > 0)
|
|||
|
|
{
|
|||
|
|
for (int i = 0; i < columns.Count; i++)
|
|||
|
|
{
|
|||
|
|
MCellStruct cellStruct = new MCellStruct(columns[i].ColumnName, DataType.GetSqlType(columns[i].DataType), columns[i].ReadOnly, columns[i].AllowDBNull, columns[i].MaxLength);
|
|||
|
|
mColumns.Add(cellStruct);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return mColumns;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public MCellStruct this[string key]
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
int index = GetIndex(key);
|
|||
|
|
if (index > -1)
|
|||
|
|
{
|
|||
|
|
return this[index];
|
|||
|
|
}
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>ܹ<EFBFBD><DCB9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>õı<C3B5>
|
|||
|
|
/// </summary>
|
|||
|
|
[JsonIgnore]
|
|||
|
|
public MDataTable Table
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
return _Table;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
private string _Description = string.Empty;
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public string Description
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
return _Description;
|
|||
|
|
}
|
|||
|
|
set
|
|||
|
|
{
|
|||
|
|
_Description = value;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
private string _TableName = string.Empty;
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public string TableName
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
return _TableName;
|
|||
|
|
}
|
|||
|
|
set
|
|||
|
|
{
|
|||
|
|
if (!string.IsNullOrEmpty(_TableName) && _TableName != value)
|
|||
|
|
{
|
|||
|
|
//<2F>ⲿ<EFBFBD><EFBFBD><DEB8>˱<EFBFBD><CBB1><EFBFBD>
|
|||
|
|
for (int i = 0; i < this.Count; i++)
|
|||
|
|
{
|
|||
|
|
this[i].TableName = value;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
_TableName = value;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
public MDataColumn Clone()
|
|||
|
|
{
|
|||
|
|
MDataColumn mcs = new MDataColumn();
|
|||
|
|
mcs.DataBaseType = DataBaseType;
|
|||
|
|
mcs.DataBaseVersion = DataBaseVersion;
|
|||
|
|
mcs.CheckDuplicate = false;
|
|||
|
|
mcs.isViewOwner = isViewOwner;
|
|||
|
|
mcs.TableName = TableName;
|
|||
|
|
mcs.Description = Description;
|
|||
|
|
foreach (string item in RelationTables)
|
|||
|
|
{
|
|||
|
|
mcs.AddRelateionTableName(item);
|
|||
|
|
}
|
|||
|
|
for (int i = 0; i < this.Count; i++)
|
|||
|
|
{
|
|||
|
|
mcs.Add(this[i].Clone());
|
|||
|
|
}
|
|||
|
|
return mcs;
|
|||
|
|
}
|
|||
|
|
public bool Contains(string columnName)
|
|||
|
|
{
|
|||
|
|
return GetIndex(columnName) > -1;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>洢<EFBFBD><E6B4A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
private Dictionary<string, int> columnIndex = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase);
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
internal void RefleshIndex()
|
|||
|
|
{
|
|||
|
|
IsNeedRefleshIndex = false;
|
|||
|
|
if (Count == 0) { return; }
|
|||
|
|
MDictionary<string, int> newIndexs = new MDictionary<string, int>(StringComparer.OrdinalIgnoreCase);
|
|||
|
|
string[] items = AppConfig.UI.AutoPrefixs.Split(',');
|
|||
|
|
for (int i = 0; i < Count; i++)
|
|||
|
|
{
|
|||
|
|
string name = this[i].ColumnName;
|
|||
|
|
if (name.IndexOf('_') > -1)
|
|||
|
|
{
|
|||
|
|
name = name.Replace("_", "");
|
|||
|
|
}
|
|||
|
|
newIndexs.Add(name, i);
|
|||
|
|
foreach (string item in items)
|
|||
|
|
{
|
|||
|
|
newIndexs.Add(item + name, i);//<2F><><EFBFBD>ȴ<EFBFBD><C8B4>ã<EFBFBD><C3A3>ӿ<EFBFBD><D3BF>ٶȡ<D9B6>
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
this.columnIndex = newIndexs;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>ڵ<EFBFBD><DAB5><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>(<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڷ<EFBFBD><DAB7><EFBFBD>-1<><31>
|
|||
|
|
/// </summary>
|
|||
|
|
public int GetIndex(string columnName)
|
|||
|
|
{
|
|||
|
|
if (IsNeedRefleshIndex)
|
|||
|
|
{
|
|||
|
|
RefleshIndex();
|
|||
|
|
}
|
|||
|
|
if (!string.IsNullOrEmpty(columnName))
|
|||
|
|
{
|
|||
|
|
if (columnIndex.ContainsKey(columnName))
|
|||
|
|
{
|
|||
|
|
return columnIndex[columnName];
|
|||
|
|
}
|
|||
|
|
if (columnName.Contains("_"))
|
|||
|
|
{
|
|||
|
|
columnName = columnName.Replace("_", "");//<2F><><EFBFBD><EFBFBD>ӳ<EFBFBD>䴦<EFBFBD><E4B4A6>
|
|||
|
|
if (columnIndex.ContainsKey(columnName))
|
|||
|
|
{
|
|||
|
|
return columnIndex[columnName];
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return -1;
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><> <20><> <20><><EFBFBD><EFBFBD><EFBFBD>Ż<EFBFBD>λ<EFBFBD>ø<EFBFBD><C3B8><EFBFBD>Ϊָ<CEAA><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ż<EFBFBD>λ<EFBFBD>á<EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="columnName"><3E><><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <param name="ordinal"><3E><><EFBFBD><EFBFBD></param>
|
|||
|
|
public void SetOrdinal(string columnName, int ordinal)
|
|||
|
|
{
|
|||
|
|
int index = GetIndex(columnName);
|
|||
|
|
if (index > -1 && index != ordinal)
|
|||
|
|
{
|
|||
|
|
MCellStruct mstruct = this[index];
|
|||
|
|
if (_Table != null && _Table.Rows.Count > 0)
|
|||
|
|
{
|
|||
|
|
List<object> items = _Table.GetColumnItems<object>(index, BreakOp.None);
|
|||
|
|
_Table.Columns.RemoveAt(index);
|
|||
|
|
_Table.Columns.Insert(ordinal, mstruct);
|
|||
|
|
for (int i = 0; i < items.Count; i++)
|
|||
|
|
{
|
|||
|
|
_Table.Rows[i].Set(ordinal, items[i]);
|
|||
|
|
}
|
|||
|
|
items = null;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
structList.RemoveAt(index);//<2F>Ƴ<EFBFBD>
|
|||
|
|
if (ordinal >= Count)
|
|||
|
|
{
|
|||
|
|
ordinal = Count;
|
|||
|
|
}
|
|||
|
|
structList.Insert(ordinal, mstruct);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
IsNeedRefleshIndex = true;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͬ<EFBFBD><CDAC>ֵ<EFBFBD><D6B5>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="columnName"><3E><><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <param name="value">ֵ</param>
|
|||
|
|
public void SetValue(string columnName, object value)
|
|||
|
|
{
|
|||
|
|
if (Contains(columnName))
|
|||
|
|
{
|
|||
|
|
this[columnName].Set(value);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>Json<6F><6E>ʽ<EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public string ToJson(bool isFullSchema)
|
|||
|
|
{
|
|||
|
|
JsonHelper helper = new JsonHelper();
|
|||
|
|
helper.Fill(this, isFullSchema);
|
|||
|
|
return helper.ToString();
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// ת<><D7AA><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="tableName"><3E><><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public MDataRow ToRow(string tableName)
|
|||
|
|
{
|
|||
|
|
MDataRow row = new MDataRow(this);
|
|||
|
|
row.TableName = tableName;
|
|||
|
|
//row.Columns.TableName = tableName;
|
|||
|
|
//row.Columns.CheckDuplicate = CheckDuplicate;
|
|||
|
|
//row.Columns.DataBaseType = DataBaseType;
|
|||
|
|
//row.Columns.DataBaseVersion = DataBaseVersion;
|
|||
|
|
//row.Columns.isViewOwner = isViewOwner;
|
|||
|
|
//row.Columns.RelationTables = RelationTables;
|
|||
|
|
row.Conn = Conn;
|
|||
|
|
return row;
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ܹ<EFBFBD><DCB9><EFBFBD><EFBFBD>ⲿ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>(json<6F><6E>ʽ<EFBFBD><CABD>
|
|||
|
|
/// </summary>
|
|||
|
|
public bool WriteSchema(string fileName)
|
|||
|
|
{
|
|||
|
|
string schema = ToJson(true).Replace("},{", "},\r\n{");//д<><D0B4><EFBFBD>ı<EFBFBD>ʱҪ<CAB1><D2AA><EFBFBD>С<EFBFBD>
|
|||
|
|
return IOHelper.Write(fileName, schema);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private List<MCellStruct> _JointPrimary = new List<MCellStruct>();
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public List<MCellStruct> JointPrimary
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
MCellStruct autoIncrementCell = null;
|
|||
|
|
if (_JointPrimary.Count == 0 && this.Count > 0)
|
|||
|
|
{
|
|||
|
|
foreach (MCellStruct item in this)
|
|||
|
|
{
|
|||
|
|
if (item.IsPrimaryKey)
|
|||
|
|
{
|
|||
|
|
_JointPrimary.Add(item);
|
|||
|
|
}
|
|||
|
|
else if (item.IsAutoIncrement)
|
|||
|
|
{
|
|||
|
|
autoIncrementCell = item;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if (_JointPrimary.Count == 0)
|
|||
|
|
{
|
|||
|
|
if (autoIncrementCell != null)
|
|||
|
|
{
|
|||
|
|
_JointPrimary.Add(autoIncrementCell);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
_JointPrimary.Add(this[0]);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return _JointPrimary;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public MCellStruct FirstPrimary
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
if (JointPrimary.Count > 0)
|
|||
|
|
{
|
|||
|
|
return JointPrimary[0];
|
|||
|
|
}
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><EFBFBD>Ψһ<CEA8><D2BB>
|
|||
|
|
/// </summary>
|
|||
|
|
public MCellStruct FirstUnique
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
MCellStruct ms = null;
|
|||
|
|
foreach (MCellStruct item in this)
|
|||
|
|
{
|
|||
|
|
if (item.IsUniqueKey)
|
|||
|
|
{
|
|||
|
|
return item;
|
|||
|
|
}
|
|||
|
|
else if (ms == null && !item.IsPrimaryKey && DataType.GetGroup(item.SqlType) == 0)//ȡ<><C8A1>һ<EFBFBD><D2BB><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
{
|
|||
|
|
ms = item;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if (ms == null && this.Count > 0)
|
|||
|
|
{
|
|||
|
|
ms = this[0];
|
|||
|
|
}
|
|||
|
|
return ms;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD>͡<EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
internal DataBaseType DataBaseType = DataBaseType.None;
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF>汾<EFBFBD>š<EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
internal string DataBaseVersion = string.Empty;
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EEA3A8><EFBFBD><EFBFBD><EFBFBD>䣩
|
|||
|
|
/// </summary>
|
|||
|
|
internal string Conn = string.Empty;
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>ýṹ<C3BD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>ͼӵ<CDBC><D3B5>
|
|||
|
|
/// </summary>
|
|||
|
|
internal bool isViewOwner = false;
|
|||
|
|
|
|||
|
|
internal List<string> relationTables = new List<string>();
|
|||
|
|
internal List<string> RelationTables
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
if (relationTables.Count == 0 && !string.IsNullOrEmpty(TableName))
|
|||
|
|
{
|
|||
|
|
relationTables.Add(TableName);
|
|||
|
|
}
|
|||
|
|
return relationTables;
|
|||
|
|
}
|
|||
|
|
set
|
|||
|
|
{
|
|||
|
|
relationTables = value;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
internal void AddRelateionTableName(string tableName)
|
|||
|
|
{
|
|||
|
|
if (!string.IsNullOrEmpty(tableName))
|
|||
|
|
{
|
|||
|
|
string[] items = TableName.Split(',');
|
|||
|
|
foreach (string name in items)
|
|||
|
|
{
|
|||
|
|
if (string.IsNullOrEmpty(name))
|
|||
|
|
{
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
if (!relationTables.Contains(tableName))
|
|||
|
|
{
|
|||
|
|
relationTables.Add(tableName);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD>ṹ<EFBFBD><E1B9B9><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Table<6C><65>ʾ
|
|||
|
|
/// </summary>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public MDataTable ToTable()
|
|||
|
|
{
|
|||
|
|
string tableName = string.Empty;
|
|||
|
|
if (_Table != null)
|
|||
|
|
{
|
|||
|
|
tableName = _Table.TableName;
|
|||
|
|
}
|
|||
|
|
MDataTable dt = new MDataTable(tableName);
|
|||
|
|
dt.Columns.Add("ColumnName,DataType,SqlType,MaxSize,Scale");
|
|||
|
|
dt.Columns.Add("IsPrimaryKey,IsAutoIncrement,IsCanNull,IsUniqueKey,IsForeignKey", SqlDbType.Bit);
|
|||
|
|
dt.Columns.Add("TableName,FKTableName,DefaultValue,Description");
|
|||
|
|
|
|||
|
|
for (int i = 0; i < Count; i++)
|
|||
|
|
{
|
|||
|
|
MCellStruct ms = this[i];
|
|||
|
|
dt.NewRow(true)
|
|||
|
|
.Sets(0, ms.ColumnName, ms.ValueType.Name, ms.SqlType, ms.MaxSize, ms.Scale)
|
|||
|
|
.Sets(5, ms.IsPrimaryKey, ms.IsAutoIncrement, ms.IsCanNull, ms.IsUniqueKey, ms.IsForeignKey)
|
|||
|
|
.Sets(10, ms.TableName, ms.FKTableName, ms.DefaultValue, ms.Description);
|
|||
|
|
}
|
|||
|
|
return dt;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
public partial class MDataColumn : IList<MCellStruct>
|
|||
|
|
{
|
|||
|
|
public int Count
|
|||
|
|
{
|
|||
|
|
get { return structList.Count; }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#region Add<EFBFBD><EFBFBD><EFBFBD>ط<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="columnName"><3E><><EFBFBD><EFBFBD></param>
|
|||
|
|
public void Add(string columnName)
|
|||
|
|
{
|
|||
|
|
Add(columnName, SqlDbType.NVarChar, false, true, -1, false, null);
|
|||
|
|
}
|
|||
|
|
/// <param name="SqlType"><3E>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
|
public void Add(string columnName, SqlDbType sqlType)
|
|||
|
|
{
|
|||
|
|
Add(columnName, sqlType, false, true, -1, false, null);
|
|||
|
|
}
|
|||
|
|
/// <param name="isAutoIncrement"><3E>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>id<69><64></param>
|
|||
|
|
public void Add(string columnName, SqlDbType sqlType, bool isAutoIncrement)
|
|||
|
|
{
|
|||
|
|
Add(columnName, sqlType, isAutoIncrement, !isAutoIncrement, -1, isAutoIncrement, null);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void Add(string columnName, SqlDbType sqlType, bool isAutoIncrement, bool isCanNull, int maxSize)
|
|||
|
|
{
|
|||
|
|
Add(columnName, sqlType, isAutoIncrement, isCanNull, maxSize, false, null);
|
|||
|
|
}
|
|||
|
|
public void Add(string columnName, SqlDbType sqlType, bool isAutoIncrement, bool isCanNull, int maxSize, bool isPrimaryKey, object defaultValue)
|
|||
|
|
{
|
|||
|
|
Add(columnName, sqlType, isAutoIncrement, isCanNull, maxSize, false, defaultValue, -1);
|
|||
|
|
}
|
|||
|
|
/// <param name="defaultValue">Ĭ<><C4AC>ֵ[<5B><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>봫<EFBFBD><EBB4AB>SqlValue.GetDate]</param>
|
|||
|
|
public void Add(string columnName, SqlDbType sqlType, bool isAutoIncrement, bool isCanNull, int maxSize, bool isPrimaryKey, object defaultValue, short scale)
|
|||
|
|
{
|
|||
|
|
string[] items = columnName.Split(',');
|
|||
|
|
foreach (string item in items)
|
|||
|
|
{
|
|||
|
|
MCellStruct mdcStruct = new MCellStruct(item, sqlType, isAutoIncrement, isCanNull, maxSize);
|
|||
|
|
mdcStruct.Scale = scale;
|
|||
|
|
mdcStruct.IsPrimaryKey = isPrimaryKey;
|
|||
|
|
mdcStruct.DefaultValue = defaultValue;
|
|||
|
|
Add(mdcStruct);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
public void Add(MCellStruct item)
|
|||
|
|
{
|
|||
|
|
if (item != null && !this.Contains(item) && (!CheckDuplicate || !Contains(item.ColumnName)))//
|
|||
|
|
{
|
|||
|
|
if (DataBaseType == DataBaseType.None)
|
|||
|
|
{
|
|||
|
|
DataBaseType = item.DalType;
|
|||
|
|
}
|
|||
|
|
item.MDataColumn = this;
|
|||
|
|
structList.Add(item);
|
|||
|
|
if (_Table != null && _Table.Rows.Count > 0)
|
|||
|
|
{
|
|||
|
|
for (int i = 0; i < _Table.Rows.Count; i++)
|
|||
|
|
{
|
|||
|
|
if (Count > _Table.Rows[i].Count)
|
|||
|
|
{
|
|||
|
|
_Table.Rows[i].Add(new MDataCell(ref item));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
IsNeedRefleshIndex = true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
//public void AddRange(IEnumerable<MCellStruct> collection)
|
|||
|
|
//{
|
|||
|
|
// AddRange(collection as MDataColumn);
|
|||
|
|
//}
|
|||
|
|
public void AddRange(MDataColumn items)
|
|||
|
|
{
|
|||
|
|
if (items.Count > 0)
|
|||
|
|
{
|
|||
|
|
foreach (MCellStruct item in items)
|
|||
|
|
{
|
|||
|
|
if (!Contains(item.ColumnName))
|
|||
|
|
{
|
|||
|
|
Add(item);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
IsNeedRefleshIndex = true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
public bool Remove(MCellStruct item)
|
|||
|
|
{
|
|||
|
|
Remove(item.ColumnName);
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
public void Remove(string columnName)
|
|||
|
|
{
|
|||
|
|
string[] items = columnName.Split(',');
|
|||
|
|
foreach (string item in items)
|
|||
|
|
{
|
|||
|
|
int index = GetIndex(item);
|
|||
|
|
if (index > -1)
|
|||
|
|
{
|
|||
|
|
RemoveAt(index);
|
|||
|
|
IsNeedRefleshIndex = true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void RemoveRange(int index, int count) // 1,4
|
|||
|
|
{
|
|||
|
|
for (int i = index; i < index + count; i++)
|
|||
|
|
{
|
|||
|
|
RemoveAt(index);//ÿ<><C3BF>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD><C6B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE>N<EFBFBD>μ<EFBFBD><CEBC>ɡ<EFBFBD>
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public void RemoveAt(int index)
|
|||
|
|
{
|
|||
|
|
structList.RemoveAt(index);
|
|||
|
|
if (_Table != null)
|
|||
|
|
{
|
|||
|
|
foreach (MDataRow row in _Table.Rows)
|
|||
|
|
{
|
|||
|
|
if (row.Count > Count)
|
|||
|
|
{
|
|||
|
|
row.RemoveAt(index);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
IsNeedRefleshIndex = true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
public void Insert(int index, MCellStruct item)
|
|||
|
|
{
|
|||
|
|
if (item != null && !this.Contains(item) && (!CheckDuplicate || !Contains(item.ColumnName)))//
|
|||
|
|
{
|
|||
|
|
item.MDataColumn = this;
|
|||
|
|
structList.Insert(index, item);
|
|||
|
|
if (_Table != null && _Table.Rows.Count > 0)
|
|||
|
|
{
|
|||
|
|
for (int i = 0; i < _Table.Rows.Count; i++)
|
|||
|
|
{
|
|||
|
|
if (Count > _Table.Rows[i].Count)
|
|||
|
|
{
|
|||
|
|
_Table.Rows[i].Insert(index, new MDataCell(ref item));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
IsNeedRefleshIndex = true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
public void InsertRange(int index, MDataColumn mdc)
|
|||
|
|
{
|
|||
|
|
for (int i = mdc.Count; i >= 0; i--)
|
|||
|
|
{
|
|||
|
|
Insert(index, mdc[i]);//<2F><><EFBFBD><EFBFBD>
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#region IList<MCellStruct> <EFBFBD><EFBFBD>Ա
|
|||
|
|
|
|||
|
|
int IList<MCellStruct>.IndexOf(MCellStruct item)
|
|||
|
|
{
|
|||
|
|
return structList.IndexOf(item);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region ICollection<MCellStruct> <EFBFBD><EFBFBD>Ա
|
|||
|
|
|
|||
|
|
void ICollection<MCellStruct>.CopyTo(MCellStruct[] array, int arrayIndex)
|
|||
|
|
{
|
|||
|
|
structList.CopyTo(array, arrayIndex);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
bool ICollection<MCellStruct>.IsReadOnly
|
|||
|
|
{
|
|||
|
|
get { return false; }
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region IEnumerable<MCellStruct> <EFBFBD><EFBFBD>Ա
|
|||
|
|
|
|||
|
|
IEnumerator<MCellStruct> IEnumerable<MCellStruct>.GetEnumerator()
|
|||
|
|
{
|
|||
|
|
return structList.GetEnumerator();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region IEnumerable <EFBFBD><EFBFBD>Ա
|
|||
|
|
|
|||
|
|
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
|
|||
|
|
{
|
|||
|
|
return structList.GetEnumerator();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region ICollection<MCellStruct> <EFBFBD><EFBFBD>Ա
|
|||
|
|
|
|||
|
|
|
|||
|
|
public void Clear()
|
|||
|
|
{
|
|||
|
|
structList.Clear();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public bool Contains(MCellStruct item)
|
|||
|
|
{
|
|||
|
|
return structList.Contains(item);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region IList<MCellStruct> <EFBFBD><EFBFBD>Ա
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// ReadOnly
|
|||
|
|
/// </summary>
|
|||
|
|
public MCellStruct this[int index]
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
return structList[index];
|
|||
|
|
}
|
|||
|
|
set
|
|||
|
|
{
|
|||
|
|
Error.Throw(AppConst.Global_NotImplemented);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endregion
|
|||
|
|
}
|
|||
|
|
public partial class MDataColumn
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>Json<6F><6E><EFBFBD>ļ<EFBFBD><C4BC>м<EFBFBD><D0BC>س<EFBFBD><D8B3><EFBFBD><EFBFBD><EFBFBD>Ϣ
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="jsonOrFileName">Json<6F><6E><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static MDataColumn CreateFrom(string jsonOrFileName)
|
|||
|
|
{
|
|||
|
|
return CreateFrom(jsonOrFileName, true);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>Json<6F><6E><EFBFBD>ļ<EFBFBD><C4BC>м<EFBFBD><D0BC>س<EFBFBD><D8B3><EFBFBD><EFBFBD><EFBFBD>Ϣ
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="jsonOrFileName">Json<6F><6E><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <param name="readTxtOrXml"><3E>Ƿ<EFBFBD><C7B7><EFBFBD>.txt<78><74>.xml<6D>ļ<EFBFBD><C4BC>ж<EFBFBD>ȡ<EFBFBD>ܹ<EFBFBD><DCB9><EFBFBD>Ĭ<EFBFBD><C4AC>Ϊtrue<75><65></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static MDataColumn CreateFrom(string jsonOrFileName, bool readTxtOrXml)
|
|||
|
|
{
|
|||
|
|
if (string.IsNullOrEmpty(jsonOrFileName))
|
|||
|
|
{
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
MDataColumn mdc = new MDataColumn();
|
|||
|
|
|
|||
|
|
MDataTable dt = null;
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
bool isTxtOrXml = false;
|
|||
|
|
string json = string.Empty;
|
|||
|
|
char c = jsonOrFileName[0];
|
|||
|
|
bool isJson = c == '{' || c == '[' || c == '<';
|
|||
|
|
string exName = null;
|
|||
|
|
string fileName = null;
|
|||
|
|
if (!isJson)
|
|||
|
|
{
|
|||
|
|
exName = Path.GetExtension(jsonOrFileName);
|
|||
|
|
fileName = Path.GetFileNameWithoutExtension(jsonOrFileName);
|
|||
|
|
switch (exName.ToLower())
|
|||
|
|
{
|
|||
|
|
case ".ts":
|
|||
|
|
case ".xml":
|
|||
|
|
case ".txt":
|
|||
|
|
string tsFileName = jsonOrFileName.Replace(exName, ".ts");
|
|||
|
|
if (File.Exists(tsFileName))
|
|||
|
|
{
|
|||
|
|
json = IOHelper.ReadAllText(tsFileName);
|
|||
|
|
}
|
|||
|
|
else if (readTxtOrXml && File.Exists(jsonOrFileName))
|
|||
|
|
{
|
|||
|
|
isTxtOrXml = true;
|
|||
|
|
if (exName == ".xml")
|
|||
|
|
{
|
|||
|
|
json = IOHelper.ReadAllText(jsonOrFileName, 0, Encoding.UTF8);
|
|||
|
|
}
|
|||
|
|
else if (exName == ".txt")
|
|||
|
|
{
|
|||
|
|
json = IOHelper.ReadAllText(jsonOrFileName);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
break;
|
|||
|
|
default:
|
|||
|
|
json = jsonOrFileName;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
json = jsonOrFileName;
|
|||
|
|
}
|
|||
|
|
if (!string.IsNullOrEmpty(json))
|
|||
|
|
{
|
|||
|
|
dt = MDataTable.CreateFrom(json);
|
|||
|
|
if (dt.TableName == MDataTable.DefaultTableName && !string.IsNullOrEmpty(fileName))
|
|||
|
|
{
|
|||
|
|
dt.TableName = fileName;
|
|||
|
|
}
|
|||
|
|
if (dt.Columns.Count > 0)
|
|||
|
|
{
|
|||
|
|
if (isTxtOrXml)
|
|||
|
|
{
|
|||
|
|
mdc = dt.Columns.Clone();
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
foreach (MDataRow row in dt.Rows)
|
|||
|
|
{
|
|||
|
|
MCellStruct cs = new MCellStruct(
|
|||
|
|
row.Get<string>("ColumnName"),
|
|||
|
|
DataType.GetSqlType(row.Get<string>("SqlType", "string")),
|
|||
|
|
row.Get<bool>("IsAutoIncrement", false),
|
|||
|
|
row.Get<bool>("IsCanNull", false),
|
|||
|
|
row.Get<int>("MaxSize", -1));
|
|||
|
|
cs.Scale = row.Get<short>("Scale");
|
|||
|
|
cs.IsPrimaryKey = row.Get<bool>("IsPrimaryKey", false);
|
|||
|
|
cs.DefaultValue = row.Get<string>("DefaultValue");
|
|||
|
|
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
cs.Description = row.Get<string>("Description");
|
|||
|
|
cs.TableName = row.Get<string>("TableName");
|
|||
|
|
cs.IsUniqueKey = row.Get<bool>("IsUniqueKey", false);
|
|||
|
|
cs.IsForeignKey = row.Get<bool>("IsForeignKey", false);
|
|||
|
|
cs.FKTableName = row.Get<string>("FKTableName");
|
|||
|
|
cs.SqlTypeName = row.Get<string>("SqlTypeName");
|
|||
|
|
mdc.Add(cs);
|
|||
|
|
}
|
|||
|
|
mdc.TableName = dt.TableName;
|
|||
|
|
mdc.Description = dt.Description;
|
|||
|
|
mdc.relationTables = dt.Columns.relationTables;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception err)
|
|||
|
|
{
|
|||
|
|
Log.Write(err, LogType.Error);
|
|||
|
|
}
|
|||
|
|
finally
|
|||
|
|
{
|
|||
|
|
dt = null;
|
|||
|
|
}
|
|||
|
|
return mdc;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>.ts <20>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>Json
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="json"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
//private static MDataColumn CreateFromByTsJson(string json)
|
|||
|
|
//{
|
|||
|
|
// MDataColumn mdc = new MDataColumn();
|
|||
|
|
//}
|
|||
|
|
}
|
|||
|
|
}
|