Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save willemnviljoen/d20ad8ad0cc365a7e80744328246610f to your computer and use it in GitHub Desktop.
Save willemnviljoen/d20ad8ad0cc365a7e80744328246610f to your computer and use it in GitHub Desktop.
SQL language injection for PHPStorm which specifically ignores the Doctrine DQL language
<LanguageInjectionConfiguration>
<injection language="SQL" injector-id="php">
<display-name>&quot;SQL select/delete/insert/update/create&quot; Excluding Doctrine DQL</display-name>
<place><![CDATA[and(not(phpLiteralMatchesBrics(".*:.*")), phpLiteralMatchesBrics(" *(((SELECT|DELETE) .*FROM)|((INSERT|REPLACE) .*INTO)|(UPDATE .* SET)|((CREATE|DROP|ALTER) +((TEMPORARY )?TABLE|(UNIQUE )?INDEX))) .*"))]]></place>
</injection>
</LanguageInjectionConfiguration>
@willemnviljoen
Copy link
Author

willemnviljoen commented Aug 17, 2017

To use, go to Preferences (or Settings) -> Editor -> Language Injections.

  1. Uncheck the standard "SQL select/delete/insert/update/create" injection.
  2. Use the "Import" button at the bottom to import this injection from the XML file
  3. Check the new "SQL select/delete/insert/update/create" Excluding Doctrine SQL injection.

This is quick and crude. It works by not matching any query expressions with a : character. This is what is normally marked as an error by PHPStorm (or more accurately IntelliLang) when it encounters DQL expressions, so it should prevent them from being marked as errors in most cases.

This can be expanded to exclude other DQL specific phrases, please link any improvements here.

@tarjei
Copy link

tarjei commented Aug 17, 2017

Cool!

Does this ensure that if I format a page with DQL it will not CAPITALIZE within the dql statement?
i.e. if I got:
$em->query("SELECT U.user from Model::User U")it will not be converted to
$em->query("SELECT U.USER from Model::USER U")

@willemnviljoen
Copy link
Author

Hi,

It should prevent that yes, since it stops the IDE from even considering that string to be SQL. I've done a quick experiment on some of my code, asking PHPStorm to format it, and it left it alone.

Hopefully it works for everyone.

@stof
Copy link

stof commented Sep 26, 2017

this would not work if you write the class name rather than a App:User shortcut though.

@neuro159
Copy link

neuro159 commented Oct 9, 2017

May be we can just pull this to core PhpStorm for now?.. //PS team

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment