drogon/drogon_ctl/templates/model_cc.csp

79 lines
2.4 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 <string>
2018-10-31 19:07:37 +08:00
using namespace drogon_model::{{dbName}};
const std::string {{className}}::primaryKeyName = "{{priKeyName}}";
<%c++ if(@@.get<bool>("hasPrimaryKey",false)){%>
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}}";
<%c++
auto cols=@@.get<std::vector<ColumnInfo>>("columns");
auto className=@@.get<std::string>("className");
%>
{{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())
{
_<%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++
for(auto col:cols)
{
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";
$$<<"std::shared_ptr<const "<<col._colType<<"> "<<className<<"::get"<<col._colTypeName<<"() const noexcept\n";
$$<<"{\n";
$$<<" return _"<<col._colValName<<";\n";
$$<<"}\n";
$$<<"void "<<className<<"::set"<<col._colTypeName<<"(const "<<col._colType<<" &"<<col._colValName<<") noexcept\n";
$$<<"{\n";
$$<<" _"<<col._colValName<<" = std::make_shared<"<<col._colType<<">("<<col._colValName<<");\n";
$$<<"}\n";
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";
$$<<"}\n";
}
}
$$<<"\n";
}
%>