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
{
///
/// 头列表集合
///
public partial class MDataColumn
{
List structList;
internal MDataTable _Table;
internal MDataColumn(MDataTable table)
{
structList = new List();
_Table = table;
}
public MDataColumn()
{
structList = new List();
}
///
/// 是否需要刷新索引
///
internal bool IsNeedRefleshIndex = false;
private int _CheckDuplicateState = -1;
///
/// 添加列时,检测名称是否重复(默认为true)。
///
public bool CheckDuplicate
{
get
{
//return true;
if (_CheckDuplicateState == -1)
{
return structList.Count < 100;//列多时,会影响性能,默认超过100条后,不检测重复项。
}
return _CheckDuplicateState == 1;
}
set
{
_CheckDuplicateState = value ? 1 : 0;
}
}
///
/// 隐式转换列头
///
///
///
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;
}
}
///
/// 架构所引用的表
///
[JsonIgnore]
public MDataTable Table
{
get
{
return _Table;
}
}
private string _Description = string.Empty;
///
/// 表名描述
///
public string Description
{
get
{
return _Description;
}
set
{
_Description = value;
}
}
private string _TableName = string.Empty;
///
/// 表名
///
public string TableName
{
get
{
return _TableName;
}
set
{
if (!string.IsNullOrEmpty(_TableName) && _TableName != value)
{
//外部修改了表名
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 索引获取与更新
///
/// 存储列名的索引
///
private Dictionary columnIndex = new Dictionary(StringComparer.OrdinalIgnoreCase);
///
/// 更新索引
///
internal void RefleshIndex()
{
IsNeedRefleshIndex = false;
if (Count == 0) { return; }
MDictionary newIndexs = new MDictionary(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);//事先存好,加快速度。
}
}
this.columnIndex = newIndexs;
}
///
/// 获取列所在的索引位置(若不存在返回-1)
///
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("_", "");//兼容映射处理
if (columnIndex.ContainsKey(columnName))
{
return columnIndex[columnName];
}
}
}
return -1;
}
#endregion
///
/// 将 列 的序号或位置更改为指定的序号或位置。
///
/// 列名
/// 序号
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