Line data Source code
1 : /**
2 : Copyright (c) 2019-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_SPDBPQDRIVER_H_
25 : #define STAPPLER_DB_PQ_SPDBPQDRIVER_H_
26 :
27 : #include "SPSqlDriver.h"
28 :
29 : namespace STAPPLER_VERSIONIZED stappler::db::pq {
30 :
31 : struct DriverSym;
32 :
33 : class Driver : public sql::Driver {
34 : public:
35 : enum class Status {
36 : Empty = 0,
37 : CommandOk,
38 : TuplesOk,
39 : CopyOut,
40 : CopyIn,
41 : BadResponse,
42 : NonfatalError,
43 : FatalError,
44 : CopyBoth,
45 : SingleTuple,
46 : };
47 :
48 : enum class TransactionStatus {
49 : Idle,
50 : Active,
51 : InTrans,
52 : InError,
53 : Unknown
54 : };
55 :
56 : static Driver *open(pool_t *, ApplicationInterface *, StringView path = StringView(), const void *external = nullptr);
57 :
58 : virtual ~Driver();
59 :
60 : virtual bool init(Handle handle, const Vector<StringView> &) override;
61 :
62 : virtual void performWithStorage(Handle handle, const Callback<void(const db::Adapter &)> &cb) const override;
63 : virtual BackendInterface *acquireInterface(Handle handle, pool_t *) const override;
64 :
65 : virtual Handle connect(const Map<StringView, StringView> &) const override;
66 : virtual void finish(Handle) const override;
67 :
68 : virtual Connection getConnection(Handle h) const override;
69 :
70 : virtual bool isValid(Handle) const override;
71 : virtual bool isValid(Connection) const override;
72 : virtual bool isIdle(Connection) const override;
73 :
74 : virtual Time getConnectionTime(Handle) const override;
75 :
76 : virtual int listenForNotifications(Handle) const override;
77 : virtual bool consumeNotifications(Handle, const Callback<void(StringView)> &) const override;
78 :
79 0 : virtual bool isNotificationsSupported() const override { return true; }
80 :
81 : TransactionStatus getTransactionStatus(Connection) const;
82 :
83 : Status getStatus(Result res) const;
84 :
85 : bool isBinaryFormat(Result res, size_t field) const;
86 : bool isNull(Result res, size_t row, size_t field) const;
87 : char *getValue(Result res, size_t row, size_t field) const;
88 : size_t getLength(Result res, size_t row, size_t field) const;
89 :
90 : char *getName(Result res, size_t field) const;
91 : unsigned int getType(Result res, size_t field) const;
92 :
93 : size_t getNTuples(Result res) const;
94 : size_t getNFields(Result res) const;
95 : size_t getCmdTuples(Result res) const;
96 :
97 : char *getStatusMessage(Status) const;
98 : char *getResultErrorMessage(Result res) const;
99 :
100 : void clearResult(Result res) const;
101 :
102 : Result exec(Connection conn, const char *query) const;
103 : Result exec(Connection conn, const char *command, int nParams, const char *const *paramValues,
104 : const int *paramLengths, const int *paramFormats, int resultFormat) const;
105 :
106 : explicit operator bool () const { return _handle != nullptr; }
107 :
108 : BackendInterface::StorageType getTypeById(uint32_t) const;
109 : StringView getTypeNameById(uint32_t) const;
110 :
111 : protected:
112 : Driver(pool_t *pool, ApplicationInterface *, StringView, const void *external);
113 :
114 : Handle doConnect(const char * const *keywords, const char * const *values, int expand_dbname) const;
115 :
116 : bool _init = false;
117 :
118 : Vector<Pair<uint32_t, BackendInterface::StorageType>> _storageTypes;
119 : Vector<Pair<uint32_t, String>> _customTypes;
120 :
121 : DriverSym *_handle = nullptr;
122 : const void *_external = nullptr;
123 : };
124 :
125 : class ResultCursor : public db::ResultCursor {
126 : public:
127 11525 : inline static constexpr bool pgsql_is_success(Driver::Status x) {
128 11525 : return (x == Driver::Status::Empty) || (x == Driver::Status::CommandOk) || (x == Driver::Status::TuplesOk) || (x == Driver::Status::SingleTuple);
129 : }
130 :
131 : ResultCursor(const Driver *d, Driver::Result res);
132 :
133 : virtual ~ResultCursor();
134 : virtual bool isBinaryFormat(size_t field) const override;
135 : virtual bool isNull(size_t field) const override;
136 : virtual StringView toString(size_t field) const override;
137 : virtual BytesView toBytes(size_t field) const override;
138 : virtual int64_t toInteger(size_t field) const override;
139 : virtual double toDouble(size_t field) const override;
140 : virtual bool toBool(size_t field) const override;
141 : virtual Value toTypedData(size_t field) const override;
142 : virtual Value toCustomData(size_t field, const FieldCustom *) const override;
143 : virtual int64_t toId() const override;
144 : virtual StringView getFieldName(size_t field) const override;
145 : virtual Value getInfo() const override;
146 : virtual void clear() override;
147 : Driver::Status getError() const;
148 :
149 : virtual bool isSuccess() const override;
150 : virtual bool isEmpty() const override;
151 : virtual bool isEnded() const override;
152 : virtual size_t getFieldsCount() const override;
153 : virtual size_t getAffectedRows() const override;
154 : virtual size_t getRowsHint() const override;
155 :
156 : virtual bool next() override;
157 : virtual void reset() override;
158 :
159 : public:
160 : const Driver *driver = nullptr;
161 : Driver::Result result = Driver::Result(nullptr);
162 : size_t nrows = 0;
163 : size_t currentRow = 0;
164 : Driver::Status err = Driver::Status::Empty;
165 : };
166 :
167 : }
168 :
169 : #endif /* STAPPLER_DB_PQ_SPDBPQDRIVER_H_ */
|