问题
helllo
I can't adapt this example of code : https://github.com/pedrofurla/slick-demo/blob/master/src/main/scala/slickdemo/domain/Person.scala.
it deals with the capacity of comparing dates in a slick query. The difficulty is you can't access to the date field(to extract for example the year, the month and the day) because the query uses Column types.
I searched and found the example above, but I can't adapt it.
here is my code:
the Table:
class Personnes( tag : Tag ) extends Table[ Rdv ]( tag, "RDV" ) {
def id = column[ Int ]( "ID", O.PrimaryKey, O.AutoInc )
def nom = column[ String ]( "NOM", O.NotNull )
def prénom = column[ String ]( "PRENOM" )
def sexe = column[ Int ]( "SEXE" )
def télPortable = column[ String ]( "TELPOR" )
def télBureau = column[ String ]( "TELBUR" )
def télPrivé = column[ String ]( "TELPRI" )
def siteRDV = column[ String ]( "SITE" )
def typeRDV = column[ String ]( "TYPE" )
def libelléRDV = column[ String ]( "LIBELLE" )
def numRDV = column[ String ]( "NUMRDV" )
def étape = column[ String ]( "ETAPE" )
def dateRDV = column[ java.sql.Date ]( "DATE" )
def heureRDVString = column[ String ]( "HEURE" )
def statut = column[ String ]( "STATUT" )
def orderId = column[ String ]( "ORDERID" )
def * = ( id.?, nom, prénom, sexe, télPortable, télBureau, télPrivé,
siteRDV, typeRDV, libelléRDV, numRDV, étape, dateRDV, heureRDVString,
statut, orderId ) <> ( Rdv.tupled, Rdv.unapply _ )
}
and the method:
def sqlite_findBetween( début : java.util.Date, fin : java.util.Date ) : List[ Rdv ]= db_sqlite.withSession { implicit db : Session =>
def débutTime:org.joda.time.DateTime = new DateTime(début)
def finTime=new DateTime(fin)
val query = for {
p <- personnes
if (p.dateRDV > débutTime && finTime < p.dateRDV)
} yield p
query.list
}
thanks!
ps : I forgot the error : near the date comparison, I get :
Cannot perform option-mapped operation with type: (java.sql.Date, org.joda.time.DateTime) => R for base type: (java.sql.Date, java.sql.Date) => Boolean
ps2 : here is the utility file : I have obviously added it in the classpath : https://github.com/pedrofurla/slick-demo/blob/master/src/main/scala/slickdemo/dal/JodaTimeSupport.scala, with this :
import models.JodaTimeSupport
package models {
trait sqlite extends JodaTimeSupport {...
回答1:
Aside from creating your own implicit MappedColumnType
from java.sql.Date
to org.joda.time.DateTime
, you can try the popular slick-joda-mapper found here on Github.
来源:https://stackoverflow.com/questions/21349547/compare-2-dates-in-a-slick-query-seems-hard