Giter Club home page Giter Club logo

Comments (7)

memento avatar memento commented on June 4, 2024 1

@darrachequesne Thanks a lot, it works !

For anyone who could be struggling to accomplish it, here is my temporary code down below with some explanations. I think I'll commit something cleaner soon on my fork of spring-data-jpa-datatables.
My datatables table contains a column named firstName (first_name in my H2 USER table).
For now, it allows filtering using regular expression on that same firstName column. Filtering of this column is (always) done using regular expression. Filtering of the other columns is done using standard LIKE mode.

From my UserRepository interface (extending the DataTablesRepository<User, Long>), I override the 2 following methods :

       @Override
	default DataTablesOutput<User> findAll(DataTablesInput input) {
		System.out.println("Local implementation of DataTablesRepositoryImpl");
		return findAll(input, null, null, null);
	}

	@SuppressWarnings("unchecked")
	@Override
	default <R> DataTablesOutput<R> findAll(DataTablesInput input, Specification<User> additionalSpecification,
			Specification<User> preFilteringSpecification, Converter<User, R> converter) {
		DataTablesOutput<User> output = new DataTablesOutput<User>();
	    output.setDraw(input.getDraw());
	    if (input.getLength() == 0) {
	      return (DataTablesOutput<R>) output;
	    }

	    try {
	      long recordsTotal =
	          preFilteringSpecification == null ? count() : count(preFilteringSpecification);
	      if (recordsTotal == 0) {
	        return (DataTablesOutput<R>) output;
	      }
	      output.setRecordsTotal(recordsTotal);
	      
	      Column column = input.getColumn("firstName");
	      column.setSearchable(false); // so the default filter will not be applied
	      String regexValue = column.getSearch().getValue();
	      output = findAll(input, (root, query, builder) -> {
	        javax.persistence.criteria.Expression<?> regex = builder.function("REGEXP_LIKE", String.class, root.get("firstName"), builder.literal(regexValue));
	        System.out.println("Expression regex : "+regex);
	        System.out.println("column : "+column);
	        return builder.equal(regex, builder.literal(1));
	      });

	    } catch (Exception e) {
	      output.setError(e.toString());
	    }

	    return (DataTablesOutput<R>) output;
	}

Again, it's a sandbox code (messy and done on a table corner). I will later, clean it, make it listen to the value of the checkboxes (regex:true or false, smart search:true or false) for every column. The front end is mainely inspired by this example.
Once again, I use embedded H2 database.

Thanks a lot Damien for saving the day !

Cheers,
Salutations toulousaines d'un ex lillois.

from spring-data-jpa-datatables.

darrachequesne avatar darrachequesne commented on June 4, 2024

Hi! The problem is that JPQL only supports LIKE expressions, with % and _ tokens. And regex support does vary accross different DBMS (like regexp_matches in Postgresql, RLIKE in MySQL...)

That's why the regex flag is currently not handled. But I'm open to suggestions!

from spring-data-jpa-datatables.

pshingavi avatar pshingavi commented on June 4, 2024

Haven't thought about it yet. I have temporarily fixed using IN clause Specification for values that I send explicitly along with the data table request params.

from spring-data-jpa-datatables.

darrachequesne avatar darrachequesne commented on June 4, 2024

Closing due to inactivity, please reopen if needed.

from spring-data-jpa-datatables.

darrachequesne avatar darrachequesne commented on June 4, 2024

(and I don't know how it could be properly handled)

from spring-data-jpa-datatables.

memento avatar memento commented on June 4, 2024

Hi, Bonjour,

Thanks a lot for your work.
I'm trying to use regex. I'll only be using H2 database. I imported your project in its version 4.3.
I'm wondering what the most elegent way to actually use those 'regex=' flags in the backend.
H2 allows regex in native queries.

Like this dummy example that selects any value for my_column column :

SELECT * FROM my_table WHERE my_column REGEXP '.*'

Overriding methods from your repository interface and modifying its methods implementation seem to be an option. I thought maybe, you had tested some things on your side, using your embedded H2, and maybe you have an idea. Shall I use SQL :

@Query(value = "...", nativeQuery = true)

I tend to have some difficulties to adapt your approach using SpecificationBuilder and I don't want to disfigure your code.

Any idea ?

Excellente journée !

from spring-data-jpa-datatables.

darrachequesne avatar darrachequesne commented on June 4, 2024

@memento since that is so nicely asked, you should be able to do:

Column column = input.getColumn("my_column");
column.setSearchable(false); // so the default filter will not be applied
String regexValue = column.getSearch().getValue();
DataTablesOutput<...> output = repository.findAll(input, (root, query, builder) -> {
  Expression<String> regex = builder.function("REGEXP_LIKE", String.class, root.get("my_column"), builder.literal(regexValue));
  return builder.equal(regex, builder.literal(1));
});

(not thoroughly tested)

from spring-data-jpa-datatables.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.