Skip to content

Commit 7833b2e

Browse files
authored
Merge pull request #1296 from fnc12/feature/content
added content
2 parents 58a1abc + 4a62772 commit 7833b2e

File tree

7 files changed

+89
-6
lines changed

7 files changed

+89
-6
lines changed

dev/constraints.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,13 @@ namespace sqlite_orm {
156156
value_type value;
157157
};
158158

159+
template<class T>
160+
struct content_t {
161+
using value_type = T;
162+
163+
value_type value;
164+
};
165+
159166
/**
160167
* DEFAULT constraint class.
161168
* T is a value type.
@@ -563,6 +570,16 @@ namespace sqlite_orm {
563570
return {std::move(value)};
564571
}
565572

573+
/**
574+
* content='' table constraint builder function. Used in FTS virtual tables.
575+
*
576+
* https://www.sqlite.org/fts5.html#contentless_tables
577+
*/
578+
template<class T>
579+
internal::content_t<T> content(T value) {
580+
return {std::move(value)};
581+
}
582+
566583
/**
567584
* PRIMARY KEY table constraint builder function.
568585
*/

dev/schema/table.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ namespace sqlite_orm {
3636
check_if_is_template<unique_t>,
3737
check_if_is_template<check_t>,
3838
check_if_is_template<prefix_t>,
39-
check_if_is_template<tokenize_t>>,
39+
check_if_is_template<tokenize_t>,
40+
check_if_is_template<content_t>>,
4041
T>;
4142

4243
#ifdef SQLITE_ORM_WITH_CTE

dev/statement_serializer.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,18 @@ namespace sqlite_orm {
10791079
}
10801080
};
10811081

1082+
template<class T>
1083+
struct statement_serializer<content_t<T>, void> {
1084+
using statement_type = content_t<T>;
1085+
1086+
template<class Ctx>
1087+
std::string operator()(const statement_type& statement, const Ctx& context) const {
1088+
std::stringstream ss;
1089+
ss << "content=" << serialize(statement.value, context);
1090+
return ss.str();
1091+
}
1092+
};
1093+
10821094
template<>
10831095
struct statement_serializer<collate_constraint_t, void> {
10841096
using statement_type = collate_constraint_t;
@@ -1094,8 +1106,8 @@ namespace sqlite_orm {
10941106
using statement_type = default_t<T>;
10951107

10961108
template<class Ctx>
1097-
std::string operator()(const statement_type& c, const Ctx& context) const {
1098-
return static_cast<std::string>(c) + " (" + serialize(c.value, context) + ")";
1109+
std::string operator()(const statement_type& statement, const Ctx& context) const {
1110+
return static_cast<std::string>(statement) + " (" + serialize(statement.value, context) + ")";
10991111
}
11001112
};
11011113

include/sqlite_orm/sqlite_orm.h

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1935,6 +1935,13 @@ namespace sqlite_orm {
19351935
value_type value;
19361936
};
19371937

1938+
template<class T>
1939+
struct content_t {
1940+
using value_type = T;
1941+
1942+
value_type value;
1943+
};
1944+
19381945
/**
19391946
* DEFAULT constraint class.
19401947
* T is a value type.
@@ -2342,6 +2349,16 @@ namespace sqlite_orm {
23422349
return {std::move(value)};
23432350
}
23442351

2352+
/**
2353+
* content='' table constraint builder function. Used in FTS virtual tables.
2354+
*
2355+
* https://www.sqlite.org/fts5.html#contentless_tables
2356+
*/
2357+
template<class T>
2358+
internal::content_t<T> content(T value) {
2359+
return {std::move(value)};
2360+
}
2361+
23452362
/**
23462363
* PRIMARY KEY table constraint builder function.
23472364
*/
@@ -11419,7 +11436,8 @@ namespace sqlite_orm {
1141911436
check_if_is_template<unique_t>,
1142011437
check_if_is_template<check_t>,
1142111438
check_if_is_template<prefix_t>,
11422-
check_if_is_template<tokenize_t>>,
11439+
check_if_is_template<tokenize_t>,
11440+
check_if_is_template<content_t>>,
1142311441
T>;
1142411442

1142511443
#ifdef SQLITE_ORM_WITH_CTE
@@ -19617,6 +19635,18 @@ namespace sqlite_orm {
1961719635
}
1961819636
};
1961919637

19638+
template<class T>
19639+
struct statement_serializer<content_t<T>, void> {
19640+
using statement_type = content_t<T>;
19641+
19642+
template<class Ctx>
19643+
std::string operator()(const statement_type& statement, const Ctx& context) const {
19644+
std::stringstream ss;
19645+
ss << "content=" << serialize(statement.value, context);
19646+
return ss.str();
19647+
}
19648+
};
19649+
1962019650
template<>
1962119651
struct statement_serializer<collate_constraint_t, void> {
1962219652
using statement_type = collate_constraint_t;
@@ -19632,8 +19662,8 @@ namespace sqlite_orm {
1963219662
using statement_type = default_t<T>;
1963319663

1963419664
template<class Ctx>
19635-
std::string operator()(const statement_type& c, const Ctx& context) const {
19636-
return static_cast<std::string>(c) + " (" + serialize(c.value, context) + ")";
19665+
std::string operator()(const statement_type& statement, const Ctx& context) const {
19666+
return static_cast<std::string>(statement) + " (" + serialize(statement.value, context) + ")";
1963719667
}
1963819668
};
1963919669

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ add_executable(unit_tests
100100
statement_serializer_tests/table_constraints/prefix.cpp
101101
statement_serializer_tests/table_constraints/foreign_key.cpp
102102
statement_serializer_tests/table_constraints/tokenize.cpp
103+
statement_serializer_tests/table_constraints/content.cpp
103104
statement_serializer_tests/bindables.cpp
104105
statement_serializer_tests/ast/upsert_clause.cpp
105106
statement_serializer_tests/ast/excluded.cpp

tests/statement_serializer_tests/schema/using_fts5.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,10 @@ TEST_CASE("statement_serializer using_fts5") {
4343
expected = R"(USING FTS5("title", "body", tokenize = 'unicode61 remove_diacritics 1'))";
4444
}
4545
}
46+
SECTION("content") {
47+
auto node = using_fts5(make_column("title", &Post::title), make_column("body", &Post::body), content(""));
48+
value = serialize(node, context);
49+
expected = R"(USING FTS5("title", "body", content=''))";
50+
}
4651
REQUIRE(value == expected);
4752
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <sqlite_orm/sqlite_orm.h>
2+
#include <catch2/catch_all.hpp>
3+
4+
using namespace sqlite_orm;
5+
6+
TEST_CASE("statement_serializer content") {
7+
internal::db_objects_tuple<> storage;
8+
internal::serializer_context<internal::db_objects_tuple<>> context{storage};
9+
std::string value;
10+
std::string expected;
11+
SECTION("empty") {
12+
auto node = content("");
13+
value = serialize(node, context);
14+
expected = "content=''";
15+
}
16+
REQUIRE(value == expected);
17+
}

0 commit comments

Comments
 (0)