Modify generation of models and the DbClient class (#498)

* Add setter method for automatical fields of models

* Put SQL queries into buffer before any connection being established

* Update trantor
This commit is contained in:
An Tao 2020-07-03 12:19:40 +08:00 committed by GitHub
parent f871d1607d
commit 9c54fb8c69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 139 additions and 89 deletions

View File

@ -783,8 +783,6 @@ void [[className]]::updateByJson(const Json::Value &pJson) noexcept(false)
$$<<" return "<<col.colValName_<<"_;\n";
$$<<"}\n";
if(!col.isAutoVal_)
{
$$<<"void "<<className<<"::set"<<col.colTypeName_<<"(const "<<col.colType_<<" &p"<<col.colTypeName_<<") noexcept\n";
$$<<"{\n";
if(col.colDatabaseType_=="date")
@ -824,7 +822,7 @@ void [[className]]::updateByJson(const Json::Value &pJson) noexcept(false)
$$<<" dirtyFlag_["<<i<<"] = true;\n";
$$<<"}\n";
}
}
if(col.isPrimaryKey_&&@@.get<int>("hasPrimaryKey")==1)
{
$$<<"const typename "<<className<<"::PrimaryKeyType & "<<className<<"::getPrimaryKey() const\n";
@ -930,13 +928,20 @@ void [[className]]::outputArgs(drogon::orm::internal::SqlBinder &binder) const
const std::vector<std::string> [[className]]::updateColumns() const
{
std::vector<std::string> ret;
for(size_t i=0;i<sizeof(dirtyFlag_);i++)
<%c++
for(size_t i=0;i<cols.size();i++)
{
if(dirtyFlag_[i])
{
ret.push_back(getColumnName(i));
}
auto & col=cols[i];
if(col.colType_.empty()||col.isAutoVal_)
continue;
%>
if(dirtyFlag_[{%i%}])
{
ret.push_back(getColumnName({%i%}));
}
<%c++
}
%>
return ret;
}

View File

@ -170,8 +170,7 @@ auto cols=@@.get<std::vector<ColumnInfo>>("columns");
}
$$<<" ///Return a shared_ptr object pointing to the column const value, or an empty shared_ptr object if the column is null\n";
$$<<" const std::shared_ptr<"<<col.colType_<<"> &get"<<col.colTypeName_<<"() const noexcept;\n";
if(!col.isAutoVal_)
{
$$<<" ///Set the value of the column "<<col.colName_<<"\n";
$$<<" void set"<<col.colTypeName_<<"(const "<<col.colType_<<" &p"<<col.colTypeName_<<") noexcept;\n";
if(col.colType_=="std::string")
@ -184,7 +183,7 @@ auto cols=@@.get<std::vector<ColumnInfo>>("columns");
{
$$<<" void set"<<col.colTypeName_<<"ToNull() noexcept;\n";
}
}
}
else
$$<<" //FIXME!!"<<" getValueOf"<<col.colTypeName_<<"() const noexcept;\n";

View File

