Skip to content

Commit 7bc3fdd

Browse files
author
g.yakimov
committed
special handling of with clauses
1 parent e206809 commit 7bc3fdd

16 files changed

+67
-54
lines changed

src/NHibernate.Test/App.config

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</configSections>
88

99
<connectionStrings>
10-
<add name="TestConnectionString" connectionString="Server=localhost\sqlexpress;Database=nhibernate;Integrated Security=SSPI" />
10+
<add name="TestConnectionString" connectionString="Server=localhost;Database=nhibernate;Integrated Security=SSPI" />
1111
<add name="DummyConnectionString" connectionString="TestConnectionString-TestConnectionString" />
1212
</connectionStrings>
1313

@@ -31,7 +31,7 @@
3131
<property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property>
3232

3333
<property name="connection.driver_class">NHibernate.Driver.Sql2008ClientDriver</property> <!-- Shouldn't be necessary, but is required by some tests -->
34-
<property name="connection.connection_string">Server=localhost\sqlexpress;Database=nhibernate;Integrated Security=SSPI</property>
34+
<property name="connection.connection_string">Server=localhost;Database=nhibernate;Integrated Security=SSPI</property>
3535
<property name="connection.provider">NHibernate.Test.DebugConnectionProvider, NHibernate.Test</property>
3636
<property name="connection.isolation">ReadCommitted</property> <!-- See System.Data.IsolationLevel for valid values -->
3737

src/NHibernate/Hql/Ast/ANTLR/Tree/AssignmentSpecification.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public AssignmentSpecification(IASTNode eq, IQueryable persister)
6161
}
6262
else
6363
{
64-
temp.Add(persister.GetSubclassTableName(persister.GetSubclassPropertyTableNumber(propertyPath)));
64+
temp.Add(persister.GetSubclassTableName(persister.GetSubclassPropertyTableNumber(propertyPath, false)));
6565
}
6666
_tableNames = new HashSet<string>(temp);
6767

src/NHibernate/Hql/Ast/ANTLR/Tree/ComponentJoin.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,14 @@ public bool TryToType(string propertyName, out IType type)
150150
return fromElementType.GetBasePropertyMapping().TryToType(GetPropertyPath(propertyName), out type);
151151
}
152152

153-
public string[] ToColumns(string alias, string propertyName)
153+
public string[] ToColumns(string alias, string propertyName, bool useLastIndex = false)
154154
{
155-
return fromElementType.GetBasePropertyMapping().ToColumns(alias, GetPropertyPath(propertyName));
155+
return fromElementType.GetBasePropertyMapping().ToColumns(alias, GetPropertyPath(propertyName), useLastIndex);
156156
}
157157

158-
public string[] ToColumns(string propertyName)
158+
public string[] ToColumns(string propertyName, bool useLastIndex = false)
159159
{
160-
return fromElementType.GetBasePropertyMapping().ToColumns(GetPropertyPath(propertyName));
160+
return fromElementType.GetBasePropertyMapping().ToColumns(GetPropertyPath(propertyName), useLastIndex);
161161
}
162162

163163
#endregion

src/NHibernate/Hql/Ast/ANTLR/Tree/IntoClause.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ private bool IsSuperclassProperty(string propertyName)
214214
//
215215
// we may want to disallow it for discrim-subclass just for
216216
// consistency-sake (currently does not work anyway)...
217-
return _persister.GetSubclassPropertyTableNumber(propertyName) != 0;
217+
return _persister.GetSubclassPropertyTableNumber(propertyName, false) != 0;
218218
}
219219

220220
/// <summary>
@@ -263,4 +263,4 @@ private static bool AreSqlTypesCompatible(SqlType target, SqlType source)
263263
return target.Equals(source);
264264
}
265265
}
266-
}
266+
}

src/NHibernate/Loader/Criteria/CriteriaQueryTranslator.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using NHibernate.Type;
1414
using NHibernate.Util;
1515
using IQueryable = NHibernate.Persister.Entity.IQueryable;
16+
using static NHibernate.Impl.CriteriaImpl;
1617

1718
namespace NHibernate.Loader.Criteria
1819
{
@@ -766,7 +767,15 @@ private bool TryGetColumns(ICriteria subcriteria, string path, bool verifyProper
766767
return false;
767768
}
768769

769-
columns = propertyMapping.ToColumns(GetSQLAlias(pathCriteria), propertyName);
770+
// here we can check if the condition belongs to a with clause
771+
bool useLastIndex = false;
772+
var withClause = pathCriteria as Subcriteria != null ? ((Subcriteria) pathCriteria).WithClause as SimpleExpression : null;
773+
if (withClause != null && withClause.PropertyName == propertyName)
774+
{
775+
useLastIndex = true;
776+
}
777+
778+
columns = propertyMapping.ToColumns(GetSQLAlias(pathCriteria), propertyName, useLastIndex);
770779
return true;
771780
}
772781

src/NHibernate/Persister/Collection/AbstractCollectionPersister.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1386,7 +1386,7 @@ public bool IsManyToManyFiltered(IDictionary<string, IFilter> enabledFilters)
13861386
return IsManyToMany && (manyToManyWhereString != null || manyToManyFilterHelper.IsAffectedBy(enabledFilters));
13871387
}
13881388

1389-
public string[] ToColumns(string alias, string propertyName)
1389+
public string[] ToColumns(string alias, string propertyName, bool useLastIndex = false)
13901390
{
13911391
if ("index".Equals(propertyName))
13921392
{
@@ -1397,10 +1397,10 @@ public string[] ToColumns(string alias, string propertyName)
13971397

13981398
return StringHelper.Qualify(alias, indexColumnNames);
13991399
}
1400-
return elementPropertyMapping.ToColumns(alias, propertyName);
1400+
return elementPropertyMapping.ToColumns(alias, propertyName, useLastIndex);
14011401
}
14021402

1403-
public string[] ToColumns(string propertyName)
1403+
public string[] ToColumns(string propertyName, bool useLastIndex = false)
14041404
{
14051405
if ("index".Equals(propertyName))
14061406
{
@@ -1412,7 +1412,7 @@ public string[] ToColumns(string propertyName)
14121412
return indexColumnNames;
14131413
}
14141414

1415-
return elementPropertyMapping.ToColumns(propertyName);
1415+
return elementPropertyMapping.ToColumns(propertyName, useLastIndex);
14161416
}
14171417

14181418
protected abstract SqlCommandInfo GenerateDeleteString();

src/NHibernate/Persister/Collection/CollectionPropertyMapping.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public bool TryToType(string propertyName, out IType type)
5757
}
5858
}
5959

60-
public string[] ToColumns(string alias, string propertyName)
60+
public string[] ToColumns(string alias, string propertyName, bool useLastIndex = false)
6161
{
6262
string[] cols;
6363
switch (propertyName)
@@ -107,7 +107,7 @@ public string[] ToColumns(string alias, string propertyName)
107107
}
108108
}
109109

110-
public string[] ToColumns(string propertyName)
110+
public string[] ToColumns(string propertyName, bool useLastIndex = false)
111111
{
112112
throw new System.NotSupportedException("References to collections must be define a SQL alias");
113113
}
@@ -117,4 +117,4 @@ public IType Type
117117
get { return memberPersister.CollectionType; }
118118
}
119119
}
120-
}
120+
}

src/NHibernate/Persister/Collection/ElementPropertyMapping.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public bool TryToType(string propertyName, out IType outType)
4747
}
4848
}
4949

50-
public string[] ToColumns(string alias, string propertyName)
50+
public string[] ToColumns(string alias, string propertyName, bool useLastIndex)
5151
{
5252
if (propertyName == null || "id".Equals(propertyName))
5353
{
@@ -59,7 +59,7 @@ public string[] ToColumns(string alias, string propertyName)
5959
}
6060
}
6161

62-
public string[] ToColumns(string propertyName)
62+
public string[] ToColumns(string propertyName, bool useLastIndex)
6363
{
6464
throw new System.NotSupportedException("References to collections must be define a SQL alias");
6565
}
@@ -71,4 +71,4 @@ public IType Type
7171

7272
#endregion
7373
}
74-
}
74+
}

src/NHibernate/Persister/Entity/AbstractEntityPersister.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1118,9 +1118,9 @@ protected virtual bool IsIdOfTable(int property, int table)
11181118
return false;
11191119
}
11201120

1121-
protected abstract int GetSubclassPropertyTableNumber(int i);
1121+
protected abstract int GetSubclassPropertyTableNumber(int i, bool useLastIndex);
11221122

1123-
internal int GetSubclassPropertyTableNumber(string propertyName, string entityName)
1123+
internal int GetSubclassPropertyTableNumber(string propertyName, string entityName, bool useLastIndex = false)
11241124
{
11251125
var type = propertyMapping.ToType(propertyName);
11261126
if (type.IsAssociationType && ((IAssociationType) type).UseLHSPrimaryKey)
@@ -1271,7 +1271,7 @@ protected internal virtual SqlString GenerateLazySelectString()
12711271
// use the subclass closure
12721272
int propertyNumber = GetSubclassPropertyIndex(lazyPropertyNames[i]);
12731273

1274-
int tableNumber = GetSubclassPropertyTableNumber(propertyNumber);
1274+
int tableNumber = GetSubclassPropertyTableNumber(propertyNumber, false);
12751275
tableNumbers.Add(tableNumber);
12761276

12771277
int[] colNumbers = subclassPropertyColumnNumberClosure[propertyNumber];
@@ -1326,7 +1326,7 @@ protected virtual IDictionary<string, SqlString> GenerateLazySelectStringsByFetc
13261326
// use the subclass closure
13271327
var propertyNumber = GetSubclassPropertyIndex(lazyPropertyDescriptor.Name);
13281328

1329-
var tableNumber = GetSubclassPropertyTableNumber(propertyNumber);
1329+
var tableNumber = GetSubclassPropertyTableNumber(propertyNumber, false);
13301330
tableNumbers.Add(tableNumber);
13311331

13321332
var colNumbers = subclassPropertyColumnNumberClosure[propertyNumber];
@@ -2050,12 +2050,12 @@ public virtual string GetRootTableAlias(string drivingAlias)
20502050
return drivingAlias;
20512051
}
20522052

