diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2013-04-09 10:03:32 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2013-04-10 18:46:45 +0200 |
commit | 5ebd62ec778bc8c9540e2379e764ac52c060adfc (patch) | |
tree | b751e62fe12599ee1a2553dc9331a03c8c957252 /odb/relational/schema-source.cxx | |
parent | 8295d4bd02946b7bdbff2a9cfa7763232e2d2fd0 (diff) |
Generate embedded schema in single function instead of one per object
Diffstat (limited to 'odb/relational/schema-source.cxx')
-rw-r--r-- | odb/relational/schema-source.cxx | 114 |
1 files changed, 97 insertions, 17 deletions
diff --git a/odb/relational/schema-source.cxx b/odb/relational/schema-source.cxx index fcd1c68..e20529d 100644 --- a/odb/relational/schema-source.cxx +++ b/odb/relational/schema-source.cxx @@ -9,36 +9,116 @@ using namespace std; namespace relational { - namespace schema_source + namespace schema { void - generate () + generate_source () { context ctx; ostream& os (ctx.os); + database db (ctx.db); + options const& ops (ctx.options); + sema_rel::model& model (*ctx.model); + string const& schema_name (ops.schema_name ()[db]); - traversal::unit unit; - traversal::defines unit_defines; - traversal::namespace_ ns; - instance<class_> c; + instance<cxx_emitter> emitter; + emitter_ostream emitter_os (*emitter); + schema_format format (schema_format::embedded); - unit >> unit_defines >> ns; - unit_defines >> c; + if (!model.names_empty ()) + { + os << "namespace odb" + << "{" + << "static bool" << endl + << "create_schema (database& db, unsigned short pass, bool drop)" + << "{" + << "ODB_POTENTIALLY_UNUSED (db);" + << "ODB_POTENTIALLY_UNUSED (pass);" + << "ODB_POTENTIALLY_UNUSED (drop);" + << endl; - traversal::defines ns_defines; + // Drop. + // + if (!ops.omit_drop ()) + { + bool close (false); - ns >> ns_defines >> ns; - ns_defines >> c; + os << "if (drop)" + << "{"; - instance<include> i; - i->generate (); + instance<drop_model> dmodel (*emitter, emitter_os, format); + instance<drop_table> dtable (*emitter, emitter_os, format); + trav_rel::qnames names; + dmodel >> names >> dtable; - os << "namespace odb" - << "{"; + for (unsigned short pass (1); pass < 3; ++pass) + { + emitter->pass (pass); + dmodel->pass (pass); + dtable->pass (pass); - unit.dispatch (ctx.unit); + dmodel->traverse (model); - os << "}"; + close = close || !emitter->empty (); + } + + if (close) // Close the last case and the switch block. + os << "return false;" + << "}" // case + << "}"; // switch + + os << "}"; + } + + // Create. + // + if (!ops.omit_create ()) + { + bool close (false); + + if (ops.omit_drop ()) + os << "if (!drop)"; + else + os << "else"; + + os << "{"; + + instance<create_model> cmodel (*emitter, emitter_os, format); + instance<create_table> ctable (*emitter, emitter_os, format); + trav_rel::qnames names; + cmodel >> names >> ctable; + + for (unsigned short pass (1); pass < 3; ++pass) + { + emitter->pass (pass); + cmodel->pass (pass); + ctable->pass (pass); + + cmodel->traverse (model); + + close = close || !emitter->empty (); + } + + if (close) // Close the last case and the switch block. + os << "return false;" + << "}" // case + << "}"; // switch + + os << "}"; + } + + os << "return false;" + << "}"; + + os << "static const schema_catalog_entry" << endl + << "create_schema_entry_ (" << endl + << "id_" << db << "," << endl + << context::strlit (schema_name) << "," << endl + << "&create_schema);" + << endl; + + os << "}"; + } } } } |