Skip to content

Commit

Permalink
402:MDataTable的AcceptChanges新增加Truncate属性(2016-10-20)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyq1162 committed Oct 20, 2016
1 parent f847517 commit 6cfedd1
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 55 deletions.
6 changes: 5 additions & 1 deletion Action/NoSqlAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,13 @@ private MDataTable Table
}
else if (_tableList.ContainsKey(_FileFullName))
{
return _tableList[_FileFullName];
_Table = _tableList[_FileFullName];
if (_Table.Rows.Count == 0)
{
if (_maxID.ContainsKey(_FileFullName))
{
_maxID.Remove(_FileFullName);
}
_tableList.Remove(_FileFullName);// 会引发最后一条数据无法删除的问题(已修正该问题,所以可开放)。
}
else
Expand Down
24 changes: 13 additions & 11 deletions Cache/CacheManage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public static void PreLoadDBSchemaToCache()
}
private static readonly object obj = new object();
/// <summary>
/// 通过该方法可以预先加载整个数据库的表结构缓存
/// 通过该方法可以预先加载整个数据库的表结构缓存(异常会抛,外层Try Catch)
/// </summary>
/// <param name="conn">指定数据链接</param>
/// <param name="isUseThread">是否开启线程</param>
Expand All @@ -208,20 +208,22 @@ public static void PreLoadDBSchemaToCache(string conn, bool isUseThread)
}
private static void LoadDBSchemaCache(object connObj)
{
string conn = Convert.ToString(connObj);
Dictionary<string, string> dic = DBTool.GetTables(Convert.ToString(conn));
if (dic != null && dic.Count > 0)
{
DbBase helper = DalCreate.CreateDal(conn);
if (helper.dalType != DalType.Txt && helper.dalType != DalType.Xml)

string conn = Convert.ToString(connObj);
Dictionary<string, string> dic = DBTool.GetTables(Convert.ToString(conn));
if (dic != null && dic.Count > 0)
{
foreach (string key in dic.Keys)
DbBase helper = DalCreate.CreateDal(conn);
if (helper.dalType != DalType.Txt && helper.dalType != DalType.Xml)
{
TableSchema.GetColumns(key, ref helper);
foreach (string key in dic.Keys)
{
TableSchema.GetColumns(key, ref helper);
}
}
helper.Dispose();
}
helper.Dispose();
}

}
}
/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,5 @@
//
// 可以指定所有这些值,也可以使用“修订号”和“内部版本号”的默认值,
// 方法是按如下所示使用“*”:
[assembly: AssemblyVersion("5.6.7.1")]
[assembly: AssemblyFileVersion("5.6.7.1")]
[assembly: AssemblyVersion("5.6.7.2")]
[assembly: AssemblyFileVersion("5.6.7.2")]
1 change: 1 addition & 0 deletions SQL/SqlCreate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,7 @@ internal static string GetWhereIn(MCellStruct ms, List<string> items, DalType da
internal static string MySqlBulkCopySql = "LOAD DATA LOCAL INFILE '{0}' INTO TABLE {1} CHARACTER SET utf8 FIELDS TERMINATED BY '{2}' LINES TERMINATED BY '\r\n' {3}";
internal static string OracleBulkCopySql = "LOAD DATA INFILE '{0}' APPEND INTO TABLE {1} FIELDS TERMINATED BY '{2}' OPTIONALLY ENCLOSED BY '\"' {3}";
internal static string OracleSqlIDR = " userid={0} control='{1}'";//sqlldr
internal static string TruncateTable = "truncate table {0}";
/// <summary>
/// 获得批量导入的列名。
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions Table/MDataRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1203,6 +1203,7 @@ internal void SetToEntity(ref object obj, MDataRow row)
{
#region 数组处理
List<string> items = JsonSplit.SplitEscapeArray(cell.strValue);//内部去掉转义符号
if (items == null) { continue; }
objValue = Activator.CreateInstance(propType, items.Count);//创建实例
Type objListType = objValue.GetType();
bool isArray = sysType == SysType.Array;
Expand Down
7 changes: 6 additions & 1 deletion Table/MDataTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,11 @@ public bool AcceptChanges(AcceptOp op, string newConn, params object[] jointPrim
return false;//木有可更新的。
}
MDataTableBatchAction action = new MDataTableBatchAction(this, newConn);
if ((op & AcceptOp.Truncate) != 0)
{
action.IsTruncate = true;
op = (AcceptOp)(op - AcceptOp.Truncate);
}
action.SetJoinPrimaryKeys(jointPrimaryKeys);
switch (op)
{
Expand Down Expand Up @@ -1690,7 +1695,7 @@ internal void Load(MDataTable dt, MCellStruct primary)
rowB = dt.FindRow(pkName + "='" + rowA[i1].strValue + "'");
if (rowB != null)
{
rowA.LoadFrom(rowB,RowOp.IgnoreNull, false);
rowA.LoadFrom(rowB, RowOp.IgnoreNull, false);
}
}
}
Expand Down
137 changes: 102 additions & 35 deletions Table/MDataTableBatchAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ internal partial class MDataTableBatchAction
MDataTable mdt, sourceTable;
internal DalType dalTypeTo = DalType.None;
internal string database = string.Empty;
private bool _IsTruncate;
/// <summary>
/// Insert前是否先清表
/// </summary>
public bool IsTruncate
{
get { return _IsTruncate; }
set { _IsTruncate = value; }
}

string _Conn = string.Empty;
/// <summary>
/// 内部操作对象(需要同一个事务处理)。
Expand Down Expand Up @@ -191,11 +201,11 @@ internal bool Insert(bool keepID)
{
return MsSqlBulkCopyInsert(keepID);
}
else if (dalTypeTo == DalType.Oracle && _dalHelper == null)
else if (dalTypeTo == DalType.Oracle && _dalHelper == null && !IsTruncate)
{
if (OracleDal.clientType == 1 && keepID)
{
return OracleBulkCopyInsert();
return OracleBulkCopyInsert();//不支持外部事务合并(因为参数只能传链接字符串。)
}
//else if (IsAllowBulkCopy(DalType.Oracle))
//{
Expand All @@ -204,7 +214,7 @@ internal bool Insert(bool keepID)
}
else if (dalTypeTo == DalType.MySql && IsAllowBulkCopy(DalType.MySql))
{
return LoadDataInsert(dalTypeTo, keepID);
return LoadDataInsert(dalTypeTo, keepID);
}

//if (dalTypeTo == DalType.Txt || dalTypeTo == DalType.Xml)
Expand Down Expand Up @@ -410,38 +420,68 @@ internal bool MsSqlBulkCopyInsert(bool keepID)
try
{
CheckGUIDAndDateTime(DalType.MsSql);
// string a, b, c;
string conn = AppConfig.GetConn(_Conn);// DAL.DalCreate.GetConnString(_Conn, out a, out b, out c);

string conn = AppConfig.GetConn(_Conn);
SqlTransaction sqlTran = null;
SqlConnection con = null;
if (_dalHelper != null)
bool isCreateDal = false;
if (_dalHelper == null)
{
sqlTran = _dalHelper._tran as SqlTransaction;
con = _dalHelper.Con as SqlConnection;
_dalHelper.OpenCon(null);//如果未开启,则开启
if (IsTruncate)
{
isCreateDal = true;
_dalHelper = DalCreate.CreateDal(conn);
}
else
{
con = new SqlConnection(conn);
con.Open();
}
}
else
bool isGoOn = true;
if (_dalHelper != null)
{
con = new SqlConnection(conn);
con.Open();
if (IsTruncate)
{
_dalHelper.isOpenTrans = true;
if (_dalHelper.ExeNonQuery(string.Format(SqlCreate.TruncateTable, mdt.TableName), false) == -2)
{
isGoOn = false;
sourceTable.DynamicData = _dalHelper.debugInfo;
Log.WriteLogToTxt(_dalHelper.debugInfo.ToString());
}
}
if (isGoOn)
{
sqlTran = _dalHelper._tran as SqlTransaction;
con = _dalHelper.Con as SqlConnection;
_dalHelper.OpenCon(null);//如果未开启,则开启
}
}

using (SqlBulkCopy sbc = new SqlBulkCopy(con, (keepID ? SqlBulkCopyOptions.KeepIdentity : SqlBulkCopyOptions.Default) | SqlBulkCopyOptions.FireTriggers, sqlTran))
if (isGoOn)
{
sbc.BatchSize = 100000;
sbc.DestinationTableName = SqlFormat.Keyword(mdt.TableName, DalType.MsSql);
sbc.BulkCopyTimeout = AppConfig.DB.CommandTimeout;
foreach (MCellStruct column in mdt.Columns)
using (SqlBulkCopy sbc = new SqlBulkCopy(con, (keepID ? SqlBulkCopyOptions.KeepIdentity : SqlBulkCopyOptions.Default) | SqlBulkCopyOptions.FireTriggers, sqlTran))
{
sbc.ColumnMappings.Add(column.ColumnName, column.ColumnName);
sbc.BatchSize = 100000;
sbc.DestinationTableName = SqlFormat.Keyword(mdt.TableName, DalType.MsSql);
sbc.BulkCopyTimeout = AppConfig.DB.CommandTimeout;
foreach (MCellStruct column in mdt.Columns)
{
sbc.ColumnMappings.Add(column.ColumnName, column.ColumnName);
}
sbc.WriteToServer(mdt);
}
sbc.WriteToServer(mdt);
}
if (_dalHelper == null)
{
con.Close();
con = null;
}
else if (isCreateDal)
{
_dalHelper.EndTransaction();
_dalHelper.Dispose();
}
return true;
}
catch (Exception err)
Expand Down Expand Up @@ -560,10 +600,17 @@ internal bool LoadDataInsert(DalType dalType, bool keepID)
}
else
{
if (_dalHelper.ExeNonQuery(sql, false) != -2)
bool isGoOn = true;
if (IsTruncate)
{
_dalHelper.isOpenTrans = true;//开启事务
isGoOn = _dalHelper.ExeNonQuery(string.Format(SqlCreate.TruncateTable, mdt.TableName), false) != -2;
}
if (isGoOn && _dalHelper.ExeNonQuery(sql, false) != -2)
{
return true;
}

}

}
Expand All @@ -580,10 +627,11 @@ internal bool LoadDataInsert(DalType dalType, bool keepID)
{
if (isNeedCreateDal && _dalHelper != null)
{
_dalHelper.EndTransaction();
_dalHelper.Dispose();
_dalHelper = null;
}
IOHelper.Delete(path);//删除文件。
//IOHelper.Delete(path);//删除文件。
}
return false;
}
Expand All @@ -596,14 +644,15 @@ private static string CreateCTL(string sql, string path)
private static string MDataTableToFile(MDataTable dt, bool keepID, DalType dalType)
{
string path = Path.GetTempPath() + dt.TableName + ".csv";//不能用.txt(会产生默认编码,影响第一行数据(空表时自增的ID被置为初始1)
using (StreamWriter sw = new StreamWriter(path, false, Encoding.UTF8))
using (StreamWriter sw = new StreamWriter(path, false, new UTF8Encoding(false)))
{
MCellStruct ms;
string value;
foreach (MDataRow row in dt.Rows)
{
for (int i = 0; i < dt.Columns.Count; i++)
{
#region 设置值
ms = dt.Columns[i];
if (!keepID && ms.IsAutoIncrement)
{
Expand Down Expand Up @@ -642,6 +691,7 @@ private static string MDataTableToFile(MDataTable dt, bool keepID, DalType dalTy
{
sw.Write(AppConst.SplitChar);
}
#endregion
}
sw.WriteLine();
}
Expand Down Expand Up @@ -841,20 +891,37 @@ internal bool NomalInsert(bool keepID)
{
action.SetIdentityInsertOn();
}
MDataRow row;
for (int i = 0; i < mdt.Rows.Count; i++)
bool isGoOn = true;
if (IsTruncate)
{
row = mdt.Rows[i];
action.ResetTable(row, false);
action.Data.SetState(1, BreakOp.Null);
result = action.Insert(InsertOp.None);
sourceTable.RecordsAffected = i;
if (!result)
if (dalTypeTo == DalType.Txt || dalTypeTo == DalType.Xml)
{
string msg = "Error On : MDataTable.AcceptChanges.Insert." + mdt.TableName + " : [" + row.PrimaryCell.Value + "] : " + action.DebugInfo;
sourceTable.DynamicData = msg;
Log.WriteLogToTxt(msg);
break;
action.Delete("1=1");
}
else if (action.dalHelper.ExeNonQuery(string.Format(SqlCreate.TruncateTable, action.TableName), false) == -2)
{
isGoOn = false;
sourceTable.DynamicData = action.DebugInfo;
Log.WriteLogToTxt(action.DebugInfo);
}
}
if (isGoOn)
{
MDataRow row;
for (int i = 0; i < mdt.Rows.Count; i++)
{
row = mdt.Rows[i];
action.ResetTable(row, false);
action.Data.SetState(1, BreakOp.Null);
result = action.Insert(InsertOp.None);
sourceTable.RecordsAffected = i;
if (!result)
{
string msg = "Error On : MDataTable.AcceptChanges.Insert." + mdt.TableName + " : [" + row.PrimaryCell.Value + "] : " + action.DebugInfo;
sourceTable.DynamicData = msg;
Log.WriteLogToTxt(msg);
break;
}
}
}
if (keepID)
Expand Down
13 changes: 9 additions & 4 deletions Table/TableEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,33 @@ namespace CYQ.Data.Table
/// <summary>
/// 批量更新选项
/// </summary>
[Flags]
public enum AcceptOp
{
/// <summary>
/// 批量插入(由系统产生自增加ID)
/// 该执行会开启事务。
/// </summary>
Insert,
Insert = 1,
/// <summary>
/// 批量插入(由用户指定ID插入)
/// 该执行会开启事务。
/// </summary>
InsertWithID,
InsertWithID = 2,
/// <summary>
/// 批量更新
/// 该执行会开启事务。
/// </summary>
Update,
Update = 4,
/// <summary>
/// 批量自动插入或更新(检测主键数据若存在,则更新;不存在,则插入)
/// 该执行不会开启事务。
/// </summary>
Auto
Auto = 8,
/// <summary>
/// 清空表(只有和Insert或InsertWithID组合使用时才有效)
/// </summary>
Truncate = 16
}
/// <summary>
/// MDataTable 与 MDataRow SetState 的过滤选项
Expand Down
3 changes: 2 additions & 1 deletion 更新记录.txt
Original file line number Diff line number Diff line change
Expand Up @@ -614,4 +614,5 @@
398��MDataRow��MDataColumn ��ToTable() ������Ӧ(����������ʾ)��2016-10-19)
399��MySql �����洢����Outֵ����2016-10-19��
400��MySql �������������Bit���ͺͿձ�ʱ����ID����Ϊ1�����⣨2016-10-20��
401��JsonHelper��NoSqlActionС�Ż�������2016-10-20��
401��JsonHelper��NoSqlActionС�Ż�������2016-10-20��
402��MDataTable��AcceptChanges������Truncate���ԣ�2016-10-20��

0 comments on commit 6cfedd1

Please sign in to comment.