2053-
public virtual string[] ToColumns(string alias, string propertyName)
2053+
public virtual string[] ToColumns(string alias, string propertyName, bool useLastIndex = false)
20542054
{
2055-
return propertyMapping.ToColumns(alias, propertyName);
2055+
return propertyMapping.ToColumns(alias, propertyName, useLastIndex);
20562056
}
20572057

2058-
public string[] ToColumns(string propertyName)
2058+
public string[] ToColumns(string propertyName, bool useLastIndex = false)
20592059
{
20602060
return propertyMapping.GetColumnNames(propertyName);
20612061
}
@@ -2083,7 +2083,7 @@ public string[] GetPropertyColumnNames(string propertyName)
20832083
/// SingleTableEntityPersister defines an overloaded form
20842084
/// which takes the entity name.
20852085
/// </remarks>
2086-
public virtual int GetSubclassPropertyTableNumber(string propertyPath)
2086+
public virtual int GetSubclassPropertyTableNumber(string propertyPath, bool useLastIndex)
20872087
{
20882088
string rootPropertyName = StringHelper.Root(propertyPath);
20892089
IType type = propertyMapping.ToType(rootPropertyName);
@@ -2110,13 +2110,16 @@ public virtual int GetSubclassPropertyTableNumber(string propertyPath)
21102110
return getSubclassColumnTableNumberClosure()[idx];
21112111
}
21122112
}*/
2113-
int index = Array.LastIndexOf(SubclassPropertyNameClosure, rootPropertyName); //TODO: optimize this better!
2114-
return index == -1 ? 0 : GetSubclassPropertyTableNumber(index);
2113+
int index = useLastIndex
2114+
? Array.LastIndexOf(SubclassPropertyNameClosure, rootPropertyName)
2115+
: Array.IndexOf(SubclassPropertyNameClosure, rootPropertyName); //TODO: optimize this better!
2116+
2117+
return index == -1 ? 0 : GetSubclassPropertyTableNumber(index, false);
21152118
}
21162119

21172120
public virtual Declarer GetSubclassPropertyDeclarer(string propertyPath)
21182121
{
2119-
int tableIndex = GetSubclassPropertyTableNumber(propertyPath);
2122+
int tableIndex = GetSubclassPropertyTableNumber(propertyPath, false);
21202123
if (tableIndex == 0)
21212124
{
21222125
return Declarer.Class;
@@ -2164,7 +2167,7 @@ private string GetSubclassAliasedColumn(string rootAlias, int tableNumber, strin
21642167

21652168
public string[] ToColumns(string name, int i)
21662169
{
2167-
string alias = GenerateTableAlias(name, GetSubclassPropertyTableNumber(i));
2170+
string alias = GenerateTableAlias(name, GetSubclassPropertyTableNumber(i, false));
21682171
string[] cols = GetSubclassPropertyColumnNames(i);
21692172
string[] templates = SubclassPropertyFormulaTemplateClosure[i];
21702173
string[] result = new string[cols.Length];
@@ -2398,7 +2401,7 @@ private EntityLoader GetAppropriateUniqueKeyLoader(string propertyName, IDiction
23982401
return uniqueKeyLoaders[propertyName];
23992402
}
24002403

2401-
return CreateUniqueKeyLoader(propertyMapping.ToType(propertyName), propertyMapping.ToColumns(propertyName), enabledFilters);
2404+
return CreateUniqueKeyLoader(propertyMapping.ToType(propertyName), propertyMapping.ToColumns(propertyName, false), enabledFilters);
24022405
}
24032406

24042407
public int GetPropertyIndex(string propertyName)
@@ -3682,7 +3685,7 @@ private IDictionary<string, string> GetColumnsToTableAliasMap(string rootAlias)
36823685

36833686
if (cols != null && cols.Length > 0)
36843687
{
3685-
PropertyKey key = new PropertyKey(cols[0], GetSubclassPropertyTableNumber(i));
3688+
PropertyKey key = new PropertyKey(cols[0], GetSubclassPropertyTableNumber(i, false));
36863689
propDictionary[key] = property;
36873690
}
36883691
}

src/NHibernate/Persister/Entity/AbstractPropertyMapping.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public bool TryToType(string propertyName, out IType type)
4444
return typesByPropertyPath.TryGetValue(propertyName, out type);
4545
}
4646

47-
public virtual string[] ToColumns(string alias, string propertyName)
47+
public virtual string[] ToColumns(string alias, string propertyName, bool useLastIndex)
4848
{
4949
//TODO: *two* hashmap lookups here is one too many...
5050
string[] columns = GetColumns(propertyName);
@@ -71,7 +71,7 @@ private string[] GetColumns(string propertyName)
7171
return columns;
7272
}
7373

74-
public virtual string[] ToColumns(string propertyName)
74+
public virtual string[] ToColumns(string propertyName, bool useLastIndex)
7575
{
7676
string[] columns = GetColumns(propertyName);
7777

0 commit comments

Comments
 (0)