@ -126,18 +126,7 @@ void DbClientImpl::execSql(
ResultCallback &&rcb,
std::function<void(const std::exception_ptr &)> &&exceptCallback)
{
if (!conn)
{
try
{
throw BrokenConnection("There is no connection to PG server!");
}
catch (...)
{
exceptCallback(std::current_exception());
}
return;
}
assert(conn);
conn->execSql(std::move(sql),
paraNum,
std::move(parameters),
@ -167,18 +156,6 @@ void DbClientImpl::execSql(
if (readyConnections_.size() == 0)
{
if (busyConnections_.size() == 0)
{
try
{
throw BrokenConnection("No connection to database server");
}
catch (...)
{
exceptCallback(std::current_exception());
}
return;
}
if (sqlCmdBuffer_.size() > 200000)
{
// too many queries in buffer;

View File

@ -88,19 +88,8 @@ void DbClientLockFree::execSql(
assert(paraNum == format.size());
assert(rcb);
loop_->assertInLoopThread();
if (connections_.empty())
{
try
{
throw BrokenConnection("No connection to database server");
}
catch (...)
{
exceptCallback(std::current_exception());
}
return;
}
else if (sqlCmdBuffer_.empty() && transCallbacks_.empty())
if (!connections_.empty() && sqlCmdBuffer_.empty() &&
transCallbacks_.empty())
{
#if (!LIBPQ_SUPPORTS_BATCH_MODE)
for (auto &conn : connections_)

View File

@ -31,8 +31,8 @@ using namespace drogon::orm;
#define RED "\033[31m" /* Red */
#define GREEN "\033[32m" /* Green */
constexpr int postgre_tests = 42;
constexpr int mysql_tests = 44;
constexpr int postgre_tests = 43;
constexpr int mysql_tests = 45;
constexpr int sqlite_tests = 47;
int test_count = 0;
@ -695,6 +695,11 @@ void doPostgreTest(const drogon::orm::DbClientPtr &clientPtr)
{
auto user = mapper.findByPrimaryKey(2);
testOutput(true, "postgresql - ORM mapper synchronous interface(0)");
Users newUser;
newUser.setId(user.getValueOfId());
newUser.setSalt("xxx");
auto c = mapper.update(newUser);
testOutput(c == 1, "postgresql - ORM mapper synchronous interface(1)");
}
catch (const DrogonDbException &e)
{
@ -1308,6 +1313,11 @@ void doMysqlTest(const drogon::orm::DbClientPtr &clientPtr)
{
auto user = mapper.findByPrimaryKey(1);
testOutput(true, "mysql - ORM mapper synchronous interface(0)");
Users newUser;
newUser.setId(user.getValueOfId());
newUser.setSalt("xxx");
auto c = mapper.update(newUser);
testOutput(c == 1, "mysql - ORM mapper synchronous interface(1)");
}
catch (const DrogonDbException &e)
{
@ -1970,25 +1980,13 @@ int main(int argc, char *argv[])
"host=127.0.0.1 port=5432 dbname=postgres user=postgres "
"client_encoding=utf8",
1);
while (!postgre_client->hasAvailableConnections())
{
std::this_thread::sleep_for(1s);
}
#endif
#if USE_MYSQL
auto mysql_client = DbClient::newMysqlClient(
"host=localhost port=3306 user=root client_encoding=utf8mb4", 1);
while (!mysql_client->hasAvailableConnections())
{
std::this_thread::sleep_for(1s);
}
#endif
#if USE_SQLITE3
auto sqlite_client = DbClient::newSqlite3Client("filename=:memory:", 1);
while (!sqlite_client->hasAvailableConnections())
{
std::this_thread::sleep_for(1s);
}
#endif
LOG_DEBUG << "start!";
std::this_thread::sleep_for(1s);

View File

@ -524,6 +524,12 @@ const std::shared_ptr<int32_t> &Users::getId() const noexcept
{
return id_;
}
void Users::setId(const int32_t &pId) noexcept
{
id_ = std::make_shared<int32_t>(pId);
dirtyFlag_[0] = true;
}
const typename Users::PrimaryKeyType &Users::getPrimaryKey() const
{
assert(id_);
@ -862,12 +868,37 @@ void Users::outputArgs(drogon::orm::internal::SqlBinder &binder) const
const std::vector<std::string> Users::updateColumns() const
{
std::vector<std::string> ret;
for (size_t i = 0; i < sizeof(dirtyFlag_); i++)
if (dirtyFlag_[1])
{
if (dirtyFlag_[i])
{
ret.push_back(getColumnName(i));
}
ret.push_back(getColumnName(1));
}
if (dirtyFlag_[2])
{
ret.push_back(getColumnName(2));
}
if (dirtyFlag_[3])
{
ret.push_back(getColumnName(3));
}
if (dirtyFlag_[4])
{
ret.push_back(getColumnName(4));
}
if (dirtyFlag_[5])
{
ret.push_back(getColumnName(5));
}
if (dirtyFlag_[6])
{
ret.push_back(getColumnName(6));
}
if (dirtyFlag_[7])
{
ret.push_back(getColumnName(7));
}
if (dirtyFlag_[8])
{
ret.push_back(getColumnName(8));
}
return ret;
}

View File

@ -6,20 +6,20 @@
*/
#pragma once
#include <drogon/orm/Field.h>
#include <drogon/orm/Mapper.h>
#include <drogon/orm/Result.h>
#include <drogon/orm/Row.h>
#include <drogon/orm/Field.h>
#include <drogon/orm/SqlBinder.h>
#include <json/json.h>
#include <stdint.h>
#include <drogon/orm/Mapper.h>
#include <trantor/utils/Date.h>
#include <trantor/utils/Logger.h>
#include <iostream>
#include <memory>
#include <json/json.h>
#include <string>
#include <tuple>
#include <memory>
#include <vector>
#include <tuple>
#include <stdint.h>
#include <iostream>
using namespace drogon::orm;
namespace drogon
@ -30,7 +30,6 @@ class DbClient;
using DbClientPtr = std::shared_ptr<DbClient>;
} // namespace orm
} // namespace drogon
namespace drogon_model
{
namespace drogonTestMysql
@ -114,6 +113,9 @@ class Users
/// empty shared_ptr object if the column is null
const std::shared_ptr<int32_t> &getId() const noexcept;
/// Set the value of the column id
void setId(const int32_t &pId) noexcept;
/** For column user_id */
/// Get the value of the column user_id, returns the default value if the
/// column is null
@ -121,6 +123,7 @@ class Users
/// Return a shared_ptr object pointing to the column const value, or an
/// empty shared_ptr object if the column is null
const std::shared_ptr<std::string> &getUserId() const noexcept;
/// Set the value of the column user_id
void setUserId(const std::string &pUserId) noexcept;
void setUserId(std::string &&pUserId) noexcept;
@ -133,6 +136,7 @@ class Users
/// Return a shared_ptr object pointing to the column const value, or an
/// empty shared_ptr object if the column is null
const std::shared_ptr<std::string> &getUserName() const noexcept;
/// Set the value of the column user_name
void setUserName(const std::string &pUserName) noexcept;
void setUserName(std::string &&pUserName) noexcept;
@ -145,6 +149,7 @@ class Users
/// Return a shared_ptr object pointing to the column const value, or an
/// empty shared_ptr object if the column is null
const std::shared_ptr<std::string> &getPassword() const noexcept;
/// Set the value of the column password
void setPassword(const std::string &pPassword) noexcept;
void setPassword(std::string &&pPassword) noexcept;
@ -157,6 +162,7 @@ class Users
/// Return a shared_ptr object pointing to the column const value, or an
/// empty shared_ptr object if the column is null
const std::shared_ptr<std::string> &getOrgName() const noexcept;
/// Set the value of the column org_name
void setOrgName(const std::string &pOrgName) noexcept;
void setOrgName(std::string &&pOrgName) noexcept;
@ -169,6 +175,7 @@ class Users
/// Return a shared_ptr object pointing to the column const value, or an
/// empty shared_ptr object if the column is null
const std::shared_ptr<std::string> &getSignature() const noexcept;
/// Set the value of the column signature
void setSignature(const std::string &pSignature) noexcept;
void setSignature(std::string &&pSignature) noexcept;
@ -181,6 +188,7 @@ class Users
/// Return a shared_ptr object pointing to the column const value, or an
/// empty shared_ptr object if the column is null
const std::shared_ptr<std::string> &getAvatarId() const noexcept;
/// Set the value of the column avatar_id
void setAvatarId(const std::string &pAvatarId) noexcept;
void setAvatarId(std::string &&pAvatarId) noexcept;
@ -193,6 +201,7 @@ class Users
/// Return a shared_ptr object pointing to the column const value, or an
/// empty shared_ptr object if the column is null
const std::shared_ptr<std::string> &getSalt() const noexcept;
/// Set the value of the column salt
void setSalt(const std::string &pSalt) noexcept;
void setSalt(std::string &&pSalt) noexcept;
@ -205,6 +214,7 @@ class Users
/// Return a shared_ptr object pointing to the column const value, or an
/// empty shared_ptr object if the column is null
const std::shared_ptr<int8_t> &getAdmin() const noexcept;
/// Set the value of the column admin
void setAdmin(const int8_t &pAdmin) noexcept;
void setAdminToNull() noexcept;

View File

@ -692,6 +692,12 @@ const std::shared_ptr<int32_t> &Users::getId() const noexcept
{
return id_;
}
void Users::setId(const int32_t &pId) noexcept
{
id_ = std::make_shared<int32_t>(pId);
dirtyFlag_[6] = true;
}
const typename Users::PrimaryKeyType &Users::getPrimaryKey() const
{
assert(id_);
@ -861,12 +867,37 @@ void Users::outputArgs(drogon::orm::internal::SqlBinder &binder) const
const std::vector<std::string> Users::updateColumns() const
{
std::vector<std::string> ret;
for (size_t i = 0; i < sizeof(dirtyFlag_); i++)
if (dirtyFlag_[0])
{
if (dirtyFlag_[i])
{
ret.push_back(getColumnName(i));
}
ret.push_back(getColumnName(0));
}
if (dirtyFlag_[1])
{
ret.push_back(getColumnName(1));
}
if (dirtyFlag_[2])
{
ret.push_back(getColumnName(2));
}
if (dirtyFlag_[3])
{
ret.push_back(getColumnName(3));
}
if (dirtyFlag_[4])
{
ret.push_back(getColumnName(4));
}
if (dirtyFlag_[5])
{
ret.push_back(getColumnName(5));
}
if (dirtyFlag_[7])
{
ret.push_back(getColumnName(7));
}
if (dirtyFlag_[8])
{
ret.push_back(getColumnName(8));
}
return ret;
}

View File

@ -6,20 +6,20 @@
*/
#pragma once
#include <drogon/orm/Field.h>
#include <drogon/orm/Mapper.h>
#include <drogon/orm/Result.h>
#include <drogon/orm/Row.h>
#include <drogon/orm/Field.h>
#include <drogon/orm/SqlBinder.h>
#include <json/json.h>
#include <stdint.h>
#include <drogon/orm/Mapper.h>
#include <trantor/utils/Date.h>
#include <trantor/utils/Logger.h>
#include <iostream>
#include <memory>
#include <json/json.h>
#include <string>
#include <tuple>
#include <memory>
#include <vector>
#include <tuple>
#include <stdint.h>
#include <iostream>
using namespace drogon::orm;
namespace drogon
@ -30,7 +30,6 @@ class DbClient;
using DbClientPtr = std::shared_ptr<DbClient>;
} // namespace orm
} // namespace drogon
namespace drogon_model
{
namespace postgres
@ -113,6 +112,7 @@ class Users
/// Return a shared_ptr object pointing to the column const value, or an
/// empty shared_ptr object if the column is null
const std::shared_ptr<std::string> &getUserId() const noexcept;
/// Set the value of the column user_id
void setUserId(const std::string &pUserId) noexcept;
void setUserId(std::string &&pUserId) noexcept;
@ -125,6 +125,7 @@ class Users
/// Return a shared_ptr object pointing to the column const value, or an
/// empty shared_ptr object if the column is null
const std::shared_ptr<std::string> &getUserName() const noexcept;
/// Set the value of the column user_name
void setUserName(const std::string &pUserName) noexcept;
void setUserName(std::string &&pUserName) noexcept;
@ -137,6 +138,7 @@ class Users
/// Return a shared_ptr object pointing to the column const value, or an
/// empty shared_ptr object if the column is null
const std::shared_ptr<std::string> &getPassword() const noexcept;
/// Set the value of the column password
void setPassword(const std::string &pPassword) noexcept;
void setPassword(std::string &&pPassword) noexcept;
@ -149,6 +151,7 @@ class Users
/// Return a shared_ptr object pointing to the column const value, or an
/// empty shared_ptr object if the column is null
const std::shared_ptr<std::string> &getOrgName() const noexcept;
/// Set the value of the column org_name
void setOrgName(const std::string &pOrgName) noexcept;
void setOrgName(std::string &&pOrgName) noexcept;
@ -161,6 +164,7 @@ class Users
/// Return a shared_ptr object pointing to the column const value, or an
/// empty shared_ptr object if the column is null
const std::shared_ptr<std::string> &getSignature() const noexcept;
/// Set the value of the column signature
void setSignature(const std::string &pSignature) noexcept;
void setSignature(std::string &&pSignature) noexcept;
@ -173,6 +177,7 @@ class Users
/// Return a shared_ptr object pointing to the column const value, or an
/// empty shared_ptr object if the column is null
const std::shared_ptr<std::string> &getAvatarId() const noexcept;
/// Set the value of the column avatar_id
void setAvatarId(const std::string &pAvatarId) noexcept;
void setAvatarId(std::string &&pAvatarId) noexcept;
@ -186,6 +191,9 @@ class Users
/// empty shared_ptr object if the column is null
const std::shared_ptr<int32_t> &getId() const noexcept;
/// Set the value of the column id
void setId(const int32_t &pId) noexcept;
/** For column salt */
/// Get the value of the column salt, returns the default value if the
/// column is null
@ -193,6 +201,7 @@ class Users
/// Return a shared_ptr object pointing to the column const value, or an
/// empty shared_ptr object if the column is null
const std::shared_ptr<std::string> &getSalt() const noexcept;
/// Set the value of the column salt
void setSalt(const std::string &pSalt) noexcept;
void setSalt(std::string &&pSalt) noexcept;
@ -205,6 +214,7 @@ class Users
/// Return a shared_ptr object pointing to the column const value, or an
/// empty shared_ptr object if the column is null
const std::shared_ptr<bool> &getAdmin() const noexcept;
/// Set the value of the column admin
void setAdmin(const bool &pAdmin) noexcept;
void setAdminToNull() noexcept;

@ -1 +1 @@
Subproject commit 3692af0ca6282854c26cf3deb9fe31d78e2434b0
Subproject commit d814acd672516d60d8f8377d175f2ba319b05719