From 1cb8b17709abe8f73c6de45f2291e75452e6bb76 Mon Sep 17 00:00:00 2001 From: An Tao Date: Fri, 18 Dec 2020 09:41:58 +0800 Subject: [PATCH] Fix some bugs when creating models via drogon_ctl (#660) --- drogon_ctl/create_model.cc | 15 +++-- drogon_ctl/templates/model_cc.csp | 102 ++++++++++++++++++------------ 2 files changed, 70 insertions(+), 47 deletions(-) diff --git a/drogon_ctl/create_model.cc b/drogon_ctl/create_model.cc index 08a8f42a..f89354b9 100644 --- a/drogon_ctl/create_model.cc +++ b/drogon_ctl/create_model.cc @@ -140,9 +140,7 @@ void create_model::createModelClassFromPG( info.colType_ = "int32_t"; info.colLength_ = 4; } - else if (type == "bigint" || - type == "numeric") /// TODO:Use int64 to represent - /// numeric type? + else if (type == "bigint") { info.colType_ = "int64_t"; info.colLength_ = 8; @@ -181,6 +179,10 @@ void create_model::createModelClassFromPG( { info.colType_ = "std::vector"; } + else if (type.find("numeric") != std::string::npos) + { + info.colType_ = "std::string"; + } else { info.colType_ = "std::string"; @@ -446,7 +448,8 @@ void create_model::createModelClassFromMysql( { info.colType_ = "std::string"; } - if (type.find("unsigned") != std::string::npos) + if (type.find("unsigned") != std::string::npos && + info.colType_ != "std::string") { info.colType_ = "u" + info.colType_; } @@ -466,12 +469,13 @@ void create_model::createModelClassFromMysql( std::cerr << e.base().what() << std::endl; exit(1); }; - std::vector pkNames, pkTypes; + std::vector pkNames, pkTypes, pkValNames; for (auto const &col : cols) { if (col.isPrimaryKey_) { pkNames.push_back(col.colName_); + pkValNames.push_back(nameTransform(col.colName_, false)); pkTypes.push_back(col.colType_); } } @@ -485,6 +489,7 @@ void create_model::createModelClassFromMysql( { data["primaryKeyName"] = pkNames; data["primaryKeyType"] = pkTypes; + data["primaryKeyValNames"] = pkValNames; } data["columns"] = cols; std::ofstream headerFile(path + "/" + className + ".h", std::ofstream::out); diff --git a/drogon_ctl/templates/model_cc.csp b/drogon_ctl/templates/model_cc.csp index cc4310e0..03595ce6 100644 --- a/drogon_ctl/templates/model_cc.csp +++ b/drogon_ctl/templates/model_cc.csp @@ -138,17 +138,20 @@ const std::string &[[className]]::getColumnName(size_t index) noexcept(false) $$<<" auto p = strptime(timeStr.c_str(),\"%Y-%m-%d %H:%M:%S\",&stm);\n"; $$<<" time_t t = mktime(&stm);\n"; $$<<" size_t decimalNum = 0;\n"; - $$<<" if(*p=='.')\n"; + $$<<" if(p)\n"; $$<<" {\n"; - $$<<" std::string decimals(p+1,&timeStr[timeStr.length()]);\n"; - $$<<" while(decimals.length()<6)\n"; + $$<<" if(*p=='.')\n"; $$<<" {\n"; - $$<<" decimals += \"0\";\n"; + $$<<" std::string decimals(p+1,&timeStr[timeStr.length()]);\n"; + $$<<" while(decimals.length()<6)\n"; + $$<<" {\n"; + $$<<" decimals += \"0\";\n"; + $$<<" }\n"; + $$<<" decimalNum = (size_t)atol(decimals.c_str());\n"; $$<<" }\n"; - $$<<" decimalNum = (size_t)atol(decimals.c_str());\n"; - $$<<" }\n"; // $$<<" "<(::trantor::Date(946656000000000).after(daysNum*86400));\n"; - $$<<" "<(t*1000000+decimalNum);\n"; + $$<<" "<(t*1000000+decimalNum);\n"; + $$<<" }\n"; $$<<" }\n"; continue; } @@ -209,17 +212,20 @@ const std::string &[[className]]::getColumnName(size_t index) noexcept(false) $$<<" auto p = strptime(timeStr.c_str(),\"%Y-%m-%d %H:%M:%S\",&stm);\n"; $$<<" time_t t = mktime(&stm);\n"; $$<<" size_t decimalNum = 0;\n"; - $$<<" if(*p=='.')\n"; + $$<<" if(p)\n"; $$<<" {\n"; - $$<<" std::string decimals(p+1,&timeStr[timeStr.length()]);\n"; - $$<<" while(decimals.length()<6)\n"; + $$<<" if(*p=='.')\n"; $$<<" {\n"; - $$<<" decimals += \"0\";\n"; + $$<<" std::string decimals(p+1,&timeStr[timeStr.length()]);\n"; + $$<<" while(decimals.length()<6)\n"; + $$<<" {\n"; + $$<<" decimals += \"0\";\n"; + $$<<" }\n"; + $$<<" decimalNum = (size_t)atol(decimals.c_str());\n"; $$<<" }\n"; - $$<<" decimalNum = (size_t)atol(decimals.c_str());\n"; - $$<<" }\n"; // $$<<" "<(::trantor::Date(946656000000000).after(daysNum*86400));\n"; - $$<<" "<(t*1000000+decimalNum);\n"; + $$<<" "<(t*1000000+decimalNum);\n"; + $$<<" }\n"; $$<<" }\n"; continue; } @@ -294,17 +300,20 @@ const std::string &[[className]]::getColumnName(size_t index) noexcept(false) $$<<" auto p = strptime(timeStr.c_str(),\"%Y-%m-%d %H:%M:%S\",&stm);\n"; $$<<" time_t t = mktime(&stm);\n"; $$<<" size_t decimalNum = 0;\n"; - $$<<" if(*p=='.')\n"; + $$<<" if(p)\n"; $$<<" {\n"; - $$<<" std::string decimals(p+1,&timeStr[timeStr.length()]);\n"; - $$<<" while(decimals.length()<6)\n"; + $$<<" if(*p=='.')\n"; $$<<" {\n"; - $$<<" decimals += \"0\";\n"; + $$<<" std::string decimals(p+1,&timeStr[timeStr.length()]);\n"; + $$<<" while(decimals.length()<6)\n"; + $$<<" {\n"; + $$<<" decimals += \"0\";\n"; + $$<<" }\n"; + $$<<" decimalNum = (size_t)atol(decimals.c_str());\n"; $$<<" }\n"; - $$<<" decimalNum = (size_t)atol(decimals.c_str());\n"; - $$<<" }\n"; // $$<<" "<(::trantor::Date(946656000000000).after(daysNum*86400));\n"; - $$<<" "<(t*1000000+decimalNum);\n"; + $$<<" "<(t*1000000+decimalNum);\n"; + $$<<" }\n"; $$<<" }\n"; $$<<" }\n"; continue; @@ -418,17 +427,20 @@ const std::string &[[className]]::getColumnName(size_t index) noexcept(false) $$<<" auto p = strptime(timeStr.c_str(),\"%Y-%m-%d %H:%M:%S\",&stm);\n"; $$<<" time_t t = mktime(&stm);\n"; $$<<" size_t decimalNum = 0;\n"; - $$<<" if(*p=='.')\n"; + $$<<" if(p)\n"; $$<<" {\n"; - $$<<" std::string decimals(p+1,&timeStr[timeStr.length()]);\n"; - $$<<" while(decimals.length()<6)\n"; + $$<<" if(*p=='.')\n"; $$<<" {\n"; - $$<<" decimals += \"0\";\n"; + $$<<" std::string decimals(p+1,&timeStr[timeStr.length()]);\n"; + $$<<" while(decimals.length()<6)\n"; + $$<<" {\n"; + $$<<" decimals += \"0\";\n"; + $$<<" }\n"; + $$<<" decimalNum = (size_t)atol(decimals.c_str());\n"; $$<<" }\n"; - $$<<" decimalNum = (size_t)atol(decimals.c_str());\n"; - $$<<" }\n"; // $$<<" "<(::trantor::Date(946656000000000).after(daysNum*86400));\n"; - $$<<" "<(t*1000000+decimalNum);\n"; + $$<<" "<(t*1000000+decimalNum);\n"; + $$<<" }\n"; $$<<" }\n"; $$<<" }\n"; continue; @@ -551,17 +563,20 @@ void [[className]]::updateByMasqueradedJson(const Json::Value &pJson, $$<<" auto p = strptime(timeStr.c_str(),\"%Y-%m-%d %H:%M:%S\",&stm);\n"; $$<<" time_t t = mktime(&stm);\n"; $$<<" size_t decimalNum = 0;\n"; - $$<<" if(*p=='.')\n"; + $$<<" if(p)\n"; $$<<" {\n"; - $$<<" std::string decimals(p+1,&timeStr[timeStr.length()]);\n"; - $$<<" while(decimals.length()<6)\n"; + $$<<" if(*p=='.')\n"; $$<<" {\n"; - $$<<" decimals += \"0\";\n"; + $$<<" std::string decimals(p+1,&timeStr[timeStr.length()]);\n"; + $$<<" while(decimals.length()<6)\n"; + $$<<" {\n"; + $$<<" decimals += \"0\";\n"; + $$<<" }\n"; + $$<<" decimalNum = (size_t)atol(decimals.c_str());\n"; $$<<" }\n"; - $$<<" decimalNum = (size_t)atol(decimals.c_str());\n"; - $$<<" }\n"; // $$<<" "<(::trantor::Date(946656000000000).after(daysNum*86400));\n"; - $$<<" "<(t*1000000+decimalNum);\n"; + $$<<" "<(t*1000000+decimalNum);\n"; + $$<<" }\n"; $$<<" }\n"; $$<<" }\n"; continue; @@ -678,17 +693,20 @@ void [[className]]::updateByJson(const Json::Value &pJson) noexcept(false) $$<<" auto p = strptime(timeStr.c_str(),\"%Y-%m-%d %H:%M:%S\",&stm);\n"; $$<<" time_t t = mktime(&stm);\n"; $$<<" size_t decimalNum = 0;\n"; - $$<<" if(*p=='.')\n"; + $$<<" if(p)\n"; $$<<" {\n"; - $$<<" std::string decimals(p+1,&timeStr[timeStr.length()]);\n"; - $$<<" while(decimals.length()<6)\n"; + $$<<" if(*p=='.')\n"; $$<<" {\n"; - $$<<" decimals += \"0\";\n"; + $$<<" std::string decimals(p+1,&timeStr[timeStr.length()]);\n"; + $$<<" while(decimals.length()<6)\n"; + $$<<" {\n"; + $$<<" decimals += \"0\";\n"; + $$<<" }\n"; + $$<<" decimalNum = (size_t)atol(decimals.c_str());\n"; $$<<" }\n"; - $$<<" decimalNum = (size_t)atol(decimals.c_str());\n"; - $$<<" }\n"; // $$<<" "<(::trantor::Date(946656000000000).after(daysNum*86400));\n"; - $$<<" "<(t*1000000+decimalNum);\n"; + $$<<" "<(t*1000000+decimalNum);\n"; + $$<<" }\n"; $$<<" }\n"; $$<<" }\n"; continue;