|
From: <fab...@us...> - 2011-06-13 12:48:19
|
Revision: 5918
http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5918&view=rev
Author: fabiomaulo
Date: 2011-06-13 12:48:08 +0000 (Mon, 13 Jun 2011)
Log Message:
-----------
Refactoring: DRY CriteriaLoader
Modified Paths:
--------------
trunk/nhibernate/src/NHibernate/Loader/Criteria/CriteriaLoader.cs
trunk/nhibernate/src/NHibernate/Loader/Custom/CustomLoader.cs
Modified: trunk/nhibernate/src/NHibernate/Loader/Criteria/CriteriaLoader.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Loader/Criteria/CriteriaLoader.cs 2011-06-13 12:31:25 UTC (rev 5917)
+++ trunk/nhibernate/src/NHibernate/Loader/Criteria/CriteriaLoader.cs 2011-06-13 12:48:08 UTC (rev 5918)
@@ -187,12 +187,11 @@
public override ISqlCommand CreateSqlCommand(QueryParameters queryParameters, ISessionImplementor session)
{
- // NOTE: repeated code PrepareQueryCommand
// 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 HQL->SQL parsing, filters can be added as SQL_TOKEN/string and the SqlGenerator will not find it
+ // dynamic-filter parameters: during the Criteria parsing, filters can be added as string.
sqlString = ExpandDynamicFilterParameters(sqlString, parameterSpecs, session);
AdjustQueryParametersForSubSelectFetching(sqlString, parameterSpecs, session, queryParameters); // NOTE: see TODO below
@@ -202,7 +201,7 @@
// 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 SqlCommand.SqlCommandImpl(sqlString, parameterSpecs, queryParameters, session.Factory);
+ return new SqlCommandImpl(sqlString, parameterSpecs, queryParameters, session.Factory);
}
/// <summary>
@@ -219,29 +218,14 @@
/// <returns>A CommandWrapper wrapping an IDbCommand that is ready to be executed.</returns>
protected internal override IDbCommand PrepareQueryCommand(QueryParameters queryParameters, bool scroll, ISessionImplementor session)
{
- // NOTE: repeated code CreateSqlCommandInfo (here we are reusing some other variables)
- // A distinct-copy of parameter specifications collected during query construction
- var parameterSpecs = new HashSet<IParameterSpecification>(translator.CollectedParameterSpecifications);
- SqlString sqlString = SqlString.Copy();
+ var sqlCommand = (SqlCommandImpl)CreateSqlCommand(queryParameters, session);
+ var parameterSpecs = sqlCommand.Specifications;
+ var sqlString = sqlCommand.Query;
+ var sqlQueryParametersList = sqlCommand.SqlQueryParametersList;
- // 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, 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
-
- // 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.
- var sqlQueryParametersList = sqlString.GetParameters().ToList();
- SqlType[] parameterTypes = parameterSpecs.GetQueryParameterTypes(sqlQueryParametersList, session.Factory);
-
parameterSpecs.SetQueryParameterLocations(sqlQueryParametersList, session.Factory);
- IDbCommand command = session.Batcher.PrepareQueryCommand(CommandType.Text, sqlString, parameterTypes);
-
+ IDbCommand command = session.Batcher.PrepareQueryCommand(CommandType.Text, sqlString, sqlCommand.ParameterTypes);
try
{
RowSelection selection = queryParameters.RowSelection;
@@ -250,7 +234,7 @@
command.CommandTimeout = selection.Timeout;
}
- BindParametersValues(command, sqlQueryParametersList, parameterSpecs, queryParameters, session);
+ sqlCommand.Bind(command, sqlQueryParametersList, 0, session);
session.Batcher.ExpandQueryParameters(command, sqlString);
}
@@ -418,21 +402,5 @@
}
return sqlString;
}
-
- /// <summary>
- /// Bind all parameters values.
- /// </summary>
- /// <param name="command">The command where bind each value.</param>
- /// <param name="sqlQueryParametersList">The list of Sql query parameter in the exact sequence they are present in the query.</param>
- /// <param name="parameterSpecs">All parameter-specifications collected during query construction.</param>
- /// <param name="queryParameters">The encapsulation of the parameter values to be bound.</param>
- /// <param name="session">The session from where execute the query.</param>
- private void BindParametersValues(IDbCommand command, IList<Parameter> sqlQueryParametersList, IEnumerable<IParameterSpecification> parameterSpecs, QueryParameters queryParameters, ISessionImplementor session)
- {
- foreach (var parameterSpecification in parameterSpecs)
- {
- parameterSpecification.Bind(command, sqlQueryParametersList, queryParameters, session);
- }
- }
}
}
\ No newline at end of file
Modified: trunk/nhibernate/src/NHibernate/Loader/Custom/CustomLoader.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Loader/Custom/CustomLoader.cs 2011-06-13 12:31:25 UTC (rev 5917)
+++ trunk/nhibernate/src/NHibernate/Loader/Custom/CustomLoader.cs 2011-06-13 12:48:08 UTC (rev 5918)
@@ -358,7 +358,7 @@
var parameterSpecs = new HashSet<IParameterSpecification>(parametersSpecifications);
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 ?
//sqlString = ExpandDynamicFilterParameters(sqlString, parameterSpecs, session);
//AdjustQueryParametersForSubSelectFetching(sqlString, parameterSpecs, session, queryParameters); // NOTE: see TODO below
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <fab...@us...> - 2011-06-17 13:48:11
|
Revision: 5943
http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5943&view=rev
Author: fabiomaulo
Date: 2011-06-17 13:48:05 +0000 (Fri, 17 Jun 2011)
Log Message:
-----------
EntityLoader with parameter specifications
Modified Paths:
--------------
trunk/nhibernate/src/NHibernate/Loader/Entity/AbstractEntityLoader.cs
trunk/nhibernate/src/NHibernate/Loader/OuterJoinLoader.cs
Modified: trunk/nhibernate/src/NHibernate/Loader/Entity/AbstractEntityLoader.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Loader/Entity/AbstractEntityLoader.cs 2011-06-17 12:08:27 UTC (rev 5942)
+++ trunk/nhibernate/src/NHibernate/Loader/Entity/AbstractEntityLoader.cs 2011-06-17 13:48:05 UTC (rev 5943)
@@ -1,9 +1,11 @@
using System.Collections;
using System.Collections.Generic;
using System.Data;
-
+using System.Linq;
using NHibernate.Engine;
+using NHibernate.Param;
using NHibernate.Persister.Entity;
+using NHibernate.SqlCommand;
using NHibernate.Transform;
using NHibernate.Type;
@@ -16,13 +18,13 @@
{
protected static readonly IInternalLogger log = LoggerProvider.LoggerFor(typeof (AbstractEntityLoader));
protected readonly IOuterJoinLoadable persister;
- protected readonly IType uniqueKeyType;
protected readonly string entityName;
+ private IParameterSpecification[] parametersSpecifications;
- public AbstractEntityLoader(IOuterJoinLoadable persister, IType uniqueKeyType, ISessionFactoryImplementor factory,
+ protected AbstractEntityLoader(IOuterJoinLoadable persister, IType uniqueKeyType, ISessionFactoryImplementor factory,
IDictionary<string, IFilter> enabledFilters) : base(factory, enabledFilters)
{
- this.uniqueKeyType = uniqueKeyType;
+ UniqueKeyType = uniqueKeyType;
entityName = persister.EntityName;
this.persister = persister;
}
@@ -39,7 +41,7 @@
protected virtual object Load(ISessionImplementor session, object id, object optionalObject, object optionalId)
{
- IList list = LoadEntity(session, id, uniqueKeyType, optionalObject, entityName, optionalId, persister);
+ IList list = LoadEntity(session, id, UniqueKeyType, optionalObject, entityName, optionalId, persister);
if (list.Count == 1)
{
@@ -69,5 +71,36 @@
{
return row[row.Length - 1];
}
+
+ protected IType UniqueKeyType { get; private set; }
+
+ protected override void InitFromWalker(JoinWalker walker)
+ {
+ base.InitFromWalker(walker);
+ parametersSpecifications = CreateParameterSpecificationsAndAssignBackTrack(SqlString.GetParameters()).ToArray();
+ }
+
+ private IEnumerable<IParameterSpecification> CreateParameterSpecificationsAndAssignBackTrack(IEnumerable<Parameter> sqlPatameters)
+ {
+ var specifications = new List<IParameterSpecification>();
+ int position = 0;
+ var parameters = sqlPatameters.ToArray();
+ for (var sqlParameterPos = 0; sqlParameterPos < parameters.Length;)
+ {
+ var specification = new PositionalParameterSpecification(1, 0, position++) {ExpectedType = UniqueKeyType};
+ var paramTrackers = specification.GetIdsForBackTrack(Factory);
+ foreach (var paramTracker in paramTrackers)
+ {
+ parameters[sqlParameterPos++].BackTrack = paramTracker;
+ }
+ specifications.Add(specification);
+ }
+ return specifications;
+ }
+
+ protected override IEnumerable<IParameterSpecification> GetParameterSpecifications(QueryParameters queryParameters, ISessionFactoryImplementor sessionFactory)
+ {
+ return parametersSpecifications;
+ }
}
}
\ No newline at end of file
Modified: trunk/nhibernate/src/NHibernate/Loader/OuterJoinLoader.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Loader/OuterJoinLoader.cs 2011-06-17 12:08:27 UTC (rev 5942)
+++ trunk/nhibernate/src/NHibernate/Loader/OuterJoinLoader.cs 2011-06-17 13:48:05 UTC (rev 5943)
@@ -98,7 +98,7 @@
get { return collectionPersisters; }
}
- protected void InitFromWalker(JoinWalker walker)
+ protected virtual void InitFromWalker(JoinWalker walker)
{
persisters = walker.Persisters;
collectionPersisters = walker.CollectionPersisters;
@@ -108,7 +108,7 @@
collectionSuffixes = walker.CollectionSuffixes;
owners = walker.Owners;
collectionOwners = walker.CollectionOwners;
- sql = walker.SqlString;
+ sql = walker.SqlString.Compact();
aliases = walker.Aliases;
}
}
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|
|
From: <fab...@us...> - 2011-06-17 16:48:45
|
Revision: 5944
http://nhibernate.svn.sourceforge.net/nhibernate/?rev=5944&view=rev
Author: fabiomaulo
Date: 2011-06-17 16:48:39 +0000 (Fri, 17 Jun 2011)
Log Message:
-----------
Collections loaders with parameter specifications
Modified Paths:
--------------
trunk/nhibernate/src/NHibernate/Loader/Collection/CollectionLoader.cs
trunk/nhibernate/src/NHibernate/Loader/Entity/AbstractEntityLoader.cs
trunk/nhibernate/src/NHibernate/Loader/Entity/CollectionElementLoader.cs
trunk/nhibernate/src/NHibernate/Loader/OuterJoinLoader.cs
Modified: trunk/nhibernate/src/NHibernate/Loader/Collection/CollectionLoader.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Loader/Collection/CollectionLoader.cs 2011-06-17 13:48:05 UTC (rev 5943)
+++ trunk/nhibernate/src/NHibernate/Loader/Collection/CollectionLoader.cs 2011-06-17 16:48:39 UTC (rev 5944)
@@ -1,6 +1,9 @@
using System.Collections.Generic;
+using System.Linq;
using NHibernate.Engine;
+using NHibernate.Param;
using NHibernate.Persister.Collection;
+using NHibernate.SqlCommand;
using NHibernate.Type;
namespace NHibernate.Loader.Collection
@@ -13,6 +16,7 @@
public class CollectionLoader : OuterJoinLoader, ICollectionInitializer
{
private readonly IQueryableCollection collectionPersister;
+ private IParameterSpecification[] parametersSpecifications;
public CollectionLoader(IQueryableCollection persister, ISessionFactoryImplementor factory,
IDictionary<string, IFilter> enabledFilters) : base(factory, enabledFilters)
@@ -39,5 +43,29 @@
{
return GetType().FullName + '(' + collectionPersister.Role + ')';
}
+
+ protected virtual IEnumerable<IParameterSpecification> CreateParameterSpecificationsAndAssignBackTrack(IEnumerable<Parameter> sqlPatameters)
+ {
+ // This implementation can manage even the case of batch-loading
+ var specifications = new List<IParameterSpecification>();
+ int position = 0;
+ var parameters = sqlPatameters.ToArray();
+ for (var sqlParameterPos = 0; sqlParameterPos < parameters.Length; )
+ {
+ var specification = new PositionalParameterSpecification(1, 0, position++) { ExpectedType = KeyType };
+ var paramTrackers = specification.GetIdsForBackTrack(Factory);
+ foreach (var paramTracker in paramTrackers)
+ {
+ parameters[sqlParameterPos++].BackTrack = paramTracker;
+ }
+ specifications.Add(specification);
+ }
+ return specifications;
+ }
+
+ protected override IEnumerable<IParameterSpecification> GetParameterSpecifications(QueryParameters queryParameters, ISessionFactoryImplementor sessionFactory)
+ {
+ return parametersSpecifications ?? (parametersSpecifications = CreateParameterSpecificationsAndAssignBackTrack(SqlString.GetParameters()).ToArray());
+ }
}
}
\ No newline at end of file
Modified: trunk/nhibernate/src/NHibernate/Loader/Entity/AbstractEntityLoader.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Loader/Entity/AbstractEntityLoader.cs 2011-06-17 13:48:05 UTC (rev 5943)
+++ trunk/nhibernate/src/NHibernate/Loader/Entity/AbstractEntityLoader.cs 2011-06-17 16:48:39 UTC (rev 5944)
@@ -74,12 +74,6 @@
protected IType UniqueKeyType { get; private set; }
- protected override void InitFromWalker(JoinWalker walker)
- {
- base.InitFromWalker(walker);
- parametersSpecifications = CreateParameterSpecificationsAndAssignBackTrack(SqlString.GetParameters()).ToArray();
- }
-
private IEnumerable<IParameterSpecification> CreateParameterSpecificationsAndAssignBackTrack(IEnumerable<Parameter> sqlPatameters)
{
var specifications = new List<IParameterSpecification>();
@@ -100,7 +94,7 @@
protected override IEnumerable<IParameterSpecification> GetParameterSpecifications(QueryParameters queryParameters, ISessionFactoryImplementor sessionFactory)
{
- return parametersSpecifications;
+ return parametersSpecifications ?? (parametersSpecifications = CreateParameterSpecificationsAndAssignBackTrack(SqlString.GetParameters()).ToArray());
}
}
}
\ No newline at end of file
Modified: trunk/nhibernate/src/NHibernate/Loader/Entity/CollectionElementLoader.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Loader/Entity/CollectionElementLoader.cs 2011-06-17 13:48:05 UTC (rev 5943)
+++ trunk/nhibernate/src/NHibernate/Loader/Entity/CollectionElementLoader.cs 2011-06-17 16:48:39 UTC (rev 5944)
@@ -1,10 +1,12 @@
using System.Collections;
using System.Collections.Generic;
using System.Data;
-
+using System.Linq;
using NHibernate.Engine;
+using NHibernate.Param;
using NHibernate.Persister.Collection;
using NHibernate.Persister.Entity;
+using NHibernate.SqlCommand;
using NHibernate.Transform;
using NHibernate.Type;
using NHibernate.Util;
@@ -19,6 +21,7 @@
private readonly IType keyType;
private readonly IType indexType;
private readonly string entityName;
+ private IParameterSpecification[] parametersSpecifications;
public CollectionElementLoader(IQueryableCollection collectionPersister, ISessionFactoryImplementor factory,
IDictionary<string, IFilter> enabledFilters) : base(factory, enabledFilters)
@@ -39,6 +42,28 @@
log.Debug("Static select for entity " + entityName + ": " + SqlString);
}
+ private IEnumerable<IParameterSpecification> CreateParameterSpecificationsAndAssignBackTrack(IEnumerable<Parameter> sqlPatameters)
+ {
+ var specifications = new IParameterSpecification[]
+ {
+ new PositionalParameterSpecification(1, 0, 0) {ExpectedType = keyType},
+ new PositionalParameterSpecification(1, 0, 1) {ExpectedType = indexType},
+ };
+ Parameter[] parameters = sqlPatameters.ToArray();
+ int sqlParameterPos = 0;
+ IEnumerable<string> paramTrackers = specifications.SelectMany(specification => specification.GetIdsForBackTrack(Factory));
+ foreach (string paramTracker in paramTrackers)
+ {
+ parameters[sqlParameterPos++].BackTrack = paramTracker;
+ }
+ return specifications;
+ }
+
+ protected override IEnumerable<IParameterSpecification> GetParameterSpecifications(QueryParameters queryParameters, ISessionFactoryImplementor sessionFactory)
+ {
+ return parametersSpecifications ?? (parametersSpecifications = CreateParameterSpecificationsAndAssignBackTrack(SqlString.GetParameters()).ToArray());
+ }
+
protected override bool IsSingleRowLoader
{
get { return true; }
Modified: trunk/nhibernate/src/NHibernate/Loader/OuterJoinLoader.cs
===================================================================
--- trunk/nhibernate/src/NHibernate/Loader/OuterJoinLoader.cs 2011-06-17 13:48:05 UTC (rev 5943)
+++ trunk/nhibernate/src/NHibernate/Loader/OuterJoinLoader.cs 2011-06-17 16:48:39 UTC (rev 5944)
@@ -98,7 +98,7 @@
get { return collectionPersisters; }
}
- protected virtual void InitFromWalker(JoinWalker walker)
+ protected void InitFromWalker(JoinWalker walker)
{
persisters = walker.Persisters;
collectionPersisters = walker.CollectionPersisters;
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|