Skip to content

Commit

Permalink
current solution that has a working query but tests need to be fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
lpandzic committed Mar 12, 2024
1 parent c459fc1 commit c9e724b
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.infobip.spring.data.r2dbc;

import com.querydsl.sql.MySQLTemplates;
import com.querydsl.sql.SQLTemplates;
import io.r2dbc.spi.Parameters;
import org.springframework.r2dbc.core.DatabaseClient;
import org.springframework.r2dbc.core.binding.BindMarkersFactory;
Expand All @@ -9,12 +11,33 @@
class QuerydslParameterBinder {

private final BindMarkersFactory bindMarkersFactory;
private final boolean useNumberedBindParameters;

QuerydslParameterBinder(BindMarkersFactory bindMarkersFactory) {
QuerydslParameterBinder(BindMarkersFactory bindMarkersFactory, SQLTemplates sqlTemplates) {
this.bindMarkersFactory = bindMarkersFactory;
this.useNumberedBindParameters = resolve(sqlTemplates);
}

private boolean resolve(SQLTemplates sqlTemplates) {

if(sqlTemplates instanceof MySQLTemplates) {
return true;
}

return false;
}

DatabaseClient.GenericExecuteSpec bind(DatabaseClient databaseClient, List<Object> bindings, String sql) {

if(useNumberedBindParameters) {
var spec = databaseClient.sql(sql);
var index = 0;
for (Object binding : bindings) {
spec = spec.bind(index++, binding);
}
return spec;
}

var parameterNameToParameterValue = parameterNameToParameterValue(bindings);
var sqlWithParameterNames = getSqlWithParameterNames(parameterNameToParameterValue, sql);
var spec = databaseClient.sql(sqlWithParameterNames);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ public class QuerydslR2dbcRepositoryFactory extends R2dbcRepositoryFactory {

public QuerydslR2dbcRepositoryFactory(R2dbcEntityOperations operations,
SQLQueryFactory sqlQueryFactory,
DatabaseClient databaseClient) {
DatabaseClient databaseClient,
SQLTemplates sqlTemplates) {
super(operations);
this.sqlQueryFactory = sqlQueryFactory;
this.converter = operations.getConverter();
this.databaseClient = databaseClient;
this.querydslParameterBinder = new QuerydslParameterBinder(
BindMarkersFactoryResolver.resolve(databaseClient.getConnectionFactory()));
BindMarkersFactoryResolver.resolve(databaseClient.getConnectionFactory()), sqlTemplates);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.infobip.spring.data.r2dbc;

import com.querydsl.sql.SQLQueryFactory;
import com.querydsl.sql.SQLTemplates;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.r2dbc.core.R2dbcEntityOperations;
import org.springframework.data.r2dbc.repository.support.R2dbcRepositoryFactoryBean;
Expand All @@ -30,14 +31,15 @@ public class QuerydslR2dbcRepositoryFactoryBean<T extends Repository<S, ID>, S,

private SQLQueryFactory sqlQueryFactory;
private DatabaseClient databaseClient;
private SQLTemplates sqlTemplates;

protected QuerydslR2dbcRepositoryFactoryBean(Class<? extends T> repositoryInterface) {
super(repositoryInterface);
}

@Override
protected RepositoryFactorySupport getFactoryInstance(R2dbcEntityOperations operations) {
return new QuerydslR2dbcRepositoryFactory(operations, sqlQueryFactory, databaseClient);
return new QuerydslR2dbcRepositoryFactory(operations, sqlQueryFactory, databaseClient, sqlTemplates);
}

@Autowired
Expand All @@ -50,4 +52,9 @@ public void setDatabaseClient(DatabaseClient databaseClient) {
super.setDatabaseClient(databaseClient);
this.databaseClient = databaseClient;
}

@Autowired
public void setSqlTemplates(SQLTemplates sqlTemplates) {
this.sqlTemplates = sqlTemplates;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

import com.infobip.spring.data.r2dbc.*;
import lombok.AllArgsConstructor;
import org.assertj.core.api.BDDAssertions;
import org.junit.jupiter.api.Test;
import org.springframework.r2dbc.core.DatabaseClient;
import org.springframework.test.context.*;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import java.time.Duration;
import java.util.function.Predicate;

import static com.infobip.spring.data.r2dbc.QPerson.person;
import static org.assertj.core.api.BDDAssertions.then;

@AllArgsConstructor
@ContextConfiguration(loader = MssqlExclusionContextLoader.class)
Expand All @@ -19,9 +21,40 @@
public class MysqlNamedParametersSupportTest extends TestBase {

private final PersonRepository repository;
private final DatabaseClient databaseClient;

@Test
void shouldNotFail() {
void shouldNotFailCustomQuery() {
// given
var given = given(givenSavedPerson("John", "Doe"),
givenSavedPerson("Johny", "Roe"),
givenSavedPerson("Jane", "Doe"),
givenSavedPerson("John", "Roe"),
givenSavedPerson("Janie", "Doe")).block(Duration.ofSeconds(10));

// when
var actual = databaseClient.sql("""
select *
from person
where FirstName IN (?, ?)
order by FirstName ASC, LastName
ASC LIMIT ? OFFSET ?
""")
.bind(0, "John")
.bind(1, "Jane")
.bind(2, 1)
.bind(3, 1)
.fetch()
.all()
.collectList();

// then
var all = actual.block(Duration.ofSeconds(10));
then(all).isNotEmpty();
}

@Test
void shouldNotFailQuerydslQuery() {
// given
var given = given(givenSavedPerson("John", "Doe"),
givenSavedPerson("Johny", "Roe"),
Expand Down Expand Up @@ -51,7 +84,7 @@ private Mono<Person> givenSavedPerson(String firstName, String lastName) {

private Predicate<? super Person> person(String firstName, String lastName) {
return person -> {
BDDAssertions.then(person).isEqualTo(new Person(person.id(), firstName, lastName));
then(person).isEqualTo(new Person(person.id(), firstName, lastName));
return true;
};
}
Expand Down

0 comments on commit c9e724b

Please sign in to comment.