Line data Source code
1 : /**
2 : Copyright (c) 2018-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_SQL_SPSQLHANDLE_H_
25 : #define STAPPLER_DB_SQL_SPSQLHANDLE_H_
26 :
27 : #include "SPSqlQuery.h"
28 :
29 : namespace STAPPLER_VERSIONIZED stappler::db::sql {
30 :
31 : class Driver;
32 : struct QueryStorageHandle;
33 :
34 : using Result = db::Result;
35 :
36 : class SqlHandle : public db::BackendInterface {
37 : public:
38 : using Scheme = db::Scheme;
39 : using Worker = db::Worker;
40 : using Field = db::Field;
41 : using Operator = stappler::sql::Operator;
42 : using Comparation = stappler::sql::Comparation;
43 : using QueryList = db::QueryList;
44 :
45 : static StringView getKeyValueSchemeName();
46 : static String getNameForDelta(const Scheme &scheme);
47 :
48 : SqlHandle(const Driver *);
49 :
50 50 : const Driver *getDriver() const { return _driver; }
51 :
52 : virtual bool set(const CoderSource &, const Value &, stappler::TimeInterval) override;
53 : virtual Value get(const CoderSource &) override;
54 : virtual bool clear(const CoderSource &) override;
55 :
56 : virtual db::User * authorizeUser(const db::Auth &auth, const StringView &iname, const StringView &password) override;
57 :
58 50 : virtual bool isNotificationsSupported() const { return false; }
59 :
60 : virtual void makeSessionsCleanup() override;
61 : void finalizeBroadcast();
62 : virtual int64_t processBroadcasts(const Callback<void(stappler::BytesView)> &, int64_t value) override;
63 : virtual void broadcast(const Bytes &) override;
64 :
65 : virtual int64_t getDeltaValue(const Scheme &scheme) override;
66 : virtual int64_t getDeltaValue(const Scheme &scheme, const db::FieldView &view, uint64_t tag) override;
67 :
68 : // get change history for scheme that supports delta ops
69 : Value getHistory(const Scheme &, const stappler::Time &, bool resolveUsers = false);
70 :
71 : // get change history for view field in object
72 : Value getHistory(const db::FieldView &, const Scheme *, uint64_t tag, const stappler::Time &, bool resolveUsers = false);
73 :
74 : // get changed objects in scheme from timestamp
75 : Value getDeltaData(const Scheme &, const stappler::Time &);
76 :
77 : // get changed objects from view field in object from timestamp
78 : Value getDeltaData(const Scheme &, const db::FieldView &, const stappler::Time &, uint64_t);
79 :
80 : virtual void makeQuery(const stappler::Callback<void(SqlQuery &)> &cb, const QueryStorageHandle *) = 0;
81 :
82 : virtual bool selectQuery(const SqlQuery &, const Callback<bool(Result &)> &cb,
83 : const Callback<void(const Value &)> &err = nullptr) = 0;
84 : virtual bool performSimpleQuery(const StringView &,
85 : const Callback<void(const Value &)> &err = nullptr) = 0;
86 : virtual bool performSimpleSelect(const StringView &, const Callback<void(Result &)> &cb,
87 : const Callback<void(const Value &)> &err = nullptr) = 0;
88 :
89 : virtual bool isSuccess() const = 0;
90 :
91 : virtual bool foreach(Worker &, const Query &, const Callback<bool(Value &)> &) override;
92 :
93 : virtual Value select(Worker &, const db::Query &) override;
94 :
95 : virtual Value create(Worker &, const Vector<InputField> &, Vector<InputRow> &, bool multiCreate) override;
96 : virtual Value save(Worker &, uint64_t oid, const Value &obj, const Vector<InputField> &, InputRow &) override;
97 :
98 : virtual bool remove(Worker &, uint64_t oid) override;
99 :
100 : virtual size_t count(Worker &, const db::Query &) override;
101 :
102 : virtual Value field(db::Action, Worker &, uint64_t oid, const Field &, Value &&) override;
103 : virtual Value field(db::Action, Worker &, const Value &, const Field &, Value &&) override;
104 :
105 : protected: // prop interface
106 : virtual Vector<int64_t> performQueryListForIds(const QueryList &, size_t count = stappler::maxOf<size_t>()) override;
107 : virtual Value performQueryList(const QueryList &, size_t count = stappler::maxOf<size_t>(), bool forUpdate = false) override;
108 :
109 : virtual bool removeFromView(const db::FieldView &, const Scheme *, uint64_t oid) override;
110 : virtual bool addToView(const db::FieldView &, const Scheme *, uint64_t oid, const Value &) override;
111 :
112 : virtual Vector<int64_t> getReferenceParents(const Scheme &, uint64_t oid, const Scheme *, const Field *) override;
113 :
114 : int64_t selectQueryId(const SqlQuery &);
115 : size_t performQuery(const SqlQuery &);
116 :
117 : Value selectValueQuery(const Scheme &, const SqlQuery &, const Vector<const Field *> &virtuals);
118 : Value selectValueQuery(const Field &, const SqlQuery &, const Vector<const Field *> &virtuals);
119 : void selectValueQuery(Value &, const FieldView &, const SqlQuery &);
120 :
121 : Value getFileField(Worker &w, SqlQuery &query, uint64_t oid, uint64_t targetId, const Field &f);
122 : size_t getFileCount(Worker &w, SqlQuery &query, uint64_t oid, uint64_t targetId, const Field &f);
123 :
124 : Value getArrayField(Worker &w, SqlQuery &query, uint64_t oid, const Field &f);
125 : size_t getArrayCount(Worker &w, SqlQuery &query, uint64_t oid, const Field &f);
126 :
127 : Value getObjectField(Worker &w, SqlQuery &query, uint64_t oid, uint64_t targetId, const Field &f);
128 : size_t getObjectCount(Worker &w, SqlQuery &query, uint64_t oid, uint64_t targetId, const Field &f);
129 :
130 : Value getSetField(Worker &w, SqlQuery &query, uint64_t oid, const Field &f, const db::Query &);
131 : size_t getSetCount(Worker &w, SqlQuery &query, uint64_t oid, const Field &f, const db::Query &);
132 :
133 : Value getViewField(Worker &w, SqlQuery &query, uint64_t oid, const Field &f, const db::Query &);
134 : size_t getViewCount(Worker &w, SqlQuery &query, uint64_t oid, const Field &f, const db::Query &);
135 :
136 : Value getSimpleField(Worker &w, SqlQuery &query, uint64_t oid, const Field &f);
137 : size_t getSimpleCount(Worker &w, SqlQuery &query, uint64_t oid, const Field &f);
138 :
139 : bool insertIntoSet(SqlQuery &, const Scheme &s, int64_t id, const db::FieldObject &field, const Field &f, const Value &d);
140 : bool insertIntoArray(SqlQuery &, const Scheme &s, int64_t id, const Field &field, Value &d);
141 : bool insertIntoRefSet(SqlQuery &, const Scheme &s, int64_t id, const Field &field, const Vector<int64_t> &d);
142 : bool cleanupRefSet(SqlQuery &query, const Scheme &, uint64_t oid, const Field &, const Vector<int64_t> &objsToRemove);
143 :
144 : void performPostUpdate(const db::Transaction &, SqlQuery &query, const Scheme &s, Value &data, int64_t id, const Value &upd, bool clear);
145 :
146 : const Driver *_driver = nullptr;
147 : Vector<stappler::Pair<stappler::Time, Bytes>> _bcasts;
148 : };
149 :
150 : }
151 :
152 : #endif /* STAPPLER_DB_SQL_STSQLHANDLE_H_ */
|