Search code examples
scalaplayframeworkslick

Scala. Play: get data and test result


I have a sql query which returns two rows and columns values. I want to get that data and check if its empty or not. How can i do that? I am a fresh man to scala with playframework can any one please help me here. I have used model class to store the response and show in json format but i want to know how to check the condition for the data.

val query= s""" select * from table"""

override def map2Object(implicit map: Map[String, Any]): HierarchyEntryBillingRoleCheck = {
  HierarchyEntryBillingRoleCheck(str("roleName"), oint("PersonID"))
}

Now i want to know how to check the condition whether the sql return empty or true data ad i want to execute the next sql query.

SO I have tried some ways in which i get error in type casting:

override val singleQuery = s"""select * from Sitable"""
override val allQuery= s"""select * from Sitable1"""
override def map2Object(implicit map: Map[String, Any]): HierarchyEntryBillingRoleCheck = {
  HierarchyEntryBillingRoleCheck(str("roleName"), oint("PersonID"))
}

def map2ObjectBilling(implicit map: Map[String, Any]): HierarchyEntryBilling = {
  HierarchyEntryBilling(str("Name"), str("Provider"), oint("Year"), 
    ostr("Month"), ostr("Status"), ostr("ProviderType"))
}

def getAll(implicit loginName: String): Future[Seq[HierarchyEntryBillingRoleCheck]] = {
  doQueryIgnoreRowErrors(allQuery, "loginName" -> loginName)

  result.map {
    if (map2Object.roleName !="" && map2Object.PersonID.isEmpty) {
      "error"
    } else {
      getOnetask()
    }  //GEtting error with unit type cannot resolve future[seq[]]
  }

  def getOnetask(): Future[Seq[HierarchyEntryBilling]] = {
    doQueryIgnoreRowErrors(singleQuery)
  } //Getting error with type unit does not confirm Future[Seq[]]

.// doQueryignore errors signature=>

  protected def doQueryIgnoreRowErrors(query: String, args: NamedParameter*) 
   = {

 logger.debug(s"SQL: $query, args: $args")

 TimedFuture(actualityTimeout) {
  queryHandler.doQuery(query, args: _*) map { list =>
    // ignore mapping errors of specific rows
    list.flatten
   }
  } flatMap {
  case scala.util.Success(s) => Future.successful(s)
  case Failure(ex) if ex.isInstanceOf[SQLException] &&
    ex.getMessage == "The executeQuery method must return a result set." => 
  Future.successful(Nil)
  case Failure(fail)         =>
    Future.failed(fail)

Solution

  • Please do check signature for your doQueryIgnoreRowErrors method I would suspect it should be something like:

     def doQueryIgnoreRowErrors(sql: String, name: String): Future[Seq[HierarchyEntryBillingRoleCheck]] = {
        // next lines instead of calling data sources
        Future[Seq[HierarchyEntryBillingRoleCheck]] {
          List(HierarchyEntryBillingRoleCheck(1, "Data"))
        }
      }
    

    I would assume you are getting result like:

        def getAll(implicit loginName: String): Future[Seq[HierarchyEntryBillingRoleCheck]] = {
    
    val result = doQueryIgnoreRowErrors(allQuery, "loginName")
    .. 
    }
    

    Let me know if you are still struggling with this.