Line data Source code
1 : /**
2 : Copyright (c) 2017-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_PQ_SPPGHANDLE_H_
25 : #define STAPPLER_DB_PQ_SPPGHANDLE_H_
26 :
27 : #include "SPPqDriver.h"
28 : #include "SPSqlHandle.h"
29 : #include "SPDbScheme.h"
30 :
31 : namespace STAPPLER_VERSIONIZED stappler::db::pq {
32 :
33 : enum class TransactionLevel {
34 : ReadCommited,
35 : RepeatableRead,
36 : Serialized,
37 : };
38 :
39 : class Handle final : public db::sql::SqlHandle {
40 : public:
41 : Handle(const Driver *, Driver::Handle);
42 :
43 : Handle(const Handle &) = delete;
44 : Handle &operator=(const Handle &) = delete;
45 :
46 : Handle(Handle &&) = delete;
47 : Handle &operator=(Handle &&) = delete;
48 :
49 : explicit operator bool () const;
50 :
51 400 : const Driver *getDriver() const { return driver; }
52 :
53 : Driver::Handle getHandle() const;
54 : Driver::Connection getConnection() const;
55 :
56 0 : virtual bool isNotificationsSupported() const override { return true; }
57 :
58 : virtual void makeQuery(const Callback<void(sql::SqlQuery &)> &cb, const sql::QueryStorageHandle *) override;
59 :
60 : virtual bool selectQuery(const db::sql::SqlQuery &, const Callback<bool(Result &)> &cb,
61 : const Callback<void(const Value &)> &err = nullptr) override;
62 : virtual bool performSimpleQuery(const StringView &,
63 : const Callback<void(const Value &)> &err = nullptr) override;
64 : virtual bool performSimpleSelect(const StringView &, const Callback<void(Result &)> &cb,
65 : const Callback<void(const Value &)> &err = nullptr) override;
66 :
67 : virtual bool isSuccess() const override;
68 :
69 : void close();
70 :
71 : public: // adapter interface
72 : virtual bool init(const BackendInterface::Config &cfg, const Map<StringView, const Scheme *> &) override;
73 :
74 : protected:
75 350 : virtual bool beginTransaction() override { return beginTransaction_pg(TransactionLevel::ReadCommited); }
76 350 : virtual bool endTransaction() override { return endTransaction_pg(); }
77 :
78 : bool beginTransaction_pg(TransactionLevel l);
79 : void cancelTransaction_pg();
80 : bool endTransaction_pg();
81 :
82 : using ViewIdVec = Vector<Pair<const Scheme::ViewScheme *, int64_t>>;
83 :
84 : const Driver *driver = nullptr;
85 : Driver::Handle handle = Driver::Handle(nullptr);
86 : Driver::Connection conn = Driver::Connection(nullptr);
87 : Driver::Status lastError = Driver::Status::Empty;
88 : Value lastErrorInfo;
89 : TransactionLevel level = TransactionLevel::ReadCommited;
90 : };
91 :
92 : class PgQueryInterface : public db::QueryInterface {
93 : public:
94 : using Binder = db::Binder;
95 :
96 : PgQueryInterface(const sql::Driver *, const sql::QueryStorageHandle *s);
97 :
98 : size_t push(String &&val);
99 : size_t push(const StringView &val);
100 : size_t push(Bytes &&val);
101 : size_t push(StringStream &query, const Value &val, bool force, bool compress = false);
102 :
103 : virtual void bindInt(db::Binder &, StringStream &query, int64_t val) override;
104 : virtual void bindUInt(db::Binder &, StringStream &query, uint64_t val) override;
105 : virtual void bindDouble(db::Binder &, StringStream &query, double val) override;
106 : virtual void bindString(db::Binder &, StringStream &query, const String &val) override;
107 : virtual void bindMoveString(db::Binder &, StringStream &query, String &&val) override;
108 : virtual void bindStringView(db::Binder &, StringStream &query, const StringView &val) override;
109 : virtual void bindBytes(db::Binder &, StringStream &query, const Bytes &val) override;
110 : virtual void bindMoveBytes(db::Binder &, StringStream &query, Bytes &&val) override;
111 : virtual void bindCoderSource(db::Binder &, StringStream &query, const stappler::CoderSource &val) override;
112 : virtual void bindValue(db::Binder &, StringStream &query, const Value &val) override;
113 : virtual void bindDataField(db::Binder &, StringStream &query, const db::Binder::DataField &f) override;
114 : virtual void bindTypeString(db::Binder &, StringStream &query, const db::Binder::TypeString &type) override;
115 : virtual void bindFullText(db::Binder &, StringStream &query, const db::Binder::FullTextField &) override;
116 : virtual void bindFullTextFrom(db::Binder &, StringStream &query, const db::Binder::FullTextFrom &) override;
117 : virtual void bindFullTextRank(db::Binder &, StringStream &query, const db::Binder::FullTextRank &) override;
118 : virtual void bindFullTextQuery(db::Binder &, StringStream &query, const db::Binder::FullTextQueryRef &d) override;
119 : virtual void bindIntVector(Binder &, StringStream &query, const Vector<int64_t> &vec) override;
120 : virtual void bindDoubleVector(Binder &b, StringStream &query, const Vector<double> &vec) override;
121 : virtual void bindStringVector(Binder &b, StringStream &query, const Vector<StringView> &vec) override;
122 : virtual void clear() override;
123 :
124 : public:
125 : Vector<Bytes> params;
126 : Vector<bool> binary;
127 : const sql::Driver *driver = nullptr;
128 : const sql::QueryStorageHandle *storage = nullptr;
129 : };
130 :
131 : }
132 :
133 : #endif /* STAPPLER_DB_PQ_STPGHANDLE_H_ */
|