#768345 : fixed COUNT handling for MySQL

This commit is contained in:
Timo Westkämper 2011-04-21 13:55:53 +00:00
parent 74ea785095
commit df4013b5f8
13 changed files with 268 additions and 189 deletions

View File

@ -16,14 +16,14 @@
<packaging>jar</packaging>
<properties>
<hibernate.version>3.5.1-Final</hibernate.version>
<hibernate.validator.version>4.0.2.GA</hibernate.validator.version>
<hibernate.version>3.6.1.Final</hibernate.version>
<hibernate.validator.version>4.1.0.Final</hibernate.validator.version>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
<exclusions>
<exclusion>
@ -47,7 +47,13 @@
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate.validator.version}</version>
<scope>provided</scope>
<scope>provided</scope>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
@ -93,7 +99,8 @@
<version>2.3.0-M6</version>
<scope>test</scope>
</dependency>
<!--
<dependency>
<groupId>org.apache.openjpa</groupId>
<artifactId>openjpa-persistence</artifactId>
@ -106,7 +113,7 @@
<version>2.1.0</version>
<scope>test</scope>
</dependency>
-->
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>

View File

@ -42,7 +42,8 @@ public abstract class AbstractSQLQuery<T extends AbstractSQLQuery<T>> extends Pr
@Override
public long count() {
return uniqueResult(Wildcard.countAsInt);
Number number = uniqueResult(Wildcard.countAsInt);
return number.longValue();
}
@Override

View File

@ -474,4 +474,10 @@ public abstract class AbstractStandardTest {
query().from(show).where(subQuery().from(show2).where(show2.id.ne(show.id)).count().gt(0)).count();
}
@Test
public void Count(){
QShow show = QShow.show;
assertTrue(query().from(show).count() > 0);
}
}

View File

@ -5,17 +5,14 @@
*/
package com.mysema.query;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.hibernate.type.DoubleType;
@SuppressWarnings("serial")
public class ExtDoubleType extends DoubleType{
@Override
public void set(PreparedStatement st, Object value, int index) throws SQLException {
st.setDouble( index, ( (Number) value ).doubleValue() );
}
// @Override
// public void set(PreparedStatement st, Object value, int index) throws SQLException {
// st.setDouble( index, ( (Number) value ).doubleValue() );
// }
}

View File

