zio / zio-quill

Compile-time Language Integrated Queries for Scala
https://zio.dev/zio-quill
Apache License 2.0
2.15k stars 348 forks source link

multi-target sources #379

Open fwbrasil opened 8 years ago

fwbrasil commented 8 years ago

Version: (e.g. 0.6.0) Module: (e.g. quill-core)

Problem

Quill doesn't provide an easy way to switch to a different SQL dialect at runtime. Queries are generated at compile time and the application needs to be recompiled in order to run on another target dialect.

Solution

Introduce multi-target sources, that generate queries against multiple compatible target sources at compile-time and allows the user to choose the target source type at runtime.

By "compatible" target sources I mean sources that have the exact same method signatures. For instance, it'd be possible to have a

val s = source(new MultiSourceConfig(new JdbcConfig[SnakeCase, MysqlDialect]("mysql"), new JdbcConfig[Literal, H2Dialect]("h2"))

but not

val s = source(new MultiSourceConfig(new JdbcConfig[SnakeCase, MysqlDialect]("mysql"), new AsyncSource[Literal, H2Dialect]("h2")))

The macro would generate a message for all targets:

case class Person(fullName: String)

val s = source(new MultiSourceConfig(new JdbcConfig[SnakeCase, MysqlDialect]("mysql"), new JdbcConfig[Literal, H2Dialect]("h2"))

s.run(query[Person])
// compilation message
// mysql: SELECT full_name FROM person
// h2: SELECT fullName from Person

@getquill/maintainers

mxl commented 7 years ago

Workaround You can build your application for each context to separate artefacts (jars) and make some supervisor process to stop application with one context and start with another. See https://gitter.im/getquill/quill?at=58498469c29531ac5d393595. Also this's useful for those who just want to build application for different environment like test and prod with different contexts.

mxl commented 7 years ago

I made an example project showing how to switch between MySQL and H2 JDBC contexts at compile-time. Do not forget to checkout quill-multiple-jdbc-contexts branch.

mxl commented 7 years ago

If someone needs to switch between sync (e. g. H2JdbcContext) and async (e. g. PostgresAsyncContext) then take a look at this sample project (checkout quill-multiple-mixed-contexts branch).

fwbrasil commented 7 years ago

@getquill/maintainers I've been thinking about this feature but I couldn't find a good way to implement it. I think I'll remove it from the 2.0 plans. It's too complex and I don't see push from the community to have it. Wdyt?

fwbrasil commented 7 years ago

I forgot to mention something. Even without this feature, users still can use multiple contexts using @mxl's approach or falling back to dynamic queries using https://github.com/getquill/quill/pull/886

mxl commented 7 years ago

@fwbrasil 👌 We receive questions about context switching on Gitter channel occasionally. But people usually ask about compile-time context switching which can be done by above workaround though it does not work in all cases (for example, when switching sync and async contexts). I think that fixing bugs is top priority now and this feature can wait.

mielientiev commented 6 years ago

any progress or plans to resolve this issue?

mosyp commented 6 years ago

@getquill/maintainers I suggest to close this in favor of #971, since introducing multi-target sources should be avoided in favor of generating queries using idioms and io monad. Wdyt?