8

Following the directions in this page: http://slick.typesafe.com/doc/2.0.0/code-generation.html
we see that something like the following segment of code is required to generate models for mysql tables

val url = "jdbc:mysql://127.0.0.1/SOME_DB_SCHEMA?characterEncoding=UTF-8&useUnicode=true"

val slickDriver = "scala.slick.driver.MySQLDriver"

  val jdbcDriver = "com.mysql.jdbc.Driver"

  val outputFolder = "/some/path"

  val pkg = "com.pligor.server"

  scala.slick.model.codegen.SourceCodeGenerator.main(
    Array(slickDriver, jdbcDriver, url, outputFolder, pkg)
  )

These parameteres are enough for an H2 database as the example in the link has it.

How to include username and password for the MySQL database?

George Pligoropoulos
  • 2,919
  • 3
  • 33
  • 65

4 Answers4

5

From several links found in the internet and also based on the cvogt's answer this is the minimum that you need to do.

Note that this is a general solution for sbt. If you are dealing with play framework you might find it easier to perform this task with the relevant plugin

First of all you need a new sbt project because of all the library dependencies that are needed to be referenced in order for slick source generator to run.
Create the new sbt project using this tutorial: http://scalatutorials.com/beginner/2013/07/18/getting-started-with-sbt/
Preferably use the method Setup using giter8

If it happens to work with Intellij then you need to create file project/plugins.sbt and insert inside this line: addSbtPlugin("com.hanhuy.sbt" % "sbt-idea" % "1.6.0").
Execute gen-idea in sbt to generate an intellij project.

With giter8 you get an auto-generated file ProjectNameBuild.scala inside project folder. Open this and include at least these library dependencies:

libraryDependencies ++= List(
    "mysql" % "mysql-connector-java" % "5.1.27",
    "com.typesafe.slick" %% "slick" % "2.0.0",
    "org.slf4j" % "slf4j-nop" % "1.6.4",
    "org.scala-lang" % "scala-reflect" % scala_version
  )

where scala version is the variable private val scala_version = "2.10.3"

Now create the custom source code generator that looks like that:

import scala.slick.model.codegen.SourceCodeGenerator

object CustomSourceCodeGenerator {

import scala.slick.driver.JdbcProfile

import scala.reflect.runtime.currentMirror

def execute(url: String,
          jdbcDriver: String,
          user: String,
          password: String,
          slickDriver: String,
          outputFolder: String,
          pkg: String) = {
val driver: JdbcProfile = currentMirror.reflectModule(
  currentMirror.staticModule(slickDriver)
).instance.asInstanceOf[JdbcProfile]

driver.simple.Database.forURL(
  url,
  driver = jdbcDriver,
  user = user,
  password = password
).withSession {
  implicit session =>
    new SourceCodeGenerator(driver.createModel).writeToFile(slickDriver, outputFolder, pkg)
    }
  }
}

Finally you need to call this execute method inside main project object. Find the file ProjectName.scala that was auto-generated by giter8.
Inside it you will find a println call since this is merely a "hello world" application. Above println call something like that:

CustomSourceCodeGenerator.execute(
url = "jdbc:mysql://127.0.0.1/SOME_DB_SCHEMA?characterEncoding=UTF-8&useUnicode=true",
slickDriver = "scala.slick.driver.MySQLDriver",
jdbcDriver = "com.mysql.jdbc.Driver",
outputFolder = "/some/path",
pkg = "com.pligor.server",
user = "root",
password = "xxxxxyourpasswordxxxxx"
)

This way every time you execute sbt run you are going to generate the Table classes required by Slick automatically

George Pligoropoulos
  • 2,919
  • 3
  • 33
  • 65
2

Note that at least for 2.0.1 this is fixed. Just add username and password to the end of the Array as Strings

Tim Pigden
  • 895
  • 2
  • 9
  • 18
0

This has been asked and answered here: https://groups.google.com/forum/#!msg/scalaquery/UcS4_wyrJq0/obLHheIWIXEJ . Currently you need to customize the code generator. A PR for 2.0.1 is in the queue.

cvogt
  • 11,260
  • 30
  • 46
0

My solution is nearly the same as George's answer, but I'll add mine anyway. This is the entire file I use to generate code for my mysql database in an SBT project.

SlickAutoGen.scala

package mypackage

import slick.model.codegen.SourceCodeGenerator

object CodeGen {

  def main(args: Array[String]) {
    SourceCodeGenerator.main(
      Array(
        "scala.slick.driver.MySQLDriver",
        "com.mysql.jdbc.Driver",
        "jdbc:mysql://localhost:3306/mydb",
        "src/main/scala/",
        "mypackage",
        "root",
        "" // I don't use a password on localhost
      )
    )
  }
}

build.sbt

// build.sbt --- Scala build tool settings

libraryDependencies ++= List(
  "com.typesafe.slick" %% "slick" % "2.0.1",
  "mysql" % "mysql-connector-java" % "5.1.24",
  ...
)

To use this, just modify settings, save in project root directory and run as follows:

$ sbt
> runMain mypackage.CodeGen
doub1ejack
  • 10,627
  • 20
  • 66
  • 125