I want to exclude View from Slick's Code-generation

Asked 1 years ago, Updated 1 years ago, 52 views

You are attempting to generate code from a database that does not have VIEW modification privileges.
The document told me to override the API, but I didn't know how to do it.

US>Library version used
scala 2.11.6
click 3.0.3
click-codegen 3.0.3
mysql-connector-java 5.1.36

Documentation
http://slick.typesafe.com/doc/3.0.3/code-generation.html#customization

Example documented https://github.com/slick/slick-codegen-customization-example

Here's the source code.

def custom={
  valslickDriver="slick.driver.MySQLDriver"
  val jdbcDriver="org.gjt.mm.mysql.Driver"
  valurl="jdbc:mysql://localhost/test?characterEncoding=UTF-8&characterSetResults=UTF-8&zeroDateTimeBehavior=convertToNull"
  val outputFolder="src/main/scala"
  valpkg = "gen"
  val user="userName"
  val pass = "password"

  val driver —JdbcProfile=
    Class.forName(slickDriver+"$").getField("MODULE$").get(null).asInstanceOf[JdbcProfile]
  valuebFactory=driver.api.Database
  valdb=dbFactory.forURL(url,driver=jdbcDriver,
    user=Some(user).getOrElse(null), password=Some(pass).getOrElse(null), keepAliveConnection=true)

  try{
    valm=Await.result(db.run(driver.createModel(None, false) (ExecutionContext.global).withPinnedSession), Duration.Inf)
    new SourceCodeGenerator(m).writeToFile(slickDriver, outputFolder, pkg)
  } finally db.close
}

Thank you for your cooperation.

scala

2022-09-30 10:19

1 Answers

Read the source and resolve it.

The MTable.getTables argument specifies the conditions under which you want to filter the table.

For types, the value defined in the name of DatabaseMetaData.TableType is available.

def custom={
  valslickDriver="slick.driver.MySQLDriver"
  val jdbcDriver="org.gjt.mm.mysql.Driver"
  valurl="jdbc:mysql://localhost/test?characterEncoding=UTF-8&characterSetResults=UTF-8&zeroDateTimeBehavior=convertToNull"
  val outputFolder="src/main/scala"
  valpkg = "gen"
  val user="userName"
  val pass = "password"

  val driver —JdbcProfile=
    Class.forName(slickDriver+"$").getField("MODULE$").get(null).asInstanceOf[JdbcProfile]
  valuebFactory=driver.api.Database
  valdb=dbFactory.forURL(url,driver=jdbcDriver,
    user=Some(user).getOrElse(null), password=Some(pass).getOrElse(null), keepAliveConnection=true)

  values=Some(MTable.getTables(Some(""),Some(""),None,Some(Seq("TABLE"))//You can filter here.
  valmodelAction=driver.createModel(tables, false) (ExecutionContext.global).withPinedSession
  val modelFuture=db.run(modelAction)
  try{
    valm=Await.result(modelFuture,Duration.Inf)
    new SourceCodeGenerator(m).writeToFile(slickDriver, outputFolder, pkg)
  } finally db.close
}


2022-09-30 10:19

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.