question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support dynamic SQL provider [DATAJDBC-319]

See original GitHub issue

Sanghyuk Jung opened DATAJDBC-319 and commented

In MyBatis, Dynamic sql is supported by @SelectProvider

http://kamalmeet.com/java/mybatis-using-selectprovider-and-resultmap/ ( updated link)

It would be more convenient if a similar feature is in Spring Data JDBC


No further details from DATAJDBC-319

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:8

github_iconTop GitHub Comments

3reactions
chenjianjxcommented, Nov 3, 2021

+1

2reactions
spring-projects-issuescommented, Dec 31, 2020

Sanghyuk Jung commented

As I mentioned, MyBatis supports a similar feature, it can be a reference.

Following examples are from http://www.mybatis.org/mybatis-3/java-api.html .

@SelectProvider(type = UserSqlBuilder.class, method = "buildGetUsersByName")
List<User> getUsersByName(
    @Param("name") String name, @Param("orderByColumn") String orderByColumn);

class UserSqlBuilder {

  // If not use @Param, you should be define same arguments with mapper method
  public static String buildGetUsersByName(
      final String name, final String orderByColumn) {
    return new SQL(){{
      SELECT("*");
      FROM("users");
      WHERE("name like #{name} || '%'");
      ORDER_BY(orderByColumn);
    }}.toString();
  }

  // If use @Param, you can define only arguments to be used
  public static String buildGetUsersByName(@Param("orderByColumn") final String orderByColumn) {
    return new SQL(){{
      SELECT("*");
      FROM("users");
      WHERE("name like #{name} || '%'");
      ORDER_BY(orderByColumn);
    }}.toString();
  }
}

The implementation of MyBatis is at https://github.com/mybatis/mybatis-3/blob/master/src/main/java/org/apache/ibatis/builder/annotation/ProviderSqlSource.java#L106

The ways of MyBatis has the disadvantage of weak inspection at the time of compilation.

An alternative is to define a stricter interface , as shown below.

//  This interface is assumed to be provided by Spring Data JDBC.
public interface QueryProvider {
    String getQuery(SqlParameterSource source);

    default boolean isNotNull(SqlParameterSource source, String paramName){
        return source.hasValue(paramName) && (source.getValue(paramName) != null);
    }
    // Another helper methods can be added as default methods, for example 'isNotEmpty()'
}


// An  implementation class of QueryProvider by the framework user
public class ProductQueryProvider implements QueryProvider {
    @Override
    public String getQuery(SqlParameterSource source) {
        String sql = "SELECT id name FROM product";
        if (isNotNull(source, "name")) {
            return sql + " WHERE name = :name";
        }
        return sql;
    }
}

//  The QueryProvider's implementation class is specified in the Repository interface, similar to the RowMapper's implementation class.

public interface ProductRepository extends CrudRepository<Product, String> {
       @Query(rowMapper=ProducMapper.class, queryProvider = productSelectProvider.class)
	List<Product> findByName(@Param("name") String name);
}

When infrastructure for semantic SQL generation ( https://jira.spring.io/browse/DATAJDBC-309) is merged in the future, this feature will create a larger synergy.

If the specifications are confirmed here, I am willing to send pull request

Read more comments on GitHub >

github_iconTop Results From Across the Web

Support dynamic SQL provider [DATAJDBC-319] #542 - GitHub
If I understand correctly you want an API for generating SQL dynamically. While this is perfectly reasonable Spring Data JDBC won't offer that....
Read more >
Microsoft OLE DB Provider for SQL Server Overview
The provider supports several provider-specific connection parameters in addition to those defined by ADO. As with the ADO connection properties ...
Read more >
MyBatis Dynamic SQL
This library is a framework for generating dynamic SQL statements. Think of it as a typesafe SQL templating library, with additional support ......
Read more >
Dynamic SQL in SQL Server
In this article, we will review how to construct and execute dynamic SQL statements in SQL Server with different examples.
Read more >
SQLProvider - fsprojects on GitHub
These are dynamically loaded at runtime so that the SQL provider project is not ... ShipName // arbitrarily complex projections are supported select...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found