SQL: how to get autocomplete for different databases?

In one project I've a MySQL and a Postgres connection. I use them intermixed in the same source files (it has to do with synchronization/migration). I only can configure highlighting for one dialect and connection source in PhpStorm.

Any way to get both working?

3 comments
Comment actions Permalink

Hi there,

I'm not 100% sure on what you mean here. Could you please provide some sample PHP code which will illustrate the issue?

As I understand so far you have 2 different SQL code fragments in one file (e.g. $sql1 = '...'; $sql2 = '...';) and only one of them is working correctly?

0
Comment actions Permalink

> As I understand so far you have 2 different SQL code fragments in one  file (e.g. $sql1 = '...'; $sql2 = '...';) and only one of them is  working correctly?

Exactly!

0
Comment actions Permalink

Well .. then only thing I can think of to use HEREDOCS (which means you need to modify your source a bit) and custom language injection rules .. otherwise only 1 SQL dialect per project as there is no other reliable way of telling IDE that this fragment uses different dialect that would definitely survive IDE restart.

I mean -- you can always try Alt+Enter while having caret inside such SQL code and try changing injected language to another SQL Dialect .. but I'm pretty sure it will not survive IDE/project restart.

Anyway, this is what I have in mind (should work -- have not actually tested myself as I do not have any code/DBs other than SQL Server ATM):
1) Settings | Editor | Language Injections
2) Find and duplicate "php: <<<SQL" rule -- it  will now have "IDE" or "Project" in last column
3) Edit newly create rule -- rename it to "<<<MYSQL" and change "ID" to "MySQL" from just "SQL"; change "SQL" to "MYSQL" in "Places pattern" filed as well.
Now edit your .php file where you have your queries -- rewrite places where you use MySQL specific code to use that HEREDOC, e.g.

$sql = <<<MYSQL
.... mysql specific code here
MYSQL;

This way PostgreSQL will be the project-wide dialect (in all SQL strings by default) and MySQL will be in those few where MYSQL HEREDOC used.

Alternatively create rule for PostgreSQL instead of MySQL ("<<<PGSQL", "PostgreSQL" and "PGSQL" accordingly) -- this way MySQL can be project-wide dialect and PostgreSQL in those specific fragments.

0

Please sign in to leave a comment.