@ -5,7 +5,6 @@
*/
package com.mysema.query._mysql;
import org.junit.Ignore;
import org.junit.runner.RunWith;
import com.mysema.query.AbstractHibernateTest;
@ -13,7 +12,6 @@ import com.mysema.query.Target;
import com.mysema.testutil.HibernateConfig;
import com.mysema.testutil.HibernateTestRunner;
@Ignore
@RunWith(HibernateTestRunner.class)
@HibernateConfig("mysql.properties")
public class MySQLStandardTest extends AbstractHibernateTest{

View File

@ -42,8 +42,10 @@ public class IntegrationTest extends ParsingTest {
private Session session;
@Override
protected QueryHelper query() {
return new QueryHelper() {
@Override
public void parse() throws RecognitionException, TokenStreamException {
try {
System.out.println("query : " + toString().replace('\n', ' '));
@ -65,12 +67,20 @@ public class IntegrationTest extends ParsingTest {
public void GroupBy() throws Exception {
// NOTE : commented out, because HQLSDB doesn't support these queries
}
@Override
@Test
public void GroupBy_2() throws Exception {
// NOTE : commented out, because HQLSDB doesn't support these queries
}
@Override
@Test
public void OrderBy() throws Exception {
// NOTE : commented out, because HQLSDB doesn't support these queries
}
@Override
@Test
public void DocoExamples910() throws Exception {
// NOTE : commented out, because HQLSDB doesn't support these queries

View File

@ -30,8 +30,10 @@ public class JPAIntegrationTest extends ParsingTest {
private EntityManager entityManager;
@Override
protected QueryHelper query() {
return new QueryHelper() {
@Override
public void parse() throws RecognitionException, TokenStreamException {
try {
System.out.println("query : " + toString().replace('\n', ' '));
@ -57,12 +59,20 @@ public class JPAIntegrationTest extends ParsingTest {
public void GroupBy() throws Exception {
// NOTE : commented out, because HQLSDB doesn't support these queries
}
@Override
@Test
public void GroupBy_2() throws Exception {
// NOTE : commented out, because HQLSDB doesn't support these queries
}
@Override
@Test
public void OrderBy() throws Exception {
// NOTE : commented out, because HQLSDB doesn't support these queries
}
@Override
@Test
public void DocoExamples910() throws Exception {
// NOTE : commented out, because HQLSDB doesn't support these queries

View File

@ -10,17 +10,20 @@ import java.util.Map;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
/**
* The Class Show.
*/
@Entity
@Table(name="show_")
public class Show {
@ElementCollection
public Map<String, String> acts;
@Id
public int id;
long id;
@ElementCollection
public Map<String, String> acts;
public Show() {}

View File

@ -0,0 +1,169 @@
package com.mysema.query.jpa.hibernate.sql;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.List;
import org.hibernate.Session;
import org.junit.Before;
import org.junit.Test;
import com.mysema.query.SearchResults;
import com.mysema.query.jpa.domain.Cat;
import com.mysema.query.jpa.domain.QCat;
import com.mysema.query.jpa.domain.sql.SAnimal;
import com.mysema.query.sql.DerbyTemplates;
import com.mysema.query.sql.SQLTemplates;
import com.mysema.query.types.ConstructorExpression;
import com.mysema.query.types.Expression;
public abstract class AbstractSQLTest {
private final SAnimal cat = new SAnimal("cat");
private static final SQLTemplates derbyTemplates = new DerbyTemplates();
private Session session;
protected HibernateSQLQuery query(){
return new HibernateSQLQuery(session, derbyTemplates);
}
public void setSession(Session session) {
this.session = session;
}
@Before
public void setUp(){
session.save(new Cat("Beck",1));
session.save(new Cat("Kate",2));
session.save(new Cat("Kitty",3));
session.save(new Cat("Bobby",4));
session.save(new Cat("Harold",5));
session.save(new Cat("Tim",6));
session.flush();
}
@Test
public void In(){
assertEquals(6l, query().from(cat).where(cat.dtype.in("C", "CX")).count());
}
@Test
public void Count(){
assertEquals(6l, query().from(cat).where(cat.dtype.eq("C")).count());
}
@Test
public void Count_Via_Unique(){
assertEquals(Integer.valueOf(6), query().from(cat).where(cat.dtype.eq("C")).uniqueResult(cat.id.count()));
}
@Test
public void CountDistinct(){
assertEquals(6l, query().from(cat).where(cat.dtype.eq("C")).countDistinct());
}
@Test
public void List(){
assertEquals(6, query().from(cat).where(cat.dtype.eq("C")).list(cat.id).size());
}
@Test
public void List_With_Limit(){
assertEquals(3, query().from(cat).limit(3).list(cat.id).size());
}
@Test
public void List_With_Offset(){
assertEquals(3, query().from(cat).offset(3).list(cat.id).size());
}
@Test
public void List_Limit_And_Offset(){
assertEquals(3, query().from(cat).offset(3).limit(3).list(cat.id).size());
}
@Test
public void List_Multiple(){
print(query().from(cat).where(cat.dtype.eq("C")).list(cat.id, cat.name, cat.bodyweight));
}
@Test
public void List_Results(){
SearchResults<String> results = query().from(cat).limit(3).orderBy(cat.name.asc()).listResults(cat.name);
assertEquals(Arrays.asList("Beck","Bobby","Harold"), results.getResults());
assertEquals(6l, results.getTotal());
}
@Test
public void Unique_Result(){
query().from(cat).limit(1).uniqueResult(cat.id);
}
@Test
public void Unique_Result_Multiple(){
query().from(cat).limit(1).uniqueResult(new Expression[]{cat.id});
}
@Test
public void Single_Result(){
query().from(cat).singleResult(cat.id);
}
@Test
public void Single_Result_Multiple(){
query().from(cat).singleResult(new Expression[]{cat.id});
}
@Test
public void EntityQueries(){
SAnimal cat = new SAnimal("cat");
SAnimal mate = new SAnimal("mate");
QCat catEntity = QCat.cat;
// 1
List<Cat> cats = query().from(cat).orderBy(cat.name.asc()).list(catEntity);
assertEquals(6, cats.size());
for (Cat c : cats) System.out.println(c.getName());
// 2
cats = query().from(cat)
.innerJoin(mate).on(cat.mateId.eq(mate.id))
.where(cat.dtype.eq("C"), mate.dtype.eq("C"))
.list(catEntity);
assertTrue(cats.isEmpty());
}
@Test
public void EntityProjections(){
SAnimal cat = new SAnimal("cat");
List<Cat> cats = query().from(cat).orderBy(cat.name.asc())
.list(ConstructorExpression.create(Cat.class, cat.name, cat.id));
assertEquals(6, cats.size());
for (Cat c : cats) System.out.println(c.getName());
}
@Test
public void Wildcard(){
SAnimal cat = new SAnimal("cat");
List<Object[]> rows = query().from(cat).list(cat.all());
assertEquals(6, rows.size());
print(rows);
// rows = query().from(cat).list(cat.id, cat.all());
// assertEquals(6, rows.size());
// print(rows);
}
private void print(Iterable<Object[]> rows){
for (Object[] row : rows){
System.out.println(Arrays.asList(row));
}
}
}

View File

@ -5,175 +5,14 @@
*/
package com.mysema.query.jpa.hibernate.sql;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.util.Arrays;
import java.util.List;
import org.hibernate.Session;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.mysema.query.SearchResults;
import com.mysema.query.jpa.domain.Cat;
import com.mysema.query.jpa.domain.QCat;
import com.mysema.query.jpa.domain.sql.SAnimal;
import com.mysema.query.sql.DerbyTemplates;
import com.mysema.query.sql.SQLTemplates;
import com.mysema.query.types.ConstructorExpression;
import com.mysema.query.types.Expression;
import com.mysema.testutil.HibernateConfig;
import com.mysema.testutil.HibernateTestRunner;
@RunWith(HibernateTestRunner.class)
@HibernateConfig("derby.properties")
public class DerbySQLTest {
public class DerbySQLTest extends AbstractSQLTest{
private SAnimal cat = new SAnimal("cat");
private static final SQLTemplates derbyTemplates = new DerbyTemplates();
private Session session;
protected HibernateSQLQuery query(){
return new HibernateSQLQuery(session, derbyTemplates);
}
public void setSession(Session session) {
this.session = session;
}
@Before
public void setUp(){
session.save(new Cat("Beck",1));
session.save(new Cat("Kate",2));
session.save(new Cat("Kitty",3));
session.save(new Cat("Bobby",4));
session.save(new Cat("Harold",5));
session.save(new Cat("Tim",6));
session.flush();
}
@Test
public void In(){
assertEquals(6l, query().from(cat).where(cat.dtype.in("C", "CX")).count());
}
@Test
public void Count(){
assertEquals(6l, query().from(cat).where(cat.dtype.eq("C")).count());
}
@Test
public void Count_Via_Unique(){
assertEquals(Integer.valueOf(6), query().from(cat).where(cat.dtype.eq("C")).uniqueResult(cat.id.count()));
}
@Test
public void CountDistinct(){
assertEquals(6l, query().from(cat).where(cat.dtype.eq("C")).countDistinct());
}
@Test
public void List(){
assertEquals(6, query().from(cat).where(cat.dtype.eq("C")).list(cat.id).size());
}
@Test
public void List_With_Limit(){
assertEquals(3, query().from(cat).limit(3).list(cat.id).size());
}
@Test
public void List_With_Offset(){
assertEquals(3, query().from(cat).offset(3).list(cat.id).size());
}
@Test
public void List_Limit_And_Offset(){
assertEquals(3, query().from(cat).offset(3).limit(3).list(cat.id).size());
}
@Test
public void List_Multiple(){
print(query().from(cat).where(cat.dtype.eq("C")).list(cat.id, cat.name, cat.bodyweight));
}
@Test
public void List_Results(){
SearchResults<String> results = query().from(cat).limit(3).orderBy(cat.name.asc()).listResults(cat.name);
assertEquals(Arrays.asList("Beck","Bobby","Harold"), results.getResults());
assertEquals(6l, results.getTotal());
}
@Test
public void Unique_Result(){
query().from(cat).limit(1).uniqueResult(cat.id);
}
@Test
public void Unique_Result_Multiple(){
query().from(cat).limit(1).uniqueResult(new Expression[]{cat.id});
}
@Test
public void Single_Result(){
query().from(cat).singleResult(cat.id);
}
@Test
public void Single_Result_Multiple(){
query().from(cat).singleResult(new Expression[]{cat.id});
}
@Test
public void EntityQueries(){
SAnimal cat = new SAnimal("cat");
SAnimal mate = new SAnimal("mate");
QCat catEntity = QCat.cat;
// 1
List<Cat> cats = query().from(cat).orderBy(cat.name.asc()).list(catEntity);
assertEquals(6, cats.size());
for (Cat c : cats) System.out.println(c.getName());
// 2
cats = query().from(cat)
.innerJoin(mate).on(cat.mateId.eq(mate.id))
.where(cat.dtype.eq("C"), mate.dtype.eq("C"))
.list(catEntity);
assertTrue(cats.isEmpty());
}
@Test
public void EntityProjections(){
SAnimal cat = new SAnimal("cat");
List<Cat> cats = query().from(cat).orderBy(cat.name.asc())
.list(ConstructorExpression.create(Cat.class, cat.name, cat.id));
assertEquals(6, cats.size());
for (Cat c : cats) System.out.println(c.getName());
}
@Test
public void Wildcard(){
SAnimal cat = new SAnimal("cat");
List<Object[]> rows = query().from(cat).list(cat.all());
assertEquals(6, rows.size());
print(rows);
// rows = query().from(cat).list(cat.id, cat.all());
// assertEquals(6, rows.size());
// print(rows);
}
private void print(Iterable<Object[]> rows){
for (Object[] row : rows){
System.out.println(Arrays.asList(row));
}
}
}

View File

@ -0,0 +1,37 @@
package com.mysema.query.jpa.hibernate.sql;
import static com.mysema.query.types.PathMetadataFactory.forVariable;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.mysema.query.sql.RelationalPathBase;
import com.mysema.query.sql.Table;
import com.mysema.query.types.path.NumberPath;
import com.mysema.testutil.HibernateConfig;
import com.mysema.testutil.HibernateTestRunner;
@RunWith(HibernateTestRunner.class)
@HibernateConfig("mysql.properties")
public class MySQLSQLTest extends AbstractSQLTest {
@Table("Animal")
public class SAnimal extends RelationalPathBase<SAnimal> {
public final NumberPath<Integer> id = createNumber("id", Integer.class);
public SAnimal(String variable) {
super(SAnimal.class, forVariable(variable));
}
}
@Override
@Test
public void Count() {
query().from(new SAnimal("cat")).count();
query().from(new SAnimal("cat")).countDistinct();
}
}

View File

@ -8,11 +8,12 @@ package com.mysema.query.jpa.support;
import java.util.Arrays;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.dialect.DerbyDialect;
import org.hibernate.dialect.function.CastFunction;
import org.hibernate.dialect.function.VarArgsSQLFunction;
import org.hibernate.engine.SessionFactoryImplementor;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.Type;
/**
* @author tiwe
@ -21,18 +22,19 @@ import org.hibernate.engine.SessionFactoryImplementor;
public class ExtendedDerbyDialect extends DerbyDialect{
private static final CastFunction castFunction = new CastFunction(){
@Override
@SuppressWarnings("unchecked")
public String render(List args, SessionFactoryImplementor factory) {
public String render(Type columnType, List args, SessionFactoryImplementor factory) {
if (args.get(1).equals("string")){
return super.render(Arrays.<Object>asList("char("+args.get(0)+")",args.get(1)), factory);
return super.render(columnType, Arrays.<Object>asList("char("+args.get(0)+")",args.get(1)), factory);
}else{
return super.render(args, factory);
return super.render(columnType, args, factory);
}
}
};
public ExtendedDerbyDialect(){
registerFunction( "concat", new VarArgsSQLFunction( Hibernate.STRING, "cast ((","||",") as varchar(128))" ) );
registerFunction( "concat", new VarArgsSQLFunction( StandardBasicTypes.STRING, "cast ((","||",") as varchar(128))" ) );
registerFunction( "cast", castFunction );
}

View File

@ -5,9 +5,9 @@
*/
package com.mysema.query.jpa.support;
import org.hibernate.Hibernate;
import org.hibernate.dialect.HSQLDialect;
import org.hibernate.dialect.function.SQLFunctionTemplate;
import org.hibernate.type.StandardBasicTypes;
/**
* @author tiwe
@ -16,6 +16,6 @@ import org.hibernate.dialect.function.SQLFunctionTemplate;
public class ExtendedHSQLDialect extends HSQLDialect{
public ExtendedHSQLDialect() {
registerFunction( "trim", new SQLFunctionTemplate( Hibernate.STRING, "trim(both from ?1)" ) );
registerFunction( "trim", new SQLFunctionTemplate( StandardBasicTypes.STRING, "trim(both from ?1)" ) );
}
}