Click here to Skip to main content
15,896,726 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 252.4K   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.Xml;
using System.Text;
using System.Drawing;
using System.Collections;
using System.Globalization;
using System.Windows.Forms;
using System.Security.Cryptography;

namespace SqlBuilder.Controls
{
	public class ListViewEx : ListView
	{
		public enum eType
		{
			Invalid,
			SQL,       // File
			VIEW,      // SysObject
			TRIGGER,   // SysObject
			PROCEDURE, // SysObject
			FUNCTION   // SysObject
		}

		public struct kItem
		{
			public ListViewItem i_Item;
			public bool         b_Valid;
			public eType        e_Type;        //  VIEW ,  SQL  ...
			public string       s_DisplayType; // "VIEW", "FILE"...
			public string       s_Path;        // "C:\Program Files\SqlBuilder\DBase\vList.view"
			public string       s_RelPath;     // "DBase\vList.view"
			public string       s_BackupPath;  // "DBase\VIEWS\vList.view"
			public string       s_FileName;    // "vList.view"
			public string       s_SysObject;   // "vList"
			public string       s_DisplayName; // "vList" or "Script.sql"
			public string       s_DataBase;    // "DBase"
		}

		public delegate void UserAcionHandler(ref bool b_Cancel);
		public event UserAcionHandler UserAction = null;

		string ms_BaseDir = "";

		public void SetBaseDir(string s_BaseDir)
		{
			ms_BaseDir = s_BaseDir;
			Items.Clear();
		}

		protected override void OnCreateControl()
		{
			base.OnCreateControl ();

			View = View.Details;
			Columns.Clear(); // IMPORTANT! (Visual Studio Designer adds columns!)
			Columns.Add("SQL File", this.Width -7, HorizontalAlignment.Left);
			HeaderStyle = ColumnHeaderStyle.None;
			FullRowSelect = true;
			HideSelection = false;
		}

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

			if (Columns.Count > 0)
				Columns[0].Width = this.Width -7;
		}


		public ListViewItem AddItem(string s_File, bool b_Selected)
		{
			string s_RelPath = s_File.Substring(ms_BaseDir.Length);

			// Add only files which are in a direct subdirectory of the EXE file
			string[] s_Count = s_RelPath.Split('\\');
			if (s_Count.Length != 2)
				return null;

			ListViewItem i_Item = new ListViewItem(s_RelPath.Replace("\\", ": "));
			i_Item.Tag = s_RelPath;

			if (Functions.GetFileExtension(s_File) == ".sql")
				i_Item.ForeColor = Color.Blue;
			else
				i_Item.ForeColor = Color.Green;

			Items.Add(i_Item);
			i_Item.Selected = b_Selected;
			i_Item.Focused  = false;
			return i_Item;
		}

		public kItem GetItemData(int Index)
		{
			kItem k_Item = new kItem();
			k_Item.b_Valid = false;

			if (Index >= 0 && Index < Items.Count)
			{
				k_Item.i_Item = Items[Index];
				// s_Parts[0] = Database, s_Parts[1]= file name
				string[] s_Parts = k_Item.i_Item.Tag.ToString().Split('\\');
				if (s_Parts.Length == 2)
				{
					switch (Functions.GetFileExtension(s_Parts[1]))
					{
						case ".sql":   k_Item.e_Type = eType.SQL;       break;
						case ".trig":  k_Item.e_Type = eType.TRIGGER;   break;
						case ".proc":  k_Item.e_Type = eType.PROCEDURE; break;
						case ".func":  k_Item.e_Type = eType.FUNCTION;  break;
						case ".view":  k_Item.e_Type = eType.VIEW;      break;
						default:       k_Item.e_Type = eType.Invalid;   break;
					}

					k_Item.s_DataBase    = s_Parts[0].ToUpper();
					k_Item.s_FileName    = s_Parts[1];
					k_Item.s_SysObject   = Functions.CutEndReverseAt(k_Item.s_FileName, ".");
					k_Item.s_RelPath     = k_Item.i_Item.Tag.ToString();
					k_Item.s_Path        = ms_BaseDir + k_Item.s_RelPath;
					k_Item.s_BackupPath  = k_Item.s_DataBase + "\\" + k_Item.e_Type + "S\\" + k_Item.s_SysObject + ".sql";
					k_Item.s_DisplayName = (k_Item.e_Type == eType.SQL) ? k_Item.s_FileName : k_Item.s_SysObject;
					k_Item.s_DisplayType = (k_Item.e_Type == eType.SQL) ? "FILE"            : k_Item.e_Type.ToString();
					k_Item.b_Valid       = (k_Item.e_Type != eType.Invalid);
				}
			}
			return k_Item;
		}

		public kItem SelectedItem
		{
			get { return GetItemData(SelectedItemIndex); }
		}

		public int SelectedItemIndex
		{
			get 
			{
				if (SelectedItems.Count == 0)
					return -1;
				else
					return SelectedItems[0].Index;
			}
			set
			{
				if (value >= 0 && value < Items.Count)
					Items[value].Selected = true;
			}
		}

		public ListViewItem FindByRelPath(string s_RelPath)
		{
			if (s_RelPath == null || s_RelPath.Length == 0)
				return null;

			foreach (ListViewItem i_Item in Items)
			{
				if (string.Compare(i_Item.Tag.ToString(), s_RelPath, true)==0)
					return i_Item;
			}
			return null;
		}

		public void SelectItem(string s_RelPath)
		{
			ListViewItem i_Item = FindByRelPath(s_RelPath);
			if (i_Item != null)
				i_Item.Selected = true;
		}

		protected override void WndProc(ref Message m)
		{
			const int WM_KEYDOWN     = 0x0100;
			const int WM_LBUTTONDOWN = 0x0201;
			const int WM_RBUTTONDOWN = 0x0204;
			const int WM_MBUTTONDOWN = 0x0207;

			if (m.Msg == WM_LBUTTONDOWN ||
				m.Msg == WM_MBUTTONDOWN ||
				m.Msg == WM_RBUTTONDOWN ||
				m.Msg == WM_KEYDOWN)
			{
				// fire event on mouse or keyboard action
				if (UserAction != null)
				{
					bool b_Cancel = false;
					UserAction(ref b_Cancel);
					if (b_Cancel)
						return;
				}
			}

			base.WndProc (ref m);
		}

		/// <summary>
		/// Start dragging a listview item
		/// </summary>
		protected override void OnItemDrag(ItemDragEventArgs e)
		{
			if (Items.Count > 1)
			{
				int s32_Index = ((ListViewItem)e.Item).Index; // The item which is dragged
				DoDragDrop(s32_Index, DragDropEffects.Move);
			}
		}

		/// <summary>
		/// During Drag operation
		/// </summary>
		protected override void OnDragOver(DragEventArgs e)
		{
			e.Effect = DragDropEffects.Move;
		}

		/// <summary>
		/// End of Drag operation
		/// </summary>
		
		protected override void OnDragDrop(DragEventArgs e)
		{
			Point pt_Drop = PointToClient(new Point(e.X, e.Y));
			
			int s32_TargetIndex = Items.Count; // drop behind last item

			for (int Index=0; Index<Items.Count; Index++)
			{
				ListViewItem i_Item = Items[Index];
				if (pt_Drop.Y <= i_Item.Bounds.Top + i_Item.Bounds.Height / 2)
				{
					s32_TargetIndex = Index;
					break;
				}
			}

			int s32_StartIndex = (int)e.Data.GetData(typeof(int));
			if (s32_StartIndex == s32_TargetIndex)
				return; // Item was dragged on itself

			ListViewItem i_MovedItem = Items[s32_StartIndex];
			ListViewItem i_Copy = (ListViewItem)i_MovedItem.Clone();
			i_Copy.Selected = true;
			Items.Insert(s32_TargetIndex, i_Copy);
			Items.Remove(i_MovedItem);

			base.OnDragDrop(e); // fire event
		}
	}
}

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