forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQLLoaderTest.cs
664 lines (542 loc) · 16.5 KB
/
SQLLoaderTest.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
using System;
using System.Collections;
using NHibernate.Dialect;
using NHibernate.DomainModel;
using NUnit.Framework;
using Single=NHibernate.DomainModel.Single;
namespace NHibernate.Test.Legacy
{
/// <summary>
/// Summary description for SQLLoaderTest.
/// </summary>
[TestFixture]
public class SQLLoaderTest : TestCase
{
protected override IList Mappings
{
get
{
return new string[]
{
"ABC.hbm.xml",
"Category.hbm.xml",
"Simple.hbm.xml",
"Fo.hbm.xml",
"SingleSeveral.hbm.xml",
"Componentizable.hbm.xml"
};
}
}
private static long nextLong = 1;
[Test]
public void TS()
{
if (Dialect is Oracle8iDialect)
{
return;
}
ISession session = OpenSession();
ITransaction txn = session.BeginTransaction();
Simple sim = new Simple();
sim.Date = DateTime.Today; // NB We don't use Now() due to the millisecond alignment problem with SQL Server
session.Save(sim, 1L);
IQuery q = session.CreateSQLQuery("select {sim.*} from Simple {sim} where {sim}.date_ = ?")
.AddEntity("sim", typeof(Simple));
q.SetTimestamp(0, sim.Date);
Assert.AreEqual(1, q.List().Count, "q.List.Count");
session.Delete(sim);
txn.Commit();
session.Close();
}
[Test]
public void TSNamed()
{
if (Dialect is Oracle8iDialect)
{
return;
}
ISession session = OpenSession();
ITransaction txn = session.BeginTransaction();
Simple sim = new Simple();
sim.Date = DateTime.Today; // NB We don't use Now() due to the millisecond alignment problem with SQL Server
session.Save(sim, 1L);
IQuery q =
session.CreateSQLQuery("select {sim.*} from Simple {sim} where {sim}.date_ = :fred")
.AddEntity("sim", typeof(Simple));
q.SetTimestamp("fred", sim.Date);
Assert.AreEqual(1, q.List().Count, "q.List.Count");
session.Delete(sim);
txn.Commit();
session.Close();
}
[Test]
public void FindBySQLStar()
{
ISession session = OpenSession();
Category s = new Category();
s.Name = nextLong.ToString();
nextLong++;
session.Save(s);
Simple simple = new Simple();
simple.Init();
session.Save(simple, nextLong++);
A a = new A();
session.Save(a);
//B b = new B();
//session.Save( b );
session.CreateSQLQuery("select {category.*} from Category {category}")
.AddEntity("category", typeof(Category)).List();
session.CreateSQLQuery("select {simple.*} from Simple {simple}")
.AddEntity("simple", typeof(Simple)).List();
session.CreateSQLQuery("select {a.*} from A {a}")
.AddEntity("a", typeof(A)).List();
session.Delete(s);
session.Delete(simple);
session.Delete(a);
//session.Delete( b );
session.Flush();
session.Close();
}
[Test]
public void FindBySQLProperties()
{
ISession session = OpenSession();
Category s = new Category();
s.Name = nextLong.ToString();
nextLong++;
session.Save(s);
s = new Category();
s.Name = "WannaBeFound";
session.Flush();
IQuery query =
session.CreateSQLQuery("select {category.*} from Category {category} where {category}.Name = :Name")
.AddEntity("category",typeof(Category));
query.SetProperties(s);
query.List();
session.Delete("from Category");
session.Flush();
session.Close();
}
[Test]
public void FindBySQLAssociatedObject()
{
ISession s = OpenSession();
Category c = new Category();
c.Name = "NAME";
Assignable assn = new Assignable();
assn.Id = "i.d.";
IList l = new ArrayList();
l.Add(c);
assn.Categories = l;
c.Assignable = assn;
s.Save(assn);
s.Flush();
s.Close();
s = OpenSession();
IList list = s.CreateSQLQuery("select {category.*} from Category {category}")
.AddEntity("category", typeof(Category)).List();
Assert.AreEqual(1, list.Count, "Count differs");
s.Delete("from Assignable");
s.Delete("from Category");
s.Flush();
s.Close();
}
[Test]
public void FindBySQLMultipleObject()
{
ISession s = OpenSession();
Category c = new Category();
c.Name = "NAME";
Assignable assn = new Assignable();
assn.Id = "i.d.";
IList l = new ArrayList();
l.Add(c);
assn.Categories = l;
c.Assignable = assn;
s.Save(assn);
s.Flush();
c = new Category();
c.Name = "NAME2";
assn = new Assignable();
assn.Id = "i.d.2";
l = new ArrayList();
l.Add(c);
assn.Categories = l;
c.Assignable = assn;
s.Save(assn);
s.Flush();
assn = new Assignable();
assn.Id = "i.d.3";
s.Save(assn);
s.Flush();
s.Close();
s = OpenSession();
if (!(Dialect is MySQLDialect))
{
IList list =
s.CreateSQLQuery("select {category.*}, {assignable.*} from Category {category}, \"assign able\" {assignable}")
.AddEntity("category", typeof(Category))
.AddEntity("assignable", typeof(Assignable))
.List();
Assert.AreEqual(6, list.Count, "Count differs"); // cross-product of 2 categories x 3 assignables;
Assert.IsTrue(list[0] is object[]);
}
s.Delete("from Assignable");
s.Delete("from Category");
s.Flush();
s.Close();
}
[Test]
public void FindBySQLParameters()
{
ISession s = OpenSession();
Category c = new Category();
c.Name = "Good";
Assignable assn = new Assignable();
assn.Id = "i.d.";
IList l = new ArrayList();
l.Add(c);
assn.Categories = l;
c.Assignable = assn;
s.Save(assn);
s.Flush();
c = new Category();
c.Name = "Best";
assn = new Assignable();
assn.Id = "i.d.2";
l = new ArrayList();
l.Add(c);
assn.Categories = l;
c.Assignable = assn;
s.Save(assn);
s.Flush();
c = new Category();
c.Name = "Better";
assn = new Assignable();
assn.Id = "i.d.7";
l = new ArrayList();
l.Add(c);
assn.Categories = l;
c.Assignable = assn;
s.Save(assn);
s.Flush();
assn = new Assignable();
assn.Id = "i.d.3";
s.Save(assn);
s.Flush();
s.Close();
s = OpenSession();
IQuery basicParam =
s.CreateSQLQuery("select {category.*} from Category {category} where {category}.Name = 'Best'")
.AddEntity("category", typeof(Category));
IList list = basicParam.List();
Assert.AreEqual(1, list.Count);
IQuery unnamedParam =
s.CreateSQLQuery("select {category.*} from Category {category} where {category}.Name = ? or {category}.Name = ?")
.AddEntity("category", typeof(Category));
unnamedParam.SetString(0, "Good");
unnamedParam.SetString(1, "Best");
list = unnamedParam.List();
Assert.AreEqual(2, list.Count);
IQuery namedParam =
s.CreateSQLQuery(
"select {category.*} from Category {category} where ({category}.Name=:firstCat or {category}.Name=:secondCat)")
.AddEntity("category", typeof(Category));
namedParam.SetString("firstCat", "Better");
namedParam.SetString("secondCat", "Best");
list = namedParam.List();
Assert.AreEqual(2, list.Count);
s.Delete("from Assignable");
s.Delete("from Category");
s.Flush();
s.Close();
}
[Test]
[Ignore("Escaping not implemented. Need to test with ODBC/OLEDB when implemented.")]
public void EscapedODBC()
{
if (Dialect is MySQLDialect || Dialect is PostgreSQLDialect) return;
ISession session = OpenSession();
A savedA = new A();
savedA.Name = "Max";
session.Save(savedA);
B savedB = new B();
session.Save(savedB);
session.Flush();
session.Close();
session = OpenSession();
IQuery query;
query =
session.CreateSQLQuery(
"select identifier_column as {a.id}, clazz_discriminata as {a.class}, count_ as {a.Count}, name as {a.Name} from A where {fn ucase(Name)} like {fn ucase('max')}")
.AddEntity("a", typeof(A));
// NH: Replaced the whole if by the line above
/*
if( dialect is Dialect.TimesTenDialect)
{
// TimesTen does not permit general expressions (like UPPER) in the second part of a LIKE expression,
// so we execute a similar test
query = session.CreateSQLQuery("select identifier_column as {a.id}, clazz_discriminata as {a.class}, count_ as {a.Count}, name as {a.Name} from A where {fn ucase(name)} like 'MAX'", "a", typeof( A ));
}
else
{
query = session.CreateSQLQuery("select identifier_column as {a.id}, clazz_discriminata as {a.class}, count_ as {a.Count}, name as {a.Name} from A where {fn ucase(name)} like {fn ucase('max')}", "a", typeof( A ));
}
*/
IList list = query.List();
Assert.IsNotNull(list);
Assert.AreEqual(1, list.Count);
session.Delete("from A");
session.Flush();
session.Close();
}
[Test]
public void DoubleAliasing()
{
if (Dialect is MySQLDialect) return;
if (Dialect is FirebirdDialect) return; // See comment below
ISession session = OpenSession();
A savedA = new A();
savedA.Name = "Max";
session.Save(savedA);
B savedB = new B();
session.Save(savedB);
session.Flush();
int count = session.CreateQuery("from A").List().Count;
session.Close();
session = OpenSession();
IQuery query =
session.CreateSQLQuery(
"select a.identifier_column as {a1.id}, a.clazz_discriminata as {a1.class}, a.count_ as {a1.Count}, a.name as {a1.Name}, a.anothername as {a1.AnotherName} " +
", b.identifier_column as {a2.id}, b.clazz_discriminata as {a2.class}, b.count_ as {a2.Count}, b.name as {a2.Name}, b.anothername as {a2.AnotherName} " +
" from A a, A b" +
" where a.identifier_column = b.identifier_column")
.AddEntity("a1", typeof(A))
.AddEntity("a2",typeof(A));
IList list = query.List();
Assert.IsNotNull(list);
Assert.AreEqual(2, list.Count);
// On Firebird the list has 4 elements, I don't understand why.
session.Delete("from A");
session.Flush();
session.Close();
}
[Test]
public void EmbeddedCompositeProperties()
{
ISession session = OpenSession();
Single s = new Single();
s.Id = "my id";
s.String = "string 1";
session.Save(s);
session.Flush();
session.Clear();
IQuery query = session.CreateSQLQuery("select {sing.*} from Single {sing}")
.AddEntity("sing", typeof(Single));
IList list = query.List();
Assert.IsTrue(list.Count == 1);
session.Clear();
query = session.CreateSQLQuery("select {sing.*} from Single {sing} where sing.Id = ?")
.AddEntity("sing", typeof(Single));
query.SetString(0, "my id");
list = query.List();
Assert.IsTrue(list.Count == 1);
session.Clear();
query =
session.CreateSQLQuery(
"select s.id as {sing.Id}, s.string_ as {sing.String}, s.prop as {sing.Prop} from Single s where s.Id = ?")
.AddEntity("sing", typeof(Single));
query.SetString(0, "my id");
list = query.List();
Assert.IsTrue(list.Count == 1);
session.Clear();
query =
session.CreateSQLQuery(
"select s.id as {sing.Id}, s.string_ as {sing.String}, s.prop as {sing.Prop} from Single s where s.Id = ?")
.AddEntity("sing", typeof(Single));
query.SetString(0, "my id");
list = query.List();
Assert.IsTrue(list.Count == 1);
session.Delete(list[0]);
session.Flush();
session.Close();
}
[Test]
public void ComponentStar()
{
ComponentTest("select {comp.*} from Componentizable comp");
}
[Test]
public void ComponentNoStar()
{
ComponentTest(
"select comp.Id as {comp.id}, comp.nickName as {comp.NickName}, comp.Name as {comp.Component.Name}, comp.SubName as {comp.Component.SubComponent.SubName}, comp.SubName1 as {comp.Component.SubComponent.SubName1} from Componentizable comp");
}
private void ComponentTest(string sql)
{
ISession session = OpenSession();
Componentizable c = new Componentizable();
c.NickName = "Flacky";
NHibernate.DomainModel.Component component = new NHibernate.DomainModel.Component();
component.Name = "flakky comp";
SubComponent subComponent = new SubComponent();
subComponent.SubName = "subway";
component.SubComponent = subComponent;
c.Component = component;
session.Save(c);
session.Flush();
session.Clear();
IQuery q = session.CreateSQLQuery(sql).AddEntity("comp", typeof(Componentizable));
IList list = q.List();
Assert.AreEqual(list.Count, 1);
Componentizable co = (Componentizable) list[0];
Assert.AreEqual(c.NickName, co.NickName);
Assert.AreEqual(c.Component.Name, co.Component.Name);
Assert.AreEqual(c.Component.SubComponent.SubName, co.Component.SubComponent.SubName);
session.Delete(co);
session.Flush();
session.Close();
}
[Test]
public void FindSimpleBySQL()
{
if (Dialect is MySQLDialect) return;
ISession session = OpenSession();
Category s = new Category();
nextLong++;
s.Name = nextLong.ToString();
session.Save(s);
session.Flush();
IQuery query =
session.CreateSQLQuery(
"select s.category_key_col as {category.id}, s.Name as {category.Name}, s.\"assign able id\" as {category.Assignable} from {category} s")
.AddEntity("category", typeof(Category));
IList list = query.List();
Assert.IsNotNull(list);
Assert.IsTrue(list.Count > 0);
Assert.IsTrue(list[0] is Category);
session.Delete(list[0]);
session.Flush();
session.Close();
// How do we handle objects with composite id's ? (such as Single)
}
[Test]
public void FindBySQLSimpleByDiffSessions()
{
if (Dialect is MySQLDialect) return;
ISession session = OpenSession();
Category s = new Category();
nextLong++;
s.Name = nextLong.ToString();
session.Save(s);
session.Flush();
session.Close();
session = OpenSession();
IQuery query =
session.CreateSQLQuery(
"select s.category_key_col as {category.id}, s.Name as {category.Name}, s.\"assign able id\" as {category.Assignable} from {category} s")
.AddEntity("category", typeof(Category));
IList list = query.List();
Assert.IsNotNull(list);
Assert.IsTrue(list.Count > 0);
Assert.IsTrue(list[0] is Category);
// How do we handle objects that does not have id property (such as Simple ?)
// How do we handle objects with composite id's ? (such as Single)
session.Delete(list[0]);
session.Flush();
session.Close();
}
[Test]
public void FindBySQLDiscriminatorSameSession()
{
if (Dialect is MySQLDialect) return;
ISession session = OpenSession();
A savedA = new A();
session.Save(savedA);
B savedB = new B();
session.Save(savedB);
session.Flush();
IQuery query =
session.CreateSQLQuery(
"select identifier_column as {a.id}, clazz_discriminata as {a.class}, name as {a.Name}, count_ as {a.Count} from {a} s")
.AddEntity("a", typeof(A));
IList list = query.List();
Assert.IsNotNull(list);
Assert.AreEqual(2, list.Count);
A a1 = (A) list[0];
A a2 = (A) list[1];
Assert.IsTrue((a2 is B) || (a1 is B));
Assert.IsFalse(a1 is B && a2 is B);
if (a1 is B)
{
Assert.AreSame(a1, savedB);
Assert.AreSame(a2, savedA);
}
else
{
Assert.AreSame(a2, savedB);
Assert.AreSame(a1, savedA);
}
session.Delete("from A");
session.Flush();
session.Close();
}
[Test]
public void FindBySQLDiscriminatedDiffSessions()
{
if (Dialect is MySQLDialect) return;
ISession session = OpenSession();
A savedA = new A();
session.Save(savedA);
B savedB = new B();
session.Save(savedB);
session.Flush();
int count = session.CreateQuery("from A").List().Count;
session.Close();
session = OpenSession();
IQuery query = session.CreateSQLQuery(
"select identifier_column as {a.id}, clazz_discriminata as {a.class}, count_ as {a.Count}, name as {a.Name}, anothername as {a.AnotherName} from A")
.AddEntity("a", typeof(A));
IList list = query.List();
Assert.IsNotNull(list);
Assert.AreEqual(count, list.Count);
session.Delete("from A");
session.Flush();
session.Close();
}
[Test]
public void NamedSQLQuery()
{
if (Dialect is MySQLDialect)
{
return;
}
ISession s = OpenSession();
Category c = new Category();
c.Name = "NAME";
Assignable assn = new Assignable();
assn.Id = "i.d.";
IList l = new ArrayList();
l.Add(c);
assn.Categories = l;
c.Assignable = assn;
s.Save(assn);
s.Flush();
s.Close();
s = OpenSession();
IQuery q = s.GetNamedQuery("namedsql");
Assert.IsNotNull(q, "should have found 'namedsql'");
IList list = q.List();
Assert.IsNotNull(list, "executing query returns list");
object[] values = list[0] as object[];
Assert.IsNotNull(values[0], "index 0 should not be null");
Assert.IsNotNull(values[1], "index 1 should not be null");
Assert.AreEqual(typeof(Category), values[0].GetType(), "should be a Category");
Assert.AreEqual(typeof(Assignable), values[1].GetType(), "should be Assignable");
s.Delete("from Category");
s.Delete("from Assignable");
s.Flush();
s.Close();
}
}
}