<%inc#include "create_model.h" using namespace drogon_ctl; %> /** * * [[className]].h * DO NOT EDIT. This file is generated by drogon_ctl * */ #pragma once #include #include #include #include #include #include #ifdef __cpp_impl_coroutine #include #endif #include #include #include #include #include #include #include #include #include #include namespace drogon { namespace orm { class DbClient; using DbClientPtr = std::shared_ptr; } } namespace drogon_model { namespace [[dbName]] { <%c++ auto &schema=@@.get("schema"); if(!schema.empty()) { $$<<"namespace "< relationshipClassNames; auto &relationships=@@.get>("relationships"); for(auto &relationship : relationships) { auto &name = relationship.targetTableName(); auto relationshipClassName = nameTransform(name, true); relationshipClassNames.push_back(relationshipClassName); if(relationship.type() == Relationship::Type::ManyToMany) { auto &pivotTableName = relationship.pivotTable().tableName(); auto pivotTableClassName = nameTransform(pivotTableName, true); relationshipClassNames.push_back(pivotTableClassName); } } std::sort(relationshipClassNames.begin(), relationshipClassNames.end()); relationshipClassNames.erase(std::unique(relationshipClassNames.begin(), relationshipClassNames.end()), relationshipClassNames.end()); for(std::string &relationshipClassName : relationshipClassNames) { %> class {%relationshipClassName%}; <%c++ } %> class [[className]] { public: struct Cols { <%c++ auto cols=@@.get>("columns"); for(size_t i=0;i }; const static int primaryKeyNumber; const static std::string tableName; const static bool hasPrimaryKey; <%c++if(@@.get("hasPrimaryKey")<=1){%> const static std::string primaryKeyName; <%c++if(!@@.get("primaryKeyType").empty()){%> using PrimaryKeyType = [[primaryKeyType]]; const PrimaryKeyType &getPrimaryKey() const; <%c++}else{%> using PrimaryKeyType = void; int getPrimaryKey() const { assert(false); return 0; } <%c++}%> <%c++}else{ auto pkTypes=@@.get>("primaryKeyType"); std::string typelist; for(size_t i=0;i const static std::vector primaryKeyName; using PrimaryKeyType = std::tuple<{%typelist%}>;//<%c++ auto pkName=@@.get>("primaryKeyName"); for(size_t i=0;i PrimaryKeyType getPrimaryKey() const; <%c++}%> /** * @brief constructor * @param r One row of records in the SQL query result. * @param indexOffset Set the offset to -1 to access all columns by column names, * otherwise access all columns by offsets. * @note If the SQL is not a style of 'select * from table_name ...' (select all * columns by an asterisk), please set the offset to -1. */ explicit [[className]](const drogon::orm::Row &r, const ssize_t indexOffset = 0) noexcept; /** * @brief constructor * @param pJson The json object to construct a new instance. */ explicit [[className]](const Json::Value &pJson) noexcept(false); /** * @brief constructor * @param pJson The json object to construct a new instance. * @param pMasqueradingVector The aliases of table columns. */ [[className]](const Json::Value &pJson, const std::vector &pMasqueradingVector) noexcept(false); [[className]]() = default; void updateByJson(const Json::Value &pJson) noexcept(false); void updateByMasqueradedJson(const Json::Value &pJson, const std::vector &pMasqueradingVector) noexcept(false); static bool validateJsonForCreation(const Json::Value &pJson, std::string &err); static bool validateMasqueradedJsonForCreation(const Json::Value &, const std::vector &pMasqueradingVector, std::string &err); static bool validateJsonForUpdate(const Json::Value &pJson, std::string &err); static bool validateMasqueradedJsonForUpdate(const Json::Value &, const std::vector &pMasqueradingVector, std::string &err); static bool validJsonOfField(size_t index, const std::string &fieldName, const Json::Value &pJson, std::string &err, bool isForCreation); <%c++ for(const auto &col:cols) { $$<<" /** For column "<") { $$<<" ///Return the column value by std::string with binary data\n"; $$<<" std::string getValueOf"< &get"<") { $$<<" void set"< static size_t getColumnNumber() noexcept { return {% cols.size() %}; } static const std::string &getColumnName(size_t index) noexcept(false); Json::Value toJson() const; Json::Value toMasqueradedJson(const std::vector &pMasqueradingVector) const; /// Relationship interfaces <%c++ for(auto &relationship : relationships) { if(relationship.targetKey().empty() || relationship.originalKey().empty()) { continue; } auto &name=relationship.targetTableName(); auto relationshipClassName=nameTransform(name, true); auto relationshipValName=nameTransform(name, false); auto alias=relationship.targetTableAlias(); auto aliasValName=nameTransform(alias, false); if(relationship.type() == Relationship::Type::HasOne) { if(!alias.empty()) { if(alias[0] <= 'z' && alias[0] >= 'a') { alias[0] += ('A' - 'a'); } std::string alind(alias.length(), ' '); %> {%relationshipClassName%} get{%alias%}(const drogon::orm::DbClientPtr &clientPtr) const; void get{%alias%}(const drogon::orm::DbClientPtr &clientPtr, {%alind%} const std::function &rcb, {%alind%} const drogon::orm::ExceptionCallback &ecb) const; <%c++ } else { std::string relationshipClassInde(relationshipClassName.length(), ' '); %> {%relationshipClassName%} get{%relationshipClassName%}(const drogon::orm::DbClientPtr &clientPtr) const; void get{%relationshipClassName%}(const drogon::orm::DbClientPtr &clientPtr, {%relationshipClassInde%} const std::function &rcb, {%relationshipClassInde%} const drogon::orm::ExceptionCallback &ecb) const; <%c++ } } else if(relationship.type() == Relationship::Type::HasMany) { if(!alias.empty()) { if(alias[0] <= 'z' && alias[0] >= 'a') { alias[0] += ('A' - 'a'); } std::string alind(alias.length(), ' '); %> std::vector<{%relationshipClassName%}> get{%alias%}(const drogon::orm::DbClientPtr &clientPtr) const; void get{%alias%}(const drogon::orm::DbClientPtr &clientPtr, {%alind%} const std::function)> &rcb, {%alind%} const drogon::orm::ExceptionCallback &ecb) const; <%c++ } else { std::string relationshipClassInde(relationshipClassName.length(), ' '); %> std::vector<{%relationshipClassName%}> get{%relationshipClassName%}(const drogon::orm::DbClientPtr &clientPtr) const; void get{%relationshipClassName%}(const drogon::orm::DbClientPtr &clientPtr, {%relationshipClassInde%} const std::function)> &rcb, {%relationshipClassInde%} const drogon::orm::ExceptionCallback &ecb) const; <%c++ } } else if(relationship.type() == Relationship::Type::ManyToMany) { auto &pivotTableName=relationship.pivotTable().tableName(); auto pivotTableClassName=nameTransform(pivotTableName, true); if(!alias.empty()) { if(alias[0] <= 'z' && alias[0] >= 'a') { alias[0] += ('A' - 'a'); } std::string alind(alias.length(), ' '); %> std::vector> get{%alias%}(const drogon::orm::DbClientPtr &clientPtr) const; void get{%alias%}(const drogon::orm::DbClientPtr &clientPtr, {%alind%} const std::function>)> &rcb, {%alind%} const drogon::orm::ExceptionCallback &ecb) const; <%c++ } else { std::string relationshipClassInde(relationshipClassName.length(), ' '); %> std::vector> get{%relationshipClassName%}(const drogon::orm::DbClientPtr &clientPtr) const; void get{%relationshipClassName%}(const drogon::orm::DbClientPtr &clientPtr, {%relationshipClassInde%} const std::function>)> &rcb, {%relationshipClassInde%} const drogon::orm::ExceptionCallback &ecb) const; <%c++ } } } %> private: friend drogon::orm::Mapper<[[className]]>; friend drogon::orm::BaseBuilder<[[className]], true, true>; friend drogon::orm::BaseBuilder<[[className]], true, false>; friend drogon::orm::BaseBuilder<[[className]], false, true>; friend drogon::orm::BaseBuilder<[[className]], false, false>; #ifdef __cpp_impl_coroutine friend drogon::orm::CoroMapper<[[className]]>; #endif static const std::vector &insertColumns() noexcept; void outputArgs(drogon::orm::internal::SqlBinder &binder) const; const std::vector 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<"< "< 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_; bool dirtyFlag_[{%cols.size()%}]={ false }; public: static const std::string &sqlForFindingByPrimaryKey() { <%c++ auto rdbms=@@.get("rdbms"); if(@@.get("hasPrimaryKey")<=1){ if(!@@.get("primaryKeyType").empty()){%> static const std::string sql="select * from " + tableName + " where [[primaryKeyName]] = {%(rdbms=="postgresql"?"$1":"?")%}"; <%c++}else{%> static const std::string sql=""; <%c++}%> <%c++}else{ auto pkName=@@.get>("primaryKeyName"); %> static const std::string sql="select * from " + tableName + " where <%c++ for(size_t i=0;i return sql; } static const std::string &sqlForDeletingByPrimaryKey() { <%c++ if(@@.get("hasPrimaryKey")<=1){ if(!@@.get("primaryKeyType").empty()){%> static const std::string sql="delete from " + tableName + " where [[primaryKeyName]] = {%(rdbms=="postgresql"?"$1":"?")%}"; <%c++}else{%> static const std::string sql=""; <%c++}%> <%c++}else{ auto pkName=@@.get>("primaryKeyName"); %> static const std::string sql="delete from " + tableName + " where <%c++ for(size_t i=0;i return sql; } std::string sqlForInserting(bool &needSelection) const { std::string sql="insert into " + tableName + " ("; size_t parametersCount = 0; needSelection = false; <%c++ bool selFlag=false; for(size_t i=0;i("rdbms")=="sqlite3") { continue; } if(@@.get("hasPrimaryKey")>0) { selFlag = true; } $$<<" sql += \""<("rdbms")!="sqlite3") { $$<<" sql += \""< if(dirtyFlag_[{%i%}]) { sql += "{%cols[i].colName_%},"; ++parametersCount; } <%c++ } if(@@.get("hasPrimaryKey")>0||@@.get("rdbms")=="postgresql") { %> if(!dirtyFlag_[{%i%}]) { needSelection=true; } <%c++ } } else { %> if(dirtyFlag_[{%i%}]) { sql += "{%cols[i].colName_%},"; ++parametersCount; } <%c++ } } if(selFlag) { $$<<" needSelection=true;\n"; } %> if(parametersCount > 0) { sql[sql.length()-1]=')'; sql += " values ("; } else sql += ") values ("; <%c++ if(@@.get("rdbms")=="postgresql") { %> int placeholder=1; char placeholderStr[64]; size_t n=0; <%c++ } for(size_t i=0;i("rdbms")!="sqlite3") { %> sql +="default,"; <%c++ } continue; } if(cols[i].colType_.empty()) continue; %> if(dirtyFlag_[{%i%}]) { <%c++ if(@@.get("rdbms")=="postgresql") { %> n = snprintf(placeholderStr,sizeof(placeholderStr),"$%d,",placeholder++); sql.append(placeholderStr, n); <%c++ }else { %> sql.append("?,"); <%c++ } %> } <%c++ if(cols[i].hasDefaultVal_&&@@.get("rdbms")!="sqlite3") { %> else { sql +="default,"; } <%c++ } } %> if(parametersCount > 0) { sql.resize(sql.length() - 1); } <%c++ if(rdbms=="postgresql") { %> if(needSelection) { sql.append(") returning *"); } else { sql.append(1, ')'); } <%c++ } else { $$<<" sql.append(1, ')');\n"; } %> LOG_TRACE << sql; return sql; } }; <%c++ if(!schema.empty()) { $$<<"} // namespace "< } // namespace [[dbName]] } // namespace drogon_model