1
+ using System ;
1
2
using System . Collections ;
3
+ using NHibernate . Criterion ;
2
4
using NHibernate . Transform ;
3
5
using NUnit . Framework ;
4
- using NHibernate . Criterion ;
5
6
6
7
namespace NHibernate . Test . SqlTest . Query
7
8
{
8
9
[ TestFixture ]
9
10
public class GeneralTest : TestCase
10
11
{
11
- protected const string OrganizationFetchJoinEmploymentSQL =
12
- "SELECT org.ORGID as {org.id}, " +
13
- " org.NAME as {org.name}, " +
14
- " emp.EMPLOYER as {emp.key}, " +
15
- " emp.EMPID as {emp.element}, " +
16
- " {emp.element.*} " +
17
- "FROM ORGANIZATION org " +
18
- " LEFT OUTER JOIN EMPLOYMENT emp ON org.ORGID = emp.EMPLOYER" ;
19
-
20
- protected const string OrganizationJoinEmploymentSQL =
21
- "SELECT org.ORGID as {org.id}, " +
22
- " org.NAME as {org.name}, " +
23
- " {emp.*} " +
24
- "FROM ORGANIZATION org " +
25
- " LEFT OUTER JOIN EMPLOYMENT emp ON org.ORGID = emp.EMPLOYER" ;
26
-
27
12
protected const string EmploymentSQL = "SELECT * FROM EMPLOYMENT" ;
28
13
29
14
protected string EmploymentSQLMixedScalarEntity =
@@ -295,7 +280,7 @@ public void MappedAliasStrategy()
295
280
296
281
s = OpenSession ( ) ;
297
282
t = s . BeginTransaction ( ) ;
298
- sqlQuery = s . GetNamedQuery ( "organizationreturnproperty " ) ;
283
+ sqlQuery = s . GetNamedQuery ( "organization-using-manual-aliases " ) ;
299
284
sqlQuery . SetResultTransformer ( CriteriaSpecification . AliasToEntityMap ) ;
300
285
list = sqlQuery . List ( ) ;
301
286
Assert . AreEqual ( 2 , list . Count ) ;
@@ -452,9 +437,9 @@ public void AutoDetectAliasing()
452
437
453
438
// TODO H3: H3.2 can guess the return column type so they can use just addScalar("employerid"),
454
439
// but NHibernate currently can't do it.
455
- list =
456
- s . CreateSQLQuery ( EmploymentSQLMixedScalarEntity ) . AddScalar ( "employerid" , NHibernateUtil . Int64 ) . AddEntity (
457
- typeof ( Employment ) ) . List ( ) ;
440
+ list = s . CreateSQLQuery ( EmploymentSQLMixedScalarEntity )
441
+ . AddScalar ( "employerid" , NHibernateUtil . Int64 )
442
+ . AddEntity ( typeof ( Employment ) ) . List ( ) ;
458
443
Assert . AreEqual ( 1 , list . Count ) ;
459
444
o = ( object [ ] ) list [ 0 ] ;
460
445
Assert . AreEqual ( 2 , o . Length ) ;
@@ -467,34 +452,6 @@ public void AutoDetectAliasing()
467
452
list = queryWithCollection . List ( ) ;
468
453
Assert . AreEqual ( list . Count , 1 ) ;
469
454
470
- s . Clear ( ) ;
471
-
472
- list = s . CreateSQLQuery ( OrganizationJoinEmploymentSQL )
473
- . AddEntity ( "org" , typeof ( Organization ) )
474
- . AddJoin ( "emp" , "org.employments" )
475
- . List ( ) ;
476
- Assert . AreEqual ( 2 , list . Count ) ;
477
-
478
- s . Clear ( ) ;
479
-
480
- list = s . CreateSQLQuery ( OrganizationFetchJoinEmploymentSQL )
481
- . AddEntity ( "org" , typeof ( Organization ) )
482
- . AddJoin ( "emp" , "org.employments" )
483
- . List ( ) ;
484
- Assert . AreEqual ( 2 , list . Count ) ;
485
-
486
- s . Clear ( ) ;
487
-
488
- // TODO : why twice?
489
- s . GetNamedQuery ( "organizationreturnproperty" ) . List ( ) ;
490
- list = s . GetNamedQuery ( "organizationreturnproperty" ) . List ( ) ;
491
- Assert . AreEqual ( 2 , list . Count ) ;
492
-
493
- s . Clear ( ) ;
494
-
495
- list = s . GetNamedQuery ( "organizationautodetect" ) . List ( ) ;
496
- Assert . AreEqual ( 2 , list . Count ) ;
497
-
498
455
t . Commit ( ) ;
499
456
s . Close ( ) ;
500
457
@@ -538,6 +495,99 @@ public void AutoDetectAliasing()
538
495
s . Close ( ) ;
539
496
}
540
497
498
+ public void CanQueryWithGeneratedAliasesOnly_UsingWildcard ( )
499
+ {
500
+ const string SQL =
501
+ "SELECT org.ORGID as {org.id}, " +
502
+ " org.NAME as {org.name}, " +
503
+ " {emp.*} " +
504
+ "FROM ORGANIZATION org " +
505
+ " LEFT OUTER JOIN EMPLOYMENT emp ON org.ORGID = emp.EMPLOYER" ;
506
+
507
+ VerifyOrganisationQuery ( session => session . CreateSQLQuery ( SQL )
508
+ . AddEntity ( "org" , typeof ( Organization ) )
509
+ . AddJoin ( "emp" , "org.employments" ) ) ;
510
+ }
511
+
512
+ [ Test ]
513
+ public void CanQueryWithGeneratedAliasesOnly_UsingCollectionElementWildcard ( )
514
+ {
515
+ const string SQL =
516
+ "SELECT org.ORGID as {org.id}, " +
517
+ " org.NAME as {org.name}, " +
518
+ " emp.EMPLOYER as {emp.key}, " +
519
+ " emp.EMPID as {emp.element}, " +
520
+ " {emp.element.*} " +
521
+ "FROM ORGANIZATION org " +
522
+ " LEFT OUTER JOIN EMPLOYMENT emp ON org.ORGID = emp.EMPLOYER" ;
523
+
524
+ VerifyOrganisationQuery ( session => session . CreateSQLQuery ( SQL )
525
+ . AddEntity ( "org" , typeof ( Organization ) )
526
+ . AddJoin ( "emp" , "org.employments" ) ) ;
527
+ }
528
+
529
+ [ Test ]
530
+ public void CanQueryWithColumnNamesOnly ( )
531
+ {
532
+ VerifyOrganisationQuery ( session => session . GetNamedQuery ( "organization-using-manual-aliases" ) ) ;
533
+ }
534
+
535
+ [ Test ]
536
+ public void CanQueryWithManualAliasesOnly ( )
537
+ {
538
+ VerifyOrganisationQuery ( session => session . GetNamedQuery ( "organization-using-column-names" ) ) ;
539
+ }
540
+
541
+ [ Test ]
542
+ public void CanQueryWithMixOfColumnNamesAndManualAliases ( )
543
+ {
544
+ VerifyOrganisationQuery ( session => session . GetNamedQuery ( "organization-using-column-names-and-manual-aliases" ) ) ;
545
+ }
546
+
547
+ private void VerifyOrganisationQuery ( Func < ISession , IQuery > queryFactory )
548
+ {
549
+ using ( ISession s = OpenSession ( ) )
550
+ using ( ITransaction t = s . BeginTransaction ( ) )
551
+ {
552
+ var ifa = new Organization ( "IFA" ) ;
553
+ var jboss = new Organization ( "JBoss" ) ;
554
+ var gavin = new Person ( "Gavin" ) ;
555
+ var emp = new Employment ( gavin , jboss , "AU" ) ;
556
+ s . Save ( jboss ) ;
557
+ s . Save ( ifa ) ;
558
+ s . Save ( gavin ) ;
559
+ s . Save ( emp ) ;
560
+
561
+ var list = queryFactory ( s ) . List ( ) ;
562
+ Assert . AreEqual ( 2 , list . Count ) ;
563
+ }
564
+ }
565
+
566
+ [ Test ]
567
+ public void CanQueryWithManualComponentPropertyAliases ( )
568
+ {
569
+ using ( ISession s = OpenSession ( ) )
570
+ using ( ITransaction t = s . BeginTransaction ( ) )
571
+ {
572
+ SpaceShip enterprise = new SpaceShip ( ) ;
573
+ enterprise . Model = "USS" ;
574
+ enterprise . Name = "Entreprise" ;
575
+ enterprise . Speed = 50d ;
576
+ Dimension d = new Dimension ( 45 , 10 ) ;
577
+ enterprise . Dimensions = d ;
578
+ s . Save ( enterprise ) ;
579
+
580
+ s . Flush ( ) ;
581
+ s . Clear ( ) ;
582
+
583
+ object [ ] result = ( object [ ] ) s . GetNamedQuery ( "spaceship" ) . UniqueResult ( ) ;
584
+ enterprise = ( SpaceShip ) result [ 0 ] ;
585
+ Assert . AreEqual ( 50d , enterprise . Speed , "Speed" ) ;
586
+ Assert . AreEqual ( 45 , enterprise . Dimensions . Length , "Dimensions.Length" ) ;
587
+ Assert . AreEqual ( 10 , enterprise . Dimensions . Width , "Dimensions.Width" ) ;
588
+ }
589
+ }
590
+
541
591
[ Test ]
542
592
public void MixAndMatchEntityScalar ( )
543
593
{
0 commit comments