Click here to Skip to main content
15,891,136 members
Articles / Programming Languages / C#

SQL Parser

Rate me:
Please Sign up or sign in to vote.
4.86/5 (47 votes)
3 Feb 2009CDDL4 min read 307.8K   15.2K   165  
This article describes an implementation of a simple SQL parser which can be used to add or modify "where" and "order by" parts of SQL queries (PL/SQL).
using System;
using System.Collections.Generic;
using System.Text;

namespace Parser
{
	#region SimpleTwoWordTag

	/// <summary>
	/// The base class for such tags as Order By, Group By etc.
	/// </summary>
	internal abstract class SimpleTwoWordTag : SimpleOneWordTag
	{
		#region Methods

		#region Common

		/// <summary>
		/// Checks whether there is the tag at the specified position 
		/// in the specified sql.
		/// </summary>
		/// <returns>
		/// The position after the tag or -1 there is no tag at the position.
		/// </returns>
		protected override int MatchStart(string sql, int position)
		{
			return MatchStart(FirstWord, SecondWord, sql, position);
		}

		/// <summary>
		/// Writes the start of the tag.
		/// </summary>
		public override void WriteStart(StringBuilder output)
		{
			CheckInitialized();

			#region Check the parameters

			if (output == null)
				throw new ArgumentNullException();

			#endregion

			output.Append(FirstWord);
			output.Append(ParserBase.cWhiteSpace);
			output.Append(SecondWord);
		}

		#endregion

		#region Static

		/// <summary>
		/// Checks whether there is the tag at the specified position 
		/// in the specified sql.
		/// </summary>
		/// <name>The value of the Name property.</name>
		/// <returns>
		/// The position after the tag or -1 there is no tag at the position.
		/// </returns>
		internal static int MatchStart(string firstWord, string secondWord, string sql, int position)
		{
			#region Check the arguments

			ParserBase.CheckTextAndPositionArguments(sql, position);

			#endregion

			if (string.Compare(sql, position, firstWord, 0, firstWord.Length, true) != 0)
				return -1;

			position += firstWord.Length;

			ParserBase.SkipWhiteSpace(sql, ref position);
			if (position == sql.Length)
				return -1;

			if (string.Compare(sql, position, secondWord, 0, secondWord.Length, true) != 0)
				return -1;

			return position + secondWord.Length;
		}

		#endregion

		#endregion

		#region Properties

		/// <summary>
		/// Gets the first word of the tag.
		/// </summary>
		protected abstract string FirstWord { get; }

		/// <summary>
		/// Gets the second word of the tag.
		/// </summary>
		protected abstract string SecondWord { get; }

		#endregion
	}

	#endregion

	#region MatchSimpleTwoWordTagAttribute

	internal abstract class MatchSimpleTwoWordTagAttribute : MatchSimpleOneWordTagAttribute
	{
		#region Methods

		public override bool Match(string sql, int position)
		{
			return SimpleTwoWordTag.MatchStart(FirstWord, SecondWord, sql, position) >= 0;
		}

		#endregion

		#region Properties

		/// <summary>
		/// Gets the first word of the tag.
		/// </summary>
		protected abstract string FirstWord { get; }

		/// <summary>
		/// Gets the second word of the tag.
		/// </summary>
		protected abstract string SecondWord { get; }

		#endregion
	}

	#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 Common Development and Distribution License (CDDL)


Written By
Software Developer (Senior)
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions