Browse Source

now has features: 1. can set/reset connection string for a connection, retrieve metadata, and save it, although it is not displayed in the UI. Beware! Metadata is currently unconstrained and consequently huge - a simple Oracle Express db is 67Mb of data in serialised xml form.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@1697 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Dickon Field 19 years ago
parent
commit
1f7f063e9f
  1. 2
      src/AddIns/Misc/SharpDbTools/Project/SharpDbTools.addin
  2. 12
      src/AddIns/Misc/SharpDbTools/Project/Src/Connection/DbProvidersService.cs
  3. 65
      src/AddIns/Misc/SharpDbTools/Project/Src/Model/DbModelInfo.cs
  4. 14
      src/AddIns/Misc/SharpDbTools/Project/Src/Model/DbModelInfoService.cs
  5. 44
      src/AddIns/Misc/SharpDbTools/Project/Src/ServerBrowserTool.cs

2
src/AddIns/Misc/SharpDbTools/Project/SharpDbTools.addin

@ -10,7 +10,7 @@ @@ -10,7 +10,7 @@
<Path name = "/SharpDevelop/Workbench/Pads">
<Pad id = "SharpDbToolsPad"
category = "Main"
title = "Server Tool"
title = "Server Explorer"
icon = "PadIcons.Output"
shortcut = "Control|Alt|D"
class = "SharpDbTools.ServerBrowserTool"/>

12
src/AddIns/Misc/SharpDbTools/Project/Src/Connection/DbProvidersService.cs

