Skip to content

Commit ca03d6d

Browse files
committed
fix: Fix SQL syntax error when UPDATE has no fields to update
1 parent 1d9dcf3 commit ca03d6d

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

orm_lib/inc/drogon/orm/CoroMapper.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,10 +418,16 @@ class CoroMapper : public Mapper<T>
418418
this->clear();
419419
static_assert(!std::is_same_v<typename T::PrimaryKeyType, void>,
420420
"No primary key in the table!");
421+
std::vector<std::string> colNames = obj.updateColumns();
422+
if (colNames.empty())
423+
{
424+
callback(0);
425+
return;
426+
}
421427
std::string sql = "update ";
422428
sql += T::tableName;
423429
sql += " set ";
424-
for (auto const &colName : obj.updateColumns())
430+
for (auto const &colName : colNames)
425431
{
426432
sql += colName;
427433
sql += " = $?,";

orm_lib/inc/drogon/orm/Mapper.h

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,10 +1342,15 @@ inline size_t Mapper<T>::update(const T &obj) noexcept(false)
13421342
clear();
13431343
static_assert(!std::is_same_v<typename T::PrimaryKeyType, void>,
13441344
"No primary key in the table!");
1345+
std::vector<std::string> colNames = obj.updateColumns();
1346+
if (colNames.empty())
1347+
{
1348+
return 0;
1349+
}
13451350
std::string sql = "update ";
13461351
sql += T::tableName;
13471352
sql += " set ";
1348-
for (auto const &colName : obj.updateColumns())
1353+
for (auto const &colName : colNames)
13491354
{
13501355
sql += colName;
13511356
sql += " = $?,";
@@ -1415,10 +1420,17 @@ inline void Mapper<T>::update(const T &obj,
14151420
clear();
14161421
static_assert(!std::is_same_v<typename T::PrimaryKeyType, void>,
14171422
"No primary key in the table!");
1423+
1424+
std::vector<std::string> colNames = obj.updateColumns();
1425+
if (colNames.empty())
1426+
{
1427+
rcb(0);
1428+
return;
1429+
}
14181430
std::string sql = "update ";
14191431
sql += T::tableName;
14201432
sql += " set ";
1421-
for (auto const &colName : obj.updateColumns())
1433+
for (auto const &colName : colNames)
14221434
{
14231435
sql += colName;
14241436
sql += " = $?,";
@@ -1478,10 +1490,18 @@ inline std::future<size_t> Mapper<T>::updateFuture(const T &obj) noexcept
14781490
clear();
14791491
static_assert(!std::is_same_v<typename T::PrimaryKeyType, void>,
14801492
"No primary key in the table!");
1493+
std::vector<std::string> colNames = obj.updateColumns();
1494+
if (colNames.empty())
1495+
{
1496+
std::shared_ptr<std::promise<size_t>> prom =
1497+
std::make_shared<std::promise<size_t>>();
1498+
prom->set_value(0);
1499+
return prom->get_future();
1500+
}
14811501
std::string sql = "update ";
14821502
sql += T::tableName;
14831503
sql += " set ";
1484-
for (auto const &colName : obj.updateColumns())
1504+
for (auto const &colName : colNames)
14851505
{
14861506
sql += colName;
14871507
sql += " = $?,";

0 commit comments

Comments
 (0)