简体   繁体   中英

How would I import tables into another class (/object??) so I can run queries on it in the other class/object? [slick 3.0][scala]

I have two tables defined in a

 class Patients(tag: Tag) extends Table[(String, String, Int, String)](tag, "Patientss") {
    def PID = column[String]("Patient Id", O.PrimaryKey)

    def Gender = column[String]("Gender")

    def Age = column[Int]("Age")

    def Ethnicity = column[String]("Ethnicity")

    def * = (PID, Gender, Age, Ethnicity)
  }

  val patientsss = TableQuery[Patients]

  class DrugEffect(tag: Tag) extends Table[(String, String, Double)](tag, "DrugEffectss") {

    def DrugID = column[String]("Drug ID", O.PrimaryKey)

    def PatientID = column[String]("Patient_ID")

    def DrugEffectssss = column[Double]("Drug Effect")

    def * = (DrugID, PatientID, DrugEffectssss)

    def Patient = foreignKey("Patient_FK", PatientID, patientsss)(_.PID)
  }

  val d_effects = TableQuery[DrugEffect]

I fill in the tables in this particular object/class as well.

I was wondering how I could call the filled in tables in another object so I can access both DrugEffect and Patients as a class, and then run queries on the table itself?

I hope I'm making myself clear, I don't really have a clue about what I'm doing

What I mean by running queries is something like this:

val q1 = for {
    c <- patientsss if (c.Age === 20 && c.Gender === "F")
    s <- d_effects if (s.DrugEffectssss > 10.0)
  } yield (c.PID, s.DrugID)

but in an object defined in a different file

You need the DB API and the table in the separate class. You can do something like:

import tables.Tables

class SeparateClass extends HasDatabaseConfig[JdbcProfile] {
  val dbConfig = DatabaseConfigProvider.get[JdbcProfile](Play.current)

  import driver.api._

  def get(id: Long) = {
    db.run(Tables.DrugEffect.d_effects.filter(_.id === id).result)
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM