playframework 2.0 - Turn Scala Anorm record into a specific object -


using findby seen below, can search authentications table record key , value

def findby(key: string, value: string): = db.withconnection { implicit connection =>   sql("select * authentications {key}={value} limit 1").on('key -> key, 'value -> value) } 

example:

authentication.findbyme("email", "toot@toot.com") 

returns:

res1: = simplesql(sqlquery(select * authentications ?=? limit 1,list(key, value),none),list((key,parametervalue(email,anorm.tostatement$$anon$3@4ca9aed5)), (value,parametervalue(toot@toot.com,anorm.tostatement$$anon$3@18154945))),<function1>) 

how can turn authentication object? object fields got query.

i've tried following, can't passed nullable object error when there's there's null in 1 of columns. can't figure out how put use solutions offered here

val authentication_parser =   get[string]("email") ~   get[string]("encrypted_password") ~   get[string]("user_id") ~   get[string]("token") ~   get[date]("created_at") ~   get[date]("updated_at")  val authentication = {   authentication_parser map {     case email~encrypted_password~user_id~token~created_at~updated_at => authentication(email, encrypted_password, user_id, token, created_at, updated_at)   } } 

using so:

def findby(key: string, value: string): = db.withconnection { implicit connection =>   sql("select * authentications {key}={value} limit 1").on('key -> key, 'value -> value).as(authentication *) } 

i'm new scala, i've been riddled problems one.

any column nullable in table should option in authentication object. example, if table this:

`email` varchar(255) not null, `encrypted_password` varchar(255) null, `user_id` int(11) not null, `token` varchar(255) null, `created_at` datetime not null, `updated_at` datetime null 

then should have:

case class authentication(email: string, password: option[string], userid: int, token: option[string], createdat: date, updatedat: option[date]) 

your parser like:

val authentication_parser = {    get[string]("email") ~    get[option[string]]("encrypted_password") ~    get[string]("user_id") ~    get[option[string]]("token") ~    get[date]("created_at") ~    get[option[date]]("updated_at") map {        case email~encrypted_password~user_id~token~created_at~updated_at =>             authentication(email, encrypted_password, user_id, token, created_at, updated_at)    } } 

if reason don't want use option in authentication class, use getorelse in map portion , fill in default values (much uglier):

case email~encrypted_password~user_id~token~created_at~updated_at =>             authentication(email, encrypted_password.getorelse(""), user_id, token.getorelse(""), created_at, updated_at.getorelse(new date()) 

for paranoid, may want exclude hashed password parser when retrieving database:

val authentication_parser = {    get[string]("email") ~    get[string]("user_id") ~    get[option[string]]("token") ~    get[date]("created_at") ~    get[option[date]]("updated_at") map {        case email~user_id~token~created_at~updated_at =>             authentication(email, none, user_id, token, created_at, updated_at)    } } 

i suggest change .as() parameter. sql("....").on(...).as(authentication_parser *) parse list[authentication]. since you're using limit 1 in query, better use authentication_parser.single (parses authentication) or authentication_parser.singleopt (parses option[authentication]). singleopt way go single records, single raise exception if nothing found.

lastly, ought make return type of function other any. (whatever matches parser) sorry wall of text.


Comments