Click here to Skip to main content
15,886,052 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 250.6K   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.Text;
using System.Drawing;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using SqlBuilder.Forms;
using SqlBuilder.Controls;

using eType = SqlBuilder.Controls.ListViewEx.eType;

namespace SqlBuilder.Forms
{
	/// <summary>
	/// Build the compound script
	/// </summary>
	public class frmBuildScript : frmBaseForm
	{
		string     ms_CompoundSql;
		string     ms_BackupDir;
		string     ms_BackupXml;
		string     ms_BackupSource = null;
		ListViewEx mi_ListFiles;

		public frmBuildScript(ListViewEx i_ListFiles, string s_BackupDir, string s_BackupXml, string s_CompoundSql)
		{
			InitializeComponent();
			
			StoreWindowPos = true; // must be set in Constructor AFTER InitializeComponent() !!

			mi_ListFiles   = i_ListFiles;
			ms_CompoundSql = s_CompoundSql;
			ms_BackupDir   = s_BackupDir;
			ms_BackupXml   = s_BackupXml;
		}

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

			btnStart.Enabled = false;

			radioServer.Text = string.Format("Build script directly from data on server {0}.", frmMain.Server.ToUpper());
            
			string s_Date   = XML.Read(this, ms_BackupXml, "Date",   "");
			string s_Server = XML.Read(this, ms_BackupXml, "Server", "");

			if (s_Date.Length > 0 && s_Server.Length > 0)
			{
				ms_BackupSource  = string.Format("Backup created on {0} from server {1}.", s_Date, s_Server.ToUpper());
				radioBackup.Text = "Build script from " + ms_BackupSource;
			}
		}

		private void OnRadioServerChecked(object sender, EventArgs e)
		{
			if (!radioServer.Checked)
				return;

			btnStart.Enabled = true;
		}

		private void OnRadioBackupChecked(object sender, EventArgs e)
		{
			if (!radioBackup.Checked)
				return;

			if (ms_BackupSource == null)
			{
				btnStart.Enabled = false;
				frmMsgBox.Err(this, "There is no valid database backup available in the current working directory.\nPlease execute a database backup first!");
			}
			else btnStart.Enabled = true;
		}

		private void OnButtonBuildScript(object sender, EventArgs e)
		{
			btnStart.Enabled = false;

			if (!Functions.MoveToRecycleBin(this, ms_CompoundSql))
				goto _Abort;

			Parser     i_Parser = new Parser();
			StringBuilder s_Out = new StringBuilder(10000);

			s_Out.Append("-- Script Creation: ");
			s_Out.Append(DateTime.Today.ToString(Defaults.DateFormat));
			s_Out.Append("\r\n-- Script Source: ");

			if (radioBackup.Checked)
				s_Out.Append(ms_BackupSource);
			else 
				s_Out.Append(frmMain.Server.ToUpper());

			s_Out.Append("\r\n\r\n");

			for (int i=0; i<mi_ListFiles.Items.Count; i++)
			{
				ListViewEx.kItem k_Item = mi_ListFiles.GetItemData(i);
				if (!k_Item.b_Valid)
					goto _Abort; // invalid data -> abort

				lblStatus.Text = k_Item.s_FileName;
				Application.DoEvents();

				int s32_DelimLength = 120; // length of "###########..."

				s_Out.Append("\r\n\r\n--");
				s_Out.Append('*', s32_DelimLength);
				s_Out.Append("\r\n--\r\n--");
				s_Out.Append(' ', Math.Max(0,(s32_DelimLength - k_Item.s_DisplayName.Length - k_Item.s_DisplayType.Length) / 2));
				s_Out.Append(k_Item.s_DisplayType);
				s_Out.Append("  ");
				s_Out.Append(k_Item.s_DisplayName);
				s_Out.Append("\r\n--\r\n--");
				s_Out.Append('*', s32_DelimLength);
				s_Out.Append("\r\n\r\n");
				s_Out.Append("USE [" + k_Item.s_DataBase + "]\r\n");

				string s_Content = null;
				if (k_Item.e_Type == eType.SQL) // SQL files
				{
					s_Content = Functions.ReadFileIntoString(this, k_Item.s_Path);
				}
				else // Procedures, Views, Functions, Triggers
				{
					s_Out.Append(SQL.BuildDeleteSysObjectCommand(k_Item.s_SysObject, k_Item.e_Type));

					if (radioBackup.Checked) // from Backup directory
					{
						string s_File = frmBackup.GetBackupFileName(ms_BackupDir + k_Item.s_BackupPath);
						s_Content = Functions.ReadFileIntoString(this, s_File);
					}
					else // from Server
					{
						SQL i_Sql = new SQL(this, frmMain.Server, k_Item.s_DataBase, frmMain.User, frmMain.Password);
						s_Content = i_Sql.ReadSysObject(k_Item.s_SysObject, k_Item.e_Type);
					}
				}

				if (s_Content == null)
					goto _Abort; // Error occurred -> abort

				i_Parser.Parse(s_Content, true, true);
				s_Out.Append(i_Parser.SQL);

				// Add "GO" if not already exists
				if (Functions.Right(s_Content, 2).ToUpper() != "GO")
					s_Out.Append("\r\nGO\r\n");
			}

			Functions.SaveAndOpenFile(this, ms_CompoundSql, s_Out.ToString(), Encoding.Unicode, ProcessWindowStyle.Maximized);
			this.Close();
			return;

			_Abort:
			lblStatus.SetTransientText("Aborted.");
			btnStart.Enabled = true;
		}

