logo

abstract class

haven::BuildDb

sys::Obj
  build::BuildScript
    haven::BuildDb
//
// Copyright (c) 2008, John Sublett
// Licensed under the Academic Free License version 3.0
//
// History:
//   13 Jan 08  John Sublett  Creation
//

using compiler
using build
using sql

**
** BuildDb is a script for building a relational
** database schema from a list of types.
**
abstract class BuildDb : BuildScript
{
  **
  ** The database to build the schema for.
  **
  abstract HavenService db
  
  **
  ** The list of types to include in the database.
  **
  abstract Type[] types

  **
  ** The default target is 'ddl' which just generates
  ** the DDL required to build the database.
  **  
  override Target defaultTarget()
  {
    return target("ddl")
  }
  
  protected DbModel model()
  {
    model := DbModel.make
    types.each |Type t| { model.addType(t) }
    model.commit
    return model
  }
  
  @target="generate DDL for a new database"
  Void ddl()
  {
    m := model
    ddl := m.ddl(db)
    ddl.each |Str stmt| { echo("$stmt\n") }
  }

  @target="generate DDL and create schema in database"
  Void create()
  {
    m := model
    ddl := m.ddl(db)
    db.open
    ddl.each |Str stmt| { echo("$stmt\n"); db.sql(stmt).execute }
    db.close
  }
}