Replacement for MappedTypeMapper in v. 2?

1,219 views
Skip to first unread message

Adam Mackler

unread,
Oct 1, 2013, 1:34:41 PM10/1/13
to scala...@googlegroups.com
I know it's not officially released yet, but I am trying to use 2.0.0-M2.  I see that MappedTypeMapper is gone, but I'm not sure what replaces it.  Would it be possible to get a code sample of how to do simple type mapping?  For example between org.joda.time.DateTime <-> java.sql.Timestamp, or org.joda.time.DateTimeZone <-> String?  As much explanation and detail as can be provided I will eagerly consume, but just a simple example to get me unstuck would be wonderfully helpful.

Thanks,
--
Adam Mackler

Peter Rakhunov

unread,
Oct 1, 2013, 5:38:32 PM10/1/13
to scala...@googlegroups.com
It is MappedColumnType now.
Usage seems to be about the same.

Adam Mackler

unread,
Oct 2, 2013, 7:04:21 AM10/2/13
to scala...@googlegroups.com
On Tuesday, October 1, 2013 5:38:32 PM UTC-4, Peter Rakhunov wrote:
It is MappedColumnType now.
Usage seems to be about the same.

And even easier than before.  Thanks!!

--
Adam Mackler
 

Adam Mackler

unread,
Oct 2, 2013, 10:29:29 AM10/2/13
to scala...@googlegroups.com
Just for the record, my code.  Allows me to use joda time DateTime and DateTimeZone:

  implicit val dateTimeColumnType = MappedColumnType.base[DateTime, Timestamp](
      { dt => new java.sql.Timestamp(dt.getMillis) },
      { ts => new DateTime(ts) }
    )

  implicit val timeZoneColumnType = MappedColumnType.base[DateTimeZone, String](
      { tz => tz.getID },
      { s => org.joda.time.DateTimeZone.forID(s) }
    )

Used with Postgres in conjunction with this very helpful custom timezone type that checks for the validity of the time zone:  http://justatheory.com/computers/databases/postgresql/timezone_validation.html
(but note the comment from Tom Lane--I changed "WHEN OTHERS" to "WHEN SQLSTATE '22023'").

--
Adam Mackler

Reid Spencer

unread,
Nov 13, 2013, 3:52:05 PM11/13/13
to scala...@googlegroups.com
Even MappedColumnType seems to be gone in 2.0.0-M3

Anyone know the magic for M3 ?

Christopher Vogt

unread,
Nov 13, 2013, 9:35:10 PM11/13/13
to scala...@googlegroups.com

> Even MappedColumnType seems to be gone in 2.0.0-M3
>
> Anyone know the magic for M3 ?

It's a feature of JdbcProfile now. I suggest you use that one instead of
BasicProfile for the time being.

Chris

Reid Spencer

unread,
Nov 13, 2013, 10:12:20 PM11/13/13
to scala...@googlegroups.com
Since my original posting I went down that road (JdbcProfile) already and that seemed to make things worse. First, it locks in to a JDBC based driver which I’m trying to avoid. I’m okay with SqlProfile or RelationalProfile as those levels of abstraction I wish to retain. However, they are incomplete and so it is not possible to us them (e.g. can’t use a SqlProfile#Session to actually initiate a database session). In the case of using JdbcProfile, I get no definitions of Table[T] or TableQuery[T] when I import profile.simple._ into a trait. I’m sure all this is clear to you, but without release documentation it is very difficult to grasp .. even harder to extend. Rather than attempt to describe the difficulty I am having, I will work up an example and put it on github so it can be dissected there.

Thanks for helping me sort through this.

Reid.
> --
>
> ---
> You received this message because you are subscribed to a topic in the Google Groups "Slick / ScalaQuery" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/scalaquery/4Ns_J_8wbqQ/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to scalaquery+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.

Stefan Zeiger

unread,
Dec 2, 2013, 8:35:24 AM12/2/13
to scala...@googlegroups.com
MappedColumnType.base is in RelationalProfile now (to be released in 2.0.0-RC1): https://github.com/slick/slick/pull/534

And if you have control over the type you want to map, it's easier to use than ever:

    case class MyCustomId(value: Long) extends MappedTo[Long]

That's it, no more implicit to write. It's generated by an implicit macro.

--
Stefan Zeiger
Slick Tech Lead
Typesafe - Build Reactive Apps!
Twitter: @StefanZeiger

TexasMynsted

unread,
Apr 22, 2014, 1:54:05 PM4/22/14
to scala...@googlegroups.com
I need to map Joda DateTime to all Timestamp fields via a CustomSourceCodeGenerator.  I have not done any such mapping so I am not sure how to accomplish this. 

I see userdefined.html#scalar-types and so it looks like I should be doing something like what Adam showed above.


implicit val dateTimeColumnType = MappedColumnType.base[DateTime, Timestamp](
      { dt => new java.sql.Timestamp(dt.getMillis) },
      { ts => new DateTime(ts) }
    )

I see some code generation customization details here
code-generation.html#customization.

Do I somehow put that implicit somewhere in the the SourceCodeGenerator or do I customize the Column generation with something like (clearly not correct as written)?

val codegen = new scala.slick.model.codegen.SourceCodeGenerator(model){
  override def Table = new Table(_){
        table =>
    override def Column = new Column(_){  

     override
def rawType = if(this.tpe == "java.sql.Timestamp") "org.joda.time.DateTime" else super.rawType }
  }
}

I appreciate any help.

TexasMynsted

unread,
Apr 23, 2014, 5:41:29 PM4/23/14
to scala...@googlegroups.com
Ok so something like this looks closer perhaps, but it fails at least because "org.joda.time.DateTime" is a String and not Code...

import scala.slick.model.codegen.SourceCodeGenerator
import scala.slick.{model => m}

class CustomSourceCodeGenerator(model: m.Model) extends SourceCodeGenerator(model) {
  override def code = {
    val typeMapperCode = """ //Map between org.joda.time.DateTime and java.sqlTimestamp
                           | implicit val mappedDateTime = MappedColumnType.base[DateTime, java.sql.Timestamp](
                           | {d => new java.sql.Timestamp(d.getMillis)}, // map DateTime to Timestamp
                           | {t => new DateTime(t)} // map Timestamp to DateTime
                           | ) """
    typeMapperCode.stripMargin + "\n" + super.code

  }
  override def Table = new Table(_) {
    override def Column = new Column(_) {
      override def rawType = this.rawType match {
        case "java.sql.Timestamp" => "org.joda.time.DateTime"
        case _ => super.rawType
      }
    }
  }
}

Any hints?
Reply all
Reply to author
Forward
0 new messages