		#region Windows Form Designer generated code

		private SqlBuilder.Controls.StatusInfo lblStatus;
		private System.Windows.Forms.Button btnStart;
		private System.Windows.Forms.RadioButton radioServer;
		private System.Windows.Forms.RadioButton radioBackup;
		private System.ComponentModel.Container components = null;

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

		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmBuildScript));
			this.btnStart = new System.Windows.Forms.Button();
			this.radioServer = new System.Windows.Forms.RadioButton();
			this.radioBackup = new System.Windows.Forms.RadioButton();
			this.lblStatus = new SqlBuilder.Controls.StatusInfo();
			this.SuspendLayout();
			// 
			// btnStart
			// 
			this.btnStart.Location = new System.Drawing.Point(151, 88);
			this.btnStart.Name = "btnStart";
			this.btnStart.Size = new System.Drawing.Size(98, 23);
			this.btnStart.TabIndex = 3;
			this.btnStart.Text = "Build Script";
			this.btnStart.Click += new System.EventHandler(this.OnButtonBuildScript);
			// 
			// radioServer
			// 
			this.radioServer.Location = new System.Drawing.Point(20, 11);
			this.radioServer.Name = "radioServer";
			this.radioServer.Size = new System.Drawing.Size(342, 30);
			this.radioServer.TabIndex = 1;
			this.radioServer.Text = "Build script directly from data on server.";
			this.radioServer.CheckedChanged += new System.EventHandler(this.OnRadioServerChecked);
			// 
			// radioBackup
			// 
			this.radioBackup.Location = new System.Drawing.Point(20, 46);
			this.radioBackup.Name = "radioBackup";
			this.radioBackup.Size = new System.Drawing.Size(342, 32);
			this.radioBackup.TabIndex = 2;
			this.radioBackup.Text = "Build script from data in backup directory.";
			this.radioBackup.CheckedChanged += new System.EventHandler(this.OnRadioBackupChecked);
			// 
			// lblStatus
			// 
			this.lblStatus.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
				| System.Windows.Forms.AnchorStyles.Right)));
			this.lblStatus.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
			this.lblStatus.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.lblStatus.ForeColor = System.Drawing.Color.Blue;
			this.lblStatus.Location = new System.Drawing.Point(9, 119);
			this.lblStatus.Name = "lblStatus";
			this.lblStatus.Size = new System.Drawing.Size(381, 20);
			this.lblStatus.TabIndex = 4;
			this.lblStatus.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// frmBuildScript
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(398, 146);
			this.Controls.Add(this.lblStatus);
			this.Controls.Add(this.radioBackup);
			this.Controls.Add(this.radioServer);
			this.Controls.Add(this.btnStart);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "frmBuildScript";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = " Build Script";
			this.ResumeLayout(false);

		}
		#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