Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ IDEA: inject language with String.format()

I have an SQL query which unfortunately cannot be written as a prepared statement because it has variable column names. Because of this, I have to build my SQL using String formatting.

The language (SQL dialect) I'm using is PostgreSQL, but because I have String.format() in my queries, IntelliJ complains about syntax errors in my code.

public class Example {
    public String getUniqueFeatureCountSqlQuery(String feature, Long sourceUserId) {
        //language=PostgreSQL
        return String.format("SELECT COUNT(DISTINCT targetUserId), %s FROM userComments GROUP BY %s WHERE userId = %d",
            feature, feature, sourceUserId);
    }
}

enter image description here

How do I make IntelliJ recognize this string properly? It should know that it is PostgreSQL which is first formatted by String.format().

like image 747
michaelsnowden Avatar asked Oct 16 '25 18:10

michaelsnowden


1 Answers

Thanks to @WillHumphreys I found a solution via youtrack.

You have to go the the User Parameters settings under Tools -> Database -> User Parameters, or use the find action command.

enter image description here

Then make these changes.

enter image description here

You may also have to use the argument index string format notation (String.format("SELECT %1s ...", ...)) for the parameters to be distinguishable when attempting to alt-enter run the query in the console.

like image 56
michaelsnowden Avatar answered Oct 19 '25 00:10

michaelsnowden



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!