2016-02-12 2 views
0

Я хотел бы добавить код класса caseID в свой код. И я хотел бы упростить метод * путем автоматического сопоставления идентификаторов ProviderID и Int.Slick MappedColumnType

case class SupplierID(id: Int) 

    case class Supplier(id: SupplierID, name: String, street: String, city: String, state: String, zip: String) 
    // Definition of the SUPPLIERS table using case class 
    class Suppliers(tag: Tag) extends Table[Supplier](tag, "SUPPLIERS") { 
    def id = column[Int]("SUP_ID", O.PrimaryKey) // This is the primary key column 
    def name = column[String]("SUP_NAME") 
    def street = column[String]("STREET") 
    def city = column[String]("CITY") 
    def state = column[String]("STATE") 
    def zip = column[String]("ZIP") 

    def * ={ 
     (id, name, street, city, state, zip).shaped.<>({ tuple => Supplier.apply(SupplierID(tuple._1), tuple._2, tuple._3, tuple._4, tuple._5, tuple._6)}, { 
     (s : Supplier) => Some{(s.id.id, s.name, s.street, s.city, s.state, s.zip)} 
     }) 
    } 

    } 

Я пытаюсь реализовать тип столбца, как этот

// And a ColumnType that maps it to Int 
    implicit val SupplierIDColumnType = MappedColumnType.base[SupplierID, Int](
    s => s.id, // map Bool to Int 
    i => SupplierID(i) // map Int to Bool 
) 

Как использовать такой mappingtype?

ответ

0

Решение:

case class SupplierID(id: Int) 

    // And a ColumnType that maps it to Int 
    implicit val SupplierIDColumnType = MappedColumnType.base[SupplierID, Int](
    s => s.id, // map SupplierID to Int 
    i => SupplierID(i) // map Int to SupplierID 
) 

    case class Supplier(id: SupplierID, name: String, street: String, city: String, state: String, zip: String) 
    // Definition of the SUPPLIERS table using case class 
    class Suppliers(tag: Tag) extends Table[Supplier](tag, "SUPPLIERS") { 
    def id = column[SupplierID]("SUP_ID", O.PrimaryKey) // This is the primary key column 
    def name = column[String]("SUP_NAME") 


     def street = column[String]("STREET") 
     def city = column[String]("CITY") 
     def state = column[String]("STATE") 
     def zip = column[String]("ZIP") 

     def * = (id, name, street, city, state, zip) <>(Supplier.tupled , Supplier.unapply _) 

    } 

 Смежные вопросы

  • Нет связанных вопросов^_^