Open sazzer opened 5 years ago
It's impossible to prepare sql without knowing the dialect and database settings like keywords, quotation parameters and much more. So you have to call Database.connect()
.
You can use H2 in-memory database for such cases.
This could be handled by telling the query builder the dialect to use, and not depend on access to the database just to build the SQL.
In my case I'm using Spring JdbcTemplate and so don't ever actually access the DataSource. However, this would also be useful, for example, if you are generating SQL constants that you want to reuse often. (And I'm sure many other reasons)
You may try to make a fake connection with unworking jdbc-url (with a proper prefix to detect dialect, like jdbc:mysql:unexisting:3306
). It should work unless jdbc driver will require some instance-specific data.
It also requires active transaction, which it should not require, because nothing actually is being executed at the moment of sql generation.
I would appreciate way to set dialect without specifying connection. Exposed could be used as great standalone SQL-generator in environments where queries are run afterwards with some other system.
I'm trying to use Exposed as a SQL DSL, but not as a means to execute the SQL.
The FAQ implies that this is possible: https://github.com/JetBrains/Exposed/wiki/FAQ#q-how-to-get-a-plain-sql-query-which-will-be-executed
However, actually doing it throws:
My code for this is simply: