pawank
pawank

Reputation: 113

How to get class object as outcome rather than tuples in ScalaQuery?

I have started my first project in Scala and ScalaQuery. So far both are looking good and promising though I am having little difficulty once in a while.

Can someone please explain me how to get a class object (in this case Domain case class having around 12 columns) instead of tuples. Below query is returning tuples but the problem is I need around 9 columns(or all columns) of the table without providing each column name in the query yield. Domain class already has * defining all columns then why would the query below is returning tuples instead of Domain object so that I can use Domain.name, Domain.level instead of figuring the position in tuple returned.

val ext_id = 'ns1.amazon.com'
val name = "www.getcrazy.com"
val validDomains = for {p <- Domain where { p => (p.ext_id is ext_id) && (p.domain_name is name) && (p.is_deleted is false) && (p.result_code is "1000")}} yield *

for(pp <- validDomains) {
            logger.debug("State is " + pp._6 + " for domain ID - " + pp._1)
}

Any suggestion?

Thanks, Pawan

Upvotes: 2

Views: 569

Answers (1)

Noel
Noel

Reputation: 2091

Create an object that extends org.scalaquery.ql.basic.BasicTable. For example,

case class Community (id:String, name:String, latitude:Double, longitude:Double)
object CommunityTable extends Table[Community]("Communities") {
  def id = column[String]("ID", O PrimaryKey, O NotNull, O DBType "uniqueidentifier")
  def name = column[String]("Name", O NotNull, O DBType "varchar(255)")
  def longitude = column[Double]("Longitude")
  def latitude = column[Double]("Latitude")
  def * = id ~ name ~ longitude ~ latitude <> (Community, Community.unapply _)
}

With this, you can simply query the CommunityTable and get a List[Community] back:

val c: List[Community] = CommunityTable.where(c => (c.name startsWith term)).list

Upvotes: 5

Related Questions