Skip to content

Commit

Permalink
Require native queries to be executed from native expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
velo committed Jan 14, 2024
1 parent cdcf44a commit 19ec723
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 17 deletions.
14 changes: 10 additions & 4 deletions querydsl-jpa/src/test/java/com/querydsl/jpa/AbstractSQLTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ private Export() {}

public static void main(String[] args) throws Exception {
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/querydsl";
Connection conn = DriverManager.getConnection(url, "querydsl", "querydsl");
String url = "jdbc:postgresql://localhost:54321/oodachi-querydsl?currentSchema=public";
Connection conn = DriverManager.getConnection(url, "postgres", "123456");

MetaDataExporter exporter = new MetaDataExporter();
exporter.setNamePrefix("S");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public interface SQLCommonQuery<Q extends SQLCommonQuery<Q>> extends Query<Q> {
* @param o from
* @return the current object
*/
Q from(Expression<?>... o);
Q from(RelationalPath<?>... o);

/**
* Adds a sub query source
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -57,15 +56,15 @@ public interface SQLCommonQueryFactory<
* @param from query source
* @return query
*/
Q from(Expression<?> from);
Q from(RelationalPath<?> from);

/**
* Create a new SELECT query
*
* @param from query sources
* @return query
*/
Q from(Expression<?>... from);
Q from(RelationalPath<?>... from);

/**
* Create a new SELECT query
Expand Down

0 comments on commit 19ec723

Please sign in to comment.