|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using Mesnac.Basic;
|
|
|
using System.Text;
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
using ICSharpCode.Data.Core;
|
|
|
using ICSharpCode.Data.Core.Common;
|
|
|
using ICSharpCode.Data.Core.DatabaseDrivers.SQLServer;
|
|
|
using ICSharpCode.Data.Core.Interfaces;
|
|
|
using ICSharpCode.Data.Core.DatabaseObjects;
|
|
|
using ICSharpCode.Data.Core.Enums;
|
|
|
using ICSharpCode.Data.Core.Interfaces;
|
|
|
|
|
|
namespace Mesnac.Gui.Edit.Common
|
|
|
{
|
|
|
public delegate void CallBackDelegate(); //回调方法
|
|
|
/// <summary>
|
|
|
/// 数据源工厂
|
|
|
/// </summary>
|
|
|
public class DataSourceFactory
|
|
|
{
|
|
|
private static DataSourceFactory instance = null; //保存工厂实例
|
|
|
private Dictionary<string, DataSourceItem> _dataSources = null; //保存数据源
|
|
|
private TreeNode root = null;
|
|
|
public event EventHandler DataSourceRefresh;
|
|
|
|
|
|
private DataSourceFactory()
|
|
|
{
|
|
|
this.root = new TreeNode();
|
|
|
root.Text = "数据源列表";
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 工厂实例
|
|
|
/// </summary>
|
|
|
public static DataSourceFactory Instance
|
|
|
{
|
|
|
get
|
|
|
{
|
|
|
if (instance == null)
|
|
|
{
|
|
|
instance = new DataSourceFactory();
|
|
|
}
|
|
|
return instance;
|
|
|
}
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 把内存中的数据源数据保存只文件
|
|
|
/// </summary>
|
|
|
/// <param name="fileName">要保存数据源信息的文件名</param>
|
|
|
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<string, DataSourceItem>();
|
|
|
}
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 把文件中的内容读取至内存
|
|
|
/// </summary>
|
|
|
/// <param name="fileName">保存数据源信息的文件名</param>
|
|
|
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(); //异步生成数据源树
|
|
|
}
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 数据源信息
|
|
|
/// </summary>
|
|
|
public Dictionary<string, DataSourceItem> DataSources
|
|
|
{
|
|
|
get
|
|
|
{
|
|
|
return this._dataSources;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 数据源树
|
|
|
/// </summary>
|
|
|
public TreeNode Root
|
|
|
{
|
|
|
get { return this.root; }
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
/// 生成数据源树
|
|
|
/// </summary>
|
|
|
public void GenerateDataSourceTree()
|
|
|
{
|
|
|
lock (this)
|
|
|
{
|
|
|
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;
|
|
|
//string driverAssembly = dataSourceItem.DriverAssembly;
|
|
|
//string driverClass = dataSourceItem.DriverClass;
|
|
|
#region 数据源处理
|
|
|
switch (dataSourceItem.Driver)
|
|
|
{
|
|
|
case "MS SQL Server":
|
|
|
SQLServerDatabaseDriver driver = new SQLServerDatabaseDriver();
|
|
|
SQLServerDatasource ds = new SQLServerDatasource(driver);
|
|
|
ds.Name = dataSourceItem.Server;
|
|
|
ds.UserId = dataSourceItem.UserName;
|
|
|
ds.Password = dataSourceItem.Password;
|
|
|
driver.PopulateDatabases(ds);
|
|
|
IDatabase database = 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;
|
|
|
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;
|
|
|
nodeTable.Nodes.Add(nodeColumn);
|
|
|
}
|
|
|
nodeViews.Nodes.Add(nodeView);
|
|
|
}
|
|
|
|
|
|
TreeNode nodeProcedures = new TreeNode();
|
|
|
nodeProcedures.Text = "存储过程";
|
|
|
TreeNode nodeProcedure = null;
|
|
|
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);
|
|
|
}
|
|
|
break;
|
|
|
default:
|
|
|
break;
|
|
|
}
|
|
|
#endregion
|
|
|
|
|
|
cnt++;
|
|
|
if (cnt > this._dataSources.Values.Count) break;
|
|
|
|
|
|
this.root.Nodes.Add(nodeDataSource);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 添加数据源
|
|
|
/// </summary>
|
|
|
/// <param name="name"></param>
|
|
|
/// <param name="item"></param>
|
|
|
public void Add(string name, DataSourceItem item)
|
|
|
{
|
|
|
if (this._dataSources == null)
|
|
|
{
|
|
|
this._dataSources = new Dictionary<string, DataSourceItem>();
|
|
|
}
|
|
|
if (!this.IsExists(name))
|
|
|
{
|
|
|
this._dataSources.Add(name, item);
|
|
|
//触发事件
|
|
|
if (DataSourceRefresh != null)
|
|
|
{
|
|
|
DataSourceRefresh(this, EventArgs.Empty);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 移除数据源
|
|
|
/// </summary>
|
|
|
/// <param name="name"></param>
|
|
|
public void Remove(string name)
|
|
|
{
|
|
|
if (this.IsExists(name))
|
|
|
{
|
|
|
this._dataSources.Remove(name);
|
|
|
//触发事件
|
|
|
if (DataSourceRefresh != null)
|
|
|
{
|
|
|
DataSourceRefresh(this, EventArgs.Empty);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 修改数据源
|
|
|
/// </summary>
|
|
|
/// <param name="name"></param>
|
|
|
/// <param name="newItem"></param>
|
|
|
public void Modify(string name, DataSourceItem newItem)
|
|
|
{
|
|
|
if (this._dataSources == null)
|
|
|
{
|
|
|
this._dataSources = new Dictionary<string, DataSourceItem>();
|
|
|
}
|
|
|
if (this.IsExists(name))
|
|
|
{
|
|
|
this._dataSources[name] = newItem;
|
|
|
//触发事件
|
|
|
if (DataSourceRefresh != null)
|
|
|
{
|
|
|
DataSourceRefresh(this, EventArgs.Empty);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 获取下一个可用的数据源名称
|
|
|
/// </summary>
|
|
|
/// <returns></returns>
|
|
|
public string GetNextDataSourceName()
|
|
|
{
|
|
|
int i = 1;
|
|
|
if (this._dataSources == null)
|
|
|
{
|
|
|
return "DataSource1";
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
while (this._dataSources.ContainsKey("DataSource" + i))
|
|
|
{
|
|
|
i++;
|
|
|
}
|
|
|
}
|
|
|
return "DataSource" + i;
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 判断指定名称的数据源是否存在
|
|
|
/// </summary>
|
|
|
/// <param name="dataSourceName">要判断的数据源名称</param>
|
|
|
/// <returns>存在返回true,否则返回false</returns>
|
|
|
public bool IsExists(string dataSourceName)
|
|
|
{
|
|
|
if (this._dataSources == null)
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
if (this._dataSources.ContainsKey(dataSourceName))
|
|
|
{
|
|
|
return true;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|