using System; using System.Collections.Generic; using Mesnac.Basic; using System.Text; using System.IO; using System.Xml; using System.Windows.Forms; using Mesnac.Codd.Session; using System.Data.SqlClient; using ICSharpCode.Data.Core; using ICSharpCode.Data.Core.Common; using ICSharpCode.Data.Core.Interfaces; using ICSharpCode.Data.Core.DatabaseObjects; using ICSharpCode.Data.Core.Enums; namespace Mesnac.Basic { public delegate void CallBackDelegate(); //回调方法 /// /// 数据源工厂 /// public class DataSourceFactory { private static DataSourceFactory instance = null; //保存工厂实例 private Dictionary _dataSources = null; //保存数据源 private TreeNode root = null; public event EventHandler DataSourceRefresh; private DataSourceFactory() { this.root = new TreeNode(); root.Text = "数据源列表"; } /// /// 工厂实例 /// public static DataSourceFactory Instance { get { if (instance == null) { instance = new DataSourceFactory(); } return instance; } } /// /// 把内存中的数据源数据保存只文件 /// /// 要保存数据源信息的文件名 public void RefreshDataFromFile(string fileName, params CallBackDelegate[] callBack) { if (System.IO.File.Exists(fileName)) { this._dataSources = XmlHandler.ParseFromDataSourceXml(fileName); //触发事件 if (DataSourceRefresh != null) { DataSourceRefresh(this, EventArgs.Empty); } new System.Threading.Thread(new System.Threading.ThreadStart(delegate() { this.GenerateDataSourceTree(); foreach (CallBackDelegate call in callBack) { call(); } })).Start(); //异步生成数据源树 } else { this._dataSources = new Dictionary(); } } /// /// 把文件中的内容读取至内存 /// /// 保存数据源信息的文件名 public void RefreshDataToFile(string fileName, params CallBackDelegate[] callBack) { if (this._dataSources != null) { XmlHandler.GenerateDataSourceXml(this._dataSources, fileName); new System.Threading.Thread(new System.Threading.ThreadStart(delegate() { this.GenerateDataSourceTree(); foreach (CallBackDelegate call in callBack) { call(); } })).Start(); //异步生成数据源树 } } /// /// 数据源信息 /// public Dictionary DataSources { get { return this._dataSources; } } /// /// 数据源树 /// public TreeNode Root { get { return this.root; } } /// /// 生成数据源树 /// public void GenerateDataSourceTree() { lock (this) { try { this.root.Nodes.Clear(); this.root = new TreeNode(); this.root.Text = "数据源列表"; if (this._dataSources != null) { TreeNode nodeDataSource = null; int cnt = 0; foreach (DataSourceItem dataSourceItem in this._dataSources.Values) { nodeDataSource = new TreeNode(); nodeDataSource.Text = dataSourceItem.Name; nodeDataSource.ToolTipText = dataSourceItem.Driver; #region 数据源处理 string driverAssembly = dataSourceItem.DriverAssembly;//.Split( new char[] { ',' } )[ 0 ]; string driverClass = dataSourceItem.DriverClass; System.Runtime.Remoting.ObjectHandle obj = Activator.CreateInstance(driverAssembly, driverClass) as System.Runtime.Remoting.ObjectHandle; IDatabaseDriver driver = obj.Unwrap() as IDatabaseDriver; IDatasource ds = driver.CreateNewIDatasource(dataSourceItem.Server, dataSourceItem.UserName, dataSourceItem.Password); driver.PopulateDatabases(ds); IDatabase database = null; if (ds.Databases != null) { foreach (IDatabase db in ds.Databases) { if (db.ToString() == dataSourceItem.Database) { database = db; break; } } if (database != null) { database.LoadDatabase(); TreeNode nodeTables = new TreeNode(); nodeTables.Text = "数据表"; TreeNode nodeTable = null; foreach (ITable table in database.Tables) { nodeTable = new TreeNode(); nodeTable.Text = table.TableName; TreeNode nodeColumn = null; foreach (IColumn col in table.Items) { nodeColumn = new TreeNode(); nodeColumn.Text = col.Name; nodeTable.Nodes.Add(nodeColumn); } nodeTables.Nodes.Add(nodeTable); } TreeNode nodeViews = new TreeNode(); nodeViews.Text = "数据视图"; TreeNode nodeView = null; if (database.Views != null && database.Views.Count > 0) { foreach (IView view in database.Views) { nodeView = new TreeNode(); nodeView.Text = view.Name; TreeNode nodeColumn = null; foreach (IColumn col in view.Items) { nodeColumn = new TreeNode(); nodeColumn.Text = col.Name; nodeView.Nodes.Add(nodeColumn); } nodeViews.Nodes.Add(nodeView); } } TreeNode nodeProcedures = new TreeNode(); nodeProcedures.Text = "存储过程"; TreeNode nodeProcedure = null; if (database.Procedures != null && database.Procedures.Count > 0) { foreach (IProcedure procedure in database.Procedures) { nodeProcedure = new TreeNode(); nodeProcedure.Text = procedure.Name; nodeProcedures.Nodes.Add(nodeProcedure); } } nodeDataSource.Nodes.Add(nodeTables); nodeDataSource.Nodes.Add(nodeViews); nodeDataSource.Nodes.Add(nodeProcedures); } } #endregion cnt++; if (cnt > this._dataSources.Values.Count) break; this.root.Nodes.Add(nodeDataSource); } } } catch (Exception ex) { ICSharpCode.Core.LoggingService.Error("获取数据源列表失败:" + ex.Message, ex); } } } /// /// 添加数据源 /// /// /// public void Add(string name, DataSourceItem item) { if (this._dataSources == null) { this._dataSources = new Dictionary(); } if (!this.IsExists(name)) { this._dataSources.Add(name, item); //触发事件 if (DataSourceRefresh != null) { DataSourceRefresh(this, EventArgs.Empty); } } } /// /// 移除数据源 /// /// public void Remove(string name) { if (this.IsExists(name)) { this._dataSources.Remove(name); //触发事件 if (DataSourceRefresh != null) { DataSourceRefresh(this, EventArgs.Empty); } } } /// /// 修改数据源 /// /// /// public void Modify(string name, DataSourceItem newItem) { if (this._dataSources == null) { this._dataSources = new Dictionary(); } if (this.IsExists(name)) { this._dataSources[name] = newItem; //触发事件 if (DataSourceRefresh != null) { DataSourceRefresh(this, EventArgs.Empty); } } } /// /// 获取下一个可用的数据源名称 /// /// public string GetNextDataSourceName() { int i = 1; if (this._dataSources == null) { return "DataSource1"; } else { while (this._dataSources.ContainsKey("DataSource" + i)) { i++; } } return "DataSource" + i; } /// /// 判断指定名称的数据源是否存在 /// /// 要判断的数据源名称 /// 存在返回true,否则返回false public bool IsExists(string dataSourceName) { if (this._dataSources == null) { return false; } else { if (this._dataSources.ContainsKey(dataSourceName)) { return true; } else { return false; } } } #region DbHelper public DbHelper GetDbHelper(DataSourceItem item) { if (item.Driver.Replace(" ", "").ToLower() == "mssqlserver") { string constr = "Persist Security Info=True;Password=" + item.Password + ";User ID=" + item.UserName + ";Initial Catalog=" + item.Database + ";Data Source=" + item.Server + ";Connection Timeout=" + item.ConnectionTimeout; DbSession dbsession = new DbSession(SqlClientFactory.Instance, constr); DbHelper Result = new DbHelper(dbsession); return Result; } else if (item.Driver.Replace(" ", "").ToLower() == "oracle") { string constr = "Persist Security Info=True;Password=" + item.Password + ";User ID=" + item.UserName + ";Data Source=" + item.Server; DbSession dbsession = new DbSession(Oracle.DataAccess.Client.OracleClientFactory.Instance, constr); DbHelper Result = new DbHelper(dbsession); return Result; } else if (item.Driver.Replace(" ", "").ToLower() == "mysql") { string constr = "Persist Security Info=True;Password=" + item.Password + ";User ID=" + item.UserName + ";Initial Catalog=" + item.Database + ";Data Source=" + item.Server; //DbSession dbsession = new DbSession(MySql.Data.MySqlClient.MySqlClientFactory.Instance, constr); DbSession dbsession = new DbSession(SqlClientFactory.Instance, constr); DbHelper Result = new DbHelper(dbsession); return Result; } else if (item.Driver.Replace(" ", "").ToLower() == "access") { string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + item.Server + ";User ID=" + item.UserName + ";Jet OLEDB:Database Password=" + item.Password; DbSession dbsession = new DbSession(System.Data.OleDb.OleDbFactory.Instance, constr); DbHelper Result = new DbHelper(dbsession); return Result; } else { string constr = "Persist Security Info=True;Password=" + item.Password + ";User ID=" + item.UserName + ";Initial Catalog=" + item.Database + ";Data Source=" + item.Server; //DbSession dbsession = new DbSession(MySql.Data.MySqlClient.MySqlClientFactory.Instance, constr); DbSession dbsession = new DbSession(SqlClientFactory.Instance, constr); DbHelper Result = new DbHelper(dbsession); return Result; } } public DbHelper GetDbHelper(string dbName) { DataSourceItem item; if (DataSourceFactory.Instance.DataSources != null && DataSourceFactory.Instance.DataSources.TryGetValue(dbName, out item)) { return GetDbHelper(item); } return null; } /// /// 获取数据源组 /// /// /// public DataSourceItem GetDataSourceItem(MCDbType dbType) { string dbName = this.GetDataSourceName(dbType); DataSourceItem item; if (DataSourceFactory.Instance.DataSources != null && DataSourceFactory.Instance.DataSources.TryGetValue(dbName, out item)) { return item; } return null; } public DbHelper GetDbHelper(MCDbType dbType) { string dbName = this.GetDataSourceName(dbType); return this.GetDbHelper(dbName); } #region GetConfigValue /// /// 数据库枚举类型 /// public enum MCDbType { /// /// 本地库 /// Local, /// /// 网络库 /// Server, /// /// 本地曲线库 /// Curve, /// /// 网络曲线库 /// CurveServer, /// /// 一次发本地库 /// LocalOne, /// /// 回放数据库 /// BackView } /// /// 根据数据枚举类型获取数据源名称的方法 /// /// 数据库枚举类型对象 /// 返回对应的数据源名称 public string GetDataSourceName(MCDbType dbtype) { switch (dbtype) { case MCDbType.Local: return RunSchema.Instance.GetConfigValue("DataSource.Local", "DataSource1"); case MCDbType.Server: return RunSchema.Instance.GetConfigValue("DataSource.Server", "DataSource2"); case MCDbType.Curve: return RunSchema.Instance.GetConfigValue("DataSource.Curve", "DataSource3"); case MCDbType.CurveServer: return RunSchema.Instance.GetConfigValue("DataSource.CurveServer", "DataSource4"); case MCDbType.LocalOne: return RunSchema.Instance.GetConfigValue("DataSource.LocalOne", "DataSource5"); case MCDbType.BackView: return RunSchema.Instance.GetConfigValue("DataSource.BackView", "DataSource1"); default: return RunSchema.Instance.GetConfigValue("DataSource.Local", "DataSource1"); } } #endregion #endregion } }