📄 hqltest.java
字号:
assertTranslation( "from Animal an where an.bodyWeight > 10" ); // 2004-06-26 [jsd] This one requires NOT GT => LE transform. assertTranslation( "from Animal an where not an.bodyWeight > 10" ); assertTranslation( "from Animal an where an.bodyWeight between 0 and 10" ); assertTranslation( "from Animal an where an.bodyWeight not between 0 and 10" ); assertTranslation( "from Animal an where sqrt(an.bodyWeight)/2 > 10" ); // 2004-06-27 [jsd] Recognize 'is null' properly. Generate 'and' and 'or' as well. assertTranslation( "from Animal an where (an.bodyWeight > 10 and an.bodyWeight < 100) or an.bodyWeight is null" ); } public void testEscapedQuote() throws Exception { assertTranslation( "from Human h where h.nickName='1 ov''tha''few'"); } public void testCaseWhenElse() { assertTranslation( "from Human h where case when h.nickName='1ovthafew' then 'Gavin' when h.nickName='turin' then 'Christian' else h.nickName end = h.name.first" ); } public void testCaseExprWhenElse() { assertTranslation( "from Human h where case h.nickName when '1ovthafew' then 'Gavin' when 'turin' then 'Christian' else h.nickName end = h.name.first" ); } public void testInvalidHql() throws Exception { Exception newException = compileBadHql( "from Animal foo where an.bodyWeight > 10", false ); assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException ); newException = compileBadHql( "select an.name from Animal foo", false ); assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException ); newException = compileBadHql( "from Animal foo where an.verybogus > 10", false ); assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException ); newException = compileBadHql( "select an.boguspropertyname from Animal foo", false ); assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException ); newException = compileBadHql( "select an.name", false ); assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException ); newException = compileBadHql( "from Animal an where (((an.bodyWeight > 10 and an.bodyWeight < 100)) or an.bodyWeight is null", false ); assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException ); newException = compileBadHql( "from Animal an where an.bodyWeight is null where an.bodyWeight is null", false ); assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException ); newException = compileBadHql( "from where name='foo'", false ); assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException ); newException = compileBadHql( "from NonexistentClass where name='foo'", false ); assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException ); newException = compileBadHql( "select new FOO_BOGUS_Animal(an.description, an.bodyWeight) from Animal an", false ); assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException ); newException = compileBadHql( "select new Animal(an.description, an.bodyWeight, 666) from Animal an", false ); assertTrue( "Wrong exception type!", newException instanceof QuerySyntaxException ); } public void testWhereBetween() throws Exception { // 2004-08-31 [jsd] This "just worked"! Woohoo! assertTranslation( "from Animal an where an.bodyWeight between 1 and 10" ); } public void testConcatenation() { if ( getDialect() instanceof MySQLDialect || getDialect() instanceof SybaseDialect ) { // MySQL uses concat(x, y, z) // SQL Server replaces '||' with '+' // // this is syntax checked in {@link ASTParserLoadingTest#testConcatenation} return; } assertTranslation("from Human h where h.nickName = '1' || 'ov' || 'tha' || 'few'"); } public void testWhereLike() throws Exception { assertTranslation( "from Animal a where a.description like '%black%'" ); assertTranslation( "from Animal an where an.description like '%fat%'" ); assertTranslation( "from Animal an where lower(an.description) like '%fat%'" ); } public void testWhereIn() throws Exception { assertTranslation( "from Animal an where an.description in ('fat', 'skinny')" ); } public void testLiteralInFunction() throws Exception { assertTranslation( "from Animal an where an.bodyWeight > abs(5)" ); assertTranslation( "from Animal an where an.bodyWeight > abs(-5)" ); } public void testExpressionInFunction() throws Exception { assertTranslation( "from Animal an where an.bodyWeight > abs(3-5)" ); assertTranslation( "from Animal an where an.bodyWeight > abs(3/5)" ); assertTranslation( "from Animal an where an.bodyWeight > abs(3+5)" ); assertTranslation( "from Animal an where an.bodyWeight > abs(3*5)" ); SQLFunction concat = getSessionFactoryImplementor().getSqlFunctionRegistry().findSQLFunction( "concat"); List list = new ArrayList(); list.add("'fat'"); list.add("'skinny'"); assertTranslation( "from Animal an where an.description = " + concat.render(list, getSessionFactoryImplementor()) ); } public void testNotOrWhereClause() { assertTranslation( "from Simple s where 'foo'='bar' or not 'foo'='foo'" ); assertTranslation( "from Simple s where 'foo'='bar' or not ('foo'='foo')" ); assertTranslation( "from Simple s where not ( 'foo'='bar' or 'foo'='foo' )" ); assertTranslation( "from Simple s where not ( 'foo'='bar' and 'foo'='foo' )" ); assertTranslation( "from Simple s where not ( 'foo'='bar' and 'foo'='foo' ) or not ('x'='y')" ); assertTranslation( "from Simple s where not ( 'foo'='bar' or 'foo'='foo' ) and not ('x'='y')" ); assertTranslation( "from Simple s where not ( 'foo'='bar' or 'foo'='foo' ) and 'x'='y'" ); assertTranslation( "from Simple s where not ( 'foo'='bar' and 'foo'='foo' ) or 'x'='y'" ); assertTranslation( "from Simple s where 'foo'='bar' and 'foo'='foo' or not 'x'='y'" ); assertTranslation( "from Simple s where 'foo'='bar' or 'foo'='foo' and not 'x'='y'" ); assertTranslation( "from Simple s where ('foo'='bar' and 'foo'='foo') or 'x'='y'" ); assertTranslation( "from Simple s where ('foo'='bar' or 'foo'='foo') and 'x'='y'" ); assertTranslation( "from Simple s where not( upper( s.name ) ='yada' or 1=2 or 'foo'='bar' or not('foo'='foo') or 'foo' like 'bar' )" ); } public void testComplexExpressionInFunction() throws Exception { assertTranslation( "from Animal an where an.bodyWeight > abs((3-5)/4)" ); } public void testStandardFunctions() throws Exception { assertTranslation( "from Animal where current_date = current_time" ); assertTranslation( "from Animal a where upper(a.description) = 'FAT'" ); assertTranslation( "select lower(a.description) from Animal a" ); } public void testOrderBy() throws Exception { assertTranslation( "from Animal an order by an.bodyWeight" ); assertTranslation( "from Animal an order by an.bodyWeight asc" ); assertTranslation( "from Animal an order by an.bodyWeight desc" ); assertTranslation( "from Animal an order by sqrt(an.bodyWeight*4)/2" ); assertTranslation( "from Animal an order by an.mother.bodyWeight" ); assertTranslation( "from Animal an order by an.bodyWeight, an.description" ); assertTranslation( "from Animal an order by an.bodyWeight asc, an.description desc" ); if ( getDialect() instanceof HSQLDialect || getDialect() instanceof DB2Dialect ) { assertTranslation( "from Human h order by sqrt(h.bodyWeight), year(h.birthdate)" ); } } public void testGroupByFunction() { if ( getDialect() instanceof Oracle9Dialect ) return; if ( getDialect() instanceof Oracle8iDialect ) return; // the new hiearchy... if ( getDialect() instanceof PostgreSQLDialect ) return; assertTranslation( "select count(*) from Human h group by year(h.birthdate)" ); assertTranslation( "select count(*) from Human h group by trunc( sqrt(h.bodyWeight*4)/2 )" ); assertTranslation( "select count(*) from Human h group by year(sysdate)" ); } public void testPolymorphism() throws Exception { Map replacements = buildTrueFalseReplacementMapForDialect(); assertTranslation( "from Mammal" ); assertTranslation( "from Dog" ); assertTranslation( "from Mammal m where m.pregnant = false and m.bodyWeight > 10", replacements ); assertTranslation( "from Dog d where d.pregnant = false and d.bodyWeight > 10", replacements ); } private Map buildTrueFalseReplacementMapForDialect() { HashMap replacements = new HashMap(); try { String dialectTrueRepresentation = getDialect().toBooleanValueString( true ); // if this call succeeds, then the dialect is saying to represent true/false as int values... Integer.parseInt( dialectTrueRepresentation ); replacements.put( "true", "1" ); replacements.put( "false", "0" ); } catch( NumberFormatException nfe ) { // the Integer#parseInt call failed... } return replacements; } public void testTokenReplacement() throws Exception { Map replacements = buildTrueFalseReplacementMapForDialect(); assertTranslation( "from Mammal m where m.pregnant = false and m.bodyWeight > 10", replacements ); } public void testProduct() throws Exception { Map replacements = buildTrueFalseReplacementMapForDialect(); assertTranslation( "from Animal, Animal" ); assertTranslation( "from Animal x, Animal y where x.bodyWeight = y.bodyWeight" ); assertTranslation( "from Animal x, Mammal y where x.bodyWeight = y.bodyWeight and not y.pregnant = true", replacements ); assertTranslation( "from Mammal, Mammal" ); } public void testJoinedSubclassProduct() throws Exception { assertTranslation( "from PettingZoo, PettingZoo" ); //product of two subclasses } public void testProjectProduct() throws Exception { assertTranslation( "select x from Human x, Human y where x.nickName = y.nickName" ); assertTranslation( "select x, y from Human x, Human y where x.nickName = y.nickName" ); } public void testExplicitEntityJoins() throws Exception { assertTranslation( "from Animal an inner join an.mother mo" ); assertTranslation( "from Animal an left outer join an.mother mo" ); assertTranslation( "from Animal an left outer join fetch an.mother" ); } public void testMultipleExplicitEntityJoins() throws Exception { assertTranslation( "from Animal an inner join an.mother mo inner join mo.mother gm" ); assertTranslation( "from Animal an left outer join an.mother mo left outer join mo.mother gm" ); assertTranslation( "from Animal an inner join an.mother m inner join an.father f" ); assertTranslation( "from Animal an left join fetch an.mother m left join fetch an.father f" ); } public void testMultipleExplicitJoins() throws Exception { assertTranslation( "from Animal an inner join an.mother mo inner join an.offspring os" ); assertTranslation( "from Animal an left outer join an.mother mo left outer join an.offspring os" ); } public void testExplicitEntityJoinsWithRestriction() throws Exception { assertTranslation( "from Animal an inner join an.mother mo where an.bodyWeight < mo.bodyWeight" ); } public void testIdProperty() throws Exception { assertTranslation( "from Animal a where a.mother.id = 12" ); } public void testSubclassAssociation() throws Exception { assertTranslation( "from DomesticAnimal da join da.owner o where o.nickName = 'Gavin'" ); assertTranslation( "from DomesticAnimal da left join fetch da.owner" ); assertTranslation( "from Human h join h.pets p where p.pregnant = 1" ); assertTranslation( "from Human h join h.pets p where p.bodyWeight > 100" ); assertTranslation( "from Human h left join fetch h.pets" ); } public void testExplicitCollectionJoins() throws Exception { assertTranslation( "from Animal an inner join an.offspring os" ); assertTranslation( "from Animal an left outer join an.offspring os" ); } public void testExplicitOuterJoinFetch() throws Exception { assertTranslation( "from Animal an left outer join fetch an.offspring" ); } public void testExplicitOuterJoinFetchWithSelect() throws Exception { assertTranslation( "select an from Animal an left outer join fetch an.offspring" ); } public void testExplicitJoins() throws Exception { Map replacements = buildTrueFalseReplacementMapForDialect(); assertTranslation( "from Zoo zoo join zoo.mammals mam where mam.pregnant = true and mam.description like '%white%'", replacements ); assertTranslation( "from Zoo zoo join zoo.animals an where an.description like '%white%'" ); } /** * Test for HHH-559 */ public void testMultibyteCharacterConstant() throws Exception { assertTranslation( "from Zoo zoo join zoo.animals an where an.description like '%\u4e2d%'" ); } public void testImplicitJoins() throws Exception { // Two dots... assertTranslation( "from Animal an where an.mother.bodyWeight > ?" ); assertTranslation( "from Animal an where an.mother.bodyWeight > 10" ); assertTranslation( "from Dog dog where dog.mother.bodyWeight > 10" ); // Three dots... assertTranslation( "from Animal an where an.mother.mother.bodyWeight > 10" ); // The new QT doesn't throw an exception here, so this belongs in ASTQueryTranslator test. [jsd]// assertTranslation( "from Animal an where an.offspring.mother.bodyWeight > 10" ); // Is not null (unary postfix operator) assertTranslation( "from Animal an where an.mother is not null" ); // ID property shortut (no implicit join) assertTranslation( "from Animal an where an.mother.id = 123" ); } public void testImplicitJoinInSelect() { assertTranslation( "select foo, foo.long from Foo foo" ); DotNode.useThetaStyleImplicitJoins = true; assertTranslation( "select foo.foo from Foo foo" ); assertTranslation( "select foo, foo.foo from Foo foo" ); assertTranslation( "select foo.foo from Foo foo where foo.foo is not null" ); DotNode.useThetaStyleImplicitJoins = false; } public void testSelectExpressions() { DotNode.useThetaStyleImplicitJoins = true; assertTranslation( "select an.mother.mother from Animal an" ); assertTranslation( "select an.mother.mother.mother from Animal an" );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -