Skip to content

Commit 3abd211

Browse files
committed
minor optimizations
1 parent a9b70b5 commit 3abd211

File tree

6 files changed

+75
-48
lines changed

6 files changed

+75
-48
lines changed

dev/ast/cross_join.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include "../functional/config.h"
21
#include "../functional/cxx_type_traits_polyfill.h"
32

43
namespace sqlite_orm {
@@ -12,9 +11,6 @@ namespace sqlite_orm {
1211
struct cross_join_t {
1312
using type = T;
1413
};
15-
16-
template<class T>
17-
using is_cross_join = polyfill::is_specialization_of<T, cross_join_t>;
1814
}
1915
}
2016

dev/conditions.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
namespace sqlite_orm {
3232

3333
namespace internal {
34-
3534
/**
3635
* Collated something
3736
*/
@@ -730,6 +729,13 @@ namespace sqlite_orm {
730729

731730
template<class T>
732731
using is_constrained_join = polyfill::is_detected<on_type_t, T>;
732+
733+
template<class T>
734+
using is_any_join = mpl::invoke_t<mpl::disjunction<check_if<is_constrained_join>,
735+
check_if_is_template<cross_join_t>,
736+
check_if_is_template<natural_join_t>>,
737+
T>;
738+
733739
}
734740
}
735741

dev/statement_serializer.h

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1946,21 +1946,23 @@ namespace sqlite_orm {
19461946
using conditions_tuple = typename statement_type::conditions_type;
19471947
constexpr bool hasExplicitFrom = tuple_has<conditions_tuple, is_from>::value;
19481948
if constexpr (!hasExplicitFrom) {
1949-
using joins_index_sequence = filter_tuple_sequence_t<conditions_tuple, is_constrained_join>;
1950-
using cross_join_index_sequence = filter_tuple_sequence_t<conditions_tuple, is_cross_join>;
1949+
using joins_index_sequence = filter_tuple_sequence_t<conditions_tuple, is_any_join>;
19511950

19521951
auto tableNames = collect_table_names(sel, context);
1953-
auto joinLambda = [&tableNames, &context](auto& join) {
1954-
using original_join_type = typename std::remove_reference_t<decltype(join)>::type;
1955-
using cross_join_type = mapped_type_proxy_t<original_join_type>;
1956-
std::pair<const std::string&, std::string> tableNameWithAlias{
1957-
lookup_table_name<cross_join_type>(context.db_objects),
1958-
alias_extractor<original_join_type>::as_alias()};
1959-
tableNames.erase(tableNameWithAlias);
1960-
};
19611952
// deduplicate table names of constrained join statements
1962-
iterate_tuple(sel.conditions, joins_index_sequence{}, joinLambda);
1963-
iterate_tuple(sel.conditions, cross_join_index_sequence{}, joinLambda);
1953+
iterate_tuple(sel.conditions, joins_index_sequence{}, [&tableNames, &context](auto& join) {
1954+
using original_join_type = typename std::remove_reference_t<decltype(join)>::type;
1955+
using join_type = mapped_type_proxy_t<original_join_type>;
1956+
1957+
const auto& tableName = lookup_table_name<join_type>(context.db_objects);
1958+
auto it = std::find_if(tableNames.begin(), tableNames.end(), [&tableName](const auto& pair) {
1959+
return pair.first == tableName;
1960+
});
1961+
if (it == tableNames.end()) {
1962+
return;
1963+
}
1964+
tableNames.erase(it);
1965+
});
19641966

19651967
if (!tableNames.empty() && !is_compound_operator<T>::value) {
19661968
ss << " FROM " << streaming_identifiers(tableNames);
@@ -2295,17 +2297,19 @@ namespace sqlite_orm {
22952297
using statement_type = Join;
22962298

22972299
template<class Ctx>
2298-
SQLITE_ORM_STATIC_CALLOP std::string operator()(const statement_type& join,
2300+
SQLITE_ORM_STATIC_CALLOP std::string operator()(const statement_type& /*join*/,
22992301
const Ctx& context) SQLITE_ORM_OR_CONST_CALLOP {
23002302
std::stringstream ss;
2301-
if constexpr (polyfill::is_specialization_of<Join, cross_join_t>::value) {
2303+
if constexpr (polyfill::is_specialization_of<statement_type, cross_join_t>::value) {
23022304
ss << "CROSS JOIN";
2303-
} else if constexpr (polyfill::is_specialization_of<Join, natural_join_t>::value) {
2305+
} else if constexpr (polyfill::is_specialization_of<statement_type, natural_join_t>::value) {
23042306
ss << "NATURAL JOIN";
23052307
} else {
23062308
static_assert(polyfill::always_false_v<statement_type>);
23072309
}
2308-
ss << " " << streaming_identifier(lookup_table_name<type_t<Join>>(context.db_objects));
2310+
ss << " "
2311+
<< streaming_identifier(
2312+
lookup_table_name<mapped_type_proxy_t<type_t<statement_type>>>(context.db_objects));
23092313
return ss.str();
23102314
}
23112315
};

include/sqlite_orm/sqlite_orm.h

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5198,8 +5198,6 @@ namespace sqlite_orm {
51985198
}
51995199

52005200
// #include "ast/cross_join.h"
5201-
// #include "../functional/config.h"
5202-
52035201
// #include "../functional/cxx_type_traits_polyfill.h"
52045202

52055203
namespace sqlite_orm {
@@ -5213,9 +5211,6 @@ namespace sqlite_orm {
52135211
struct cross_join_t {
52145212
using type = T;
52155213
};
5216-
5217-
template<class T>
5218-
using is_cross_join = polyfill::is_specialization_of<T, cross_join_t>;
52195214
}
52205215
}
52215216

@@ -5234,7 +5229,6 @@ SQLITE_ORM_EXPORT namespace sqlite_orm {
52345229
namespace sqlite_orm {
52355230

52365231
namespace internal {
5237-
52385232
/**
52395233
* Collated something
52405234
*/
@@ -5933,6 +5927,13 @@ namespace sqlite_orm {
59335927

59345928
template<class T>
59355929
using is_constrained_join = polyfill::is_detected<on_type_t, T>;
5930+
5931+
template<class T>
5932+
using is_any_join = mpl::invoke_t<mpl::disjunction<check_if<is_constrained_join>,
5933+
check_if_is_template<cross_join_t>,
5934+
check_if_is_template<natural_join_t>>,
5935+
T>;
5936+
59365937
}
59375938
}
59385939

@@ -22366,21 +22367,23 @@ namespace sqlite_orm {
2236622367
using conditions_tuple = typename statement_type::conditions_type;
2236722368
constexpr bool hasExplicitFrom = tuple_has<conditions_tuple, is_from>::value;
2236822369
if constexpr (!hasExplicitFrom) {
22369-
using joins_index_sequence = filter_tuple_sequence_t<conditions_tuple, is_constrained_join>;
22370-
using cross_join_index_sequence = filter_tuple_sequence_t<conditions_tuple, is_cross_join>;
22370+
using joins_index_sequence = filter_tuple_sequence_t<conditions_tuple, is_any_join>;
2237122371

2237222372
auto tableNames = collect_table_names(sel, context);
22373-
auto joinLambda = [&tableNames, &context](auto& join) {
22374-
using original_join_type = typename std::remove_reference_t<decltype(join)>::type;
22375-
using cross_join_type = mapped_type_proxy_t<original_join_type>;
22376-
std::pair<const std::string&, std::string> tableNameWithAlias{
22377-
lookup_table_name<cross_join_type>(context.db_objects),
22378-
alias_extractor<original_join_type>::as_alias()};
22379-
tableNames.erase(tableNameWithAlias);
22380-
};
2238122373
// deduplicate table names of constrained join statements
22382-
iterate_tuple(sel.conditions, joins_index_sequence{}, joinLambda);
22383-
iterate_tuple(sel.conditions, cross_join_index_sequence{}, joinLambda);
22374+
iterate_tuple(sel.conditions, joins_index_sequence{}, [&tableNames, &context](auto& join) {
22375+
using original_join_type = typename std::remove_reference_t<decltype(join)>::type;
22376+
using join_type = mapped_type_proxy_t<original_join_type>;
22377+
22378+
const auto& tableName = lookup_table_name<join_type>(context.db_objects);
22379+
auto it = std::find_if(tableNames.begin(), tableNames.end(), [&tableName](const auto& pair) {
22380+
return pair.first == tableName;
22381+
});
22382+
if (it == tableNames.end()) {
22383+
return;
22384+
}
22385+
tableNames.erase(it);
22386+
});
2238422387

2238522388
if (!tableNames.empty() && !is_compound_operator<T>::value) {
2238622389
ss << " FROM " << streaming_identifiers(tableNames);
@@ -22715,17 +22718,19 @@ namespace sqlite_orm {
2271522718
using statement_type = Join;
2271622719

2271722720
template<class Ctx>
22718-
SQLITE_ORM_STATIC_CALLOP std::string operator()(const statement_type& join,
22721+
SQLITE_ORM_STATIC_CALLOP std::string operator()(const statement_type& /*join*/,
2271922722
const Ctx& context) SQLITE_ORM_OR_CONST_CALLOP {
2272022723
std::stringstream ss;
22721-
if constexpr (polyfill::is_specialization_of<Join, cross_join_t>::value) {
22724+
if constexpr (polyfill::is_specialization_of<statement_type, cross_join_t>::value) {
2272222725
ss << "CROSS JOIN";
22723-
} else if constexpr (polyfill::is_specialization_of<Join, natural_join_t>::value) {
22726+
} else if constexpr (polyfill::is_specialization_of<statement_type, natural_join_t>::value) {
2272422727
ss << "NATURAL JOIN";
2272522728
} else {
2272622729
static_assert(polyfill::always_false_v<statement_type>);
2272722730
}
22728-
ss << " " << streaming_identifier(lookup_table_name<type_t<Join>>(context.db_objects));
22731+
ss << " "
22732+
<< streaming_identifier(
22733+
lookup_table_name<mapped_type_proxy_t<type_t<statement_type>>>(context.db_objects));
2272922734
return ss.str();
2273022735
}
2273122736
};

tests/statement_serializer_tests/ast/cross_join.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@ TEST_CASE("cross_join") {
1616
auto dbObjects = db_objects_t{table};
1717
using context_t = internal::serializer_context<db_objects_t>;
1818
context_t context{dbObjects};
19-
auto node = cross_join<User>();
20-
auto value = serialize(node, context);
19+
std::string value;
20+
SECTION("straight") {
21+
auto node = cross_join<User>();
22+
value = serialize(node, context);
23+
}
24+
SECTION("alias") {
25+
using user_s = alias_s<User>;
26+
auto node = cross_join<user_s>();
27+
value = serialize(node, context);
28+
}
2129
REQUIRE(value == R"(CROSS JOIN "users")");
2230
}

tests/statement_serializer_tests/statements/select.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,17 @@ TEST_CASE("statement_serializer select_t") {
308308
db_objects_t dbObjects{rankTable, suitTable};
309309
internal::serializer_context<db_objects_t> context{dbObjects};
310310

311-
auto expression = select(columns(&Rank::rank, &Suit::suit), cross_join<Suit>(), order_by(&Suit::suit));
312-
expression.highest_level = true;
313-
stringValue = serialize(expression, context);
311+
SECTION("straight") {
312+
auto expression = select(columns(&Rank::rank, &Suit::suit), cross_join<Suit>(), order_by(&Suit::suit));
313+
expression.highest_level = true;
314+
stringValue = serialize(expression, context);
315+
}
316+
SECTION("alias") {
317+
using suit_s = alias_s<Suit>;
318+
auto expression = select(columns(&Rank::rank, &Suit::suit), cross_join<suit_s>(), order_by(&Suit::suit));
319+
expression.highest_level = true;
320+
stringValue = serialize(expression, context);
321+
}
314322
expected = R"(SELECT "ranks"."rank", "suits"."suit" FROM "ranks" CROSS JOIN "suits" ORDER BY "suits"."suit")";
315323
}
316324
REQUIRE(stringValue == expected);

0 commit comments

Comments
 (0)