// file      : odb/relational/schema.cxx
// copyright : Copyright (c) 2009-2012 Code Synthesis Tools CC
// license   : GNU GPL v3; see accompanying LICENSE file

#include <cassert>
#include <limits>
#include <sstream>

#include <odb/emitter.hxx>

#include <odb/relational/schema.hxx>
#include <odb/relational/generate.hxx>

using namespace std;

namespace relational
{
  namespace schema
  {
    static char const file_header[] =
    "/* This file was generated by ODB, object-relational mapping (ORM)\n"
    " * compiler for C++.\n"
    " */\n\n";

    void
    generate ()
    {
      context ctx;
      ostream& os (ctx.os);

      os << file_header;

      instance<schema_file> file;
      file->prologue ();

      instance<schema_emitter> em;
      emitter_ostream emos (*em);

      schema_format f (schema_format::sql);

      // Drop.
      //
      {

        instance<drop_model> model (*em, emos, f);
        trav_rel::qnames names;
        instance<drop_table> table (*em, emos, f);
        instance<drop_index> index (*em, emos, f);

        model >> names;
        names >> table;
        names >> index;

        // Pass 1 and 2.
        //
        for (unsigned short pass (1); pass < 3; ++pass)
        {
          model->pass (pass);
          table->pass (pass);
          index->pass (pass);

          model->traverse (*ctx.model);
        }
      }

      os << endl;

      // Create.
      //
      {
        instance<create_model> model (*em, emos, f);
        trav_rel::qnames names;
        instance<create_table> table (*em, emos, f);
        instance<create_index> index (*em, emos, f);

        model >> names;
        names >> table;
        names >> index;

        // Pass 1 and 2.
        //
        for (unsigned short pass (1); pass < 3; ++pass)
        {
          model->pass (pass);
          table->pass (pass);
          index->pass (pass);

          model->traverse (*ctx.model);
        }
      }

      file->epilogue ();
    }
  }
}
