Is there any program IDE or not that can format MySQL code inside PHP string e.g. I use PHPStorm IDE and it cannot do it.
It does that for PHP and MYSQL but not for MYS
There is no such IDE.
Maybe some SQL is broken into multiple string concatenation, or maybe some SQL is generated using some iterator. Or maybe it contains invalid SQL such as {$_SESSION['admin_id']}
.
Your only chance is writing a small script, tuned to match the coding style and possible invalid SQL stuff, which will repair all application source code files. It will take you hours to get it right, but in the end you won't need an IDE which doesn't exist, and you will have a prettyfiend stonified SQL and better source code.
(The above is a solution, thinking you have hundreds of SQL statement throughout the app; if not, just repair them by hand).
Edit: make sure you log all changes in a comparison table, so you can review all which was formatted by your script.
You can use the tokenizer extension to write a script that would format the SQL.
The following script can be invoked from the command-line, it reads from standard input and writes to standard output. It handles both quoting styles (double or single quotes).
All strings are scanned for possible SQL grammar, based on the starting word, and line breaks are inserted before every keyword; both starting word and keywords are extensible, so go wild :)
<?php
$tokens = token_get_all(file_get_contents('php://stdin'));
function token_value($token)
{
return is_array($token) ? $token[1] : $token;
}
function sql_format($s)
{
if (preg_match("/^(?:select|insert|update|delete)/i", $s)) {
// prefix newline and tab before every keyword
return preg_replace('/\b(from|where|and|order|group by)/i', "\n\t\\0", $s);
}
return $s;
}
$target = '';
$i = 0; $n = count($tokens);
while ($i < $n) {
$token = $tokens[$i];
if ($token === '"') {
$s = ''; ++$i;
while ($i < $n && $tokens[$i] !== '"') {
$s .= token_value($tokens[$i]);
++$i;
}
if ($i < $n) {
++$i;
}
$target .= '"' . sql_format($s) . '"';
} elseif (is_array($token) && $token[0] === T_CONSTANT_ENCAPSED_STRING) {
$quote_style = $token[1][0];
$target .= $quote_style . sql_format(trim($token[1], "'\"")) . $quote_style;
++$i;
} else {
$target .= token_value($token);
++$i;
}
}
echo $target;
I always used SQLyog to format my SQL statements and paste them back to the IDE, because i didn't find an IDE either, which is able to format sql inside php blocks.
As fas as I know PhpStorm can do it, if you use the heredoc syntax
$request1 = <<<SQL
SELECT *
FROM tbl_admin
WHERE admin_id = {$_SESSION['admin_id']}
AND active = 1
ORDER BY admin_id ASC
SQL;
You asked:
Is there any program IDE or not that can format MySQL code inside PHP string
with specifically this PHP string definition:
$request1 = "select * from tbl_admin where admin_id= {$_SESSION['admin_id']} and active= 1 order By admin_id Asc";
And the answer is no. There doesn't exist a well use-able settop on a PHP parser in IDEs or on the commandline that works PHP's token_get_all
.
With token_get_all
you should be able to first of all extract that part that makes sense in your case to be edited. Here with context:
<309:T_VARIABLE> "$request1" <371:T_WHITESPACE> " " "=" <371:T_WHITESPACE> " " """ <314:T_ENCAPSED_AND_WHITESPACE> "select * from tbl_admin where admin_id= " <375:T_CURLY_OPEN> "{" <309:T_VARIABLE> "$_SESSION" "[" <315:T_CONSTANT_ENCAPSED_STRING> "'admin_id'" "]" "}" <314:T_ENCAPSED_AND_WHITESPACE> " and active= 1 order By admin_id Asc" """ ";"
As these tokens show, there is a lot of additional work going to extract that what you call string from it:
$request1 = "select * from tbl_admin where admin_id= {$_SESSION['admin_id']} and active= 1 order By admin_id Asc"
This need to be managed and identified in tokens:
<314:T_ENCAPSED_AND_WHITESPACE> "select * from tbl_admin where admin_id= " <375:T_CURLY_OPEN> "{" <309:T_VARIABLE> "$_SESSION" "[" <315:T_CONSTANT_ENCAPSED_STRING> "'admin_id'" "]" "}" <314:T_ENCAPSED_AND_WHITESPACE> " and active= 1 order By admin_id Asc"
As this example shows, this is not true SQL. So you not only need to find a SQL parser (which exists in PHP, see Parsing sql query PHP), but you also need to make that SQL parser aware of PHP variable substitution like {$_SESSION['admin_id']}
in your case. For you it is a variable, for an SQL parser this is just a freaky syntax soup if not an error.
Your small code example is a good one: It already shows that there is no information about the nature of the string that will be substituted. As this information is hidden, a SQL parser will never be able to deal with this string as something well-formed.
So to make my answer "No" more profound: Because no general tools on how to deal with multiple outcome on the higher languages level (the PHP string has multiple probably correct representations in SQL, only the author of the variable content will can tell which one is correct) that already exists, there is no general solution available.
You might want to just fire up some regular expressions on your codebase and blitzshit together an outcome you can feel happy with, and indeed the tools ehime lined-up are worth to consider.
But probably it's worth to invest the time and first extract the strings from the PHP token stream, reformat the string and then proceed outputting the result. That's something perl and regexes can't give you because they don't have the PHP tokenizer.
I built a tool to do this for MS SQL Server and C# code. I'd be happy to alter the tool for this purpose for a small fee. http://www.softfrontiers.com/Downloads/ReIndenter.shtml
An hour or two of my time will save you many hours of frustration.