diff --git a/drogon_ctl/create_model.cc b/drogon_ctl/create_model.cc index 7e9d3a7c..6201229a 100644 --- a/drogon_ctl/create_model.cc +++ b/drogon_ctl/create_model.cc @@ -137,12 +137,12 @@ void create_model::createModelClassFromPG(const std::string &path, const DbClien } else if (type == "bytea") { - info._colType = "std::string"; + info._colType = "std::vector"; } else { info._colType = "std::string"; - //FIXME add more type such as hstore,blob... + //FIXME add more type such as hstore... } auto defaultVal = row["column_default"].as(); diff --git a/drogon_ctl/templates/model_cc.csp b/drogon_ctl/templates/model_cc.csp index 091f2cd0..262cadee 100644 --- a/drogon_ctl/templates/model_cc.csp +++ b/drogon_ctl/templates/model_cc.csp @@ -110,7 +110,7 @@ const std::string &{{className}}::getColumnName(size_t index) noexcept(false) $$<<" str[0]=='\\\\'&&str[1]=='x')\n"; $$<<" {\n"; $$<<" auto binStr=drogon::hexToBinaryString((unsigned char *)str.c_str()+2,str.length()-2);\n"; - $$<<" _"<(std::move(binStr));\n"; + $$<<" _"<>((char *)binStr.data(),(char *)binStr.data()+binStr.length());\n"; $$<<" }\n"; $$<<" }\n"; continue; @@ -134,7 +134,15 @@ const std::string &{{className}}::getColumnName(size_t index) noexcept(false) $$<<" return *_"<") + { + $$<<"std::string "<data(),_"<size());\n"; + $$<<" return defaultValue;\n"; + $$<<"}\n"; + } $$<<"std::shared_ptr "<") + { + $$<<"void "<>("<("hasPrimaryKey")==1) { @@ -226,8 +243,6 @@ void {{className}}::outputArgs(drogon::orm::internal::SqlBinder &binder) const binder << (int32_t)(getValueOf<%c++$$<().microSecondsSinceEpoch()/1000000-946656000)/86400; <%c++}else if(col._colDatabaseType.find("timestamp")!=std::string::npos){%> binder << get<%c++$$<()->toCustomedFormattedStringLocal("%Y-%m-%d %H:%M:%S", true); -<%c++}else if(col._colDatabaseType=="bytea"){%> - binder << std::string("\\x")+drogon::binaryStringToHex((unsigned char *)getValueOf<%c++$$<().c_str(),getValueOf<%c++$$<().length()); <%c++}else{%> binder << getValueOf<%c++$$<(); <%c++}%> @@ -272,8 +287,6 @@ void {{className}}::updateArgs(drogon::orm::internal::SqlBinder &binder) const binder << (int32_t)(getValueOf<%c++$$<().microSecondsSinceEpoch()/1000000-946656000)/86400; <%c++}else if(col._colDatabaseType.find("timestamp")!=std::string::npos){%> binder << get<%c++$$<()->toCustomedFormattedStringLocal("%Y-%m-%d %H:%M:%S", true); -<%c++}else if(col._colDatabaseType=="bytea"){%> - binder << std::string("\\x")+drogon::binaryStringToHex((unsigned char *)getValueOf<%c++$$<().c_str(),getValueOf<%c++$$<().length()); <%c++}else{%> binder << getValueOf<%c++$$<(); <%c++}%> diff --git a/drogon_ctl/templates/model_h.csp b/drogon_ctl/templates/model_h.csp index ee6558ed..bc37a1a1 100644 --- a/drogon_ctl/templates/model_h.csp +++ b/drogon_ctl/templates/model_h.csp @@ -84,12 +84,20 @@ auto cols=@@.get>("columns"); if(!col._colType.empty()) { $$<<" const "<") + { + $$<<" std::string getValueOf"< get"<") + { + $$<<" void set"<(obj) = ntohs(parameter); break; case 4: - *std::static_pointer_cast(obj) = ntohl(parameter); + *std::static_pointer_cast(obj) = ntohl(parameter); break; case 8: *std::static_pointer_cast(obj) = ntohll(parameter); @@ -265,21 +265,19 @@ class SqlBinder //template <> self &operator<<(const char str[]) { - _paraNum++; - _parameters.push_back((char *)str); - _length.push_back(strlen(str)); - _format.push_back(0); - return *this; + return operator<<(std::string(str)); } self &operator<<(char str[]) { - return operator<<((const char *)str); + return operator<<(std::string(str)); } self &operator<<(const std::string &str) { + std::shared_ptr obj = std::make_shared(str); + _objs.push_back(obj); _paraNum++; - _parameters.push_back((char *)str.c_str()); - _length.push_back(str.length()); + _parameters.push_back((char *)obj->c_str()); + _length.push_back(obj->length()); _format.push_back(0); return *this; } @@ -305,6 +303,26 @@ class SqlBinder { return operator<<(date.toCustomedFormattedStringLocal("%Y-%m-%d %H:%M:%S", true)); //for postgreSQL } + self &operator<<(const std::vector &v) + { + std::shared_ptr> obj = std::make_shared>(v); + _objs.push_back(obj); + _paraNum++; + _parameters.push_back((char *)obj->data()); + _length.push_back(obj->size()); + _format.push_back(1); + return *this; + } + self &operator<<(std::vector &&v) + { + std::shared_ptr> obj = std::make_shared>(std::move(v)); + _objs.push_back(obj); + _paraNum++; + _parameters.push_back((char *)obj->data()); + _length.push_back(obj->size()); + _format.push_back(1); + return *this; + } self &operator<<(std::nullptr_t nullp) { _paraNum++; @@ -323,6 +341,7 @@ class SqlBinder _mode = mode; return *this; } + void exec() noexcept(false); private: