|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using NHibernate; |
| 5 | +using NHibernate.Criterion; |
| 6 | +using NHibernate.Engine; |
| 7 | +using NHibernate.Persister.Entity; |
| 8 | +using NHibernate.SqlCommand; |
| 9 | +using NHibernate.Type; |
| 10 | + |
| 11 | +namespace NHibernate.Criterion |
| 12 | +{ |
| 13 | + [Serializable] |
| 14 | + public abstract class BaseEntityProjection : IProjection |
| 15 | + { |
| 16 | + private string alias = null; |
| 17 | + private string[] columnAliases = null; |
| 18 | + private bool lazy = true; |
| 19 | + |
| 20 | + protected BaseEntityProjection(System.Type rootEntity, string alias) |
| 21 | + { |
| 22 | + this.RootEntity = rootEntity; |
| 23 | + this.alias = alias; |
| 24 | + } |
| 25 | + |
| 26 | + protected System.Type RootEntity |
| 27 | + { |
| 28 | + get; |
| 29 | + private set; |
| 30 | + } |
| 31 | + |
| 32 | + public BaseEntityProjection SetLazy(bool lazy) |
| 33 | + { |
| 34 | + this.lazy = lazy; |
| 35 | + |
| 36 | + return (this); |
| 37 | + } |
| 38 | + |
| 39 | + string[] IProjection.Aliases |
| 40 | + { |
| 41 | + get |
| 42 | + { |
| 43 | + return (this.columnAliases.ToArray()); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + TypedValue[] IProjection.GetTypedValues(ICriteria criteria, ICriteriaQuery criteriaQuery) |
| 48 | + { |
| 49 | + throw new NotImplementedException(); |
| 50 | + } |
| 51 | + |
| 52 | + IType[] IProjection.GetTypes(string alias, ICriteria criteria, ICriteriaQuery criteriaQuery) |
| 53 | + { |
| 54 | + return (new IType[] { criteriaQuery.GetType(criteria, alias) }); |
| 55 | + } |
| 56 | + |
| 57 | + private void SetFields(ICriteria criteria) |
| 58 | + { |
| 59 | + if (this.RootEntity == null) |
| 60 | + { |
| 61 | + this.RootEntity = criteria.GetRootEntityTypeIfAvailable(); |
| 62 | + } |
| 63 | + |
| 64 | + if (this.alias == null) |
| 65 | + { |
| 66 | + this.alias = criteria.Alias; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + IType[] IProjection.GetTypes(ICriteria criteria, ICriteriaQuery criteriaQuery) |
| 71 | + { |
| 72 | + this.SetFields(criteria); |
| 73 | + |
| 74 | + return (new IType[] { new ManyToOneType(this.RootEntity.FullName, this.lazy) }); |
| 75 | + } |
| 76 | + |
| 77 | + bool IProjection.IsAggregate |
| 78 | + { |
| 79 | + get { return (false); } |
| 80 | + } |
| 81 | + |
| 82 | + bool IProjection.IsGrouped |
| 83 | + { |
| 84 | + get { return (false); } |
| 85 | + } |
| 86 | + |
| 87 | + SqlString IProjection.ToGroupSqlString(ICriteria criteria, ICriteriaQuery criteriaQuery) |
| 88 | + { |
| 89 | + throw new NotImplementedException(); |
| 90 | + } |
| 91 | + |
| 92 | + SqlString IProjection.ToSqlString(ICriteria criteria, int position, ICriteriaQuery criteriaQuery) |
| 93 | + { |
| 94 | + this.SetFields(criteria); |
| 95 | + |
| 96 | + SqlStringBuilder builder = new SqlStringBuilder(); |
| 97 | + AbstractEntityPersister persister = criteriaQuery.Factory.TryGetEntityPersister(this.RootEntity.FullName) as AbstractEntityPersister; |
| 98 | + ICriteria subcriteria = criteria.GetCriteriaByAlias(this.alias); |
| 99 | + |
| 100 | + this.columnAliases = persister.GetIdentifierAliases(string.Empty); |
| 101 | + |
| 102 | + string[] columnNames = persister.GetPropertyColumnNames(persister.IdentifierPropertyName).Select(x => string.Concat(criteriaQuery.GetSQLAlias(subcriteria, persister.IdentifierPropertyName), ".", criteriaQuery.Factory.Dialect.QuoteForColumnName(x))).ToArray(); |
| 103 | + |
| 104 | + for (int i = 0; i < columnNames.Length; ++i) |
| 105 | + { |
| 106 | + builder.Add(String.Format("{0} as {1}", columnNames[i], this.columnAliases[i])); |
| 107 | + |
| 108 | + if (i < columnNames.Length - 1) |
| 109 | + { |
| 110 | + builder.Add(", "); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + return (builder.ToSqlString()); |
| 115 | + } |
| 116 | + |
| 117 | + public string[] GetColumnAliases(int position, ICriteria criteria, ICriteriaQuery criteriaQuery) |
| 118 | + { |
| 119 | + return ((this as IProjection).Aliases); |
| 120 | + } |
| 121 | + |
| 122 | + public string[] GetColumnAliases(string alias, int position, ICriteria criteria, ICriteriaQuery criteriaQuery) |
| 123 | + { |
| 124 | + return ((this as IProjection).Aliases); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments