drogon/drogon_ctl/templates/model_h.csp
2018-11-09 15:46:39 +08:00

102 lines
2.8 KiB
Plaintext

/**
*
* {{className}}.h
* Created by drogon_ctl
*
*/
<%inc#include "create_model.h"
using namespace 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 <string>
#include <memory>
#include <vector>
#include <stdint.h>
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 std::string primaryKeyName;
const static bool hasPrimaryKey;
const static std::string tableName;
<%c++if(!@@.get<std::string>("primaryKeyType").empty()){%>
typedef {{primaryKeyType}} PrimaryKeyType;
const PrimaryKeyType & getPrimaryKey() const;
<%c++}else{%>
typedef void PrimaryKeyType;
<%c++}%>
{{className}}(const Row &r) noexcept;
<%c++
for(auto col:cols)
{
$$<<" //For column "<<col._colName<<"\n";
if(!col._colType.empty())
{
$$<<" const "<<col._colType<<" &getValueOf"<<col._colTypeName<<"(const "<<col._colType<<" &defaultValue="<<col._colType<<"()) const noexcept;\n";
$$<<" std::shared_ptr<const "<<col._colType<<"> get"<<col._colTypeName<<"() const noexcept;\n";
if(!col._isAutoVal)
{
$$<<" 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";
}
}
else
$$<<" //FIXME!!"<<" getValueOf"<<col._colTypeName<<"() const noexcept;\n";
$$<<"\n";
}
%>
static const std::string &getColumnName(int index) noexcept(false);
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;
private:
<%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 ssize_t _colLength;
const bool _isAutoVal;
const bool _isPrimaryKey;
const bool _notNull;
};
static const std::vector<MetaData> _metaData;
bool _dirtyFlag[<%c++$$<<cols.size();%>]={ false };
};
} // namespace {{dbName}}
} // namespace drogon_model