zio / zio-quill

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

Bug with lift + mappedEncoding #407

Closed pronvis closed 7 years ago

pronvis commented 8 years ago

Version: 0.6.0 Module: quill-jdbc

Expected behavior

Select rows from DB.

Actual behavior

Compile time error.

Steps to reproduce the behavior

For example we have a Table with column type char(1) that actually a enum. In scala code we want to use handwritten objects as enums, for example:

object Status {
  trait Type
  case object N extends Type
  case object B extends Type
}

To use this class in Quill we should add mappedEncodings:

implicit val encoderStatus = mappedEncoding[Status.Type, String] {
    case Status.N => "N"
    case Status.B => "B"
  }

  implicit val decoderStatus = mappedEncoding[String, Status.Type] {
    case "N" => Status.N
    case "B" => Status.B
  }

Now we can create and run those queries:

    val q2 = quote {
      (status: Status.Type) => query[UserStatus].filter(_.status == status)
    }
db.run(q2)(Status.B)

    def q3(status: Status.Type) = {
      val q = quote {
        query[UserStatus].filter(_.status == lift(status))
      }
      db.run(q)
    }
q3(Status.B)

And all will be fine! But, if we want to use this query:

val q = quote {
      query[UserStatus].filter(s => s.status == lift(Status.B))
    }
db.run(q)

We will have

Source doesn't know how do encode 'Status.B: Status.B.type'

Of course we can add new mappedEncoding: implicit val encodeBType = mappedEncoding[Status.B.type, String](_ => "B") But this is not want we want to do.

@getquill/maintainers

gustavoamigo commented 7 years ago

I've done some testing. I think this case is very close to #584. If you add : Status.Type to your lift, it will work. Here's the complete example:

    val q = quote {
      query[UserStatus].filter(s => s.status == lift(Status.B: Status.Type))
    }

I don't think there is an easy way to fix this issue.

fwbrasil commented 7 years ago

closing in favor of https://github.com/getquill/quill/issues/584