Click here to Skip to main content
15,885,032 members
Articles / Desktop Programming / WPF

Reflection Studio - Part 1 - Introduction: Architecture and Design

Rate me:
Please Sign up or sign in to vote.
4.83/5 (23 votes)
22 Sep 2010GPL36 min read 59.9K   6.9K   111  
Reflection Studio is a "developer" application for assembly, database, performance, and code generation, written in C# under WPF 4.0.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using ReflectionStudio.Core.Database.Schema;

namespace ReflectionStudio.Core.Database.SQL2KProvider
{
	class SqlWriter : IDbSQLWriter
	{
		#region ---------------------CREATE---------------------

		public string Create(SchemaObjectBase schemaObject)
		{
			StringWriter stringWriter = new StringWriter();

			Create(stringWriter, schemaObject);

			return stringWriter.ToString();
		}

		public void Create(TextWriter writer, SchemaObjectBase schemaObject)
		{
			if (schemaObject is ViewSchema)
				writer.Write(schemaObject.Database.Provider.GetViewText(schemaObject.Database.ConnectionString, (ViewSchema)schemaObject));

			if( schemaObject is CommandSchema )
				writer.Write(schemaObject.Database.Provider.GetCommandText(schemaObject.Database.ConnectionString, (CommandSchema)schemaObject));

			if (schemaObject is TableSchema)
			{
				// TODO
			}
		}

		#endregion

		#region ---------------------ALTER---------------------

		public string Alter(SchemaObjectBase schemaObject)
		{
			StringWriter stringWriter = new StringWriter();

			Alter(stringWriter, schemaObject);

			return stringWriter.ToString();
		}

		public void Alter(TextWriter writer, SchemaObjectBase schemaObject)
		{
			if (schemaObject is ViewSchema)
				writer.Write(
					schemaObject.Database.Provider.GetViewText(schemaObject.Database.ConnectionString, (ViewSchema)schemaObject).Replace("CREATE ", "ALTER ")
					);

			if (schemaObject is CommandSchema)
				writer.Write(
					schemaObject.Database.Provider.GetCommandText(schemaObject.Database.ConnectionString, (CommandSchema)schemaObject).Replace("CREATE ", "ALTER ")
					);

			if (schemaObject is TableSchema)
			{
				// TODO
			}
		}

		#endregion

		#region ---------------------DROP---------------------

		public string Drop(SchemaObjectBase schemaObject)
		{
			StringWriter stringWriter = new StringWriter();

			Drop(stringWriter, schemaObject);

			return stringWriter.ToString();
		}

		public void Drop(TextWriter writer, SchemaObjectBase schemaObject)
		{
			writer.WriteLine( string.Format( "/****** Objet :  [{0}].[{1}]    Date de génération du script : {2} ******/",
													schemaObject.Owner, schemaObject.Name, DateTime.Now.ToLongDateString()) );

			writer.Write( "IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'");
			writer.WriteLine(string.Format("[{0}].[{1}]')", schemaObject.Owner, schemaObject.Name));

			if (schemaObject is CommandSchema)
				writer.WriteLine("AND type in (N'P', N'PC'))");
			if (schemaObject is TableSchema)
				writer.WriteLine("AND type in (N'U'))");
			if (schemaObject is ViewSchema)
				writer.WriteLine("AND type in (N'V'))");

			writer.Write("DROP ");
			if( schemaObject is CommandSchema )
				writer.Write("PROCEDURE");
			if (schemaObject is TableSchema)
				writer.Write("TABLE");
			if (schemaObject is ViewSchema)
				writer.Write("VIEW");

			writer.WriteLine(string.Format("[{0}].[{1}])", schemaObject.Owner, schemaObject.Name));
		}

		#endregion

		#region ---------------------SELECT---------------------

		public string Select(ITabularObjectBase tableOrView)
		{
			StringWriter stringWriter = new StringWriter();

			Select(stringWriter, tableOrView);

			return stringWriter.ToString();
		}

		public void Select(TextWriter writer, ITabularObjectBase tableOrView)
		{
			writer.WriteLine("SELECT");

			WriteTableColumns( writer, tableOrView );

			writer.WriteLine("FROM ");
			WriteTableName(writer, tableOrView);
		}

		#endregion

		#region ---------------------INSERT---------------------

		public string Insert(ITabularObjectBase tableOrView)
		{
			StringWriter stringWriter = new StringWriter();

			Insert(stringWriter, tableOrView);

			return stringWriter.ToString();
		}

