From d835a89b344963af6617f64b5c22906f0e6a1d25 Mon Sep 17 00:00:00 2001 From: Marvin Froeder Date: Mon, 15 Jan 2024 08:36:31 +1300 Subject: [PATCH] Require native queries to be executed from native expressions --- .../java/com/querydsl/jpa/AbstractSQLTest.java | 14 ++++++++++---- .../java/com/querydsl/jpa/JPAQueryFactoryTest.java | 3 +-- .../main/scala/com/querydsl/scala/sql/SQL.scala | 2 +- .../com/querydsl/sql/AbstractSQLQueryFactory.java | 4 ++-- .../java/com/querydsl/sql/ProjectableSQLQuery.java | 13 +++++++++++-- .../main/java/com/querydsl/sql/SQLCommonQuery.java | 2 +- .../com/querydsl/sql/SQLCommonQueryFactory.java | 5 ++--- 7 files changed, 28 insertions(+), 15 deletions(-) diff --git a/querydsl-jpa/src/test/java/com/querydsl/jpa/AbstractSQLTest.java b/querydsl-jpa/src/test/java/com/querydsl/jpa/AbstractSQLTest.java index 121bc1d9fb..4765c537e6 100644 --- a/querydsl-jpa/src/test/java/com/querydsl/jpa/AbstractSQLTest.java +++ b/querydsl-jpa/src/test/java/com/querydsl/jpa/AbstractSQLTest.java @@ -15,8 +15,8 @@ import com.querydsl.jpa.domain.Cat; import com.querydsl.jpa.domain.Color; import com.querydsl.jpa.domain.QCat; -import com.querydsl.jpa.domain.QCompany; import com.querydsl.jpa.domain.sql.SAnimal_; +import com.querydsl.jpa.domain.sql.SCompany_; import java.sql.SQLException; import java.util.Arrays; import java.util.Collections; @@ -111,8 +111,14 @@ public void entityQueries2() { @Test public void entityQueries3() { - QCat catEntity = new QCat("animal_"); - assertThat(query().from(catEntity).select(catEntity.toes.max()).fetchFirst().intValue()) + SAnimal_ catEntity = new SAnimal_("animal_"); + assertThat( + query() + .from(catEntity) + .select(catEntity.toes.max()) + .where(catEntity.dtype.eq("C")) + .fetchFirst() + .intValue()) .isEqualTo(0); } @@ -163,7 +169,7 @@ public void entityQueries6() { @Test public void entityQueries7() { - QCompany company = QCompany.company; + SCompany_ company = SCompany_.company_; assertThat(query().from(company).select(company.officialName).fetch()) .isEqualTo(Collections.emptyList()); } diff --git a/querydsl-jpa/src/test/java/com/querydsl/jpa/JPAQueryFactoryTest.java b/querydsl-jpa/src/test/java/com/querydsl/jpa/JPAQueryFactoryTest.java index 6266561a75..ed5e80d29f 100644 --- a/querydsl-jpa/src/test/java/com/querydsl/jpa/JPAQueryFactoryTest.java +++ b/querydsl-jpa/src/test/java/com/querydsl/jpa/JPAQueryFactoryTest.java @@ -17,7 +17,6 @@ import com.querydsl.jpa.domain.QAnimal; import com.querydsl.jpa.impl.JPAQueryFactory; -import com.querydsl.sql.SQLExpressions; import jakarta.persistence.EntityManager; import jakarta.persistence.EntityManagerFactory; import java.util.Date; @@ -156,7 +155,7 @@ public void insert4() { .insert(QAnimal.animal) .columns(QAnimal.animal.id, QAnimal.animal.birthdate) .select( - SQLExpressions.select(QAnimal.animal.id, QAnimal.animal.birthdate) + JPAExpressions.select(QAnimal.animal.id, QAnimal.animal.birthdate) .from(QAnimal.animal)); } } diff --git a/querydsl-scala/src/main/scala/com/querydsl/scala/sql/SQL.scala b/querydsl-scala/src/main/scala/com/querydsl/scala/sql/SQL.scala index 6500dbe27b..b32d1fe396 100644 --- a/querydsl-scala/src/main/scala/com/querydsl/scala/sql/SQL.scala +++ b/querydsl-scala/src/main/scala/com/querydsl/scala/sql/SQL.scala @@ -46,7 +46,7 @@ trait SQL extends SQLHelpers { def query() = new SQLQuery(connection, templates) - def from(expr: Expression[_]*) = query.from(expr:_*) + def from(expr: RelationalPath[_]*) = query.from(expr:_*) def insert(path: RelationalPath[_]) = new SQLInsertClause(connection, templates, path) diff --git a/querydsl-sql/src/main/java/com/querydsl/sql/AbstractSQLQueryFactory.java b/querydsl-sql/src/main/java/com/querydsl/sql/AbstractSQLQueryFactory.java index 988d05b612..222f92d816 100644 --- a/querydsl-sql/src/main/java/com/querydsl/sql/AbstractSQLQueryFactory.java +++ b/querydsl-sql/src/main/java/com/querydsl/sql/AbstractSQLQueryFactory.java @@ -51,13 +51,13 @@ public final SQLDeleteClause delete(RelationalPath path) { @SuppressWarnings("unchecked") @Override - public final Q from(Expression from) { + public final Q from(RelationalPath from) { return (Q) query().from(from); } @SuppressWarnings("unchecked") @Override - public final Q from(Expression... args) { + public final Q from(RelationalPath... args) { return (Q) query().from(args); } diff --git a/querydsl-sql/src/main/java/com/querydsl/sql/ProjectableSQLQuery.java b/querydsl-sql/src/main/java/com/querydsl/sql/ProjectableSQLQuery.java index f166b2874e..f7f1e1081e 100644 --- a/querydsl-sql/src/main/java/com/querydsl/sql/ProjectableSQLQuery.java +++ b/querydsl-sql/src/main/java/com/querydsl/sql/ProjectableSQLQuery.java @@ -150,12 +150,21 @@ public long fetchCount() { return ((Number) fetchOne()).longValue(); } - public Q from(Expression arg) { + protected Q from(Expression arg) { return queryMixin.from(arg); } + public Q from(RelationalPath arg) { + return queryMixin.from(arg); + } + + public Q from(RelationalPath arg, Expression... args) { + queryMixin.from(arg); + return queryMixin.from(args); + } + @Override - public Q from(Expression... args) { + public Q from(RelationalPath... args) { return queryMixin.from(args); } diff --git a/querydsl-sql/src/main/java/com/querydsl/sql/SQLCommonQuery.java b/querydsl-sql/src/main/java/com/querydsl/sql/SQLCommonQuery.java index 3f69c02203..a581add78f 100644 --- a/querydsl-sql/src/main/java/com/querydsl/sql/SQLCommonQuery.java +++ b/querydsl-sql/src/main/java/com/querydsl/sql/SQLCommonQuery.java @@ -78,7 +78,7 @@ public interface SQLCommonQuery> extends Query { * @param o from * @return the current object */ - Q from(Expression... o); + Q from(RelationalPath... o); /** * Adds a sub query source diff --git a/querydsl-sql/src/main/java/com/querydsl/sql/SQLCommonQueryFactory.java b/querydsl-sql/src/main/java/com/querydsl/sql/SQLCommonQueryFactory.java index cd271f695a..8e22fc7376 100644 --- a/querydsl-sql/src/main/java/com/querydsl/sql/SQLCommonQueryFactory.java +++ b/querydsl-sql/src/main/java/com/querydsl/sql/SQLCommonQueryFactory.java @@ -14,7 +14,6 @@ package com.querydsl.sql; import com.querydsl.core.QueryFactory; -import com.querydsl.core.types.Expression; import com.querydsl.core.types.Path; import com.querydsl.core.types.SubQueryExpression; import com.querydsl.sql.dml.SQLDeleteClause; @@ -57,7 +56,7 @@ public interface SQLCommonQueryFactory< * @param from query source * @return query */ - Q from(Expression from); + Q from(RelationalPath from); /** * Create a new SELECT query @@ -65,7 +64,7 @@ public interface SQLCommonQueryFactory< * @param from query sources * @return query */ - Q from(Expression... from); + Q from(RelationalPath... from); /** * Create a new SELECT query