0

I have case classes which have Option[java.sql.Timestamp] field.

case class BooRow(id:Int, createdTs: Option[java.sql.Timestamp])

case class FooRow(id: Int, name:String, deptId:Int,createdTs:Option[java.sql.Timestamp])

case class BarRow(id:Int,name:String)

looking for a generic solution which is also compile time safe to observe incoming case class instance and if it finds createdTs:Option[java.sql.Timestamp] in it then populate it.

Originally thought started with looking at shapless and had posted this question. Gabriele 's answer is right in the context of that previous question but may be I wasn't clear enough in the question so not sure how to "catch" "implicit not found error for that shapeless solution" (for the case class which doesn't have createdTs field) somehow. Generic function should populate createdTs if found in a case class instance it was passed, if it doesn't find createdTs in an instance of a case class then do nothing.

I have tagged slick for this question because these are slick generated case classes (not the reason for tagging) but I assume this can be a common need (generically populating such timestamp columns) when dealing with slick/databases in general. To clarify, I am not looking for db based solutions (like triggers etc) but something at an application level also without using structural types.

Edit: Providing default value to createdTs in case class declaration is not a desired solution. That will stamp wrong timestamp when case class instance is created for an "update" query.

Vikas Pandya
  • 1,998
  • 1
  • 15
  • 32

1 Answers1

0

Depending on how you are expecting to arrive at instances of you case classes, one option might be to ensure the createdTs parameter is given a default value on creation:

case class BooRow(id:Int, createdTs: Option[java.sql.Timestamp] = Some(new java.sql.Timestamp((new java.util.Date).getTime)))

However, it isn't clear from your question if this can cover your particular use case.

Shadowlands
  • 14,994
  • 4
  • 45
  • 43