Version Matrix

sbt-dao-generator

Gitter

Build Status Maven Central Scaladoc Reference Status

sbt-dao-generator is the code generator plug-in for O/R Mapper Free. Even if you use any O/R mapper framework, DAOs are automatically generated by this plug-in functions.

How to use plugin

Add this to your project/plugins.sbt file:

  • if you use release version:
resolvers += "Sonatype OSS Release Repository" at "https://oss.sonatype.org/content/repositories/releases/"

addSbtPlugin("jp.co.septeni-original" % "sbt-dao-generator" % "1.0.8")
  • if you use snapshot version:
resolvers += "Sonatype OSS Snapshot Repository" at "https://oss.sonatype.org/content/repositories/snapshots/"

addSbtPlugin("jp.co.septeni-original" % "sbt-dao-generator" % "1.0.9-SNAPSHOT")

How to configuration

Add this to your build.sbt file:

// JDBC Driver Class Name (required)
driverClassName in generator := "org.h2.Driver"

// JDBC URL (required)
jdbcUrl in generator := "jdbc:h2:file:./target/test"

// JDBC User Name (required)
jdbcUser in generator := "sa"

// JDBC Password (required)
jdbcPassword in generator := ""

// The Function that convert The Column Type Name to Property Type Name (required)
propertyTypeNameMapper in generator := {
  case "INTEGER" => "Int"
  case "VARCHAR" => "String"
  case "BOOLEAN" => "Boolean"
  case "DATE" | "TIMESTAMP" => "java.util.Date"
  case "DECIMAL" => "BigDecimal"
}

// Schema Name (Optional, Default is None)
schemaName in generator := None,

// The Function for filtering the table to be processed (Optional, default is the following)
tableNameFilter in generator := { tableName: String => tableName.toUpperCase != "SCHEMA_VERSION"}

// The Function for converting Table Name to Class Name (Optional, default is the following)
classNameMapper in generator := { tableName: String =>
    Seq(StringUtil.camelize(tableName))
}

// e.g.) If you want to specify multiple output files, you can configure it as follows.
/*
classNameMapper in generator := {
  case "DEPT" => Seq("Dept", "DeptSpec")
  case "EMP" => Seq("Emp", "EmpSpec")
}
*/

// The Function for converting Column Name to Property Name (Optional, default is the following)
propertyNameMapper in generator := { columnName: String =>
    StringUtil.decapitalize(StringUtil.camelize(columnName))
}

// The Function that decides which Template Name for Model Name (Optional, defaults below)
templateNameMapper in generator := { className: String => "template.ftl" },

// e.g.) If you want to specify different templates for the model and spec, you can configure it as follows.
/*
templateNameMapper in generator := {
  case className if className.endsWith("Spec") => "template_spec.ftl"
  case _ => "template.ftl"
}
*/

// The Directory where template files are placed (Optional, default is the following)
templateDirectory in generator := baseDirectory.value / "templates"

// The Directory where source code is output (Optional, default is the following)
outputDirectoryMapper in generator := { className: String => (sourceManaged in Compile).value }

// e.g.) You can change the output destination directory for each class name dynamically.
/*
outputDirectoryMapper in generator := { className: String =>
  className match {
    case s if s.endsWith("Spec") => (sourceManaged in Test).value
    case s => (sourceManaged in Compile).value
  }
}
*/

How to configure a model template

The supported template syntax is FTL(FreeMarker Template Language).Please refer to the official FreeMarker document for details.

templates/temlate.ftl

case class ${className}(
<#list allColumns as column>
<#if column.nullable>
${column.propertyName}: Option[${column.propertyTypeName}]<#if column_has_next>,</#if>
<#else>
${column.propertyName}: ${column.propertyTypeName}<#if column_has_next>,</#if>
</#if>
</#list>
) {

}

You can use the following template contexts.

Top level objects

Variable name Type Description
tableName String Table Name (USER_NAME)
className String Class Name (UserName). The string converted tableName by classNameMapper
decapitalizedClassName String Decapitalized Class Name (userName)
primaryKeys java.util.List Primary Keys
columns java.util.List Columns without Primary Keys
allColumns java.util.List Columns with Primary Keys

Column objects

Variable name Type Description
columnName String Column Name (FIRST_NAME)
columnTypeName String Column Type Name (VARCHAR, DATETIME, ...)
propertyName String Property Name (firstName). The string converted columnName by propertyNameMapper
propertyTypeName String Property Type Name (String, java.util.Date,...). The string converted columnTypeName by propertyTypeNameMapper
capitalizedPropertyName String Capitalized Property Name (FirstName)
nullable Boolean NULL is true

Code generation

  • When processing all tables
$ sbt generator::generateAll
<snip>
[info] tableName = DEPT, generate file = /Users/sbt-user/myproject/target/scala-2.10/src_managed/Dept.scala
[info] tableName = EMP, generate file = /Users/sbt-user/myproject/target/scala-2.10/src_managed/Emp.scala
[success] Total time: 0 s, completed 2015/06/24 18:17:20
  • When processing multiple tables
$ sbt generator::generateMany DEPT EMP
<snip>
[info] tableNames = EMP, DEPT
[info] tableName = DEPT, generate file = /Users/sbt-user/myproject/target/scala-2.10/src_managed/Dept.scala
[info] tableName = EMP, generate file = /Users/sbt-user/myproject/target/scala-2.10/src_managed/Emp.scala
[success] Total time: 0 s, completed 2015/06/24 18:17:20
  • When processing one table
$ sbt generator::generateOne DEPT
<snip>
[info] tableName = DEPT
[info] tableName = DEPT, generate file = /Users/sbt-user/myproject/target/scala-2.10/src_managed/Dept.scala
[success] Total time: 0 s, completed 2015/06/24 18:17:20

If you want to run generator::generateAll atsbt compile, add the following to build.sbt:

// sbt 0.12.x
sourceGenerators in Compile <+= generateAll in generator

// sbt 0.13.x
sourceGenerators in Compile += (generateAll in generator).value

How to migration from v1.0.4 to v1.0.8

The following parameters have been changed. Please change your project accordingly.

  • projectSettings
Modify type Old name New name
Modified typeNameMapper propertyTypeNameMapper
  • Top level objects
Modify type Old name New name
Modified name className
Added - tableName
Modified primaryKeysWithColumns allColumns
  • Column objects
Modify type Old name New name
Modified columnType columnTypeName
Modified propertyType propertyTypeName