How to select the default source in injected SQL code?
Is there a way to define the default data source and schema for SQL injection?
I have several database sources configured in the Database window (they are all MySQL sources, I can see all tables from the Database window, and I can execute scripts against them)
If I create a JDBC string with a select statement, and then inject MySQL language into it(Alt + Enter > Inject Language or Reference > MySQL), the string automatically auto highlights recognising the select/from/where keywords.
However, it does not recognize the table or column names, so auto-complete does not work.
String sql = "select * from account where name=?"
However, if I enter the schema before the table name, then it works:
String sql = "select * from dev_db.account where name=?"
I would expect to be able to setup somewhere at project level the default source and schema for SQL injection, but I cannot find where.
Thanks
Please sign in to leave a comment.
See https://www.jetbrains.com/help/idea/2016.1/data-sources-and-drivers-dialog.html#schemasAndTablesTab .
Note that the DBMS may reject short names when you execute the corresponding statement or statements.
Thanks Serge, that fixed it! :)
I just had to tick the "Resolve unqualified references" in the Schema tab of the data source configuration, and all my injected SQL started resolving the tables/columns.
As a suggestion, it would be nice if IntelliJ automatically asked you to select the default schema for unqualified references in the injected SQL if not setup or when it cannot find a table, i.e. by pressing Alt + Enter.
Yes, it's something that we plan to add soon.
Has this unqualified references option disappeared? I can't see it in 2017.1 (IDEA or PhpStorm)...
It is called SQL resolution scopes now, please check blog post about this update: https://blog.jetbrains.com/datagrip/2017/03/22/datagrip-2017-1-released/#tree It is located under Preferences | Languages & Frameworks | SQL Resolution Scopes.
Andrey Dernov not working for me
Tobi Akiny what exactly is not working? which DBMS you are using?
@... What I had to do was connect to the target database, instead of the default postgres database, for the SQL injections to correctly link