diff --git a/src/AddIns/Misc/SharpServerTools/SharpDbTools/SharpDbTools.csproj b/src/AddIns/Misc/SharpServerTools/SharpDbTools/SharpDbTools.csproj index d3a19e0234..fe9ce0daf6 100644 --- a/src/AddIns/Misc/SharpServerTools/SharpDbTools/SharpDbTools.csproj +++ b/src/AddIns/Misc/SharpServerTools/SharpDbTools/SharpDbTools.csproj @@ -57,6 +57,7 @@ + diff --git a/src/AddIns/Misc/SharpServerTools/SharpDbTools/Src/Forms/SQL.xshd b/src/AddIns/Misc/SharpServerTools/SharpDbTools/Src/Forms/SQL.xshd new file mode 100644 index 0000000000..d5d303b968 --- /dev/null +++ b/src/AddIns/Misc/SharpServerTools/SharpDbTools/Src/Forms/SQL.xshd @@ -0,0 +1,165 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + =!><+-/*%&|^~.}{,;][?:() + + + -- + + + + /* + */ + + + + " + " + + + + ' + ' + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/AddIns/Misc/SharpServerTools/SharpDbTools/Src/Forms/SQLTool.Designer.cs b/src/AddIns/Misc/SharpServerTools/SharpDbTools/Src/Forms/SQLTool.Designer.cs index 1b23f95836..4fdd59bf63 100644 --- a/src/AddIns/Misc/SharpServerTools/SharpDbTools/Src/Forms/SQLTool.Designer.cs +++ b/src/AddIns/Misc/SharpServerTools/SharpDbTools/Src/Forms/SQLTool.Designer.cs @@ -37,13 +37,13 @@ namespace SharpDbTools.Forms this.sqlToolTabControl = new System.Windows.Forms.TabControl(); this.editorTab = new System.Windows.Forms.TabPage(); this.resultTab = new System.Windows.Forms.TabPage(); - this.messageTab = new System.Windows.Forms.TabPage(); this.resultDataGridView = new System.Windows.Forms.DataGridView(); + this.messageTab = new System.Windows.Forms.TabPage(); this.messageTextBox = new System.Windows.Forms.TextBox(); this.sqlToolTabControl.SuspendLayout(); this.resultTab.SuspendLayout(); - this.messageTab.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.resultDataGridView)).BeginInit(); + this.messageTab.SuspendLayout(); this.SuspendLayout(); // // sqlToolTabControl @@ -57,7 +57,6 @@ namespace SharpDbTools.Forms this.sqlToolTabControl.SelectedIndex = 0; this.sqlToolTabControl.Size = new System.Drawing.Size(885, 522); this.sqlToolTabControl.TabIndex = 0; - this.sqlToolTabControl.UseWaitCursor = true; // // editorTab // @@ -68,7 +67,6 @@ namespace SharpDbTools.Forms this.editorTab.TabIndex = 0; this.editorTab.Text = "Editor"; this.editorTab.UseVisualStyleBackColor = true; - this.editorTab.UseWaitCursor = true; // // resultTab // @@ -80,7 +78,15 @@ namespace SharpDbTools.Forms this.resultTab.TabIndex = 1; this.resultTab.Text = "Results"; this.resultTab.UseVisualStyleBackColor = true; - this.resultTab.UseWaitCursor = true; + // + // resultDataGridView + // + this.resultDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; + this.resultDataGridView.Dock = System.Windows.Forms.DockStyle.Fill; + this.resultDataGridView.Location = new System.Drawing.Point(3, 3); + this.resultDataGridView.Name = "resultDataGridView"; + this.resultDataGridView.Size = new System.Drawing.Size(871, 488); + this.resultDataGridView.TabIndex = 0; // // messageTab // @@ -92,23 +98,13 @@ namespace SharpDbTools.Forms this.messageTab.TabIndex = 2; this.messageTab.Text = "Messages"; this.messageTab.UseVisualStyleBackColor = true; - this.messageTab.UseWaitCursor = true; // - // dataGridView1 - // - this.resultDataGridView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; - this.resultDataGridView.Dock = System.Windows.Forms.DockStyle.Fill; - this.resultDataGridView.Location = new System.Drawing.Point(3, 3); - this.resultDataGridView.Name = "resultDataGridView"; - this.resultDataGridView.Size = new System.Drawing.Size(871, 488); - this.resultDataGridView.TabIndex = 0; - // - // textBox1 + // messageTextBox // this.messageTextBox.Dock = System.Windows.Forms.DockStyle.Fill; this.messageTextBox.Location = new System.Drawing.Point(3, 3); this.messageTextBox.Multiline = true; - this.messageTextBox.Name = "messages"; + this.messageTextBox.Name = "messageTextBox"; this.messageTextBox.Size = new System.Drawing.Size(871, 488); this.messageTextBox.TabIndex = 0; // @@ -121,9 +117,9 @@ namespace SharpDbTools.Forms this.Size = new System.Drawing.Size(885, 522); this.sqlToolTabControl.ResumeLayout(false); this.resultTab.ResumeLayout(false); + ((System.ComponentModel.ISupportInitialize)(this.resultDataGridView)).EndInit(); this.messageTab.ResumeLayout(false); this.messageTab.PerformLayout(); - ((System.ComponentModel.ISupportInitialize)(this.resultDataGridView)).EndInit(); this.ResumeLayout(false); } private System.Windows.Forms.TextBox messageTextBox; diff --git a/src/AddIns/Misc/SharpServerTools/SharpDbTools/Src/Forms/SQLTool.cs b/src/AddIns/Misc/SharpServerTools/SharpDbTools/Src/Forms/SQLTool.cs index b01b093929..d1c2382ecc 100644 --- a/src/AddIns/Misc/SharpServerTools/SharpDbTools/Src/Forms/SQLTool.cs +++ b/src/AddIns/Misc/SharpServerTools/SharpDbTools/Src/Forms/SQLTool.cs @@ -1,5 +1,6 @@ /* * User: dickon + * Contributions from: troy@ebswift.com * Date: 21/11/2006 * Time: 19:12 * @@ -9,10 +10,15 @@ using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; +using System.IO; +using System.Data.Common; +using System.Data; using ICSharpCode.TextEditor; using ICSharpCode.Core; +using SharpDbTools.Data; + namespace SharpDbTools.Forms { /// @@ -23,6 +29,8 @@ namespace SharpDbTools.Forms { private string logicalConnectionName = null; private TextEditorControl sqlEditorControl = null; + private BackgroundWorker backgroundWorker; + private string lastSQL; public SQLTool(string logicalConnectionName) { @@ -37,33 +45,128 @@ namespace SharpDbTools.Forms sqlEditorControl = new TextEditorControl(); sqlEditorControl.Dock = DockStyle.Fill; - sqlEditorControl.SetHighlighting("SQL"); + //sqlEditorControl.SetHighlighting("SQL"); + + string appPath = Path.GetDirectoryName(Application.ExecutablePath); + ICSharpCode.TextEditor.Document.FileSyntaxModeProvider provider = new ICSharpCode.TextEditor.Document.FileSyntaxModeProvider(appPath); + ICSharpCode.TextEditor.Document.HighlightingManager.Manager.AddSyntaxModeFileProvider(provider); + sqlEditorControl.Document.HighlightingStrategy = + ICSharpCode.TextEditor.Document.HighlightingManager.Manager.FindHighlighter("SQL"); this.editorTab.Controls.Add(sqlEditorControl); // add context behaviour to the editor control ContextMenuStrip contextMenu = new ContextMenuStrip(); - ToolStripMenuItem executeSQLMenuItem = new ToolStripMenuItem("Execute SQL"); - executeSQLMenuItem.Click += new EventHandler(ExecuteSQLClickHandler); + ToolStripMenuItem runSQLMenuItem = new ToolStripMenuItem("Run SQL"); + runSQLMenuItem.Click += new EventHandler(RunSQLClickHandler); contextMenu.Items.AddRange(new ToolStripMenuItem[] { - executeSQLMenuItem + runSQLMenuItem }); sqlEditorControl.ContextMenuStrip = contextMenu; } - private void ExecuteSQLClickHandler(object sender, EventArgs args) + private void RunSQLClickHandler(object sender, EventArgs args) { // 1. get a connection from the the logical connection name // 2. attempt to execute any SQL currently contained in the editor // 3. display either a result set in the result DataGridView, or // messages in the messages textbox in the message tab. - LoggingService.Debug(this.GetType().Name + "-> ExecuteSQLClickHandler"); + LoggingService.Debug(this.GetType().Name + "-> RunSQLClickHandler"); + this.lastSQL = this.sqlEditorControl.Document.TextContent; + this.backgroundWorker = new BackgroundWorker(); + backgroundWorker.DoWork += DispatchSQL; + backgroundWorker.RunWorkerCompleted += DispatchSQLComplete; + backgroundWorker.RunWorkerAsync(); // TODO: hand off the execution of the query to a background thread... } + + private void DispatchSQL(object sender, DoWorkEventArgs e) + { + // use the logical connection name to map to the invariant name + // in the DbModelInfoService + DbModelInfo modelInfo = DbModelInfoService.GetDbModelInfo(this.logicalConnectionName); + string invariantName = modelInfo.InvariantName; + + // use the invariant name to get the DbProviderFactory from the DBProvidersService + DbProvidersService dbProvidersService = DbProvidersService.GetDbProvidersService(); + DbProviderFactory factory = dbProvidersService.GetFactoryByInvariantName(invariantName); + + // get a connection from the DbProviderFactory + DbConnection connection = factory.CreateConnection(); + + // use the logical connection name to map to the connection string + // for this connection in the DbModelInfoService + string connectionString = modelInfo.ConnectionString; + connection.ConnectionString = connectionString; + + + try { + // dispatch the sql on this connection + // if result is successful invoke an update to the DataGridView of + // SQLTool + connection.Open(); + DbCommand command = connection.CreateCommand(); + LoggingService.Debug("getting sql command"); + command.CommandText = this.lastSQL; + LoggingService.Debug("dispatching sql: " + command.CommandText); + DbDataReader reader = command.ExecuteReader(); + LoggingService.Debug("received ResultSet, showing in SQLTool..."); + this.SetDataGridViewContent(reader); + } + catch(Exception ex) { + // if the result is unsuccessful invoke an update to the message + // view of SQLTool hopefully with the reason for the failure + string msg = "caught exception: " + ex.GetType().Name + + ": " + ex.Message; + LoggingService.Debug(msg); + LoggingService.Debug(ex.StackTrace); + this.AppendMessageContent(msg); + } + finally { + connection.Close(); + connection.Dispose(); + } + } + + delegate void AppendMessageContentCallback(string msg); + + private void AppendMessageContent(string msg) + { + if (this.messageTextBox.InvokeRequired) { + AppendMessageContentCallback c = new AppendMessageContentCallback(AppendMessageContent); + this.Invoke(c, new object[] { msg }); + } else { + this.messageTextBox.AppendText(msg); + this.sqlToolTabControl.SelectTab(this.messageTab); + } + } + + delegate void SetDataGridViewContentCallback(DbDataReader reader); + + private void SetDataGridViewContent(DbDataReader reader) + { + if (this.resultDataGridView.InvokeRequired) { + SetDataGridViewContentCallback c = new SetDataGridViewContentCallback(SetDataGridViewContent); + this.Invoke(c, new object[] { reader }); + } else { + string tableName = reader.GetSchemaTable().TableName; + this.resultDataGridView.ClearSelection(); + DataTable data = new DataTable(); + data.BeginInit(); + data.Load(reader); + data.EndInit(); + this.resultDataGridView.DataSource = data; + this.sqlToolTabControl.SelectTab(this.resultTab); + } + } + + public void DispatchSQLComplete(object sender, RunWorkerCompletedEventArgs args) + { + } } }