diff --git a/drogon_ctl/create_model.cc b/drogon_ctl/create_model.cc index c2697ce3..4461f2ef 100644 --- a/drogon_ctl/create_model.cc +++ b/drogon_ctl/create_model.cc @@ -42,6 +42,43 @@ static std::string toLower(const std::string &str) std::transform(ret.begin(), ret.end(), ret.begin(), tolower); return ret; } + +static std::map> getConvertMethods( + const Json::Value &convertColumns) +{ + std::map> ret; + auto enabled = convertColumns.get("enabled", false).asBool(); + if (!enabled) + { + return ret; + } // endif + auto items = convertColumns["items"]; + if (items.isNull()) + { + return ret; + } // endif + if (!items.isArray()) + { + std::cerr << "items of convert must be an array" << std::endl; + exit(1); + } // endif + for (auto &convertColumn : items) + { + try + { + ConvertMethod c(convertColumn); + ret[c.tableName()].push_back(c); + } // try + catch (const std::runtime_error &e) + { + std::cerr << e.what() << std::endl; + exit(1); + } // catch + } // for + + return ret; +} + static std::map> getRelationships( const Json::Value &relationships) { @@ -80,6 +117,19 @@ static std::map> getRelationships( return ret; } +bool drogon_ctl::ConvertMethod::shouldConvert(const std::string &tableName, + const std::string &colName) const +{ + if (tableName == "*") + { + return colName == colName_; + } + else + { + return (tableName == tableName_ && colName == colName_); + } // endif +} + #if USE_POSTGRESQL void create_model::createModelClassFromPG( const std::string &path, @@ -87,7 +137,8 @@ void create_model::createModelClassFromPG( const std::string &tableName, const std::string &schema, const Json::Value &restfulApiConfig, - const std::vector &relationships) + const std::vector &relationships, + const std::vector &convertMethods) { auto className = nameTransform(tableName, true); HttpViewData data; @@ -98,6 +149,7 @@ void create_model::createModelClassFromPG( data["dbName"] = dbname_; data["rdbms"] = std::string("postgresql"); data["relationships"] = relationships; + data["convertMethods"] = convertMethods; if (schema != "public") { data["schema"] = schema; @@ -320,7 +372,8 @@ void create_model::createModelFromPG( const DbClientPtr &client, const std::string &schema, const Json::Value &restfulApiConfig, - std::map> &relationships) + std::map> &relationships, + std::map> &convertMethods) { *client << "SELECT a.oid," "a.relname AS name," @@ -345,7 +398,8 @@ void create_model::createModelFromPG( tableName, schema, restfulApiConfig, - relationships[tableName]); + relationships[tableName], + convertMethods[tableName]); } } >> [](const DrogonDbException &e) { @@ -361,7 +415,8 @@ void create_model::createModelClassFromMysql( const DbClientPtr &client, const std::string &tableName, const Json::Value &restfulApiConfig, - const std::vector &relationships) + const std::vector &relationships, + const std::vector &convertMethods) { auto className = nameTransform(tableName, true); HttpViewData data; @@ -372,6 +427,7 @@ void create_model::createModelClassFromMysql( data["dbName"] = dbname_; data["rdbms"] = std::string("mysql"); data["relationships"] = relationships; + data["convertMethods"] = convertMethods; std::vector cols; int i = 0; *client << "desc " + tableName << Mode::Blocking >> @@ -505,7 +561,8 @@ void create_model::createModelFromMysql( const std::string &path, const DbClientPtr &client, const Json::Value &restfulApiConfig, - std::map> &relationships) + std::map> &relationships, + std::map> &convertMethods) { *client << "show tables" << Mode::Blocking >> [&](bool isNull, std::string &&tableName) { @@ -516,7 +573,8 @@ void create_model::createModelFromMysql( client, tableName, restfulApiConfig, - relationships[tableName]); + relationships[tableName], + convertMethods[tableName]); } } >> [](const DrogonDbException &e) { std::cerr << e.base().what() << std::endl; @@ -530,7 +588,8 @@ void create_model::createModelClassFromSqlite3( const DbClientPtr &client, const std::string &tableName, const Json::Value &restfulApiConfig, - const std::vector &relationships) + const std::vector &relationships, + const std::vector &convertMethods) { HttpViewData data; auto className = nameTransform(tableName, true); @@ -541,6 +600,7 @@ void create_model::createModelClassFromSqlite3( data["dbName"] = std::string("sqlite3"); data["rdbms"] = std::string("sqlite3"); data["relationships"] = relationships; + data["convertMethods"] = convertMethods; std::vector cols; std::string sql = "PRAGMA table_info(" + tableName + ");"; *client << sql << Mode::Blocking >> [&](const Result &result) { @@ -664,7 +724,8 @@ void create_model::createModelFromSqlite3( const std::string &path, const DbClientPtr &client, const Json::Value &restfulApiConfig, - std::map> &relationships) + std::map> &relationships, + std::map> &convertMethods) { *client << "SELECT name FROM sqlite_master WHERE name!='sqlite_sequence' " "and (type='table' or type='view') ORDER BY name;" @@ -677,7 +738,8 @@ void create_model::createModelFromSqlite3( client, tableName, restfulApiConfig, - relationships[tableName]); + relationships[tableName], + convertMethods[tableName]); } } >> [](const DrogonDbException &e) { @@ -695,6 +757,7 @@ void create_model::createModel(const std::string &path, std::transform(dbType.begin(), dbType.end(), dbType.begin(), tolower); auto restfulApiConfig = config["restful_api_controllers"]; auto relationships = getRelationships(config["relationships"]); + auto convertMethods = getConvertMethods(config["convert"]); if (dbType == "postgresql") { #if USE_POSTGRESQL @@ -764,8 +827,12 @@ void create_model::createModel(const std::string &path, { auto tables = config["tables"]; if (!tables || tables.size() == 0) - createModelFromPG( - path, client, schema, restfulApiConfig, relationships); + createModelFromPG(path, + client, + schema, + restfulApiConfig, + relationships, + convertMethods); else { for (int i = 0; i < (int)tables.size(); ++i) @@ -781,7 +848,8 @@ void create_model::createModel(const std::string &path, tableName, schema, restfulApiConfig, - relationships[tableName]); + relationships[tableName], + convertMethods[tableName]); } } } @@ -792,7 +860,8 @@ void create_model::createModel(const std::string &path, singleModelName, schema, restfulApiConfig, - relationships[singleModelName]); + relationships[singleModelName], + convertMethods[singleModelName]); } #else std::cerr @@ -871,7 +940,8 @@ void create_model::createModel(const std::string &path, createModelFromMysql(path, client, restfulApiConfig, - relationships); + relationships, + convertMethods); else { for (int i = 0; i < (int)tables.size(); ++i) @@ -886,7 +956,8 @@ void create_model::createModel(const std::string &path, client, tableName, restfulApiConfig, - relationships[tableName]); + relationships[tableName], + convertMethods[tableName]); } } } @@ -896,7 +967,8 @@ void create_model::createModel(const std::string &path, client, singleModelName, restfulApiConfig, - relationships[singleModelName]); + relationships[singleModelName], + convertMethods[singleModelName]); } #else @@ -943,7 +1015,8 @@ void create_model::createModel(const std::string &path, createModelFromSqlite3(path, client, restfulApiConfig, - relationships); + relationships, + convertMethods); else { for (int i = 0; i < (int)tables.size(); ++i) @@ -958,7 +1031,8 @@ void create_model::createModel(const std::string &path, client, tableName, restfulApiConfig, - relationships[tableName]); + relationships[tableName], + convertMethods[tableName]); } } } @@ -968,7 +1042,8 @@ void create_model::createModel(const std::string &path, client, singleModelName, restfulApiConfig, - relationships[singleModelName]); + relationships[singleModelName], + convertMethods[singleModelName]); } #else diff --git a/drogon_ctl/create_model.h b/drogon_ctl/create_model.h index 5da2c471..dcf9f8d9 100644 --- a/drogon_ctl/create_model.h +++ b/drogon_ctl/create_model.h @@ -123,6 +123,75 @@ class PivotTable std::string originalKey_; std::string targetKey_; }; + +class ConvertMethod +{ + public: + ConvertMethod(const Json::Value &convert) + { + tableName_ = convert.get("table", "*").asString(); + colName_ = convert.get("column", "*").asString(); + + auto method = convert["method"]; + if (method.isNull()) + { + throw std::runtime_error("method - object is missing."); + } // endif + if (!method.isObject()) + { + throw std::runtime_error("method is not an object."); + } // endif + methodBeforeDbWrite_ = method.get("before_db_write", "").asString(); + methodAfterDbRead_ = method.get("after_db_read", "").asString(); + + auto includeFiles = convert["includes"]; + if (includeFiles.isNull()) + { + return; + } // endif + if (!includeFiles.isArray()) + { + throw std::runtime_error("includes must be an array"); + } // endif + for (auto &i : includeFiles) + { + includeFiles_.push_back(i.asString()); + } // for + } + ConvertMethod() = default; + + bool shouldConvert(const std::string &tableName, + const std::string &colName) const; + + const std::string &tableName() const + { + return tableName_; + } + const std::string &colName() const + { + return colName_; + } + const std::string &methodBeforeDbWrite() const + { + return methodBeforeDbWrite_; + } + const std::string &methodAfterDbRead() const + { + return methodAfterDbRead_; + } + const std::vector &includeFiles() const + { + return includeFiles_; + } + + private: + std::string tableName_{"*"}; + std::string colName_{"*"}; + std::string methodBeforeDbWrite_; + std::string methodAfterDbRead_; + std::vector includeFiles_; +}; + class Relationship { public: @@ -279,18 +348,22 @@ class create_model : public DrObject, public CommandHandler const Json::Value &config, const std::string &singleModelName); #if USE_POSTGRESQL - void createModelClassFromPG(const std::string &path, - const DbClientPtr &client, - const std::string &tableName, - const std::string &schema, - const Json::Value &restfulApiConfig, - const std::vector &relationships); + void createModelClassFromPG( + const std::string &path, + const DbClientPtr &client, + const std::string &tableName, + const std::string &schema, + const Json::Value &restfulApiConfig, + const std::vector &relationships, + const std::vector &convertMethods); + void createModelFromPG( const std::string &path, const DbClientPtr &client, const std::string &schema, const Json::Value &restfulApiConfig, - std::map> &relationships); + std::map> &relationships, + std::map> &convertMethods); #endif #if USE_MYSQL void createModelClassFromMysql( @@ -298,12 +371,14 @@ class create_model : public DrObject, public CommandHandler const DbClientPtr &client, const std::string &tableName, const Json::Value &restfulApiConfig, - const std::vector &relationships); + const std::vector &relationships, + const std::vector &convertMethods); void createModelFromMysql( const std::string &path, const DbClientPtr &client, const Json::Value &restfulApiConfig, - std::map> &relationships); + std::map> &relationships, + std::map> &convertMethods); #endif #if USE_SQLITE3 void createModelClassFromSqlite3( @@ -311,12 +386,14 @@ class create_model : public DrObject, public CommandHandler const DbClientPtr &client, const std::string &tableName, const Json::Value &restfulApiConfig, - const std::vector &relationships); + const std::vector &relationships, + const std::vector &convertMethod); void createModelFromSqlite3( const std::string &path, const DbClientPtr &client, const Json::Value &restfulApiConfig, - std::map> &relationships); + std::map> &relationships, + std::map> &convertMethod); #endif void createRestfulAPIController(const DrTemplateData &tableInfo, const Json::Value &restfulApiConfig); diff --git a/drogon_ctl/templates/model_cc.csp b/drogon_ctl/templates/model_cc.csp index 03595ce6..4696dca8 100644 --- a/drogon_ctl/templates/model_cc.csp +++ b/drogon_ctl/templates/model_cc.csp @@ -39,6 +39,15 @@ for(auto &relationship : relationships) } %> #include +<%c++ +auto &convertMethods=@@.get>("convertMethods"); +for(auto convertMethod : convertMethods ) { + for(auto i : convertMethod.includeFiles()) { %> +<%c++ $$<<"#include "< +<%c++ + } //for +} //for +%> #include <%c++ const auto &cols=@@.get>("columns"); @@ -125,8 +134,12 @@ const std::string &[[className]]::getColumnName(size_t index) noexcept(false) $$<<" memset(&stm,0,sizeof(stm));\n"; $$<<" strptime(daysStr.c_str(),\"%Y-%m-%d\",&stm);\n"; $$<<" time_t t = mktime(&stm);\n"; - // $$<<" "<(::trantor::Date(946656000000000).after(daysNum*86400));\n"; +// $$<<" "<(::trantor::Date(946656000000000).after(daysNum*86400));\n"; $$<<" "<(t*1000000);\n"; + auto convertMethod=std::find_if(convertMethods.begin(),convertMethods.end(),[col](const ConvertMethod& c){ return c.shouldConvert("*", col.colName_); }); + if (convertMethod != convertMethods.end() ) { + $$<<" "<< convertMethod->methodAfterDbRead() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; continue; } @@ -151,6 +164,10 @@ const std::string &[[className]]::getColumnName(size_t index) noexcept(false) $$<<" }\n"; // $$<<" "<(::trantor::Date(946656000000000).after(daysNum*86400));\n"; $$<<" "<(t*1000000+decimalNum);\n"; + auto convertMethod=std::find_if(convertMethods.begin(),convertMethods.end(),[col](const ConvertMethod& c){return c.shouldConvert("*", col.colName_); }); + if (convertMethod != convertMethods.end() ) { + $$<<" "<< convertMethod->methodAfterDbRead() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -162,12 +179,21 @@ const std::string &[[className]]::getColumnName(size_t index) noexcept(false) $$<<" str[0]=='\\\\'&&str[1]=='x')\n"; $$<<" {\n"; $$<<" "<>(drogon::utils::hexToBinaryVector(str.data()+2,str.length()-2));\n"; + auto convertMethod=std::find_if(convertMethods.begin(),convertMethods.end(),[col](const ConvertMethod& c){ return c.shouldConvert("*", col.colName_); }); + if (convertMethod != convertMethods.end() ) { + $$<<" "<< convertMethod->methodAfterDbRead() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; } %> {%col.colValName_%}_=std::make_shared<{%col.colType_%}>(r["{%col.colName_%}"].as<{%col.colType_%}>()); +<%c++ + auto convertMethod=std::find_if(convertMethods.begin(),convertMethods.end(),[col](const ConvertMethod& c){ return c.shouldConvert("*", col.colName_); }); + if (convertMethod != convertMethods.end() ) { + $$<<" "<< convertMethod->methodAfterDbRead() << "(" << col.colValName_ << "_);\n"; + } //endif %> } <%c++} %> @@ -201,6 +227,10 @@ const std::string &[[className]]::getColumnName(size_t index) noexcept(false) $$<<" time_t t = mktime(&stm);\n"; // $$<<" "<(::trantor::Date(946656000000000).after(daysNum*86400));\n"; $$<<" "<(t*1000000);\n"; + auto convertMethod=std::find_if(convertMethods.begin(),convertMethods.end(),[col](const ConvertMethod& c){ return c.shouldConvert("*", col.colName_); }); + if (convertMethod != convertMethods.end() ) { + $$<<" "<< convertMethod->methodAfterDbRead() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; continue; } @@ -225,6 +255,10 @@ const std::string &[[className]]::getColumnName(size_t index) noexcept(false) $$<<" }\n"; // $$<<" "<(::trantor::Date(946656000000000).after(daysNum*86400));\n"; $$<<" "<(t*1000000+decimalNum);\n"; + auto convertMethod=std::find_if(convertMethods.begin(),convertMethods.end(),[col](const ConvertMethod& c){ return c.shouldConvert("*", col.colName_); }); + if (convertMethod != convertMethods.end() ) { + $$<<" "<< convertMethod->methodAfterDbRead() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -236,12 +270,21 @@ const std::string &[[className]]::getColumnName(size_t index) noexcept(false) $$<<" str[0]=='\\\\'&&str[1]=='x')\n"; $$<<" {\n"; $$<<" "<>(drogon::utils::hexToBinaryVector(str.data()+2,str.length()-2));\n"; + auto convertMethod=std::find_if(convertMethods.begin(),convertMethods.end(),[col](const ConvertMethod& c){ return c.shouldConvert("*", col.colName_); }); + if (convertMethod != convertMethods.end() ) { + $$<<" "<< convertMethod->methodAfterDbRead() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; } %> {%col.colValName_%}_=std::make_shared<{%col.colType_%}>(r[index].as<{%col.colType_%}>()); +<%c++ + auto convertMethod=std::find_if(convertMethods.begin(),convertMethods.end(),[col](const ConvertMethod& c){ return c.shouldConvert("*", col.colName_); }); + if (convertMethod != convertMethods.end() ) { + $$<<" "<< convertMethod->methodAfterDbRead() << "(" << col.colValName_ << "_);\n"; + } //endif %> } <%c++}%> } @@ -271,6 +314,11 @@ const std::string &[[className]]::getColumnName(size_t index) noexcept(false) $$<<" if(!pJson[pMasqueradingVector["<(pJson[pMasqueradingVector["<methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif + $$<<" }\n"; $$<<" }\n"; continue; @@ -286,6 +334,10 @@ const std::string &[[className]]::getColumnName(size_t index) noexcept(false) $$<<" time_t t = mktime(&stm);\n"; // $$<<" "<(::trantor::Date(946656000000000).after(daysNum*86400));\n"; $$<<" "<(t*1000000);\n"; + auto convertMethod=std::find_if(convertMethods.begin(),convertMethods.end(),[col](const ConvertMethod& c){ return c.shouldConvert("*", col.colName_); }); + if (convertMethod != convertMethods.end() ) { + $$<<" "<< convertMethod->methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -313,6 +365,10 @@ const std::string &[[className]]::getColumnName(size_t index) noexcept(false) $$<<" }\n"; // $$<<" "<(::trantor::Date(946656000000000).after(daysNum*86400));\n"; $$<<" "<(t*1000000+decimalNum);\n"; + auto convertMethod=std::find_if(convertMethods.begin(),convertMethods.end(),[col](const ConvertMethod& c){ return c.shouldConvert("*", col.colName_); }); + if (convertMethod != convertMethods.end() ) { + $$<<" "<< convertMethod->methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; $$<<" }\n"; @@ -324,6 +380,10 @@ const std::string &[[className]]::getColumnName(size_t index) noexcept(false) $$<<" {\n"; $$<<" auto str = pJson[pMasqueradingVector["<>(drogon::utils::base64DecodeToVector(str));\n"; + auto convertMethod=std::find_if(convertMethods.begin(),convertMethods.end(),[col](const ConvertMethod& c){ return c.shouldConvert("*", col.colName_); }); + if (convertMethod != convertMethods.end() ) { + $$<<" "<< convertMethod->methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -333,6 +393,10 @@ const std::string &[[className]]::getColumnName(size_t index) noexcept(false) $$<<" if(!pJson[pMasqueradingVector["<(("<methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -342,6 +406,10 @@ const std::string &[[className]]::getColumnName(size_t index) noexcept(false) $$<<" if(!pJson[pMasqueradingVector["<(("<methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -351,6 +419,10 @@ const std::string &[[className]]::getColumnName(size_t index) noexcept(false) $$<<" if(!pJson[pMasqueradingVector["<(pJson[pMasqueradingVector["<methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -360,6 +432,10 @@ const std::string &[[className]]::getColumnName(size_t index) noexcept(false) $$<<" if(!pJson[pMasqueradingVector["<(pJson[pMasqueradingVector["<methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -369,6 +445,10 @@ const std::string &[[className]]::getColumnName(size_t index) noexcept(false) $$<<" if(!pJson[pMasqueradingVector["<(pJson[pMasqueradingVector["<methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -398,6 +478,10 @@ const std::string &[[className]]::getColumnName(size_t index) noexcept(false) $$<<" if(!pJson[\""<(pJson[\""<methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -413,6 +497,10 @@ const std::string &[[className]]::getColumnName(size_t index) noexcept(false) $$<<" time_t t = mktime(&stm);\n"; // $$<<" "<(::trantor::Date(946656000000000).after(daysNum*86400));\n"; $$<<" "<(t*1000000);\n"; + auto convertMethod=std::find_if(convertMethods.begin(),convertMethods.end(),[col](const ConvertMethod& c){ return c.shouldConvert("*", col.colName_); }); + if (convertMethod != convertMethods.end() ) { + $$<<" "<< convertMethod->methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -440,6 +528,10 @@ const std::string &[[className]]::getColumnName(size_t index) noexcept(false) $$<<" }\n"; // $$<<" "<(::trantor::Date(946656000000000).after(daysNum*86400));\n"; $$<<" "<(t*1000000+decimalNum);\n"; + auto convertMethod=std::find_if(convertMethods.begin(),convertMethods.end(),[col](const ConvertMethod& c){ return c.shouldConvert("*", col.colName_); }); + if (convertMethod != convertMethods.end() ) { + $$<<" "<< convertMethod->methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; $$<<" }\n"; @@ -451,6 +543,10 @@ const std::string &[[className]]::getColumnName(size_t index) noexcept(false) $$<<" {\n"; $$<<" auto str = pJson[\""<>(drogon::utils::base64DecodeToVector(str));\n"; + auto convertMethod=std::find_if(convertMethods.begin(),convertMethods.end(),[col](const ConvertMethod& c){ return c.shouldConvert("*", col.colName_); }); + if (convertMethod != convertMethods.end() ) { + $$<<" "<< convertMethod->methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -460,6 +556,10 @@ const std::string &[[className]]::getColumnName(size_t index) noexcept(false) $$<<" if(!pJson[\""<(("<methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -469,6 +569,10 @@ const std::string &[[className]]::getColumnName(size_t index) noexcept(false) $$<<" if(!pJson[\""<(("<methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -478,6 +582,10 @@ const std::string &[[className]]::getColumnName(size_t index) noexcept(false) $$<<" if(!pJson[\""<(pJson[\""<methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -487,6 +595,10 @@ const std::string &[[className]]::getColumnName(size_t index) noexcept(false) $$<<" if(!pJson[\""<(pJson[\""<methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -496,6 +608,10 @@ const std::string &[[className]]::getColumnName(size_t index) noexcept(false) $$<<" if(!pJson[\""<(pJson[\""<methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -534,6 +650,10 @@ void [[className]]::updateByMasqueradedJson(const Json::Value &pJson, $$<<" if(!pJson[pMasqueradingVector["<(pJson[pMasqueradingVector["<methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -549,6 +669,10 @@ void [[className]]::updateByMasqueradedJson(const Json::Value &pJson, $$<<" time_t t = mktime(&stm);\n"; // $$<<" "<(::trantor::Date(946656000000000).after(daysNum*86400));\n"; $$<<" "<(t*1000000);\n"; + auto convertMethod=std::find_if(convertMethods.begin(),convertMethods.end(),[col](const ConvertMethod& c){ return c.shouldConvert("*", col.colName_); }); + if (convertMethod != convertMethods.end() ) { + $$<<" "<< convertMethod->methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -576,6 +700,10 @@ void [[className]]::updateByMasqueradedJson(const Json::Value &pJson, $$<<" }\n"; // $$<<" "<(::trantor::Date(946656000000000).after(daysNum*86400));\n"; $$<<" "<(t*1000000+decimalNum);\n"; + auto convertMethod=std::find_if(convertMethods.begin(),convertMethods.end(),[col](const ConvertMethod& c){ return c.shouldConvert("*", col.colName_); }); + if (convertMethod != convertMethods.end() ) { + $$<<" "<< convertMethod->methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; $$<<" }\n"; @@ -587,6 +715,10 @@ void [[className]]::updateByMasqueradedJson(const Json::Value &pJson, $$<<" {\n"; $$<<" auto str = pJson[pMasqueradingVector["<>(drogon::utils::base64DecodeToVector(str));\n"; + auto convertMethod=std::find_if(convertMethods.begin(),convertMethods.end(),[col](const ConvertMethod& c){ return c.shouldConvert("*", col.colName_); }); + if (convertMethod != convertMethods.end() ) { + $$<<" "<< convertMethod->methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -596,6 +728,10 @@ void [[className]]::updateByMasqueradedJson(const Json::Value &pJson, $$<<" if(!pJson[pMasqueradingVector["<(("<methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -605,6 +741,10 @@ void [[className]]::updateByMasqueradedJson(const Json::Value &pJson, $$<<" if(!pJson[pMasqueradingVector["<(("<methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -614,6 +754,10 @@ void [[className]]::updateByMasqueradedJson(const Json::Value &pJson, $$<<" if(!pJson[pMasqueradingVector["<(pJson[pMasqueradingVector["<methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -623,6 +767,10 @@ void [[className]]::updateByMasqueradedJson(const Json::Value &pJson, $$<<" if(!pJson[pMasqueradingVector["<(pJson[pMasqueradingVector["<methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -632,6 +780,10 @@ void [[className]]::updateByMasqueradedJson(const Json::Value &pJson, $$<<" if(!pJson[pMasqueradingVector["<(pJson[pMasqueradingVector["<methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -664,6 +816,10 @@ void [[className]]::updateByJson(const Json::Value &pJson) noexcept(false) $$<<" if(!pJson[\""<(pJson[\""<methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -679,6 +835,10 @@ void [[className]]::updateByJson(const Json::Value &pJson) noexcept(false) $$<<" time_t t = mktime(&stm);\n"; // $$<<" "<(::trantor::Date(946656000000000).after(daysNum*86400));\n"; $$<<" "<(t*1000000);\n"; + auto convertMethod=std::find_if(convertMethods.begin(),convertMethods.end(),[col](const ConvertMethod& c){ return c.shouldConvert("*", col.colName_); }); + if (convertMethod != convertMethods.end() ) { + $$<<" "<< convertMethod->methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -706,6 +866,10 @@ void [[className]]::updateByJson(const Json::Value &pJson) noexcept(false) $$<<" }\n"; // $$<<" "<(::trantor::Date(946656000000000).after(daysNum*86400));\n"; $$<<" "<(t*1000000+decimalNum);\n"; + auto convertMethod=std::find_if(convertMethods.begin(),convertMethods.end(),[col](const ConvertMethod& c){ return c.shouldConvert("*", col.colName_); }); + if (convertMethod != convertMethods.end() ) { + $$<<" "<< convertMethod->methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; $$<<" }\n"; @@ -717,6 +881,10 @@ void [[className]]::updateByJson(const Json::Value &pJson) noexcept(false) $$<<" {\n"; $$<<" auto str = pJson[\""<>(drogon::utils::base64DecodeToVector(str));\n"; + auto convertMethod=std::find_if(convertMethods.begin(),convertMethods.end(),[col](const ConvertMethod& c){ return c.shouldConvert("*", col.colName_); }); + if (convertMethod != convertMethods.end() ) { + $$<<" "<< convertMethod->methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -726,6 +894,10 @@ void [[className]]::updateByJson(const Json::Value &pJson) noexcept(false) $$<<" if(!pJson[\""<(("<methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -735,6 +907,10 @@ void [[className]]::updateByJson(const Json::Value &pJson) noexcept(false) $$<<" if(!pJson[\""<(("<methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -744,6 +920,10 @@ void [[className]]::updateByJson(const Json::Value &pJson) noexcept(false) $$<<" if(!pJson[\""<(pJson[\""<methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -753,6 +933,10 @@ void [[className]]::updateByJson(const Json::Value &pJson) noexcept(false) $$<<" if(!pJson[\""<(pJson[\""<methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; @@ -762,6 +946,10 @@ void [[className]]::updateByJson(const Json::Value &pJson) noexcept(false) $$<<" if(!pJson[\""<(pJson[\""<methodBeforeDbWrite() << "(" << col.colValName_ << "_);\n"; + } //endif $$<<" }\n"; $$<<" }\n"; continue; diff --git a/drogon_ctl/templates/model_json.csp b/drogon_ctl/templates/model_json.csp index 354d5db7..c5045aeb 100644 --- a/drogon_ctl/templates/model_json.csp +++ b/drogon_ctl/templates/model_json.csp @@ -20,6 +20,24 @@ //"client_encoding": "", //table: An array of tables to be modelized. if the array is empty, all revealed tables are modelized. "tables": [], + //convert: the value can be changed by a function call before it is stored into database or + //after it is read from database + "convert": { + "enabled": false, + "items":[{ + "table": "user", + "column": "password", + "method": { + //after_db_read: name of the method which is called after reading from database, signature: void([const] std::shared_ptr [&]) + "after_db_read": "decrypt_password", + //before_db_write: name of the method which is called before writing to database, signature: void([const] std::shared_ptr [&]) + "before_db_write": "encrypt_password" + }, + "includes": [ + "\"file_local_search_path.h\"","" + ] + }] + }, "relationships": { "enabled": false, "items": [{ @@ -83,4 +101,4 @@ // generate_base_only: false by default. Set to true to avoid overwriting custom subclasses. "generate_base_only": false } -} \ No newline at end of file +}