Quantcast
Viewing all articles
Browse latest Browse all 4

Answer by Brandon LS for Using static final Strings for SQL Query in Spring Boot

I agree with @mtj's answer : YAGNI, no point in making the string reusable if it's not being reused at the moment.

I would want to add that in certain scenarios it may be smart to factor out only part of the SQL query.

For example if you had a few queries with slightly more complicated WHERE statements, something like :

select * from image where (created_date is null or created_date > date)

and

select id from text where (created_date is null or created_date > date)

Then we can have

private static String WHERE_CREATED_IS_NULL_OR_AFTER_DATE = " where (created_date is null or created_date > date) "

and use it in the respective methods :

public void getImages() {    String query = "select * from image"+ WHERE_CREATED_IS_NULL_OR_AFTER_DATE;    ...}
public void getTextIds() {    String query = "select id from text"+ WHERE_CREATED_IS_NULL_OR_AFTER_DATE;    ...}

Viewing all articles
Browse latest Browse all 4

Trending Articles