|
| 1 | +using System; |
| 2 | +using NHibernate.Criterion; |
| 3 | +using NHibernate.Engine; |
| 4 | +using NHibernate.SqlCommand; |
| 5 | +using NHibernate.Type; |
| 6 | +using System.Linq; |
| 7 | +using IQueryable = NHibernate.Persister.Entity.IQueryable; |
| 8 | + |
| 9 | +namespace NHibernate.Test.NHSpecificTest.GH948 |
| 10 | +{ |
| 11 | + using TThis = EntityProjection; |
| 12 | + |
| 13 | + [Serializable] |
| 14 | + public class EntityProjection : IProjection |
| 15 | + { |
| 16 | + private string _entityAlias; |
| 17 | + private string _columnAliasSuffix; |
| 18 | + public string _tableAlias; |
| 19 | + private IType[] _types; |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Root entity projection |
| 23 | + /// </summary> |
| 24 | + public EntityProjection():this(null, null) |
| 25 | + {} |
| 26 | + |
| 27 | + public EntityProjection(System.Type rootEntity, string entityAlias) |
| 28 | + { |
| 29 | + RootEntity = rootEntity; |
| 30 | + _entityAlias = entityAlias; |
| 31 | + } |
| 32 | + |
| 33 | + public bool FetchLazyProperties { get; set; } |
| 34 | + public bool IsReadOnly { get; set; } |
| 35 | + public bool Lazy { get; set; } |
| 36 | + |
| 37 | + internal string[] IdentifierColumnAliases { get; private set; } |
| 38 | + internal string[][] PropertyColumnAliases { get; private set; } |
| 39 | + internal IQueryable Persister { get; private set; } |
| 40 | + internal System.Type RootEntity { get; private set; } |
| 41 | + |
| 42 | + #region Configuration methods |
| 43 | + |
| 44 | + public TThis SetLazy(bool lazy = true) |
| 45 | + { |
| 46 | + Lazy = lazy; |
| 47 | + return this; |
| 48 | + } |
| 49 | + |
| 50 | + public TThis SetAllPropertyFetch(bool fetchLazyProperties = true) |
| 51 | + { |
| 52 | + FetchLazyProperties = fetchLazyProperties; |
| 53 | + return this; |
| 54 | + } |
| 55 | + |
| 56 | + public TThis SetReadonly(bool isReadOnly = true) |
| 57 | + { |
| 58 | + IsReadOnly = isReadOnly; |
| 59 | + return this; |
| 60 | + } |
| 61 | + |
| 62 | + #endregion Configuration methods |
| 63 | + |
| 64 | + #region IProjection implementation |
| 65 | + |
| 66 | + public string[] Aliases |
| 67 | + { |
| 68 | + get; |
| 69 | + private set; |
| 70 | + } |
| 71 | + |
| 72 | + bool IProjection.IsAggregate |
| 73 | + { |
| 74 | + get { return false; } |
| 75 | + } |
| 76 | + |
| 77 | + bool IProjection.IsGrouped |
| 78 | + { |
| 79 | + get { return false; } |
| 80 | + } |
| 81 | + |
| 82 | + IType[] IProjection.GetTypes(string alias, ICriteria criteria, ICriteriaQuery criteriaQuery) |
| 83 | + { |
| 84 | + return new[] { criteriaQuery.GetType(criteria, alias) }; |
| 85 | + } |
| 86 | + |
| 87 | + IType[] IProjection.GetTypes(ICriteria criteria, ICriteriaQuery criteriaQuery) |
| 88 | + { |
| 89 | + SetFields(criteria, criteriaQuery); |
| 90 | + return _types; |
| 91 | + } |
| 92 | + |
| 93 | + public string[] GetColumnAliases(int position, ICriteria criteria, ICriteriaQuery criteriaQuery) |
| 94 | + { |
| 95 | + SetFields(criteria, criteriaQuery); |
| 96 | + return Aliases; |
| 97 | + } |
| 98 | + |
| 99 | + public string[] GetColumnAliases(string alias, int position, ICriteria criteria, ICriteriaQuery criteriaQuery) |
| 100 | + { |
| 101 | + SetFields(criteria, criteriaQuery); |
| 102 | + return Aliases; |
| 103 | + } |
| 104 | + |
| 105 | + SqlString IProjection.ToSqlString(ICriteria criteria, int position, ICriteriaQuery criteriaQuery) |
| 106 | + { |
| 107 | + SetFields(criteria, criteriaQuery); |
| 108 | + |
| 109 | + string identifierSelectFragment = Persister.IdentifierSelectFragment(_tableAlias, _columnAliasSuffix); |
| 110 | + if (Lazy) |
| 111 | + return new SqlString(identifierSelectFragment); |
| 112 | + |
| 113 | + return new SqlString( |
| 114 | + string.Concat( |
| 115 | + identifierSelectFragment, |
| 116 | + Persister.PropertySelectFragment(_tableAlias, _columnAliasSuffix, FetchLazyProperties))); |
| 117 | + } |
| 118 | + |
| 119 | + SqlString IProjection.ToGroupSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery) |
| 120 | + { |
| 121 | + throw new NotImplementedException(); |
| 122 | + } |
| 123 | + |
| 124 | + TypedValue[] IProjection.GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery) |
| 125 | + { |
| 126 | + throw new NotImplementedException(); |
| 127 | + } |
| 128 | + |
| 129 | + #endregion IProjection implementation |
| 130 | + |
| 131 | + private void SetFields(ICriteria criteria, ICriteriaQuery criteriaQuery) |
| 132 | + { |
| 133 | + //Persister is required, so let's use it as "initialized marker" |
| 134 | + if (Persister != null) |
| 135 | + return; |
| 136 | + |
| 137 | + if (RootEntity == null) |
| 138 | + { |
| 139 | + RootEntity = criteria.GetRootEntityTypeIfAvailable(); |
| 140 | + } |
| 141 | + |
| 142 | + if (_entityAlias == null) |
| 143 | + { |
| 144 | + _entityAlias = criteria.Alias; |
| 145 | + } |
| 146 | + |
| 147 | + Persister = (IQueryable) criteriaQuery.Factory.GetEntityPersister(RootEntity.FullName); |
| 148 | + |
| 149 | + ICriteria subcriteria = criteria.GetCriteriaByAlias(_entityAlias); |
| 150 | + if (subcriteria == null) |
| 151 | + throw new HibernateException($"Criteria\\QueryOver alias '{_entityAlias}' for entity projection is not found."); |
| 152 | + |
| 153 | + _tableAlias = criteriaQuery.GetSQLAlias( |
| 154 | + subcriteria, |
| 155 | + Persister.IdentifierPropertyName ?? string.Empty); |
| 156 | + |
| 157 | + _columnAliasSuffix = criteriaQuery.GetIndexForAlias().ToString(); |
| 158 | + |
| 159 | + _types = new IType[] {new EntityProjectionType(this)}; |
| 160 | + |
| 161 | + IdentifierColumnAliases = Persister.GetIdentifierAliases(_columnAliasSuffix); |
| 162 | + |
| 163 | + PropertyColumnAliases = Lazy |
| 164 | + ? new string[][] { } |
| 165 | + : Enumerable.Range(0, Persister.PropertyNames.Length).Select(i => Persister.GetPropertyAliases(_columnAliasSuffix, i)).ToArray(); |
| 166 | + |
| 167 | + Aliases = IdentifierColumnAliases.Concat(PropertyColumnAliases.SelectMany(alias => alias)).ToArray(); |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments