You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm playing with slick 2 and codegen, using MySql for the database. I started with the Coffee / Suppliers example from the hello slick activator template.
I'm trying to use codegen to create a new Tables.scala file from the Coffee and Suppliers database tables, which will allow the existing client code to work e.g.
I've been posting to the slick google group for help. I've had to use a custom generator to use tuples for the rows instead of case classes:
import scala.slick.driver.MySQLDriver
import scala.slick.driver.MySQLDriver.simple._
import scala.slick.codegen.SourceCodeGenerator
import scala.slick.model.Model
object SlickAutoGen {
def main(args: Array[String]) {
val db = Database.forConfig("db")
val model = db.withSession { implicit session =>
MySQLDriver.profile.createModel()
}
case class SourceGen(model: Model) extends SourceCodeGenerator(model) {
override def Table = new Table(_) {
override lazy val EntityType = new EntityType {
override val classEnabled: Boolean = false
}
}
}
SourceGen(model).writeToFile(
"scala.slick.driver.MySQLDriver", "src/main/scala", "org.ludwiggj.finance.database", "Tables", "Tables.scala")
}
}
This generates the following code for Tables.scala (relevant parts shown only):
trait Tables {
...
/** Row type of table Coffees */
type CoffeesRow = (String, Int, Double, Int, Int)
/** Constructor for CoffeesRow providing default values if available in the database schema. */
def CoffeesRow(name: String, supId: Int, price: Double, sales: Int, total: Int): CoffeesRow = {
(name, supId, price, sales, total)
}
/** GetResult implicit for fetching CoffeesRow objects using plain SQL queries */
implicit def GetResultCoffeesRow(implicit e0: GR[String], e1: GR[Int], e2: GR[Double]): GR[CoffeesRow] = GR{
prs => import prs._
CoffeesRow.tupled((<<[String], <<[Int], <<[Double], <<[Int], <<[Int]))
}
/** Table description of table COFFEES. Objects of this class serve as prototypes for rows in queries. */
class Coffees(_tableTag: Tag) extends Table[CoffeesRow](_tableTag, "COFFEES") {
def * = (name, supId, price, sales, total) <> (CoffeesRow.tupled, CoffeesRow.unapply)
/** Maps whole row to an option. Useful for outer joins. */
def ? = (name.?, supId.?, price.?, sales.?, total.?).shaped.<>({r=>import r._; _1.map(_=>
CoffeesRow.tupled((_1.get, _2.get, _3.get, _4.get, _5.get)))}, (_:Any) =>
throw new Exception("Inserting into ? projection not supported."))
...
}
...
}
This results in the following compilation errors:
Error:(28, 5) missing arguments for method CoffeesRow in trait Tables;
follow this method with `_' if you want to treat it as a partially applied function
CoffeesRow.tupled((<<[String], <<[Int], <<[Double], <<[Int], <<[Int]))
Error:(32, 52) missing arguments for method CoffeesRow in trait Tables;
follow this method with `_' if you want to treat it as a partially applied function
def * = (name, supId, price, sales, total) <> (CoffeesRow.tupled, CoffeesRow.unapply)
Error:(32, 71) missing arguments for method CoffeesRow in trait Tables;
follow this method with `_' if you want to treat it as a partially applied function
def * = (name, supId, price, sales, total) <> (CoffeesRow.tupled, CoffeesRow.unapply)
Error:(34, 95) missing arguments for method CoffeesRow in trait Tables;
follow this method with `_' if you want to treat it as a partially applied function
def ? = (name.?, supId.?, price.?, sales.?, total.?).shaped.<>({r=>import r._; _1.map(_=> CoffeesRow.tupled((_1.get, _2.get, _3.get, _4.get, _5.get)))}, (_:Any) => throw new Exception("Inserting into ? projection not supported."))
I've been advised on the forum to raise this as a bug against CodeGenerator, and that I could disable the PlainSqlMapper as follows:
case class SourceGen(model: m.Model) extends SourceCodeGenerator(model) {
override def Table = new Table(_) {
override lazy val EntityType = new EntityType {
override val classEnabled: Boolean = false
}
override lazy val PlainSqlMapper = new PlainSqlMapper{
override val enabled = false
}
}
}
Disabling the PlainSqlMapper as above removes the first compilation error, but not those connected with the incorrect definitions of the * and ? methods, so it still won't compile.
I know I can fix this code by hand, but obviously codegen should produce the correctly compiling code!
(As an aside, I also wonder why the default behaviour of the code generator is to create Tables.scala with case classes; will this change in future to generate the tuples only version, so that the existing way of populating the database works with the schema classes generated by codegen?)
The text was updated successfully, but these errors were encountered:
I'm playing with slick 2 and codegen, using MySql for the database. I started with the Coffee / Suppliers example from the hello slick activator template.
I'm trying to use codegen to create a new Tables.scala file from the Coffee and Suppliers database tables, which will allow the existing client code to work e.g.
I've been posting to the slick google group for help. I've had to use a custom generator to use tuples for the rows instead of case classes:
This generates the following code for Tables.scala (relevant parts shown only):
This results in the following compilation errors:
I've been advised on the forum to raise this as a bug against CodeGenerator, and that I could disable the PlainSqlMapper as follows:
Disabling the PlainSqlMapper as above removes the first compilation error, but not those connected with the incorrect definitions of the * and ? methods, so it still won't compile.
I know I can fix this code by hand, but obviously codegen should produce the correctly compiling code!
(As an aside, I also wonder why the default behaviour of the code generator is to create Tables.scala with case classes; will this change in future to generate the tuples only version, so that the existing way of populating the database works with the schema classes generated by codegen?)
The text was updated successfully, but these errors were encountered: