slick / slick

Slick (Scala Language Integrated Connection Kit) is a modern database query and access library for Scala
https://scala-slick.org/
BSD 2-Clause "Simplified" License
2.66k stars 610 forks source link

support date comparisons #627

Open cvogt opened 10 years ago

cvogt commented 10 years ago

somehow related to https://github.com/tototoshi/slick-joda-mapper http://stackoverflow.com/questions/21350399/filtering-all-records-which-have-a-joda-datetime-date-equal-to-today

ijuma commented 10 years ago

Also consider Java 8 date/time support.

szeiger commented 10 years ago

Let's see if we can do something about this in the GSoC project for adding database-specific features. Assigning to 2.2.

cvogt commented 10 years ago

@dimitrkovalsky is the corresponding GSOC student.

cvogt commented 10 years ago

Wasn't handled in the GSOC project. Another Stackoverflow instance of this: http://stackoverflow.com/questions/26457945/compare-joda-datetime-in-filter

szeiger commented 9 years ago

Works for me:

  def testMappedNumeric = {
    implicit val javaDateMapper = MappedColumnType.base[java.util.Date, java.sql.Timestamp](
      d => new java.sql.Timestamp(d.getTime),
      d => new java.util.Date(d.getTime))

    class T(tag: Tag) extends Table[(Int, java.util.Date)](tag, u"t") {
      def id = column[Int]("id", O.PrimaryKey)
      def d = column[java.util.Date]("d")
      def * = (id, d)
    }
    val ts = TableQuery[T]

    val d1 = new java.util.Date(java.sql.Date.valueOf("2012-12-24").getTime)
    val d2 = new java.util.Date(java.sql.Date.valueOf("2012-12-25").getTime)
    val d3 = new java.util.Date(java.sql.Date.valueOf("2012-12-26").getTime)
    val d4 = new java.util.Date(java.sql.Date.valueOf("2012-12-27").getTime)

    seq(
      ts.schema.create,
      ts ++= Seq((1, d1), (2, d2), (3, d3), (4, d4)),
      ts.filter(_.d === d2).map(_.id).to[Set].result.map(_ shouldBe Set(2)),
      ts.filter(_.d >= d2).map(_.id).to[Set].result.map(_ shouldBe Set(2, 3, 4))
    )
  }

It looks like the problem in the SO posts is that people use == instead of ===