Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ IDEA - Syntax Highlighting of SQL Inside Java Code

I am using IntelliJ 13 as an IDE for a project where communication with DB is done via Spring's JDBC Template. When I have code fragments in Java like the following one:

getJdbcTemplate().queryForObject("SELECT CONVERT(VARCHAR(255), NEWID())", String.class);

where getJdbcTemplate() returns initialized JdbcTemplate object, the IDE has proper syntax highlighting for the SQL statement (you can see it on the snippet bellow):


.code {font-family: Monospace}
.db-stmt {background: #EDFCED}
.db-keyword {color: #000080; font-weight: bold;}
.db-column {color: #7D0C8D; font-weight: bold;}
.db-number {color: #0000FF;}
.java-class {color: #39008E; font-weight: bold;}
<span class="code">getJdbcTemplate().queryForObject("<span class="db-stmt"><span class="db-keyword">SELECT CONVERT</span>(<span class="db-keyword">VARCHAR</span>(<span class="db-number">255</span>), NEWID())</span>", String.<span class="java-class">class</span>);</span>

However, when I use another method, created by me:

protected String queryForString(String sql, Object... args) {
    try {
        return getJdbcTemplate().queryForObject(sql, String.class, args);
    }
    catch (RuntimeException e) {
        return null;
    }
}

for the same query:

queryForString("SELECT CONVERT(VARCHAR(255), NEWID())");

there is no syntax highlighting of the SQL statement. Do you know how to enable syntax highlighting of SQL staments for Java stings that are not arguments of methonds of JdbcTemplate or java.sql interfaces?

like image 696
Vasilen Donchev Avatar asked Jun 10 '15 10:06

Vasilen Donchev


People also ask

Can you do SQL in IntelliJ?

Database tools and SQLUltimateThe database management functionality in IntelliJ IDEA is supported by the Database tools and SQL plugin. The Database tools and SQL plugin provides support of all the features that are available in DataGrip, the standalone database management environment for developers.

How can I see mysql dialect in IntelliJ?

Right-click the editing area of the input pane, select Change Dialect, and select Generic SQL.


1 Answers

You can do this via any combination of:

  1. a setting
  2. an annotation
  3. a comment.

For any of these, you must have the (bundled) IntelliLang Plugin enabled.

Via Setting

You can add a setting to say that a particular method's parameter is of a certain 'language' (SQL for example.)

  1. Open Setting (Ctrl+Alt+S / ,) > Editor > Language Injections
  2. On the right, click the add button and select Java Parameter
  3. In the Language Injection Settings dialog, select SQL (or the desired language) in the ID field
    • You can also specify a specific SQL dialect such as MySQL, Oracle, etc.
  4. In the "Class Methods" field, enter the method you want to identify as taking an SQL parameter. You can open a class browser/search window via the ellipsis button enter image description here
    • You can select classes from your code, or from libraries
  5. If your method takes multiple parameters, you can select the parameter in the dialog:

enter image description here

Now IntelliJ IDEA will automatically apply language injection when you use that method:

enter image description here

Via Annotation

You can annotate a method parameter (or variable or field) as being of a particular language type. By default, IntelliJ IDEA will use the org.intellij.lang.annotations.Language annotation that is in the annotations.jar included with IntelliJ IDEA. It is located at <installDirectory>/redist/annotations.jar or in maven central, org.jetbrains:annotations

Note: There are also some unofficial uploads of the annotations jar with different group IDs including (but not limited to) "com.intellij". This is because years ago, JetBrains was not uploading them to maven central. So some community members did, but under various group IDs. For the past several years, JetBrains has been uploading them with the (official) group ID of "org.jetbrains". This is what other artifacts use in their POMs (such as the Kotlin POMs), and so should be used.

Add the annotations JAR to your class path. (In most cases, the JAR only needs to be on the classpath for development, and is not needed at runtime. Thus you can use a maven <scope> of "provided".) Then annotate your method's Parameter as being SQL. Note that code completion will work when you type the language type:

public void checkDatabase(String foo, @Language("SQL")String sql, String bar)

I like the annotation since even for non-IntelliJ IDEA users, it lets them know that a String should be valid SQL, XML, CSS, or whatever. You can also annotate a variable, or field:

enter image description here

If desired, you can change the annotation to use in Setting (Ctrl+Alt+S / ,) > Editor > Language Injections > Advanced

Via Comment

As an alternative to annotations, you can use a comment. (I can't remember when this was added. So it may not be in v13). To the best of my knowledge though, language injection via comments only works for variables and fields. It does not work for parameters. For example:

enter image description here

For more information, see the IntelliJ IDEA user guide

like image 103
Javaru Avatar answered Oct 06 '22 01:10

Javaru