Skip to content

Commit

Permalink
improved algo
Browse files Browse the repository at this point in the history
  • Loading branch information
fnc12 committed May 5, 2024
1 parent df91a2b commit 3f6a287
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 54 deletions.
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_except_unindexed = false;
bool fts5_columns = false;
};

template<class DBOs>
Expand Down
30 changes: 19 additions & 11 deletions dev/serializing_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -406,18 +406,26 @@ namespace sqlite_orm {
const bool& isNotNull = std::get<2>(tpl);
auto& context = std::get<3>(tpl);

iterate_tuple(column.constraints, [&ss, &context](auto& constraint) {
ss << ' ' << serialize(constraint, context);
});
using constraints_tuple = decltype(column.constraints);
constexpr bool hasExplicitNullableConstraint =
mpl::invoke_t<mpl::disjunction<check_if_has_type<null_t>, check_if_has_type<not_null_t>>,
constraints_tuple>::value;
if(!hasExplicitNullableConstraint) {
if(isNotNull) {
ss << " NOT NULL";
} else {
ss << " NULL";
if(!context.fts5_columns) {
iterate_tuple(column.constraints, [&ss, &context](auto& constraint) {
ss << ' ' << serialize(constraint, context);
});
constexpr bool hasExplicitNullableConstraint =
mpl::invoke_t<mpl::disjunction<check_if_has_type<null_t>, check_if_has_type<not_null_t>>,
constraints_tuple>::value;
if SQLITE_ORM_CONSTEXPR_IF(!hasExplicitNullableConstraint) {
if(isNotNull) {
ss << " NOT NULL";
} else {
ss << " NULL";
}
}
} else {
constexpr bool hasUnindexedOption =
mpl::invoke_t<check_if_has_type<unindexed_t>, constraints_tuple>::value;
if SQLITE_ORM_CONSTEXPR_IF(hasUnindexedOption) {
ss << " UNINDEXED";
}
}

Expand Down
21 changes: 7 additions & 14 deletions dev/statement_serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -1146,22 +1146,15 @@ namespace sqlite_orm {

template<class Ctx>
std::string operator()(const statement_type& column, const Ctx& context) const {
using column_type = statement_type;

std::stringstream ss;
ss << streaming_identifier(column.name);
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";
}
if(!context.fts5_columns) {
ss << " " << type_printer<field_type_t<column_field<G, S>>>().print();
}
ss << streaming_column_constraints(
call_as_template_base<column_constraints>(polyfill::identity{})(column),
column.is_not_null(),
context);
return ss.str();
}
};
Expand Down Expand Up @@ -1773,7 +1766,7 @@ namespace sqlite_orm {
std::stringstream ss;
ss << "USING FTS5(";
auto subContext = context;
subContext.skip_types_and_constraints_except_unindexed = true;
subContext.fts5_columns = true;
ss << streaming_expressions_tuple(statement.columns, subContext) << ")";
return ss.str();
}
Expand Down
53 changes: 27 additions & 26 deletions include/sqlite_orm/sqlite_orm.h
Original file line number Diff line number Diff line change
Expand Up @@ -3248,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_except_unindexed = false;
bool fts5_columns = false;
};

template<class DBOs>
Expand Down Expand Up @@ -15679,18 +15679,26 @@ namespace sqlite_orm {
const bool& isNotNull = std::get<2>(tpl);
auto& context = std::get<3>(tpl);

iterate_tuple(column.constraints, [&ss, &context](auto& constraint) {
ss << ' ' << serialize(constraint, context);
});
using constraints_tuple = decltype(column.constraints);
constexpr bool hasExplicitNullableConstraint =
mpl::invoke_t<mpl::disjunction<check_if_has_type<null_t>, check_if_has_type<not_null_t>>,
constraints_tuple>::value;
if(!hasExplicitNullableConstraint) {
if(isNotNull) {
ss << " NOT NULL";
} else {
ss << " NULL";
if(!context.fts5_columns) {
iterate_tuple(column.constraints, [&ss, &context](auto& constraint) {
ss << ' ' << serialize(constraint, context);
});
constexpr bool hasExplicitNullableConstraint =
mpl::invoke_t<mpl::disjunction<check_if_has_type<null_t>, check_if_has_type<not_null_t>>,
constraints_tuple>::value;
if SQLITE_ORM_CONSTEXPR_IF(!hasExplicitNullableConstraint) {
if(isNotNull) {
ss << " NOT NULL";
} else {
ss << " NULL";
}
}
} else {
constexpr bool hasUnindexedOption =
mpl::invoke_t<check_if_has_type<unindexed_t>, constraints_tuple>::value;
if SQLITE_ORM_CONSTEXPR_IF(hasUnindexedOption) {
ss << " UNINDEXED";
}
}

Expand Down Expand Up @@ -19220,22 +19228,15 @@ namespace sqlite_orm {

template<class Ctx>
std::string operator()(const statement_type& column, const Ctx& context) const {
using column_type = statement_type;

std::stringstream ss;
ss << streaming_identifier(column.name);
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";
}
if(!context.fts5_columns) {
ss << " " << type_printer<field_type_t<column_field<G, S>>>().print();
}
ss << streaming_column_constraints(
call_as_template_base<column_constraints>(polyfill::identity{})(column),
column.is_not_null(),
context);
return ss.str();
}
};
Expand Down Expand Up @@ -19847,7 +19848,7 @@ namespace sqlite_orm {
std::stringstream ss;
ss << "USING FTS5(";
auto subContext = context;
subContext.skip_types_and_constraints_except_unindexed = true;
subContext.fts5_columns = true;
ss << streaming_expressions_tuple(statement.columns, subContext) << ")";
return ss.str();
}
Expand Down
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_except_unindexed = false;
context.fts5_columns = 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_except_unindexed = true;
context.fts5_columns = true;
SECTION("id INTEGER NOT NULL") {
auto column = make_column("id", &User::id);
value = serialize(column, context);
Expand Down

0 comments on commit 3f6a287

Please sign in to comment.