Skip to content

Commit

Permalink
427:修正MDataTable的AcceptChanges方法(MSSQL异常时链接未关闭问题)(2016-11-12)
Browse files Browse the repository at this point in the history
428:JsonHelper、MDataTable 增加对List<string>等基础类型的Json转换(2016-11-12)
  • Loading branch information
cyq1162 committed Nov 12, 2016
1 parent a6e8ac5 commit 005808b
Show file tree
Hide file tree
Showing 13 changed files with 115 additions and 68 deletions.
5 changes: 4 additions & 1 deletion Action/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using CYQ.Data.SQL;
using CYQ.Data.Tool;
using System.Web;
using System.Collections.Generic;

namespace CYQ.Data
{
Expand Down Expand Up @@ -551,7 +552,9 @@ public static string FilterSqlInjection
SetApp("FilterSqlInjection", value);
if (!string.IsNullOrEmpty(value))
{
SqlInjection.FilterKeyList = value.TrimEnd(',').Split(',');
List<string> list = new List<string>();
list.AddRange(value.TrimEnd(',').Split(','));
SqlInjection.FilterKeyList = list;
}
}
}
Expand Down
16 changes: 10 additions & 6 deletions Action/MAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,7 @@ public MAction SetPara(object paraName, object value, DbType dbType)
}
return this;
}

/// <summary>
/// Aop scenes can only be used by the parameterization of the Senate
/// <para>Aop场景才可能使用的参数化传参</para>
Expand Down Expand Up @@ -1244,7 +1244,7 @@ public void ClearPara()
customParaNames.Clear();
}
}


/// <summary>
/// 清除系统参数[保留自定义参数]
Expand Down Expand Up @@ -1297,17 +1297,21 @@ public MAction SetSelectColumns(params object[] columnNames)
/// Get where statement
/// <para>根据元数据列组合where条件。</para>
/// </summary>

/// <param name="isAnd">connect by and/or<para>true为and连接,反之为or链接</para></param>
public string GetWhere(bool isAnd, params MDataCell[] cells)
{
return SqlCreate.GetWhere(DalType, isAnd, cells);
List<MDataCell> cs = new List<MDataCell>(cells.Length);
if (cs.Count > 0)
{
cs.AddRange(cells);
}
return SqlCreate.GetWhere(DalType, isAnd, cs);
}

/// <param name="isAnd">connect by and/or<para>true为and连接,反之为or链接</para></param>
/// <param name="cells">MDataCell<para>单元格</para></param>
public string GetWhere(params MDataCell[] cells)
{
return SqlCreate.GetWhere(DalType, cells);
return GetWhere(true, cells);
}


