The Wayback Machine - https://web.archive.org/web/20201011181946/https://github.com/JSQLParser/JSqlParser/issues/286
Skip to content
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

Pretty print for Statement #286

Open
jesperpedersen opened this issue Jun 17, 2016 · 7 comments
Open

Pretty print for Statement #286

jesperpedersen opened this issue Jun 17, 2016 · 7 comments

Comments

@jesperpedersen
Copy link

@jesperpedersen jesperpedersen commented Jun 17, 2016

it would be great if Statement had a

String prettyPrint(int width)

method, which formats the resulting String into a maximum width.

It would make it much nicer to display to end-users.

Or something similar, like

SELECT ...
FROM ...
WHERE ...

with predefined line breaks.

@wumpz
Copy link
Member

@wumpz wumpz commented Jun 18, 2016

It could easily implemented if you overwrite the methods from the deparsers.

@mlkammer
Copy link

@mlkammer mlkammer commented Jun 23, 2016

@jesperpedersen, on a side note, did you know about the Poor Man's SQL Formatter?

@wumpz
Copy link
Member

@wumpz wumpz commented Jul 24, 2016

Pull requests are wellcome. As I mentioned the deparsers modifications should be easy.

@tomershay
Copy link
Contributor

@tomershay tomershay commented Oct 3, 2017

An alternative to implementing this in JSqlParser will be to use Hibernate's implementation for SQL formatting. If you already have a dependency on the relevant libraries, just call:
String formattedSQL = new BasicFormatterImpl().format(sql);

@stewartbryson
Copy link

@stewartbryson stewartbryson commented Jul 27, 2020

Just implemented this: https://github.com/vertical-blank/sql-formatter

Extremely simple and effective.

@stewartbryson
Copy link

@stewartbryson stewartbryson commented Jul 27, 2020

An alternative to implementing this in JSqlParser will be to use Hibernate's implementation for SQL formatting. If you already have a dependency on the relevant libraries, just call:
String formattedSQL = new BasicFormatterImpl().format(sql);

I couldn't figure out the dependency. I tried hibernate-core but couldn't get the import correct.

@tomershay
Copy link
Contributor

@tomershay tomershay commented Jul 27, 2020

@stewartbryson, I didn't try to import it directly from hibernate actually, as I needed to apply a few small changes to the hibernate formatter, so we just copied the source code of the file to our codebase, from:
https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/engine/jdbc/internal/BasicFormatterImpl.java

Once I had that class, I can just import the class BasicFormatterImpl and call:
String formattedSQL = new BasicFormatterImpl().format(sql);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.