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

Add support of dynamic table names #373

Closed beikern closed 5 years ago

beikern commented 8 years ago

First, forgive me about the possible ambiguous title, feel free to edit it if needed.

Version: 0.6.0 Module: quill-cassandra

Expected behavior

The insert query should be executed pointing at the table "test_table"

Actual behavior

The insert query is executed pointing at the table "foo", using the naming strategy defined in CassandraAsyncSource (SnakeCase in this example) throwing the following error

error] (run-main-0) com.google.common.util.concurrent.UncheckedExecutionException: com.datastax.driver.core.exceptions.InvalidQueryException: unconfigured table foo
com.google.common.util.concurrent.UncheckedExecutionException: com.datastax.driver.core.exceptions.InvalidQueryException: unconfigured table foo
    at com.google.common.cache.LocalCache$Segment.get(LocalCache.java:2201)
    at com.google.common.cache.LocalCache.get(LocalCache.java:3934)
    at com.google.common.cache.LocalCache$LocalManualCache.get(LocalCache.java:4736)
    at io.getquill.sources.cassandra.PrepareStatementCache.apply(PrepareStatementCache.scala:16)
    at io.getquill.sources.cassandra.CassandraSourceSession.prepare(CassandraSourceSession.scala:29)
    at io.getquill.sources.cassandra.CassandraSourceSession.prepare(CassandraSourceSession.scala:34)
    at io.getquill.sources.cassandra.CassandraAsyncSource.io$getquill$sources$cassandra$CassandraAsyncSource$$run$1(CassandraAsyncSource.scala:43)
    at io.getquill.sources.cassandra.CassandraAsyncSource$$anonfun$executeBatch$1.apply(CassandraAsyncSource.scala:47)
    at io.getquill.sources.cassandra.CassandraAsyncSource$$anonfun$executeBatch$1.apply(CassandraAsyncSource.scala:47)
    at io.getquill.sources.cassandra.CassandraAsyncSource$ActionApply.apply(CassandraAsyncSource.scala:25)
    at com.beikern.test.app.Main$.delayedEndpoint$com$beikern$test$app$Main$1(Main.scala:141)
    at com.beikern.test.app.Main$delayedInit$body.apply(Main.scala:9)
    at scala.Function0$class.apply$mcV$sp(Function0.scala:34)
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
    at scala.App$$anonfun$main$1.apply(App.scala:76)
...

Steps to reproduce the behavior


implicit val encodeDate = mappedEncoding[Date, DateTime](new DateTime(_, DateTimeZone.UTC))
  implicit val decodeDate = mappedEncoding[DateTime, Date](_.toDate)

  object schema {
    val foos = quote {
      (entityName: String) =>
      query[Foo].schema(
        _.entity(entityName)
          .columns(_.id -> "i",
            _.columnOne -> "v",
            _.columnTwo -> "v1",
            _.columnThree -> "v2"))
    }
  }

/**
    * CQL table, indirect mapping, the case class and the table don't have the same name fields
    *
    * The CQL to create the table is the following
    * CREATE TABLE IF NOT EXISTS test_table (
    *      i text,
    *      v int,
    *      v1 bigint,
    *      v2 timestamp,
    *      v3 decimal,
    *      PRIMARY KEY (i)
    *  );
    *
    */

case class Foo (id: String,
    columnOne: Int,
    columnTwo: Long,
    columnThree: DateTime
  )

  val db: CassandraAsyncSource[SnakeCase] = source (new CassandraAsyncSourceConfig[SnakeCase]("test"))

  db.run(schema.foos("test_table").insert)(Foo("idToInsert:D", 1, 1L, new DateTime()))

@getquill/maintainers

gustavoamigo commented 8 years ago

@getquill/maintainers SQL does not support passing table names or column names as parameters. Therefore, my question is, Is this feature a priority? Can anyone think of another way to do this?

fwbrasil commented 8 years ago

@gustavoamigo I think it wouldn't be hard to support it. Maybe extending BetaReduction to reduce the schema definition as well would be enough?

gustavoamigo commented 8 years ago

@fwbrasil We would need to change the Entity AST to use Ident instead of pure String for table alias and change the BetaReduction. But there is a big but, we would open Quill to SQL Injection vulnerabilities.

fwbrasil commented 8 years ago

@gustavoamigo good point. I'd remove the 1.0 tag then and think about it after the 1.0 release.

gustavoamigo commented 8 years ago

We should also have a better error message, related to #42

vveresko commented 7 years ago

Hi guys! We really need feature to change table name dynamically in runtime, because we deal with great amount of tables with similar structure but different name in cassandra. Can you please advice how to tweak quill quickly for this?

rusho commented 6 years ago

I'm also interested in when this could be expected. We're at the decision point at the project regarding DB, and this is currently the only but major downside for us as we use multiple schemas at Postgres. So knowing approximate timeframe would be great

jilen commented 6 years ago

Another way is using infix with literal interpolation as mentioned #223 , and allow infix as table.

For example, we may add liftSql and process that ast within ReifyStatement

val suffix = "123"
val foo  = quote(infix"foo_${liftSql(suffix)}".as[EntityQuery[Foo]])

This way may be much easier

fwbrasil commented 5 years ago

Closed in favor of https://github.com/getquill/quill/issues/223