drogon/drogon_ctl/templates/model_h.csp
2019-03-07 11:56:20 +08:00

152 lines
4.7 KiB
Plaintext

<%inc#include "create_model.h"
using namespace drogon_ctl;
%>
/**
*
* {{className}}.h
* DO NOT EDIT. This file is generated by drogon_ctl
*
*/
#pragma once
#include <drogon/orm/Result.h>
#include <drogon/orm/Row.h>
#include <drogon/orm/Field.h>
#include <drogon/orm/SqlBinder.h>
#include <drogon/orm/Mapper.h>
#include <trantor/utils/Date.h>
#include <json/json.h>
#include <string>
#include <memory>
#include <vector>
#include <tuple>
#include <stdint.h>
#include <iostream>
using namespace drogon::orm;
namespace drogon_model
{
namespace {{dbName}}
{
class {{className}}
{
public:
struct Cols
{
<%c++
auto cols=@@.get<std::vector<ColumnInfo>>("columns");
for(size_t i=0;i<cols.size();i++)
{
$$<<" static const std::string "<<cols[i]._colName<<";\n";
}
%>
};
const static int primaryKeyNumber;
const static std::string tableName;
const static bool hasPrimaryKey;
<%c++if(@@.get<int>("hasPrimaryKey")<=1){%>
const static std::string primaryKeyName;
<%c++if(!@@.get<std::string>("primaryKeyType").empty()){%>
typedef {{primaryKeyType}} PrimaryKeyType;
const PrimaryKeyType & getPrimaryKey() const;
<%c++}else{%>
typedef void PrimaryKeyType;
<%c++}%>
<%c++}else{
auto pkTypes=@@.get<std::vector<std::string>>("primaryKeyType");
std::string typelist;
for(size_t i=0;i<pkTypes.size();i++)
{
typelist += pkTypes[i];
if(i<(pkTypes.size()-1))
typelist += ",";
}
%>
const static std::vector<std::string> primaryKeyName;
typedef std::tuple<{%typelist%}> PrimaryKeyType;//<%c++
auto pkName=@@.get<std::vector<std::string>>("primaryKeyName");
for(size_t i=0;i<pkName.size();i++)
{
$$<<pkName[i];
if(i<(pkName.size()-1))
$$<<",";
}
%>
PrimaryKeyType getPrimaryKey() const;
<%c++}%>
{{className}}(const Row &r) noexcept;
{{className}}() = default;
<%c++
for(auto col:cols)
{
$$<<" /** For column "<<col._colName<<" */\n";
if(!col._colType.empty())
{
$$<<" ///Get the value of the column "<<col._colName<<", returns the default value if the column is null\n";
$$<<" const "<<col._colType<<" &getValueOf"<<col._colTypeName<<"(const "<<col._colType<<" &defaultValue="<<col._colType<<"()) const noexcept;\n";
if(col._colType=="std::vector<char>")
{
$$<<" ///Returns the column value by std::string with binary data\n";
$$<<" std::string getValueOf"<<col._colTypeName<<"AsString(const std::string &defaultValue=\"\") const noexcept;\n";
}
$$<<" ///Returns a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null\n";
$$<<" std::shared_ptr<const "<<col._colType<<"> get"<<col._colTypeName<<"() const noexcept;\n";
if(!col._isAutoVal)
{
$$<<" ///Set the value of the column "<<col._colName<<"\n";
$$<<" void set"<<col._colTypeName<<"(const "<<col._colType<<" &"<<col._colValName<<") noexcept;\n";
if(col._colType=="std::string")
$$<<" void set"<<col._colTypeName<<"("<<col._colType<<" &&"<<col._colValName<<") noexcept;\n";
if(col._colType=="std::vector<char>")
{
$$<<" void set"<<col._colTypeName<<"(const std::string &"<<col._colValName<<") noexcept;\n";
}
}
}
else
$$<<" //FIXME!!"<<" getValueOf"<<col._colTypeName<<"() const noexcept;\n";
$$<<"\n";
}
%>
static size_t getColumnNumber() noexcept { return {% cols.size() %}; }
static const std::string &getColumnName(size_t index) noexcept(false);
Json::Value toJson() const;
private:
friend Mapper<{{className}}>;
static const std::vector<std::string> &insertColumns() noexcept;
void outputArgs(drogon::orm::internal::SqlBinder &binder) const;
const std::vector<std::string> updateColumns() const;
void updateArgs(drogon::orm::internal::SqlBinder &binder) const;
///For mysql or sqlite3
void updateId(const uint64_t id);
<%c++
for(auto col:cols)
{
if(!col._colType.empty())
$$<<" std::shared_ptr<"<<col._colType<<"> _"<<col._colValName<<";\n";
}
%>
struct MetaData
{
const std::string _colName;
const std::string _colType;
const std::string _colDatabaseType;
const ssize_t _colLength;
const bool _isAutoVal;
const bool _isPrimaryKey;
const bool _notNull;
};
static const std::vector<MetaData> _metaData;
bool _dirtyFlag[{%cols.size()%}]={ false };
};
} // namespace {{dbName}}
} // namespace drogon_model