-
-
Notifications
You must be signed in to change notification settings - Fork 443
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Poor SQL formatting #750
Comments
The current reformat logic was a quick shot, some years ago, but at least gave me line breaks where I wanted them. This, and also the fact that it's my own brewn code, are the advantages here. But I'm aware this provides quite poor results. So, what ideas do we have?
|
@ansgarbecker thanks for your attention. For example you can add option which holds path to command for reformatting and pass query to it. sql-formatter.php then would be like: require_once('SqlFormatter.php');
echo SqlFormatter::format($argv[1]); And get its output to HeidiSQL. |
Yea, interesting idea. I just found a free SQL formatter written in Delphi: https://github.com/SqlToys/SqlFormatter |
Or I'll give it a try and enhance my existing formatter first. The other options have their own disadvantages. I should at least check how much effort it will be to implement a similar formatting as your example code from SQLyog. |
Thanks! Will wait for possible solutions. |
jdorn's parser creates following output, quite impressive: SELECT
el.ID,
el.NAME,
el.IBLOCK_SECTION_ID,
price.PRICE
FROM
b_iblock_element el
JOIN b_iblock_element_property SHOW_IN_CAT ON (
SHOW_IN_CAT.IBLOCK_ELEMENT_ID = el.ID
AND SHOW_IN_CAT.IBLOCK_PROPERTY_ID = 716
AND SHOW_IN_CAT.VALUE = "329"
)
JOIN (
SELECT
prop.IBLOCK_ELEMENT_ID
FROM
b_iblock_element_property prop
WHERE
prop.IBLOCK_PROPERTY_ID IN (676, 678, 674, 664, 665, 675, 456)
AND prop.VALUE = "экокожа"
) AS prop ON prop.IBLOCK_ELEMENT_ID = el.ID
JOIN b_iblock_element_property DESTINATION ON (
DESTINATION.IBLOCK_PROPERTY_ID = 1315
AND DESTINATION.VALUE = "для молодежной"
AND DESTINATION.IBLOCK_ELEMENT_ID = el.ID
)
JOIN b_catalog_price price ON (el.ID = price.PRODUCT_ID)
WHERE
el.IBLOCK_ID = 38
AND el.IBLOCK_SECTION_ID IN (18449, 18466) Comparing it to your SQLyog example, differences are minor (left: jdorn, right: SQLyog): |
Yes, pretty similar. As I see that PHP library has not any syntax settings so this output is only one way to go. But that would be enough. |
Yes, and that library seems to be unmaintained since 2015. Not the best option probably. |
porting https://github.com/jdorn/sql-formatter/blob/master/lib/SqlFormatter.php to delphi is probably not a huge task, though. i've "contributed" to that lib in my past php life. jdorn/sql-formatter#34 |
Of course the best way is if you can write your own formatter and not to rely on third party libraries (especially written on other languages and with lack of support). But we all understand you provide HeidiSQL for free and it may require much efforts so maybe option with call external command is applicable for the first time. |
|
… API, using the engine from https://github.com/doctrine/sql-formatter/
Next HeidiSQL build now finally has an additional online formatter, using doctrine/sql-formatter : This is how the example in the very first post here gets reformatted: SELECT
el.ID,
el.NAME,
el.IBLOCK_SECTION_ID,
price.PRICE
FROM
b_iblock_element el
JOIN b_iblock_element_property SHOW_IN_CAT ON (
SHOW_IN_CAT.IBLOCK_ELEMENT_ID = el.ID
AND SHOW_IN_CAT.IBLOCK_PROPERTY_ID = 716
AND SHOW_IN_CAT.VALUE = "329"
)
JOIN (
SELECT
prop.IBLOCK_ELEMENT_ID
FROM
b_iblock_element_property prop
WHERE
prop.IBLOCK_PROPERTY_ID IN (676, 678, 674, 664, 665, 675, 456)
AND prop.VALUE = "экокожа"
) AS prop ON prop.IBLOCK_ELEMENT_ID = el.ID
JOIN b_iblock_element_property DESTINATION ON (
DESTINATION.IBLOCK_PROPERTY_ID = 1315
AND DESTINATION.VALUE = "для молодежной"
AND DESTINATION.IBLOCK_ELEMENT_ID = el.ID
)
JOIN b_catalog_price price ON (el.ID = price.PRODUCT_ID)
WHERE
el.IBLOCK_ID = 38
AND el.IBLOCK_SECTION_ID IN (18449, 18466) Note that it adds indentation like in your preferences > SQL > Tab width / Tabs to spaces. I'm marking this as completed for now, as there is nothing I can configure in the formatter, but I suppose it's much better than the old internal one from myself. |
@ansgarbecker Thanks! |
Thanks to Andi, we have a third formatter option for www.sqlformat.org now: |
Hello. Firstly thanks for the great free application. It's very fast and handy.
But there is one problem I faced from time to time when using HeidiSQL.
Steps to reproduce this issue
Let's pretend we have following unformatted SQL query.
Current behavior
HeidiSQL 10.2.0 Reformat SQL feature produces next SQL:
Better but still not very readable. It just adds new lines but it's hard to analyze this query.
Expected behavior
For example SQLyog 12.2.6 produces this SQL:
It formats JOINs and moves every condition on new line plus adds tabs.
I found some old threads on your website forum (like here) but seems nothing has changed from that time.
Is there any chance to enhance this feature?
Thanks.
The text was updated successfully, but these errors were encountered: