Click here to Skip to main content
15,892,746 members
Articles / Programming Languages / SQL

SQL Editor for Database Developers

Rate me:
Please Sign up or sign in to vote.
4.55/5 (65 votes)
10 Mar 2010GPL317 min read 251.8K   9K   236  
SQL editor with syntax parser, direct editing, code execution, database backup, table comparison, script generation, time measurement
/*
SqlBuilder - an intelligent database tool
 
This file is part of SqlBuilder.
www.netcult.ch/elmue

This program is free software; you can redistribute it and/or modify it 
under the terms of the GNU General Public License as published by the 
Free Software Foundation; either version 2 of the License, or 
(at your option) any later version. 
 
This program is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of 
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
GNU General Public License for more details. 
 
You find the GNU General Public License in the subfolder GNU
if not, write to the Free Software Foundation, 
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/


using System;
using System.IO;
using System.Data;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using SqlBuilder.Controls;

using eType = SqlBuilder.Controls.ListViewEx.eType;

namespace SqlBuilder.Forms
{
	/// <summary>
	/// Summary description for AddFile.
	/// </summary>
	public class frmAddFile : frmBaseForm
	{
		string  ms_AddedItem  = null;
		string  ms_CreateCode = null;
		eType   me_ObjType    = eType.Invalid;
		string  ms_WorkDir    = "";

		public frmAddFile(string s_WorkDir)
		{
			InitializeComponent();
	
			StoreWindowPos = true; // must be set in Constructor AFTER InitializeComponent() !!
			ms_WorkDir     = s_WorkDir;
			DialogResult   = DialogResult.Cancel;
		}

		/// <summary>
		/// The new item to be added
		/// </summary>
		public string AddedItem
		{
			get { return ms_AddedItem; }
		}

		public string Comment
		{
			get { return txtComment.Text; }
		}

		/// <summary>
		/// retuns the SQL code to create a new function, view, etc...
		/// </summary>
		public string CreateCode
		{
			get { return ms_CreateCode; }
		}

		protected override void OnLoad(EventArgs e)	
		{
			base.OnLoad(e);

			radioProcedure.Enabled = false;
			radioFunction. Enabled = false;
			radioView.     Enabled = false;
			radioTrigger.  Enabled = false;
			radioFile.     Enabled = false;
			cmbObjName.    Enabled = false;
			cmbDatabase.   Enabled = false;
			btnAdd.        Enabled = false;
			txtComment.    Enabled = false;
			txtFileName.   Visible = false;
		}

		protected override void OnLoadDelayed()
		{
			SQL i_Sql = new SQL(this, frmMain.Server, null, frmMain.User, frmMain.Password);
			string[] s_DataBases = i_Sql.ListAllDataBases();
			if (s_DataBases == null)
			{
				this.Close();
				return;
			}

			cmbDatabase.Sorted = false; // incredible SLOW!!
			cmbDatabase.Enabled = true;
			cmbDatabase.MaxDropDownItems = 25;
			cmbDatabase.Items.AddRange(s_DataBases);
			cmbDatabase.Text = (string)Functions.RegistryRead(eReg.WorkDir,"AddedDatabase", "");
		}

		private void OnComboDatabaseSelChanged(object sender, EventArgs e)
		{
			Functions.RegistryWrite(eReg.WorkDir,"AddedDatabase", cmbDatabase.Text);

			cmbObjName.Enabled = false; // read anew !

			if (cmbDatabase.Text.Length > 0)
			{
				radioProcedure.Enabled = true;
				radioProcedure.Checked = false;
				radioFunction. Enabled = true;
				radioFunction. Checked = false;
				radioView.     Enabled = true;
				radioView.     Checked = false;
				radioTrigger.  Enabled = true;
				radioTrigger.  Checked = false;
				radioFile.     Enabled = true;
				radioFile.     Checked = false;
			}
		}

		private void OnRadioChanged(object sender, EventArgs e)
		{
			me_ObjType = eType.Invalid;
			cmbObjName.Enabled = false;
			txtComment.Enabled = false;
			cmbObjName.Items.Clear();
			cmbObjName.Text = "";
			lblName.   Text = "Name:";

			if (radioFile.Checked)
			{
				me_ObjType = eType.SQL;
				txtComment. Text    = "";
				cmbObjName. Visible = false;
				txtFileName.Visible = true;
				txtFileName.Focus();
			}
			else
			{
				if (radioFunction.Checked)  me_ObjType = eType.FUNCTION;
				if (radioView.Checked)      me_ObjType = eType.VIEW;
				if (radioTrigger.Checked)   me_ObjType = eType.TRIGGER;
				if (radioProcedure.Checked) me_ObjType = eType.PROCEDURE;

				txtFileName.Visible = false;
				cmbObjName. Visible = true;

				SQL i_Sql = new SQL(this, frmMain.Server, cmbDatabase.Text, frmMain.User, frmMain.Password);
				string[] s_SysObj = i_Sql.ListAllSysObjects(me_ObjType);
				if (s_SysObj == null)
					return;

				lblName.Text = "Name:  " + s_SysObj.Length + " Items";

				if (s_SysObj.Length == 0)
				{
					cmbObjName.Text = "<EMPTY>";
				}
				else
				{
					cmbObjName.Sorted = false; // incredible SLOW!!!
					cmbObjName.Enabled = true;
					cmbObjName.DropDownWidth    = 450;
					cmbObjName.MaxDropDownItems = 25;
					cmbObjName.Items.AddRange(s_SysObj);
					cmbObjName.Focus();
				}
			}
		}

		private void OnComboObjNameKeyUp(object sender, KeyEventArgs e)
		{
			OnComboNameSelChanged(sender, e);
		}
		private void OnComboNameSelChanged(object sender, EventArgs e)
		{
			btnAdd.    Enabled = (cmbObjName.Text.Trim().Length > 0);
			txtComment.Enabled = (cmbObjName.Text.Trim().Length > 0);
		}

		private void OnTxtFileNameTextChanged(object sender, EventArgs e)
		{
			btnAdd.Enabled = true;
		}

		private void OnButtonAdd(object sender, System.EventArgs e)
		{
			if (me_ObjType == eType.Invalid ||
				(cmbObjName. Visible && cmbObjName. Text.Trim().Length == 0) ||
				(txtFileName.Visible && txtFileName.Text.Trim().Length == 0))
			{
				frmMsgBox.Err(this, "Invalid settings!");
				return;
			}

			string s_Obj = "";
			if (cmbObjName.Visible) // not for files
			{
				s_Obj = cmbObjName.Text.Trim();
				if (cmbObjName.FindString(s_Obj) == -1)
				{
					if (!frmMsgBox.Ask(this, string.Format("Do you want to create a new {0} '{1}'?", me_ObjType.ToString().ToLower(), s_Obj)))
						return;

					switch (me_ObjType)
					{
						case eType.FUNCTION:  ms_CreateCode = string.Format(Defaults.CreateFunc, s_Obj); break;
						case eType.VIEW:      ms_CreateCode = string.Format(Defaults.CreateView, s_Obj); break;
						case eType.PROCEDURE: ms_CreateCode = string.Format(Defaults.CreateProc, s_Obj); break;
						case eType.TRIGGER:   ms_CreateCode = string.Format(Defaults.CreateTrig, s_Obj); break;
					}
				}
			}
			
			if (txtFileName.Visible) // If user has put a file extension -> cut it
				s_Obj = Functions.CutEndReverseAt(txtFileName.Text, ".sql");

			ms_AddedItem = cmbDatabase.Text + "\\" + s_Obj + "." + Functions.Left(me_ObjType.ToString().ToLower(), 4);

			DialogResult = DialogResult.OK;
			this.Close();
		}

		/// <summary>
		/// On hitting enter for filename or objectname or comment -> click button "Add"
		/// On Escape -> close window
		/// </summary>
		private void OnKeyDown(object sender, KeyEventArgs e)
		{
			if (e.Control || e.Alt)
				return;

			if (e.KeyCode == Keys.Enter)
				OnButtonAdd(sender, e);

			if (e.KeyCode == Keys.Escape)
				this.Close();
		}

		#region Windows Form Designer generated code

		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.RadioButton radioProcedure;
		private System.Windows.Forms.RadioButton radioFunction;
		private System.Windows.Forms.RadioButton radioView;
		private System.Windows.Forms.RadioButton radioTrigger;
		private System.Windows.Forms.ComboBox cmbObjName;
		private System.Windows.Forms.Button btnAdd;
		private System.Windows.Forms.ComboBox cmbDatabase;
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.TextBox txtComment;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label lblName;
		private System.Windows.Forms.RadioButton radioFile;
		private System.Windows.Forms.TextBox txtFileName;

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.radioProcedure = new System.Windows.Forms.RadioButton();
			this.radioFunction = new System.Windows.Forms.RadioButton();
			this.radioView = new System.Windows.Forms.RadioButton();
			this.radioTrigger = new System.Windows.Forms.RadioButton();
			this.lblName = new System.Windows.Forms.Label();
			this.cmbObjName = new System.Windows.Forms.ComboBox();
			this.btnAdd = new System.Windows.Forms.Button();
			this.cmbDatabase = new System.Windows.Forms.ComboBox();
			this.label2 = new System.Windows.Forms.Label();
			this.txtComment = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.radioFile = new System.Windows.Forms.RadioButton();
			this.txtFileName = new System.Windows.Forms.TextBox();
			this.SuspendLayout();
			// 
			// radioProcedure
			// 
			this.radioProcedure.Location = new System.Drawing.Point(20, 70);
			this.radioProcedure.Name = "radioProcedure";
			this.radioProcedure.Size = new System.Drawing.Size(78, 16);
			this.radioProcedure.TabIndex = 2;
			this.radioProcedure.Text = "Procedure";
			this.radioProcedure.CheckedChanged += new System.EventHandler(this.OnRadioChanged);
			// 
			// radioFunction
			// 
			this.radioFunction.Location = new System.Drawing.Point(20, 92);
			this.radioFunction.Name = "radioFunction";
			this.radioFunction.Size = new System.Drawing.Size(104, 16);
			this.radioFunction.TabIndex = 3;
			this.radioFunction.Text = "Function";
			this.radioFunction.CheckedChanged += new System.EventHandler(this.OnRadioChanged);
			// 
			// radioView
			// 
			this.radioView.Location = new System.Drawing.Point(20, 114);
			this.radioView.Name = "radioView";
			this.radioView.Size = new System.Drawing.Size(104, 16);
			this.radioView.TabIndex = 4;
			this.radioView.Text = "View";
			this.radioView.CheckedChanged += new System.EventHandler(this.OnRadioChanged);
			// 
			// radioTrigger
			// 
			this.radioTrigger.Location = new System.Drawing.Point(20, 136);
			this.radioTrigger.Name = "radioTrigger";
			this.radioTrigger.Size = new System.Drawing.Size(104, 16);
			this.radioTrigger.TabIndex = 5;
			this.radioTrigger.Text = "Trigger";
			this.radioTrigger.CheckedChanged += new System.EventHandler(this.OnRadioChanged);
			// 
			// lblName
			// 
			this.lblName.Location = new System.Drawing.Point(20, 184);
			this.lblName.Name = "lblName";
			this.lblName.Size = new System.Drawing.Size(100, 14);
			this.lblName.TabIndex = 0;
			this.lblName.Text = "Name:";
			// 
			// cmbObjName
			// 
			this.cmbObjName.ItemHeight = 13;
			this.cmbObjName.Location = new System.Drawing.Point(20, 198);
			this.cmbObjName.Name = "cmbObjName";
			this.cmbObjName.Size = new System.Drawing.Size(368, 21);
			this.cmbObjName.Sorted = true;
			this.cmbObjName.TabIndex = 7;
			this.cmbObjName.KeyDown += new System.Windows.Forms.KeyEventHandler(this.OnKeyDown);
			this.cmbObjName.KeyUp += new System.Windows.Forms.KeyEventHandler(this.OnComboObjNameKeyUp);
			this.cmbObjName.SelectedIndexChanged += new System.EventHandler(this.OnComboNameSelChanged);
			// 
			// btnAdd
			// 
			this.btnAdd.Location = new System.Drawing.Point(166, 270);
			this.btnAdd.Name = "btnAdd";
			this.btnAdd.TabIndex = 9;
			this.btnAdd.Text = "Add";
			this.btnAdd.Click += new System.EventHandler(this.OnButtonAdd);
			// 
			// cmbDatabase
			// 
			this.cmbDatabase.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cmbDatabase.Location = new System.Drawing.Point(20, 32);
			this.cmbDatabase.Name = "cmbDatabase";
			this.cmbDatabase.Size = new System.Drawing.Size(168, 21);
			this.cmbDatabase.Sorted = true;
			this.cmbDatabase.TabIndex = 1;
			this.cmbDatabase.SelectedIndexChanged += new System.EventHandler(this.OnComboDatabaseSelChanged);
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(20, 18);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(100, 14);
			this.label2.TabIndex = 0;
			this.label2.Text = "Database:";
			// 
			// txtComment
			// 
			this.txtComment.Location = new System.Drawing.Point(20, 240);
			this.txtComment.Name = "txtComment";
			this.txtComment.Size = new System.Drawing.Size(368, 20);
			this.txtComment.TabIndex = 8;
			this.txtComment.Text = "";
			this.txtComment.KeyDown += new System.Windows.Forms.KeyEventHandler(this.OnKeyDown);
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(20, 226);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(100, 14);
			this.label3.TabIndex = 0;
			this.label3.Text = "Comment:";
			// 
			// radioFile
			// 
			this.radioFile.Location = new System.Drawing.Point(20, 158);
			this.radioFile.Name = "radioFile";
			this.radioFile.Size = new System.Drawing.Size(104, 16);
			this.radioFile.TabIndex = 6;
			this.radioFile.Text = "File";
			this.radioFile.CheckedChanged += new System.EventHandler(this.OnRadioChanged);
			// 
			// txtFileName
			// 
			this.txtFileName.Location = new System.Drawing.Point(20, 198);
			this.txtFileName.Name = "txtFileName";
			this.txtFileName.Size = new System.Drawing.Size(368, 20);
			this.txtFileName.TabIndex = 7;
			this.txtFileName.Text = "";
			this.txtFileName.KeyDown += new System.Windows.Forms.KeyEventHandler(this.OnKeyDown);
			this.txtFileName.TextChanged += new System.EventHandler(this.OnTxtFileNameTextChanged);
			// 
			// frmAddFile
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(404, 301);
			this.Controls.Add(this.txtFileName);
			this.Controls.Add(this.txtComment);
			this.Controls.Add(this.radioFile);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.cmbDatabase);
			this.Controls.Add(this.btnAdd);
			this.Controls.Add(this.cmbObjName);
			this.Controls.Add(this.lblName);
			this.Controls.Add(this.radioTrigger);
			this.Controls.Add(this.radioView);
			this.Controls.Add(this.radioFunction);
			this.Controls.Add(this.radioProcedure);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "frmAddFile";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = " Add";
			this.ResumeLayout(false);

		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer (Senior) ElmüSoft
Chile Chile
Software Engineer since 40 years.

Comments and Discussions