Click here to Skip to main content
15,891,905 members
Articles / Database Development / SQL Server

Using SQL Server Metadata and Statistics to Build a Table Explorer Application

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
1 Oct 2012CPOL8 min read 22.4K   488   7  
Develops an analogy between database table and file directory information and argues that it could be very useful if it were possible to have a similar application to Windows Explorer for database tables. Demonstrates how to obtain the information from Sql Server system tables and dynamic management
using System;
using System.Collections;
using System.Data;

namespace DbView
{
	/// <summary>
	/// Summary description for GridState.
	/// </summary>
	public class GridState
	{
        public String m_connection_tag="";
        public String m_view_name="";
        public String m_search_text="";
        public bool  m_search_source=false;
        public int m_special_view=0;
		public MainForm.DisplayType m_DisplayType;
		public int m_seq = 0;
        public int m_grid_row=0;

		public GridState(String conn_tag, MainForm.DisplayType displayType)
		{
			m_DisplayType = displayType;
		    m_connection_tag = conn_tag;
		}

		public GridState(GridState src)
		{
			CopyContent(src);
		}

		public void CopyContent(GridState src)
		{
			m_connection_tag = src.m_connection_tag;
			m_view_name = src.m_view_name;
            m_search_text = src.m_search_text;
            m_search_source = src.m_search_source;
            m_special_view = src.m_special_view;
			m_DisplayType = src.m_DisplayType;
			m_seq = src.m_seq;
			m_grid_row = src.m_grid_row;
		}
		
		
        public String decodeSpecial()
        {
			if (m_DisplayType == MainForm.DisplayType.CustomView)
				return m_view_name;
			return m_DisplayType.ToString();
/*
            String view = (m_special_view == 0) ? m_view_name :
                (m_special_view == 1) ? "Tables" :
                (m_special_view == 2) ? "Views" :
                (m_special_view == 3) ? "Procs" :
                (m_special_view == 4) ? "Triggers" :
                (m_special_view == 5) ? "Fields" :
                (m_special_view == 6) ? "Profile" : 
                (m_special_view == 7) ? "History" : 
                (m_special_view == 8) ? "Permissions" :
                (m_special_view == 9) ? "DC Message" :
                (m_special_view == 10) ? "Aggregate" :
                (m_special_view == 11) ? "Functions" :
				(m_special_view == 12) ? "Jobs" :
				(m_special_view == 13) ? "Field Info" :
                (m_special_view == 14) ? "Computed Fields" :
                (m_special_view == 15) ? "Dependencies" :
                (m_special_view == 16) ? "Processes" :
                (m_special_view == 17) ? "Locks" :
                (m_special_view == 18) ? "Service Broker" :
				(m_special_view == 19) ? "Sql Templates" :
				(m_special_view == 20) ? "Job Steps" :
				"???";
            return view;
*/
        }
        public override string ToString()
        {
            String view = decodeSpecial();
            return String.Format("{0}.{1} [{2}]",  m_connection_tag, view, m_search_text);
        }

        public int CompareTo(GridState comp)
        {
            if (this.m_connection_tag == comp.m_connection_tag)
                if (this.m_DisplayType == comp.m_DisplayType)
                    if (this.m_view_name == comp.m_view_name)
                        if (this.m_search_text == comp.m_search_text)
                            if (this.m_search_source == comp.m_search_source)
                                return 0;
            return 1; // force greater than.                        
                    
        }

	}

    public class GridStateList
    {
        private GridStateHistory m_History;
        private ArrayList m_list;
        private int m_pos_in_list;
		
        public override string ToString()
        {
            String s = "--- Grid State List -------\n";
            foreach (GridState gs in m_list)
            {
                s += gs.ToString() + "\n";
            }
            return s;            
        }

        public GridStateList()
        {
            m_list = new ArrayList();
            m_pos_in_list = 0;
            m_History = new GridStateHistory();
        }

        public GridStateHistory History
        {
            get {return this.m_History; }
        }
        
        
        private void trace(String tag)
        {
            //Console.WriteLine("{2}: {0} of {1}", m_pos_in_list, m_list.Count, tag);
        }

        private void trace(String tag, int pos)
        {
			/*
            Console.Write("{2}: {0} of {1}", m_pos_in_list, m_list.Count, tag);
            GridState gs = (GridState)m_list[pos];
            Console.WriteLine("{0}", gs.ToString());
            */
        }

        private bool okToAdd(GridState NewState)
        {
            if (m_list.Count == 0)  // always ok to add 1st one
                return true;
            int iPrevious = m_list.Count-1;
            GridState gs = (GridState)this.m_list[iPrevious];
            // not ok if they represent the same state
            return (NewState.CompareTo(gs) == 0) ? false : true;
        }

        private bool okToInsertAfter(GridState NewState, int iPos)
        {
            if (iPos >= m_list.Count-1)  // can't insert at end
                return false;
            GridState gs = (GridState)this.m_list[iPos];
            // not ok if they represent the same state
            return (NewState.CompareTo(gs) == 0) ? false : true;
        }
        
        public void add(GridState state)
        {
            trace("pre-add");
            if (m_pos_in_list >= m_list.Count-1) // if positioned at the end
            {
                if (okToAdd(state))
                {
                    trace("add to end");
                    // check for previous
                    m_list.Add(state);
                    m_pos_in_list = m_list.Count-1;
                    m_History.add(state);
                }
                else
                    trace("same as last state");
            }                
            else
            {
                if (this.okToInsertAfter(state, m_pos_in_list))
                {
                    trace("insert");
                    ++m_pos_in_list; 
                    m_list.Insert(m_pos_in_list, state);
                    while (m_list.Count > m_pos_in_list+1)
                        m_list.RemoveAt(m_pos_in_list+1);
                    m_History.add(state);
                }
                else
                    trace("same as last state");
            }
            trace("post-add");
            trace(this.ToString());
        }

