drogon/drogon_ctl/templates/model_h.csp

153 lines
4.7 KiB
Plaintext
Raw Normal View History

2018-11-27 17:37:41 +08:00
<%inc#include "create_model.h"
using namespace drogon_ctl;
%>
/**
*
* [[className]].h
2018-11-27 17:37:41 +08:00
* DO NOT EDIT. This file is generated by drogon_ctl
*
*/
2018-11-27 17:37:41 +08:00
#pragma once
#include <drogon/orm/Result.h>
#include <drogon/orm/Row.h>
#include <drogon/orm/Field.h>
2018-11-01 15:40:24 +08:00
#include <drogon/orm/SqlBinder.h>
2018-11-23 10:31:45 +08:00
#include <drogon/orm/Mapper.h>
2018-11-05 16:00:49 +08:00
#include <trantor/utils/Date.h>
#include <json/json.h>
#include <string>
#include <memory>
2018-11-01 15:40:24 +08:00
#include <vector>
2018-11-07 18:46:16 +08:00
#include <tuple>
2018-10-31 19:07:37 +08:00
#include <stdint.h>
2018-11-07 18:46:16 +08:00
#include <iostream>
using namespace drogon::orm;
namespace drogon_model
{
namespace [[dbName]]
{
class [[className]]
{
2018-10-31 17:32:46 +08:00
public:
2018-11-01 17:58:32 +08:00
struct Cols
2018-11-01 15:40:24 +08:00
{
<%c++
auto cols=@@.get<std::vector<ColumnInfo>>("columns");
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
{
2018-11-01 17:58:32 +08:00
$$<<" static const std::string "<<cols[i]._colName<<";\n";
2018-11-01 15:40:24 +08:00
}
%>
};
2018-11-01 17:58:32 +08:00
2018-11-07 18:46:16 +08:00
const static int primaryKeyNumber;
const static std::string tableName;
2018-11-07 18:46:16 +08:00
const static bool hasPrimaryKey;
<%c++if(@@.get<int>("hasPrimaryKey")<=1){%>
const static std::string primaryKeyName;
2018-10-31 19:07:37 +08:00
<%c++if(!@@.get<std::string>("primaryKeyType").empty()){%>
typedef [[primaryKeyType]] PrimaryKeyType;
2018-11-01 17:58:32 +08:00
const PrimaryKeyType & getPrimaryKey() const;
2018-10-31 19:07:37 +08:00
<%c++}else{%>
typedef void PrimaryKeyType;
2018-11-07 18:46:16 +08:00
<%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;
2018-12-07 19:08:17 +08:00
typedef std::tuple<{%typelist%}> PrimaryKeyType;//<%c++
2018-11-08 11:20:07 +08:00
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-08 11:20:07 +08:00
{
$$<<pkName[i];
if(i<(pkName.size()-1))
$$<<",";
}
%>
2018-11-07 18:46:16 +08:00
PrimaryKeyType getPrimaryKey() const;
2018-10-31 19:07:37 +08:00
<%c++}%>
2019-05-23 18:42:49 +08:00
explicit [[className]](const Row &r) noexcept;
[[className]]() = default;
2018-10-31 17:32:46 +08:00
<%c++
for(auto col:cols)
{
2018-11-23 10:31:45 +08:00
$$<<" /** For column "<<col._colName<<" */\n";
if(!col._colType.empty())
{
2018-11-23 10:31:45 +08:00
$$<<" ///Get the value of the column "<<col._colName<<", returns the default value if the column is null\n";
2018-10-31 19:07:37 +08:00
$$<<" const "<<col._colType<<" &getValueOf"<<col._colTypeName<<"(const "<<col._colType<<" &defaultValue="<<col._colType<<"()) const noexcept;\n";
2018-11-20 10:54:46 +08:00
if(col._colType=="std::vector<char>")
{
2019-04-29 14:47:23 +08:00
$$<<" ///Return the column value by std::string with binary data\n";
2018-11-20 10:54:46 +08:00
$$<<" std::string getValueOf"<<col._colTypeName<<"AsString(const std::string &defaultValue=\"\") const noexcept;\n";
}
2019-04-29 14:47:23 +08:00
$$<<" ///Return 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)
{
2018-11-23 10:31:45 +08:00
$$<<" ///Set the value of the column "<<col._colName<<"\n";
$$<<" void set"<<col._colTypeName<<"(const "<<col._colType<<" &"<<col._colValName<<") noexcept;\n";
2018-11-20 10:54:46 +08:00
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";
}
2018-10-31 17:32:46 +08:00
%>
2018-11-01 15:40:24 +08:00
2018-12-07 19:08:17 +08:00
static size_t getColumnNumber() noexcept { return {% cols.size() %}; }
2018-11-23 10:31:45 +08:00
static const std::string &getColumnName(size_t index) noexcept(false);
2018-11-01 15:40:24 +08:00
Json::Value toJson() const;
private:
friend Mapper<[[className]]>;
2018-11-23 10:31:45 +08:00
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;
2019-03-07 11:56:20 +08:00
///For mysql or sqlite3
2018-12-29 18:02:39 +08:00
void updateId(const uint64_t id);
2018-10-31 17:32:46 +08:00
<%c++
for(auto col:cols)
{
if(!col._colType.empty())
$$<<" std::shared_ptr<"<<col._colType<<"> _"<<col._colValName<<";\n";
}
%>
2018-11-01 15:40:24 +08:00
struct MetaData
{
const std::string _colName;
const std::string _colType;
2018-11-05 16:00:49 +08:00
const std::string _colDatabaseType;
2018-11-01 15:40:24 +08:00
const ssize_t _colLength;
const bool _isAutoVal;
const bool _isPrimaryKey;
const bool _notNull;
};
static const std::vector<MetaData> _metaData;
2018-12-07 19:08:17 +08:00
bool _dirtyFlag[{%cols.size()%}]={ false };
};
} // namespace [[dbName]]
} // namespace drogon_model