		public void Insert(TextWriter writer, ITabularObjectBase tableOrView)
		{
			writer.Write("INSERT INTO ");
			WriteTableName(writer, tableOrView);

			writer.Write("(");

			// get all columns that are "writable" including PKs that are not auto generated
			WriteTableColumns(writer, tableOrView);

			writer.WriteLine(")");
			writer.WriteLine("VALUES");
			writer.WriteLine("(");

			WriteTableColumnsForInsert(writer, tableOrView);
			
			writer.WriteLine(")");
		}
		#endregion

		#region ---------------------UPDATE---------------------

		public string Update(ITabularObjectBase tableOrView)
		{
			StringWriter stringWriter = new StringWriter();

			Update(stringWriter, tableOrView);

			return stringWriter.ToString();
		}

		public void Update(TextWriter writer, ITabularObjectBase tableOrView)
		{
			writer.WriteLine("UPDATE ");
			
			WriteTableName(writer, tableOrView);
			
			writer.WriteLine("SET ");
			
			WriteTableColumnsForUpdate(writer, tableOrView);
			
			writer.WriteLine("WHERE <Conditions>");
		}

		#endregion

		#region ---------------------DELETE---------------------

		public string Delete(ITabularObjectBase tableOrView)
		{
			StringWriter stringWriter = new StringWriter();

			Delete(stringWriter, tableOrView);

			return stringWriter.ToString();
		}

		public void Delete(TextWriter writer, ITabularObjectBase tableOrView)
		{
			writer.WriteLine("DELETE FROM ");
			
			WriteTableName( writer, tableOrView );
			
			writer.WriteLine("WHERE <Conditions>");
		}

		#endregion

		#region ---------------------SELECT COUNT---------------------

		public string SelectCount(ITabularObjectBase tableOrView)
		{
			StringWriter stringWriter = new StringWriter();

			SelectCount(stringWriter, tableOrView);

			return stringWriter.ToString();
		}

		public void SelectCount(TextWriter writer, ITabularObjectBase tableOrView)
		{
			writer.WriteLine("SELECT COUNT(*) FROM ");
			
			WriteTableName(writer, tableOrView);
		}
		#endregion

		#region ---------------------INTERNALS---------------------

		internal void WriteTableColumnsForInsert(TextWriter writer, ITabularObjectBase table)
		{
			int maxCol = table.Columns.Count - 1;

			for (int i = 0; i < table.Columns.Count; i++)
			{
				DataObjectBase cs = table.Columns[i];
				writer.WriteLine("\t" + string.Concat("<", cs.Name, cs.DataType , ">"));

				if (i < maxCol)
					writer.Write(",");
			}
		}

		internal void WriteTableColumnsForUpdate(TextWriter writer, ITabularObjectBase table)
		{
			int maxCol = table.Columns.Count - 1;

			for (int i = 0; i < table.Columns.Count; i++)
			{
				DataObjectBase cs = table.Columns[i];
				writer.WriteLine("\t" + string.Concat("[", cs.Name, "] = ", "<", cs.Name, cs.DataType, ">"));
				
				if (i < maxCol)
					writer.Write(",");
			}
		}

		internal void WriteTableColumns(TextWriter writer, ITabularObjectBase table)
		{
			int maxCol = table.Columns.Count - 1;

			for (int i = 0; i < table.Columns.Count; i++)
			{
				writer.WriteLine("\t" + string.Concat("[", table.Columns[i].Name, "]"));

				if (i < maxCol)
					writer.Write(",");
			}
		}

		internal void WriteTableName(TextWriter writer, ITabularObjectBase table)
		{
			writer.WriteLine("\t[{0}].[{1}].[{2}]", table.Database.Name, table.Owner, table.Name);
		}
		#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
Architect
France France
WPF and MVVM fan, I practice C # in all its forms from the beginning of the NET Framework without mentioning C ++ / MFC and other software packages such as databases, ASP, WCF, Web & Windows services, Application, and now Core and UWP.
In my wasted hours, I am guilty of having fathered C.B.R. and its cousins C.B.R. for WinRT and UWP on the Windows store.
But apart from that, I am a great handyman ... the house, a rocket stove to heat the jacuzzi and the last one: a wood oven for pizza, bread, and everything that goes inside

https://guillaumewaser.wordpress.com/
https://fouretcompagnie.wordpress.com/

Comments and Discussions