Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions dev/statement_serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -548,9 +548,10 @@ namespace sqlite_orm::internal {
template<class Tuple, class Ctx>
void serialize_over_arguments(std::stringstream& ss, const Tuple& arguments, const Ctx& context) {
using args_tuple = std::decay_t<Tuple>;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Decay is unnecessary here.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

removed

constexpr bool is_named_ref = std::tuple_size<args_tuple>::value == 1 &&
std::is_same<std::tuple_element_t<0, args_tuple>, window_ref_t>::value;
if constexpr (is_named_ref) {
if constexpr (std::tuple_size_v<args_tuple> == 0) {
ss << " OVER ()";
} else if constexpr (std::tuple_size_v<args_tuple> == 1 &&
std::is_same_v<std::tuple_element_t<0, args_tuple>, window_ref_t>) {
ss << " OVER " << std::get<0>(arguments).name;
} else {
ss << " OVER (";
Expand Down
7 changes: 4 additions & 3 deletions include/sqlite_orm/sqlite_orm.h
Original file line number Diff line number Diff line change
Expand Up @@ -22208,9 +22208,10 @@ namespace sqlite_orm::internal {
template<class Tuple, class Ctx>
void serialize_over_arguments(std::stringstream& ss, const Tuple& arguments, const Ctx& context) {
using args_tuple = std::decay_t<Tuple>;
constexpr bool is_named_ref = std::tuple_size<args_tuple>::value == 1 &&
std::is_same<std::tuple_element_t<0, args_tuple>, window_ref_t>::value;
if constexpr (is_named_ref) {
if constexpr (std::tuple_size_v<args_tuple> == 0) {
ss << " OVER ()";
} else if constexpr (std::tuple_size_v<args_tuple> == 1 &&
std::is_same_v<std::tuple_element_t<0, args_tuple>, window_ref_t>) {
ss << " OVER " << std::get<0>(arguments).name;
} else {
ss << " OVER (";
Expand Down
47 changes: 47 additions & 0 deletions tests/window_function_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,50 @@ TEST_CASE("window functions") {
REQUIRE(std::get<2>(rows[5]) == 2);
}
}

TEST_CASE("window functions - issue #475 count over with where order by limit") {
struct UserProfile {
int id = 0;
std::string firstName;
std::string lastName;
};

auto storage = make_storage("",
make_table("user_profile",
make_column("id", &UserProfile::id, primary_key().autoincrement()),
make_column("first_name", &UserProfile::firstName),
make_column("last_name", &UserProfile::lastName)));
storage.sync_schema();

storage.insert(UserProfile{0, "Alice", "Smith"});
storage.insert(UserProfile{0, "Bob", "Jones"});
storage.insert(UserProfile{0, "Charlie", "Brown"});
storage.insert(UserProfile{0, "Diana", "Davis"});
storage.insert(UserProfile{0, "Eve", "Wilson"});

int refId = 0;
int resultPerPage = 3;

// SQL: SELECT id, first_name, last_name, COUNT(id) OVER ()
// FROM user_profile WHERE id > ? ORDER BY id LIMIT ?
auto rows = storage.select(
columns(&UserProfile::id, &UserProfile::firstName, &UserProfile::lastName, count(&UserProfile::id).over()),
where(c(&UserProfile::id) > refId),
order_by(&UserProfile::id),
limit(resultPerPage));

REQUIRE(rows.size() == 3);
// Every row should have total_count=5 (total rows in table)
REQUIRE(std::get<0>(rows[0]) == 1);
REQUIRE(std::get<1>(rows[0]) == "Alice");
REQUIRE(std::get<2>(rows[0]) == "Smith");
REQUIRE(std::get<3>(rows[0]) == 5);

REQUIRE(std::get<0>(rows[1]) == 2);
REQUIRE(std::get<1>(rows[1]) == "Bob");
REQUIRE(std::get<3>(rows[1]) == 5);

REQUIRE(std::get<0>(rows[2]) == 3);
REQUIRE(std::get<1>(rows[2]) == "Charlie");
REQUIRE(std::get<3>(rows[2]) == 5);
}
Loading