Line data Source code
1 : /**
2 : Copyright (c) 2016-2022 Roman Katuntsev <sbkarr@stappler.org>
3 : Copyright (c) 2023-2024 Stappler LLC <admin@stappler.dev>
4 :
5 : Permission is hereby granted, free of charge, to any person obtaining a copy
6 : of this software and associated documentation files (the "Software"), to deal
7 : in the Software without restriction, including without limitation the rights
8 : to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 : copies of the Software, and to permit persons to whom the Software is
10 : furnished to do so, subject to the following conditions:
11 :
12 : The above copyright notice and this permission notice shall be included in
13 : all copies or substantial portions of the Software.
14 :
15 : THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 : IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 : FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 : AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 : LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 : OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 : THE SOFTWARE.
22 : **/
23 :
24 : #ifndef STAPPLER_DB_SPDBADAPTER_H_
25 : #define STAPPLER_DB_SPDBADAPTER_H_
26 :
27 : #include "SPDbBackendInterface.h"
28 :
29 : namespace STAPPLER_VERSIONIZED stappler::db {
30 :
31 : class ApplicationInterface {
32 : public:
33 : virtual ~ApplicationInterface();
34 :
35 : template <typename ... Args>
36 0 : void error(Args && ...args) const {
37 0 : _pushError(std::forward<Args>(args)...);
38 0 : }
39 :
40 : template <typename ... Args>
41 0 : void debug(Args && ...args) const {
42 0 : _pushDebug(std::forward<Args>(args)...);
43 0 : }
44 :
45 : template <typename Source, typename Text>
46 0 : void _pushError(Source &&source, Text &&text) const {
47 0 : pushErrorMessage(Value{
48 0 : std::make_pair("source", Value(std::forward<Source>(source))),
49 0 : std::make_pair("text", Value(std::forward<Text>(text)))
50 : });
51 0 : }
52 :
53 : template <typename Source, typename Text>
54 0 : void _pushError(Source &&source, Text &&text, Value &&d) const {
55 0 : pushErrorMessage(Value{
56 0 : std::make_pair("source", Value(std::forward<Source>(source))),
57 0 : std::make_pair("text", Value(std::forward<Text>(text))),
58 0 : std::make_pair("data", std::move(d))
59 : });
60 0 : }
61 :
62 : template <typename Source, typename Text>
63 : void _pushDebug(Source &&source, Text &&text) const {
64 : pushDebugMessage(Value{
65 : std::make_pair("source", Value(std::forward<Source>(source))),
66 : std::make_pair("text", Value(std::forward<Text>(text)))
67 : });
68 : }
69 :
70 : template <typename Source, typename Text>
71 0 : void _pushDebug(Source &&source, Text &&text, Value &&d) const {
72 0 : pushDebugMessage(Value{
73 0 : std::make_pair("source", Value(std::forward<Source>(source))),
74 0 : std::make_pair("text", Value(std::forward<Text>(text))),
75 0 : std::make_pair("data", std::move(d))
76 : });
77 0 : }
78 :
79 : virtual db::Adapter getAdapterFromContext() const;
80 : virtual void scheduleAyncDbTask(const Callback<Function<void(const Transaction &)>(pool_t *)> &setupCb) const;
81 :
82 : virtual StringView getDocumentRoot() const;
83 :
84 0 : virtual const Scheme *getFileScheme() const { return nullptr; }
85 0 : virtual const Scheme *getUserScheme() const { return nullptr; }
86 :
87 : virtual void pushErrorMessage(Value &&) const;
88 : virtual void pushDebugMessage(Value &&) const;
89 :
90 0 : virtual db::InputFile *getFileFromContext(int64_t id) const { return nullptr; }
91 3075 : virtual int64_t getUserIdFromContext() const { return 0; }
92 :
93 0 : virtual RequestData getRequestData() const { return RequestData(); }
94 :
95 25 : virtual void initTransaction(db::Transaction &) const { }
96 :
97 : virtual void reportDbUpdate(StringView, bool successful);
98 : };
99 :
100 : class Adapter final : public AllocBase {
101 : public:
102 : static Adapter FromContext(const ApplicationInterface *);
103 :
104 : Adapter(BackendInterface *, const ApplicationInterface *);
105 :
106 : Adapter(const Adapter &);
107 : Adapter& operator=(const Adapter &);
108 :
109 21200 : explicit operator bool () const { return _interface != nullptr && _application != nullptr; }
110 :
111 0 : bool operator==(const Adapter &other) const { return _interface == other._interface && _application == other._application; }
112 :
113 1292 : const ApplicationInterface *getApplicationInterface() const { return _application; }
114 500 : BackendInterface *getBackendInterface() const { return _interface; }
115 :
116 : String getTransactionKey() const;
117 :
118 : bool set(const CoderSource &, const Value &, TimeInterval = config::STORAGE_DEFAULT_KEY_VALUE_INTERVAL) const;
119 : Value get(const CoderSource &) const;
120 : bool clear(const CoderSource &) const;
121 :
122 : bool init(const BackendInterface::Config &cfg, const Map<StringView, const Scheme *> &) const;
123 :
124 : void makeSessionsCleanup() const;
125 :
126 : User * authorizeUser(const Auth &, const StringView &name, const StringView &password) const;
127 :
128 : void broadcast(const Bytes &) const;
129 : void broadcast(const Value &val) const;
130 : void broadcast(StringView url, Value &&val, bool exclusive) const;
131 :
132 : bool performWithTransaction(const Callback<bool(const db::Transaction &)> &cb) const;
133 :
134 : Vector<int64_t> getReferenceParents(const Scheme &, uint64_t oid, const Scheme *, const Field *) const;
135 :
136 74 : StringView getDatabaseName() const { return _interface->getDatabaseName(); }
137 :
138 : protected:
139 : friend class Transaction;
140 :
141 : int64_t getDeltaValue(const Scheme &); // scheme-based delta
142 : int64_t getDeltaValue(const Scheme &, const FieldView &, uint64_t); // view-based delta
143 :
144 : Vector<int64_t> performQueryListForIds(const QueryList &, size_t count = stappler::maxOf<size_t>()) const;
145 : Value performQueryList(const QueryList &, size_t count = stappler::maxOf<size_t>(), bool forUpdate = false) const;
146 :
147 : bool foreach(Worker &, const Query &, const Callback<bool(Value &)> &) const;
148 : Value select(Worker &, const Query &) const;
149 :
150 : Value create(Worker &, Value &) const;
151 : Value save(Worker &, uint64_t oid, Value &obj, Value &patch, const Set<const Field *> &fields) const;
152 :
153 : bool remove(Worker &, uint64_t oid) const;
154 :
155 : size_t count(Worker &, const Query &) const;
156 :
157 : void scheduleAutoField(const Scheme &, const Field &, uint64_t id);
158 :
159 : Value field(Action, Worker &, uint64_t oid, const Field &, Value && = Value()) const;
160 : Value field(Action, Worker &, const Value &, const Field &, Value && = Value()) const;
161 :
162 : bool addToView(const FieldView &, const Scheme *, uint64_t oid, const Value &) const;
163 : bool removeFromView(const FieldView &, const Scheme *, uint64_t oid) const;
164 :
165 : bool beginTransaction() const;
166 : bool endTransaction() const;
167 :
168 : void cancelTransaction() const;
169 : bool isInTransaction() const;
170 : TransactionStatus getTransactionStatus() const;
171 :
172 : void runAutoFields(const Transaction &t, const Vector<uint64_t> &vec, const Scheme &, const Field &);
173 : void processFullTextFields(const Scheme &, Value &patch, Vector<InputField> &ifields, Vector<InputRow> &ivalues) const;
174 :
175 : const ApplicationInterface *_application = nullptr;
176 : BackendInterface *_interface = nullptr;
177 : };
178 :
179 : }
180 :
181 : #endif /* STAPPLER_DB_SPDBADAPTER_H_ */
|