Skip to content

Commit

Permalink
added unindexed
Browse files Browse the repository at this point in the history
  • Loading branch information
fnc12 committed May 4, 2024
1 parent 50cb629 commit df91a2b
Show file tree
Hide file tree
Showing 12 changed files with 300 additions and 200 deletions.
10 changes: 10 additions & 0 deletions dev/constraints.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ namespace sqlite_orm {
unique_t(columns_tuple columns_) : columns(std::move(columns_)) {}
};

struct unindexed_t {};

/**
* DEFAULT constraint class.
* T is a value type.
Expand Down Expand Up @@ -471,6 +473,7 @@ namespace sqlite_orm {
check_if<is_foreign_key>,
check_if_is_type<null_t>,
check_if_is_type<not_null_t>,
check_if_is_type<unindexed_t>,
check_if_is_template<unique_t>,
check_if_is_template<default_t>,
check_if_is_template<check_t>,
Expand Down Expand Up @@ -514,6 +517,13 @@ namespace sqlite_orm {
return {{}};
}

/**
* UNINDEXED constraint builder function. Used in FTS virtual tables.
*/
inline internal::unindexed_t unindexed() {
return {};
}

template<class... Cs>
internal::primary_key_t<Cs...> primary_key(Cs... cs) {
return {std::make_tuple(std::forward<Cs>(cs)...)};
Expand Down
2 changes: 1 addition & 1 deletion dev/serializer_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace sqlite_orm {
bool replace_bindable_with_question = false;
bool skip_table_name = true;
bool use_parentheses = true;
bool skip_types_and_constraints = false;
bool skip_types_and_constraints_except_unindexed = false;
};

template<class DBOs>
Expand Down
21 changes: 18 additions & 3 deletions dev/statement_serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ namespace sqlite_orm {
template<class Ctx>
auto serialize(const statement_type& statement, const Ctx& context, const std::string& tableName) {
std::stringstream ss;
ss << "CREATE TABLE " << streaming_identifier(tableName) << " ( "
ss << "CREATE TABLE " << streaming_identifier(tableName) << " ("
<< streaming_expressions_tuple(statement.elements, context) << ")";
if(statement_type::is_without_rowid_v) {
ss << " WITHOUT ROWID";
Expand Down Expand Up @@ -1045,6 +1045,16 @@ namespace sqlite_orm {
}
};

template<>
struct statement_serializer<unindexed_t, void> {
using statement_type = unindexed_t;

template<class Ctx>
std::string operator()(const statement_type& c, const Ctx& context) const {
return "UNINDEXED";
}
};

template<>
struct statement_serializer<collate_constraint_t, void> {
using statement_type = collate_constraint_t;
Expand Down Expand Up @@ -1140,12 +1150,17 @@ namespace sqlite_orm {

std::stringstream ss;
ss << streaming_identifier(column.name);
if(!context.skip_types_and_constraints) {
if(!context.skip_types_and_constraints_except_unindexed) {
ss << " " << type_printer<field_type_t<column_type>>().print();
ss << streaming_column_constraints(
call_as_template_base<column_constraints>(polyfill::identity{})(column),
column.is_not_null(),
context);
} else {
using constraints_tuple = typename column_type::constraints_type;
if(tuple_has_type<constraints_tuple, unindexed_t>::value) {
ss << " UNINDEXED";
}
}
return ss.str();
}
Expand Down Expand Up @@ -1758,7 +1773,7 @@ namespace sqlite_orm {
std::stringstream ss;
ss << "USING FTS5(";
auto subContext = context;
subContext.skip_types_and_constraints = true;
subContext.skip_types_and_constraints_except_unindexed = true;
ss << streaming_expressions_tuple(statement.columns, subContext) << ")";
return ss.str();
}
Expand Down
33 changes: 29 additions & 4 deletions include/sqlite_orm/sqlite_orm.h
Original file line number Diff line number Diff line change
Expand Up @@ -1800,6 +1800,8 @@ namespace sqlite_orm {
unique_t(columns_tuple columns_) : columns(std::move(columns_)) {}
};

struct unindexed_t {};

/**
* DEFAULT constraint class.
* T is a value type.
Expand Down Expand Up @@ -2131,6 +2133,7 @@ namespace sqlite_orm {
check_if<is_foreign_key>,
check_if_is_type<null_t>,
check_if_is_type<not_null_t>,
check_if_is_type<unindexed_t>,
check_if_is_template<unique_t>,
check_if_is_template<default_t>,
check_if_is_template<check_t>,
Expand Down Expand Up @@ -2174,6 +2177,13 @@ namespace sqlite_orm {
return {{}};
}

/**
* UNINDEXED constraint builder function. Used in FTS virtual tables.
*/
inline internal::unindexed_t unindexed() {
return {};
}

template<class... Cs>
internal::primary_key_t<Cs...> primary_key(Cs... cs) {
return {std::make_tuple(std::forward<Cs>(cs)...)};
Expand Down Expand Up @@ -3238,7 +3248,7 @@ namespace sqlite_orm {
bool replace_bindable_with_question = false;
bool skip_table_name = true;
bool use_parentheses = true;
bool skip_types_and_constraints = false;
bool skip_types_and_constraints_except_unindexed = false;
};

template<class DBOs>
Expand Down Expand Up @@ -18215,7 +18225,7 @@ namespace sqlite_orm {
template<class Ctx>
auto serialize(const statement_type& statement, const Ctx& context, const std::string& tableName) {
std::stringstream ss;
ss << "CREATE TABLE " << streaming_identifier(tableName) << " ( "
ss << "CREATE TABLE " << streaming_identifier(tableName) << " ("
<< streaming_expressions_tuple(statement.elements, context) << ")";
if(statement_type::is_without_rowid_v) {
ss << " WITHOUT ROWID";
Expand Down Expand Up @@ -19109,6 +19119,16 @@ namespace sqlite_orm {
}
};

template<>
struct statement_serializer<unindexed_t, void> {
using statement_type = unindexed_t;

template<class Ctx>
std::string operator()(const statement_type& c, const Ctx& context) const {
return "UNINDEXED";
}
};

template<>
struct statement_serializer<collate_constraint_t, void> {
using statement_type = collate_constraint_t;
Expand Down Expand Up @@ -19204,12 +19224,17 @@ namespace sqlite_orm {

std::stringstream ss;
ss << streaming_identifier(column.name);
if(!context.skip_types_and_constraints) {
if(!context.skip_types_and_constraints_except_unindexed) {
ss << " " << type_printer<field_type_t<column_type>>().print();
ss << streaming_column_constraints(
call_as_template_base<column_constraints>(polyfill::identity{})(column),
column.is_not_null(),
context);
} else {
using constraints_tuple = typename column_type::constraints_type;
if(tuple_has_type<constraints_tuple, unindexed_t>::value) {
ss << " UNINDEXED";
}
}
return ss.str();
}
Expand Down Expand Up @@ -19822,7 +19847,7 @@ namespace sqlite_orm {
std::stringstream ss;
ss << "USING FTS5(";
auto subContext = context;
subContext.skip_types_and_constraints = true;
subContext.skip_types_and_constraints_except_unindexed = true;
ss << streaming_expressions_tuple(statement.columns, subContext) << ")";
return ss.str();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ add_executable(unit_tests
xdestroy_handling.cpp
sync_schema_tests.cpp
tests.cpp
tests2.cpp
tests3.cpp
tests4.cpp
tests5.cpp
Expand Down Expand Up @@ -96,6 +95,7 @@ add_executable(unit_tests
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/bindables.cpp
statement_serializer_tests/ast/upsert_clause.cpp
statement_serializer_tests/ast/excluded.cpp
Expand Down
2 changes: 0 additions & 2 deletions tests/schema/virtual_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ TEST_CASE("virtual table") {
Post{"SQLite Tutorial", "Help you learn SQLite quickly and effectively"},
};

// storage.remove_all<Post>();

/// INSERT INTO posts(title,body)
/// VALUES('Learn SQlite FTS5','This tutorial teaches you how to perform full-text search in SQLite using FTS5'),
/// ('Advanced SQlite Full-text Search','Show you some advanced techniques in SQLite full-text searching'),
Expand Down
12 changes: 12 additions & 0 deletions tests/statement_serializer_tests/column_constraints/unindexed.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <sqlite_orm/sqlite_orm.h>
#include <catch2/catch_all.hpp>

using namespace sqlite_orm;

TEST_CASE("statement_serializer unindexed") {
internal::db_objects_tuple<> storage;
internal::serializer_context<internal::db_objects_tuple<>> context{storage};
auto node = unindexed();
auto value = serialize(node, context);
REQUIRE(value == "UNINDEXED");
}
4 changes: 2 additions & 2 deletions tests/statement_serializer_tests/schema/column.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ TEST_CASE("statement_serializer column") {
std::string value;
std::string expected;
SECTION("with types and constraints") {
context.skip_types_and_constraints = false;
context.skip_types_and_constraints_except_unindexed = false;
SECTION("id INTEGER (implicit) NOT NULL") {
auto column = make_column("id", &User::id);
value = serialize(column, context);
Expand Down Expand Up @@ -82,7 +82,7 @@ TEST_CASE("statement_serializer column") {
}
}
SECTION("without types and constraints") {
context.skip_types_and_constraints = true;
context.skip_types_and_constraints_except_unindexed = true;
SECTION("id INTEGER NOT NULL") {
auto column = make_column("id", &User::id);
value = serialize(column, context);
Expand Down
33 changes: 33 additions & 0 deletions tests/statement_serializer_tests/schema/table.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <sqlite_orm/sqlite_orm.h>
#include <catch2/catch_all.hpp>

using namespace sqlite_orm;

TEST_CASE("statement_serializer table_t") {
struct User {
int id = 0;
std::string name;
};
std::string value;
std::string expected;
SECTION("simple") {
auto table = make_table("users", make_column("id", &User::id), make_column("name", &User::name));
using db_objects_t = internal::db_objects_tuple<decltype(table)>;
auto dbObjects = db_objects_t{table};
using context_t = internal::serializer_context<db_objects_t>;
context_t context{dbObjects};
value = internal::serialize(table, context);
expected = "CREATE TABLE \"users\" (\"id\" INTEGER NOT NULL, \"name\" TEXT NOT NULL)";
}
SECTION("without_rowid") {
auto table =
make_table("users", make_column("id", &User::id), make_column("name", &User::name)).without_rowid();
using db_objects_t = internal::db_objects_tuple<decltype(table)>;
auto dbObjects = db_objects_t{table};
using context_t = internal::serializer_context<db_objects_t>;
context_t context{dbObjects};
value = internal::serialize(table, context);
expected = "CREATE TABLE \"users\" (\"id\" INTEGER NOT NULL, \"name\" TEXT NOT NULL) WITHOUT ROWID";
}
REQUIRE(value == expected);
}
16 changes: 13 additions & 3 deletions tests/statement_serializer_tests/schema/using_fts5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,19 @@ TEST_CASE("statement_serializer using_fts5") {
std::string title;
std::string body;
};
std::string value;
std::string expected;
internal::db_objects_tuple<> storage;
internal::serializer_context<internal::db_objects_tuple<>> context{storage};
auto node = using_fts5(make_column("title", &Post::title), make_column("body", &Post::body));
auto value = serialize(node, context);
REQUIRE(value == "USING FTS5(\"title\", \"body\")");
SECTION("simple") {
auto node = using_fts5(make_column("title", &Post::title), make_column("body", &Post::body));
value = serialize(node, context);
expected = "USING FTS5(\"title\", \"body\")";
}
SECTION("unindexed") {
auto node = using_fts5(make_column("title", &Post::title), make_column("body", &Post::body, unindexed()));
value = serialize(node, context);
expected = "USING FTS5(\"title\", \"body\" UNINDEXED)";
}
REQUIRE(value == expected);
}
Loading

0 comments on commit df91a2b

Please sign in to comment.