From 08a47c70ad517b80b72914d47d547463f576bcd3 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 24 Oct 2011 16:32:51 +0200 Subject: Generate database schema from database model instead of C++ model We now first create the so-called database model from C++ model and then use that to generate the database schema. The new approach also adds more general support for primary/foreign keys, including multi- column keys. Finally, for MySQL we now generate out-of-line foreign key definitions. Because MySQL does not support deferred constraints checking, deferred foreign keys are written commented out, for documentation. --- odb/generator.cxx | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) (limited to 'odb/generator.cxx') diff --git a/odb/generator.cxx b/odb/generator.cxx index aa56644..6c1fda4 100644 --- a/odb/generator.cxx +++ b/odb/generator.cxx @@ -93,6 +93,32 @@ generate (options const& ops, semantics::unit& unit, path const& p) { try { + // First create the database model. + // + cutl::shared_ptr model; + + if (ops.generate_schema ()) + { + auto_ptr ctx (create_context (cerr, unit, ops, 0)); + + switch (ops.database ()) + { + case database::mysql: + case database::oracle: + case database::pgsql: + case database::sqlite: + { + model = relational::model::generate (); + break; + } + case database::tracer: + { + cerr << "error: the tracer database does not have schema" << endl; + throw failed (); + } + } + } + // Output files. // path file (p.leaf ()); @@ -196,7 +222,7 @@ generate (options const& ops, semantics::unit& unit, path const& p) // { cxx_filter filt (hxx); - auto_ptr ctx (create_context (hxx, unit, ops)); + auto_ptr ctx (create_context (hxx, unit, ops, model.get ())); string guard (make_guard (gp + hxx_name, *ctx)); @@ -270,7 +296,7 @@ generate (options const& ops, semantics::unit& unit, path const& p) // { cxx_filter filt (ixx); - auto_ptr ctx (create_context (ixx, unit, ops)); + auto_ptr ctx (create_context (ixx, unit, ops, model.get ())); // Copy prologue. // @@ -313,7 +339,7 @@ generate (options const& ops, semantics::unit& unit, path const& p) // { cxx_filter filt (cxx); - auto_ptr ctx (create_context (cxx, unit, ops)); + auto_ptr ctx (create_context (cxx, unit, ops, model.get ())); cxx << "#include " << endl << endl; @@ -363,7 +389,7 @@ generate (options const& ops, semantics::unit& unit, path const& p) // if (sql_schema) { - auto_ptr ctx (create_context (sql, unit, ops)); + auto_ptr ctx (create_context (sql, unit, ops, model.get ())); // Copy prologue. // -- cgit v1.1