drogon/drogon_ctl/templates/model_cc.csp

328 lines
12 KiB
Plaintext
Raw Normal View History

/**
*
* {{className}}.cc
* created by drogon_ctl
*
*/
<%inc#include "create_model.h"
using namespace drogon_ctl;
%>
#include "{{className}}.h"
#include <drogon/utils/Utilities.h>
#include <string>
2018-11-01 17:58:32 +08:00
<%c++
auto cols=@@.get<std::vector<ColumnInfo>>("columns");
auto className=@@.get<std::string>("className");
%>
2018-10-31 19:07:37 +08:00
using namespace drogon_model::{{dbName}};
2018-11-01 17:58:32 +08:00
<%c++for(auto col:cols){
%>
const std::string {{className}}::Cols::<%c++$$<<col._colName;%> = "<%c++$$<<col._colName;%>";
<%c++
}%>
2018-11-07 18:46:16 +08:00
<%c++if(@@.get<int>("hasPrimaryKey")<=1){%>
2018-11-01 17:58:32 +08:00
const std::string {{className}}::primaryKeyName = "{{primaryKeyName}}";
2018-11-07 18:46:16 +08:00
<%c++}else{%>
const std::vector<std::string> {{className}}::primaryKeyName = {<%c++
auto pkName=@@.get<std::vector<std::string>>("primaryKeyName");
2018-11-09 15:55:20 +08:00
for(size_t i=0;i<pkName.size();i++)
2018-11-07 18:46:16 +08:00
{
$$<<"\""<<pkName[i]<<"\"";
if(i<(pkName.size()-1))
$$<<",";
}
%>};
<%c++}%>
<%c++ if(@@.get<int>("hasPrimaryKey",0)>0){%>
2018-10-31 19:07:37 +08:00
const bool {{className}}::hasPrimaryKey = true;
<%c++ }else{%>
2018-10-31 19:07:37 +08:00
const bool {{className}}::hasPrimaryKey = false;
<%c++}%>
2018-10-31 19:07:37 +08:00
const std::string {{className}}::tableName = "{{tableName}}";
2018-11-01 17:58:32 +08:00
2018-11-01 15:40:24 +08:00
const std::vector<typename {{className}}::MetaData> {{className}}::_metaData={
2018-11-01 22:14:53 +08:00
<%c++for(size_t i=0;i<cols.size();i++){
2018-11-01 15:40:24 +08:00
auto &col=cols[i];
2018-11-05 16:00:49 +08:00
$$<<"{\""<<col._colName<<"\",\""<<col._colType<<"\",\""<<col._colDatabaseType<<"\","<<col._colLength<<","<<col._isAutoVal<<","<<col._isPrimaryKey<<","<<col._notNull<<"}";
2018-11-01 15:40:24 +08:00
if(i<(cols.size()-1))
$$<<",\n";
else
$$<<"\n";
}%>
};
2018-11-11 18:51:39 +08:00
const std::string &{{className}}::getColumnName(size_t index) noexcept(false)
2018-11-01 15:40:24 +08:00
{
assert(index < _metaData.size());
return _metaData[index]._colName;
}
{{className}}::{{className}}(const Row &r) noexcept
{
<%c++
for(auto col:cols)
{
2018-10-31 17:32:46 +08:00
if(col._colType.empty())
continue;
%>
if(!r["<%c++$$<<col._colName;%>"].isNull())
{
2018-11-05 16:00:49 +08:00
<%c++
if(col._colDatabaseType=="date")
{
2018-11-08 11:05:50 +08:00
$$<<" auto daysStr = r[\""<<col._colName<<"\"].as<std::string>();\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";
// $$<<" _"<<col._colValName<<"=std::make_shared<::trantor::Date>(::trantor::Date(946656000000000).after(daysNum*86400));\n";
$$<<" _"<<col._colValName<<"=std::make_shared<::trantor::Date>(t*1000000);\n";
2018-11-05 16:00:49 +08:00
$$<<" }\n";
continue;
2018-11-08 16:31:39 +08:00
}
else if(col._colDatabaseType.find("timestamp")!=std::string::npos)
{
$$<<" auto timeStr = r[\""<<col._colName<<"\"].as<std::string>();\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";
// $$<<" _"<<col._colValName<<"=std::make_shared<::trantor::Date>(::trantor::Date(946656000000000).after(daysNum*86400));\n";
$$<<" _"<<col._colValName<<"=std::make_shared<::trantor::Date>(t*1000000+decimalNum);\n";
$$<<" }\n";
continue;
}
else if(col._colDatabaseType=="bytea")
{
$$<<" auto str = r[\""<<col._colName<<"\"].as<std::string>();\n";
$$<<" if(str.length()>=2&&\n";
$$<<" str[0]=='\\\\'&&str[1]=='x')\n";
$$<<" {\n";
$$<<" auto binStr=drogon::hexToBinaryString((unsigned char *)str.c_str()+2,str.length()-2);\n";
2018-11-20 10:54:46 +08:00
$$<<" _"<<col._colValName<<"=std::make_shared<std::vector<char>>((char *)binStr.data(),(char *)binStr.data()+binStr.length());\n";
$$<<" }\n";
$$<<" }\n";
continue;
}
2018-11-05 16:00:49 +08:00
%>
_<%c++$$<<col._colValName;%>=std::make_shared<<%c++$$<<col._colType;%>>(r["<%c++$$<<col._colName;%>"].as<<%c++$$<<col._colType;%>>());
}
2018-10-31 17:32:46 +08:00
<%c++
}
%>
}
<%c++
2018-11-01 22:14:53 +08:00
for(size_t i=0;i<cols.size();i++)
{
2018-11-01 15:40:24 +08:00
auto & col = cols[i];
if(!col._colType.empty())
{
2018-10-31 19:07:37 +08:00
$$<<"const "<<col._colType<<" & "<<className<<"::getValueOf"<<col._colTypeName<<"(const "<<col._colType<<" &defaultValue) const noexcept\n";
$$<<"{\n";
$$<<" if(_"<<col._colValName<<")\n";
$$<<" return *_"<<col._colValName<<";\n";
2018-10-31 19:07:37 +08:00
$$<<" return defaultValue;\n";
$$<<"}\n";
2018-11-20 10:54:46 +08:00
if(col._colType=="std::vector<char>")
{
$$<<"std::string "<<className<<"::getValueOf"<<col._colTypeName<<"AsString(const std::string &defaultValue) const noexcept\n";
$$<<"{\n";
$$<<" if(_"<<col._colValName<<")\n";
$$<<" return std::string(_"<<col._colValName<<"->data(),_"<<col._colValName<<"->size());\n";
$$<<" return defaultValue;\n";
$$<<"}\n";
}
$$<<"std::shared_ptr<const "<<col._colType<<"> "<<className<<"::get"<<col._colTypeName<<"() const noexcept\n";
$$<<"{\n";
$$<<" return _"<<col._colValName<<";\n";
$$<<"}\n";
if(!col._isAutoVal)
{
2018-11-01 17:58:32 +08:00
$$<<"void "<<className<<"::set"<<col._colTypeName<<"(const "<<col._colType<<" &"<<col._colValName<<") noexcept\n";
$$<<"{\n";
2018-11-05 16:00:49 +08:00
if(col._colDatabaseType=="date")
{
$$<<" _"<<col._colValName<<" = std::make_shared<"<<col._colType<<">("<<col._colValName<<".roundDay());\n";
}
else
{
$$<<" _"<<col._colValName<<" = std::make_shared<"<<col._colType<<">("<<col._colValName<<");\n";
}
2018-11-01 15:40:24 +08:00
$$<<" _dirtyFlag["<<i<<"] = true;\n";
$$<<"}\n";
2018-11-01 17:58:32 +08:00
if(col._colType=="std::string")
{
$$<<"void "<<className<<"::set"<<col._colTypeName<<"("<<col._colType<<" &&"<<col._colValName<<") noexcept\n";
$$<<"{\n";
$$<<" _"<<col._colValName<<" = std::make_shared<"<<col._colType<<">(std::move("<<col._colValName<<"));\n";
$$<<" _dirtyFlag["<<i<<"] = true;\n";
$$<<"}\n";
}
2018-11-20 10:54:46 +08:00
if(col._colType=="std::vector<char>")
{
$$<<"void "<<className<<"::set"<<col._colTypeName<<"(const std::string &"<<col._colValName<<") noexcept\n";
$$<<"{\n";
$$<<" _"<<col._colValName<<" = std::make_shared<std::vector<char>>("<<col._colValName<<".c_str(),"<<col._colValName<<".c_str()+"<<col._colValName<<".length());\n";
$$<<" _dirtyFlag["<<i<<"] = true;\n";
$$<<"}\n";
}
}
2018-11-07 18:46:16 +08:00
if(col._isPrimaryKey&&@@.get<int>("hasPrimaryKey")==1)
2018-11-01 17:58:32 +08:00
{
$$<<"const typename "<<className<<"::PrimaryKeyType & "<<className<<"::getPrimaryKey() const\n";
$$<<"{\n";
$$<<" assert(_"<<col._colValName<<");\n";
$$<<" return *_"<<col._colValName<<";\n";
$$<<"}\n";
}
}
$$<<"\n";
}
2018-11-07 18:46:16 +08:00
if(@@.get<int>("hasPrimaryKey")>1)
{
$$<<"typename "<<className<<"::PrimaryKeyType "<<className<<"::getPrimaryKey() const\n";
$$<<"{\n";
$$<<" return std::make_tuple(";
int count=0;
for(auto col:cols)
{
if(col._isPrimaryKey)
{
count++;
$$<<"*_"<<col._colValName;
if(count<@@.get<int>("hasPrimaryKey"))
$$<<",";
}
}
$$<<");\n";
$$<<"}\n";
}
2018-11-01 15:40:24 +08:00
%>
const std::vector<std::string> &{{className}}::insertColumns() noexcept
{
static const std::vector<std::string> _inCols={
2018-11-01 22:14:53 +08:00
<%c++for(size_t i=0;i<cols.size();i++){
2018-11-01 15:40:24 +08:00
auto col=cols[i];
if(!col._isAutoVal&&!col._colType.empty())
{
$$<<" \""<<col._colName<<"\"";
if(i<(cols.size()-1))
$$<<",\n";
else
$$<<"\n";
}
}%>
};
return _inCols;
}
void {{className}}::outputArgs(drogon::orm::internal::SqlBinder &binder) const
{
<%c++for(auto col:cols){
if(!col._isAutoVal&&!col._colType.empty())
{
%>
if(get<%c++$$<<col._colTypeName;%>())
{
2018-11-05 16:00:49 +08:00
<%c++if(col._colDatabaseType=="date"){%>
binder << (int32_t)(getValueOf<%c++$$<<col._colTypeName;%>().microSecondsSinceEpoch()/1000000-946656000)/86400;
2018-11-08 16:31:39 +08:00
<%c++}else if(col._colDatabaseType.find("timestamp")!=std::string::npos){%>
binder << get<%c++$$<<col._colTypeName;%>()->toCustomedFormattedStringLocal("%Y-%m-%d %H:%M:%S", true);
2018-11-05 16:00:49 +08:00
<%c++}else{%>
2018-11-01 15:40:24 +08:00
binder << getValueOf<%c++$$<<col._colTypeName;%>();
2018-11-05 16:00:49 +08:00
<%c++}%>
2018-11-01 15:40:24 +08:00
}
else
{
binder << nullptr;
}
<%c++
}
}
%>
}
const std::vector<std::string> {{className}}::updateColumns() const
{
std::vector<std::string> ret;
2018-11-01 22:14:53 +08:00
for(size_t i=0;i<sizeof(_dirtyFlag);i++)
2018-11-01 15:40:24 +08:00
{
if(_dirtyFlag[i])
{
ret.push_back(getColumnName(i));
}
}
return ret;
}
void {{className}}::updateArgs(drogon::orm::internal::SqlBinder &binder) const
{
<%c++
2018-11-01 22:14:53 +08:00
for(size_t i=0;i<cols.size();i++)
2018-11-01 15:40:24 +08:00
{
auto & col=cols[i];
if(col._colType.empty()||col._isAutoVal)
continue;
%>
if(_dirtyFlag[<%c++$$<<i;%>])
{
if(get<%c++$$<<col._colTypeName;%>())
{
2018-11-05 16:00:49 +08:00
<%c++if(col._colDatabaseType=="date"){%>
binder << (int32_t)(getValueOf<%c++$$<<col._colTypeName;%>().microSecondsSinceEpoch()/1000000-946656000)/86400;
2018-11-08 16:31:39 +08:00
<%c++}else if(col._colDatabaseType.find("timestamp")!=std::string::npos){%>
binder << get<%c++$$<<col._colTypeName;%>()->toCustomedFormattedStringLocal("%Y-%m-%d %H:%M:%S", true);
2018-11-05 16:00:49 +08:00
<%c++}else{%>
2018-11-01 15:40:24 +08:00
binder << getValueOf<%c++$$<<col._colTypeName;%>();
2018-11-05 16:00:49 +08:00
<%c++}%>
2018-11-01 15:40:24 +08:00
}
else
{
binder << nullptr;
}
}
<%c++
}
%>
2018-11-07 18:46:16 +08:00
}
Json::Value {{className}}::toJson() const
{
Json::Value ret;
<%c++for(auto col:cols){%>
if(get<%c++$$<<col._colTypeName;%>())
{
<%c++if(col._colDatabaseType=="date"){%>
ret["<%c++$$<<col._colName;%>"]=get<%c++$$<<col._colTypeName;%>()->toCustomedFormattedStringLocal("%Y-%m-%d", false);
<%c++}else if(col._colDatabaseType.find("timestamp")!=std::string::npos){%>
ret["<%c++$$<<col._colName;%>"]=get<%c++$$<<col._colTypeName;%>()->toCustomedFormattedStringLocal("%Y-%m-%d %H:%M:%S", true);
<%c++}else if(col._colDatabaseType=="bytea"){%>
ret["<%c++$$<<col._colName;%>"]=drogon::base64Encode((const unsigned char *)get<%c++$$<<col._colTypeName;%>()->data(),get<%c++$$<<col._colTypeName;%>()->size());
<%c++}else{%>
ret["<%c++$$<<col._colName;%>"]=getValueOf<%c++$$<<col._colTypeName;%>();
<%c++}%>
}
else
{
ret["<%c++$$<<col._colName;%>"]=Json::Value();
}
<%c++
}%>
return ret;
}