607 lines
22 KiB
C#
607 lines
22 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Text;
|
|||
|
|
using CYQ.Data.Table;
|
|||
|
|
using CYQ.Data.SQL;
|
|||
|
|
|
|||
|
|
using System.IO;
|
|||
|
|
using CYQ.Data.Tool;
|
|||
|
|
using CYQ.Data.Json;
|
|||
|
|
|
|||
|
|
namespace CYQ.Data
|
|||
|
|
{
|
|||
|
|
internal class NoSqlAction : IDisposable
|
|||
|
|
{
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
internal static void Clear()
|
|||
|
|
{
|
|||
|
|
ResetStaticVar();
|
|||
|
|
_needToSaveState.Clear();
|
|||
|
|
_lockNextIDObj.Clear();
|
|||
|
|
_maxID.Clear();
|
|||
|
|
_lastWriteTimeList.Clear();
|
|||
|
|
_isDeleteAll.Clear();
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>þ<EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ棬<DAB4><E6A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݺ<EFBFBD><DDBA>ڴ治<DAB4><E6B2BB><EFBFBD>գ<EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
internal static void ResetStaticVar()
|
|||
|
|
{
|
|||
|
|
_tableList.Clear();
|
|||
|
|
_tableList = null;//<2F><>̬<EFBFBD>ֵ<EFBFBD><D6B5><EFBFBD>ΪNull<6C><6C><EFBFBD><EFBFBD><EFBFBD>ͷ<EFBFBD><CDB7>ڴ档
|
|||
|
|
_tableList = new MDictionary<string, MDataTable>(5, StringComparer.OrdinalIgnoreCase);//<2F><><EFBFBD>³<EFBFBD>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>
|
|||
|
|
//_needToSaveState.Clear();
|
|||
|
|
//_lockNextidObj.Clear();
|
|||
|
|
//_maxid.Clear();
|
|||
|
|
}
|
|||
|
|
private static MDictionary<string, MDataTable> _tableList = new MDictionary<string, MDataTable>(5, StringComparer.OrdinalIgnoreCase);//<2F>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD>ݿ<EFBFBD>
|
|||
|
|
// private static readonly object _lockTableListObj = new object();
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>Ƿ<EFBFBD><C7B7><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD>£<EFBFBD>0δ<30><CEB4><EFBFBD>£<EFBFBD>1<EFBFBD><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD>[<5B><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>]<5D><>2<EFBFBD><32><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>[<5B><><EFBFBD>±<EFBFBD><C2B1><EFBFBD>],//<2F><>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>[ȫ<>ֵĿ<D6B5><C4BF><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD><D0A7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>]
|
|||
|
|
/// </summary>
|
|||
|
|
private static MDictionary<string, int> _needToSaveState = new MDictionary<string, int>(5, StringComparer.OrdinalIgnoreCase);
|
|||
|
|
private static MDictionary<string, object> _lockNextIDObj = new MDictionary<string, object>(5, StringComparer.OrdinalIgnoreCase);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>id<69><64>
|
|||
|
|
private static MDictionary<string, int> _maxID = new MDictionary<string, int>(5, StringComparer.OrdinalIgnoreCase);//<2F><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>id
|
|||
|
|
private static MDictionary<string, DateTime> _lastWriteTimeList = new MDictionary<string, DateTime>(5, StringComparer.OrdinalIgnoreCase);//<2F><>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>id
|
|||
|
|
// private static MDictionary<string, object> _lockOperatorObj = new MDictionary<string, object>(5, StringComparer.OrdinalIgnoreCase);//<2F><>ɾ<EFBFBD><C9BE>ʱ<EFBFBD>Ļ<EFBFBD><C4BB><EFBFBD>
|
|||
|
|
private List<MDataRow> _insertRows = new List<MDataRow>();//<2F>²<EFBFBD><C2B2><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC>ϣ<EFBFBD><CFA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>MDataTable<6C><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
private static List<string> _isDeleteAll = new List<string>(5);
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4>ʱ<EFBFBD><CAB1>
|
|||
|
|
/// </summary>
|
|||
|
|
// private static DateTime _lastWriteTimeUtc = DateTime.UtcNow;
|
|||
|
|
private MDataTable _Table = null;
|
|||
|
|
private MDataTable Table
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
if (_Table != null && _Table.Rows.Count > 0)
|
|||
|
|
{
|
|||
|
|
return _Table;
|
|||
|
|
}
|
|||
|
|
else if (_tableList.ContainsKey(_FileFullName))
|
|||
|
|
{
|
|||
|
|
_Table = _tableList[_FileFullName];
|
|||
|
|
if (_Table.Rows.Count == 0 && !_isDeleteAll.Contains(_FileFullName))
|
|||
|
|
{
|
|||
|
|
if (_maxID.ContainsKey(_FileFullName))
|
|||
|
|
{
|
|||
|
|
_maxID.Remove(_FileFullName);
|
|||
|
|
}
|
|||
|
|
_tableList.Remove(_FileFullName);// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɾ<EFBFBD><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>(<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>⣬<EFBFBD><E2A3AC><EFBFBD>Կɿ<D4BF><C9BF><EFBFBD>)<29><>
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
return _Table;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
switch (_DalType)
|
|||
|
|
{
|
|||
|
|
case DataBaseType.Txt:
|
|||
|
|
_Table = MDataTable.CreateFrom(_FileFullName, _Row.Columns, EscapeOp.Encode);
|
|||
|
|
break;
|
|||
|
|
case DataBaseType.Xml:
|
|||
|
|
_Table = MDataTable.CreateFromXml(_FileFullName, _Row.Columns);
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
if (_Table == null || _Table.Columns.Count == 0)
|
|||
|
|
{
|
|||
|
|
Error.Throw("MDataTable can't load data from file : " + _FileFullName);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
DateTime lastWriteTime = new IOInfo(_FileFullName).LastWriteTime;
|
|||
|
|
if (!_lastWriteTimeList.ContainsKey(_FileFullName))
|
|||
|
|
{
|
|||
|
|
_lastWriteTimeList.Add(_FileFullName, lastWriteTime);
|
|||
|
|
}
|
|||
|
|
if (_Table.Rows.Count > 0)
|
|||
|
|
{
|
|||
|
|
if (!_tableList.ContainsKey(_FileFullName))
|
|||
|
|
{
|
|||
|
|
_tableList.Add(_FileFullName, _Table);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return _Table;
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private int maxID
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
if (!_maxID.ContainsKey(_FileFullName))
|
|||
|
|
{
|
|||
|
|
_maxID.Add(_FileFullName, 0);
|
|||
|
|
}
|
|||
|
|
return _maxID[_FileFullName];
|
|||
|
|
}
|
|||
|
|
set
|
|||
|
|
{
|
|||
|
|
_maxID[_FileFullName] = value;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
private object lockNextIDObj
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
if (!_lockNextIDObj.ContainsKey(_FileFullName))
|
|||
|
|
{
|
|||
|
|
_lockNextIDObj.Add(_FileFullName, new object());
|
|||
|
|
}
|
|||
|
|
return _lockNextIDObj[_FileFullName];
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
private int needToSaveState
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
if (!_needToSaveState.ContainsKey(_FileFullName))
|
|||
|
|
{
|
|||
|
|
_needToSaveState.Add(_FileFullName, 0);
|
|||
|
|
}
|
|||
|
|
return _needToSaveState[_FileFullName];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
set
|
|||
|
|
{
|
|||
|
|
_needToSaveState[_FileFullName] = value;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>id
|
|||
|
|
/// </summary>
|
|||
|
|
private int NextID
|
|||
|
|
{
|
|||
|
|
get
|
|||
|
|
{
|
|||
|
|
lock (lockNextIDObj)
|
|||
|
|
{
|
|||
|
|
if (maxID > 0)
|
|||
|
|
{
|
|||
|
|
maxID++;
|
|||
|
|
}
|
|||
|
|
else if (DataType.GetGroup(Table.Columns.FirstPrimary.SqlType) == DataGroupType.Number)//<2F><><EFBFBD><EFBFBD>id<69><64><EFBFBD><EFBFBD>int<6E><74>Ч
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
int rowsCount = Table.Rows.Count;
|
|||
|
|
if (rowsCount > 0)
|
|||
|
|
{
|
|||
|
|
#region <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
int maxRowID = 0;
|
|||
|
|
for (int i = 0; i < 50; i++)
|
|||
|
|
{
|
|||
|
|
int index = rowsCount - 1 - i;
|
|||
|
|
if (index >= 0)
|
|||
|
|
{
|
|||
|
|
int value = Table.Rows[index][0].Get<int>();
|
|||
|
|
maxRowID = Math.Max(maxRowID, value);
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
maxID = maxRowID + 1;
|
|||
|
|
//do
|
|||
|
|
//{
|
|||
|
|
// if (lastIndex >= 0)
|
|||
|
|
// {
|
|||
|
|
// if (_Table.Rows[lastIndex][0].IsNull)
|
|||
|
|
// {
|
|||
|
|
// lastIndex--;
|
|||
|
|
// }
|
|||
|
|
// else
|
|||
|
|
// {
|
|||
|
|
// maxID = Convert.ToInt32(_Table.Rows[lastIndex][0].Value) + 1;
|
|||
|
|
// }
|
|||
|
|
// }
|
|||
|
|
// else
|
|||
|
|
// {
|
|||
|
|
// maxID = 1;
|
|||
|
|
// }
|
|||
|
|
//}
|
|||
|
|
//while (maxID == 0);
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
maxID = 1;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch
|
|||
|
|
{
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
Error.Throw("Increment id only allow use for int type");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
return maxID;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
string _FileFullName = string.Empty;
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD>(<28><><EFBFBD><EFBFBD>չ<EFBFBD><D5B9>)
|
|||
|
|
/// </summary>
|
|||
|
|
public string _FileName = string.Empty;
|
|||
|
|
internal MDataRow _Row;//MAction<6F>е<EFBFBD>Row
|
|||
|
|
DataBaseType _DalType = DataBaseType.None;
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
public string TableName;
|
|||
|
|
public NoSqlAction(ref MDataRow row, string fileName, string filePath, DataBaseType dalType)
|
|||
|
|
{
|
|||
|
|
Reset(ref row, fileName, filePath, dalType);
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20>л<EFBFBD><D0BB><EFBFBD>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="row"><3E><><EFBFBD><EFBFBD><EFBFBD>нṹ</param>
|
|||
|
|
/// <param name="fileName"><3E>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
|
/// <param name="filePath"><3E>ļ<EFBFBD>·<EFBFBD><C2B7></param>
|
|||
|
|
/// <param name="dalType"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
|||
|
|
public void Reset(ref MDataRow row, string fileName, string filePath, DataBaseType dalType)
|
|||
|
|
{
|
|||
|
|
TableName = Path.GetFileNameWithoutExtension(fileName);
|
|||
|
|
string exName = Path.GetExtension(fileName);
|
|||
|
|
if (string.IsNullOrEmpty(exName))
|
|||
|
|
{
|
|||
|
|
switch (dalType)
|
|||
|
|
{
|
|||
|
|
case DataBaseType.Txt:
|
|||
|
|
fileName = fileName + ".txt";
|
|||
|
|
break;
|
|||
|
|
case DataBaseType.Xml:
|
|||
|
|
fileName = fileName + ".xml";
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if (fileName != _FileName)
|
|||
|
|
{
|
|||
|
|
_insertRows.Clear();//<2F>л<EFBFBD><D0BB><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>á<EFBFBD>
|
|||
|
|
Dispose();//<2F>ȱ<EFBFBD><C8B1><EFBFBD>
|
|||
|
|
}
|
|||
|
|
_Row = row;
|
|||
|
|
_FileName = fileName;
|
|||
|
|
_FileFullName = filePath + _FileName;
|
|||
|
|
_DalType = dalType;
|
|||
|
|
_Table = null;
|
|||
|
|
}
|
|||
|
|
public bool Delete(object where)
|
|||
|
|
{
|
|||
|
|
int count = 0;
|
|||
|
|
return Delete(where, out count);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
internal bool Delete(object where, out int count)
|
|||
|
|
{
|
|||
|
|
count = -1;
|
|||
|
|
if (!string.IsNullOrEmpty(Convert.ToString(where)))
|
|||
|
|
{
|
|||
|
|
//lock (lockOperatorObj) // ɾ<><C9BE><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӱ<EFBFBD>쵽Insert<72><74>
|
|||
|
|
//{
|
|||
|
|
IList<MDataRow> rowList = Table.FindAll(where);
|
|||
|
|
if (rowList != null)
|
|||
|
|
{
|
|||
|
|
count = rowList.Count;
|
|||
|
|
if (count > 0)
|
|||
|
|
{
|
|||
|
|
bool isDeleteAll = count == Table.Rows.Count;
|
|||
|
|
if (isDeleteAll)
|
|||
|
|
{
|
|||
|
|
_Table.Rows.Clear();//<2F><><EFBFBD>վ<EFBFBD>Ȼ<EFBFBD><C8BB>Ч<EFBFBD><D0A7><EFBFBD><EFBFBD>ʱδ<CAB1>ҵ<EFBFBD>ԭ<EFBFBD><D4AD>
|
|||
|
|
_insertRows.Clear();
|
|||
|
|
if (!_isDeleteAll.Contains(_FileFullName))
|
|||
|
|
{
|
|||
|
|
_isDeleteAll.Add(_FileFullName);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
for (int i = rowList.Count - 1; i >= 0; i--)
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
MDataRow row = rowList[i];
|
|||
|
|
if (row != null)
|
|||
|
|
{
|
|||
|
|
if (Table.Rows.Contains(row)) //<2F>̶߳<DFB3>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD>ɾ
|
|||
|
|
{
|
|||
|
|
Table.Rows.Remove(row);
|
|||
|
|
}
|
|||
|
|
if (_insertRows.Count > 0 && _insertRows.Contains(row))
|
|||
|
|
{
|
|||
|
|
_insertRows.Remove(row);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch { }
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
// if (isDeleteAll) { Save(); }
|
|||
|
|
needToSaveState = 2;
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
//}
|
|||
|
|
}
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
public bool Insert(bool isOpenTrans)
|
|||
|
|
{
|
|||
|
|
MDataCell cell = _Row.PrimaryCell;
|
|||
|
|
if (IsCanDoInsertCheck((cell.IsNullOrEmpty || cell.Struct.IsAutoIncrement || cell.Struct.IsPrimaryKey) ? 1 : 0))
|
|||
|
|
{
|
|||
|
|
//<2F>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>id
|
|||
|
|
if (!cell.Struct.IsCanNull && (cell.Struct.IsAutoIncrement || cell.Struct.IsPrimaryKey))
|
|||
|
|
{
|
|||
|
|
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ
|
|||
|
|
DataGroupType group = DataType.GetGroup(cell.Struct.SqlType);
|
|||
|
|
string existWhere = cell.ColumnName + (group == DataGroupType.Number ? "={0}" : "='{0}'");
|
|||
|
|
if (cell.IsNull || cell.State == 0 || cell.StringValue == "0" || Exists(string.Format(existWhere, cell.Value)))//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD><DAA3><EFBFBD><EFBFBD><EFBFBD>id<69>ظ<EFBFBD>
|
|||
|
|
{
|
|||
|
|
switch (group)
|
|||
|
|
{
|
|||
|
|
case DataGroupType.Number:
|
|||
|
|
cell.Value = NextID;
|
|||
|
|
break;
|
|||
|
|
case DataGroupType.Guid:
|
|||
|
|
cell.Value = Guid.NewGuid();
|
|||
|
|
break;
|
|||
|
|
case DataGroupType.Text:
|
|||
|
|
cell.Value = Guid.NewGuid().ToString();
|
|||
|
|
break;
|
|||
|
|
default:
|
|||
|
|
return (bool)Error.Throw("first column value can't be null");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if (group == DataGroupType.Number || group == DataGroupType.Guid)//<2F>ټ<EFBFBD><D9BC><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>Ѵ<EFBFBD><D1B4><EFBFBD>
|
|||
|
|
{
|
|||
|
|
if (!isOpenTrans && Exists(string.Format(existWhere, cell.Value)))//<2F><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD>id<69><64><EFBFBD>ܿ<EFBFBD><DCBF><EFBFBD><EFBFBD>⣬<EFBFBD><E2A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
{
|
|||
|
|
Error.Throw("first column value must be unique:(" + cell.ColumnName + ":" + cell.Value + ")");
|
|||
|
|
}
|
|||
|
|
else if (group == DataGroupType.Number)
|
|||
|
|
{
|
|||
|
|
maxID = (int)cell.Value;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
CheckFileChanged(true);
|
|||
|
|
_Row.SetState(0);//״̬<D7B4><CCAC><EFBFBD>ã<EFBFBD><C3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD>ʹ<EFBFBD>ò<EFBFBD><C3B2>룡
|
|||
|
|
MDataRow newRow = Table.NewRow(true);
|
|||
|
|
newRow.LoadFrom(_Row);
|
|||
|
|
_insertRows.Add(newRow);//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ã<EFBFBD><C3A3>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD>ûд<C3BB><D0B4><EFBFBD>£<EFBFBD>
|
|||
|
|
needToSaveState = needToSaveState > 1 ? 2 : 1;
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
public bool Update(object where)
|
|||
|
|
{
|
|||
|
|
int count = 0;
|
|||
|
|
return Update(where, out count);
|
|||
|
|
}
|
|||
|
|
public bool Update(object where, out int count)
|
|||
|
|
{
|
|||
|
|
count = -1;
|
|||
|
|
CheckFileChanged(true);
|
|||
|
|
IList<MDataRow> rowList = Table.FindAll(where);
|
|||
|
|
if (rowList != null)
|
|||
|
|
{
|
|||
|
|
count = rowList.Count;
|
|||
|
|
if (count > 0)
|
|||
|
|
{
|
|||
|
|
for (int i = rowList.Count - 1; i >= 0; i--)
|
|||
|
|
{
|
|||
|
|
rowList[i].LoadFrom(_Row, RowOp.Update, false);
|
|||
|
|
rowList[i].SetState(0);//״̬<D7B4><CCAC><EFBFBD><EFBFBD>
|
|||
|
|
}
|
|||
|
|
_Row.SetState(0);
|
|||
|
|
needToSaveState = 2;
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
public bool Fill(object where)
|
|||
|
|
{
|
|||
|
|
CheckFileChanged(true);
|
|||
|
|
MDataRow row = Table.FindRow(where);
|
|||
|
|
if (row != null)
|
|||
|
|
{
|
|||
|
|
_Row.LoadFrom(row);
|
|||
|
|
_Row.SetState(0);//<2F><>ѯʱ<D1AF><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ᶨλ״̬Ϊ1
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
public int GetCount(object where)
|
|||
|
|
{
|
|||
|
|
CheckFileChanged(true);
|
|||
|
|
return Table.GetCount(where);
|
|||
|
|
}
|
|||
|
|
public bool Exists(object where)
|
|||
|
|
{
|
|||
|
|
CheckFileChanged(true);
|
|||
|
|
return Table.FindRow(where) != null;
|
|||
|
|
}
|
|||
|
|
public MDataTable Select(int pageIndex, int pageSize, object where, out int rowCount, params object[] selectColumns)
|
|||
|
|
{
|
|||
|
|
CheckFileChanged(true);
|
|||
|
|
MDataTable dt = Table.Select(pageIndex, pageSize, where, selectColumns);
|
|||
|
|
rowCount = dt.RecordsAffected;
|
|||
|
|
return dt;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
private bool IsCanDoInsertCheck(int start)
|
|||
|
|
{
|
|||
|
|
bool isCanDo = false;
|
|||
|
|
for (int i = start; i < _Row.Count; i++)
|
|||
|
|
{
|
|||
|
|
if (_Row[i].State == 0 && !_Row[i].IsNull)
|
|||
|
|
{
|
|||
|
|
_Row[i].Value = null;
|
|||
|
|
}
|
|||
|
|
if (!_Row[i].IsNullOrEmpty)
|
|||
|
|
{
|
|||
|
|
isCanDo = true;
|
|||
|
|
}
|
|||
|
|
else if (Convert.ToString(_Row[i].Struct.DefaultValue).Length > 0)
|
|||
|
|
{
|
|||
|
|
_Row[i].SetDefaultValueToValue();
|
|||
|
|
if (!_Row[i].IsNullOrEmpty)
|
|||
|
|
{
|
|||
|
|
isCanDo = true;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
else if (!_Row[i].Struct.IsCanNull)
|
|||
|
|
{
|
|||
|
|
Error.Throw("Column [" + _Row[i].ColumnName + "] 's value can't be null or empty ! (tip:column property:iscannull=false)");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
if (!isCanDo && start == 1) // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><D5B5><EFBFBD><EFBFBD><EFBFBD>(<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
{
|
|||
|
|
return !_Row[0].IsNullOrEmpty && _Row[0].Struct.IsPrimaryKey && !_Row[0].Struct.IsAutoIncrement;
|
|||
|
|
}
|
|||
|
|
return isCanDo;
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region IDisposable <EFBFBD><EFBFBD>Ա
|
|||
|
|
public void Dispose()
|
|||
|
|
{
|
|||
|
|
if (string.IsNullOrEmpty(_FileFullName) || AppConfig.DB.IsTxtReadOnly)
|
|||
|
|
{
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
int state = needToSaveState;
|
|||
|
|
if (state > 0)
|
|||
|
|
{
|
|||
|
|
bool isFirstAddRow = (Table.Rows.Count - _insertRows.Count) == 0;//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>״<EFBFBD><D7B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݡ<EFBFBD>
|
|||
|
|
if (state > 1 || isFirstAddRow || _DalType == DataBaseType.Xml || _insertRows.Count == 0)
|
|||
|
|
{
|
|||
|
|
Save();
|
|||
|
|
}
|
|||
|
|
else//<2F>ı<EFBFBD><C4B1><EFBFBD><EFBFBD>в<EFBFBD><D0B2><EFBFBD>
|
|||
|
|
{
|
|||
|
|
StringBuilder sb = new StringBuilder();
|
|||
|
|
for (int i = 0; i < _insertRows.Count; i++)
|
|||
|
|
{
|
|||
|
|
sb.Append("," + AppConst.NewLine + _insertRows[i].ToJson(RowOp.None, false, EscapeOp.Encode));
|
|||
|
|
}
|
|||
|
|
_insertRows.Clear();//<2F><><EFBFBD><EFBFBD>
|
|||
|
|
if (!Tool.IOHelper.Append(_FileFullName, sb.ToString()))
|
|||
|
|
{
|
|||
|
|
Save();//ʧ<>ܣ<EFBFBD><DCA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>³<EFBFBD><C2B3><EFBFBD>д<EFBFBD>룡
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
needToSaveState = 0;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC>Ƿ<EFBFBD><C7B7>ѱ<EFBFBD><D1B1>Ĺ<DEB8>
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="isNeedToReloadTable"></param>
|
|||
|
|
private void CheckFileChanged(bool isNeedToReloadTable)
|
|||
|
|
{
|
|||
|
|
if (!_isDeleteAll.Contains(_FileFullName))
|
|||
|
|
{
|
|||
|
|
DateTime lastWriteTime = _lastWriteTimeList[_FileFullName];
|
|||
|
|
if (IOHelper.IsLastFileWriteTimeChanged(_FileFullName, ref lastWriteTime))
|
|||
|
|
{
|
|||
|
|
_lastWriteTimeList[_FileFullName] = lastWriteTime;
|
|||
|
|
if (isNeedToReloadTable && _tableList.ContainsKey(_FileFullName))
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
_tableList[_FileFullName].Rows.Clear();
|
|||
|
|
_tableList.Remove(_FileFullName);
|
|||
|
|
}
|
|||
|
|
catch// (Exception err)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
_Table = null;//<2F><>Ҫ<EFBFBD><D2AA><EFBFBD>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݡ<EFBFBD>
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void Save()
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
string text = string.Empty;
|
|||
|
|
if (string.IsNullOrEmpty(text))
|
|||
|
|
{
|
|||
|
|
text = _DalType == DataBaseType.Txt ? Table.ToJson(false, true, RowOp.None, false, EscapeOp.Encode).Replace("},{", "},\r\n{").Trim('[', ']') : Table.ToXml();
|
|||
|
|
}
|
|||
|
|
int tryAgainCount = 3;
|
|||
|
|
bool isError = false;
|
|||
|
|
do
|
|||
|
|
{
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
IOHelper.Write(_FileFullName, text, _DalType == DataBaseType.Txt ? IOHelper.DefaultEncoding : Encoding.UTF8);
|
|||
|
|
needToSaveState = 0;//<2F><><EFBFBD><EFBFBD>Ϊ0
|
|||
|
|
DateTime lastWriteTime = _lastWriteTimeList[_FileFullName];
|
|||
|
|
if (IOHelper.IsLastFileWriteTimeChanged(_FileFullName, ref lastWriteTime))
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
|
_lastWriteTimeList[_FileFullName] = lastWriteTime;
|
|||
|
|
}
|
|||
|
|
tryAgainCount = 0;
|
|||
|
|
if (_isDeleteAll.Contains(_FileFullName))
|
|||
|
|
{
|
|||
|
|
_isDeleteAll.Remove(_FileFullName);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch
|
|||
|
|
{
|
|||
|
|
tryAgainCount--;
|
|||
|
|
isError = true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (isError)
|
|||
|
|
{
|
|||
|
|
System.Threading.Thread.Sleep(20 * (4 - tryAgainCount));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
}
|
|||
|
|
while (tryAgainCount > 0);
|
|||
|
|
}
|
|||
|
|
catch (Exception err)
|
|||
|
|
{
|
|||
|
|
Log.Write(err, LogType.DataBase);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
}
|
|||
|
|
}
|