753 lines
23 KiB
C#
753 lines
23 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;
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
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><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
internal bool IsColumnNameChanged = false;
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>洢<EFBFBD><E6B4A2><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
private MDictionary<string, int> columnIndex = new MDictionary<string, int>(StringComparer.OrdinalIgnoreCase);
|
|||
|
|
/// <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 = true;
|
|||
|
|
/// <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>
|
|||
|
|
public MDataTable Table
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
return _Table;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
private string _Description = string.Empty;
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public string Description
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
if (string.IsNullOrEmpty(_Description) && _Table != null)
|
|||
|
|
{
|
|||
|
|
return _Table.Description;
|
|||
|
|
}
|
|||
|
|
return _Description;
|
|||
|
|
}
|
|||
|
|
set
|
|||
|
|
{
|
|||
|
|
_Description = value;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
public MDataColumn Clone()
|
|||
|
|
{
|
|||
|
|
MDataColumn mcs = new MDataColumn();
|
|||
|
|
mcs.dalType = dalType;
|
|||
|
|
mcs.CheckDuplicate = false;
|
|||
|
|
mcs.isViewOwner = isViewOwner;
|
|||
|
|
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;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <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 (columnIndex.Count == 0 || IsColumnNameChanged || columnIndex.Count != Count)
|
|||
|
|
{
|
|||
|
|
columnIndex.Clear();
|
|||
|
|
for (int i = 0; i < Count; i++)
|
|||
|
|
{
|
|||
|
|
columnIndex.Add(this[i].ColumnName.Replace("_", ""), i);
|
|||
|
|
}
|
|||
|
|
IsColumnNameChanged = false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (!string.IsNullOrEmpty(columnName))
|
|||
|
|
{
|
|||
|
|
if (columnName.IndexOf('_') > -1)
|
|||
|
|
{
|
|||
|
|
columnName = columnName.Replace("_", "");//<2F><><EFBFBD><EFBFBD>ӳ<EFBFBD>䴦<EFBFBD><E4B4A6>
|
|||
|
|
}
|
|||
|
|
if (columnIndex.ContainsKey(columnName))
|
|||
|
|
{
|
|||
|
|
return columnIndex[columnName];
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC>ǰ<C7B0><D7BA><EFBFBD><EFBFBD>
|
|||
|
|
string[] items = AppConfig.UI.AutoPrefixs.Split(',');
|
|||
|
|
if (items != null && items.Length > 0)
|
|||
|
|
{
|
|||
|
|
foreach (string item in items)
|
|||
|
|
{
|
|||
|
|
columnName = columnName.StartsWith(item) ? columnName.Substring(3) : item + columnName;
|
|||
|
|
if (columnIndex.ContainsKey(columnName))
|
|||
|
|
{
|
|||
|
|
return columnIndex[columnName];
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
//for (int i = 0; i < Count; i++)
|
|||
|
|
//{
|
|||
|
|
// if (string.Compare(this[i].ColumnName.Replace("_", ""), columnName, StringComparison.OrdinalIgnoreCase) == 0)//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>StringComparison.OrdinalIgnoreCase<73><65><EFBFBD><EFBFBD>true<75>졣
|
|||
|
|
// {
|
|||
|
|
// return i;
|
|||
|
|
// }
|
|||
|
|
//}
|
|||
|
|
}
|
|||
|
|
return -1;
|
|||
|
|
}
|
|||
|
|
/// <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);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
columnIndex.Clear();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <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.CheckDuplicate = CheckDuplicate;
|
|||
|
|
row.Columns.dalType = dalType;
|
|||
|
|
row.Columns.isViewOwner = isViewOwner;
|
|||
|
|
row.Columns.relationTables = relationTables;
|
|||
|
|
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 DalType dalType = DalType.None;
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>ýṹ<C3BD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>ͼӵ<CDBC><D3B5>
|
|||
|
|
/// </summary>
|
|||
|
|
internal bool isViewOwner = false;
|
|||
|
|
internal List<string> relationTables = new List<string>();
|
|||
|
|
internal void AddRelateionTableName(string tableName)
|
|||
|
|
{
|
|||
|
|
if (!string.IsNullOrEmpty(tableName) && !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");
|
|||
|
|
dt.Columns.Add("MaxSize");
|
|||
|
|
dt.Columns.Add("Scale");
|
|||
|
|
dt.Columns.Add("IsCanNull");
|
|||
|
|
dt.Columns.Add("IsAutoIncrement");
|
|||
|
|
dt.Columns.Add("SqlType");
|
|||
|
|
dt.Columns.Add("IsPrimaryKey");
|
|||
|
|
dt.Columns.Add("IsUniqueKey");
|
|||
|
|
dt.Columns.Add("IsForeignKey");
|
|||
|
|
dt.Columns.Add("FKTableName");
|
|||
|
|
dt.Columns.Add("DefaultValue");
|
|||
|
|
dt.Columns.Add("Description");
|
|||
|
|
dt.Columns.Add("TableName");
|
|||
|
|
|
|||
|
|
for (int i = 0; i < Count; i++)
|
|||
|
|
{
|
|||
|
|
MCellStruct ms = this[i];
|
|||
|
|
dt.NewRow(true)
|
|||
|
|
.Set(0, ms.ColumnName)
|
|||
|
|
.Set(1, ms.MaxSize)
|
|||
|
|
.Set(2, ms.Scale)
|
|||
|
|
.Set(3, ms.IsCanNull)
|
|||
|
|
.Set(4, ms.IsAutoIncrement)
|
|||
|
|
.Set(5, ms.SqlType)
|
|||
|
|
.Set(6, ms.IsPrimaryKey)
|
|||
|
|
.Set(7, ms.IsUniqueKey)
|
|||
|
|
.Set(8, ms.IsForeignKey)
|
|||
|
|
.Set(9, ms.FKTableName)
|
|||
|
|
.Set(10, ms.DefaultValue)
|
|||
|
|
.Set(11, ms.Description)
|
|||
|
|
.Set(12, ms.TableName);
|
|||
|
|
}
|
|||
|
|
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<49><44></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);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <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)
|
|||
|
|
{
|
|||
|
|
string[] items = columnName.Split(',');
|
|||
|
|
foreach (string item in items)
|
|||
|
|
{
|
|||
|
|
MCellStruct mdcStruct = new MCellStruct(item, sqlType, isAutoIncrement, isCanNull, maxSize);
|
|||
|
|
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 (dalType == DalType.None)
|
|||
|
|
{
|
|||
|
|
dalType = 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));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
columnIndex.Clear();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
//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);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
columnIndex.Clear();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
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);
|
|||
|
|
columnIndex.Clear();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void RemoveRange(int index, int count) // 1,4
|
|||
|
|
{
|
|||
|
|
for (int i = index; i < index + count; i++)
|
|||
|
|
{
|
|||
|
|
RemoveAt(i);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
public void RemoveAt(int index)
|
|||
|
|
{
|
|||
|
|
structList.RemoveAt(index);
|
|||
|
|
if (_Table != null)
|
|||
|
|
{
|
|||
|
|
foreach (MDataRow row in _Table.Rows)
|
|||
|
|
{
|
|||
|
|
if (row.Count > Count)
|
|||
|
|
{
|
|||
|
|
row.RemoveAt(index);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
columnIndex.Clear();
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
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));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
columnIndex.Clear();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
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)
|
|||
|
|
{
|
|||
|
|
MDataColumn mdc = new MDataColumn();
|
|||
|
|
MDataTable dt = null;
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
bool isTxtOrXml = false;
|
|||
|
|
string json = string.Empty;
|
|||
|
|
string exName = Path.GetExtension(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;
|
|||
|
|
}
|
|||
|
|
if (!string.IsNullOrEmpty(json))
|
|||
|
|
{
|
|||
|
|
dt = MDataTable.CreateFrom(json);
|
|||
|
|
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.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");
|
|||
|
|
mdc.Add(cs);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception err)
|
|||
|
|
{
|
|||
|
|
Log.WriteLogToTxt(err);
|
|||
|
|
}
|
|||
|
|
finally
|
|||
|
|
{
|
|||
|
|
dt = null;
|
|||
|
|
}
|
|||
|
|
return mdc;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
//internal bool AcceptChanges(AcceptOp op)
|
|||
|
|
//{
|
|||
|
|
// if (_Table == null)
|
|||
|
|
// {
|
|||
|
|
// return false;
|
|||
|
|
// }
|
|||
|
|
// return AcceptChanges(op, _Table.TableName, _Table.Conn);
|
|||
|
|
//}
|
|||
|
|
//internal bool AcceptChanges(AcceptOp op, string tableName, string newConn)
|
|||
|
|
//{
|
|||
|
|
// if (string.IsNullOrEmpty(tableName) || string.IsNullOrEmpty(newConn) || Count == 0)
|
|||
|
|
// {
|
|||
|
|
// return false;
|
|||
|
|
// }
|
|||
|
|
// return true;
|
|||
|
|
//}
|
|||
|
|
}
|
|||
|
|
}
|