<%inc#include "create_model.h" using namespace drogon_ctl; %> /** * * [[className]].cc * DO NOT EDIT. This file is generated by drogon_ctl * */ #include "[[className]].h" #include #include <%c++ auto cols=@@.get>("columns"); auto className=@@.get("className"); %> using namespace drogon; using namespace drogon_model::[[dbName]]; <%c++for(auto col:cols){ %> const std::string [[className]]::Cols::{%col._colName%} = "{%col._colName%}"; <%c++ }%> <%c++if(@@.get("hasPrimaryKey")<=1){%> const std::string [[className]]::primaryKeyName = "[[primaryKeyName]]"; <%c++}else{%> const std::vector [[className]]::primaryKeyName = {<%c++ auto pkName=@@.get>("primaryKeyName"); for(size_t i=0;i}; <%c++}%> <%c++ if(@@.get("hasPrimaryKey")>0){%> const bool [[className]]::hasPrimaryKey = true; <%c++ }else{%> const bool [[className]]::hasPrimaryKey = false; <%c++}%> const std::string [[className]]::tableName = "[[tableName]]"; const std::vector [[className]]::_metaData={ <%c++for(size_t i=0;i }; const std::string &[[className]]::getColumnName(size_t index) noexcept(false) { assert(index < _metaData.size()); return _metaData[index]._colName; } [[className]]::[[className]](const Row &r) noexcept { <%c++ for(auto col:cols) { if(col._colType.empty()) continue; %> if(!r["{%col._colName%}"].isNull()) { <%c++ if(col._colDatabaseType=="date") { $$<<" auto daysStr = r[\""<();\n"; $$<<" struct tm stm;\n"; $$<<" memset(&stm,0,sizeof(stm));\n"; $$<<" strptime(daysStr.c_str(),\"%Y-%m-%d\",&stm);\n"; $$<<" long t = timelocal(&stm);\n"; // $$<<" _"<(::trantor::Date(946656000000000).after(daysNum*86400));\n"; $$<<" _"<(t*1000000);\n"; $$<<" }\n"; continue; } else if(col._colDatabaseType.find("timestamp")!=std::string::npos||col._colDatabaseType.find("datetime")!=std::string::npos) { $$<<" auto timeStr = r[\""<();\n"; $$<<" struct tm stm;\n"; $$<<" memset(&stm,0,sizeof(stm));\n"; $$<<" auto p = strptime(timeStr.c_str(),\"%Y-%m-%d %H:%M:%S\",&stm);\n"; $$<<" size_t t = timelocal(&stm);\n"; $$<<" size_t decimalNum = 0;\n"; $$<<" if(*p=='.')\n"; $$<<" {\n"; $$<<" std::string decimals(p+1,&timeStr[timeStr.length()]);\n"; $$<<" while(decimals.length()<6)\n"; $$<<" {\n"; $$<<" decimals += \"0\";\n"; $$<<" }\n"; $$<<" decimalNum = (size_t)atol(decimals.c_str());\n"; $$<<" }\n"; // $$<<" _"<(::trantor::Date(946656000000000).after(daysNum*86400));\n"; $$<<" _"<(t*1000000+decimalNum);\n"; $$<<" }\n"; continue; } else if(col._colDatabaseType=="bytea") { $$<<" auto str = r[\""<();\n"; $$<<" if(str.length()>=2&&\n"; $$<<" str[0]=='\\\\'&&str[1]=='x')\n"; $$<<" {\n"; $$<<" _"<>(drogon::utils::hexToBinaryVector(str.data()+2,str.length()-2));\n"; $$<<" }\n"; $$<<" }\n"; continue; } %> _{%col._colValName%}=std::make_shared<{%col._colType%}>(r["{%col._colName%}"].as<{%col._colType%}>()); } <%c++ } %> } <%c++ for(size_t i=0;i") { $$<<"std::string "<data(),_"<size());\n"; $$<<" return defaultValue;\n"; $$<<"}\n"; } $$<<"const std::shared_ptr<"< &"<("<("<(std::move("<") { $$<<"void "<>("<("hasPrimaryKey")==1) { $$<<"const typename "<("rdbms")=="postgresql") { $$<<"void "<("rdbms")=="mysql"||@@.get("rdbms")=="sqlite3") { $$<<"void "<(id);\n"; break; } } $$<<"}\n"; } if(@@.get("hasPrimaryKey")>1) { $$<<"typename "<("hasPrimaryKey")) $$<<","; } } $$<<");\n"; $$<<"}\n"; } %> const std::vector &[[className]]::insertColumns() noexcept { static const std::vector _inCols={ <%c++for(size_t i=0;i }; return _inCols; } void [[className]]::outputArgs(drogon::orm::internal::SqlBinder &binder) const { <%c++for(auto col:cols){ if(!col._isAutoVal&&!col._colType.empty()) { %> if(get{%col._colTypeName%}()) { binder << getValueOf{%col._colTypeName%}(); } else { binder << nullptr; } <%c++ } } %> } const std::vector [[className]]::updateColumns() const { std::vector ret; for(size_t i=0;i if(_dirtyFlag[{%i%}]) { if(get{%col._colTypeName%}()) { binder << getValueOf{%col._colTypeName%}(); } else { binder << nullptr; } } <%c++ } %> } Json::Value [[className]]::toJson() const { Json::Value ret; <%c++for(auto col:cols){%> if(get{%col._colTypeName%}()) { <%c++if(col._colDatabaseType=="date"){%> 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["{%col._colName%}"]=get{%col._colTypeName%}()->toDbStringLocal(); <%c++}else if(col._colDatabaseType=="bytea"||col._colDatabaseType.find("blob")!=std::string::npos){%> ret["{%col._colName%}"]=drogon::utils::base64Encode((const unsigned char *)get{%col._colTypeName%}()->data(),get{%col._colTypeName%}()->size()); <%c++}else if(col._colType=="int64_t"){%> ret["{%col._colName%}"]=(Json::Int64)getValueOf{%col._colTypeName%}(); <%c++}else if(col._colType=="uint64_t"){%> ret["{%col._colName%}"]=(Json::UInt64)getValueOf{%col._colTypeName%}(); <%c++}else{%> ret["{%col._colName%}"]=getValueOf{%col._colTypeName%}(); <%c++}%> } else { ret["{%col._colName%}"]=Json::Value(); } <%c++ }%> return ret; }