Skip to content

Commit f82faa4

Browse files
committed
Renamed public-facing connection control member variable
1 parent ef12ae4 commit f82faa4

File tree

5 files changed

+181
-136
lines changed

5 files changed

+181
-136
lines changed

dev/storage.h

Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -81,54 +81,37 @@ namespace sqlite_orm {
8181
polyfill::void_t<indirectly_test_preparable<decltype(std::declval<S>().prepare(std::declval<E>()))>>> =
8282
true;
8383

84+
template<class Opt, class OptionsTpl>
85+
decltype(auto) storage_opt_or_default(OptionsTpl& options) {
8486
#ifdef SQLITE_ORM_CTAD_SUPPORTED
85-
template<class PropsTpl>
86-
on_open on_open_spec_or_default(PropsTpl& properties) {
87-
if constexpr (tuple_has_type<PropsTpl, on_open>::value) {
88-
return std::move(std::get<on_open>(properties));
87+
if constexpr (tuple_has_type<OptionsTpl, Opt>::value) {
88+
return std::move(std::get<Opt>(options));
8989
} else {
90-
return {};
90+
return Opt{};
9191
}
92-
}
93-
94-
template<class PropsTpl>
95-
connection_control connection_control_or_default(PropsTpl& properties) {
96-
if constexpr (tuple_has_type<PropsTpl, connection_control>::value) {
97-
return std::move(std::get<connection_control>(properties));
98-
} else {
99-
return {};
100-
}
101-
}
10292
#else
103-
template<class PropsTpl>
104-
on_open on_open_spec_or_default(PropsTpl&) {
105-
return {};
106-
}
107-
108-
template<class PropsTpl>
109-
connection_control connection_control_or_default(PropsTpl&) {
110-
return {};
111-
}
93+
return Opt{};
11294
#endif
95+
}
11396

11497
/**
11598
* Storage class itself. Create an instanse to use it as an interfacto to sqlite db by calling `make_storage`
11699
* function.
117100
*/
118101
template<class... DBO>
119102
struct storage_t : storage_base {
120-
using self = storage_t;
103+
using self_type = storage_t;
121104
using db_objects_type = db_objects_tuple<DBO...>;
122105

123106
/**
124107
* @param filename database filename.
125108
* @param dbObjects db_objects_tuple
126109
*/
127-
template<class PropsTpl>
128-
storage_t(std::string filename, db_objects_type dbObjects, PropsTpl properties) :
110+
template<class OptionsTpl>
111+
storage_t(std::string filename, db_objects_type dbObjects, OptionsTpl options) :
129112
storage_base{std::move(filename),
130-
on_open_spec_or_default(properties),
131-
connection_control_or_default(properties),
113+
storage_opt_or_default<connection_control>(options),
114+
storage_opt_or_default<on_open_spec>(options),
132115
foreign_keys_count(dbObjects)},
133116
db_objects{std::move(dbObjects)} {}
134117

@@ -149,7 +132,7 @@ namespace sqlite_orm {
149132
*
150133
* Hence, friend was replaced by `obtain_db_objects()` and `pick_const_impl()`.
151134
*/
152-
friend const db_objects_type& obtain_db_objects(const self& storage) noexcept {
135+
friend const db_objects_type& obtain_db_objects(const self_type& storage) noexcept {
153136
return storage.db_objects;
154137
}
155138

@@ -281,7 +264,7 @@ namespace sqlite_orm {
281264

282265
public:
283266
template<class T, class O = mapped_type_proxy_t<T>, class... Args>
284-
mapped_view<O, self, Args...> iterate(Args&&... args) {
267+
mapped_view<O, self_type, Args...> iterate(Args&&... args) {
285268
this->assert_mapped_type<O>();
286269

287270
auto con = this->get_connection();
@@ -816,7 +799,7 @@ namespace sqlite_orm {
816799
std::enable_if_t<!is_prepared_statement<Ex>::value && !is_mapped<db_objects_type, Ex>::value,
817800
bool> = true>
818801
std::string dump(E&& expression, bool parametrized = false) const {
819-
static_assert(is_preparable_v<self, Ex>, "Expression must be a high-level statement");
802+
static_assert(is_preparable_v<self_type, Ex>, "Expression must be a high-level statement");
820803

821804
decltype(auto) e2 = static_if<is_select<Ex>::value>(
822805
[](auto expression) -> auto {
@@ -1739,18 +1722,15 @@ namespace sqlite_orm {
17391722
}; // struct storage_t
17401723

17411724
#ifdef SQLITE_ORM_CTAD_SUPPORTED
1742-
template<typename T>
1743-
using storage_prop_tag_t = typename T::storage_prop_tag;
1744-
17451725
template<class Elements>
1746-
using prop_index_sequence = filter_tuple_sequence_t<Elements, check_if_names<storage_prop_tag_t>::template fn>;
1726+
using dbo_index_sequence = filter_tuple_sequence_t<Elements, check_if_lacks<storage_opt_tag_t>::template fn>;
17471727

17481728
template<class Elements>
1749-
using dbo_index_sequence = filter_tuple_sequence_t<Elements, check_if_lacks<storage_prop_tag_t>::template fn>;
1729+
using opt_index_sequence = filter_tuple_sequence_t<Elements, check_if_names<storage_opt_tag_t>::template fn>;
17501730

1751-
template<class... DBO, class PropsTpl>
1752-
storage_t<DBO...> make_storage(std::string filename, std::tuple<DBO...> dbObjects, PropsTpl properties) {
1753-
return {std::move(filename), std::move(dbObjects), std::move(properties)};
1731+
template<class... DBO, class OptionsTpl>
1732+
storage_t<DBO...> make_storage(std::string filename, std::tuple<DBO...> dbObjects, OptionsTpl options) {
1733+
return {std::move(filename), std::move(dbObjects), std::move(options)};
17541734
}
17551735
#endif
17561736
}
@@ -1762,13 +1742,14 @@ SQLITE_ORM_EXPORT namespace sqlite_orm {
17621742
* Factory function for a storage, from a database file and a bunch of database object definitions.
17631743
*/
17641744
template<class... Spec>
1765-
auto make_storage(std::string filename, Spec... arguments) {
1745+
auto make_storage(std::string filename, Spec... specifications) {
17661746
using namespace ::sqlite_orm::internal;
17671747

1768-
std::tuple args{std::forward<Spec>(arguments)...};
1769-
return make_storage(std::move(filename),
1770-
create_from_tuple<std::tuple>(std::move(args), dbo_index_sequence<decltype(args)>{}),
1771-
create_from_tuple<std::tuple>(std::move(args), prop_index_sequence<decltype(args)>{}));
1748+
std::tuple specTuple{std::forward<Spec>(specifications)...};
1749+
return internal::make_storage(
1750+
std::move(filename),
1751+
create_from_tuple<std::tuple>(std::move(specTuple), dbo_index_sequence<decltype(specTuple)>{}),
1752+
create_from_tuple<std::tuple>(std::move(specTuple), opt_index_sequence<decltype(specTuple)>{}));
17721753
}
17731754
#else
17741755
template<class... DBO>

dev/storage_base.h

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include <list> // std::list
1515
#include <memory> // std::make_unique, std::unique_ptr
1616
#include <map> // std::map
17-
#include <type_traits> // std::is_same
17+
#include <type_traits> // std::is_same, std::is_aggregate
1818
#include <algorithm> // std::find_if, std::ranges::find
1919
#endif
2020

@@ -34,25 +34,7 @@
3434
#include "udf_proxy.h"
3535
#include "serializing_util.h"
3636
#include "table_info.h"
37-
38-
SQLITE_ORM_EXPORT namespace sqlite_orm {
39-
struct on_open {
40-
using storage_prop_tag = int;
41-
42-
#ifndef SQLITE_ORM_AGGREGATE_PAREN_INIT_SUPPORTED
43-
on_open() = default;
44-
on_open(std::function<void(sqlite3*)> onOpen) : onOpen{std::move(onOpen)} {}
45-
#endif
46-
47-
std::function<void(sqlite3*)> onOpen;
48-
};
49-
50-
struct connection_control {
51-
using storage_prop_tag = int;
52-
53-
bool openForever = false;
54-
};
55-
}
37+
#include "storage_options.h"
5638

5739
namespace sqlite_orm {
5840
namespace internal {
@@ -682,10 +664,10 @@ namespace sqlite_orm {
682664

683665
protected:
684666
storage_base(std::string filename,
685-
sqlite_orm::on_open onOpenSpec,
686667
connection_control connectionCtrl,
668+
on_open_spec onOpenSpec,
687669
int foreignKeysCount) :
688-
on_open{std::move(onOpenSpec.onOpen)}, isOpenedForever{connectionCtrl.openForever},
670+
on_open{std::move(onOpenSpec.onOpen)}, isOpenedForever{connectionCtrl.open_forever},
689671
pragma(std::bind(&storage_base::get_connection, this)),
690672
limit(std::bind(&storage_base::get_connection, this)),
691673
inMemory(filename.empty() || filename == ":memory:"),
@@ -1020,8 +1002,8 @@ namespace sqlite_orm {
10201002
});
10211003
#endif
10221004
if (dbColumnInfoIt != dbTableInfo.end()) {
1023-
auto& dbColumnInfo = *dbColumnInfoIt;
1024-
auto columnsAreEqual =
1005+
table_xinfo& dbColumnInfo = *dbColumnInfoIt;
1006+
bool columnsAreEqual =
10251007
dbColumnInfo.name == storageColumnInfo.name &&
10261008
dbColumnInfo.notnull == storageColumnInfo.notnull &&
10271009
(!dbColumnInfo.dflt_value.empty()) == (!storageColumnInfo.dflt_value.empty()) &&
@@ -1032,8 +1014,7 @@ namespace sqlite_orm {
10321014
break;
10331015
}
10341016
dbTableInfo.erase(dbColumnInfoIt);
1035-
storageTableInfo.erase(storageTableInfo.begin() +
1036-
static_cast<ptrdiff_t>(storageColumnInfoIndex));
1017+
storageTableInfo.erase(storageTableInfo.begin() + storageColumnInfoIndex);
10371018
--storageColumnInfoIndex;
10381019
} else {
10391020
columnsToAdd.push_back(&storageColumnInfo);

dev/storage_options.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#pragma once
2+
3+
#include <sqlite3.h>
4+
#ifdef SQLITE_ORM_IMPORT_STD_MODULE
5+
#include <version>
6+
#else
7+
#include <type_traits> // std::is_aggregate
8+
#include <utility> // std::move
9+
#include <functional> // std::function
10+
#endif
11+
12+
namespace sqlite_orm {
13+
namespace internal {
14+
template<typename T>
15+
using storage_opt_tag_t = typename T::storage_opt_tag;
16+
17+
struct on_open_spec {
18+
using storage_opt_tag = int;
19+
20+
std::function<void(sqlite3*)> onOpen;
21+
};
22+
}
23+
}
24+
25+
SQLITE_ORM_EXPORT namespace sqlite_orm {
26+
/**
27+
* Database connection control options to be passed to `make_storage()`.
28+
*/
29+
struct connection_control {
30+
/// Whether to open the database once and for all.
31+
/// Required if using a 'storage' instance from multiple threads.
32+
bool open_forever = false;
33+
34+
using storage_opt_tag = int;
35+
};
36+
#if __cpp_lib_is_aggregate >= 201703L
37+
// design choice: must be an aggregate that can be constructed using designated initializers
38+
static_assert(std::is_aggregate_v<connection_control>);
39+
#endif
40+
41+
#ifdef SQLITE_ORM_CTAD_SUPPORTED
42+
/**
43+
* Callback function to be passed to `make_storage()`.
44+
* The provided function is called immdediately after the database connection has been established and set up.
45+
*/
46+
inline internal::on_open_spec on_open(std::function<void(sqlite3*)> onOpen) {
47+
return {std::move(onOpen)};
48+
}
49+
#endif
50+
}

0 commit comments

Comments
 (0)