You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

744 lines
28 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Mesnac.Controls.Base;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using Mesnac.Codd.Session;
using Mesnac.Basic;
using Mesnac.Gui.Edit.Global;
using System.ComponentModel;
namespace Mesnac.Action.Base
{
#region 数据库操作控件基本信息
/// <summary>
/// 软控数据库控件命名解析
/// </summary>
public class DbMCControlDesignKeyConfig
{
/// <summary>
/// 解析命名
/// </summary>
/// <param name="ss"></param>
/// <returns></returns>
public string[] SplitKey(string ss)
{
List<string> Result = new List<string>();
if (!string.IsNullOrWhiteSpace(ss))
{
string[] arr = ss.Split(new string[] { "].[" }, StringSplitOptions.RemoveEmptyEntries);
foreach (string a in arr)
{
string str = a.Trim();
if (str.StartsWith("["))
{
str = str.Substring(1);
}
if (str.EndsWith("]"))
{
str = str.Substring(0, str.Length - 1);
}
Result.Add(str);
}
}
return Result.ToArray();
}
/// <summary>
/// 组合命名
/// </summary>
/// <param name="ss"></param>
/// <returns></returns>
public string ToString(string[] ss)
{
StringBuilder sb = new StringBuilder("[");
foreach (string s in ss)
{
if (!string.IsNullOrWhiteSpace(s))
{
sb.Append(s.Trim()).Append("].[");
}
}
if (sb.Length > 2)
{
sb.Remove(sb.Length - 2, 2);
return sb.ToString();
}
else
{
return string.Empty;
}
}
}
/// <summary>
/// 软控不可见控件 MCDataSource 的解析封装
/// </summary>
public class DbMCSource
{
public DbMCSource(MCDataSource source)
{
this.MCDataSource = source;
string[] dbInfo_dbsource = new DbMCControlDesignKeyConfig().SplitKey(source.TableName.ToString());
if ((dbInfo_dbsource != null) && (dbInfo_dbsource.Length >= 2))
{
this.DesignSource = dbInfo_dbsource[0];
this.DataTable = dbInfo_dbsource[1];
}
if (!string.IsNullOrWhiteSpace(this.DesignSource))
{
DataSourceItem item;
if (DataSourceFactory.Instance.DataSources.TryGetValue(this.DesignSource, out item))
{
this.DataSourceItem = item;
}
}
}
/// <summary>
/// 不可见控件MCDataSource
/// </summary>
public MCDataSource MCDataSource { get; private set; }
/// <summary>
/// 数据连接信息
/// </summary>
public DataSourceItem DataSourceItem { get; private set; }
/// <summary>
/// 数据源名称
/// </summary>
public string DesignSource { get; private set; }
/// <summary>
/// 表名
/// </summary>
public string DataTable { get; private set; }
/// <summary>
/// 标准名称
/// </summary>
public string DbMCKey
{
get
{
DbMCControlDesignKeyConfig config = new DbMCControlDesignKeyConfig();
return config.ToString(new string[] { this.DesignSource, this.DataTable });
}
}
}
/// <summary>
/// 软控控件 的解析封装
/// </summary>
public class DbMCControl
{
public DbMCControl(DbMCSource source, IBaseControl control)
{
this.BaseControl = control;
this.DbMCSource = source;
string[] dbInfo_control = new DbMCControlDesignKeyConfig().SplitKey(control.MCKey);
if (source != null)
{
this.DesignSource = source.DesignSource;
this.DataTable = source.DataTable;
if (dbInfo_control.Length > 1)
{
if ((dbInfo_control[0].Equals(source.DesignSource, StringComparison.CurrentCultureIgnoreCase))
&& (dbInfo_control[1].Equals(source.DataTable, StringComparison.CurrentCultureIgnoreCase)))
{
if (dbInfo_control.Length > 2)
{
this.DataField = dbInfo_control[2];
}
}
}
else if (dbInfo_control.Length > 0)
{
this.DataField = dbInfo_control[0];
}
}
else if (dbInfo_control.Length >= 2)
{
this.DesignSource = dbInfo_control[0];
this.DataTable = dbInfo_control[1];
if (dbInfo_control.Length > 2)
{
this.DataField = dbInfo_control[2];
}
}
if (!string.IsNullOrWhiteSpace(this.DesignSource))
{
DataSourceItem item;
if (DataSourceFactory.Instance.DataSources.TryGetValue(this.DesignSource, out item))
{
this.DataSourceItem = item;
}
}
}
/// <summary>
/// 软控控件
/// </summary>
public IBaseControl BaseControl { get; private set; }
/// <summary>
/// 软控控件数据源
/// </summary>
public DbMCSource DbMCSource { get; set; }
/// <summary>
/// 数据连接信息
/// </summary>
public DataSourceItem DataSourceItem { get; private set; }
/// <summary>
/// 数据源名称
/// </summary>
public string DesignSource { get; private set; }
/// <summary>
/// 表名
/// </summary>
public string DataTable { get; private set; }
/// <summary>
/// 字段名
/// </summary>
public string DataField { get; private set; }
/// <summary>
/// 标准名称
/// </summary>
public string DbMCKey
{
get
{
DbMCControlDesignKeyConfig config = new DbMCControlDesignKeyConfig();
return config.ToString(new string[] { this.DesignSource, this.DataTable, this.DataField });
}
}
}
#endregion
/// <summary>
/// 数据操作类
/// </summary>
public class DatabaseAction : BaseAction
{
#region RunIni
/// <summary>
/// 接口方法
/// </summary>
/// <param name="sender"></param>
public new void RunIni(RuntimeParameter runtime)
{
base.RunIni(runtime); //必须调用
}
#endregion
#region GetAllDbMCSources
private List<DbMCSource> getAllDbMCSources(List<Component> clist)
{
List<DbMCSource> Result = new List<DbMCSource>();
foreach (Component component in clist)
{
if ((component is MCDataSource) && (!string.IsNullOrWhiteSpace(component.Site.Name)))
{
Result.Add(new DbMCSource(component as MCDataSource));
}
}
return Result;
}
protected List<DbMCSource> GetThisFormAllDbMCSources()
{
return getAllDbMCSources(GetThisFormAllComponents());
}
protected List<DbMCSource> GetAllFormAllDbMCSources()
{
return getAllDbMCSources(GetAllFormAllComponents());
}
protected List<DbMCSource> GetThisOrAllFormAllDbMCSources()
{
List<DbMCSource> Result = GetThisFormAllDbMCSources();
if (Result.Count > 0)
{
return Result;
}
return GetAllFormAllDbMCSources();
}
public List<DbMCSource> GetAllDbMCSources()
{
return GetThisFormAllDbMCSources();
}
#endregion
#region GetAllDbMCControls
private List<DbMCControl> getAllDbMCControls(List<DbMCSource> lstDbMCSource, List<IBaseControl> lstControl)
{
List<DbMCControl> Result = new List<DbMCControl>();
foreach (IBaseControl control in lstControl)
{
if (!control.IsDbControl)
{
continue;
}
//初始化MCDataSource属性
DbMCSource source = null;
if (!string.IsNullOrWhiteSpace(control.MCDataSourceID))
{
foreach (DbMCSource component in lstDbMCSource)
{
if (component.MCDataSource.Equals(control.MCDataSource))
{
source = component;
break;
}
}
}
Result.Add(new DbMCControl(source, control));
}
foreach (DbMCControl control in Result)
{
if (control.DbMCSource != null)
{
continue;
}
foreach (DbMCSource dbsource in lstDbMCSource)
{
if ((control.DesignSource == dbsource.DesignSource) &&
(control.DataTable == dbsource.DataTable))
{
control.DbMCSource = dbsource;
}
}
}
return Result;
}
protected List<DbMCControl> GetThisFormAllDbMCControls()
{
return getAllDbMCControls(GetThisFormAllDbMCSources(), GetThisFormAllMCControls());
}
protected List<DbMCControl> GetAllFormAllDbMCControls()
{
return getAllDbMCControls(GetAllFormAllDbMCSources(), GetAllFormAllMCControls());
}
protected List<DbMCControl> GetThisOrAllFormAllDbMCControls()
{
List<DbMCControl> Result = GetThisFormAllDbMCControls();
if (Result.Count > 0)
{
return Result;
}
return GetAllFormAllDbMCControls();
}
public List<DbMCControl> GetAllDbMCControls()
{
return GetThisFormAllDbMCControls();
}
#endregion
#region GetAllDbMCControlsByOption
private List<DbMCControl> getAllDbMCControlsByOption(DbOptionTypes dbopttype, List<DbMCSource> lstDbMCSource, List<IBaseControl> lstControl)
{
List<DbMCControl> Result = new List<DbMCControl>();
foreach (IBaseControl control in lstControl)
{
if (!(control is IBaseControl) || control.DbOptionType != dbopttype)
{
continue;
}
//初始化MCDataSource属性
DbMCSource source = null;
if (!string.IsNullOrWhiteSpace(control.MCDataSourceID))
{
foreach (DbMCSource component in lstDbMCSource)
{
if (component.MCDataSource.Site.Name == control.MCDataSourceID)
{
source = component;
break;
}
}
}
Result.Add(new DbMCControl(source, control));
}
foreach (DbMCControl control in Result)
{
if (control.DbMCSource != null)
{
continue;
}
foreach (DbMCSource dbsource in lstDbMCSource)
{
if ((control.DesignSource == dbsource.DesignSource) &&
(control.DataTable == dbsource.DataTable))
{
control.DbMCSource = dbsource;
}
}
}
return Result;
}
protected List<DbMCControl> GetThisFormAllDbMCControlsByOption(DbOptionTypes dbopttype)
{
return getAllDbMCControlsByOption(dbopttype, GetThisFormAllDbMCSources(), GetThisFormAllMCControls());
}
protected List<DbMCControl> GetAllFormAllDbMCControlsByOption(DbOptionTypes dbopttype)
{
return getAllDbMCControlsByOption(dbopttype, GetAllFormAllDbMCSources(), GetAllFormAllMCControls());
}
protected List<DbMCControl> GetThisOrAllFormAllDbMCControlsByOption(DbOptionTypes dbopttype)
{
List<DbMCControl> Result = GetThisFormAllDbMCControlsByOption(dbopttype);
if (Result.Count > 0)
{
return Result;
}
return GetAllFormAllDbMCControlsByOption(dbopttype);
}
public List<DbMCControl> GetAllDbMCControlsByOption(DbOptionTypes dbopttype)
{
return GetThisFormAllDbMCControlsByOption(dbopttype);
}
#endregion
#region GetDbMCControlByKey
private List<DbMCControl> getDbMCControlByKey(string key, List<DbMCControl> lstDbMCControl)
{
List<DbMCControl> Result = new List<DbMCControl>();
DbMCControlDesignKeyConfig config = new DbMCControlDesignKeyConfig();
string[] dbInfo_control = config.SplitKey(key);
if (dbInfo_control.Length > 2)
{
key = config.ToString(dbInfo_control);
}
foreach (DbMCControl control in lstDbMCControl)
{
if (control.DbMCKey != null && control.DbMCKey.Equals(key, StringComparison.CurrentCultureIgnoreCase))
{
Result.Add(control);
}
}
return Result;
}
protected List<DbMCControl> GetThisFormDbMCControlByKey(string key)
{
return getDbMCControlByKey(key,GetThisFormAllDbMCControls());
}
protected List<DbMCControl> GetAllFormDbMCControlByKey(string key)
{
return getDbMCControlByKey(key, GetAllFormAllDbMCControls());
}
protected List<DbMCControl> GetThisOrAllFormDbMCControlByKey(string key)
{
List<DbMCControl> Result = GetThisFormDbMCControlByKey(key);
if (Result.Count > 0)
{
return Result;
}
return GetAllFormDbMCControlByKey(key);
}
public List<DbMCControl> GetDbMCControlByKey(string key)
{
return GetThisOrAllFormDbMCControlByKey(key);
}
#endregion
#region GetDbMCControlByKey
private List<DbMCControl> getDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType dbType, string key, List<DbMCControl> lstControl)
{
List<DbMCControl> Result = new List<DbMCControl>();
DbMCControlDesignKeyConfig config = new DbMCControlDesignKeyConfig();
string[] dbInfo_keys = config.SplitKey(key);
string[] dbInfo_control = new string[dbInfo_keys.Length + 1];
dbInfo_control[0] = Mesnac.Basic.DataSourceFactory.Instance.GetDataSourceName(dbType);
for (int i = 0; i < dbInfo_keys.Length; i++)
{
dbInfo_control[i + 1] = dbInfo_keys[i];
}
key = config.ToString(dbInfo_control);
foreach (DbMCControl control in lstControl)
{
if (control.DbMCKey != null && control.DbMCKey.Equals(key, StringComparison.CurrentCultureIgnoreCase))
{
Result.Add(control);
}
}
return Result;
}
protected List<DbMCControl> GetThisFomrDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType dbType, string key)
{
return getDbMCControlByKey(dbType,key,GetThisFormAllDbMCControls());
}
protected List<DbMCControl> GetAllFomrDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType dbType, string key)
{
return getDbMCControlByKey(dbType, key, GetAllFormAllDbMCControls());
}
protected List<DbMCControl> GetThisOrAllFomrDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType dbType, string key)
{
List<DbMCControl> Result = GetThisFomrDbMCControlByKey(dbType, key);
if (Result.Count > 0)
{
return Result;
}
return GetAllFomrDbMCControlByKey(dbType, key);
}
public List<DbMCControl> GetDbMCControlByKey(Mesnac.Basic.DataSourceFactory.MCDbType dbType, string key)
{
return GetThisOrAllFomrDbMCControlByKey(dbType, key);
}
#endregion
#region GetDbMCControlByBaseControlKey
private List<DbMCControl> getDbMCControlByBaseControlKey(string key, List<DbMCControl> lstDbMCControl)
{
List<DbMCControl> Result = new List<DbMCControl>();
foreach (DbMCControl control in lstDbMCControl)
{
if (control.BaseControl.MCKey != null && control.BaseControl.MCKey.Equals(key, StringComparison.CurrentCultureIgnoreCase))
{
Result.Add(control);
}
}
return Result;
}
protected List<DbMCControl> GetThisFormDbMCControlByBaseControlKey(string key)
{
return getDbMCControlByBaseControlKey(key,GetThisFormAllDbMCControls());
}
protected List<DbMCControl> GetAllFormDbMCControlByBaseControlKey(string key)
{
return getDbMCControlByBaseControlKey(key, GetAllFormAllDbMCControls());
}
protected List<DbMCControl> GetThisOrAllFormDbMCControlByBaseControlKey(string key)
{
List<DbMCControl> Result = GetThisFormDbMCControlByBaseControlKey(key);
if (Result.Count > 0)
{
return Result;
}
return GetAllFormDbMCControlByBaseControlKey(key);
}
public List<DbMCControl> GetDbMCControlByBaseControlKey(string key)
{
return GetThisFormDbMCControlByBaseControlKey(key);
}
public List<IBaseControl> GetComponentContral(string key)
{
List<IBaseControl> list = new List<IBaseControl>();
foreach (Component c in GetAllFormAllComponents())
{
if (c.Site.Name == key)
{
list.Add(c as IBaseControl);
}
}
return list;
}
#endregion
#region 数据库操作
#region 获取数据源组
/// <summary>
/// 获取数据源组
/// </summary>
/// <param name="dbType"></param>
/// <returns></returns>
public DataSourceItem GetDataSourceItem(Mesnac.Basic.DataSourceFactory.MCDbType dbType)
{
return DataSourceFactory.Instance.GetDataSourceItem(dbType);
}
#endregion
#region NewDbHelper
/// <summary>
/// 新建数据库操作类
/// </summary>
/// <param name="item"></param>
/// <returns></returns>
private DbHelper NewDbHelper(DataSourceItem item)
{
return DataSourceFactory.Instance.GetDbHelper(item);
}
public DbHelper NewDbHelper(string dbName)
{
return DataSourceFactory.Instance.GetDbHelper(dbName);
}
public DbHelper NewDbHelper(Mesnac.Basic.DataSourceFactory.MCDbType dbType)
{
return DataSourceFactory.Instance.GetDbHelper(dbType);
}
#endregion
#region 同步数据库
/// <summary>
/// 同步表
/// </summary>
public void SynchroTable()
{
foreach (DbMCSource dbsource in GetThisFormAllDbMCSources())
{
if (string.IsNullOrWhiteSpace(dbsource.DesignSource))
{
continue;
}
if (string.IsNullOrWhiteSpace(dbsource.DataTable.ToString()))
{
continue;
}
string source = dbsource.DesignSource;
string table = dbsource.DataTable;
DbHelper dbHelper = NewDbHelper(source);
if (dbHelper == null)
{
continue;
}
string sqlstr = "SELECT name FROM sysobjects WHERE name = N'" + table + "' AND type = 'U'";
if (dbHelper.DbSession.ProviderFactory.GetType().FullName.Contains("OracleClientFactory"))
{
sqlstr = "select TNAME as name from tab where TNAME='"+ table + "' and TABTYPE='TABLE'";
}
else if (dbHelper.DbSession.ProviderFactory.GetType().FullName.Contains("OleDbFactory"))
{
sqlstr = "SELECT MSysObjects.Name as name FROM MsysObjects WHERE (Left([Name],1)<>'~') AND (Left$([Name],4) <> 'Msys') AND (MSysObjects.Type)=1 and MSysObjects.Name='" + table + "'";
}
dbHelper.ClearParameter();
dbHelper.CommandText = sqlstr;
DataTable dt = dbHelper.ToDataTable();
if (dt.Rows.Count < 1)
{
//创建表
sqlstr = @"CREATE TABLE [" + table + @"](
[ObjID] INT IDENTITY(1,1) NOT NULL,
CONSTRAINT PK_ObjID_IN"
+ table + @"_IDENTITY PRIMARY KEY CLUSTERED([ObjID] ASC)
);";
if (dbHelper.DbSession.ProviderFactory.GetType().FullName.Contains("OracleClientFactory"))
{
sqlstr = @"CREATE SEQUENCE seq_{0};
CREATE TABLE {0}(ObjID INT primary key);
CREATE OR REPLACE TRIGGER tri_{0}_insert
before insert on userinfo
for each row
declare
begin
select seq_{0}.nextval into :NEW.ObjID from dual;
end tri_userInfo_insert;";
sqlstr = String.Format(sqlstr, table);
}
else if (dbHelper.DbSession.ProviderFactory.GetType().FullName.Contains("OleDbFactory"))
{
sqlstr = @"CREATE TABLE {0}(ObjID INT IDENTITY(1,1) primary key)";
sqlstr = String.Format(sqlstr, table);
}
dbHelper.CommandText = sqlstr;
dbHelper.ExecuteNonQuery();
}
}
}
/// <summary>
/// 同步字段
/// </summary>
public void SynchroField()
{
foreach (DbMCControl control in GetThisFormAllDbMCControls())
{
if ((control.BaseControl.IsDbControl)
&& (!string.IsNullOrWhiteSpace(control.DesignSource))
&& (!string.IsNullOrWhiteSpace(control.DataTable))
&& (!string.IsNullOrWhiteSpace(control.DataField))
)
{
DbHelper dbHelper = NewDbHelper(control.DesignSource);
if (dbHelper == null)
{
continue;
}
//查询表
string sqlstr = String.Empty;
sqlstr = "select * from [" + control.DataTable + "] where 1=2";
if (dbHelper.DbSession.ProviderFactory.GetType().FullName.Contains("OracleClientFactory"))
{
sqlstr = "select * from {0} where 1=2";
sqlstr = String.Format(sqlstr, control.DataTable);
}
dbHelper.ClearParameter();
dbHelper.CommandText = sqlstr;
DataTable dt = dbHelper.ToDataTable();
bool isExists = false;
foreach (DataColumn dc in dt.Columns)
{
if (control.DataField.Equals(dc.ColumnName, StringComparison.CurrentCultureIgnoreCase))
{
isExists = true;
break;
}
}
if (!isExists)
{
//创建字段
sqlstr = "ALTER TABLE [" + control.DataTable + "] ADD " + control.DataField + " ntext NULL";
if (dbHelper.DbSession.ProviderFactory.GetType().FullName.Contains("OracleClientFactory"))
{
sqlstr = "alter table {0} add {1} varchar2(500) null";
sqlstr = String.Format(sqlstr, control.DataTable, control.DataField);
}
dbHelper.CommandText = sqlstr;
dbHelper.ExecuteNonQuery();
}
}
}
}
#endregion
#region 执行存储过程的方法
/// <summary>
/// 获取存储过程执行器
/// </summary>
/// <param name="dbType">数据库类型,网络库、本地库</param>
/// <param name="procedureName">存储过程名称</param>
/// <param name="parameters">参数列表</param>
/// <returns>返回存储过程执行器</returns>
public DbHelper GetProcedureHelper(Mesnac.Basic.DataSourceFactory.MCDbType dbType, string procedureName, Dictionary<string, object> parameters)
{
if (!String.IsNullOrEmpty(procedureName))
{
DbHelper dbHelper;
dbHelper = this.NewDbHelper(dbType);
if (dbHelper == null)
{
base.LogError("获取数据连接失败...");
return null;
}
dbHelper.ClearParameter();
dbHelper.CommandType = CommandType.StoredProcedure;
dbHelper.CommandText = procedureName;
if (parameters != null && parameters.Keys.Count > 0)
{
foreach (string key in parameters.Keys)
{
dbHelper.AddParameter(key, parameters[key]);
}
}
return dbHelper;
}
return null;
}
/// <summary>
/// 执行存储过程
/// </summary>
/// <param name="dbType">数据库类型,网络库、本地库</param>
/// <param name="procedureName">存储过程名称</param>
/// <param name="parameters">参数列表</param>
public void ExecuteProcedure(Mesnac.Basic.DataSourceFactory.MCDbType dbType, string procedureName, Dictionary<string, object> parameters)
{
DbHelper dbHelper = this.GetProcedureHelper(dbType, procedureName, parameters);
if (dbHelper != null)
{
dbHelper.ExecuteNonQuery();
}
}
#endregion
#endregion
}
}