@ -32,6 +32,7 @@ namespace SharpDbTools.Connection @@ -32,6 +32,7 @@ namespace SharpDbTools.Connection
private static DbProvidersService me = new DbProvidersService();
private static Boolean initialized = false;
private Dictionary<string, DbProviderFactory> factories = new Dictionary<string, DbProviderFactory>();
private Dictionary<string, DbProviderFactory> factoriesByInvariantName = new Dictionary<string, DbProviderFactory>();
// This is only valid witin one session - do not persist
private Dictionary<string, string> invariantByNameLookup = new Dictionary<string, string>();
@ -44,13 +45,12 @@ namespace SharpDbTools.Connection @@ -44,13 +45,12 @@ namespace SharpDbTools.Connection
private void Initialize()
{
// get a complete list of config data for DbProviderFactories, indexed by name
DataTable providerFactoriesTable = DbProviderFactories.GetFactoryClasses();
DataRow[] rows = providerFactoriesTable.Select();
foreach(DataRow row in rows)
{
// TODO: factory out string literals for column names
// TODO: factor out string literals for column names
string name = (string)row["Name"];
string invariantName = (string)row["InvariantName"];
invariantByNameLookup.Add(name, invariantName);
@ -58,6 +58,7 @@ namespace SharpDbTools.Connection @@ -58,6 +58,7 @@ namespace SharpDbTools.Connection
DbProviderFactory factory = DbProviderFactories.GetFactory(row);
names.Add(name);
factories.Add(name, factory);
factoriesByInvariantName.Add(invariantName, factory);
}
initialized = true;
@ -97,6 +98,13 @@ namespace SharpDbTools.Connection @@ -97,6 +98,13 @@ namespace SharpDbTools.Connection
}
}
public DbProviderFactory GetFactoryByInvariantName(string invariantName)
{
DbProviderFactory factory = null;
this.factoriesByInvariantName.TryGetValue(invariantName, out factory);
return factory;
}
public static DbProvidersService GetDbProvidersService()
{
lock(me)

65
src/AddIns/Misc/SharpDbTools/Project/Src/Model/DbModelInfo.cs

@ -34,6 +34,24 @@ namespace SharpDbTools.Model @@ -34,6 +34,24 @@ namespace SharpDbTools.Model
{
public const string METADATACOLLECTIONS = "MetaDataCollections";
public DbModelInfo() : base()
{
}
public DbModelInfo(string name) : base()
{
DataTable table = CreateConnectionInfoTable();
// add the first and only column of this table;
table.Rows.Add(new object[] {name, null, null});
}
public DbModelInfo(string name, string invariantName, string connectionString): base()
{
DataTable table = CreateConnectionInfoTable();
// add the first and only column of this table;
table.Rows.Add(new object[] {name, invariantName, connectionString});
}
public string Name {
get {
DataTable table = this.Tables[TableNames.ConnectionInfo];
@ -61,8 +79,8 @@ namespace SharpDbTools.Model @@ -61,8 +79,8 @@ namespace SharpDbTools.Model
// if invariant has changed must clear any existing metadata
else if (!(invariantName.Equals(value))) {
// clear tables
this.Tables.Clear();
DataTable newTable = CreateConnectionTable();
this.Clear();
DataTable newTable = CreateConnectionInfoTable();
// add the first and only column of this table;
newTable.Rows.Add(new object[] {name, invariantName, connectionString});
}
@ -77,41 +95,37 @@ namespace SharpDbTools.Model @@ -77,41 +95,37 @@ namespace SharpDbTools.Model
}
set {
DataTable table = this.Tables[TableNames.ConnectionInfo];
string invariantName = table.Rows[0][ColumnNames.InvariantName] as string;
string name = this.Name;
string connectionString = this.ConnectionString;
if (connectionString == null) {
table.Rows[0][ColumnNames.ConnectionString] = value;
}
else if (!(connectionString.Equals(value))) {
this.Tables.Clear();
DataTable newTable = CreateConnectionTable();
string invariantName = this.InvariantName;
string name = this.Name;
this.Clear();
// add the first and only column of this table;
newTable.Rows.Add(new object[] {name, invariantName, connectionString});
table.Rows.Add(new object[] {name, invariantName, value});
}
}
}
public DbModelInfo() : base()
public void SetConnectionInfo(string invariantName, string connectionString)
{
string name = this.Name;
SetConnectionInfo(name, invariantName, connectionString);
}
public DbModelInfo(string name) : base()
public void SetConnectionInfo(string name, string invariantName,
string connectionString)
{
DataTable table = CreateConnectionTable();
this.Clear();
DataTable table = CreateConnectionInfoTable();
// add the first and only column of this table;
table.Rows.Add(new object[] {name, null, null});
}
public DbModelInfo(string name, string invariantName, string connectionString): base()
{
DataTable table = CreateConnectionTable();
// add the first and only column of this table;
table.Rows.Add(new object[] {name, invariantName, connectionString});
table.Rows.Add(new object[] {name, invariantName, connectionString});
}
private DataTable CreateConnectionTable()
private DataTable CreateConnectionInfoTable()
{
// create a table in the DbModelInfo to hold this initial info.
// this creates a consistent representation of the data and makes
@ -123,6 +137,17 @@ namespace SharpDbTools.Model @@ -123,6 +137,17 @@ namespace SharpDbTools.Model
return table;
}
public void ClearMetaData()
{
DataTable metadataCollectionsTable = this.MetaDataCollections;
if (metadataCollectionsTable != null) {
foreach (DataRow collectionRow in metadataCollectionsTable.Rows) {
String collectionName = (string)collectionRow[0];
this.Tables.Remove(collectionName);
}
}
}
public DataTable MetaDataCollections {
get {
return this.Tables[METADATACOLLECTIONS];

14
src/AddIns/Misc/SharpDbTools/Project/Src/Model/DbModelInfoService.cs

@ -90,20 +90,26 @@ namespace SharpDbTools.Model @@ -90,20 +90,26 @@ namespace SharpDbTools.Model
string connectionString = modelInfo.ConnectionString;
// get a connection - wait until a connection has been successfully made
// until clearing the DbModelInfo
// before clearing the DbModelInfo
DbProvidersService factoryService = DbProvidersService.GetDbProvidersService();
DbProviderFactory factory = factoryService[invariantName];
DbProviderFactory factory = factoryService.GetFactoryByInvariantName(invariantName);
DbConnection connection = factory.CreateConnection();
// TODO: clear the DbModelInfo prior to refreshing from the connection
modelInfo.ClearMetaData();
// reload the metadata from the connection
// get the Schema table
connection.ConnectionString = connectionString;
connection.Open();
DataTable schemaInfo = connection.GetSchema();
connection.Close();
// clear the DbModelInfo prior to refreshing from the connection
modelInfo.ClearMetaData();
// iterate through the rows in it - the first column of each is a
// schema info collection name that can be retrieved as a DbTable
// Add each one to the DbModel DataSet

44
src/AddIns/Misc/SharpDbTools/Project/Src/ServerBrowserTool.cs

@ -13,6 +13,7 @@ @@ -13,6 +13,7 @@
using System;
using System.Windows.Forms;
using System.Data.Common;
using ICSharpCode.Core;
using ICSharpCode.SharpDevelop.Gui;
using SharpDbTools.Model;
@ -136,13 +137,22 @@ namespace SharpDbTools @@ -136,13 +137,22 @@ namespace SharpDbTools
{
TreeNode treeNode = new TreeNode(name);
// create and add the menustrip for this node
DbModelInfoContextMenuStrip cMenu = new DbModelInfoContextMenuStrip(treeNode);
// create menu items
ToolStripMenuItem setConnectionStringMenuItem =
new ToolStripMenuItem("Set Connection String");
setConnectionStringMenuItem.Click += new EventHandler(SetConnectionStringOnDbModelInfoClickHandler);
ToolStripMenuItem loadMetadataMenuItem =
new ToolStripMenuItem("Load Metadata from Connection");
loadMetadataMenuItem.Click += new EventHandler(LoadMetadataClickHandler);
cMenu.Items.AddRange(new ToolStripMenuItem[]
{
setConnectionStringMenuItem
setConnectionStringMenuItem,
loadMetadataMenuItem
});
treeNode.ContextMenuStrip = cMenu;
@ -207,14 +217,19 @@ namespace SharpDbTools @@ -207,14 +217,19 @@ namespace SharpDbTools
DbModelInfoService.SaveAll();
}
public void SetConnectionStringOnDbModelInfoClickHandler(object sender, EventArgs e)
private static string getConnectionName(object sender)
{
ToolStripMenuItem menuItem = sender as ToolStripMenuItem;
DbModelInfoContextMenuStrip toolStrip = menuItem.Owner as DbModelInfoContextMenuStrip;
TreeNode node = toolStrip.TreeNode;
string connectionLogicalName = node.Text;
LoggingService.Debug("add connection string clicked for item with name: " + node.Text);
return connectionLogicalName;
}
public void SetConnectionStringOnDbModelInfoClickHandler(object sender, EventArgs e)
{
string connectionLogicalName = getConnectionName(sender);
LoggingService.Debug("add connection string clicked for item with name: " + connectionLogicalName);
// use the ConnectionStringDefinitionDialog to get a connection string and invariant name
ConnectionStringDefinitionDialog definitionDialog = new ConnectionStringDefinitionDialog();
@ -237,9 +252,24 @@ namespace SharpDbTools @@ -237,9 +252,24 @@ namespace SharpDbTools
return;
}
if ((connectionString == null) || (!((newConnectionString.Equals(connectionString))))) {
dbModelInfo.ConnectionString = newConnectionString;
dbModelInfo.InvariantName = definitionDialog.InvariantName;
dbModelInfo.ConnectionString = newConnectionString;
dbModelInfo.InvariantName = definitionDialog.InvariantName;
// rebuild the database explorer node
this.BeginInvoke(new MethodInvoker(this.RebuildDbConnectionsNode));
}
public void LoadMetadataClickHandler(object sender, EventArgs args)
{
string connectionLogicalName = getConnectionName(sender);
LoggingService.Debug("load metadata from connection clicked for connection with name: "
+ connectionLogicalName);
try {
DbModelInfoService.LoadMetadataFromConnection(connectionLogicalName);
}
catch(DbException e) {
MessageService.ShowError(e,
"An Exception was thrown while trying to connect to: " + connectionLogicalName);
}
}
}

Loading…
Cancel
Save