From: <aye...@us...> - 2008-07-18 05:59:32
|
Revision: 3637 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=3637&view=rev Author: ayenderahien Date: 2008-07-18 05:59:41 +0000 (Fri, 18 Jul 2008) Log Message: ----------- Applying patch from Will Shaver for NH-1291, Example.Create with anonymous objects Modified Paths: -------------- trunk/nhibernate/src/NHibernate/Criterion/Example.cs Modified: trunk/nhibernate/src/NHibernate/Criterion/Example.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Criterion/Example.cs 2008-07-13 16:06:50 UTC (rev 3636) +++ trunk/nhibernate/src/NHibernate/Criterion/Example.cs 2008-07-18 05:59:41 UTC (rev 3637) @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Reflection; using Iesi.Collections.Generic; using NHibernate.Engine; using NHibernate.Persister.Entity; @@ -271,6 +272,30 @@ _selector.Include(value, name, type); } + private object[] GetPropertyValues(IEntityPersister persister, ICriteria criteria, ICriteriaQuery criteriaQuery) + { + System.Type type = _entity.GetType(); + if(type == persister.GetMappedClass(GetEntityMode(criteria, criteriaQuery))) //not using anon object + { + return persister.GetPropertyValues(_entity, GetEntityMode(criteria, criteriaQuery)); + } + ArrayList list = new ArrayList(); + for(int i = 0; i < persister.PropertyNames.Length; i++) + { + PropertyInfo pInfo = type.GetProperty(persister.PropertyNames[i]); + if(pInfo != null) + { + list.Add(pInfo.GetValue(_entity, null)); + } + else + { + list.Add(null); //to maintain same order as PropertyNames list + _excludedProperties.Add(persister.PropertyNames[i]); //exclude the properties that aren't in the anon object (duplicates ok) + } + } + return list.ToArray(); + } + public override SqlString ToSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery, IDictionary<string, IFilter> enabledFilters) { SqlStringBuilder builder = new SqlStringBuilder(); @@ -279,7 +304,7 @@ IEntityPersister meta = criteriaQuery.Factory.GetEntityPersister(criteriaQuery.GetEntityName(criteria)); String[] propertyNames = meta.PropertyNames; IType[] propertyTypes = meta.PropertyTypes; - object[] propertyValues = meta.GetPropertyValues(_entity, GetEntityMode(criteria, criteriaQuery)); + object[] propertyValues = GetPropertyValues(meta, criteria, criteriaQuery); for (int i = 0; i < propertyNames.Length; i++) { object propertyValue = propertyValues[i]; @@ -323,12 +348,14 @@ return builder.ToSqlString(); } + + //note: now that Criterion are adding typed values via ICriteriaQuery.AddUsedTypedValues this function is never called. public override TypedValue[] GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery) { IEntityPersister meta = criteriaQuery.Factory.GetEntityPersister(criteriaQuery.GetEntityName(criteria)); string[] propertyNames = meta.PropertyNames; IType[] propertyTypes = meta.PropertyTypes; - object[] values = meta.GetPropertyValues(_entity, GetEntityMode(criteria, criteriaQuery)); + object[] values = GetPropertyValues(meta, criteria, criteriaQuery); ArrayList list = new ArrayList(); for (int i = 0; i < propertyNames.Length; i++) @@ -366,7 +393,8 @@ EntityMode? result = meta.GuessEntityMode(_entity); if (!result.HasValue) { - throw new InvalidCastException(_entity.GetType().FullName); + return EntityMode.Poco; //this occurs for anon objects + //throw new InvalidCastException(_entity.GetType().FullName); } return result.Value; } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <fab...@us...> - 2009-11-22 15:18:04
|
Revision: 4853 http://nhibernate.svn.sourceforge.net/nhibernate/?rev=4853&view=rev Author: fabiomaulo Date: 2009-11-22 15:17:52 +0000 (Sun, 22 Nov 2009) Log Message: ----------- Merge r4852 (refactoring fix NH-2022) Modified Paths: -------------- trunk/nhibernate/src/NHibernate/Criterion/Example.cs Modified: trunk/nhibernate/src/NHibernate/Criterion/Example.cs =================================================================== --- trunk/nhibernate/src/NHibernate/Criterion/Example.cs 2009-11-22 15:14:55 UTC (rev 4852) +++ trunk/nhibernate/src/NHibernate/Criterion/Example.cs 2009-11-22 15:17:52 UTC (rev 4853) @@ -57,9 +57,9 @@ } //private static readonly IPropertySelector NotNull = new NotNullPropertySelector(); - private static readonly IPropertySelector NotNullOrEmptyString = new NotNullOrEmptyStringPropertySelector(); - private static readonly IPropertySelector All = new AllPropertySelector(); - private static readonly IPropertySelector NotNullOrZero = new NotNullOrZeroPropertySelector(); + protected static readonly IPropertySelector NotNullOrEmptyString = new NotNullOrEmptyStringPropertySelector(); + protected static readonly IPropertySelector All = new AllPropertySelector(); + protected static readonly IPropertySelector NotNullOrZero = new NotNullOrZeroPropertySelector(); /// <summary> /// Implementation of <see cref="IPropertySelector"/> that includes all @@ -476,21 +476,22 @@ builder.Add(" and "); } - ICriterion crit; - if (propertyValue != null) - { - bool isString = propertyValue is String; - crit = (_isLikeEnabled && isString) ? - (ICriterion) new LikeExpression(propertyName, propertyValue.ToString(), _matchMode, escapeCharacter, _isIgnoreCaseEnabled) : - new SimpleExpression(propertyName, propertyValue, " = ", _isIgnoreCaseEnabled && isString); - } - else - { - crit = new NullExpression(propertyName); - } + ICriterion crit = propertyValue != null + ? GetNotNullPropertyCriterion(propertyValue, propertyName) + : new NullExpression(propertyName); builder.Add(crit.ToSqlString(criteria, cq, enabledFilters)); } + protected virtual ICriterion GetNotNullPropertyCriterion(object propertyValue, string propertyName) + { + bool isString = propertyValue is string; + return (_isLikeEnabled && isString) + ? (ICriterion) + new LikeExpression(propertyName, propertyValue.ToString(), _matchMode, escapeCharacter, + _isIgnoreCaseEnabled) + : new SimpleExpression(propertyName, propertyValue, " = ", _isIgnoreCaseEnabled && isString); + } + protected void AppendComponentCondition( String path, object component, This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |