Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mapping a null value to JsValue #285

Closed
alex88 opened this issue Jun 16, 2016 · 7 comments
Closed

Mapping a null value to JsValue #285

alex88 opened this issue Jun 16, 2016 · 7 comments

Comments

@alex88
Copy link

alex88 commented Jun 16, 2016

I've this query

var onTargetShots = events.filter(event => event.playerOptaId.inSet(players) && (event.eventType === 15 || event.eventType === 16))
var offTargetShots = events.filter(event => event.playerOptaId.inSet(players) && (event.eventType === 13 || event.eventType === 14))

val data = onTargetShots.map(event => (event.posX, event.posY, (event.qualifiers +> "140"), (event.qualifiers +> "141"), "on-target")).unionAll(offTargetShots.map(event => (event.posX, event.posY, (event.qualifiers +> "140"), (event.qualifiers +> "141"), "off-target")))

db.run(data.result)

and I get this slick issue:

SlickException: Read NULL value (null) for ResultSet column <computed>

looking at the code:

object SpecializedJdbcResultConverter {
  /** Create a new type-specialized `BaseResultConverter` for the given type-specialized `JdbcType` */
  def base[T](ti: JdbcType[T], name: String, idx: Int) = (ti.scalaType match {
    case ScalaBaseType.byteType => new BaseResultConverter[Byte](ti.asInstanceOf[JdbcType[Byte]], name, idx)
    case ScalaBaseType.shortType => new BaseResultConverter[Short](ti.asInstanceOf[JdbcType[Short]], name, idx)
    case ScalaBaseType.intType => new BaseResultConverter[Int](ti.asInstanceOf[JdbcType[Int]], name, idx)
    case ScalaBaseType.longType => new BaseResultConverter[Long](ti.asInstanceOf[JdbcType[Long]], name, idx)
    case ScalaBaseType.charType => new BaseResultConverter[Char](ti.asInstanceOf[JdbcType[Char]], name, idx)
    case ScalaBaseType.floatType => new BaseResultConverter[Float](ti.asInstanceOf[JdbcType[Float]], name, idx)
    case ScalaBaseType.doubleType => new BaseResultConverter[Double](ti.asInstanceOf[JdbcType[Double]], name, idx)
    case ScalaBaseType.booleanType => new BaseResultConverter[Boolean](ti.asInstanceOf[JdbcType[Boolean]], name, idx)
    case _ => new BaseResultConverter[T](ti.asInstanceOf[JdbcType[T]], name, idx) {
      override def read(pr: ResultSet) = {
        val v = ti.getValue(pr, idx)
        if(v.asInstanceOf[AnyRef] eq null) throw new SlickException("Read NULL value ("+v+") for ResultSet column "+name)
        v
      }
    }
  }).asInstanceOf[ResultConverter[JdbcResultConverterDomain, T]]

seems it's trying to convert null to play.api.libs.json.JsValue.

However seems the conversion fails since it throws an exception, shouldn't it be returning a JsNull value?

@alex88
Copy link
Author

alex88 commented Jun 16, 2016

Nevermind, adding .? at the end of the selector worked

@alex88 alex88 closed this as completed Jun 16, 2016
@tminglei
Copy link
Owner

Yes, adding .? at the end is also my suggestion. :-)

@alex88
Copy link
Author

alex88 commented Jun 16, 2016

I just thought that since the return value was set to JsValue which includes JsNull, slick-pg was going to fill the JsValue with JsNull

@tminglei
Copy link
Owner

tminglei commented Jun 16, 2016

hmm ... yes, you're right. We should enhance it.
I'll check/do it asap.

@alex88
Copy link
Author

alex88 commented Jun 16, 2016

Not a problem, I'm just not sure if it's a slick or pg-slick responsibility. Both Option[JsValue] or JsNull are valid for null, I think it's just a matter of covering every possible cases or not.

@tminglei
Copy link
Owner

Updated, you can help check.
If no more questions, I'll release it.

@tminglei
Copy link
Owner

tminglei commented Aug 19, 2016

@alex88 when I processing #297, I noticed the null default mechanism was discarded by slick for some time. So I'd to remove it in the next release.


Well, that means null will be mapped to null or None instead of JsNull or Some(JsNull).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants