Skip to content

Instantly share code, notes, and snippets.

@Ciantic
Last active September 11, 2017 16:57
Show Gist options
  • Save Ciantic/dcbbb087e9bfe289bc6e to your computer and use it in GitHub Desktop.
Save Ciantic/dcbbb087e9bfe289bc6e to your computer and use it in GitHub Desktop.
injection with DB.getConnection fails
import javax.inject.{Singleton, Inject}
import com.google.inject.AbstractModule
import play.api.db.{DBApi}
import play.api.{Logger, Configuration}
import org.jooq.util.GenerationTool
import org.jooq.util.jaxb.{Target, Database, Generator}
import play.api.db.evolutions.EvolutionsModule
import play.api.Play.current
trait JooqCreator {
def create(): Unit
}
class DefaultJooqCreator @Inject() (val playConfiguration: Configuration, dbApi: DBApi) extends JooqCreator {
create()
override def create(): Unit = {
dbApi.databases().foreach {
db => {
Logger.info("Generate JOOQ classes for: " + db.name)
executeGeneration(db.name)
}
}
}
def executeGeneration(db: String) = {
val generationTool = new GenerationTool()
val connection = dbApi.database(db).getConnection()
val configuration = new org.jooq.util.jaxb.Configuration()
val generator = new Generator()
val database = new Database()
val target = new Target()
val jooqConfig = playConfiguration.getConfig("jooq." + db).get
generationTool.setConnection(connection)
val databaseName = jooqConfig.getString("database.name").getOrElse({
Logger.error("No database.name property for $db")
""
})
val databaseSchema = jooqConfig.getString("database.schema").getOrElse({
Logger.error("No database.schema property for $db")
""
})
database.setName(databaseName)
database.setInputSchema(databaseSchema)
database.setIncludes(jooqConfig.getString("database.includes").getOrElse(".*"))
database.setExcludes(jooqConfig.getString("database.excludes").getOrElse(""))
generator.setDatabase(database)
generator.setName(jooqConfig.getString("generator.name").getOrElse("org.jooq.util.JavaGenerator"))
target.setDirectory(jooqConfig.getString("target.directory").getOrElse("./app"))
target.setPackageName(jooqConfig.getString("target.package").getOrElse("org.jooq.generated"))
generator.setTarget(target)
configuration.setGenerator(generator)
generationTool.run(configuration)
connection.close()
}
}
@Singleton
class JooqModule extends AbstractModule {
override def configure(): Unit = {
bind(classOf[JooqCreator])
.to(classOf[DefaultJooqCreator]).asEagerSingleton
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment