Browse Source

Now features a basic query tool - syntax highlighting (thanks to Troy for the template) using DbModelInfo framework for connection info. SQL parsing and field name completion not there yet.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@2125 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Dickon Field 19 years ago
parent
commit
521696851f
  1. 1
      src/AddIns/Misc/SharpServerTools/SharpDbTools/SharpDbTools.csproj
  2. 165
      src/AddIns/Misc/SharpServerTools/SharpDbTools/Src/Forms/SQL.xshd
  3. 32
      src/AddIns/Misc/SharpServerTools/SharpDbTools/Src/Forms/SQLTool.Designer.cs
  4. 115
      src/AddIns/Misc/SharpServerTools/SharpDbTools/Src/Forms/SQLTool.cs

1
src/AddIns/Misc/SharpServerTools/SharpDbTools/SharpDbTools.csproj

@ -57,6 +57,7 @@
<Compile Include="Src\Data\DbModelInfoService.cs" /> <Compile Include="Src\Data\DbModelInfoService.cs" />
<Compile Include="Src\Data\DbProvidersService.cs" /> <Compile Include="Src\Data\DbProvidersService.cs" />
<Compile Include="Src\Forms\TableTreeNode.cs" /> <Compile Include="Src\Forms\TableTreeNode.cs" />
<None Include="Src\Forms\SQL.xshd" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Src" /> <Folder Include="Src" />

165
src/AddIns/Misc/SharpServerTools/SharpDbTools/Src/Forms/SQL.xshd

@ -0,0 +1,165 @@
<?xml version = "1.0"?>
<!-- Thanks to Troy@ebswift.com -->
<SyntaxDefinition name = "SQL" extensions = ".sql">
<Environment>
<Default color = "SystemColors.WindowText" bgcolor="SystemColors.Window"/>
<VRuler color = "Blue"/>
<Selection bgcolor = "LightBlue"/>
<LineNumbers color = "Teal" bgcolor = "SystemColors.Window"/>
<InvalidLines color = "Red"/>
<EOLMarkers color = "White"/>
<SpaceMarkers color = "#E0E0E5"/>
<TabMarkers color = "#E0E0E5"/>
<CaretMarker color = "Yellow"/>
<FoldLine color = "#808080" bgcolor="Black"/>
<FoldMarker color = "#808080" bgcolor="White"/>
</Environment>
<Properties>
<Property name="LineComment" value="--"/>
</Properties>
<Digits name = "Digits" bold = "false" italic = "false" color = "DarkBlue"/>
<RuleSets>
<RuleSet ignorecase = "true">
<Delimiters>=!&gt;&lt;+-/*%&amp;|^~.}{,;][?:()</Delimiters>
<Span name ="LineComment" bold ="false" italic ="true" color ="DarkGreen" stopateol ="true">
<Begin>--</Begin>
</Span>
<Span name ="BlockComment" bold ="false" italic ="false" color ="DarkGreen" stopateol ="false">
<Begin>/*</Begin>
<End>*/</End>
</Span>
<Span name ="String" bold ="false" italic ="false" color ="Sienna" stopateol ="false">
<Begin>&quot;</Begin>
<End>&quot;</End>
</Span>
<Span name = "Character" bold = "false" italic = "false" color = "Sienna" stopateol = "true">
<Begin>&apos;</Begin>
<End>&apos;</End>
</Span>
<KeyWords name ="SqlKeywords" bold="false" italic = "false" color = "Blue">
<Key word = "from" />
<Key word = "where" />
<Key word = "in" />
<Key word = "on" />
<Key word = "exists" />
<Key word = "or" />
<Key word = "and" />
<Key word = "not" />
<Key word = "null" />
<Key word = "having" />
<Key word = "group" />
<Key word = "by" />
<Key word = "order" />
<Key word = "into" />
<Key word = "values" />
<Key word = "set" />
<Key word = "inner" />
<Key word = "outer" />
<Key word = "join" />
<Key word = "union" />
<Key word = "desc" />
<Key word = "asc" />
</KeyWords>
<KeyWords name ="SqlActionWords" bold="false" italic = "false" color = "Blue">
<Key word = "select" />
<Key word = "update" />
<Key word = "use" />
<Key word = "insert" />
<Key word = "create" />
<Key word = "drop" />
<Key word = "delete" />
<Key word = "truncate" />
<Key word = "alter" />
<Key word = "add" />
</KeyWords>
<KeyWords name ="SqlTypes" bold="false" italic = "false" color = "DarkRed">
<Key word = "varchar" />
<Key word = "nvarchar" />
<Key word = "char" />
<Key word = "nchar" />
<Key word = "int" />
<Key word = "text" />
<Key word = "ntext" />
<Key word = "double" />
<Key word = "money" />
<Key word = "bit" />
</KeyWords>
<KeyWords name ="SqlObjects" bold="false" italic = "false" color = "DarkRed">
<Key word = "table" />
<Key word = "proc" />
<Key word = "procedure" />
<Key word = "function" />
<Key word = "view" />
<Key word = "trigger" />
<Key word = "index" />
<Key word = "database" />
</KeyWords>
<KeyWords name ="TSql" bold="false" italic = "false" color = "DarkBlue">
<Key word = "IF" />
<Key word = "ELSE" />
<Key word = "CASE" />
<Key word = "RETURN" />
<Key word = "END" />
<Key word = "DECLARE" />
<Key word = "SWITCH" />
<Key word = "END" />
<Key word = "BEGIN" />
<Key word = "CASE" />
<Key word = "WHEN" />
<Key word = "END" />
</KeyWords>
<KeyWords name ="Punctuation" bold="false" italic = "false" color = "DarkSlateGray">
<Key word = "(" />
<Key word = ")" />
</KeyWords>
<KeyWords name ="Operators" bold="false" italic = "false" color = "DarkSlateGray">
<Key word = "&lt;" />
<Key word = "&gt;" />
<Key word = "=" />
</KeyWords>
<KeyWords name ="Functions" bold="false" italic = "false" color = "DarkRed">
<Key word = "substring" />
<Key word = "upper" />
<Key word = "lower" />
<Key word = "reverse" />
<Key word = "avg" />
<Key word = "min" />
<Key word = "max" />
<Key word = "replace" />
<Key word = "ltrim" />
<Key word = "rtrim" />
<Key word = "cast" />
</KeyWords>
</RuleSet>
</RuleSets>
</SyntaxDefinition>

32
src/AddIns/Misc/SharpServerTools/SharpDbTools/Src/Forms/SQLTool.Designer.cs generated

@ -37,13 +37,13 @@ namespace SharpDbTools.Forms
this.sqlToolTabControl = new System.Windows.Forms.TabControl(); this.sqlToolTabControl = new System.Windows.Forms.TabControl();
this.editorTab = new System.Windows.Forms.TabPage(); this.editorTab = new System.Windows.Forms.TabPage();
this.resultTab = 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.resultDataGridView = new System.Windows.Forms.DataGridView();
this.messageTab = new System.Windows.Forms.TabPage();
this.messageTextBox = new System.Windows.Forms.TextBox(); this.messageTextBox = new System.Windows.Forms.TextBox();
this.sqlToolTabControl.SuspendLayout(); this.sqlToolTabControl.SuspendLayout();
this.resultTab.SuspendLayout(); this.resultTab.SuspendLayout();
this.messageTab.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.resultDataGridView)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.resultDataGridView)).BeginInit();
this.messageTab.SuspendLayout();
this.SuspendLayout(); this.SuspendLayout();
// //
// sqlToolTabControl // sqlToolTabControl
@ -57,7 +57,6 @@ namespace SharpDbTools.Forms
this.sqlToolTabControl.SelectedIndex = 0; this.sqlToolTabControl.SelectedIndex = 0;
this.sqlToolTabControl.Size = new System.Drawing.Size(885, 522); this.sqlToolTabControl.Size = new System.Drawing.Size(885, 522);
this.sqlToolTabControl.TabIndex = 0; this.sqlToolTabControl.TabIndex = 0;
this.sqlToolTabControl.UseWaitCursor = true;
// //
// editorTab // editorTab
// //
@ -68,7 +67,6 @@ namespace SharpDbTools.Forms
this.editorTab.TabIndex = 0; this.editorTab.TabIndex = 0;
this.editorTab.Text = "Editor"; this.editorTab.Text = "Editor";
this.editorTab.UseVisualStyleBackColor = true; this.editorTab.UseVisualStyleBackColor = true;
this.editorTab.UseWaitCursor = true;
// //
// resultTab // resultTab
// //
@ -80,7 +78,15 @@ namespace SharpDbTools.Forms
this.resultTab.TabIndex = 1; this.resultTab.TabIndex = 1;
this.resultTab.Text = "Results"; this.resultTab.Text = "Results";
this.resultTab.UseVisualStyleBackColor = true; 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 // messageTab
// //
@ -92,23 +98,13 @@ namespace SharpDbTools.Forms
this.messageTab.TabIndex = 2; this.messageTab.TabIndex = 2;
this.messageTab.Text = "Messages"; this.messageTab.Text = "Messages";
this.messageTab.UseVisualStyleBackColor = true; this.messageTab.UseVisualStyleBackColor = true;
this.messageTab.UseWaitCursor = true;
// //
// dataGridView1 // messageTextBox
//
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
// //
this.messageTextBox.Dock = System.Windows.Forms.DockStyle.Fill; this.messageTextBox.Dock = System.Windows.Forms.DockStyle.Fill;
this.messageTextBox.Location = new System.Drawing.Point(3, 3); this.messageTextBox.Location = new System.Drawing.Point(3, 3);
this.messageTextBox.Multiline = true; this.messageTextBox.Multiline = true;
this.messageTextBox.Name = "messages"; this.messageTextBox.Name = "messageTextBox";
this.messageTextBox.Size = new System.Drawing.Size(871, 488); this.messageTextBox.Size = new System.Drawing.Size(871, 488);
this.messageTextBox.TabIndex = 0; this.messageTextBox.TabIndex = 0;
// //
@ -121,9 +117,9 @@ namespace SharpDbTools.Forms
this.Size = new System.Drawing.Size(885, 522); this.Size = new System.Drawing.Size(885, 522);
this.sqlToolTabControl.ResumeLayout(false); this.sqlToolTabControl.ResumeLayout(false);
this.resultTab.ResumeLayout(false); this.resultTab.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.resultDataGridView)).EndInit();
this.messageTab.ResumeLayout(false); this.messageTab.ResumeLayout(false);
this.messageTab.PerformLayout(); this.messageTab.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.resultDataGridView)).EndInit();
this.ResumeLayout(false); this.ResumeLayout(false);
} }
private System.Windows.Forms.TextBox messageTextBox; private System.Windows.Forms.TextBox messageTextBox;

