Anorm, Magic and Database Updates

141 views
Skip to first unread message

Joe Terry

unread,
Oct 9, 2011, 3:24:01 PM10/9/11
to play-framework
I have the following code working mostly ...

See the line ... //session.valid = false

If I un-comment that line I get a compilation error ....

Error raised is : reassignment to val

As you can see I look for the simplest possible solution to
problems ... I see a great deal of confusion in the Play/Scala
world ... with limited knowledge of Scala ... people like me ...
people are doing all kinds of crazy things to get their programs to
work ...

Please, the simplest solution ?


====================================

case class Session(
id: Pk[Long],
created: Date, modified: Date,
session_key: String, value: String, valid: Boolean
)

object Session extends Magic[Session] {

def put(session_key: String, value: String) = {

val session = get(session_key)

Logger.info("1. session: " + session)

session match {
case Some( session ) => {
session
Logger.info("2. session: " + session)
}
case None => {
Logger.info("3. session: " + session_key + " value: " + value)
Session.insert(new Session(NotAssigned, new Date(), new
Date(), session_key, value, true))
}
}
}

def get(session_key: String) = {

Session.find("session_key = {session_key} and valid = true")
.on("session_key" -> session_key)
.first()
}

def invalidate(session_key: String) = {

var session = get(session_key)
session match {
case Some( session ) => {
//session.valid = false
Logger.info("1. invalidate: " + session)
Session.update(session)
}
case None => {
}
}
}
}

Ivan Meredith

unread,
Oct 9, 2011, 8:30:49 PM10/9/11
to play-fr...@googlegroups.com
case class objects are immutable. You need to do session.copy(value=false)

> --
> You received this message because you are subscribed to the Google Groups "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>
>

Joe Terry

unread,
Oct 9, 2011, 9:14:29 PM10/9/11
to play-framework
Yeah, page 263 in my edition of PinS ... Programming in Scala ...

Ok ... a little light reading for me ... Case classes, pattern
matching, sealed classes and the Option type, obviously all very
important subjects for developers using Anorm.

But, the part about case classes being immutable ... Where's that
discussed ?

Joet



On Oct 9, 5:30 pm, Ivan Meredith <i...@ivan.net.nz> wrote:
> case class objects are immutable. You need to do session.copy(value=false)
>

Joe Terry

unread,
Oct 9, 2011, 9:36:27 PM10/9/11
to play-framework
def get(session_key: String) = {
Session.find("session_key = {session_key} and valid =
true")
.on("session_key" -> session_key)
.first()
}

def invalidate(session_key: String) = {
val session = get(session_key)
session match {
case Some( session ) => {
Session.update(session.copy(valid=false))
}
case None => {
}
}
}
==================
That is a thing of beauty ... that's my kind of database update
method. This is all in the development of a database-backed session
management scheme.

So far, so good !

Joet
Reply all
Reply to author
Forward
0 new messages