Skip to content

Commit

Permalink
306:优化通过Reader获取列结构(该方法不靠谱,需要重新修正元数据的DataType、Size、Scale、DalType等参数)(…
Browse files Browse the repository at this point in the history
…2016-07-16)

307:为MDataTable和MAction的Set方法增加重载Set(key,value,state),在循环赋值时,产生批量更新时,可以对state赋值2(2016-07-21)
308:增加贴心功能:自定义参数化语句@符号,在各数据库自动被替换成相应的?或:符号兼容多数据库。(2016-07-22)
309:增强MDataTable绑定到Winform和WFP的功能(2016-07-23)
  • Loading branch information
cyq1162 committed Jul 23, 2016
1 parent f21f183 commit 06ea58c
Show file tree
Hide file tree
Showing 16 changed files with 364 additions and 233 deletions.
4 changes: 4 additions & 0 deletions Action/AppConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,10 @@ public static string HiddenFields
}
set
{
if (!value.Contains("cyqrownum,rowguid,"))
{
value = "cyqrownum,rowguid," + value;
}
SetApp("HiddenFields", value);
}
}
Expand Down
25 changes: 24 additions & 1 deletion Action/MAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ public MDataTable Select(int pageIndex, int pageSize, object where, out int rowC
}
if (sdReader != null)
{
// _aop.Para.Table.ReadFromDbDataReader(sdReader);//内部有关闭。
// _aop.Para.Table.ReadFromDbDataReader(sdReader);//内部有关闭。
_aop.Para.Table = sdReader;
if (!byPager)
{
Expand Down Expand Up @@ -961,6 +961,16 @@ public MDataTable Select(int pageIndex, int pageSize, object where, out int rowC
}
_aop.Para.Table.TableName = TableName;//Aop从Json缓存加载时会丢失表名。
_aop.Para.Table.Conn = _Data.Conn;
//修正DataType和Size、Scale
for (int i = 0; i < _aop.Para.Table.Columns.Count; i++)
{
MCellStruct msTable = _aop.Para.Table.Columns[i];
MCellStruct ms = _Data.Columns[msTable.ColumnName];
if (ms != null)
{
msTable.Load(ms);
}
}
if (_sqlCreate != null)
{
_sqlCreate.selectColumns = null;
Expand Down Expand Up @@ -1186,11 +1196,24 @@ public T Get<T>(object key, T defaultValue)
/// get示例:int id=action.Get&lt;int&gt;(Users.ID);
/// </code></example>
public MAction Set(object key, object value)
{
return Set(key, value, -1);
}
/// <summary>
/// 设置值
/// </summary>
/// <param name="state">手工设置状态[0:未更改;1:已赋值,值相同[可插入];2:已赋值,值不同[可更新]]</param>
/// <returns></returns>
public MAction Set(object key, object value, int state)
{
MDataCell cell = _Data[key];
if (cell != null)
{
cell.Value = value;
if (state > 0 && state < 3)
{
cell.State = state;
}
}
else
{
Expand Down
1 change: 0 additions & 1 deletion Cache/LocalCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ internal LocalCache()
catch (Exception err)
{
Log.WriteLogToTxt(err);
// throw;
}
}
int taskCount = 0, taskInterval = 10;//10分钟清一次缓存。
Expand Down
22 changes: 22 additions & 0 deletions DAL/DalCreate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,28 @@ public static DalType GetDalTypeByConn(string conn)
{
return GetConnBean(conn).ConnDalType;
}
public static DalType GetDalTypeByReaderName(string typeName)
{
switch (typeName.Replace("DataReader", "").ToLower())
{
case "oracle":
return DalType.Oracle;
case "sql":
return DalType.MsSql;
case "sqlite":
return DalType.SQLite;
case "oledb":
return DalType.Access;
case "mysql":
return DalType.MySql;
case "odbc":
case "ase":
return DalType.Sybase;
default:
return DalType.None;

}
}
public static string GetProvider(string connString)
{
connString = connString.ToLower().Replace(" ", "");//去掉空格
Expand Down
16 changes: 15 additions & 1 deletion DAL/DbBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -652,9 +652,23 @@ private void SetCommandText(string commandText, bool isProc)
//取消多余的参数,新加的小贴心,过滤掉用户不小心写多的参数。
if (_com != null && _com.Parameters != null && _com.Parameters.Count > 0)
{
bool needToReplace = (dalType == DalType.Oracle || dalType == DalType.MySql) && _com.CommandText.Contains("@");
string paraName;
for (int i = 0; i < _com.Parameters.Count; i++)
{
if (commandText.IndexOf(_com.Parameters[i].ParameterName, StringComparison.OrdinalIgnoreCase) == -1)
paraName = _com.Parameters[i].ParameterName.TrimStart(Pre);//默认自带前缀的,取消再判断
if (needToReplace && _com.CommandText.IndexOf("@" + paraName) > -1)
{
//兼容多数据库的参数(虽然提供了=:?"为兼容语法,但还是贴心的再处理一下)
switch (dalType)
{
case DalType.Oracle:
case DalType.MySql:
_com.CommandText = _com.CommandText.Replace("@" + paraName, Pre + paraName);
break;
}
}
if (_com.CommandText.IndexOf(Pre + paraName, StringComparison.OrdinalIgnoreCase) == -1)
{
_com.Parameters.RemoveAt(i);
i--;
Expand Down
5 changes: 4 additions & 1 deletion Orm/SimpleOrmBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ protected void SetInit(Object entityInstance, string tableName, string conn)
Columns = TableSchema.GetColumns(typeInfo);
if (!DBTool.ExistsTable(tableName, conn))
{
DBTool.CreateTable(tableName, Columns, conn);
if (!DBTool.CreateTable(tableName, Columns, conn))
{
Error.Throw("Create Table Error:" + tableName);
}
}
}
else if (isTxtDal)//文本数据库
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-07-16)")]
[assembly: AssemblyCompany("秋式软件 (2016-07-23)")]
[assembly: AssemblyProduct("CYQ.Data 数据层(ORM)框架 V5 版本")]
[assembly: AssemblyCopyright("版权所有 (C) 秋式软件 2010-2020")]
[assembly: AssemblyTrademark("CYQ.Data")]
Expand All @@ -31,5 +31,5 @@
//
// 可以指定所有这些值,也可以使用“修订号”和“内部版本号”的默认值,
// 方法是按如下所示使用“*”:
[assembly: AssemblyVersion("5.6.3.2")]
[assembly: AssemblyFileVersion("5.6.3.2")]
[assembly: AssemblyVersion("5.6.3.3")]
[assembly: AssemblyFileVersion("5.6.3.3")]
34 changes: 30 additions & 4 deletions SQL/TableSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ public static MDataColumn GetColumns(string tableName, ref DbBase dbHelper)
case DalType.Xml:
tableName = Path.GetFileNameWithoutExtension(tableName);//视图表,带“.“的,会出问题
string fileName = dbHelper.Con.DataSource + tableName + (dalType == DalType.Txt ? ".txt" : ".xml");
return MDataColumn.CreateFrom(fileName);
MDataColumn mdc = MDataColumn.CreateFrom(fileName);
mdc.dalType = dalType;
return mdc;
}

MDataColumn mdcs = new MDataColumn();
Expand Down Expand Up @@ -385,6 +387,20 @@ public static MDataColumn GetColumns(string tableName, ref DbBase dbHelper)
}
return mdcs;
}
/// <summary>
/// DbDataReader的GetSchema拿到的DataType、Size、Scale很不靠谱
/// </summary>
internal static void FixTableSchemaType(DbDataReader sdr, DataTable tableSchema)
{
if (sdr != null && tableSchema != null)
{
tableSchema.Columns.Add("DataTypeString");
for (int i = 0; i < sdr.FieldCount; i++)
{
tableSchema.Rows[i]["DataTypeString"] = sdr.GetDataTypeName(i);
}
}
}
internal static MDataColumn GetColumns(DataTable tableSchema)
{
MDataColumn mdcs = new MDataColumn();
Expand All @@ -396,10 +412,14 @@ internal static MDataColumn GetColumns(DataTable tableSchema)
int maxSize = -1;
short maxSizeScale = 0;
SqlDbType sqlDbType;
string dataTypeName = "DataTypeName";
string dataTypeName = "DataTypeString";
if (!tableSchema.Columns.Contains(dataTypeName))
{
dataTypeName = "DataType";
if (!tableSchema.Columns.Contains(dataTypeName))
{
dataTypeName = "DataTypeName";
}
}
bool isHasAutoIncrement = tableSchema.Columns.Contains("IsAutoIncrement");
bool isHasHidden = tableSchema.Columns.Contains("IsHidden");
Expand Down Expand Up @@ -479,9 +499,12 @@ private static MDataColumn GetViewColumns(string sqlText, ref DbBase helper)
if (sdr != null)
{
keyDt = sdr.GetSchemaTable();
FixTableSchemaType(sdr, keyDt);
sdr.Close();
}
return GetColumns(keyDt);
MDataColumn mdc = GetColumns(keyDt);
mdc.dalType = helper.dalType;
return mdc;

}
/// <summary>
Expand Down Expand Up @@ -553,7 +576,10 @@ public static Dictionary<string, string> GetTables(ref DbBase helper)
tableName = Convert.ToString(sdr["TableName"]);
if (!tables.ContainsKey(tableName))
{
tables.Add(tableName, Convert.ToString(sdr["Description"]));
if (!tableName.StartsWith("BIN$"))//Oracle的已删除的表。
{
tables.Add(tableName, Convert.ToString(sdr["Description"]));
}
}
else
{
Expand Down
17 changes: 15 additions & 2 deletions Table/MDataRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -355,18 +355,29 @@ public int GetState(bool ignorePrimaryKey)
}
return state;
}

/// <summary>
/// 为行设置值
/// </summary>
public MDataRow Set(object key, object value)
{
return Set(key, value, -1);
}
/// <summary>
/// 为行设置值
/// </summary>
/// <param name="key">字段名</param>
/// <param name="value">值</param>
public MDataRow Set(object key, object value)
/// <param name="state">手工设置状态[0:未更改;1:已赋值,值相同[可插入];2:已赋值,值不同[可更新]]</param>
public MDataRow Set(object key, object value, int state)
{
MDataCell cell = this[key];
if (cell != null)
{
cell.Value = value;
if (state > 0 && state < 3)
{
cell.State = state;
}
}
return this;
}
Expand Down Expand Up @@ -1207,6 +1218,7 @@ internal void SetToEntity(ref object obj, MDataRow row)
}

}

public partial class MDataRow : System.ComponentModel.ICustomTypeDescriptor
{
#region ICustomTypeDescriptor 成员
Expand Down Expand Up @@ -1287,4 +1299,5 @@ PropertyDescriptorCollection Create()
}
#endregion
}

}
Loading

0 comments on commit 06ea58c

Please sign in to comment.