115
src/AddIns/Misc/SharpServerTools/SharpDbTools/Src/Forms/SQLTool.cs

@ -1,5 +1,6 @@
/* /*
* User: dickon * User: dickon
* Contributions from: troy@ebswift.com
* Date: 21/11/2006 * Date: 21/11/2006
* Time: 19:12 * Time: 19:12
* *
@ -9,10 +10,15 @@ using System;
using System.ComponentModel; using System.ComponentModel;
using System.Drawing; using System.Drawing;
using System.Windows.Forms; using System.Windows.Forms;
using System.IO;
using System.Data.Common;
using System.Data;
using ICSharpCode.TextEditor; using ICSharpCode.TextEditor;
using ICSharpCode.Core; using ICSharpCode.Core;
using SharpDbTools.Data;
namespace SharpDbTools.Forms namespace SharpDbTools.Forms
{ {
/// <summary> /// <summary>
@ -23,6 +29,8 @@ namespace SharpDbTools.Forms
{ {
private string logicalConnectionName = null; private string logicalConnectionName = null;
private TextEditorControl sqlEditorControl = null; private TextEditorControl sqlEditorControl = null;
private BackgroundWorker backgroundWorker;
private string lastSQL;
public SQLTool(string logicalConnectionName) public SQLTool(string logicalConnectionName)
{ {
@ -37,33 +45,128 @@ namespace SharpDbTools.Forms
sqlEditorControl = new TextEditorControl(); sqlEditorControl = new TextEditorControl();
sqlEditorControl.Dock = DockStyle.Fill; 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); this.editorTab.Controls.Add(sqlEditorControl);
// add context behaviour to the editor control // add context behaviour to the editor control
ContextMenuStrip contextMenu = new ContextMenuStrip(); ContextMenuStrip contextMenu = new ContextMenuStrip();
ToolStripMenuItem executeSQLMenuItem = new ToolStripMenuItem("Execute SQL"); ToolStripMenuItem runSQLMenuItem = new ToolStripMenuItem("Run SQL");
executeSQLMenuItem.Click += new EventHandler(ExecuteSQLClickHandler); runSQLMenuItem.Click += new EventHandler(RunSQLClickHandler);
contextMenu.Items.AddRange(new ToolStripMenuItem[] contextMenu.Items.AddRange(new ToolStripMenuItem[]
{ {
executeSQLMenuItem runSQLMenuItem
}); });
sqlEditorControl.ContextMenuStrip = contextMenu; 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 // 1. get a connection from the the logical connection name
// 2. attempt to execute any SQL currently contained in the editor // 2. attempt to execute any SQL currently contained in the editor
// 3. display either a result set in the result DataGridView, or // 3. display either a result set in the result DataGridView, or
// messages in the messages textbox in the message tab. // 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... // 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)
{
}
} }
} }

Loading…
Cancel
Save