From: <fab...@us...> - 2011-06-17 17:44:00
|
Revision: 5945 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5945&view=rev Author: fabiomaulo Date: 2011-06-17 17:43:54 +0000 (Fri, 17 Jun 2011) Log Message: ----------- Refactoring (DRY) Modified Paths: -------------- trunk/nhibernate/src/NHibernate/Hql/Ast/ANTLR/Loader/QueryLoader.cs trunk/nhibernate/src/NHibernate/Hql/Classic/QueryTranslator.cs trunk/nhibernate/src/NHibernate/Loader/Collection/CollectionLoader.cs trunk/nhibernate/src/NHibernate/Loader/Collection/SubselectCollectionLoader.cs trunk/nhibernate/src/NHibernate/Loader/Collection/SubselectOneToManyLoader.cs trunk/nhibernate/src/NHibernate/Loader/Criteria/CriteriaLoader.cs trunk/nhibernate/src/NHibernate/Loader/Custom/CustomLoader.cs trunk/nhibernate/src/NHibernate/Loader/Entity/AbstractEntityLoader.cs trunk/nhibernate/src/NHibernate/Loader/Entity/CollectionElementLoader.cs trunk/nhibernate/src/NHibernate/Loader/Loader.cs trunk/nhibernate/src/NHibernate/Loader/OuterJoinLoader.cs Modified: trunk/nhibernate/src/NHibernate/Hql/Ast/ANTLR/Loader/QueryLoader.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Hql/Ast/ANTLR/Loader/QueryLoader.cs 2011-06-17 16:48:39 UTC (rev 5944) +++ trunk/nhibernate/src/NHibernate/Hql/Ast/ANTLR/Loader/QueryLoader.cs 2011-06-17 17:43:54 UTC (rev 5945) @@ -433,26 +433,14 @@ return result; } - public override ISqlCommand CreateSqlCommand(QueryParameters queryParameters, ISessionImplementor session) + protected override void ResetEffectiveExpectedType(IEnumerable<IParameterSpecification> parameterSpecs, QueryParameters queryParameters) { - // A distinct-copy of parameter specifications collected during query construction - var parameterSpecs = new HashSet<IParameterSpecification>(_queryTranslator.CollectedParameterSpecifications); - SqlString sqlString = SqlString.Copy(); - - // dynamic-filter parameters: during the HQL->SQL parsing, filters can be added as SQL_TOKEN/string and the SqlGenerator will not find it - sqlString = ExpandDynamicFilterParameters(sqlString, parameterSpecs, session); - AdjustQueryParametersForSubSelectFetching(sqlString, parameterSpecs, queryParameters); // NOTE: see TODO below - - sqlString = AddLimitsParametersIfNeeded(sqlString, parameterSpecs, queryParameters, session); - // TODO: for sub-select fetching we have to try to assign the QueryParameter.ProcessedSQL here (with limits) but only after use IParameterSpecification for any kind of queries and taking care about the work done by SubselectClauseExtractor - - // The PreprocessSQL method can modify the SqlString but should never add parameters (or we have to override it) - sqlString = PreprocessSQL(sqlString, queryParameters, session.Factory.Dialect); - - // After the last modification to the SqlString we can collect all parameters types. parameterSpecs.ResetEffectiveExpectedType(queryParameters); + } - return new SqlCommandImpl(sqlString, parameterSpecs, queryParameters, session.Factory); + protected override IEnumerable<IParameterSpecification> GetParameterSpecifications() + { + return _queryTranslator.CollectedParameterSpecifications; } } } \ No newline at end of file Modified: trunk/nhibernate/src/NHibernate/Hql/Classic/QueryTranslator.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Hql/Classic/QueryTranslator.cs 2011-06-17 16:48:39 UTC (rev 5944) +++ trunk/nhibernate/src/NHibernate/Hql/Classic/QueryTranslator.cs 2011-06-17 17:43:54 UTC (rev 5945) @@ -301,26 +301,14 @@ get { return ((superQuery != null) ? superQuery.CollectedParameterSpecifications:Enumerable.Empty<IParameterSpecification>()).Concat(collectedParameters); } } - public override ISqlCommand CreateSqlCommand(QueryParameters queryParameters, ISessionImplementor session) + protected override void ResetEffectiveExpectedType(IEnumerable<IParameterSpecification> parameterSpecs, QueryParameters queryParameters) { - // A distinct-copy of parameter specifications collected during query construction - var parameterSpecs = new HashSet<IParameterSpecification>(CollectedParameterSpecifications); - SqlString sql = SqlString.Copy(); - - // dynamic-filter parameters: during the HQL->SQL parsing, filters can be added as SQL_TOKEN/string and the SqlGenerator will not find it - sql = ExpandDynamicFilterParameters(sql, parameterSpecs, session); - AdjustQueryParametersForSubSelectFetching(sql, parameterSpecs, queryParameters); // NOTE: see TODO below - - sql = AddLimitsParametersIfNeeded(sql, parameterSpecs, queryParameters, session); - // TODO: for sub-select fetching we have to try to assign the QueryParameter.ProcessedSQL here (with limits) but only after use IParameterSpecification for any kind of queries - - // The PreprocessSQL method can modify the SqlString but should never add parameters (or we have to override it) - sql = PreprocessSQL(sql, queryParameters, session.Factory.Dialect); - - // After the last modification to the SqlString we can collect all parameters types. parameterSpecs.ResetEffectiveExpectedType(queryParameters); + } - return new SqlCommandImpl(sql, parameterSpecs, queryParameters, session.Factory); + protected override IEnumerable<IParameterSpecification> GetParameterSpecifications() + { + return CollectedParameterSpecifications; } /// <summary> Modified: trunk/nhibernate/src/NHibernate/Loader/Collection/CollectionLoader.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Loader/Collection/CollectionLoader.cs 2011-06-17 16:48:39 UTC (rev 5944) +++ trunk/nhibernate/src/NHibernate/Loader/Collection/CollectionLoader.cs 2011-06-17 17:43:54 UTC (rev 5945) @@ -63,7 +63,7 @@ return specifications; } - protected override IEnumerable<IParameterSpecification> GetParameterSpecifications(QueryParameters queryParameters, ISessionFactoryImplementor sessionFactory) + protected override IEnumerable<IParameterSpecification> GetParameterSpecifications() { return parametersSpecifications ?? (parametersSpecifications = CreateParameterSpecificationsAndAssignBackTrack(SqlString.GetParameters()).ToArray()); } Modified: trunk/nhibernate/src/NHibernate/Loader/Collection/SubselectCollectionLoader.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Loader/Collection/SubselectCollectionLoader.cs 2011-06-17 16:48:39 UTC (rev 5944) +++ trunk/nhibernate/src/NHibernate/Loader/Collection/SubselectCollectionLoader.cs 2011-06-17 17:43:54 UTC (rev 5945) @@ -30,10 +30,9 @@ keys[i++] = entityKey.Identifier; } - namedParameters = queryParameters.NamedParameters; - // NH Different behavior: to deal with positionslParameter+NamedParameter+ParameterOfFilters parametersSpecifications = queryParameters.ProcessedSqlParameters.ToList(); + namedParameters = queryParameters.NamedParameters; types = queryParameters.PositionalParameterTypes; values = queryParameters.PositionalParameterValues; this.namedParameterLocMap = namedParameterLocMap; @@ -49,7 +48,7 @@ return namedParameterLocMap[name]; } - protected override IEnumerable<IParameterSpecification> GetParameterSpecifications(QueryParameters queryParameters, ISessionFactoryImplementor sessionFactory) + protected override IEnumerable<IParameterSpecification> GetParameterSpecifications() { return parametersSpecifications; } Modified: trunk/nhibernate/src/NHibernate/Loader/Collection/SubselectOneToManyLoader.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Loader/Collection/SubselectOneToManyLoader.cs 2011-06-17 16:48:39 UTC (rev 5944) +++ trunk/nhibernate/src/NHibernate/Loader/Collection/SubselectOneToManyLoader.cs 2011-06-17 17:43:54 UTC (rev 5945) @@ -33,9 +33,9 @@ keys[i++] = entityKey.Identifier; } - namedParameters = queryParameters.NamedParameters; // NH Different behavior: to deal with positionslParameter+NamedParameter+ParameterOfFilters parametersSpecifications = queryParameters.ProcessedSqlParameters.ToList(); + namedParameters = queryParameters.NamedParameters; types = queryParameters.PositionalParameterTypes; values = queryParameters.PositionalParameterValues; this.namedParameterLocMap = namedParameterLocMap; @@ -51,7 +51,7 @@ return namedParameterLocMap[name]; } - protected override IEnumerable<IParameterSpecification> GetParameterSpecifications(QueryParameters queryParameters, ISessionFactoryImplementor sessionFactory) + protected override IEnumerable<IParameterSpecification> GetParameterSpecifications() { return parametersSpecifications; } Modified: trunk/nhibernate/src/NHibernate/Loader/Criteria/CriteriaLoader.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Loader/Criteria/CriteriaLoader.cs 2011-06-17 16:48:39 UTC (rev 5944) +++ trunk/nhibernate/src/NHibernate/Loader/Criteria/CriteriaLoader.cs 2011-06-17 17:43:54 UTC (rev 5945) @@ -182,23 +182,9 @@ return customResultTransformer.TransformList(results); } - public override ISqlCommand CreateSqlCommand(QueryParameters queryParameters, ISessionImplementor session) + protected override IEnumerable<IParameterSpecification> GetParameterSpecifications() { - // A distinct-copy of parameter specifications collected during query construction - var parameterSpecs = new HashSet<IParameterSpecification>(translator.CollectedParameterSpecifications); - SqlString sqlString = SqlString.Copy(); - - // dynamic-filter parameters: during the Criteria parsing, filters can be added as string. - sqlString = ExpandDynamicFilterParameters(sqlString, parameterSpecs, session); - AdjustQueryParametersForSubSelectFetching(sqlString, parameterSpecs, queryParameters); // NOTE: see TODO below - - sqlString = AddLimitsParametersIfNeeded(sqlString, parameterSpecs, queryParameters, session); - // TODO: for sub-select fetching we have to try to assign the QueryParameter.ProcessedSQL here (with limits) but only after use IParameterSpecification for any kind of queries and taking care about the work done by SubselectClauseExtractor - - // The PreprocessSQL method can modify the SqlString but should never add parameters (or we have to override it) - sqlString = PreprocessSQL(sqlString, queryParameters, session.Factory.Dialect); - - return new SqlCommandImpl(sqlString, parameterSpecs, queryParameters, session.Factory); + return translator.CollectedParameterSpecifications; } public override int[] GetNamedParameterLocs(string name) Modified: trunk/nhibernate/src/NHibernate/Loader/Custom/CustomLoader.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Loader/Custom/CustomLoader.cs 2011-06-17 16:48:39 UTC (rev 5944) +++ trunk/nhibernate/src/NHibernate/Loader/Custom/CustomLoader.cs 2011-06-17 17:43:54 UTC (rev 5945) @@ -343,26 +343,14 @@ transformerAliases = aliases.ToArray(); } - public override ISqlCommand CreateSqlCommand(QueryParameters queryParameters, ISessionImplementor session) + protected override void ResetEffectiveExpectedType(IEnumerable<IParameterSpecification> parameterSpecs, QueryParameters queryParameters) { - // A distinct-copy of parameter specifications collected during query construction - var parameterSpecs = new HashSet<IParameterSpecification>(parametersSpecifications); - SqlString sqlString = SqlString.Copy(); - - // dynamic-filter parameters ? - //sqlString = ExpandDynamicFilterParameters(sqlString, parameterSpecs, session); - //AdjustQueryParametersForSubSelectFetching(sqlString, parameterSpecs, session, queryParameters); // NOTE: see TODO below - - sqlString = AddLimitsParametersIfNeeded(sqlString, parameterSpecs, queryParameters, session); - // TODO: for sub-select fetching we have to try to assign the QueryParameter.ProcessedSQL here (with limits) but only after use IParameterSpecification for any kind of queries and taking care about the work done by SubselectClauseExtractor - - // The PreprocessSQL method can modify the SqlString but should never add parameters (or we have to override it) - sqlString = PreprocessSQL(sqlString, queryParameters, session.Factory.Dialect); - - // After the last modification to the SqlString we can collect all parameters types. parameterSpecs.ResetEffectiveExpectedType(queryParameters); + } - return new SqlCommandImpl(sqlString, parameterSpecs, queryParameters, session.Factory); + protected override IEnumerable<IParameterSpecification> GetParameterSpecifications() + { + return parametersSpecifications; } public IType[] ResultTypes Modified: trunk/nhibernate/src/NHibernate/Loader/Entity/AbstractEntityLoader.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Loader/Entity/AbstractEntityLoader.cs 2011-06-17 16:48:39 UTC (rev 5944) +++ trunk/nhibernate/src/NHibernate/Loader/Entity/AbstractEntityLoader.cs 2011-06-17 17:43:54 UTC (rev 5945) @@ -92,7 +92,7 @@ return specifications; } - protected override IEnumerable<IParameterSpecification> GetParameterSpecifications(QueryParameters queryParameters, ISessionFactoryImplementor sessionFactory) + protected override IEnumerable<IParameterSpecification> GetParameterSpecifications() { return parametersSpecifications ?? (parametersSpecifications = CreateParameterSpecificationsAndAssignBackTrack(SqlString.GetParameters()).ToArray()); } Modified: trunk/nhibernate/src/NHibernate/Loader/Entity/CollectionElementLoader.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Loader/Entity/CollectionElementLoader.cs 2011-06-17 16:48:39 UTC (rev 5944) +++ trunk/nhibernate/src/NHibernate/Loader/Entity/CollectionElementLoader.cs 2011-06-17 17:43:54 UTC (rev 5945) @@ -59,7 +59,7 @@ return specifications; } - protected override IEnumerable<IParameterSpecification> GetParameterSpecifications(QueryParameters queryParameters, ISessionFactoryImplementor sessionFactory) + protected override IEnumerable<IParameterSpecification> GetParameterSpecifications() { return parametersSpecifications ?? (parametersSpecifications = CreateParameterSpecificationsAndAssignBackTrack(SqlString.GetParameters()).ToArray()); } Modified: trunk/nhibernate/src/NHibernate/Loader/Loader.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Loader/Loader.cs 2011-06-17 16:48:39 UTC (rev 5944) +++ trunk/nhibernate/src/NHibernate/Loader/Loader.cs 2011-06-17 17:43:54 UTC (rev 5945) @@ -1641,15 +1641,11 @@ public virtual ISqlCommand CreateSqlCommand(QueryParameters queryParameters, ISessionImplementor session) { - // The implementation of this method is itended to be used just for "internal" command (not for queries coming from users) - // Internally NH creates various commands, all using just positional-parameters, to then potentially apply dynamic-filters and pagination. - // In practice this method should be overriden by those loaders representing a user-query as CriteriaLoader, QueryLoader, classic QueryTraslator, CustomLoader (for custmon SQL queries). - // A distinct-copy of parameter specifications collected during query construction - var parameterSpecs = new HashSet<IParameterSpecification>(GetParameterSpecifications(queryParameters, session.Factory)); + var parameterSpecs = new HashSet<IParameterSpecification>(GetParameterSpecifications()); SqlString sqlString = SqlString.Copy(); - // dynamic-filter parameters: during the HQL->SQL parsing, filters can be added as SQL_TOKEN/string and the SqlGenerator will not find it + // dynamic-filter parameters: during the createion of the SqlString of allLoader implementation, filters can be added as SQL_TOKEN/string for this reason we have to re-parse the SQL. sqlString = ExpandDynamicFilterParameters(sqlString, parameterSpecs, session); AdjustQueryParametersForSubSelectFetching(sqlString, parameterSpecs, queryParameters); // NOTE: see TODO below @@ -1659,25 +1655,19 @@ // The PreprocessSQL method can modify the SqlString but should never add parameters (or we have to override it) sqlString = PreprocessSQL(sqlString, queryParameters, session.Factory.Dialect); + // After the last modification to the SqlString we can collect all parameters types (there are cases where we can't infer the type during the creation of the query) + ResetEffectiveExpectedType(parameterSpecs, queryParameters); + return new SqlCommandImpl(sqlString, parameterSpecs, queryParameters, session.Factory); } - protected virtual IEnumerable<IParameterSpecification> GetParameterSpecifications(QueryParameters queryParameters, ISessionFactoryImplementor sessionFactory) + protected virtual void ResetEffectiveExpectedType(IEnumerable<IParameterSpecification> parameterSpecs, QueryParameters queryParameters) { - // TODO FM: remove this implementation and put an abstract ParameterSpecifications in the Loader (each concrete implementation have to expose it) => NH1990, SubselectFetchFixture - var positionalSpecifications = queryParameters.PositionalParameterTypes.Select((t, i) => (IParameterSpecification)new PositionalParameterSpecification(1, 0, i) { ExpectedType = t }); - var namedSpecifications = queryParameters.NamedParameters != null ? queryParameters.NamedParameters.Select(np => (IParameterSpecification)new NamedParameterSpecification(1, 0, np.Key) { ExpectedType = np.Value.Type }): Enumerable.Empty<IParameterSpecification>(); - var specifications= positionalSpecifications.Concat(namedSpecifications).ToList(); - var parameters = SqlString.GetParameters().ToArray(); - var sqlParameterPos = 0; - var paramTrackers = specifications.SelectMany(specification => specification.GetIdsForBackTrack(sessionFactory)); - foreach (var paramTracker in paramTrackers) - { - parameters[sqlParameterPos++].BackTrack = paramTracker; - } - return specifications; + // Have to be overridden just by those loaders that can't infer the type during the parse process } + protected abstract IEnumerable<IParameterSpecification> GetParameterSpecifications(); + protected void AdjustQueryParametersForSubSelectFetching(SqlString sqlString, IEnumerable<IParameterSpecification> parameterSpecs, QueryParameters queryParameters) { queryParameters.ProcessedSql = sqlString; Modified: trunk/nhibernate/src/NHibernate/Loader/OuterJoinLoader.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Loader/OuterJoinLoader.cs 2011-06-17 16:48:39 UTC (rev 5944) +++ trunk/nhibernate/src/NHibernate/Loader/OuterJoinLoader.cs 2011-06-17 17:43:54 UTC (rev 5945) @@ -14,7 +14,7 @@ /// Generates an SQL select string containing all properties of those classes. /// Tablse are joined using an ANSI-style left outer join. /// </remarks> - public class OuterJoinLoader : BasicLoader + public abstract class OuterJoinLoader : BasicLoader { // Having these fields as protected prevents CLS compliance, so they are // private in NHibernate, and setters are created for the relevant @@ -32,7 +32,7 @@ private readonly IDictionary<string, IFilter> enabledFilters; - public OuterJoinLoader(ISessionFactoryImplementor factory, IDictionary<string, IFilter> enabledFilters) + protected OuterJoinLoader(ISessionFactoryImplementor factory, IDictionary<string, IFilter> enabledFilters) : base(factory) { this.enabledFilters = enabledFilters; This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |