Skip to content

Commit 32fef04

Browse files
committed
Applied changes requested in PR review
1 parent 60eb1a8 commit 32fef04

File tree

7 files changed

+35
-39
lines changed

7 files changed

+35
-39
lines changed

dev/alias.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <utility> // std::make_index_sequence, std::move
55
#include <string> // std::string
66
#include <sstream> // std::stringstream
7-
#include <string> // std::string
87
#ifdef SQLITE_ORM_WITH_CTE
98
#include <array>
109
#endif

dev/select_constraints.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ namespace sqlite_orm {
243243
this->subselect.highest_level = true;
244244
}
245245
};
246+
246247
template<class... CTEs>
247248
using common_table_expressions = std::tuple<CTEs...>;
248249

dev/statement_serializer.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,16 +590,17 @@ namespace sqlite_orm {
590590
using statement_type = materialized_t;
591591

592592
template<class Ctx>
593-
std::string operator()(const statement_type& /*statement*/, const Ctx& /*context*/) {
593+
std::string operator()(const statement_type& /*statement*/, const Ctx& /*context*/) const {
594594
return "MATERIALIZED";
595595
}
596596
};
597+
597598
template<>
598599
struct statement_serializer<not_materialized_t, void> {
599600
using statement_type = not_materialized_t;
600601

601602
template<class Ctx>
602-
std::string operator()(const statement_type& /*statement*/, const Ctx& /*context*/) {
603+
std::string operator()(const statement_type& /*statement*/, const Ctx& /*context*/) const {
603604
return "NOT MATERIALIZED";
604605
}
605606
};

examples/common_table_expressions.cpp

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,17 @@ void select_from_subselect() {
542542
return true;
543543
});
544544

545+
// alternative way of writing a subselect by using a CTE.
546+
//
547+
// original select from subquery:
545548
// SELECT * FROM (SELECT salary, comm AS commmission FROM emp) WHERE salary < 5000
549+
//
550+
// with CTE:
551+
// WITH
552+
// sub AS(
553+
// SELECT salary, comm AS commmission FROM emp) WHERE salary < 5000
554+
// )
555+
// SELECT * from sub;
546556
#ifdef SQLITE_ORM_WITH_CPP20_ALIASES
547557
constexpr auto sub = "sub"_cte;
548558
auto expression = with(sub().as(select(columns(&Employee::m_salary, &Employee::m_commission))),
@@ -776,7 +786,7 @@ void show_optimization_fence() {
776786

777787
void show_mapping_and_backreferencing() {
778788
struct Object {
779-
int64 id;
789+
int64 id = 0;
780790
};
781791

782792
// column alias
@@ -790,7 +800,8 @@ void show_mapping_and_backreferencing() {
790800
auto storage = make_storage("", make_table("object", make_column("id", &Object::id)));
791801
storage.sync_schema();
792802

793-
// back-reference via `column_pointer<cte_1, F O::*>`
803+
// back-reference via `column_pointer<cte_1, F O::*>`;
804+
// WITH "1"("id") AS (SELECT "object"."id" FROM "object") SELECT "1"."id" FROM "1"
794805
{
795806
auto ast = with(cte<cte_1>().as(select(&Object::id)), select(column<cte_1>(&Object::id)));
796807

@@ -799,7 +810,8 @@ void show_mapping_and_backreferencing() {
799810
}
800811

801812
// map column via alias_holder into cte,
802-
// back-reference via `column_pointer<cte_1, alias_holder>`
813+
// back-reference via `column_pointer<cte_1, alias_holder>`;
814+
// WITH "1"("x") AS (SELECT 1 AS "counter" UNION ALL SELECT "1"."x" + 1 FROM "1" LIMIT 10) SELECT "1"."x" FROM "1"
803815
{
804816
auto ast =
805817
with(cte<cte_1>("x").as(union_all(select(as<cnt>(1)), select(column<cte_1>(get<cnt>()) + 1, limit(10)))),
@@ -809,7 +821,8 @@ void show_mapping_and_backreferencing() {
809821
auto stmt = storage.prepare(ast);
810822
}
811823
// map column via alias_holder into cte,
812-
// back-reference via `column_pointer<cte_1, alias_holder>`
824+
// back-reference via `column_pointer<cte_1, alias_holder>`;
825+
// WITH "1"("x") AS (SELECT 1 AS "counter" UNION ALL SELECT "1"."x" + 1 FROM "1" LIMIT 10) SELECT "1"."x" FROM "1"
813826
{
814827
auto ast = with(cte<cte_1>("x").as(union_all(select(as<cnt>(1)), select(column<cte_1>(cnt{}) + 1, limit(10)))),
815828
select(column<cte_1>(cnt{})));
@@ -818,31 +831,35 @@ void show_mapping_and_backreferencing() {
818831
auto stmt = storage.prepare(ast);
819832
}
820833

821-
// implicitly remap column into cte
834+
// implicitly remap column into cte;
835+
// WITH "1"("id") AS (SELECT "object"."id" FROM "object") SELECT "1"."id" FROM "1"
822836
{
823837
auto ast = with(cte<cte_1>(std::ignore).as(select(&Object::id)), select(column<cte_1>(&Object::id)));
824838

825839
string sql = storage.dump(ast);
826840
auto stmt = storage.prepare(ast);
827841
}
828842

829-
// explicitly remap column into cte (independent of subselect)
843+
// explicitly remap column into cte (independent of subselect);
844+
// WITH "1"("id") AS (SELECT "object"."id" FROM "object") SELECT "1"."id" FROM "1"
830845
{
831846
auto ast = with(cte<cte_1>(&Object::id).as(select(&Object::id)), select(column<cte_1>(&Object::id)));
832847

833848
string sql = storage.dump(ast);
834849
auto stmt = storage.prepare(ast);
835850
}
836851

837-
// explicitly remap column as an alias into cte (independent of subselect)
852+
// explicitly remap column as an alias into cte (independent of subselect);
853+
// WITH "1"("counter") AS (SELECT "object"."id" FROM "object") SELECT "1"."counter" FROM "1"
838854
{
839855
auto ast = with(cte<cte_1>(cnt{}).as(select(&Object::id)), select(column<cte_1>(get<cnt>())));
840856

841857
string sql = storage.dump(ast);
842858
auto stmt = storage.prepare(ast);
843859
}
844860

845-
// explicitly state that column name should be taken from subselect
861+
// explicitly state that column name should be taken from subselect;
862+
// WITH "CTEObj"("xyz") AS (SELECT "object"."id" FROM "object") SELECT "CTEObj"."xyz" FROM "CTEObj"
846863
{
847864
struct CTEObject : alias_tag {
848865
// a CTE object is its own table alias
@@ -852,7 +869,7 @@ void show_mapping_and_backreferencing() {
852869
return "CTEObj";
853870
}
854871

855-
int64 xyz;
872+
int64 xyz = 0;
856873
};
857874
auto ast =
858875
with(cte<CTEObject>(make_column("xyz", &CTEObject::xyz)).as(select(&Object::id)), select(&CTEObject::xyz));
@@ -964,7 +981,7 @@ void greatest_n_per_group() {
964981
#ifdef SQLITE_ORM_WITH_CPP20_ALIASES
965982
// Having a table consisting of multiple results for items,
966983
// I want to select a single result row per item, based on a condition like an aggregated value.
967-
// This is possible using a (self) join and filtering by aggregated value (in case it is unique), or using window functions.
984+
// This is possible using a (self) join and filtering by aggregated value (in case it is unique) or using window functions.
968985
//
969986
// In this example, we select the most recent (successful) result per item
970987

include/sqlite_orm/sqlite_orm.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4971,7 +4971,6 @@ namespace sqlite_orm {
49714971
#include <utility> // std::make_index_sequence, std::move
49724972
#include <string> // std::string
49734973
#include <sstream> // std::stringstream
4974-
#include <string> // std::string
49754974
#ifdef SQLITE_ORM_WITH_CTE
49764975
#include <array>
49774976
#endif
@@ -8296,6 +8295,7 @@ namespace sqlite_orm {
82968295
this->subselect.highest_level = true;
82978296
}
82988297
};
8298+
82998299
template<class... CTEs>
83008300
using common_table_expressions = std::tuple<CTEs...>;
83018301

@@ -18651,16 +18651,17 @@ namespace sqlite_orm {
1865118651
using statement_type = materialized_t;
1865218652

1865318653
template<class Ctx>
18654-
std::string operator()(const statement_type& /*statement*/, const Ctx& /*context*/) {
18654+
std::string_view operator()(const statement_type& /*statement*/, const Ctx& /*context*/) const {
1865518655
return "MATERIALIZED";
1865618656
}
1865718657
};
18658+
1865818659
template<>
1865918660
struct statement_serializer<not_materialized_t, void> {
1866018661
using statement_type = not_materialized_t;
1866118662

1866218663
template<class Ctx>
18663-
std::string operator()(const statement_type& /*statement*/, const Ctx& /*context*/) {
18664+
std::string_view operator()(const statement_type& /*statement*/, const Ctx& /*context*/) const {
1866418665
return "NOT MATERIALIZED";
1866518666
}
1866618667
};

tests/ast_iterator_tests.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,14 +336,12 @@ TEST_CASE("ast_iterator") {
336336
auto expression = with(cte<cte_1>().as(select(1)), select(column<cte_1>(1_colalias)));
337337
expected.insert(expected.cend(), {typeid(int), typeid(column_pointer<cte_1, alias_holder<column_alias<'1'>>>)});
338338
iterate_ast(expression, lambda);
339-
REQUIRE(typeIndexes == expected);
340339
}
341340
SECTION("with not enforced recursive") {
342341
using cte_1 = decltype(1_ctealias);
343342
auto expression = with_recursive(cte<cte_1>().as(select(1)), select(column<cte_1>(1_colalias)));
344343
expected.insert(expected.cend(), {typeid(int), typeid(column_pointer<cte_1, alias_holder<column_alias<'1'>>>)});
345344
iterate_ast(expression, lambda);
346-
REQUIRE(typeIndexes == expected);
347345
}
348346
SECTION("with optional recursive") {
349347
using cte_1 = decltype(1_ctealias);
@@ -359,7 +357,6 @@ TEST_CASE("ast_iterator") {
359357
typeid(int),
360358
typeid(column_pointer<cte_1, alias_holder<column_alias<'1'>>>)});
361359
iterate_ast(expression, lambda);
362-
REQUIRE(typeIndexes == expected);
363360
}
364361
SECTION("with recursive") {
365362
using cte_1 = decltype(1_ctealias);
@@ -375,7 +372,6 @@ TEST_CASE("ast_iterator") {
375372
typeid(int),
376373
typeid(column_pointer<cte_1, alias_holder<column_alias<'1'>>>)});
377374
iterate_ast(expression, lambda);
378-
REQUIRE(typeIndexes == expected);
379375
}
380376
#ifdef SQLITE_ORM_WITH_CPP20_ALIASES
381377
SECTION("aliased CTE column pointer") {
@@ -385,7 +381,6 @@ TEST_CASE("ast_iterator") {
385381
auto expression = z_alias->*&User::id;
386382
expected.push_back(typeid(alias_column_t<alias_z<cte_1>, column_pointer<cte_1, decltype(&User::id)>>));
387383
iterate_ast(expression, lambda);
388-
REQUIRE(typeIndexes == expected);
389384
}
390385
SECTION("aliased CTE column alias") {
391386
constexpr auto c = "1"_cte;
@@ -395,7 +390,6 @@ TEST_CASE("ast_iterator") {
395390
expected.push_back(
396391
typeid(alias_column_t<alias_z<cte_1>, column_pointer<cte_1, alias_holder<column_alias<'1'>>>>));
397392
iterate_ast(expression, lambda);
398-
REQUIRE(typeIndexes == expected);
399393
}
400394
#endif
401395
#endif

tests/statement_serializer_tests/select_constraints.cpp

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -103,65 +103,55 @@ TEST_CASE("statement_serializer select constraints") {
103103
auto expression = from<cte_1>();
104104
value = serialize(expression, context);
105105
expected = R"(FROM "1")";
106-
REQUIRE(value == expected);
107106
}
108107
SECTION("with alias 1") {
109108
auto expression = from<alias_z<cte_1>>();
110109
value = serialize(expression, context);
111110
expected = R"(FROM "1" "z")";
112-
REQUIRE(value == expected);
113111
}
114112
#ifdef SQLITE_ORM_WITH_CPP20_ALIASES
115113
SECTION("without alias 2") {
116114
auto expression = from<1_ctealias>();
117115
value = serialize(expression, context);
118116
expected = R"(FROM "1")";
119-
REQUIRE(value == expected);
120117
}
121118
SECTION("with alias 2") {
122119
constexpr auto z_alias = "z"_alias.for_<1_ctealias>();
123120
auto expression = from<z_alias>();
124121
value = serialize(expression, context);
125122
expected = R"(FROM "1" "z")";
126-
REQUIRE(value == expected);
127123
}
128124
SECTION("as") {
129125
auto expression = cte<cte_1>().as(select(1));
130126
value = serialize(expression, context);
131127
expected = R"("1"("1") AS (SELECT 1))";
132-
REQUIRE(value == expected);
133128
}
134129
SECTION("as materialized") {
135130
auto expression = cte<cte_1>().as<materialized()>(select(1));
136131
value = serialize(expression, context);
137132
expected = R"("1"("1") AS MATERIALIZED (SELECT 1))";
138-
REQUIRE(value == expected);
139133
}
140134
SECTION("as not materialized") {
141135
auto expression = cte<cte_1>().as<not_materialized()>(select(1));
142136
value = serialize(expression, context);
143137
expected = R"("1"("1") AS NOT MATERIALIZED (SELECT 1))";
144-
REQUIRE(value == expected);
145138
}
146139
#endif
147140
SECTION("with ordinary") {
148141
auto expression = with(cte<cte_1>().as(select(1)), select(column<cte_1>(1_colalias)));
149142
value = serialize(expression, context);
150143
expected = R"(WITH "1"("1") AS (SELECT 1) SELECT "1"."1" FROM "1")";
151-
REQUIRE(value == expected);
152144
}
153145
SECTION("with ordinary, compound") {
154146
auto expression = with(cte<cte_1>().as(select(1)),
155147
union_all(select(column<cte_1>(1_colalias)), select(column<cte_1>(1_colalias))));
156148
value = serialize(expression, context);
157149
expected = R"(WITH "1"("1") AS (SELECT 1) SELECT "1"."1" FROM "1" UNION ALL SELECT "1"."1" FROM "1")";
158-
REQUIRE(value == expected);
159150
}
160151
SECTION("with not enforced recursive") {
161152
auto expression = with_recursive(cte<cte_1>().as(select(1)), select(column<cte_1>(1_colalias)));
162153
value = serialize(expression, context);
163154
expected = R"(WITH RECURSIVE "1"("1") AS (SELECT 1) SELECT "1"."1" FROM "1")";
164-
REQUIRE(value == expected);
165155
}
166156
SECTION("with not enforced recursive, compound") {
167157
auto expression =
@@ -170,29 +160,25 @@ TEST_CASE("statement_serializer select constraints") {
170160
value = serialize(expression, context);
171161
expected =
172162
R"(WITH RECURSIVE "1"("1") AS (SELECT 1) SELECT "1"."1" FROM "1" UNION ALL SELECT "1"."1" FROM "1")";
173-
REQUIRE(value == expected);
174163
}
175164
SECTION("with ordinary, multiple") {
176165
auto expression = with(std::make_tuple(cte<cte_1>().as(select(1)), cte<cte_1>().as(select(1))),
177166
select(column<cte_1>(1_colalias)));
178167
value = serialize(expression, context);
179168
expected = R"(WITH "1"("1") AS (SELECT 1), "1"("1") AS (SELECT 1) SELECT "1"."1" FROM "1")";
180-
REQUIRE(value == expected);
181169
}
182170
SECTION("with ordinary, multiple, compound") {
183171
auto expression = with(std::make_tuple(cte<cte_1>().as(select(1)), cte<cte_1>().as(select(1))),
184172
union_all(select(column<cte_1>(1_colalias)), select(column<cte_1>(1_colalias))));
185173
value = serialize(expression, context);
186174
expected =
187175
R"(WITH "1"("1") AS (SELECT 1), "1"("1") AS (SELECT 1) SELECT "1"."1" FROM "1" UNION ALL SELECT "1"."1" FROM "1")";
188-
REQUIRE(value == expected);
189176
}
190177
SECTION("with not enforced recursive, multiple") {
191178
auto expression = with_recursive(std::make_tuple(cte<cte_1>().as(select(1)), cte<cte_1>().as(select(1))),
192179
select(column<cte_1>(1_colalias)));
193180
value = serialize(expression, context);
194181
expected = R"(WITH RECURSIVE "1"("1") AS (SELECT 1), "1"("1") AS (SELECT 1) SELECT "1"."1" FROM "1")";
195-
REQUIRE(value == expected);
196182
}
197183
SECTION("with not enforced recursive, multiple, compound") {
198184
auto expression =
@@ -201,7 +187,6 @@ TEST_CASE("statement_serializer select constraints") {
201187
value = serialize(expression, context);
202188
expected =
203189
R"(WITH RECURSIVE "1"("1") AS (SELECT 1), "1"("1") AS (SELECT 1) SELECT "1"."1" FROM "1" UNION ALL SELECT "1"."1" FROM "1")";
204-
REQUIRE(value == expected);
205190
}
206191
SECTION("with optional recursive") {
207192
auto expression = with(
@@ -211,7 +196,6 @@ TEST_CASE("statement_serializer select constraints") {
211196
value = serialize(expression, context);
212197
expected =
213198
R"(WITH "1"("1") AS (SELECT 1 UNION ALL SELECT "1"."1" + 1 FROM "1" WHERE ("1"."1" < 10)) SELECT "1"."1" FROM "1")";
214-
REQUIRE(value == expected);
215199
}
216200
SECTION("with recursive") {
217201
auto expression = with_recursive(
@@ -221,7 +205,6 @@ TEST_CASE("statement_serializer select constraints") {
221205
value = serialize(expression, context);
222206
expected =
223207
R"(WITH RECURSIVE "1"("1") AS (SELECT 1 UNION ALL SELECT "1"."1" + 1 FROM "1" WHERE ("1"."1" < 10)) SELECT "1"."1" FROM "1")";
224-
REQUIRE(value == expected);
225208
}
226209
}
227210
#endif

0 commit comments

Comments
 (0)