From: <fab...@us...> - 2009-05-06 22:59:57
|
Revision: 4256 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=4256&view=rev Author: fabiomaulo Date: 2009-05-06 22:59:51 +0000 (Wed, 06 May 2009) Log Message: ----------- Minor (reformatting, removed warning, completed function) Modified Paths: -------------- trunk/nhibernate/src/NHibernate/Hql/Ast/ANTLR/SqlGenerator.cs Modified: trunk/nhibernate/src/NHibernate/Hql/Ast/ANTLR/SqlGenerator.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Hql/Ast/ANTLR/SqlGenerator.cs 2009-05-06 21:50:40 UTC (rev 4255) +++ trunk/nhibernate/src/NHibernate/Hql/Ast/ANTLR/SqlGenerator.cs 2009-05-06 22:59:51 UTC (rev 4256) @@ -20,15 +20,8 @@ [CLSCompliant(false)] public partial class SqlGenerator : IErrorReporter { - /// <summary> - /// Handles parser errors. - /// </summary> - private readonly IParseErrorHandler _parseErrorHandler; + private readonly List<IParameterSpecification> collectedParameters = new List<IParameterSpecification>(); - private readonly ISessionFactoryImplementor _sessionFactory; - - private readonly List<IParameterSpecification> _collectedParameters = new List<IParameterSpecification>(); - /** * all append invocations on the buf should go through this Output instance variable. * The value of this variable may be temporarily substitued by sql function processing code @@ -36,131 +29,137 @@ * This is because sql function templates need arguments as seperate string chunks * that will be assembled into the target dialect-specific function call. */ - private ISqlWriter _writer; + private readonly List<ISqlWriter> outputStack = new List<ISqlWriter>(); - private readonly List<ISqlWriter> _outputStack = new List<ISqlWriter>(); + /// <summary> + /// Handles parser errors. + /// </summary> + private readonly IParseErrorHandler parseErrorHandler; - private readonly SqlStringBuilder _sqlStringBuilder = new SqlStringBuilder(); + private readonly ISessionFactoryImplementor sessionFactory; + private readonly SqlStringBuilder sqlStringBuilder = new SqlStringBuilder(); + private ISqlWriter writer; + public SqlGenerator(ISessionFactoryImplementor sfi, ITreeNodeStream input) : this(input) { - _parseErrorHandler = new ErrorCounter(); - _sessionFactory = sfi; - _writer = new DefaultWriter(this); + parseErrorHandler = new ErrorCounter(); + sessionFactory = sfi; + writer = new DefaultWriter(this); } - public override void ReportError(RecognitionException e) + public IParseErrorHandler ParseErrorHandler { - _parseErrorHandler.ReportError(e); // Use the delegate. + get { return parseErrorHandler; } } + #region IErrorReporter Members + + public override void ReportError(RecognitionException e) + { + parseErrorHandler.ReportError(e); // Use the delegate. + } + public void ReportError(String s) { - _parseErrorHandler.ReportError(s); // Use the delegate. + parseErrorHandler.ReportError(s); // Use the delegate. } public void ReportWarning(String s) { - _parseErrorHandler.ReportWarning(s); + parseErrorHandler.ReportWarning(s); } + #endregion + public SqlString GetSQL() { - return _sqlStringBuilder.ToSqlString(); + return sqlStringBuilder.ToSqlString(); } public IList<IParameterSpecification> GetCollectedParameters() { - return _collectedParameters; + return collectedParameters; } - public IParseErrorHandler ParseErrorHandler + private void Out(string s) { - get { return _parseErrorHandler; } + writer.Clause(s); } - private void Out(string s) + private void Out(SqlString s) { - _writer.Clause(s); + writer.Clause(s); } - private void Out(SqlString s) - { - _writer.Clause(s); - } - private void ParameterOut() { - _writer.Parameter(); + writer.Parameter(); } - /** - * Returns the last character written to the output, or -1 if there isn't one. - *//* - private int GetLastChar() + /// <summary> + /// Add a aspace if the previous token was not a space or a parenthesis. + /// </summary> + private void OptionalSpace() { - int len = _buf.Length; - if ( len == 0 ) - return -1; + int len = sqlStringBuilder.Count; + if (len == 0) + { + return; + } else - return _buf[len - 1]; - }*/ - - /** - * Add a aspace if the previous token was not a space or a parenthesis. - */ - void OptionalSpace() - {/* - int c = GetLastChar(); - switch ( c ) { - case -1: + { + string lastPart = sqlStringBuilder[len - 1].ToString(); + if (lastPart.Length == 0) + { return; - case ' ': - return; - case ')': - return; - case '(': - return; - default: - Out( " " ); - break; - }*/ + } + switch (lastPart[lastPart.Length - 1]) + { + case ' ': + return; + case ')': + return; + case '(': + return; + } + } Out(" "); } - private void Out(IASTNode n) + private void Out(IASTNode n) { if (n is ParameterNode) { ParameterOut(); } - else if ( n is SqlNode ) + else if (n is SqlNode) { - Out(((SqlNode) n).RenderText(_sessionFactory)); + Out(((SqlNode) n).RenderText(sessionFactory)); } - else + else { Out(n.Text); } - if ( n is ParameterNode ) + if (n is ParameterNode) { - _collectedParameters.Add( ( ( ParameterNode ) n ).HqlParameterSpecification ); + collectedParameters.Add(((ParameterNode) n).HqlParameterSpecification); } - else if ( n is IParameterContainer ) + else if (n is IParameterContainer) { - if ( ( ( IParameterContainer ) n ).HasEmbeddedParameters ) + if (((IParameterContainer) n).HasEmbeddedParameters) { - IParameterSpecification[] specifications = ( ( IParameterContainer ) n ).GetEmbeddedParameters(); - if ( specifications != null ) + IParameterSpecification[] specifications = ((IParameterContainer) n).GetEmbeddedParameters(); + if (specifications != null) { - _collectedParameters.AddRange(specifications); + collectedParameters.AddRange(specifications); } } } } - private void Separator(IASTNode n, String sep) + private void Separator(IASTNode n, String sep) { if (n.NextSibling != null) { @@ -168,22 +167,22 @@ } } - private static bool HasText(IASTNode a) + private static bool HasText(IASTNode a) { return !string.IsNullOrEmpty(a.Text); } - protected virtual void FromFragmentSeparator(IASTNode a) + protected virtual void FromFragmentSeparator(IASTNode a) { // check two "adjecent" nodes at the top of the from-clause tree IASTNode next = a.NextSibling; - if ( next == null || !HasText( a ) ) + if (next == null || !HasText(a)) { return; } - FromElement left = ( FromElement ) a; - FromElement right = ( FromElement ) next; + var left = (FromElement) a; + var right = (FromElement) next; /////////////////////////////////////////////////////////////////////// // HACK ALERT !!!!!!!!!!!!!!!!!!!!!!!!!!!! @@ -194,40 +193,39 @@ // // Essentially, look-ahead to the next FromElement that actually // writes something to the SQL - while ( right != null && !HasText( right ) ) + while (right != null && !HasText(right)) { - right = ( FromElement ) right.NextSibling; + right = (FromElement) right.NextSibling; } - if ( right == null ) + if (right == null) { return; } /////////////////////////////////////////////////////////////////////// - if ( !HasText( right ) ) + if (!HasText(right)) { return; } - if ( right.RealOrigin == left || - ( right.RealOrigin != null && right.RealOrigin == left.RealOrigin ) ) + if (right.RealOrigin == left || (right.RealOrigin != null && right.RealOrigin == left.RealOrigin)) { // right represents a joins originating from left; or // both right and left reprersent joins originating from the same FromElement - if ( right.JoinSequence != null && right.JoinSequence.IsThetaStyle) + if (right.JoinSequence != null && right.JoinSequence.IsThetaStyle) { - Out( ", " ); + Out(", "); } - else + else { - Out( " " ); + Out(" "); } } - else + else { // these are just two unrelated table references - Out( ", " ); + Out(", "); } } @@ -240,8 +238,8 @@ if (parent != null && HasText(parent)) { // again, both should be FromElements - FromElement left = (FromElement) parent; - FromElement right = (FromElement) d; + var left = (FromElement) parent; + var right = (FromElement) d; if (right.RealOrigin == left) { // right represents a joins originating from left... @@ -268,12 +266,12 @@ private SqlStringBuilder GetStringBuilder() { - return _sqlStringBuilder; + return sqlStringBuilder; } - private void BeginFunctionTemplate(IASTNode m, IASTNode i) + private void BeginFunctionTemplate(IASTNode m, IASTNode i) { - MethodNode methodNode = (MethodNode)m; + var methodNode = (MethodNode) m; ISQLFunction template = methodNode.SQLFunction; if (template == null) { @@ -284,126 +282,142 @@ else { // this function has a template -> redirect output and catch the arguments - _outputStack.Insert(0, _writer); - _writer = new FunctionArguments(); + outputStack.Insert(0, writer); + writer = new FunctionArguments(); } } - private void EndFunctionTemplate(IASTNode m) + private void EndFunctionTemplate(IASTNode m) { - MethodNode methodNode = ( MethodNode ) m; + var methodNode = (MethodNode) m; ISQLFunction template = methodNode.SQLFunction; - if ( template == null ) + if (template == null) { - Out(")"); + Out(")"); } - else + else { // this function has a template -> restore output, apply the template and write the result out - FunctionArguments functionArguments = ( FunctionArguments ) _writer; // TODO: Downcast to avoid using an interface? Yuck. - _writer = _outputStack[0]; - _outputStack.RemoveAt(0); - Out( template.Render( functionArguments.Args, _sessionFactory) ); + var functionArguments = (FunctionArguments) writer; // TODO: Downcast to avoid using an interface? Yuck. + writer = outputStack[0]; + outputStack.RemoveAt(0); + Out(template.Render(functionArguments.Args, sessionFactory)); } } - private void CommaBetweenParameters(String comma) + private void CommaBetweenParameters(String comma) { - _writer.CommaBetweenParameters(comma); + writer.CommaBetweenParameters(comma); } - /** - * Writes SQL fragments. - */ - interface ISqlWriter - { - void Clause(String clause); - void Clause(SqlString clause); - void Parameter(); - /** - * todo remove this hack - * The parameter is either ", " or " , ". This is needed to pass sql generating tests as the old - * sql generator uses " , " in the WHERE and ", " in SELECT. - * - * @param comma either " , " or ", " - */ - void CommaBetweenParameters(String comma); - } - /** - * SQL function processing code redirects generated SQL output to an instance of this class - * which catches function arguments. - */ - class FunctionArguments : ISqlWriter + #region Nested type: DefaultWriter + + /// <summary> + /// The default SQL writer. + /// </summary> + private class DefaultWriter : ISqlWriter { - private int argInd; - private readonly List<object> args = new List<object>(); + private readonly SqlGenerator generator; - public void Clause(String clause) + internal DefaultWriter(SqlGenerator generator) { - if (argInd == args.Count) - { - args.Add(clause); - } - else - { - args[argInd] = args[argInd] + clause; - } + this.generator = generator; } - public void Clause(SqlString clause) - { - this.Clause(clause.ToString()); - } + #region ISqlWriter Members + public void Clause(String clause) + { + generator.GetStringBuilder().Add(clause); + } + + public void Clause(SqlString clause) + { + generator.GetStringBuilder().Add(clause); + } + public void Parameter() { - args.Add(SqlCommand.Parameter.Placeholder); + generator.GetStringBuilder().AddParameter(); } - public void CommaBetweenParameters(String comma) + public void CommaBetweenParameters(String comma) { - ++argInd; + generator.GetStringBuilder().Add(comma); } + #endregion + } + + #endregion + + #region Nested type: FunctionArguments + + private class FunctionArguments : ISqlWriter + { + private readonly List<object> args = new List<object>(); + private int argInd; + public IList Args { get { return args; } } - } - /** - * The default SQL writer. - */ - class DefaultWriter : ISqlWriter - { - private readonly SqlGenerator _generator; + #region ISqlWriter Members - internal DefaultWriter(SqlGenerator generator) + public void Clause(string clause) { - _generator = generator; + if (argInd == args.Count) + { + args.Add(clause); + } + else + { + args[argInd] = args[argInd] + clause; + } } - public void Clause(String clause) + public void Clause(SqlString clause) { - _generator.GetStringBuilder().Add( clause ); + Clause(clause.ToString()); } - public void Clause(SqlString clause) - { - _generator.GetStringBuilder().Add(clause); - } - public void Parameter() { - _generator.GetStringBuilder().AddParameter(); + args.Add(SqlCommand.Parameter.Placeholder); } - - public void CommaBetweenParameters(String comma) + public void CommaBetweenParameters(string comma) { - _generator.GetStringBuilder().Add(comma); + ++argInd; } + + #endregion } + + #endregion + + #region Nested type: ISqlWriter + + /// <summary> + /// Writes SQL fragments. + /// </summary> + private interface ISqlWriter + { + void Clause(string clause); + void Clause(SqlString clause); + void Parameter(); + /** + * todo remove this hack + * The parameter is either ", " or " , ". This is needed to pass sql generating tests as the old + * sql generator uses " , " in the WHERE and ", " in SELECT. + * + * @param comma either " , " or ", " + */ + void CommaBetweenParameters(string comma); + } + + #endregion } -} +} \ No newline at end of file This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |