Skip to content

Commit 8d07fee

Browse files
committed
jdbc: decouple querydsl module fix jooby-project#872
1 parent 479e4d1 commit 8d07fee

File tree

3 files changed

+109
-186
lines changed

3 files changed

+109
-186
lines changed

doc/doc/querydsl/querydsl.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
SQL abstraction provided by <a href="http://www.querydsl.com">QueryDSL</a> using plain JDBC underneath.
44

5-
This module depends on [jdbc module](doc/jdbc), make sure you read the doc of the [jdbc module](doc/jdbc) module before using this module.
5+
This module depends on [jdbc](doc/jdbc) module.
66

77
## exports
88

@@ -23,6 +23,7 @@ This module depends on [jdbc module](doc/jdbc), make sure you read the doc of th
2323
```java
2424
import org.jooby.querydsl.QueryDSL;
2525
{
26+
use(new Jdbc());
2627
use(new QueryDSL());
2728

2829
get("/my-api", req -> {
@@ -41,6 +42,7 @@ Dialect is detected automatically and usually you don't need to do anything. But
4142

4243
```java
4344
{
45+
use(new Jdbc());
4446
use(new QueryDSL().with(new MyCustomTemplates());
4547
}
4648
```
@@ -50,8 +52,10 @@ Dialect is detected automatically and usually you don't need to do anything. But
5052
```java
5153
import org.jooby.querydsl.QueryDSL;
5254
{
55+
use(new Jdbc("db.main"));
5356
use(new QueryDSL("db.main"));
5457

58+
use(new Jdbc("db.aux"));
5559
use(new QueryDSL("db.aux"));
5660

5761
get("/my-api", req -> {
@@ -69,6 +73,7 @@ Advanced configuration can be added by invoking the ```doWith``` method, adding
6973

7074
```java
7175
{
76+
use(new Jdbc());
7277
use(new QueryDSL().doWith(conf -> {
7378
conf.set(...);
7479
});

modules/jooby-querydsl/src/main/java/org/jooby/querydsl/QueryDSL.java

Lines changed: 62 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -204,24 +204,24 @@
204204
package org.jooby.querydsl;
205205

206206
import com.google.inject.Binder;
207-
import com.querydsl.sql.Configuration;
208-
import com.querydsl.sql.DB2Templates;
209-
import com.querydsl.sql.FirebirdTemplates;
210-
import com.querydsl.sql.H2Templates;
211-
import com.querydsl.sql.HSQLDBTemplates;
212-
import com.querydsl.sql.MySQLTemplates;
213-
import com.querydsl.sql.OracleTemplates;
214-
import com.querydsl.sql.PostgreSQLTemplates;
215-
import com.querydsl.sql.SQLQueryFactory;
216-
import com.querydsl.sql.SQLTemplates;
217-
import com.querydsl.sql.SQLiteTemplates;
207+
import com.google.inject.Key;
208+
import com.google.inject.name.Names;
209+
import com.querydsl.sql.*;
218210
import com.typesafe.config.Config;
211+
219212
import static java.util.Objects.requireNonNull;
213+
import static org.jooby.funzy.When.when;
214+
220215
import org.jooby.Env;
221216
import org.jooby.Env.ServiceKey;
217+
import org.jooby.Jooby;
218+
import org.jooby.funzy.When;
222219
import org.jooby.jdbc.Jdbc;
223220

221+
import javax.sql.DataSource;
222+
import java.util.NoSuchElementException;
224223
import java.util.function.BiConsumer;
224+
import java.util.function.Consumer;
225225
import java.util.function.Function;
226226

227227
/**
@@ -243,6 +243,7 @@
243243
* import org.jooby.querydsl.QueryDSL;
244244
*
245245
* {
246+
* use(new Jdbc());
246247
* use(new QueryDSL());
247248
*
248249
* get("/my-api", req {@literal ->} {
@@ -261,6 +262,7 @@
261262
*
262263
* <pre>
263264
* {
265+
* use(new Jdbc());
264266
* use(new QueryDSL().with(new MyCustomTemplates());
265267
* }
266268
* </pre>
@@ -271,7 +273,10 @@
271273
* import org.jooby.querydsl.QueryDSL;
272274
*
273275
* {
276+
* use(new Jdbc("db.main"));
274277
* use(new QueryDSL("db.main"));
278+
*
279+
* use(new Jdbc("db.aux"));
275280
* use(new QueryDSL("db.aux"));
276281
*
277282
* get("/my-api", req {@literal ->} {
@@ -293,6 +298,7 @@
293298
*
294299
* <pre>
295300
* {
301+
* use(new Jdbc());
296302
* use(new QueryDSL().doWith(conf {@literal ->} {
297303
* conf.set(...);
298304
* });
@@ -310,41 +316,61 @@
310316
* @since 1.0.0.CR
311317
* @author sjackel
312318
*/
313-
public class QueryDSL extends Jdbc {
319+
public class QueryDSL implements Jooby.Module {
314320

315321
private Function<String, SQLTemplates> templates = QueryDSL::toSQLTemplates;
316322

323+
private String name;
324+
325+
private BiConsumer<Configuration, Config> callback;
326+
317327
/**
318328
* Creates a new {@link QueryDSL} module
319329
*
320330
* @param name Database name
321331
*/
322332
public QueryDSL(final String name) {
323-
super(name);
333+
this.name = name;
324334
}
325335

326336
/**
327337
* Creates a new {@link QueryDSL} module
328338
*/
329339
public QueryDSL() {
340+
this("db");
330341
}
331342

332343
@Override
333344
public void configure(final Env env, final Config conf, final Binder binder) {
334-
super.configure(env, conf, binder, (name, ds) -> {
335-
SQLTemplates templates = this.templates.apply(dbtype.orElse("unknown"));
345+
Key<DataSource> dskey = Key.get(DataSource.class, Names.named(name));
346+
DataSource ds = env.get(dskey)
347+
.orElseThrow(() -> new NoSuchElementException("DataSource missing: " + dskey));
348+
349+
String dbtype = env.get(Key.get(String.class, Names.named(name + ".dbtype")))
350+
.orElse("unknown");
351+
SQLTemplates templates = this.templates.apply(dbtype);
336352

337-
Configuration querydslconf = new Configuration(templates);
353+
Configuration querydslconf = new Configuration(templates);
338354

339-
callback(querydslconf, conf);
355+
if (callback != null) {
356+
callback.accept(querydslconf, conf);
357+
}
340358

341-
SQLQueryFactory sqfp = new SQLQueryFactory(querydslconf, ds);
359+
SQLQueryFactory sqfp = new SQLQueryFactory(querydslconf, ds);
342360

343-
ServiceKey serviceKey = env.serviceKey();
344-
serviceKey.generate(SQLTemplates.class, name, k -> binder.bind(k).toInstance(templates));
345-
serviceKey.generate(Configuration.class, name, k -> binder.bind(k).toInstance(querydslconf));
346-
serviceKey.generate(SQLQueryFactory.class, name, k -> binder.bind(k).toInstance(sqfp));
347-
});
361+
ServiceKey serviceKey = env.serviceKey();
362+
serviceKey.generate(SQLTemplates.class, name, k -> binder.bind(k).toInstance(templates));
363+
serviceKey.generate(Configuration.class, name, k -> binder.bind(k).toInstance(querydslconf));
364+
serviceKey.generate(SQLQueryFactory.class, name, k -> binder.bind(k).toInstance(sqfp));
365+
}
366+
367+
public QueryDSL doWith(BiConsumer<Configuration, Config> configurer) {
368+
this.callback = configurer;
369+
return this;
370+
}
371+
372+
public QueryDSL doWith(Consumer<Configuration> configurer) {
373+
return doWith((confuration, conf) -> configurer.accept(confuration));
348374
}
349375

350376
public QueryDSL with(final SQLTemplates templates) {
@@ -354,29 +380,18 @@ public QueryDSL with(final SQLTemplates templates) {
354380
}
355381

356382
static SQLTemplates toSQLTemplates(final String type) {
357-
switch (type) {
358-
case "db2":
359-
return new DB2Templates();
360-
case "mysql":
361-
return new MySQLTemplates();
362-
case "mariadb":
363-
return new MySQLTemplates();
364-
case "h2":
365-
return new H2Templates();
366-
case "hsqldb":
367-
return new HSQLDBTemplates();
368-
case "pgsql":
369-
return new PostgreSQLTemplates();
370-
case "postgresql":
371-
return new PostgreSQLTemplates();
372-
case "sqlite":
373-
return new SQLiteTemplates();
374-
case "oracle":
375-
return new OracleTemplates();
376-
case "firebirdsql":
377-
return new FirebirdTemplates();
378-
default:
379-
throw new IllegalStateException("Unsupported database: " + type);
380-
}
383+
return when(type)
384+
.<SQLTemplates>is("db2", DB2Templates::new)
385+
.is("mysql", MySQLTemplates::new)
386+
.is("mariadb", MySQLTemplates::new)
387+
.is("h2", H2Templates::new)
388+
.is("hsqldb", HSQLDBTemplates::new)
389+
.is("pgsql", PostgreSQLTemplates::new)
390+
.is("postgresql", PostgreSQLTemplates::new)
391+
.is("sqlite", SQLiteTemplates::new)
392+
.is("oracle", OracleTemplates::new)
393+
.is("firebirdsql", FirebirdTemplates::new)
394+
.is("sqlserver", SQLServer2012Templates::new)
395+
.orElseThrow(() -> new IllegalStateException("Unsupported database: " + type));
381396
}
382397
}

0 commit comments

Comments
 (0)