1+ using System ;
12using System . Collections ;
3+ using NHibernate . Criterion ;
24using NHibernate . Transform ;
35using NUnit . Framework ;
4- using NHibernate . Criterion ;
56
67namespace NHibernate . Test . SqlTest . Query
78{
89 [ TestFixture ]
910 public class GeneralTest : TestCase
1011 {
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-
2712 protected const string EmploymentSQL = "SELECT * FROM EMPLOYMENT" ;
2813
2914 protected string EmploymentSQLMixedScalarEntity =
@@ -295,7 +280,7 @@ public void MappedAliasStrategy()
295280
296281 s = OpenSession ( ) ;
297282 t = s . BeginTransaction ( ) ;
298- sqlQuery = s . GetNamedQuery ( "organizationreturnproperty " ) ;
283+ sqlQuery = s . GetNamedQuery ( "organization-using-manual-aliases " ) ;
299284 sqlQuery . SetResultTransformer ( CriteriaSpecification . AliasToEntityMap ) ;
300285 list = sqlQuery . List ( ) ;
301286 Assert . AreEqual ( 2 , list . Count ) ;
@@ -452,9 +437,9 @@ public void AutoDetectAliasing()
452437
453438 // TODO H3: H3.2 can guess the return column type so they can use just addScalar("employerid"),
454439 // 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 ( ) ;
458443 Assert . AreEqual ( 1 , list . Count ) ;
459444 o = ( object [ ] ) list [ 0 ] ;
460445 Assert . AreEqual ( 2 , o . Length ) ;
@@ -467,34 +452,6 @@ public void AutoDetectAliasing()
467452 list = queryWithCollection . List ( ) ;
468453 Assert . AreEqual ( list . Count , 1 ) ;
469454
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-
498455 t . Commit ( ) ;
499456 s . Close ( ) ;
500457
@@ -538,6 +495,99 @@ public void AutoDetectAliasing()
538495 s . Close ( ) ;
539496 }
540497
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+
541591 [ Test ]
542592 public void MixAndMatchEntityScalar ( )
543593 {
0 commit comments