diff --git a/drogon_ctl/create_model.cc b/drogon_ctl/create_model.cc index 0971d1a9..3b7dd933 100644 --- a/drogon_ctl/create_model.cc +++ b/drogon_ctl/create_model.cc @@ -67,7 +67,8 @@ std::string nameTransform(const std::string &origName, bool isType) #if USE_POSTGRESQL void create_model::createModelClassFromPG(const std::string &path, const DbClientPtr &client, - const std::string &tableName) + const std::string &tableName, + const std::string &schema) { auto className = nameTransform(tableName, true); HttpViewData data; @@ -77,12 +78,16 @@ void create_model::createModelClassFromPG(const std::string &path, data["primaryKeyName"] = ""; data["dbName"] = _dbname; data["rdbms"] = std::string("postgresql"); + if (schema != "public") + { + data["schema"] = schema; + } std::vector cols; *client << "SELECT * \ FROM information_schema.columns \ - WHERE table_schema = 'public' \ - AND table_name = $1" - << tableName << Mode::Blocking >> + WHERE table_schema = $1 \ + AND table_name = $2" + << schema << tableName << Mode::Blocking >> [&](const Result &r) { if (r.size() == 0) { @@ -285,7 +290,8 @@ void create_model::createModelClassFromPG(const std::string &path, sourceFile << templ->genText(data); } void create_model::createModelFromPG(const std::string &path, - const DbClientPtr &client) + const DbClientPtr &client, + const std::string &schema) { *client << "SELECT a.oid," "a.relname AS name," @@ -294,9 +300,9 @@ void create_model::createModelFromPG(const std::string &path, "LEFT OUTER JOIN pg_description b ON b.objsubid = 0 AND a.oid = " "b.objoid " "WHERE a.relnamespace = (SELECT oid FROM pg_namespace WHERE " - "nspname = 'public') " + "nspname = $1) " "AND a.relkind = 'r' ORDER BY a.relname" - << Mode::Blocking >> + << schema << Mode::Blocking >> [&](bool isNull, size_t oid, const std::string &tableName, @@ -304,7 +310,7 @@ void create_model::createModelFromPG(const std::string &path, if (!isNull) { std::cout << "table name:" << tableName << std::endl; - createModelClassFromPG(path, client, tableName); + createModelClassFromPG(path, client, tableName, schema); } } >> [](const DrogonDbException &e) { @@ -652,6 +658,8 @@ void create_model::createModel(const std::string &path, connStr += " password="; connStr += password; } + + auto schema = config.get("schema", "public").asString(); DbClientPtr client = drogon::orm::DbClient::newPgClient(connStr, 1); std::cout << "Connect to server..." << std::endl; std::cout << "Source files in the " << path @@ -664,14 +672,14 @@ void create_model::createModel(const std::string &path, } auto tables = config["tables"]; if (!tables || tables.size() == 0) - createModelFromPG(path, client); + createModelFromPG(path, client, schema); else { for (int i = 0; i < (int)tables.size(); i++) { auto tableName = tables[i].asString(); std::cout << "table name:" << tableName << std::endl; - createModelClassFromPG(path, client, tableName); + createModelClassFromPG(path, client, tableName, schema); } } #else diff --git a/drogon_ctl/create_model.h b/drogon_ctl/create_model.h index fadca6dd..89c5c1bb 100644 --- a/drogon_ctl/create_model.h +++ b/drogon_ctl/create_model.h @@ -57,8 +57,11 @@ class create_model : public DrObject, public CommandHandler #if USE_POSTGRESQL void createModelClassFromPG(const std::string &path, const DbClientPtr &client, - const std::string &tableName); - void createModelFromPG(const std::string &path, const DbClientPtr &client); + const std::string &tableName, + const std::string &schema); + void createModelFromPG(const std::string &path, + const DbClientPtr &client, + const std::string &schema); #endif #if USE_MYSQL void createModelClassFromMysql(const std::string &path, diff --git a/drogon_ctl/templates/model_cc.csp b/drogon_ctl/templates/model_cc.csp index d3698c3a..365ef374 100644 --- a/drogon_ctl/templates/model_cc.csp +++ b/drogon_ctl/templates/model_cc.csp @@ -17,7 +17,17 @@ using namespace drogon_ctl; %> using namespace drogon; -using namespace drogon_model::[[dbName]]; +using namespace drogon_model::[[dbName]]<%c++ +auto &schema=@@.get("schema"); +if(!schema.empty()) +{ + $$<<"::"< <%c++for(auto col:cols){ %> diff --git a/drogon_ctl/templates/model_h.csp b/drogon_ctl/templates/model_h.csp index 2a89ed57..01c52ae2 100644 --- a/drogon_ctl/templates/model_h.csp +++ b/drogon_ctl/templates/model_h.csp @@ -29,7 +29,14 @@ namespace drogon_model { namespace [[dbName]] { - +<%c++ +auto &schema=@@.get("schema"); +if(!schema.empty()) +{ + $$<<"namespace "< class [[className]] { public: @@ -147,6 +154,11 @@ auto cols=@@.get>("columns"); static const std::vector _metaData; bool _dirtyFlag[{%cols.size()%}]={ false }; }; - +<%c++ +if(!schema.empty()) +{ + $$<<"} // namespace "< } // namespace [[dbName]] } // namespace drogon_model diff --git a/drogon_ctl/templates/model_json.csp b/drogon_ctl/templates/model_json.csp index 4a9c8ce3..49b42c74 100644 --- a/drogon_ctl/templates/model_json.csp +++ b/drogon_ctl/templates/model_json.csp @@ -1,14 +1,16 @@ { - //rdbms:server type, postgresql,mysql or sqlite3 + //rdbms: server type, postgresql,mysql or sqlite3 "rdbms":"postgresql", - //filename:sqlite3 db file name + //filename: sqlite3 db file name //"filename":"", - //host:server address,localhost by default; + //host: server address,localhost by default; "host":"127.0.0.1", - //port:server port, 5432 by default; + //port: server port, 5432 by default; "port":5432, - //dbname:Database name; + //dbname: Database name; "dbname":"", + //schema: valid for postgreSQL, "public" by default; + //"schema":"public", "user":"", "passwd":"", "tables":[]