7namespace Stroika::Foundation::Database::SQL::ORM {
14 template <
typename T,
typename TRAITS>
15 TableConnection<T, TRAITS>::TableConnection (
const Connection::Ptr& conn,
const Schema::Table& tableSchema,
16 const ObjectVariantMapper& objectVariantMapper,
const OpertionCallbackPtr& operationCallback)
17 : connection{[qStroika_Foundation_Common_Property_ExtraCaptureStuff] ([[maybe_unused]] const auto* property) {
18 const TableConnection* thisObj = qStroika_Foundation_Common_Property_OuterObjPtr (property, &TableConnection::connection);
19 return thisObj->fConnection_;
21 , tableSchema{[qStroika_Foundation_Common_Property_ExtraCaptureStuff] ([[maybe_unused]]
const auto* property) {
22 const TableConnection* thisObj = qStroika_Foundation_Common_Property_OuterObjPtr (property, &TableConnection::tableSchema);
23 return thisObj->fTableSchema_;
25 , objectVariantMapper{[qStroika_Foundation_Common_Property_ExtraCaptureStuff] ([[maybe_unused]]
const auto* property) {
26 const TableConnection* thisObj = qStroika_Foundation_Common_Property_OuterObjPtr (property, &TableConnection::objectVariantMapper);
27 return thisObj->fObjectVariantMapper_;
29 , operationCallback{[qStroika_Foundation_Common_Property_ExtraCaptureStuff] ([[maybe_unused]]
const auto* property) {
30 const TableConnection* thisObj = qStroika_Foundation_Common_Property_OuterObjPtr (property, &TableConnection::operationCallback);
31 return thisObj->fTableOpertionCallback_;
34 , fEngineProperties_{conn->GetEngineProperties ()}
35 , fTableSchema_{tableSchema}
36 , fObjectVariantMapper_{objectVariantMapper}
37 , fTableOpertionCallback_{operationCallback}
38 , fGetByID_Statement_{conn->mkStatement (Schema::StandardSQLStatements{tableSchema}.GetByID ())}
39 , fGetAll_Statement_{conn->mkStatement (Schema::StandardSQLStatements{tableSchema}.GetAllElements ())}
40 , fAddNew_Statement_{conn->mkStatement (Schema::StandardSQLStatements{tableSchema}.Insert ())}
41 , fUpdate_Statement_{conn->mkStatement (Schema::StandardSQLStatements{tableSchema}.UpdateByID ())}
42 , fDeleteByID_Statement_{conn->mkStatement (Schema::StandardSQLStatements{tableSchema}.DeleteByID ())}
44 Require (conn !=
nullptr);
46 template <
typename T,
typename TRAITS>
47 inline TableConnection<T, TRAITS>::TableConnection (
const TableConnection& src)
48 : TableConnection{src.fConnection_, src.fTableSchema_, src.fObjectVariantMapper_, src.fTableOpertionCallback_}
51 template <
typename T,
typename TRAITS>
57 fGetByID_Statement_.Reset ();
59 optional<Statement::Row> row;
61 [[maybe_unused]]
auto&& cleanup = Execution::Finally ([&] ()
noexcept {
62 if (fEngineProperties_->RequireStatementResetAfterModifyingStatmentToCompleteTransaction ()) {
64 fGetByID_Statement_.Reset ();
68 return fObjectVariantMapper_.ToObject<T> (
VariantValue{fTableSchema_.MapFromDB (*row)});
72 template <
typename T,
typename TRAITS>
75 return Get (TRAITS::ID2VariantValue (
id));
77 template <
typename T,
typename TRAITS>
85 [[maybe_unused]]
auto&& cleanup = Execution::Finally ([&] ()
noexcept {
86 if (fEngineProperties_->RequireStatementResetAfterModifyingStatmentToCompleteTransaction ()) {
88 fGetAll_Statement_.Reset ();
91 return rows.template Map<Sequence<T>> (
94 template <
typename T,
typename TRAITS>
102 [[maybe_unused]]
auto&& cleanup = Execution::Finally ([&] ()
noexcept {
103 if (fEngineProperties_->RequireStatementResetAfterModifyingStatmentToCompleteTransaction ()) {
105 fGetAll_Statement_.Reset ();
110 return rows.template Map<Sequence<T>> ([
this, &onItemException] (
const Statement::Row& r) -> optional<T> {
112 return fObjectVariantMapper_.ToObject<T> (VariantValue{fTableSchema_.MapFromDB (r)});
115 return onItemException (r, current_exception ());
119 template <
typename T,
typename TRAITS>
124 fAddNew_Statement_.Reset ();
126 DoExecute_ (fAddNew_Statement_,
true);
127 [[maybe_unused]]
auto&& cleanup = Execution::Finally ([&] ()
noexcept {
128 if (fEngineProperties_->RequireStatementResetAfterModifyingStatmentToCompleteTransaction ()) {
130 fAddNew_Statement_.Reset ();
134 template <
typename T,
typename TRAITS>
140 if (Get (dbV[fTableSchema_.GetIDField ()->fName])) {
147 template <
typename T,
typename TRAITS>
153 fUpdate_Statement_.Reset ();
155 DoExecute_ (fUpdate_Statement_,
true);
156 [[maybe_unused]]
auto&& cleanup = Execution::Finally ([&] ()
noexcept {
157 if (fEngineProperties_->RequireStatementResetAfterModifyingStatmentToCompleteTransaction ()) {
159 fUpdate_Statement_.Reset ();
163 template <
typename T,
typename TRAITS>
169 fDeleteByID_Statement_.Reset ();
170 fDeleteByID_Statement_.Bind (initializer_list<KeyValuePair<String, VariantValue>>{{fTableSchema_.GetIDField ()->fName,
id}});
171 DoExecute_ (fDeleteByID_Statement_,
true);
172 [[maybe_unused]]
auto&& cleanup = Execution::Finally ([&] ()
noexcept {
173 if (fEngineProperties_->RequireStatementResetAfterModifyingStatmentToCompleteTransaction ()) {
175 fDeleteByID_Statement_.Reset ();
179 template <
typename T,
typename TRAITS>
180 inline void TableConnection<T, TRAITS>::Delete (
const typename TRAITS::IDType&
id)
182 Delete (TRAITS::ID2VariantValue (
id));
184 template <
typename T,
typename TRAITS>
185 inline void TableConnection<T, TRAITS>::Delete (
const T& v)
187 Debug::AssertExternallySynchronizedMutex::WriteContext declareContext{fThisAssertExternallySynchronized_};
188 using DataExchange::VariantValue;
189 Mapping<String, VariantValue> objFields = fObjectVariantMapper_.FromObject (v).template As<Mapping<String, VariantValue>> ();
190 VariantValue idField = *objFields.Lookup (Memory::ValueOf (fTableSchema_.GetIDField ()).GetVariantValueFieldName ());
193 template <
typename T,
typename TRAITS>
194 template <
typename FUN>
195 void TableConnection<T, TRAITS>::DoExecute_ (FUN&& f, Statement& s,
bool write)
197 if (fTableOpertionCallback_ ==
nullptr) {
201 fTableOpertionCallback_ (write ? Operation::eStartingWrite : Operation::eStartingRead, this, &s, nullptr);
204 fTableOpertionCallback_ (write ? Operation::eCompletedWrite : Operation::eCompletedRead, this, &s, nullptr);
207 fTableOpertionCallback_ (Operation::eNotifyError,
this, &s, current_exception ());
208 fTableOpertionCallback_ (write ? Operation::eCompletedWrite : Operation::eCompletedRead, this, &fUpdate_Statement_, nullptr);
209 Execution::ReThrow ();
213 template <
typename T,
typename TRAITS>
214 inline void TableConnection<T, TRAITS>::DoExecute_ (Statement& s,
bool write)
216 DoExecute_ ([] (Statement& s) { s.Execute (); }, s, write);
218 template <
typename T,
typename TRAITS>
219 const typename TableConnection<T, TRAITS>::OpertionCallbackPtr TableConnection<T, TRAITS>::kDefaultTracingOpertionCallback =
220 [] (Operation op,
const TableConnection* tableConn,
const Statement* s) {
221 if (op == Operation::eStartingRead or op == Operation::eStartingWrite) {
223 using namespace Characters::Literals;
224 DbgTrace (
"SQL: {}"_f, s->GetSQL (Statement::WhichSQLFlag::eExpanded));
A generalization of a vector: a container whose elements are keyed by the natural numbers.
Simple variant-value (case variant union) object, with (variant) basic types analogous to a value in ...
TableConnection<T> wraps a database Connection::Ptr with information to map c++ objects to/from SQL d...
nonvirtual optional< Row > GetNextRow()
nonvirtual Sequence< Row > GetAllRows()
call Reset (), and then GetAllRemainingRows () - which always starts current statement with current b...
unique_lock< AssertExternallySynchronizedMutex > WriteContext
Instantiate AssertExternallySynchronizedMutex::WriteContext to designate an area of code where protec...