Skip to content

fix: Fix SQL syntax error when UPDATE has no fields to update#2422

Merged
an-tao merged 1 commit intodrogonframework:masterfrom
1980243524:master
Dec 23, 2025
Merged

fix: Fix SQL syntax error when UPDATE has no fields to update#2422
an-tao merged 1 commit intodrogonframework:masterfrom
1980243524:master

Conversation

@1980243524
Copy link
Copy Markdown
Contributor

fix #2421

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a SQL syntax error that occurs when UPDATE operations have no fields to update by adding early return checks when the column list is empty. The fix prevents generating invalid SQL like "UPDATE table SET WHERE ..." which would cause database errors.

Key changes:

  • Adds early return logic for empty column lists across all update and increment methods
  • Modifies update-related methods to return 0 affected rows when no columns need updating
  • Applies the fix to synchronous, callback-based, and future-based variants

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread orm_lib/inc/drogon/orm/Mapper.h Outdated
Comment on lines +1376 to +1387
static_assert(sizeof...(args) > 0);
assert(colNames.size() == sizeof...(args));
clear();
if (colNames.empty())
{
return 0;
}
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The empty check on line 1384 is unreachable due to the static_assert on line 1381 and the runtime assert on line 1382. If colNames is empty, then sizeof...(args) must be 0 (due to the assert), which would violate the static_assert that requires sizeof...(args) > 0. Consider removing the empty check or adjusting the assertions to make this logic consistent.

Copilot uses AI. Check for mistakes.
Comment thread orm_lib/inc/drogon/orm/Mapper.h Outdated
Comment on lines +1465 to +1469
if (colNames.empty())
{
rcb(0);
return;
}
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The empty check on line 1465 is unreachable due to the static_assert on line 1462 and the runtime assert on line 1463. If colNames is empty, then sizeof...(args) must be 0 (due to the assert), which would violate the static_assert that requires sizeof...(args) > 0. Consider removing the empty check or adjusting the assertions to make this logic consistent.

Suggested change
if (colNames.empty())
{
rcb(0);
return;
}

Copilot uses AI. Check for mistakes.
Comment thread orm_lib/inc/drogon/orm/Mapper.h Outdated
Comment on lines +1514 to +1551
assert(colNames.size() == sizeof...(args));
clear();
if (colNames.empty())
{
std::shared_ptr<std::promise<size_t>> prom =
std::make_shared<std::promise<size_t>>();
prom->set_value(0);
return prom->get_future();
}
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The empty check on line 1545 is unreachable due to the static_assert on line 1542 and the runtime assert on line 1543. If colNames is empty, then sizeof...(args) must be 0 (due to the assert), which would violate the static_assert that requires sizeof...(args) > 0. Consider removing the empty check or adjusting the assertions to make this logic consistent.

Suggested change
assert(colNames.size() == sizeof...(args));
clear();
if (colNames.empty())
{
std::shared_ptr<std::promise<size_t>> prom =
std::make_shared<std::promise<size_t>>();
prom->set_value(0);
return prom->get_future();
}
assert(!colNames.empty());
assert(colNames.size() == sizeof...(args));
clear();

Copilot uses AI. Check for mistakes.
Comment thread orm_lib/inc/drogon/orm/Mapper.h Outdated
Comment on lines +1553 to +1595
static_assert(sizeof...(args) > 0);
assert(colNames.size() == sizeof...(args));
clear();
if (colNames.empty())
{
return 0;
}
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The empty check on line 1592 is unreachable due to the static_assert on line 1589 and the runtime assert on line 1590. If colNames is empty, then sizeof...(args) must be 0 (due to the assert), which would violate the static_assert that requires sizeof...(args) > 0. Consider removing the empty check or adjusting the assertions to make this logic consistent.

Copilot uses AI. Check for mistakes.
Comment thread orm_lib/inc/drogon/orm/Mapper.h Outdated
Comment on lines +1650 to +1654
if (colNames.empty())
{
rcb(0);
return;
}
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The empty check on line 1650 is unreachable due to the static_assert on line 1647 and the runtime assert on line 1648. If colNames is empty, then sizeof...(args) must be 0 (due to the assert), which would violate the static_assert that requires sizeof...(args) > 0. Consider removing the empty check or adjusting the assertions to make this logic consistent.

Suggested change
if (colNames.empty())
{
rcb(0);
return;
}

Copilot uses AI. Check for mistakes.
Comment thread orm_lib/inc/drogon/orm/Mapper.h Outdated
Comment on lines +1655 to +1709
static_assert(sizeof...(args) > 0);
assert(colNames.size() == sizeof...(args));
clear();
if (colNames.empty())
{
std::shared_ptr<std::promise<size_t>> prom =
std::make_shared<std::promise<size_t>>();
prom->set_value(0);
return prom->get_future();
}
Copy link

Copilot AI Dec 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The empty check on line 1703 is unreachable due to the static_assert on line 1700 and the runtime assert on line 1701. If colNames is empty, then sizeof...(args) must be 0 (due to the assert), which would violate the static_assert that requires sizeof...(args) > 0. Consider removing the empty check or adjusting the assertions to make this logic consistent.

Copilot uses AI. Check for mistakes.
@an-tao
Copy link
Copy Markdown
Member

an-tao commented Dec 22, 2025

@1980243524 Thanks so much for your patch, please also modify the coroutine version (CoroMapper.h)

@1980243524
Copy link
Copy Markdown
Contributor Author

@an-tao The update method provided by the user (which is used to update specified fields) throws an exception when there are no fields to update. However, the update method implemented with the dirty flag directly returns without throwing an exception. Is such an inconsistency permissible?

@1980243524
Copy link
Copy Markdown
Contributor Author

@an-tao The updated fields provided by the user are visible to the user, whereas the update operation implemented via the dirty flag is invisible to the user (unless the user manually sets a dirty flag themselves).

@an-tao
Copy link
Copy Markdown
Member

an-tao commented Dec 23, 2025

he updated fields provided by the user are visible to the user, whereas the update operation implemented via the dirty flag is invisible to the user (unless the user manually sets a dirty flag themselves).

This is currently designed because the former is likely to be a mistake, while the latter may be the result of repeated calls to an update interface.

@1980243524
Copy link
Copy Markdown
Contributor Author

@an-tao Ok, I've finished the remaining parts of the code as per your previous suggestions.

@an-tao an-tao merged commit 11d9f4d into drogonframework:master Dec 23, 2025
34 checks passed
adal2404-Real added a commit to adal2404-Real/drogon that referenced this pull request Dec 24, 2025
fix: Fix SQL syntax error when UPDATE has no fields to update (drogonframework#2422)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

mapper调用update时如果没有脏位被标记的话,生成的sql语句有问题

3 participants