Skip to content

Commit 9c245b1

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

1 file changed

Lines changed: 55 additions & 3 deletions

File tree

orm_lib/inc/drogon/orm/Mapper.h

Lines changed: 55 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 += " = $?,";
@@ -1376,6 +1381,10 @@ size_t Mapper<T>::updateBy(const std::vector<std::string> &colNames,
13761381
static_assert(sizeof...(args) > 0);
13771382
assert(colNames.size() == sizeof...(args));
13781383
clear();
1384+
if (colNames.empty())
1385+
{
1386+
return 0;
1387+
}
13791388
std::string sql = "update ";
13801389
sql += T::tableName;
13811390
sql += " set ";
@@ -1415,10 +1424,17 @@ inline void Mapper<T>::update(const T &obj,
14151424
clear();
14161425
static_assert(!std::is_same_v<typename T::PrimaryKeyType, void>,
14171426
"No primary key in the table!");
1427+
1428+
std::vector<std::string> colNames = obj.updateColumns();
1429+
if (colNames.empty())
1430+
{
1431+
rcb(0);
1432+
return;
1433+
}
14181434
std::string sql = "update ";
14191435
sql += T::tableName;
14201436
sql += " set ";
1421-
for (auto const &colName : obj.updateColumns())
1437+
for (auto const &colName : colNames)
14221438
{
14231439
sql += colName;
14241440
sql += " = $?,";
@@ -1446,6 +1462,11 @@ void Mapper<T>::updateBy(const std::vector<std::string> &colNames,
14461462
static_assert(sizeof...(args) > 0);
14471463
assert(colNames.size() == sizeof...(args));
14481464
clear();
1465+
if (colNames.empty())
1466+
{
1467+
rcb(0);
1468+
return;
1469+
}
14491470
std::string sql = "update ";
14501471
sql += T::tableName;
14511472
sql += " set ";
@@ -1478,10 +1499,18 @@ inline std::future<size_t> Mapper<T>::updateFuture(const T &obj) noexcept
14781499
clear();
14791500
static_assert(!std::is_same_v<typename T::PrimaryKeyType, void>,
14801501
"No primary key in the table!");
1502+
std::vector<std::string> colNames = obj.updateColumns();
1503+
if (colNames.empty())
1504+
{
1505+
std::shared_ptr<std::promise<size_t>> prom =
1506+
std::make_shared<std::promise<size_t>>();
1507+
prom->set_value(0);
1508+
return prom->get_future();
1509+
}
14811510
std::string sql = "update ";
14821511
sql += T::tableName;
14831512
sql += " set ";
1484-
for (auto const &colName : obj.updateColumns())
1513+
for (auto const &colName : colNames)
14851514
{
14861515
sql += colName;
14871516
sql += " = $?,";
@@ -1513,6 +1542,13 @@ inline std::future<size_t> Mapper<T>::updateFutureBy(
15131542
static_assert(sizeof...(args) > 0);
15141543
assert(colNames.size() == sizeof...(args));
15151544
clear();
1545+
if (colNames.empty())
1546+
{
1547+
std::shared_ptr<std::promise<size_t>> prom =
1548+
std::make_shared<std::promise<size_t>>();
1549+
prom->set_value(0);
1550+
return prom->get_future();
1551+
}
15161552
std::string sql = "update ";
15171553
sql += T::tableName;
15181554
sql += " set ";
@@ -1553,6 +1589,10 @@ inline size_t Mapper<T>::increment(const std::vector<std::string> &colNames,
15531589
static_assert(sizeof...(args) > 0);
15541590
assert(colNames.size() == sizeof...(args));
15551591
clear();
1592+
if (colNames.empty())
1593+
{
1594+
return 0;
1595+
}
15561596
std::string sql = "update ";
15571597
sql += T::tableName;
15581598
sql += " set ";
@@ -1607,6 +1647,11 @@ inline void Mapper<T>::increment(const std::vector<std::string> &colNames,
16071647
static_assert(sizeof...(args) > 0);
16081648
assert(colNames.size() == sizeof...(args));
16091649
clear();
1650+
if (colNames.empty())
1651+
{
1652+
rcb(0);
1653+
return;
1654+
}
16101655
std::string sql = "update ";
16111656
sql += T::tableName;
16121657
sql += " set ";
@@ -1655,6 +1700,13 @@ inline std::future<size_t> Mapper<T>::incrementFuture(
16551700
static_assert(sizeof...(args) > 0);
16561701
assert(colNames.size() == sizeof...(args));
16571702
clear();
1703+
if (colNames.empty())
1704+
{
1705+
std::shared_ptr<std::promise<size_t>> prom =
1706+
std::make_shared<std::promise<size_t>>();
1707+
prom->set_value(0);
1708+
return prom->get_future();
1709+
}
16581710
std::string sql = "update ";
16591711
sql += T::tableName;
16601712
sql += " set ";

0 commit comments

Comments
 (0)