PhpStorm : add SQL dialect

2020-02-15 06:46发布

We use a class which extends Mysqli at work, and it adds another syntax for prepared statements, something like :

<?php
$req = MySQLiWrapperClass::getInstance()->prepare('
    INSERT INTO
        table
    SET
        field = :field_value:s
    ')
    ->bindNamedValue('field_value', 'stackOverflow')
    ->execute();

The class will then transform :field_value:s to ? before executing the query.

Problem is, when using MySQL SQL dialect in PhpStorm, this syntax will trigger an error because this syntax is not recognized.

Is there a way to declare a new SQL Dialect, in which I could add this specific syntax ?

标签: phpstorm
1条回答
家丑人穷心不美
2楼-- · 2020-02-15 06:54

Is there a way to declare a new SQL Dialect, in which I could add this specific syntax?

You cannot add your own SQL Dialect just like that (that's not configurable via some GUI).

What I mean is: Yes, you can, but you have to code it as a plugin in Java, do the all stuff needed -- it will work.


The issue that you are having here has nothing to do with SQL Dialect -- you are not providing new keywords or actual query syntax (or stuff like that).

The issue in your code is in custom syntax for parameters (:field_value:s vs :field_value).

This can be resolved by specifying your own syntax at Settings/Preferences | Tools | Database | User Parameters:

  • Pattern: :(\w+\:[d|i|s])
  • Scope: PHP & MySQL at very least (based on your code sample)
  • Where: in literals at very least

Important: order of the rules matters as well.

Here is what I did and how it works:

enter image description here

查看更多
登录 后发表回答