Skip to content

Commit 508fbae

Browse files
committed
Merge fix for NH-3624.
2 parents 6ea7f43 + 8e3032b commit 508fbae

File tree

7 files changed

+98
-89
lines changed

7 files changed

+98
-89
lines changed

src/NHibernate.Test/Hql/Ast/BulkManipulation.cs

Lines changed: 64 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,10 @@ public void InsertWithGeneratedVersionAndId()
228228

229229
s = OpenSession();
230230
t = s.BeginTransaction();
231-
int count = s.CreateQuery("insert into IntegerVersioned ( name ) select name from IntegerVersioned").ExecuteUpdate();
231+
int count =
232+
s.CreateQuery("insert into IntegerVersioned ( name, Data ) select name, Data from IntegerVersioned where id = :id")
233+
.SetInt64("id", entity.Id)
234+
.ExecuteUpdate();
232235
t.Commit();
233236
s.Close();
234237

@@ -277,7 +280,10 @@ public void InsertWithGeneratedTimestampVersion()
277280
s = OpenSession();
278281
t = s.BeginTransaction();
279282
int count =
280-
s.CreateQuery("insert into TimestampVersioned ( name ) select name from TimestampVersioned").ExecuteUpdate();
283+
s.CreateQuery(
284+
"insert into TimestampVersioned ( name, Data ) select name, Data from TimestampVersioned where id = :id")
285+
.SetInt64("id", entity.Id)
286+
.ExecuteUpdate();
281287
t.Commit();
282288
s.Close();
283289

@@ -393,59 +399,81 @@ public void UpdateWithWhereExistsSubquery()
393399
[Test]
394400
public void IncrementCounterVersion()
395401
{
396-
ISession s = OpenSession();
397-
ITransaction t = s.BeginTransaction();
402+
IntegerVersioned entity;
398403

399-
var entity = new IntegerVersioned {Name = "int-vers"};
400-
s.Save(entity);
401-
t.Commit();
402-
s.Close();
404+
using (ISession s = OpenSession())
405+
using (ITransaction t = s.BeginTransaction())
406+
{
407+
entity = new IntegerVersioned {Name = "int-vers", Data = "foo"};
408+
s.Save(entity);
409+
t.Commit();
410+
}
403411

404412
int initialVersion = entity.Version;
405413

406-
s = OpenSession();
407-
t = s.BeginTransaction();
408-
int count = s.CreateQuery("update versioned IntegerVersioned set name = name").ExecuteUpdate();
409-
Assert.That(count, Is.EqualTo(1), "incorrect exec count");
410-
t.Commit();
414+
using (ISession s = OpenSession())
415+
{
416+
using (ITransaction t = s.BeginTransaction())
417+
{
418+
// Note: Update more than one column to showcase NH-3624, which involved losing some columns. /2014-07-26
419+
int count = s.CreateQuery("update versioned IntegerVersioned set name = concat(name, 'upd'), Data = concat(Data, 'upd')")
420+
.ExecuteUpdate();
421+
Assert.That(count, Is.EqualTo(1), "incorrect exec count");
422+
t.Commit();
423+
}
424+
425+
using (ITransaction t = s.BeginTransaction())
426+
{
427+
entity = s.Get<IntegerVersioned>(entity.Id);
428+
s.Delete(entity);
429+
t.Commit();
430+
}
431+
}
411432

412-
t = s.BeginTransaction();
413-
entity = s.Load<IntegerVersioned>(entity.Id);
414433
Assert.That(entity.Version, Is.EqualTo(initialVersion + 1), "version not incremented");
415-
416-
s.Delete(entity);
417-
t.Commit();
418-
s.Close();
434+
Assert.That(entity.Name, Is.EqualTo("int-versupd"));
435+
Assert.That(entity.Data, Is.EqualTo("fooupd"));
419436
}
420437

