Rotate the Scala Anorm entry to a specific object

Using findBy, as shown below, I can search the table Authenticationsfor writing with keyandvalue

def findBy(key: String, value: String): Any = DB.withConnection { implicit connection =>
  SQL("select * from authentications where {key}={value} limit 1").on('key -> key, 'value -> value)
}

Example:

Authentication.findByMe("email", "toot@toot.com")

Return:

res1: Any = SimpleSql(SqlQuery(select * from authentications where ?=? 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 I get it back to the authentication object? An object with all the fields that I received from the request.

I tried the following, but I cannot get a nullable object error when there is something empty in one of the columns. I can’t understand how to use the proposed solutions 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 it like this:

def findBy(key: String, value: String): Any = DB.withConnection { implicit connection =>
  SQL("select * from authentications where {key}={value} limit 1").on('key -> key, 'value -> value).as(authentication *)
}

I am also new to Scala, so I was riddled with issues like this.

+3
source share
1 answer

Option Authentication. , :

`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

:

case class Authentication(email: String, password: Option[String], userId: Int, token: Option[String], createdAt: Date, updatedAt: Option[Date])

:

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)
   }
}

- Option Authentication, getOrElse ( ):

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())

:

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)
   }
}

.as(). SQL("....").on(...).as(authentication_parser *) List[Authentication]. LIMIT 1 , authentication_parser.single ( Authentication) authentication_parser.singleOpt ( Option[Authentication]). singleOpt - , single , .

, - , Any. ( ) .

+3

All Articles