Modify the creation of models

This commit is contained in:
antao 2018-11-20 10:54:46 +08:00
parent ad37969535
commit 9ee0ef6cc8
4 changed files with 59 additions and 19 deletions

View File

@ -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<char>";
}
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<std::string>();

View File

@ -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";
$$<<" _"<<col._colValName<<"=std::make_shared<std::string>(std::move(binStr));\n";
$$<<" _"<<col._colValName<<"=std::make_shared<std::vector<char>>((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 *_"<<col._colValName<<";\n";
$$<<" return defaultValue;\n";
$$<<"}\n";
if(col._colType=="std::vector<char>")
{
$$<<"std::string "<<className<<"::getValueOf"<<col._colTypeName<<"AsString(const std::string &defaultValue) const noexcept\n";
$$<<"{\n";
$$<<" if(_"<<col._colValName<<")\n";
$$<<" return std::string(_"<<col._colValName<<"->data(),_"<<col._colValName<<"->size());\n";
$$<<" return defaultValue;\n";
$$<<"}\n";
}
$$<<"std::shared_ptr<const "<<col._colType<<"> "<<className<<"::get"<<col._colTypeName<<"() const noexcept\n";
$$<<"{\n";
$$<<" return _"<<col._colValName<<";\n";
@ -163,6 +171,15 @@ const std::string &{{className}}::getColumnName(size_t index) noexcept(false)
$$<<" _dirtyFlag["<<i<<"] = true;\n";
$$<<"}\n";
}
if(col._colType=="std::vector<char>")
{
$$<<"void "<<className<<"::set"<<col._colTypeName<<"(const std::string &"<<col._colValName<<") noexcept\n";
$$<<"{\n";
$$<<" _"<<col._colValName<<" = std::make_shared<std::vector<char>>("<<col._colValName<<".c_str(),"<<col._colValName<<".c_str()+"<<col._colValName<<".length());\n";
$$<<" _dirtyFlag["<<i<<"] = true;\n";
$$<<"}\n";
}
}
if(col._isPrimaryKey&&@@.get<int>("hasPrimaryKey")==1)
{
@ -226,8 +243,6 @@ void {{className}}::outputArgs(drogon::orm::internal::SqlBinder &binder) const
binder << (int32_t)(getValueOf<%c++$$<<col._colTypeName;%>().microSecondsSinceEpoch()/1000000-946656000)/86400;
<%c++}else if(col._colDatabaseType.find("timestamp")!=std::string::npos){%>
binder << get<%c++$$<<col._colTypeName;%>()->toCustomedFormattedStringLocal("%Y-%m-%d %H:%M:%S", true);
<%c++}else if(col._colDatabaseType=="bytea"){%>
binder << std::string("\\x")+drogon::binaryStringToHex((unsigned char *)getValueOf<%c++$$<<col._colTypeName;%>().c_str(),getValueOf<%c++$$<<col._colTypeName;%>().length());
<%c++}else{%>
binder << getValueOf<%c++$$<<col._colTypeName;%>();
<%c++}%>
@ -272,8 +287,6 @@ void {{className}}::updateArgs(drogon::orm::internal::SqlBinder &binder) const
binder << (int32_t)(getValueOf<%c++$$<<col._colTypeName;%>().microSecondsSinceEpoch()/1000000-946656000)/86400;
<%c++}else if(col._colDatabaseType.find("timestamp")!=std::string::npos){%>
binder << get<%c++$$<<col._colTypeName;%>()->toCustomedFormattedStringLocal("%Y-%m-%d %H:%M:%S", true);
<%c++}else if(col._colDatabaseType=="bytea"){%>
binder << std::string("\\x")+drogon::binaryStringToHex((unsigned char *)getValueOf<%c++$$<<col._colTypeName;%>().c_str(),getValueOf<%c++$$<<col._colTypeName;%>().length());
<%c++}else{%>
binder << getValueOf<%c++$$<<col._colTypeName;%>();
<%c++}%>

View File

@ -84,12 +84,20 @@ auto cols=@@.get<std::vector<ColumnInfo>>("columns");
if(!col._colType.empty())
{
$$<<" const "<<col._colType<<" &getValueOf"<<col._colTypeName<<"(const "<<col._colType<<" &defaultValue="<<col._colType<<"()) const noexcept;\n";
if(col._colType=="std::vector<char>")
{
$$<<" std::string getValueOf"<<col._colTypeName<<"AsString(const std::string &defaultValue=\"\") 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";
if(col._colType=="std::string")
$$<<" void set"<<col._colTypeName<<"("<<col._colType<<" &&"<<col._colValName<<") noexcept;\n";
if(col._colType=="std::vector<char>")
{
$$<<" void set"<<col._colTypeName<<"(const std::string &"<<col._colValName<<") noexcept;\n";
}
}
}
else

View File

@ -245,7 +245,7 @@ class SqlBinder
*std::static_pointer_cast<short>(obj) = ntohs(parameter);
break;
case 4:
*std::static_pointer_cast<int>(obj) = ntohl(parameter);
*std::static_pointer_cast<int>(obj) = ntohl(parameter);
break;
case 8:
*std::static_pointer_cast<long>(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<std::string> obj = std::make_shared<std::string>(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<char> &v)
{
std::shared_ptr<std::vector<char>> obj = std::make_shared<std::vector<char>>(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<char> &&v)
{
std::shared_ptr<std::vector<char>> obj = std::make_shared<std::vector<char>>(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: