diff options
Diffstat (limited to 'odb/generator.cxx')
-rw-r--r-- | odb/generator.cxx | 354 |
1 files changed, 241 insertions, 113 deletions
diff --git a/odb/generator.cxx b/odb/generator.cxx index 5500860..ec98a15 100644 --- a/odb/generator.cxx +++ b/odb/generator.cxx @@ -25,6 +25,7 @@ using namespace std; using namespace cutl; using semantics::path; +typedef vector<string> strings; typedef vector<path> paths; namespace @@ -52,20 +53,21 @@ namespace } void - append (ostream& os, vector<string> const& text, string const& file) + append (ostream& os, strings const& text) { - for (vector<string>::const_iterator i (text.begin ()); + for (strings::const_iterator i (text.begin ()); i != text.end (); ++i) { os << *i << endl; } + } - if (!file.empty ()) - { - ifstream ifs; - open (ifs, file); - os << ifs.rdbuf (); - } + void + append (ostream& os, string const& file) + { + ifstream ifs; + open (ifs, file); + os << ifs.rdbuf (); } } @@ -78,6 +80,8 @@ generate (options const& ops, { try { + database db (ops.database ()[0]); + // First create the database model. // cutl::shared_ptr<semantics::relational::model> model; @@ -86,8 +90,12 @@ generate (options const& ops, { auto_ptr<context> ctx (create_context (cerr, unit, ops, fts, 0)); - switch (ops.database ()) + switch (db) { + case database::common: + { + break; // No schema for common. + } case database::mssql: case database::mysql: case database::oracle: @@ -109,11 +117,11 @@ generate (options const& ops, fs::auto_removes auto_rm; - string hxx_name (base + ops.odb_file_suffix () + ops.hxx_suffix ()); - string ixx_name (base + ops.odb_file_suffix () + ops.ixx_suffix ()); - string cxx_name (base + ops.odb_file_suffix () + ops.cxx_suffix ()); - string sch_name (base + ops.schema_file_suffix () + ops.cxx_suffix ()); - string sql_name (base + ops.sql_suffix ()); + string hxx_name (base + ops.odb_file_suffix ()[db] + ops.hxx_suffix ()); + string ixx_name (base + ops.odb_file_suffix ()[db] + ops.ixx_suffix ()); + string cxx_name (base + ops.odb_file_suffix ()[db] + ops.cxx_suffix ()); + string sch_name (base + ops.schema_file_suffix ()[db] + ops.cxx_suffix ()); + string sql_name (base + ops.sql_file_suffix ()[db] + ops.sql_suffix ()); path hxx_path (hxx_name); path ixx_path (ixx_name); @@ -173,7 +181,7 @@ generate (options const& ops, // ofstream cxx; - if (gen_cxx) + if (gen_cxx && db != database::common) { cxx.open (cxx_path.string ().c_str (), ios_base::out); @@ -190,7 +198,8 @@ generate (options const& ops, // // bool gen_sql_schema (ops.generate_schema () && - ops.schema_format ().count (schema_format::sql)); + ops.schema_format ()[db].count (schema_format::sql) && + db != database::common); ofstream sql; if (gen_sql_schema) @@ -209,9 +218,11 @@ generate (options const& ops, // // - bool gen_sep_schema (gen_cxx && - ops.generate_schema () && - ops.schema_format ().count (schema_format::separate)); + bool gen_sep_schema ( + gen_cxx && + ops.generate_schema () && + ops.schema_format ()[db].count (schema_format::separate) && + db != database::common); ofstream sch; @@ -235,7 +246,9 @@ generate (options const& ops, { hxx << cxx_file_header; ixx << cxx_file_header; - cxx << cxx_file_header; + + if (db != database::common) + cxx << cxx_file_header; } if (gen_sep_schema) @@ -284,14 +297,22 @@ generate (options const& ops, // Copy prologue. // - if (!ops.hxx_prologue ().empty () || !ops.hxx_prologue_file ().empty ()) { - hxx << "// Begin prologue." << endl - << "//" << endl; - append (hxx, ops.hxx_prologue (), ops.hxx_prologue_file ()); - hxx << "//" << endl - << "// End prologue." << endl - << endl; + bool p (ops.hxx_prologue ().count (db) != 0); + bool pf (ops.hxx_prologue_file ().count (db) != 0); + + if (p || pf) + { + hxx << "// Begin prologue." << endl + << "//" << endl; + if (p) + append (hxx, ops.hxx_prologue ()[db]); + if (pf) + append (hxx, ops.hxx_prologue_file ()[db]); + hxx << "//" << endl + << "// End prologue." << endl + << endl; + } } // Include main file(s). @@ -299,10 +320,8 @@ generate (options const& ops, for (paths::const_iterator i (inputs.begin ()); i != inputs.end (); ++i) hxx << "#include " << ctx->process_include_path (i->leaf ().string ()) << endl; - hxx << endl; - { // We don't want to indent prologues/epilogues. // @@ -314,16 +333,31 @@ generate (options const& ops, if (!ops.at_once ()) include::generate (true); - header::generate (); - - switch (ops.database ()) + switch (db) { + case database::common: + { + header::generate (); + break; + } case database::mssql: case database::mysql: case database::oracle: case database::pgsql: case database::sqlite: { + if (ops.multi_database () == multi_database::disabled) + header::generate (); + else + { + string n (base + + ops.odb_file_suffix ()[database::common] + + ops.hxx_suffix ()); + + hxx << "#include " << ctx->process_include_path (n) << endl + << endl; + } + relational::header::generate (); break; } @@ -335,14 +369,22 @@ generate (options const& ops, // Copy epilogue. // - if (!ops.hxx_epilogue ().empty () || !ops.hxx_epilogue_file ().empty ()) { - hxx << "// Begin epilogue." << endl - << "//" << endl; - append (hxx, ops.hxx_epilogue (), ops.hxx_epilogue_file ()); - hxx << "//" << endl - << "// End epilogue." << endl - << endl; + bool e (ops.hxx_epilogue ().count (db) != 0); + bool ef (ops.hxx_epilogue_file ().count (db) != 0); + + if (e || ef) + { + hxx << "// Begin epilogue." << endl + << "//" << endl; + if (e) + append (hxx, ops.hxx_epilogue ()[db]); + if (ef) + append (hxx, ops.hxx_epilogue_file ()[db]); + hxx << "//" << endl + << "// End epilogue." << endl + << endl; + } } hxx << "#include <odb/post.hxx>" << endl @@ -367,14 +409,22 @@ generate (options const& ops, // Copy prologue. // - if (!ops.ixx_prologue ().empty () || !ops.ixx_prologue_file ().empty ()) { - ixx << "// Begin prologue." << endl - << "//" << endl; - append (ixx, ops.ixx_prologue (), ops.ixx_prologue_file ()); - ixx << "//" << endl - << "// End prologue." << endl - << endl; + bool p (ops.ixx_prologue ().count (db) != 0); + bool pf (ops.ixx_prologue_file ().count (db) != 0); + + if (p || pf) + { + ixx << "// Begin prologue." << endl + << "//" << endl; + if (p) + append (ixx, ops.ixx_prologue ()[db]); + if (pf) + append (ixx, ops.ixx_prologue_file ()[db]); + ixx << "//" << endl + << "// End prologue." << endl + << endl; + } } { @@ -382,16 +432,22 @@ generate (options const& ops, // ind_filter ind (ctx->os); - inline_::generate (); - - switch (ops.database ()) + switch (db) { + case database::common: + { + inline_::generate (); + break; + } case database::mssql: case database::mysql: case database::oracle: case database::pgsql: case database::sqlite: { + if (ops.multi_database () == multi_database::disabled) + inline_::generate (); + relational::inline_::generate (); break; } @@ -400,13 +456,22 @@ generate (options const& ops, // Copy epilogue. // - if (!ops.ixx_epilogue ().empty () || !ops.ixx_epilogue_file ().empty ()) { - ixx << "// Begin epilogue." << endl - << "//" << endl; - append (ixx, ops.ixx_epilogue (), ops.ixx_epilogue_file ()); - ixx << "//" << endl - << "// End epilogue." << endl; + bool e (ops.ixx_epilogue ().count (db) != 0); + bool ef (ops.ixx_epilogue_file ().count (db) != 0); + + if (e || ef) + { + ixx << "// Begin epilogue." << endl + << "//" << endl; + if (e) + append (ixx, ops.ixx_epilogue ()[db]); + if (ef) + append (ixx, ops.ixx_epilogue_file ()[db]); + ixx << "//" << endl + << "// End epilogue." << endl + << endl; + } } if (ops.show_sloc ()) @@ -417,7 +482,7 @@ generate (options const& ops, // CXX // - if (gen_cxx) + if (gen_cxx && db != database::common) { auto_ptr<context> ctx ( create_context (cxx, unit, ops, fts, model.get ())); @@ -429,14 +494,22 @@ generate (options const& ops, // Copy prologue. // - if (!ops.cxx_prologue ().empty () || !ops.cxx_prologue_file ().empty ()) { - cxx << "// Begin prologue." << endl - << "//" << endl; - append (cxx, ops.cxx_prologue (), ops.cxx_prologue_file ()); - cxx << "//" << endl - << "// End prologue." << endl - << endl; + bool p (ops.cxx_prologue ().count (db) != 0); + bool pf (ops.cxx_prologue_file ().count (db) != 0); + + if (p || pf) + { + cxx << "// Begin prologue." << endl + << "//" << endl; + if (p) + append (cxx, ops.cxx_prologue ()[db]); + if (pf) + append (cxx, ops.cxx_prologue_file ()[db]); + cxx << "//" << endl + << "// End prologue." << endl + << endl; + } } cxx << "#include " << ctx->process_include_path (hxx_name) << endl @@ -453,8 +526,12 @@ generate (options const& ops, if (!ops.at_once ()) include::generate (false); - switch (ops.database ()) + switch (db) { + case database::common: + { + assert (false); + } case database::mssql: case database::mysql: case database::oracle: @@ -469,14 +546,22 @@ generate (options const& ops, // Copy epilogue. // - if (!ops.cxx_epilogue ().empty () || !ops.cxx_epilogue_file ().empty ()) { - cxx << "// Begin epilogue." << endl - << "//" << endl; - append (cxx, ops.cxx_epilogue (), ops.cxx_epilogue_file ()); - cxx << "//" << endl - << "// End epilogue." << endl - << endl; + bool e (ops.cxx_epilogue ().count (db) != 0); + bool ef (ops.cxx_epilogue_file ().count (db) != 0); + + if (e || ef) + { + cxx << "// Begin epilogue." << endl + << "//" << endl; + if (e) + append (cxx, ops.cxx_epilogue ()[db]); + if (ef) + append (cxx, ops.cxx_epilogue_file ()[db]); + cxx << "//" << endl + << "// End epilogue." << endl + << endl; + } } cxx << "#include <odb/post.hxx>" << endl; @@ -501,15 +586,22 @@ generate (options const& ops, // Copy prologue. // - if (!ops.schema_prologue ().empty () || - !ops.schema_prologue_file ().empty ()) { - sch << "// Begin prologue." << endl - << "//" << endl; - append (sch, ops.schema_prologue (), ops.schema_prologue_file ()); - sch << "//" << endl - << "// End prologue." << endl - << endl; + bool p (ops.schema_prologue ().count (db) != 0); + bool pf (ops.schema_prologue_file ().count (db) != 0); + + if (p || pf) + { + sch << "// Begin prologue." << endl + << "//" << endl; + if (p) + append (sch, ops.schema_prologue ()[db]); + if (pf) + append (sch, ops.schema_prologue_file ()[db]); + sch << "//" << endl + << "// End prologue." << endl + << endl; + } } sch << "#include " << ctx->process_include_path (hxx_name) << endl @@ -520,8 +612,12 @@ generate (options const& ops, // ind_filter ind (ctx->os); - switch (ops.database ()) + switch (db) { + case database::common: + { + assert (false); + } case database::mssql: case database::mysql: case database::oracle: @@ -536,15 +632,22 @@ generate (options const& ops, // Copy epilogue. // - if (!ops.schema_epilogue ().empty () || - !ops.schema_epilogue_file ().empty ()) { - sch << "// Begin epilogue." << endl - << "//" << endl; - append (sch, ops.schema_epilogue (), ops.schema_epilogue_file ()); - sch << "//" << endl - << "// End epilogue." << endl - << endl; + bool e (ops.schema_epilogue ().count (db) != 0); + bool ef (ops.schema_epilogue_file ().count (db) != 0); + + if (e || ef) + { + sch << "// Begin epilogue." << endl + << "//" << endl; + if (e) + append (sch, ops.schema_epilogue ()[db]); + if (ef) + append (sch, ops.schema_epilogue_file ()[db]); + sch << "//" << endl + << "// End epilogue." << endl + << endl; + } } sch << "#include <odb/post.hxx>" << endl; @@ -562,8 +665,12 @@ generate (options const& ops, auto_ptr<context> ctx ( create_context (sql, unit, ops, fts, model.get ())); - switch (ops.database ()) + switch (db) { + case database::common: + { + assert (false); + } case database::mssql: case database::mysql: case database::oracle: @@ -574,15 +681,22 @@ generate (options const& ops, // Copy prologue. // - if (!ops.sql_prologue ().empty () || - !ops.sql_prologue_file ().empty ()) { - sql << "/* Begin prologue." << endl - << " */" << endl; - append (sql, ops.sql_prologue (), ops.sql_prologue_file ()); - sql << "/*" << endl - << " * End prologue. */" << endl - << endl; + bool p (ops.sql_prologue ().count (db) != 0); + bool pf (ops.sql_prologue_file ().count (db) != 0); + + if (p || pf) + { + sql << "/* Begin prologue." << endl + << " */" << endl; + if (p) + append (sql, ops.sql_prologue ()[db]); + if (pf) + append (sql, ops.sql_prologue_file ()[db]); + sql << "/*" << endl + << " * End prologue. */" << endl + << endl; + } } if (!ops.omit_drop ()) @@ -590,15 +704,22 @@ generate (options const& ops, // Copy interlude. // - if (!ops.sql_interlude ().empty () || - !ops.sql_interlude_file ().empty ()) { - sql << "/* Begin interlude." << endl - << " */" << endl; - append (sql, ops.sql_interlude (), ops.sql_interlude_file ()); - sql << "/*" << endl - << " * End interlude. */" << endl - << endl; + bool i (ops.sql_interlude ().count (db) != 0); + bool ifl (ops.sql_interlude_file ().count (db) != 0); + + if (i || ifl) + { + sql << "/* Begin interlude." << endl + << " */" << endl; + if (i) + append (sql, ops.sql_interlude ()[db]); + if (ifl) + append (sql, ops.sql_interlude_file ()[db]); + sql << "/*" << endl + << " * End interlude. */" << endl + << endl; + } } if (!ops.omit_create ()) @@ -606,15 +727,22 @@ generate (options const& ops, // Copy epilogue. // - if (!ops.sql_epilogue ().empty () || - !ops.sql_epilogue_file ().empty ()) { - sql << "/* Begin epilogue." << endl - << " */" << endl; - append (sql, ops.sql_epilogue (), ops.sql_epilogue_file ()); - sql << "/*" << endl - << " * End epilogue. */" << endl - << endl; + bool e (ops.sql_epilogue ().count (db) != 0); + bool ef (ops.sql_epilogue_file ().count (db) != 0); + + if (e || ef) + { + sql << "/* Begin epilogue." << endl + << " */" << endl; + if (e) + append (sql, ops.sql_epilogue ()[db]); + if (ef) + append (sql, ops.sql_epilogue_file ()[db]); + sql << "/*" << endl + << " * End epilogue. */" << endl + << endl; + } } relational::schema::generate_epilogue (); |