421438
[Test]
422439
public void IncrementTimestampVersion()
423440
{
424-
ISession s = OpenSession();
425-
ITransaction t = s.BeginTransaction();
441+
TimestampVersioned entity;
426442

427-
var entity = new TimestampVersioned {Name = "ts-vers"};
428-
s.Save(entity);
429-
t.Commit();
430-
s.Close();
443+
using (ISession s = OpenSession())
444+
using(ITransaction t = s.BeginTransaction())
445+
{
446+
entity = new TimestampVersioned { Name = "ts-vers", Data = "foo" };
447+
s.Save(entity);
448+
t.Commit();
449+
}
431450

432451
DateTime initialVersion = entity.Version;
433452

434453
Thread.Sleep(1300);
435454

436-
s = OpenSession();
437-
t = s.BeginTransaction();
438-
int count = s.CreateQuery("update versioned TimestampVersioned set name = name").ExecuteUpdate();
439-
Assert.That(count, Is.EqualTo(1), "incorrect exec count");
440-
t.Commit();
455+
using (ISession s = OpenSession())
456+
{
457+
using (ITransaction t = s.BeginTransaction())
458+
{
459+
// Note: Update more than one column to showcase NH-3624, which involved losing some columns. /2014-07-26
460+
int count = s.CreateQuery("update versioned TimestampVersioned set name = concat(name, 'upd'), Data = concat(Data, 'upd')")
461+
.ExecuteUpdate();
462+
Assert.That(count, Is.EqualTo(1), "incorrect exec count");
463+
t.Commit();
464+
}
465+
466+
using (ITransaction t = s.BeginTransaction())
467+
{
468+
entity = s.Load<TimestampVersioned>(entity.Id);
469+
s.Delete(entity);
470+
t.Commit();
471+
}
472+
}
441473

442-
t = s.BeginTransaction();
443-
entity = s.Load<TimestampVersioned>(entity.Id);
444474
Assert.That(entity.Version, Is.GreaterThan(initialVersion), "version not incremented");
445-
446-
s.Delete(entity);
447-
t.Commit();
448-
s.Close();
475+
Assert.That(entity.Name, Is.EqualTo("ts-versupd"));
476+
Assert.That(entity.Data, Is.EqualTo("fooupd"));
449477
}
450478

451479
[Test]

src/NHibernate.Test/Hql/Ast/IntegerVersioned.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,7 @@ public virtual string Name
2323
get { return name; }
2424
set { name = value; }
2525
}
26+
27+
public virtual string Data { get; set; }
2628
}
2729
}

src/NHibernate.Test/Hql/Ast/TimestampVersioned.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,7 @@ public virtual string Name
2525
get { return name; }
2626
set { name = value; }
2727
}
28+
29+
public virtual string Data { get; set; }
2830
}
2931
}

src/NHibernate.Test/Hql/Ast/Versions.hbm.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
</id>
1111
<version name="version" column="vers"/>
1212
<property name="name"/>
13+
<property name="Data" access="property"/>
1314
</class>
1415

1516
<class name="TimestampVersioned">
@@ -18,6 +19,7 @@
1819
</id>
1920
<timestamp name="version" column="vers"/>
2021
<property name="name"/>
22+
<property name="Data" access="property"/>
2123
</class>
2224

2325
</hibernate-mapping>

src/NHibernate/Hql/Ast/ANTLR/HqlSqlWalker.cs

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,7 @@ void PrepareVersioned(IASTNode updateNode, IASTNode versioned)
229229
EvaluateAssignment(eq, persister, 0);
230230

231231
IASTNode setClause = updateStatement.SetClause;
232-
IASTNode currentFirstSetElement = setClause.GetFirstChild();
233-
setClause.SetFirstChild(eq);
234-
eq.NextSibling= currentFirstSetElement;
232+
setClause.InsertChild(0, eq);
235233
}
236234
}
237235

@@ -296,9 +294,7 @@ void PostProcessInsert(IASTNode insert)
296294

297295
if (idSelectExprNode != null)
298296
{
299-
IASTNode currentFirstSelectExprNode = selectClause.GetFirstChild();
300-
selectClause.SetFirstChild(idSelectExprNode);
301-
idSelectExprNode.NextSibling= currentFirstSelectExprNode;
297+
selectClause.InsertChild(0, idSelectExprNode);
302298

303299
insertStatement.IntoClause.PrependIdColumnSpec();
304300
}
@@ -343,9 +339,7 @@ void PostProcessInsert(IASTNode insert)
343339
}
344340
}
345341

346-
IASTNode currentFirstSelectExprNode = selectClause.GetFirstChild();
347-
selectClause.SetFirstChild(versionValueNode);
348-
versionValueNode.NextSibling = currentFirstSelectExprNode;
342+
selectClause.InsertChild(0, versionValueNode);
349343

350344
insertStatement.IntoClause.PrependVersionColumnSpec();
351345
}
@@ -793,32 +787,10 @@ void ProcessNumericLiteral(IASTNode literal)
793787
protected IASTNode LookupProperty(IASTNode dot, bool root, bool inSelect)
794788
{
795789
DotNode dotNode = (DotNode) dot;
796-
FromReferenceNode lhs = dotNode.GetLhs();
797-
IASTNode rhs = lhs.NextSibling;
798-
switch (rhs.Type)
799-
{
800-
case ELEMENTS:
801-
case INDICES:
802-
if (log.IsDebugEnabled)
803-
{
804-
log.Debug("lookupProperty() " + dotNode.Path + " => " + rhs.Text + "(" + lhs.Path + ")");
805-
}
806-
807-
CollectionFunction f = (CollectionFunction) rhs;
808-
// Re-arrange the tree so that the collection function is the root and the lhs is the path.
809790

810-
f.SetFirstChild(lhs);
811-
lhs.NextSibling = null;
812-
dotNode.SetFirstChild(f);
813-
814-
Resolve(lhs); // Don't forget to resolve the argument!
815-
f.Resolve(inSelect); // Resolve the collection function now.
816-
return f;
817-
default:
818-
// Resolve everything up to this dot, but don't resolve the placeholders yet.
819-
dotNode.ResolveFirstChild();
820-
return dotNode;
821-
}
791+
// Resolve everything up to this dot, but don't resolve the placeholders yet.
792+
dotNode.ResolveFirstChild();
793+
return dotNode;
822794
}
823795

824796
static void ProcessIndex(IASTNode indexOp)

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

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -260,24 +260,27 @@ public IASTNode NextSibling
260260

261261
return null;
262262
}
263-
set
264-
{
265-
if (_parent != null)
266-
{
267-
if (_parent.ChildCount > (ChildIndex + 1))
268-
{
269-
_parent.SetChild(ChildIndex + 1, value);
270-
}
271-
else
272-
{
273-
AddSibling(value);
274-
}
275-
}
276-
else
277-
{
278-
throw new InvalidOperationException("Trying set NextSibling without a parent.");
279-
}
280-
}
263+
// Setter commented out 2014-07-26. I don't like it since it drops the current next sibling from
264+
// the tree, and the name of the property doesn't give a clear indication if it overwrites or not.
265+
// Better to use InsertChild() on the parent.
266+
//set
267+
//{
268+
// if (_parent != null)
269+
// {
270+
// if (_parent.ChildCount > (ChildIndex + 1))
271+
// {
272+
// _parent.SetChild(ChildIndex + 1, value);
273+
// }
274+
// else
275+
// {
276+
// AddSibling(value);
277+
// }
278+
// }
279+
// else
280+
// {
281+
// throw new InvalidOperationException("Trying set NextSibling without a parent.");
282+
// }
283+
//}
281284
}
282285

283286
public IASTNode GetChild(int index)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public interface IASTNode : IEnumerable<IASTNode>
1515
int ChildCount { get; }
1616
int ChildIndex { get; }
1717
IASTNode Parent { get; set; }
18-
IASTNode NextSibling { get; set; }
18+
IASTNode NextSibling { get; /*set;*/ }
1919
IASTNode GetChild(int index);
2020
IASTNode GetFirstChild();
2121
void SetFirstChild(IASTNode newChild);

0 commit comments

Comments
 (0)