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. |
From: <pa...@us...> - 2011-04-12 05:28:48
|
Revision: 5671 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5671&view=rev Author: patearl Date: 2011-04-12 05:28:42 +0000 (Tue, 12 Apr 2011) Log Message: ----------- Hql: Don't try to generate limit clauses when we don't need them. 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 2011-04-12 04:41:51 UTC (rev 5670) +++ trunk/nhibernate/src/NHibernate/Hql/Ast/ANTLR/SqlGenerator.cs 2011-04-12 05:28:42 UTC (rev 5671) @@ -321,7 +321,8 @@ QueryWriter queryWriter = ((QueryWriter) writer); SqlString sqlString = queryWriter.ToSqlString(); - sqlString = sessionFactory.Dialect.GetLimitString(sqlString, queryWriter.Skip ?? 0, queryWriter.Take ?? int.MaxValue); + if (queryWriter.Take.HasValue || queryWriter.Skip.HasValue) + sqlString = sessionFactory.Dialect.GetLimitString(sqlString, queryWriter.Skip ?? 0, queryWriter.Take ?? int.MaxValue); writer = outputStack[0]; outputStack.RemoveAt(0); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fab...@us...> - 2011-05-22 11:02:32
|
Revision: 5851 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5851&view=rev Author: fabiomaulo Date: 2011-05-22 11:02:26 +0000 (Sun, 22 May 2011) Log Message: ----------- Minor (reformat) 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 2011-05-22 10:59:47 UTC (rev 5850) +++ trunk/nhibernate/src/NHibernate/Hql/Ast/ANTLR/SqlGenerator.cs 2011-05-22 11:02:26 UTC (rev 5851) @@ -316,36 +316,38 @@ writer = new QueryWriter(); } - private void EndQuery() - { - QueryWriter queryWriter = ((QueryWriter) writer); - SqlString sqlString = queryWriter.ToSqlString(); + private void EndQuery() + { + var queryWriter = ((QueryWriter) writer); + SqlString sqlString = queryWriter.ToSqlString(); - if (queryWriter.Take.HasValue || queryWriter.Skip.HasValue) - sqlString = sessionFactory.Dialect.GetLimitString(sqlString, queryWriter.Skip ?? 0, queryWriter.Take ?? int.MaxValue); + if (queryWriter.Take.HasValue || queryWriter.Skip.HasValue) + { + sqlString = sessionFactory.Dialect.GetLimitString(sqlString, queryWriter.Skip ?? 0, queryWriter.Take ?? int.MaxValue); + } - writer = outputStack[0]; - outputStack.RemoveAt(0); - Out(sqlString); - } + writer = outputStack[0]; + outputStack.RemoveAt(0); + Out(sqlString); + } - private void Skip(IASTNode node) - { - if(node is ParameterNode) - { - throw new NotSupportedException("Parameter limits is not supported yet."); - } - ((QueryWriter) writer).Skip = Convert.ToInt32(node.Text); - } + private void Skip(IASTNode node) + { + if (node is ParameterNode) + { + throw new NotSupportedException("Parameter limits is not supported yet."); + } + ((QueryWriter) writer).Skip = Convert.ToInt32(node.Text); + } - private void Take(IASTNode node) - { - if (node is ParameterNode) - { - throw new NotSupportedException("Parameter limits is not supported yet."); - } - ((QueryWriter)writer).Take = Convert.ToInt32(node.Text); - } + private void Take(IASTNode node) + { + if (node is ParameterNode) + { + throw new NotSupportedException("Parameter limits is not supported yet."); + } + ((QueryWriter) writer).Take = Convert.ToInt32(node.Text); + } #region Nested type: DefaultWriter This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fab...@us...> - 2011-05-31 17:08:13
|
Revision: 5899 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5899&view=rev Author: fabiomaulo Date: 2011-05-31 17:08:07 +0000 (Tue, 31 May 2011) Log Message: ----------- Removed not needed code to prevent addition of meaningless Parameter 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 2011-05-31 15:51:16 UTC (rev 5898) +++ trunk/nhibernate/src/NHibernate/Hql/Ast/ANTLR/SqlGenerator.cs 2011-05-31 17:08:07 UTC (rev 5899) @@ -418,11 +418,6 @@ generator.GetStringBuilder().Add(clause); } - public void Parameter() - { - generator.GetStringBuilder().AddParameter(); - } - public void PushParameter(Parameter parameter) { generator.GetStringBuilder().Add(parameter); @@ -464,11 +459,6 @@ builder.Add(clause); } - public void Parameter() - { - builder.AddParameter(); - } - public void PushParameter(Parameter parameter) { builder.Add(parameter); @@ -533,18 +523,6 @@ } } - public void Parameter() - { - if (argInd == args.Count) - { - args.Add(new SqlString(SqlCommand.Parameter.Placeholder)); - } - else - { - args[argInd] = args[argInd].Append(new SqlString(SqlCommand.Parameter.Placeholder)); - } - } - public void PushParameter(Parameter parameter) { if (argInd == args.Count) @@ -576,7 +554,6 @@ { void Clause(string clause); void Clause(SqlString clause); - void Parameter(); void PushParameter(Parameter parameter); /** * todo remove this hack This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <pa...@us...> - 2011-06-04 05:07:51
|
Revision: 5903 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5903&view=rev Author: patearl Date: 2011-06-04 05:07:45 +0000 (Sat, 04 Jun 2011) Log Message: ----------- Hql: Minor simplification to limit string generation that keeps the bulk of the logic in one place in the code and makes it easier to convert parameter limits into constant limits later. 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 2011-06-02 15:16:15 UTC (rev 5902) +++ trunk/nhibernate/src/NHibernate/Hql/Ast/ANTLR/SqlGenerator.cs 2011-06-04 05:07:45 UTC (rev 5903) @@ -325,23 +325,6 @@ private SqlString GetSqlStringWithLimitsIfNeeded(QueryWriter queryWriter) { - SqlString sqlString = queryWriter.ToSqlString(); - var skipIsParameter = queryWriter.SkipParameter != null; - var takeIsParameter = queryWriter.TakeParameter != null; - var hqlQueryHasLimits = queryWriter.Take.HasValue || queryWriter.Skip.HasValue || skipIsParameter || takeIsParameter; - if (!hqlQueryHasLimits) - { - return sqlString; - } - - var dialect = sessionFactory.Dialect; - - // Skip-Take in HQL should be supported just for Dialect supporting variable limits at least when users use parameters for skip-take. - if (!dialect.SupportsVariableLimit && (skipIsParameter || takeIsParameter)) - { - throw new NotSupportedException("The dialect " + dialect.GetType().FullName + " does not supports variable limits"); - } - Parameter skipParameter = null; Parameter takeParameter = null; if(queryWriter.SkipParameter != null) @@ -358,8 +341,9 @@ } // We allow the user to specify either constants or parameters for their limits. - // The dialect can move the given parameters where he need, what it can't do is generates new parameters loosing the BackTrack. - return dialect.GetLimitString(sqlString, + // The dialect can move the given parameters where he need, what it can't do is generates new parameters, losing the BackTrack. + var dialect = sessionFactory.Dialect; + return dialect.GetLimitString(queryWriter.ToSqlString(), queryWriter.Skip.HasValue ? (int?) dialect.GetOffsetValue(queryWriter.Skip.Value) : null, queryWriter.Take.HasValue ? (int?) dialect.GetLimitValue(queryWriter.Skip ?? 0, queryWriter.Take.Value) : null, skipParameter, This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |