Add the toJson() method to the database model

This commit is contained in:
antao 2018-11-20 14:08:08 +08:00
parent 972b0bf600
commit fe16066a4b
4 changed files with 30 additions and 2 deletions

View File

@ -300,3 +300,28 @@ void {{className}}::updateArgs(drogon::orm::internal::SqlBinder &binder) const
} }
%> %>
} }
Json::Value {{className}}::toJson() const
{
Json::Value ret;
<%c++for(auto col:cols){%>
if(get<%c++$$<<col._colTypeName;%>())
{
<%c++if(col._colDatabaseType=="date"){%>
ret["<%c++$$<<col._colName;%>"]=get<%c++$$<<col._colTypeName;%>()->toCustomedFormattedStringLocal("%Y-%m-%d", false);
<%c++}else if(col._colDatabaseType.find("timestamp")!=std::string::npos){%>
ret["<%c++$$<<col._colName;%>"]=get<%c++$$<<col._colTypeName;%>()->toCustomedFormattedStringLocal("%Y-%m-%d %H:%M:%S", true);
<%c++}else if(col._colDatabaseType=="bytea"){%>
ret["<%c++$$<<col._colName;%>"]=drogon::base64Encode((const unsigned char *)get<%c++$$<<col._colTypeName;%>()->data(),get<%c++$$<<col._colTypeName;%>()->size());
<%c++}else{%>
ret["<%c++$$<<col._colName;%>"]=getValueOf<%c++$$<<col._colTypeName;%>();
<%c++}%>
}
else
{
ret["<%c++$$<<col._colName;%>"]=Json::Value();
}
<%c++
}%>
return ret;
}

View File

@ -13,6 +13,7 @@ using namespace drogon_ctl;
#include <drogon/orm/Field.h> #include <drogon/orm/Field.h>
#include <drogon/orm/SqlBinder.h> #include <drogon/orm/SqlBinder.h>
#include <trantor/utils/Date.h> #include <trantor/utils/Date.h>
#include <json/json.h>
#include <string> #include <string>
#include <memory> #include <memory>
#include <vector> #include <vector>
@ -112,6 +113,8 @@ auto cols=@@.get<std::vector<ColumnInfo>>("columns");
const std::vector<std::string> updateColumns() const; const std::vector<std::string> updateColumns() const;
void updateArgs(drogon::orm::internal::SqlBinder &binder) const; void updateArgs(drogon::orm::internal::SqlBinder &binder) const;
Json::Value toJson() const;
private: private:
<%c++ <%c++
for(auto col:cols) for(auto col:cols)

View File

@ -27,7 +27,7 @@ std::string binaryStringToHex(unsigned char *ptr, size_t length);
std::string hexToBinaryString(unsigned char *ptr, size_t length); std::string hexToBinaryString(unsigned char *ptr, size_t length);
std::vector<std::string> splitString(const std::string &str, const std::string &separator); std::vector<std::string> splitString(const std::string &str, const std::string &separator);
std::string getuuid(); std::string getuuid();
std::string base64Encode(unsigned char const *bytes_to_encode, unsigned int in_len); std::string base64Encode(const unsigned char *bytes_to_encode, unsigned int in_len);
std::string base64Decode(std::string const &encoded_string); std::string base64Decode(std::string const &encoded_string);
std::string urlDecode(const std::string &szToDecode); std::string urlDecode(const std::string &szToDecode);
int gzipCompress(const char *data, const size_t ndata, int gzipCompress(const char *data, const size_t ndata,

View File

@ -170,7 +170,7 @@ std::string getuuid()
return binaryStringToHex(uu, 16); return binaryStringToHex(uu, 16);
} }
std::string base64Encode(unsigned char const *bytes_to_encode, unsigned int in_len) std::string base64Encode(const unsigned char *bytes_to_encode, unsigned int in_len)
{ {
std::string ret; std::string ret;
int i = 0; int i = 0;