Skip to content
This repository has been archived by the owner on Jan 3, 2024. It is now read-only.

Commit

Permalink
More operations taking column_t's base classes where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
trueqbit committed May 21, 2022
1 parent 28bca3e commit 9db2346
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 46 deletions.
8 changes: 4 additions & 4 deletions dev/column.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace sqlite_orm {
struct empty_setter {};

template<class G, class S>
struct field_access_closure {
struct column_field {
using member_pointer_t = G;
using setter_type = S;
using object_type = member_object_type_t<G>;
Expand Down Expand Up @@ -94,11 +94,11 @@ namespace sqlite_orm {
* Op... is a constraints pack, e.g. primary_key_t, autoincrement_t etc
*/
template<class G, class S, class... Op>
struct column_t : basic_column, field_access_closure<G, S>, column_constraints<Op...> {
struct column_t : basic_column, column_field<G, S>, column_constraints<Op...> {
#ifndef SQLITE_ORM_AGGREGATE_BASES_SUPPORTED
column_t(std::string name, G memberPointer, S setter, std::tuple<Op...> op) :
basic_column{move(name)}, field_access_closure<G, S>{memberPointer, setter}, column_constraints<Op...>{
move(op)} {}
basic_column{move(name)}, column_field<G, S>{memberPointer, setter}, column_constraints<Op...>{
move(op)} {}
#endif
// Simplified interface for cast to base class
constexpr const column_constraints<Op...>& as_column_constraints() const {
Expand Down
9 changes: 4 additions & 5 deletions dev/object_from_column_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,10 @@ namespace sqlite_orm {
object_from_column_builder(object_type& object_, sqlite3_stmt* stmt_) :
object_from_column_builder_base{stmt_}, object(object_) {}

template<class C>
void operator()(const C& column) {
using field_type = typename C::field_type;
auto value = row_extractor<field_type>().extract(this->stmt, this->index++);
static_if<std::is_member_object_pointer<typename C::member_pointer_t>::value>(
template<class G, class S>
void operator()(const column_field<G, S>& column) {
auto value = row_extractor<member_field_type_t<G>>().extract(this->stmt, this->index++);
static_if<std::is_member_object_pointer<G>::value>(
[&value, &object = this->object](const auto& column) {
object.*column.member_pointer = std::move(value);
},
Expand Down
7 changes: 6 additions & 1 deletion dev/statement_serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -1120,7 +1120,12 @@ namespace sqlite_orm {
<< streaming_field_values_excluding(
mpl::conjunction<mpl::not_<mpl::always<is_without_rowid>>,
mpl::disjunction_fn<is_primary_key, is_generated_always>>{},
[&table](auto& column) {
[&table]
#ifdef SQLITE_ORM_EXPLICIT_GENERIC_LAMBDA_SUPPORTED
<class G, class S>(const column_field<G, S>& column) {
#else
(auto& column) {
#endif
return table.exists_in_composite_primary_key(column);
},
context,
Expand Down
20 changes: 15 additions & 5 deletions dev/storage.h
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,7 @@ namespace sqlite_orm {
bind_value = field_value_binder{stmt}](auto& object) mutable {
table.template for_each_column_excluding<is_generated_always>(
#ifdef SQLITE_ORM_EXPLICIT_GENERIC_LAMBDA_SUPPORTED
[&bind_value, &object]<class G, class S>(const field_access_closure<G, S>& column)
[&bind_value, &object]<class G, class S>(const column_field<G, S>& column)
#else
[&bind_value, &object](auto& column)
#endif
Expand Down Expand Up @@ -1240,7 +1240,12 @@ namespace sqlite_orm {
table.template for_each_column_excluding<
mpl::conjunction<mpl::not_<mpl::always<is_without_rowid>>,
mpl::disjunction_fn<is_primary_key, is_generated_always>>>(
[&table, &bind_value, &object](auto& column) {
[&table, &bind_value, &object]
#ifdef SQLITE_ORM_EXPLICIT_GENERIC_LAMBDA_SUPPORTED
<class G, class S>(const column_field<G, S>& column) {
#else
(auto& column) {
#endif
if(!table.exists_in_composite_primary_key(column)) {
bind_value(polyfill::invoke(column.member_pointer, object));
}
Expand Down Expand Up @@ -1294,7 +1299,12 @@ namespace sqlite_orm {
field_value_binder bind_value{stmt};
auto& object = get_object(statement.expression);
table.template for_each_column_excluding<mpl::disjunction_fn<is_primary_key, is_generated_always>>(
[&table, &bind_value, &object](auto& column) {
[&table, &bind_value, &object]
#ifdef SQLITE_ORM_EXPLICIT_GENERIC_LAMBDA_SUPPORTED
<class G, class S>(const column_field<G, S>& column) {
#else
(auto& column) {
#endif
if(!table.exists_in_composite_primary_key(column)) {
bind_value(polyfill::invoke(column.member_pointer, object));
}
Expand Down Expand Up @@ -1478,8 +1488,8 @@ namespace sqlite_orm {
std::stringstream ss;
ss << "SELECT COUNT(*)"
<< " FROM " << streaming_identifier(table.name) << " WHERE ";
iterate_tuple(foreignKey.columns, [&ss, &table, first = true](auto& column) mutable {
auto* columnName = table.find_column_name(column);
iterate_tuple(foreignKey.columns, [&ss, &table, first = true](auto& colRef) mutable {
auto* columnName = table.find_column_name(colRef);
if(!columnName) {
throw std::system_error{orm_error_code::column_not_found};
}
Expand Down
22 changes: 14 additions & 8 deletions dev/table.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ namespace sqlite_orm {
const member_field_type_t<M>* object_field_value(const object_type& object, M memberPointer) const {
using F = member_field_type_t<M>;
const F* res = nullptr;
this->for_each_column_with_field_type<F>(
this->for_each_column_with_field_type<F>( ///
[&res, &memberPointer, &object]
#ifdef SQLITE_ORM_EXPLICIT_GENERIC_LAMBDA_SUPPORTED
[&res, &memberPointer, &object]<class G, class S>(const field_access_closure<G, S>& column)
<class G, class S>(const column_field<G, S>& column) {
#else
[&res, &memberPointer, &object](const auto& column)
(const auto& column) {
#endif
{
if(compare_any(column.setter, memberPointer)) {
res = &polyfill::invoke(column.member_pointer, object);
}
Expand All @@ -116,7 +116,7 @@ namespace sqlite_orm {
}

template<class G, class S>
bool exists_in_composite_primary_key(const field_access_closure<G, S>& column) const {
bool exists_in_composite_primary_key(const column_field<G, S>& column) const {
bool res = false;
this->for_each_primary_key([&column, &res](auto& primaryKey) {
using colrefs_tuple = decltype(primaryKey.columns);
Expand Down Expand Up @@ -165,9 +165,15 @@ namespace sqlite_orm {

template<class L>
void for_each_primary_key_column(L&& lambda) const {
this->for_each_column_with<is_primary_key>([&lambda](auto& column) {
lambda(column.member_pointer);
});
this->for_each_column_with<is_primary_key>( ///
[&lambda]
#ifdef SQLITE_ORM_EXPLICIT_GENERIC_LAMBDA_SUPPORTED
<class G, class S>(const column_field<G, S>& column) {
#else
(auto& column) {
#endif
lambda(column.member_pointer);
});
this->for_each_primary_key([this, &lambda](auto& primaryKey) {
this->for_each_column_in_primary_key(primaryKey, lambda);
});
Expand Down
66 changes: 43 additions & 23 deletions include/sqlite_orm/sqlite_orm.h
Original file line number Diff line number Diff line change
Expand Up @@ -2124,7 +2124,7 @@ namespace sqlite_orm {
struct empty_setter {};

template<class G, class S>
struct field_access_closure {
struct column_field {
using member_pointer_t = G;
using setter_type = S;
using object_type = member_object_type_t<G>;
Expand Down Expand Up @@ -2189,11 +2189,11 @@ namespace sqlite_orm {
* Op... is a constraints pack, e.g. primary_key_t, autoincrement_t etc
*/
template<class G, class S, class... Op>
struct column_t : basic_column, field_access_closure<G, S>, column_constraints<Op...> {
struct column_t : basic_column, column_field<G, S>, column_constraints<Op...> {
#ifndef SQLITE_ORM_AGGREGATE_BASES_SUPPORTED
column_t(std::string name, G memberPointer, S setter, std::tuple<Op...> op) :
basic_column{move(name)}, field_access_closure<G, S>{memberPointer, setter}, column_constraints<Op...>{
move(op)} {}
basic_column{move(name)}, column_field<G, S>{memberPointer, setter}, column_constraints<Op...>{
move(op)} {}
#endif
// Simplified interface for cast to base class
constexpr const column_constraints<Op...>& as_column_constraints() const {
Expand Down Expand Up @@ -10337,13 +10337,13 @@ namespace sqlite_orm {
const member_field_type_t<M>* object_field_value(const object_type& object, M memberPointer) const {
using F = member_field_type_t<M>;
const F* res = nullptr;
this->for_each_column_with_field_type<F>(
this->for_each_column_with_field_type<F>( ///
[&res, &memberPointer, &object]
#ifdef SQLITE_ORM_EXPLICIT_GENERIC_LAMBDA_SUPPORTED
[&res, &memberPointer, &object]<class G, class S>(const field_access_closure<G, S>& column)
<class G, class S>(const column_field<G, S>& column) {
#else
[&res, &memberPointer, &object](const auto& column)
(const auto& column) {
#endif
{
if(compare_any(column.setter, memberPointer)) {
res = &polyfill::invoke(column.member_pointer, object);
}
Expand All @@ -10370,7 +10370,7 @@ namespace sqlite_orm {
}

template<class G, class S>
bool exists_in_composite_primary_key(const field_access_closure<G, S>& column) const {
bool exists_in_composite_primary_key(const column_field<G, S>& column) const {
bool res = false;
this->for_each_primary_key([&column, &res](auto& primaryKey) {
using colrefs_tuple = decltype(primaryKey.columns);
Expand Down Expand Up @@ -10419,9 +10419,15 @@ namespace sqlite_orm {

template<class L>
void for_each_primary_key_column(L&& lambda) const {
this->for_each_column_with<is_primary_key>([&lambda](auto& column) {
lambda(column.member_pointer);
});
this->for_each_column_with<is_primary_key>( ///
[&lambda]
#ifdef SQLITE_ORM_EXPLICIT_GENERIC_LAMBDA_SUPPORTED
<class G, class S>(const column_field<G, S>& column) {
#else
(auto& column) {
#endif
lambda(column.member_pointer);
});
this->for_each_primary_key([this, &lambda](auto& primaryKey) {
this->for_each_column_in_primary_key(primaryKey, lambda);
});
Expand Down Expand Up @@ -10824,11 +10830,10 @@ namespace sqlite_orm {
object_from_column_builder(object_type& object_, sqlite3_stmt* stmt_) :
object_from_column_builder_base{stmt_}, object(object_) {}

template<class C>
void operator()(const C& column) {
using field_type = typename C::field_type;
auto value = row_extractor<field_type>().extract(this->stmt, this->index++);
static_if<std::is_member_object_pointer<typename C::member_pointer_t>::value>(
template<class G, class S>
void operator()(const column_field<G, S>& column) {
auto value = row_extractor<member_field_type_t<G>>().extract(this->stmt, this->index++);
static_if<std::is_member_object_pointer<G>::value>(
[&value, &object = this->object](const auto& column) {
object.*column.member_pointer = std::move(value);
},
Expand Down Expand Up @@ -16308,7 +16313,12 @@ namespace sqlite_orm {
<< streaming_field_values_excluding(
mpl::conjunction<mpl::not_<mpl::always<is_without_rowid>>,
mpl::disjunction_fn<is_primary_key, is_generated_always>>{},
[&table](auto& column) {
[&table]
#ifdef SQLITE_ORM_EXPLICIT_GENERIC_LAMBDA_SUPPORTED
<class G, class S>(const column_field<G, S>& column) {
#else
(auto& column) {
#endif
return table.exists_in_composite_primary_key(column);
},
context,
Expand Down Expand Up @@ -18352,7 +18362,7 @@ namespace sqlite_orm {
bind_value = field_value_binder{stmt}](auto& object) mutable {
table.template for_each_column_excluding<is_generated_always>(
#ifdef SQLITE_ORM_EXPLICIT_GENERIC_LAMBDA_SUPPORTED
[&bind_value, &object]<class G, class S>(const field_access_closure<G, S>& column)
[&bind_value, &object]<class G, class S>(const column_field<G, S>& column)
#else
[&bind_value, &object](auto& column)
#endif
Expand Down Expand Up @@ -18402,7 +18412,12 @@ namespace sqlite_orm {
table.template for_each_column_excluding<
mpl::conjunction<mpl::not_<mpl::always<is_without_rowid>>,
mpl::disjunction_fn<is_primary_key, is_generated_always>>>(
[&table, &bind_value, &object](auto& column) {
[&table, &bind_value, &object]
#ifdef SQLITE_ORM_EXPLICIT_GENERIC_LAMBDA_SUPPORTED
<class G, class S>(const column_field<G, S>& column) {
#else
(auto& column) {
#endif
if(!table.exists_in_composite_primary_key(column)) {
bind_value(polyfill::invoke(column.member_pointer, object));
}
Expand Down Expand Up @@ -18456,7 +18471,12 @@ namespace sqlite_orm {
field_value_binder bind_value{stmt};
auto& object = get_object(statement.expression);
table.template for_each_column_excluding<mpl::disjunction_fn<is_primary_key, is_generated_always>>(
[&table, &bind_value, &object](auto& column) {
[&table, &bind_value, &object]
#ifdef SQLITE_ORM_EXPLICIT_GENERIC_LAMBDA_SUPPORTED
<class G, class S>(const column_field<G, S>& column) {
#else
(auto& column) {
#endif
if(!table.exists_in_composite_primary_key(column)) {
bind_value(polyfill::invoke(column.member_pointer, object));
}
Expand Down Expand Up @@ -18640,8 +18660,8 @@ namespace sqlite_orm {
std::stringstream ss;
ss << "SELECT COUNT(*)"
<< " FROM " << streaming_identifier(table.name) << " WHERE ";
iterate_tuple(foreignKey.columns, [&ss, &table, first = true](auto& column) mutable {
auto* columnName = table.find_column_name(column);
iterate_tuple(foreignKey.columns, [&ss, &table, first = true](auto& colRef) mutable {
auto* columnName = table.find_column_name(colRef);
if(!columnName) {
throw std::system_error{orm_error_code::column_not_found};
}
Expand Down

0 comments on commit 9db2346

Please sign in to comment.