Lightweight 0.20260617.0
Loading...
Searching...
No Matches
DataMapperAsync.hpp
1// SPDX-License-Identifier: Apache-2.0
2//
3// Out-of-line definitions for DataMapper's asynchronous (coroutine) methods.
4// This file is included unconditionally at the end of DataMapper/DataMapper.hpp.
5//
6// Each method offloads the existing synchronous DataMapper method to the connection's async backend
7// (a worker thread, serialized per connection via a strand) and resumes the awaiting coroutine on the
8// app's resume scheduler. The whole synchronous operation runs as one closure on the strand, so the
9// ODBC connection is only ever touched by one thread at a time.
10
11#pragma once
12
13#include "Backend.hpp"
14
15#include <optional>
16#include <string>
17#include <utility>
18#include <vector>
19
20namespace Lightweight
21{
22
23template <DataMapperOptions QueryOptions, typename Record>
25{
26 return Async::RunAsync(_connection.AsyncBackend(),
27 [this, &record]() -> RecordPrimaryKeyType<Record> { return Create<QueryOptions>(record); });
28}
29
30template <typename Record, DataMapperOptions QueryOptions, typename... PrimaryKeyTypes>
32{
33 return Async::RunAsync(_connection.AsyncBackend(),
34 [this, ... primaryKeys = std::move(primaryKeys)]() mutable -> std::optional<Record> {
35 return QuerySingle<Record, QueryOptions>(std::move(primaryKeys)...);
36 });
37}
38
39template <typename Record>
41{
42 return Async::RunAsync(_connection.AsyncBackend(), [this, &record] { Update(record); });
43}
44
45template <typename Record>
47{
48 return Async::RunAsync(_connection.AsyncBackend(), [this, &record]() -> std::size_t { return Delete(record); });
49}
50
51template <typename Record>
53{
54 return Async::RunAsync(_connection.AsyncBackend(), [this, &record] { LoadRelations(record); });
55}
56
57} // namespace Lightweight
Async::Task< void > LoadRelationsAsync(Record &record)
Asynchronously loads record's relations.
Async::Task< std::optional< Record > > QuerySingleAsync(PrimaryKeyTypes... primaryKeys)
Async::Task< void > UpdateAsync(Record &record)
Asynchronously updates record's modified fields.
Async::Task< RecordPrimaryKeyType< Record > > CreateAsync(Record &record)
Asynchronously inserts record, updating its primary key in place.
Async::Task< std::size_t > DeleteAsync(Record const &record)
Asynchronously deletes record.
LIGHTWEIGHT_API Async::IAsyncBackend & AsyncBackend()