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

Commit

Permalink
Simplified value binding and row extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
trueqbit committed May 1, 2022
1 parent 68cb757 commit 170e9ea
Show file tree
Hide file tree
Showing 28 changed files with 816 additions and 1,110 deletions.
260 changes: 130 additions & 130 deletions dev/ast_iterator.h

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions dev/backup.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <system_error> // std::system_error
#include <string> // std::string
#include <memory>
#include <utility> // std::move, std::exchange

#include "error_code.h"
#include "connection_holder.h"
Expand Down Expand Up @@ -32,14 +33,12 @@ namespace sqlite_orm {
}

backup_t(backup_t&& other) :
handle(other.handle), holder(move(other.holder)), to(other.to), from(other.from) {
other.handle = nullptr;
}
handle(std::exchange(other.handle, nullptr)), holder(move(other.holder)), to(other.to),
from(other.from) {}

~backup_t() {
if(this->handle) {
(void)sqlite3_backup_finish(this->handle);
this->handle = nullptr;
}
}

Expand Down
4 changes: 4 additions & 0 deletions dev/error_code.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,8 @@ namespace sqlite_orm {
[[noreturn]] inline void throw_translated_sqlite_error(sqlite3* db) {
throw sqlite_to_system_error(db);
}

[[noreturn]] inline void throw_translated_sqlite_error(sqlite3_stmt* stmt) {
throw sqlite_to_system_error(sqlite3_db_handle(stmt));
}
}
37 changes: 10 additions & 27 deletions dev/expression_object_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,16 @@ namespace sqlite_orm {
struct expression_object_type;

template<class T>
struct expression_object_type<update_t<T>> {
using type = typename std::decay<T>::type;
};
struct expression_object_type<update_t<T>> : std::decay<T> {};

template<class T>
struct expression_object_type<update_t<std::reference_wrapper<T>>> {
using type = typename std::decay<T>::type;
};
struct expression_object_type<update_t<std::reference_wrapper<T>>> : std::decay<T> {};

template<class T>
struct expression_object_type<replace_t<T>> {
using type = typename std::decay<T>::type;
};
struct expression_object_type<replace_t<T>> : std::decay<T> {};

template<class T>
struct expression_object_type<replace_t<std::reference_wrapper<T>>> {
using type = typename std::decay<T>::type;
};
struct expression_object_type<replace_t<std::reference_wrapper<T>>> : std::decay<T> {};

template<class It, class L, class O>
struct expression_object_type<replace_range_t<It, L, O>> {
Expand All @@ -53,18 +45,13 @@ namespace sqlite_orm {
};

template<class T>
struct expression_object_type<insert_t<T>> {
using type = typename std::decay<T>::type;
};
struct expression_object_type<insert_t<T>> : std::decay<T> {};

template<class T>
struct expression_object_type<insert_t<std::reference_wrapper<T>>> {
using type = typename std::decay<T>::type;
};
struct expression_object_type<insert_t<std::reference_wrapper<T>>> : std::decay<T> {};

template<class It, class L, class O>
struct expression_object_type<insert_range_t<It, L, O>> {
using transformer_type = L;
using type = typename insert_range_t<It, L, O>::object_type;
};

Expand All @@ -74,14 +61,10 @@ namespace sqlite_orm {
};

template<class T, class... Cols>
struct expression_object_type<insert_explicit<T, Cols...>> {
using type = typename std::decay<T>::type;
};
struct expression_object_type<insert_explicit<T, Cols...>> : std::decay<T> {};

template<class T, class... Cols>
struct expression_object_type<insert_explicit<std::reference_wrapper<T>, Cols...>> {
using type = typename std::decay<T>::type;
};
struct expression_object_type<insert_explicit<std::reference_wrapper<T>, Cols...>> : std::decay<T> {};

template<class T>
struct get_ref_t {
Expand All @@ -103,7 +86,7 @@ namespace sqlite_orm {

template<class T>
auto& get_ref(T& t) {
using arg_type = typename std::decay<T>::type;
using arg_type = std::decay_t<T>;
get_ref_t<arg_type> g;
return g(t);
}
Expand All @@ -116,7 +99,7 @@ namespace sqlite_orm {

template<class T>
auto& get_object(T& t) {
using expression_type = typename std::decay<T>::type;
using expression_type = std::decay_t<T>;
get_object_t<expression_type> obj;
return obj(t);
}
Expand Down
12 changes: 6 additions & 6 deletions dev/get_prepared_statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,21 +121,21 @@ namespace sqlite_orm {

template<int N, class T>
const auto& get(const internal::prepared_statement_t<T>& statement) {
using statement_type = typename std::decay<decltype(statement)>::type;
using statement_type = std::decay_t<decltype(statement)>;
using expression_type = typename statement_type::expression_type;
using node_tuple = internal::node_tuple_t<expression_type>;
using bind_tuple = typename internal::bindable_filter<node_tuple>::type;
using result_tupe = std::tuple_element_t<static_cast<size_t>(N), bind_tuple>;
const result_tupe* result = nullptr;
auto index = -1;
internal::iterate_ast(statement.expression, [&result, &index](auto& node) {
using node_type = typename std::decay<decltype(node)>::type;
using node_type = std::decay_t<decltype(node)>;
if(internal::is_bindable_v<node_type>) {
++index;
}
if(index == N) {
internal::static_if<std::is_same<result_tupe, node_type>::value>([](auto& r, auto& n) {
r = const_cast<typename std::remove_reference<decltype(r)>::type>(&n);
r = const_cast<std::remove_reference_t<decltype(r)>>(&n);
})(result, node);
}
});
Expand All @@ -144,21 +144,21 @@ namespace sqlite_orm {

template<int N, class T>
auto& get(internal::prepared_statement_t<T>& statement) {
using statement_type = typename std::decay<decltype(statement)>::type;
using statement_type = std::decay_t<decltype(statement)>;
using expression_type = typename statement_type::expression_type;
using node_tuple = internal::node_tuple_t<expression_type>;
using bind_tuple = typename internal::bindable_filter<node_tuple>::type;
using result_tupe = std::tuple_element_t<static_cast<size_t>(N), bind_tuple>;
result_tupe* result = nullptr;
auto index = -1;
internal::iterate_ast(statement.expression, [&result, &index](auto& node) {
using node_type = typename std::decay<decltype(node)>::type;
using node_type = std::decay_t<decltype(node)>;
if(internal::is_bindable_v<node_type>) {
++index;
}
if(index == N) {
internal::static_if<std::is_same<result_tupe, node_type>::value>([](auto& r, auto& n) {
r = const_cast<typename std::remove_reference<decltype(r)>::type>(&n);
r = const_cast<std::remove_reference_t<decltype(r)>>(&n);
})(result, node);
}
});
Expand Down
10 changes: 5 additions & 5 deletions dev/implementations/storage_definitions.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace sqlite_orm {
// this vector will contain pointers to columns that gotta be added..
std::vector<const table_xinfo*> columnsToAdd;

calculate_remove_add_columns(columnsToAdd, storageTableInfo, dbTableInfo);
this->calculate_remove_add_columns(columnsToAdd, storageTableInfo, dbTableInfo);

if(schema_stat == sync_schema_result::old_columns_removed) {
#if SQLITE_VERSION_NUMBER >= 3035000 // DROP COLUMN feature exists (v3.35.0)
Expand Down Expand Up @@ -79,7 +79,7 @@ namespace sqlite_orm {
if(schema_stat == sync_schema_result::new_columns_added_and_old_columns_removed) {

auto storageTableInfo = tImpl.table.get_table_info();
add_generated_cols(columnsToAdd, storageTableInfo);
this->add_generated_cols(columnsToAdd, storageTableInfo);

// remove extra columns and generated columns
this->backup_table(db, tImpl, columnsToAdd);
Expand All @@ -94,14 +94,14 @@ namespace sqlite_orm {
// this vector will contain pointers to columns that gotta be added..
std::vector<const table_xinfo*> columnsToAdd;

calculate_remove_add_columns(columnsToAdd, storageTableInfo, dbTableInfo);
this->calculate_remove_add_columns(columnsToAdd, storageTableInfo, dbTableInfo);

add_generated_cols(columnsToAdd, storageTableInfo);
this->add_generated_cols(columnsToAdd, storageTableInfo);

if(preserve && attempt_to_preserve) {
this->backup_table(db, tImpl, columnsToAdd);
} else {
this->drop_create_with_loss(tImpl, db);
this->drop_create_with_loss(db, tImpl);
}
res = schema_stat;
}
Expand Down
2 changes: 2 additions & 0 deletions dev/index.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ namespace sqlite_orm {
using elements_type = std::tuple<Els...>;
using object_type = void;

#ifndef SQLITE_ORM_AGGREGATE_BASES_SUPPORTED
index_t(std::string name_, bool unique_, elements_type elements_) :
index_base{move(name_), unique_}, elements(move(elements_)) {}
#endif

elements_type elements;
};
Expand Down
5 changes: 5 additions & 0 deletions dev/indexed_column.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

#include <string> // std::string

#include "start_macros.h"
#include "ast/where.h"

namespace sqlite_orm {

namespace internal {
Expand All @@ -10,8 +13,10 @@ namespace sqlite_orm {
struct indexed_column_t {
using column_type = C;

#ifndef SQLITE_ORM_AGGREGATE_NSDMI_SUPPORTED
indexed_column_t(column_type _column_or_expression) :
column_or_expression(std::move(_column_or_expression)) {}
#endif

column_type column_or_expression;
std::string _collation_name;
Expand Down
15 changes: 6 additions & 9 deletions dev/is_base_of_template.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,28 +11,25 @@ namespace sqlite_orm {
* https://stackoverflow.com/questions/34672441/stdis-base-of-for-template-classes/34672753#34672753
*/
#ifdef SQLITE_ORM_BROKEN_VARIADIC_PACK_EXPANSION
template<template<typename...> class Base, typename Derived>
template<template<typename...> class Base>
struct is_base_of_template_impl {
template<typename... Ts>
static constexpr std::true_type test(const Base<Ts...>*);
static constexpr std::true_type test(const Base<Ts...>&);

static constexpr std::false_type test(...);

using type = decltype(test(std::declval<Derived*>()));
};

template<typename Derived, template<typename...> class Base>
using is_base_of_template = typename is_base_of_template_impl<Base, Derived>::type;

template<typename T, template<typename...> class C>
using is_base_of_template = decltype(is_base_of_template_impl<C>::test(std::declval<T>()));
#else
template<template<typename...> class C, typename... Ts>
std::true_type is_base_of_template_impl(const C<Ts...>*);
std::true_type is_base_of_template_impl(const C<Ts...>&);

template<template<typename...> class C>
std::false_type is_base_of_template_impl(...);

template<typename T, template<typename...> class C>
using is_base_of_template = decltype(is_base_of_template_impl<C>(std::declval<T*>()));
using is_base_of_template = decltype(is_base_of_template_impl<C>(std::declval<T>()));
#endif

template<typename T, template<typename...> class C>
Expand Down
6 changes: 2 additions & 4 deletions dev/mapped_row_extractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace sqlite_orm {
/**
* This is a private row extractor class. It is used for extracting rows as objects instead of tuple.
* Main difference from regular `row_extractor` is that this class takes table info which is required
* for constructing objects by member pointers. To construct please use `row_extractor_builder` class
* for constructing objects by member pointers. To construct please use `make_row_extractor()`.
* Type arguments:
* V is value type just like regular `row_extractor` has
* T is table info class `table_t`
Expand All @@ -20,9 +20,7 @@ namespace sqlite_orm {
struct mapped_row_extractor {
using table_info_t = T;

mapped_row_extractor(const table_info_t& tableInfo_) : tableInfo(tableInfo_) {}

V extract(sqlite3_stmt* stmt, int /*columnIndex*/) {
V extract(sqlite3_stmt* stmt, int /*columnIndex*/) const {
V res;
object_from_column_builder<V> builder{res, stmt};
this->tableInfo.for_each_column(builder);
Expand Down
2 changes: 1 addition & 1 deletion dev/mapped_type_proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace sqlite_orm {

/**
* If T is alias than mapped_type_proxy<T>::type is alias::type
* otherwise T is T.
* otherwise T is unqualified T.
*/
template<class T, class SFINAE = void>
struct mapped_type_proxy {
Expand Down
2 changes: 1 addition & 1 deletion dev/member_traits/member_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

#include <type_traits> // std::enable_if, std::is_member_object_pointer

#include "is_getter.h"
#include "field_member_traits.h"
#include "is_getter.h"
#include "is_setter.h"
#include "getter_traits.h"
#include "setter_traits.h"
Expand Down
6 changes: 3 additions & 3 deletions dev/object_from_column_builder.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include <sqlite3.h>

#include <type_traits> // std::is_member_object_pointer

#include "static_magic.h"
#include "row_extractor.h"

Expand All @@ -12,7 +12,7 @@ namespace sqlite_orm {

struct object_from_column_builder_base {
sqlite3_stmt* stmt = nullptr;
mutable int index = 0;
int index = 0;

#ifndef SQLITE_ORM_AGGREGATE_NSDMI_SUPPORTED
object_from_column_builder_base(sqlite3_stmt* stmt) : stmt{stmt} {}
Expand All @@ -32,7 +32,7 @@ namespace sqlite_orm {
object_from_column_builder_base{stmt_}, object(object_) {}

template<class C>
void operator()(const C& c) const {
void operator()(const C& c) {
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>(
Expand Down
4 changes: 2 additions & 2 deletions dev/row_extractor.h
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ namespace sqlite_orm {
protected:
template<size_t I, std::enable_if_t<I != 0, bool> = true>
void extract(std::tuple<Args...>& t, sqlite3_stmt* stmt) const {
using tuple_type = typename std::tuple_element<I - 1, typename std::tuple<Args...>>::type;
using tuple_type = std::tuple_element_t<I - 1, std::tuple<Args...>>;
std::get<I - 1>(t) = row_extractor<tuple_type>().extract(stmt, I - 1);
this->extract<I - 1>(t, stmt);
}
Expand All @@ -329,7 +329,7 @@ namespace sqlite_orm {

template<size_t I, std::enable_if_t<I != 0, bool> = true>
void extract(std::tuple<Args...>& t, char** argv) const {
using tuple_type = typename std::tuple_element<I - 1, typename std::tuple<Args...>>::type;
using tuple_type = std::tuple_element_t<I - 1, std::tuple<Args...>>;
std::get<I - 1>(t) = row_extractor<tuple_type>().extract(argv[I - 1]);
this->extract<I - 1>(t, argv);
}
Expand Down
Loading

0 comments on commit 170e9ea

Please sign in to comment.