Expand Down
6 changes: 3 additions & 3 deletions Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[assembly: AssemblyTitle("CYQ.Data 数据层(ORM)框架 V5 版本")]
[assembly: AssemblyDescription("论坛:http://www.cyqdata.com/cyqdata")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("路过秋天 (2016-11-10)")]
[assembly: AssemblyCompany("路过秋天 (2016-11-12)")]
[assembly: AssemblyProduct("CYQ.Data 数据层(ORM)框架 V5 版本")]
[assembly: AssemblyCopyright("版权所有 (C) 路过秋天 2010-2020")]
[assembly: AssemblyTrademark("CYQ.Data")]
Expand All @@ -31,5 +31,5 @@
//
// 可以指定所有这些值,也可以使用“修订号”和“内部版本号”的默认值,
// 方法是按如下所示使用“*”:
[assembly: AssemblyVersion("5.7.4.1")]
[assembly: AssemblyFileVersion("5.7.4.1")]
[assembly: AssemblyVersion("5.7.4.2")]
[assembly: AssemblyFileVersion("5.7.4.2")]
8 changes: 4 additions & 4 deletions SQL/SqlCreate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ internal string GetColumnsSql()
/// <returns></returns>
internal string GetPrimaryWhere()
{
return GetWhere(_action.DalType, _action.Data.JointPrimaryCell.ToArray());
return GetWhere(_action.DalType, _action.Data.JointPrimaryCell);
}
#endregion
#region 共用函数
Expand Down Expand Up @@ -678,19 +678,19 @@ internal static string AddOrderBy(string where, string primaryKey)
}
return where;
}
internal static string GetWhere(DalType dalType, params MDataCell[] cells)
internal static string GetWhere(DalType dalType, List<MDataCell> cells)
{
return GetWhere(dalType, true, cells);
}
/// <summary>
/// 根据元数据列组合where条件。
/// </summary>
/// <returns></returns>
internal static string GetWhere(DalType dalType, bool isAnd, params MDataCell[] cells)
internal static string GetWhere(DalType dalType, bool isAnd, List<MDataCell> cells)
{
string where = "";
MDataCell cell;
for (int i = 0; i < cells.Length; i++)
for (int i = 0; i < cells.Count; i++)
{
cell = cells[i];
if (i > 0)
Expand Down
13 changes: 9 additions & 4 deletions SQL/SqlInjection.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;



Expand All @@ -10,14 +11,18 @@ internal static class SqlInjection
//select;from,
internal const string filterSqlInjection = "select;into,delete;from,drop;table,drop;database,update;set,truncate;table,create;table,exists;select,insert;into,xp_cmdshell,declare;@,exec;master,waitfor;delay";
//internal const string replaceSqlInjection = "--";
private static string[] filterKeyList = null;
internal static string[] FilterKeyList
private static List<string> filterKeyList = null;
/// <summary>
/// 用List 也是因为内存读写异常问题(所有的[]数组,看似都有这问题)
/// </summary>
internal static List<string> FilterKeyList
{
get
{
if (filterKeyList == null)
{
filterKeyList = filterSqlInjection.TrimEnd(',').Split(',');
filterKeyList = new List<string>();
filterKeyList.AddRange(filterSqlInjection.TrimEnd(',').Split(','));
}
return filterKeyList;
}
Expand Down Expand Up @@ -81,7 +86,7 @@ public static string Filter(string text, DalType dalType)
string tempKey = string.Empty;
string filterKey = string.Empty;
string[] filterSpitItems = null;
for (int i = 0; i < FilterKeyList.Length; i++)
for (int i = 0; i < FilterKeyList.Count; i++)
{
filterSpitItems = filterKeyList[i].Split(';');//分隔
filterKey = filterSpitItems[0];//取第一个为关键词
Expand Down
15 changes: 12 additions & 3 deletions SQL/TableSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ public static MDataColumn GetColumns(Type typeInfo)

}

PropertyInfo[] pis = StaticTool.GetPropertyInfo(typeInfo);
List<PropertyInfo> pis = StaticTool.GetPropertyInfo(typeInfo);

SqlDbType sqlType;
for (int i = 0; i < pis.Length; i++)
for (int i = 0; i < pis.Count; i++)
{
sqlType = SQL.DataType.GetSqlType(pis[i].PropertyType);
mdc.Add(pis[i].Name, sqlType);
Expand All @@ -85,7 +85,7 @@ public static MDataColumn GetColumns(Type typeInfo)
column.IsAutoIncrement = true;
}
}
else if (i > pis.Length - 3 && sqlType == SqlDbType.DateTime && pis[i].Name.EndsWith("Time"))
else if (i > pis.Count - 3 && sqlType == SqlDbType.DateTime && pis[i].Name.EndsWith("Time"))
{
column.DefaultValue = SqlValue.GetDate;
}
Expand Down Expand Up @@ -571,6 +571,15 @@ public static Dictionary<string, string> GetTables(ref DbBase helper)
break;
case DalType.Txt:
case DalType.Xml:
tables = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
string folder = Path.GetDirectoryName(helper.conn.Split(';')[0].Split('=')[1]);
string[] files = Directory.GetFiles(folder, "*.ts");
foreach (string file in files)
{
tables.Add(Path.GetFileNameWithoutExtension(file), "");
}
files = null;
break;
case DalType.Access:
case DalType.SQLite:
case DalType.Sybase:
Expand Down
13 changes: 10 additions & 3 deletions Table/MDataRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,14 @@ public bool WriteJson(string fileName, RowOp op)
/// <typeparam name="T">实体名称</typeparam>
public T ToEntity<T>()
{
object obj = Activator.CreateInstance(typeof(T));
Type t = typeof(T);
switch (StaticTool.GetSystemType(ref t))
{
case SysType.Base:
return (T)StaticTool.ChangeType(this[0].Value, t);

}
object obj = Activator.CreateInstance(t);
SetToEntity(ref obj, this);
return (T)obj;
}
Expand Down Expand Up @@ -1081,7 +1088,7 @@ public void LoadFrom(object entity, BreakOp op)
{
TableName = t.Name;
}
PropertyInfo[] pis = StaticTool.GetPropertyInfo(t);
List<PropertyInfo> pis = StaticTool.GetPropertyInfo(t);
if (pis != null)
{
foreach (PropertyInfo pi in pis)
Expand Down Expand Up @@ -1146,7 +1153,7 @@ internal void SetToEntity(ref object obj, MDataRow row)
try
{
#region 处理核心
PropertyInfo[] pis = StaticTool.GetPropertyInfo(objType);
List<PropertyInfo> pis = StaticTool.GetPropertyInfo(objType);
foreach (PropertyInfo p in pis)
{
cellName = p.Name;
Expand Down
39 changes: 20 additions & 19 deletions Table/MDataTableBatchAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,22 +175,21 @@ internal void SetJoinPrimaryKeys(object[] jointPrimaryKeys)
}
}
}
internal MDataCell[] GetJoinPrimaryCell(MDataRow row)
internal List<MDataCell> GetJoinPrimaryCell(MDataRow row)
{
MDataCell[] cells = null;
if (jointPrimaryIndex != null && jointPrimaryIndex.Count > 0)
{
cells = new MDataCell[jointPrimaryIndex.Count];
List<MDataCell> cells = new List<MDataCell>(jointPrimaryIndex.Count);
for (int i = 0; i < jointPrimaryIndex.Count; i++)
{
cells[i] = row[jointPrimaryIndex[i]];
cells.Add(row[jointPrimaryIndex[i]]);
}
return cells;
}
else
{
cells = row.JointPrimaryCell.ToArray();
return row.JointPrimaryCell;
}
return cells;
}

internal bool Insert(bool keepID)
Expand Down Expand Up @@ -431,14 +430,13 @@ internal bool Delete()
#region 批量插入
internal bool MsSqlBulkCopyInsert(bool keepID)
{
SqlTransaction sqlTran = null;
SqlConnection con = null;
bool isCreateDal = false;
try
{
CheckGUIDAndDateTime(DalType.MsSql);

string conn = AppConfig.GetConn(_Conn);
SqlTransaction sqlTran = null;
SqlConnection con = null;
bool isCreateDal = false;
if (_dalHelper == null)
{
if (IsTruncate)
Expand All @@ -458,7 +456,7 @@ internal bool MsSqlBulkCopyInsert(bool keepID)
if (IsTruncate)
{
_dalHelper.isOpenTrans = true;
if (_dalHelper.ExeNonQuery(string.Format(SqlCreate.TruncateTable, mdt.TableName), false) == -2)
if (_dalHelper.ExeNonQuery(string.Format(SqlCreate.TruncateTable, SqlFormat.Keyword(mdt.TableName, dalTypeTo)), false) == -2)
{
isGoOn = false;
sourceTable.DynamicData = _dalHelper.debugInfo;
Expand Down Expand Up @@ -486,6 +484,15 @@ internal bool MsSqlBulkCopyInsert(bool keepID)
sbc.WriteToServer(mdt);
}
}
return true;
}
catch (Exception err)
{
sourceTable.DynamicData = err;
Log.WriteLogToTxt(err);
}
finally
{
if (_dalHelper == null)
{
con.Close();
Expand All @@ -496,12 +503,6 @@ internal bool MsSqlBulkCopyInsert(bool keepID)
_dalHelper.EndTransaction();
_dalHelper.Dispose();
}
return true;
}
catch (Exception err)
{
sourceTable.DynamicData = err;
Log.WriteLogToTxt(err);
}
return false;
}
Expand Down Expand Up @@ -618,7 +619,7 @@ internal bool LoadDataInsert(DalType dalType, bool keepID)
if (IsTruncate)
{
_dalHelper.isOpenTrans = true;//开启事务
isGoOn = _dalHelper.ExeNonQuery(string.Format(SqlCreate.TruncateTable, mdt.TableName), false) != -2;
isGoOn = _dalHelper.ExeNonQuery(string.Format(SqlCreate.TruncateTable, SqlFormat.Keyword(mdt.TableName, dalTypeTo)), false) != -2;
}
if (isGoOn && _dalHelper.ExeNonQuery(sql, false) != -2)
{
Expand Down Expand Up @@ -972,7 +973,7 @@ internal bool NomalInsert(bool keepID)
{
action.Delete("1=1");
}
else if (action.dalHelper.ExeNonQuery(string.Format(SqlCreate.TruncateTable, action.TableName), false) == -2)
else if (action.dalHelper.ExeNonQuery(string.Format(SqlCreate.TruncateTable, SqlFormat.Keyword(action.TableName, dalTypeTo)), false) == -2)
{
isGoOn = false;
sourceTable.DynamicData = action.DebugInfo;
Expand Down
Loading

0 comments on commit 005808b

Please sign in to comment.