No description, website, or topics provided.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.gitignore
COPYING
README.md
build.sbt

README.md

Database Access Object (DAO) generator

Punychar DAO Generator is a minimalistic tool for generating Database Access Object (DAO) Scala source code, on a per-database-table and per-database-view basis, following the builder pattern.

scalikejdbc

Currently, scalikejdbc is the only source code DAO schema variant. It is generating scalikejdbc compatible DAO source code. Please refer to http://scalikejdbc.org/ for more details

PostgreSQL

Currently PostgreSQL is the default target database, but this can changed by configuration.

Other DBMS

To adopt for an different DBMS, create a custom implementation for DbConfig by subclassing it. Then, instead of using the default one, set the factory dbConfig(...) to refer to the custom DbConig create function (factory function).

    DAO_Generator
      .create
      .dbConfig(()=>new MyPersonal_DbConfig {
             ...
      })

Credentials

For adding connect credentials you can use this function:

    DAO_Generator
      .create
      .dbConfig(db => {
         db.db = "dao_test_database";
         db.user = "testuser";
         db.password = "dummy"
      })

DAO class naming convention

If your database table is called my_table your corresponding scala class is called My_table_dao.

As You can see, for the scala class name, the first letter is capitalized and _dao is prepended.

Selection of tables and views

There is a include / exclude logic to have the wished tables and views selected. If you don't specify any of those filters, all tables and all views are fetched and run through the code factory.

Example configuration

package punychar.dao_generator_demo

import punychar.dao_generator.DAO_Generator

object DAO_Generator_Demo {

   def main(args: Array[String]): Unit = {

      DAO_Generator
        .create
        .dbConfig(db => {
           db.db = "dao_test_database";
           db.user = "testuser";
           db.password = "dummy"
        })
        .tablesExclude("table_x100", "table_x200")
        .tablesInclude("test_table_01")
        .outputFolder("src\\main\\scala\\punychar\\dao_generator_demo\\dao_generated")
        .scalaPackageName("punychar.dao_generator_demo.dao_generated")
        .listTables
        .dao_generate

   }
}

Types Transformation Logic

Currently, the transformation of database and Scala-JDBC types is the following

      val transformation = this.dbtype match {
         case "bool"  TypeTransformation("Boolean", s"""rs.boolean("${this.columnName}")""")
         case "daterange"  TypeTransformation("postgresql.DateRange", s"""postgresql.DateRange.parse(rs.string("${this.columnName}"))""")
         case "date"  TypeTransformation("java.util.Date", s"""rs.date("${this.columnName}")""")
         case "bigint" | "numeric"  TypeTransformation("BigDecimal", s"""rs.bigDecimal("${this.columnName}")""")
         case "int4" | "integer"  TypeTransformation("Int", s"""rs.int("${this.columnName}")""")
         case "int8"  TypeTransformation("Long", s"""rs.long("${this.columnName}")""")
         case "varchar" | "text"  TypeTransformation("String", s"""rs.string("${this.columnName}")""")
         case "timestamp"  TypeTransformation("java.sql.Timestamp", s"""rs.timestamp("${this.columnName}")""")
         case _  throw new Exception(s"Transformation mapping missing for «${this.tableName}».«${this.columnName}» type «${this.dbtype}» ")
      }

Example Database

SQL DDL (PostgreSQL)

create table test_table_01
(
	field_varchar255 varchar(255),
	field_integer integer,
	field_daterange daterange,
	field_bool boolean,
	field_date date,
	field_bigint bigint,
	field_numeric numeric,
	field_varchar varchar,
	field_text text,
	field_timestamp timestamp,
	field_numeric_15_2 numeric(15,2),
	field_int4 integer,
	field_int8 bigint
);

alter table dao_test_table_01 owner to testuser;

Example DAO class

// This file has has been generated using Punychar DAO Generator
// Don't edit this file - since changes to it will be overwritten!
// https://github.com/hartmut27/dao_generator


package punychar.dao_generator_demo.dao_generated

import punychar.dao_generator._
import scalikejdbc._

case class Test_table_01_dao(field_varchar255: String, 
       field_integer: Int, 
       field_daterange: postgresql.DateRange, 
       field_bool: Boolean, 
       field_date: java.util.Date, 
       field_bigint: Long, 
       field_numeric: BigDecimal, 
       field_varchar: String, 
       field_text: String, 
       field_timestamp: java.sql.Timestamp, 
       field_numeric_15_2: BigDecimal, 
       field_int4: Int, 
       field_int8: Long)

object Test_table_01_dao extends SQLSyntaxSupport[Test_table_01_dao] {

   override val tableName = "test_table_01"

   def apply(e: ResultName[Test_table_01_dao])(rs:WrappedResultSet):Test_table_01_dao = new Test_table_01_dao(
       field_varchar255 = rs.string("field_varchar255"), 
       field_integer = rs.int("field_integer"), 
       field_daterange = postgresql.DateRange.parse(rs.string("field_daterange")), 
       field_bool = rs.boolean("field_bool"), 
       field_date = rs.date("field_date"), 
       field_bigint = rs.long("field_bigint"), 
       field_numeric = rs.bigDecimal("field_numeric"), 
       field_varchar = rs.string("field_varchar"), 
       field_text = rs.string("field_text"), 
       field_timestamp = rs.timestamp("field_timestamp"), 
       field_numeric_15_2 = rs.bigDecimal("field_numeric_15_2"), 
       field_int4 = rs.int("field_int4"), 
       field_int8 = rs.long("field_int8")
   )
}