        public GridState back()
        {
            trace("pre-back()");
            if (m_pos_in_list <= 0)
                return null;
            --m_pos_in_list;
            trace("post-back()", m_pos_in_list);
            return (GridState)m_list[m_pos_in_list];
        }

        public GridState back(String conn_tag)
        {
            trace("back(conn)");
            while (true)
            {
                if (m_pos_in_list <= 0)
                    return null;
                --m_pos_in_list;
                GridState g = (GridState)m_list[m_pos_in_list];
                if (g.m_connection_tag.CompareTo(conn_tag) == 0)
                    return g;
            }
        }

        public GridState forward()
        {
            trace("pre-forward");
            if (m_pos_in_list >= m_list.Count - 1)
                return null;
            ++m_pos_in_list;
            trace("post-forward");
            return (GridState)m_list[m_pos_in_list];
        }

        // hacky effort
		// schedule for removal
        public void updateRowState(int iRow)
        {
            if (m_pos_in_list < 0 || m_pos_in_list >= m_list.Count)
                return;
            GridState g = (GridState)m_list[m_pos_in_list];   
            g.m_grid_row = iRow;
        }

        public String [] getHistoryMemoList()
        {
            int iMaxInList = 15;         // maximum history entries
            int iPrev = m_pos_in_list-1; // show all back from current
            int iSize = (iPrev < iMaxInList) ? iPrev+1 : iMaxInList; // array size
            String [] s = new String[iSize];
            int idx = iPrev;   
            int iSeq = 0;
            while (idx >= 0 && iSeq < iMaxInList)
            {
                s[iSeq++] = ((GridState)m_list[idx]).ToString();
                --idx;
            }
            return s;
        }
        public GridState goBackTo(String strStateMemo)
        {
            GridState g = back();
            while (g != null)
            {
                if (strStateMemo.CompareTo(g.ToString()) == 0)
                    return g;
                g = back();
            }   
            return g; 
        }
        
        public DataTable getAllHistory()
        {
            return m_History.getAllHistory();
        }

    }

    public class GridStateHistory
    {
        private ArrayList m_list;
        private int m_pos_in_list;
        private int m_last_seq = 0;
        		
        public override string ToString()
        {
            String s = "--- Grid State List -------\n";
            foreach (GridState gs in m_list)
            {
                s += gs.ToString() + "\n";
            }
            return s;            
        }

        public GridStateHistory()
        {
            m_list = new ArrayList();
            m_pos_in_list = 0;
            m_last_seq = 0;
        }

        private void trace(String tag)
        {
            //Console.WriteLine("{2}: {0} of {1}", m_pos_in_list, m_list.Count, tag);
        }

        private void trace(String tag, int pos)
        {
            //Console.Write("{2}: {0} of {1}", m_pos_in_list, m_list.Count, tag);
            //GridState gs = (GridState)m_list[pos];
            //Console.WriteLine("{0}", gs.ToString());
        }

        public GridState findInList(GridState NewState)
        {
            if (m_list.Count == 0)  // always ok to add 1st one
                return null;
            foreach (GridState g in m_list)
                if (NewState.CompareTo(g) == 0)
                    return g;
            return null;
        }

        public GridState findInList(int iSeq)
        {
            if (m_list.Count == 0)  // always ok to add 1st one
                return null;
            foreach (GridState g in m_list)
                if (g.m_seq == iSeq)
                    return g;
            return null;
        }

        public void add(GridState state)
        {
            GridState g = findInList(state);
            ++m_last_seq;
            if (g == null)
            {
                m_list.Add(state);
                g = state;
            }    
            // make latest in history
            g.m_seq = m_last_seq;
        }            

        public DataTable getAllHistory()
        {
            // conver
            DataTable dt = new DataTable("History");
            dt.Columns.Add("id", typeof(int));
            dt.Columns.Add("Connection", typeof(String));
            dt.Columns.Add("View", typeof(String));
            dt.Columns.Add("Text", typeof(String));
            // ought to sort in the DataGridView using DataView
            // but its too late for that
            int idx = 0;
            int idx2 = 0;
            int [] seq = new int[m_list.Count];
            for (idx = 0; idx < m_list.Count; ++idx)
                seq[idx] = idx;
             
            for (idx = 0; idx < m_list.Count; ++idx)
                for (idx2 = idx; idx2 < m_list.Count; ++idx2)
                    if (((GridState)m_list[seq[idx2]]).m_seq > ((GridState)m_list[seq[idx]]).m_seq)
                    {
                        int tmp  = seq[idx2];
                        seq[idx2] = seq[idx];
                        seq[idx] = tmp;
                    }
            
            
            for (idx = 0; idx < m_list.Count; ++idx)
            {
                GridState state = (GridState)m_list[seq[idx]];
                DataRow dr = dt.NewRow();    
                dr[0] = state.m_seq;
                dr[1] = state.m_connection_tag;
                dr[2] = state.decodeSpecial();
                dr[3] = state.m_search_text;
                dt.Rows.Add(dr);
            } 
            return dt;
        }


    }
}

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 Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions