Skip to content

Commit 420844b

Browse files
author
Sergey Koshcheyev
committed
NH-742 test and fix. Renamed a few methods in persister hierarchy.
SVN: trunk@2357
1 parent a8974fa commit 420844b

14 files changed

+198
-57
lines changed

src/NHibernate.DomainModel/CustomPersister.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ public void Lock(object id, object version, object obj, LockMode lockMode, ISess
354354
/// <param name="version"></param>
355355
/// <param name="session"></param>
356356
/// <returns></returns>
357-
public object[] GetCurrentPersistentState( object id, object version, ISessionImplementor session )
357+
public object[] GetDatabaseSnapshot( object id, object version, ISessionImplementor session )
358358
{
359359
return null;
360360
}

src/NHibernate.Test/NHSpecificTest/EntityKeyFixture.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public NHibernate.Type.IType[] PropertyTypes
203203
}
204204
}
205205

206-
public object[] GetCurrentPersistentState(object id, object version, ISessionImplementor session)
206+
public object[] GetDatabaseSnapshot(object id, object version, ISessionImplementor session)
207207
{
208208
// TODO: Add TestingClassPersister.GetCurrentPersistentState implementation
209209
return null;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
3+
namespace NHibernate.Test.NHSpecificTest.NH742
4+
{
5+
public class Point
6+
{
7+
int x, y;
8+
9+
public int X
10+
{
11+
get { return x; }
12+
set { x = value; }
13+
}
14+
15+
public int Y
16+
{
17+
get { return y; }
18+
set { y = value; }
19+
}
20+
}
21+
22+
public class SomeClass
23+
{
24+
private int id;
25+
private Point point;
26+
27+
public virtual int Id
28+
{
29+
get { return id; }
30+
set { id = value; }
31+
}
32+
33+
public virtual Point Point
34+
{
35+
get { return point; }
36+
set { point = value; }
37+
}
38+
}
39+
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using NUnit.Framework;
3+
4+
namespace NHibernate.Test.NHSpecificTest.NH742
5+
{
6+
[TestFixture]
7+
public class Fixture : BugTestCase
8+
{
9+
[Test]
10+
public void Bug()
11+
{
12+
// Do nothing, we only want to check the configuration
13+
}
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
3+
assembly="NHibernate.Test"
4+
namespace="NHibernate.Test.NHSpecificTest.NH742">
5+
<class name="SomeClass">
6+
<id name="Id">
7+
<generator class="increment" />
8+
</id>
9+
<component name="Point">
10+
<property name="X" formula="10" />
11+
<property name="Y" formula="20" />
12+
</component>
13+
</class>
14+
</hibernate-mapping>

src/NHibernate.Test/NHibernate.Test-2.0.csproj

+5
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@
325325
<Compile Include="NHSpecificTest\NH555\Fixture.cs" />
326326
<Compile Include="NHSpecificTest\NH739\Cat.cs" />
327327
<Compile Include="NHSpecificTest\NH739\Fixture.cs" />
328+
<Compile Include="NHSpecificTest\NH742\Classes.cs" />
329+
<Compile Include="NHSpecificTest\NH742\Fixture.cs" />
328330
<Compile Include="NHSpecificTest\NH750\Device.cs" />
329331
<Compile Include="NHSpecificTest\NH750\Drive.cs" />
330332
<Compile Include="NHSpecificTest\NH750\Fixture.cs" />
@@ -678,6 +680,9 @@
678680
<ItemGroup>
679681
<EmbeddedResource Include="NHSpecificTest\NH706\Mappings.hbm.xml" />
680682
</ItemGroup>
683+
<ItemGroup>
684+
<EmbeddedResource Include="NHSpecificTest\NH742\Mappings.hbm.xml" />
685+
</ItemGroup>
681686
<ItemGroup>
682687
<Folder Include="Properties\" />
683688
</ItemGroup>

src/NHibernate/Impl/SessionImpl.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3378,7 +3378,7 @@ private void FlushEntity(object obj, EntityEntry entry)
33783378
}
33793379
else
33803380
{
3381-
currentPersistentState = persister.GetCurrentPersistentState(entry.Id, entry.Version, this);
3381+
currentPersistentState = persister.GetDatabaseSnapshot(entry.Id, entry.Version, this);
33823382
if (currentPersistentState != null)
33833383
{
33843384
dirtyProperties = persister.FindModified(currentPersistentState, values, obj, this);

src/NHibernate/Mapping/SimpleValue.cs

+7-3
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,14 @@ public virtual IType Type
7171
this.type = value;
7272
int count = 0;
7373

74-
foreach( Column col in ColumnCollection )
74+
foreach( ISelectable sel in ColumnCollection )
7575
{
76-
col.Type = type;
77-
col.TypeIndex = count++;
76+
if (sel is Column)
77+
{
78+
Column col = (Column) sel;
79+
col.Type = type;
80+
col.TypeIndex = count++;
81+
}
7882
}
7983
}
8084
}

src/NHibernate/Persister/Entity/AbstractEntityPersister.cs

+83-7
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public abstract class AbstractEntityPersister : AbstractPropertyMapping, IOuterJ
157157
private SqlCommandInfo[] sqlInsertStrings;
158158
private SqlCommandInfo sqlIdentityInsertString;
159159
private SqlCommandInfo[] sqlUpdateStrings;
160-
private SqlString sqlConcreteSelectString;
160+
private SqlString sqlSnapshotSelectString;
161161
private SqlString sqlVersionSelectString;
162162
private bool[] tableHasColumns;
163163

@@ -1503,7 +1503,7 @@ protected object GetGeneratedIdentity(object obj, ISessionImplementor session, I
15031503
return id;
15041504
}
15051505

1506-
public object[] GetCurrentPersistentState(object id, object version, ISessionImplementor session)
1506+
public object[] GetDatabaseSnapshot(object id, object version, ISessionImplementor session)
15071507
{
15081508
if (!HasSelectBeforeUpdate)
15091509
{
@@ -1518,7 +1518,7 @@ public object[] GetCurrentPersistentState(object id, object version, ISessionImp
15181518
IType[] types = PropertyTypes;
15191519
object[] values = new object[types.Length];
15201520
bool[] includeProperty = PropertyUpdateability;
1521-
SqlString sql = ConcreteSelectString;
1521+
SqlString sql = SQLSnapshotSelectString;
15221522
try
15231523
{
15241524
IDbCommand st = session.Batcher.PrepareCommand(CommandType.Text, sql, idAndVersionSqlTypes);
@@ -1639,6 +1639,11 @@ protected int GetPropertyColumnSpan(int i)
16391639
{
16401640
return propertyColumnSpans[i];
16411641
}
1642+
1643+
protected string[] GetPropertyColumnFormulaTemplates(int i)
1644+
{
1645+
return propertyColumnFormulaTemplates[i];
1646+
}
16421647

16431648
public string SelectFragment(string alias, string suffix, bool includeCollectionColumns)
16441649
{
@@ -2229,6 +2234,11 @@ protected EntityMetamodel EntityMetamodel
22292234
get { return entityMetamodel; }
22302235
}
22312236

2237+
private string RootAlias
2238+
{
2239+
get { return StringHelper.GenerateAlias(ClassName); }
2240+
}
2241+
22322242
protected void PostConstruct(IMapping mapping)
22332243
{
22342244
InitPropertyPaths(mapping);
@@ -2239,7 +2249,73 @@ protected void PostConstruct(IMapping mapping)
22392249
idSqlTypes = IdentifierType.SqlTypes(mapping);
22402250
}
22412251

2242-
protected abstract SqlString GenerateConcreteSelectString();
2252+
protected abstract int[] PropertyTableNumbersInSelect { get; }
2253+
2254+
protected string ConcretePropertySelectFragment(string alias, bool[] includeProperty)
2255+
{
2256+
int propertyCount = entityMetamodel.PropertySpan;
2257+
int[] propertyTableNumbers = PropertyTableNumbersInSelect;
2258+
SelectFragment frag = new SelectFragment(Factory.Dialect);
2259+
for (int i = 0; i < propertyCount; i++)
2260+
{
2261+
if (includeProperty[i])
2262+
{
2263+
//ie. updateable, not a formula
2264+
frag.AddColumns(
2265+
GenerateTableAlias(alias, propertyTableNumbers[i]),
2266+
propertyColumnNames[i],
2267+
propertyColumnAliases[i]
2268+
);
2269+
frag.AddFormulas(
2270+
GenerateTableAlias(alias, propertyTableNumbers[i]),
2271+
propertyColumnFormulaTemplates[i],
2272+
propertyColumnAliases[i]
2273+
);
2274+
}
2275+
}
2276+
return frag.ToSqlStringFragment();
2277+
}
2278+
2279+
protected virtual SqlString GenerateSnapshotSelectString()
2280+
{
2281+
//TODO: should we use SELECT .. FOR UPDATE?
2282+
2283+
SqlSelectBuilder select = new SqlSelectBuilder(Factory);
2284+
2285+
//if (Factory.Settings.IsCommentsEnabled)
2286+
//{
2287+
// select.SetComment("get current state " + ClassName);
2288+
//}
2289+
2290+
string[] aliasedIdColumns = StringHelper.Qualify(RootAlias, IdentifierColumnNames);
2291+
string selectClause = StringHelper.Join(", ", aliasedIdColumns) +
2292+
ConcretePropertySelectFragment(RootAlias, PropertyUpdateability);
2293+
2294+
SqlString fromClause = FromTableFragment(RootAlias) +
2295+
FromJoinFragment(RootAlias, true, false);
2296+
2297+
SqlString joiner = new SqlString("=", Parameter.Placeholder, " and ");
2298+
SqlString whereClause = new SqlStringBuilder()
2299+
.Add(StringHelper.Join(joiner, aliasedIdColumns))
2300+
.Add("=")
2301+
.AddParameter()
2302+
.Add(WhereJoinFragment(RootAlias, true, false))
2303+
.ToSqlString();
2304+
2305+
// TODO H3: this is commented out in H3.2
2306+
if (IsVersioned)
2307+
{
2308+
whereClause.Append(" and ")
2309+
.Append(VersionColumnName)
2310+
.Append("=?");
2311+
}
2312+
2313+
return select.SetSelectClause(selectClause)
2314+
.SetFromClause(fromClause)
2315+
.SetOuterJoins(SqlString.Empty, SqlString.Empty)
2316+
.SetWhereClause(whereClause)
2317+
.ToSqlString();
2318+
}
22432319

22442320
public virtual void PostInstantiate()
22452321
{
@@ -2269,7 +2345,7 @@ public virtual void PostInstantiate()
22692345
? GenerateIdentityInsertString(PropertyInsertability)
22702346
: null;
22712347

2272-
sqlConcreteSelectString = GenerateConcreteSelectString();
2348+
sqlSnapshotSelectString = GenerateSnapshotSelectString();
22732349
sqlVersionSelectString = GenerateSelectVersionString();
22742350

22752351
// This is used to determine updates for objects that came in via update()
@@ -2818,9 +2894,9 @@ protected SqlString VersionSelectString
28182894
get { return sqlVersionSelectString; }
28192895
}
28202896

2821-
protected SqlString ConcreteSelectString
2897+
protected SqlString SQLSnapshotSelectString
28222898
{
2823-
get { return sqlConcreteSelectString; }
2899+
get { return sqlSnapshotSelectString; }
28242900
}
28252901

28262902
protected SqlCommandInfo GenerateDeleteString(int j)

src/NHibernate/Persister/Entity/IEntityPersister.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ public interface IEntityPersister
391391
/// <param name="version"></param>
392392
/// <param name="session"></param>
393393
/// <returns><c>null</c> if select-before-update is not enabled or not supported</returns>
394-
object[] GetCurrentPersistentState( object id, object version, ISessionImplementor session );
394+
object[] GetDatabaseSnapshot( object id, object version, ISessionImplementor session );
395395

396396
/// <summary>
397397
/// Get the current version of the object, or return null if there is no row for

src/NHibernate/Persister/Entity/JoinedSubclassEntityPersister.cs

+3-17
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ protected override SqlString GenerateLockString(SqlString sqlString, string forU
209209

210210
private const string ConcreteAlias = "x";
211211

212-
protected override SqlString GenerateConcreteSelectString()
212+
protected override SqlString GenerateSnapshotSelectString()
213213
{
214214
SqlStringBuilder select = new SqlStringBuilder();
215215

@@ -673,23 +673,9 @@ public override string[] ToColumns(string alias, string property)
673673
}
674674
}
675675

676-
protected string ConcretePropertySelectFragment(string alias, bool[] includeProperty)
676+
protected override int[] PropertyTableNumbersInSelect
677677
{
678-
int propertyCount = PropertyNames.Length;
679-
SelectFragment frag = new SelectFragment(Dialect);
680-
681-
for (int i = 0; i < propertyCount; i++)
682-
{
683-
if (includeProperty[i])
684-
{
685-
frag.AddColumns(
686-
Alias(alias, propertyTables[i]),
687-
GetPropertyColumnNames(i),
688-
GetPropertyColumnAliases(i));
689-
}
690-
}
691-
692-
return frag.ToSqlStringFragment();
678+
get { return propertyTables; }
693679
}
694680

695681
private CaseFragment DiscriminatorFragment(string alias)

src/NHibernate/Persister/Entity/SingleTableEntityPersister.cs

+2-25
Original file line numberDiff line numberDiff line change
@@ -194,32 +194,9 @@ protected virtual SqlString GenerateSelectString(string forUpdateFragment)
194194
return selectSqlString;
195195
}
196196

197-
/// <summary>
198-
/// Generate the SQL that selects a row by id, excluding subclasses
199-
/// </summary>
200-
protected override SqlString GenerateConcreteSelectString()
197+
protected override int[] PropertyTableNumbersInSelect
201198
{
202-
SqlSimpleSelectBuilder builder = new SqlSimpleSelectBuilder(Factory);
203-
204-
// set the table and the identity columns
205-
builder.SetTableName(TableName)
206-
.AddColumns(IdentifierColumnNames);
207-
208-
for (int i = 0; i < PropertyNames.Length; i++)
209-
{
210-
if (PropertyUpdateability[i])
211-
{
212-
builder.AddColumns(GetPropertyColumnNames(i), GetPropertyColumnAliases(i));
213-
}
214-
}
215-
216-
builder.SetIdentityColumn(IdentifierColumnNames, IdentifierType);
217-
if (IsVersioned)
218-
{
219-
builder.SetVersionColumn(new string[] {VersionColumnName}, VersionType);
220-
}
221-
222-
return builder.ToSqlString();
199+
get { return propertyTableNumbers; }
223200
}
224201

225202
/// <summary>

src/NHibernate/SqlCommand/SqlString.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public SqlString(string sqlPart)
3737
}
3838
}
3939

40-
public SqlString(object[] sqlParts)
40+
public SqlString(params object[] sqlParts)
4141
{
4242
#if DEBUG
4343
foreach (object obj in sqlParts)

0 commit comments

Comments
 (0)