PhpStorm - Disable SQL inspection for one line
Asked Answered
K

1

6

I am using the ZendDb database adapter which doesn't bring all the tweaks SQL can do. For example if I want to do a REPLACE INTO I would have to code it like this:

$SQL = sprintf('REPLACE INTO %s (id, NAME, cache_id, compile_id, content) VALUES  (%s, %s, %s, %s, %s)' %
        array(self::TABLE_NAME, $id, $name, $cache_id, $compile_id));
$this->_zdb->query($SQL);

The problem is that PhpStorm tells me that the %s is an error inside the SQL.

When I try hotfixing it with Alt + Enter I don't get the option to suppress the inspection for this line.

I' need something like this:

/** @noinspection SqlInspection */

I googled and found this page but nothing of the options seemed to help.

Any ideas?

Kenlay answered 8/1, 2018 at 9:14 Comment(4)
Well -- your code seems to be fine here, excluding the unknown column names of course, as I do not have the Database definition (I've selected MySQL dialect in PhpStorm). This must be because I have a rule for %s placeholders/parameters -- quite possibly I have added it manually long time ago and it's not provided by default in your version (what PhpStorm version do you use anyway)? So ... check your patterns at Settings/Preferences | Tools | Database | User Parameters -- see if you have \%\w+ pattern there; if you do -- what's the definition?Doeskin
It may solve it for you so no suppression may be required. I cannot post this as an answer straight away because you may have some another code sample where this will not be enough and actual suppression will be required.Doeskin
He complains <reference> expected, got %. But I think just the correct ignore param would be ok as well :)Kenlay
That's what User Parameters are for -- for dynamic assembling the final SQL, be it ordinary placeholders (prepared statements) or alike .. or assembling actual SQL with sprintf. So .. does adding such aforementioned \%\w+ pattern helps or not?Doeskin
D
10

Add \%\w+ pattern to Settings/Preferences | Tools | Database | User Parameters -- it will tell IDE to treat such %s as dynamic/external part of the code instead of actual SQL.

For example:

enter image description here

P.S. One day such pattern will be provided by default in PhpStorm.

https://youtrack.jetbrains.com/issue/WI-39271 -- watch this ticket (star/vote/comment) to get notified on any progress.


If you need an actual suppression .. then the most effective way is to treat the string as Plain Text instead of autodetected SQL.

For this, just place /** @lang text*/ just before the string, e.g.

$sql = /** @lang text*/'REPLACE INTO %s (id, NAME, cache_id, compile_id, content) VALUES (%s, %s, %s, %s, %s)';

enter image description here

NOTE: may not work if you are concatenating such string or perform other manipulations in place.

Doeskin answered 8/1, 2018 at 12:34 Comment(2)
Do you know how I could do it for this statement? (the error is on the ?) INSERT INTO badge_users (badge_id, event_id, user_id, friend_user_id, created_at) VALUES ?;,Corona
@KyleVenn Looks OK to me in my TEST project. No complains on ? bit, only on unknown table/columns (MySQL dialect). What do you have at Settings/Preferences | Tools | Database | Query Execution | User Parameters -- I believe I have the default values there. Try it in a brand new empty project with a single simple file only -- any difference?Doeskin

© 2022 - 2024 — McMap. All rights reserved.