'How to use one query for different condition in spring boot webflux
I am using mysql syntax for extracting data from datatbase entity in spring boot webflux.I have eight conditions(possiblities) depending on HttpRequest.My HttpRequest contains query parameter search,status and date.The different conditions depending on HttpRequests are 1.!date !status !search (HttpRequest doesn't contain any value for these queryParameters) 2.date status search (HttpRequest contains value for all queryParameters) 3.date !status !search (HttpRequest contain only date value not for status and search )
4.date status !search HttpRequest contain date and status value not for search ) 5.date search !status 6.status search !date 7.status !search !date 8.search !status !date
I created 8 query for each condition.Can I implement all of these condition with just a single query?My sql syntax is like that with different where condition
final String search=request.getQueryParams().get("search").get(0);
final String date=request.getQueryParams().get("date").get(0);
final String status=request.getQueryParams().get("status").get(0);
1. db.sql(mysql+" WHERE e.created_by = '"+createdBy+"'").map(this::process);//!date !search !status
2. db.sql( mysql+" WHERE e.created_by = '"+createdBy+"' and DATE_FORMAT(e.created_date, '%m/%d/%Y')= '"+date+"' and (e.id Like '%"+search+"%' ) and e.status in ('"+status.replace(",","','")+"')").map(this::process);//date search status
3.db.sql( mysql+" WHERE e.created_by = '"+createdBy+"' and DATE_FORMAT(e.created_date, '%m/%d/%Y')= '"+date+"'").map(this::process);//date !status !search
4.db.sql( mysql+" WHERE e.created_by = '"+createdBy+"' and DATE_FORMAT(e.created_date, '%m/%d/%Y')= '"+date+"' and e.status in ('"+status.replace(",","','")+"')").map(this::process);//date status !search
5.
6.
7.
8.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
