From 71faac9fb26cdbe65cce194301f7cb42550b4d65 Mon Sep 17 00:00:00 2001 From: antao Date: Fri, 7 Dec 2018 19:08:17 +0800 Subject: [PATCH] Modify the drogon_ctl --- drogon_ctl/create_model.h | 2 +- drogon_ctl/create_view.cc | 7 +++++++ drogon_ctl/templates/model_cc.csp | 32 +++++++++++++++---------------- drogon_ctl/templates/model_h.csp | 6 +++--- 4 files changed, 27 insertions(+), 20 deletions(-) diff --git a/drogon_ctl/create_model.h b/drogon_ctl/create_model.h index 37b70a94..aa23199a 100644 --- a/drogon_ctl/create_model.h +++ b/drogon_ctl/create_model.h @@ -38,7 +38,7 @@ struct ColumnInfo std::string _colType; std::string _colDatabaseType; std::string _dbType; - ssize_t _colLength = -1; + ssize_t _colLength = 0; size_t _index = 0; bool _isAutoVal = false; bool _isPrimaryKey = false; diff --git a/drogon_ctl/create_view.cc b/drogon_ctl/create_view.cc index 32946fc2..eee8f03b 100755 --- a/drogon_ctl/create_view.cc +++ b/drogon_ctl/create_view.cc @@ -18,6 +18,8 @@ #include #include #include +#include + static const std::string cxx_include = "<%inc"; static const std::string cxx_end = "%>"; static const std::string cxx_lang = "<%c++"; @@ -362,6 +364,11 @@ void create_view::newViewSourceFile(std::ofstream &file, const std::string &clas while (infile.getline(line, sizeof(line))) { buffer = line; + if (buffer.length() > 0) + { + std::regex re("\\{%[ \\t]*([^ \\t%]*)[^%]*%\\}"); + buffer = std::regex_replace(buffer, re, "<%c++$$$$<<$1;%>"); + } parseLine(file, buffer, streamName, viewDataName, cxx_flag); } diff --git a/drogon_ctl/templates/model_cc.csp b/drogon_ctl/templates/model_cc.csp index 588303d8..46fb9b77 100644 --- a/drogon_ctl/templates/model_cc.csp +++ b/drogon_ctl/templates/model_cc.csp @@ -20,7 +20,7 @@ using namespace drogon_model::{{dbName}}; <%c++for(auto col:cols){ %> -const std::string {{className}}::Cols::<%c++$$< = "<%c++$$<"; +const std::string {{className}}::Cols::{%col._colName%} = "{%col._colName%}"; <%c++ }%> <%c++if(@@.get("hasPrimaryKey")<=1){%> @@ -66,7 +66,7 @@ const std::string &{{className}}::getColumnName(size_t index) noexcept(false) if(col._colType.empty()) continue; %> - if(!r["<%c++$$<"].isNull()) + if(!r["{%col._colName%}"].isNull()) { <%c++ if(col._colDatabaseType=="date") @@ -116,7 +116,7 @@ const std::string &{{className}}::getColumnName(size_t index) noexcept(false) continue; } %> - _<%c++$$<=std::make_shared<<%c++$$<>(r["<%c++$$<"].as<<%c++$$<>()); + _{%col._colValName%}=std::make_shared<{%col._colType%}>(r["{%col._colName%}"].as<{%col._colType%}>()); } <%c++ } @@ -257,9 +257,9 @@ void {{className}}::outputArgs(drogon::orm::internal::SqlBinder &binder) const if(!col._isAutoVal&&!col._colType.empty()) { %> - if(get<%c++$$<()) + if(get{%col._colTypeName%}()) { - binder << getValueOf<%c++$$<(); + binder << getValueOf{%col._colTypeName%}(); } else { @@ -293,11 +293,11 @@ void {{className}}::updateArgs(drogon::orm::internal::SqlBinder &binder) const if(col._colType.empty()||col._isAutoVal) continue; %> - if(_dirtyFlag[<%c++$$<]) + if(_dirtyFlag[{%i%}]) { - if(get<%c++$$<()) + if(get{%col._colTypeName%}()) { - binder << getValueOf<%c++$$<(); + binder << getValueOf{%col._colTypeName%}(); } else { @@ -312,25 +312,25 @@ Json::Value {{className}}::toJson() const { Json::Value ret; <%c++for(auto col:cols){%> - if(get<%c++$$<()) + if(get{%col._colTypeName%}()) { <%c++if(col._colDatabaseType=="date"){%> - ret["<%c++$$<"]=get<%c++$$<()->toDbStringLocal(); + ret["{%col._colName%}"]=get{%col._colTypeName%}()->toDbStringLocal(); <%c++}else if(col._colDatabaseType.find("timestamp")!=std::string::npos||col._colDatabaseType.find("datetime")!=std::string::npos){%> - ret["<%c++$$<"]=get<%c++$$<()->toDbStringLocal(); + ret["{%col._colName%}"]=get{%col._colTypeName%}()->toDbStringLocal(); <%c++}else if(col._colDatabaseType=="bytea"||col._colDatabaseType.find("blob")!=std::string::npos){%> - ret["<%c++$$<"]=drogon::base64Encode((const unsigned char *)get<%c++$$<()->data(),get<%c++$$<()->size()); + ret["{%col._colName%}"]=drogon::base64Encode((const unsigned char *)get{%col._colTypeName%}()->data(),get{%col._colTypeName%}()->size()); <%c++}else if(col._colType=="int64_t"){%> - ret["<%c++$$<"]=(Json::Int64)getValueOf<%c++$$<(); + ret["{%col._colName%}"]=(Json::Int64)getValueOf{%col._colTypeName%}(); <%c++}else if(col._colType=="uint64_t"){%> - ret["<%c++$$<"]=(Json::UInt64)getValueOf<%c++$$<(); + ret["{%col._colName%}"]=(Json::UInt64)getValueOf{%col._colTypeName%}(); <%c++}else{%> - ret["<%c++$$<"]=getValueOf<%c++$$<(); + ret["{%col._colName%}"]=getValueOf{%col._colTypeName%}(); <%c++}%> } else { - ret["<%c++$$<"]=Json::Value(); + ret["{%col._colName%}"]=Json::Value(); } <%c++ }%> diff --git a/drogon_ctl/templates/model_h.csp b/drogon_ctl/templates/model_h.csp index 83559154..057bb4df 100644 --- a/drogon_ctl/templates/model_h.csp +++ b/drogon_ctl/templates/model_h.csp @@ -65,7 +65,7 @@ auto cols=@@.get>("columns"); } %> const static std::vector primaryKeyName; - typedef std::tuple<<%c++$$<> PrimaryKeyType;//<%c++ + typedef std::tuple<{%typelist%}> PrimaryKeyType;//<%c++ auto pkName=@@.get>("primaryKeyName"); for(size_t i=0;i>("columns"); } %> - static size_t getColumnNumber() noexcept { return <%c++$$<; } + static size_t getColumnNumber() noexcept { return {% cols.size() %}; } static const std::string &getColumnName(size_t index) noexcept(false); Json::Value toJson() const; @@ -144,7 +144,7 @@ auto cols=@@.get>("columns"); const bool _notNull; }; static const std::vector _metaData; - bool _dirtyFlag[<%c++$$<]={ false }; + bool _dirtyFlag[{%cols.size()%}]={ false }; }; } // namespace {{dbName}}