Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to setup username and password with Slick's source code generator?

Tags:

mysql

slick

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?

like image 648
George Pligoropoulos Avatar asked Jan 25 '14 11:01

George Pligoropoulos


1 Answers

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

like image 82
George Pligoropoulos Avatar answered Oct 07 '22 08:10

George Pligoropoulos