Skip to content

Commit

Permalink
Merge pull request #1372 from fnc12/bugfix/1354
Browse files Browse the repository at this point in the history
added table name to order by serialization
  • Loading branch information
fnc12 authored Jan 17, 2025
2 parents bd2fd6b + d0c7283 commit 8533b9e
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 157 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ cmake-build-debug/
.idea/

/compile
build
build/
build-xcode/
2 changes: 1 addition & 1 deletion dev/conditions.h
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ namespace sqlite_orm {
template<class O>
void push_back(order_by_t<O> order_by) {
auto newContext = this->context;
newContext.skip_table_name = true;
newContext.skip_table_name = false;
auto columnName = serialize(order_by.expression, newContext);
this->entries.emplace_back(std::move(columnName),
order_by.asc_desc,
Expand Down
2 changes: 1 addition & 1 deletion include/sqlite_orm/sqlite_orm.h
Original file line number Diff line number Diff line change
Expand Up @@ -5320,7 +5320,7 @@ namespace sqlite_orm {
template<class O>
void push_back(order_by_t<O> order_by) {
auto newContext = this->context;
newContext.skip_table_name = true;
newContext.skip_table_name = false;
auto columnName = serialize(order_by.expression, newContext);
this->entries.emplace_back(std::move(columnName),
order_by.asc_desc,
Expand Down
159 changes: 5 additions & 154 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,163 +1,14 @@
cmake_minimum_required (VERSION 3.16)

# note: find_package(SQLite3 REQUIRED) already done in top-level CMakeLists
# Note: find_package(SQLite3 REQUIRED) already done in top-level CMakeLists
FetchContent_MakeAvailable(Catch2)

option(SQLITE_ORM_OMITS_CODECVT "Omits codec testing" OFF)

add_executable(unit_tests
static_tests/functional/static_if_tests.cpp
static_tests/functional/mpl.cpp
static_tests/functional/same_or_void.cpp
static_tests/functional/index_sequence_util.cpp
static_tests/functional/tuple_conc.cpp
static_tests/functional/tuple_filter.cpp
static_tests/functional/tuple_traits.cpp
static_tests/functional/tuple_transform.cpp
static_tests/is_printable.cpp
static_tests/is_bindable.cpp
static_tests/row_extractor.cpp
static_tests/iterator_t.cpp
static_tests/arithmetic_operators_result_type.cpp
static_tests/node_tuple.cpp
static_tests/bindable_filter.cpp
static_tests/member_traits_tests.cpp
static_tests/select_return_type.cpp
static_tests/column.cpp
static_tests/foreign_key.cpp
static_tests/function_static_tests.cpp
static_tests/aggregate_function_return_types.cpp
static_tests/core_function_return_types.cpp
static_tests/column_expression_type.cpp
static_tests/column_result_t.cpp
static_tests/column_pointer.cpp
static_tests/alias.cpp
static_tests/cte.cpp
static_tests/is_primary_key_insertable.cpp
static_tests/is_column_with_insertable_primary_key.cpp
static_tests/operators_adl.cpp
static_tests/table_static_tests.cpp
static_tests/statements.cpp
tuple_iteration.cpp
xdestroy_handling.cpp
sync_schema_tests.cpp
tests.cpp
tests3.cpp
tests4.cpp
tests5.cpp
schema/column_tests.cpp
schema/index_tests.cpp
schema/table_tests.cpp
schema/explicit_columns.cpp
schema/virtual_table.cpp
private_getters_tests.cpp
pragma_tests.cpp
built_in_functions_tests/core_functions_tests.cpp
built_in_functions_tests/datetime_function_tests.cpp
built_in_functions_tests/math_functions.cpp
user_defined_functions.cpp
constraints/composite_key.cpp
operators/like.cpp
operators/glob.cpp
operators/in.cpp
operators/cast.cpp
operators/is_null.cpp
operators/arithmetic_operators.cpp
operators/bitwise_operators.cpp
operators/binary_operators.cpp
operators/not_operator.cpp
prepared_statement_tests/select.cpp
prepared_statement_tests/get_all.cpp
prepared_statement_tests/get_all_pointer.cpp
prepared_statement_tests/get_all_optional.cpp
prepared_statement_tests/update_all.cpp
prepared_statement_tests/remove_all.cpp
prepared_statement_tests/get.cpp
prepared_statement_tests/get_pointer.cpp
prepared_statement_tests/get_optional.cpp
prepared_statement_tests/update.cpp
prepared_statement_tests/remove.cpp
prepared_statement_tests/insert.cpp
prepared_statement_tests/replace.cpp
prepared_statement_tests/insert_range.cpp
prepared_statement_tests/replace_range.cpp
prepared_statement_tests/insert_explicit.cpp
prepared_statement_tests/column_names.cpp
pragma_tests.cpp
simple_query.cpp
constraints/default.cpp
constraints/unique.cpp
constraints/foreign_key.cpp
constraints/check.cpp
statement_serializer_tests/column_constraints/generated.cpp
statement_serializer_tests/column_constraints/default.cpp
statement_serializer_tests/column_constraints/primary_key.cpp
statement_serializer_tests/column_constraints/unique.cpp
statement_serializer_tests/column_constraints/check.cpp
statement_serializer_tests/column_constraints/null.cpp
statement_serializer_tests/column_constraints/not_null.cpp
statement_serializer_tests/column_constraints/unindexed.cpp
statement_serializer_tests/table_constraints/prefix.cpp
statement_serializer_tests/table_constraints/foreign_key.cpp
statement_serializer_tests/table_constraints/tokenize.cpp
statement_serializer_tests/table_constraints/content.cpp
statement_serializer_tests/bindables.cpp
statement_serializer_tests/ast/upsert_clause.cpp
statement_serializer_tests/ast/excluded.cpp
statement_serializer_tests/ast/set.cpp
statement_serializer_tests/ast/match.cpp
statement_serializer_tests/ast/rank.cpp
statement_serializer_tests/arithmetic_operators.cpp
statement_serializer_tests/base_types.cpp
statement_serializer_tests/collate.cpp
statement_serializer_tests/comparison_operators.cpp
statement_serializer_tests/core_functions.cpp
statement_serializer_tests/rowid.cpp
statement_serializer_tests/column_names.cpp
statement_serializer_tests/schema/index.cpp
statement_serializer_tests/schema/trigger.cpp
statement_serializer_tests/schema/new_old.cpp
statement_serializer_tests/schema/raise.cpp
statement_serializer_tests/schema/using_fts5.cpp
statement_serializer_tests/schema/virtual_table.cpp
statement_serializer_tests/schema/column.cpp
statement_serializer_tests/schema/table.cpp
statement_serializer_tests/indexed_column.cpp
statement_serializer_tests/logical_operators.cpp
statement_serializer_tests/statements/select.cpp
statement_serializer_tests/select_constraints.cpp
statement_serializer_tests/conditions.cpp
statement_serializer_tests/statements/insert_replace.cpp
statement_serializer_tests/statements/update.cpp
statement_serializer_tests/statements/remove.cpp
statement_serializer_tests/statements/update_all.cpp
statement_serializer_tests/aggregate_functions.cpp
statement_serializer_tests/alias_extractor.cpp
storage_tests.cpp
storage_non_crud_tests.cpp
unique_cases/get_all_with_two_tables.cpp
unique_cases/prepare_get_all_with_case.cpp
unique_cases/index_named_table_with_fk.cpp
unique_cases/issue525.cpp
unique_cases/delete_with_two_fields.cpp
unique_cases/join_iterator_ctor_compilation_error.cpp
unique_cases/issue86.cpp
unique_cases/issue937.cpp
unique_cases/issue663.cpp
unique_cases/issue1357.cpp
get_all_custom_containers.cpp
select_constraints_tests.cpp
backup_tests.cpp
transaction_tests.cpp
json.cpp
row_id.cpp
trigger_tests.cpp
ast_iterator_tests.cpp
table_name_collector.cpp
pointer_passing_interface.cpp
row_extractor.cpp
iterate.cpp
)
# Glob all .cpp files recursively in the current directory
file(GLOB_RECURSE UNIT_TEST_SOURCES "*.cpp")

add_executable(unit_tests ${UNIT_TEST_SOURCES})

if(SQLITE_ORM_OMITS_CODECVT)
message(STATUS "SQLITE_ORM_OMITS_CODECVT is enabled")
Expand Down
69 changes: 69 additions & 0 deletions tests/statement_serializer_tests/ast/dynamic_order_by.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#include <sqlite_orm/sqlite_orm.h>
#include <catch2/catch_all.hpp>

using namespace sqlite_orm;

TEST_CASE("dynamic_order_by") {
using internal::serialize;

struct User {
uint32_t id;
std::string name;
std::string lastName;
};

struct OtherUser {
uint32_t id;
std::string name;
std::string lastName;
};

auto table1 = make_table("users",
make_column("id", &User::id),
make_column("name", &User::name),
make_column("last_name", &User::lastName));
auto table2 = make_table("other_users",
make_column("id", &OtherUser::id),
make_column("name", &OtherUser::name),
make_column("last_name", &OtherUser::lastName));

using db_objects_t = internal::db_objects_tuple<decltype(table1), decltype(table2)>;
auto dbObjects = db_objects_t{table1, table2};
using context_t = internal::serializer_context<db_objects_t>;
context_t context{dbObjects};
auto storage = make_storage("", table1, table2);

std::string value;
decltype(value) expected;

auto orderBySql = dynamic_order_by(storage);
SECTION("users.name") {
orderBySql.push_back(order_by(&User::name).asc());
expected = R"(ORDER BY "users"."name" ASC)";
}
SECTION("users.lastName") {
orderBySql.push_back(order_by(&User::lastName).asc());
expected = R"(ORDER BY "users"."last_name" ASC)";
}
SECTION("users.both") {
orderBySql.push_back(order_by(&User::name).asc());
orderBySql.push_back(order_by(&User::lastName).asc());
expected = R"(ORDER BY "users"."name" ASC, "users"."last_name" ASC)";
}
SECTION("other_users.name") {
orderBySql.push_back(order_by(&OtherUser::name).asc());
expected = R"(ORDER BY "other_users"."name" ASC)";
}
SECTION("other_users.lastName") {
orderBySql.push_back(order_by(&OtherUser::lastName).asc());
expected = R"(ORDER BY "other_users"."last_name" ASC)";
}
SECTION("other_users.both") {
orderBySql.push_back(order_by(&OtherUser::name).asc());
orderBySql.push_back(order_by(&OtherUser::lastName).asc());
expected = R"(ORDER BY "other_users"."name" ASC, "other_users"."last_name" ASC)";
}
value = serialize(orderBySql, context);

REQUIRE(value == expected);
}
52 changes: 52 additions & 0 deletions tests/unique_cases/issue1354.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <sqlite_orm/sqlite_orm.h>
#include <catch2/catch_all.hpp>

using namespace sqlite_orm;

TEST_CASE("issue1354") {
struct User {
uint32_t id;
std::string name;
std::string lastName;
};

struct OtherUser {
uint32_t id;
std::string name;
std::string lastName;
};

struct UserStruct {
uint32_t Id;
std::string Name;
std::string LastName;
};

constexpr auto userStruct = struct_<UserStruct>(&User::id, &User::name, &OtherUser::lastName);

auto storage = make_storage("",
make_table("users",
make_column("id", &User::id),
make_column("name", &User::name),
make_column("last_name", &User::lastName)),
make_table("other_users",
make_column("id", &OtherUser::id),
make_column("name", &OtherUser::name),
make_column("last_name", &OtherUser::lastName)));
storage.sync_schema();
auto orderBySql = dynamic_order_by(storage);

SECTION("name") {
orderBySql.push_back(order_by(&User::name).asc());
}
SECTION("lastName") {
orderBySql.push_back(order_by(&User::lastName).asc());
}
SECTION("both") {
orderBySql.push_back(order_by(&User::name).asc());
orderBySql.push_back(order_by(&User::lastName).asc());
}

auto users =
storage.select(userStruct, left_join<OtherUser>(on(c(&User::id) == &OtherUser::id)), orderBySql, limit(1));
}

0 comments on commit 8533b9e

Please sign in to comment.