sptk2 logo
SPTK Home Page
Query.h
1/*
2╔══════════════════════════════════════════════════════════════════════════════╗
3║ SIMPLY POWERFUL TOOLKIT (SPTK) ║
4╟──────────────────────────────────────────────────────────────────────────────╢
5║ copyright © 1999-2022 Alexey Parshin. All rights reserved. ║
6║ email alexeyp@gmail.com ║
7╚══════════════════════════════════════════════════════════════════════════════╝
8┌──────────────────────────────────────────────────────────────────────────────┐
9│ This library is free software; you can redistribute it and/or modify it │
10│ under the terms of the GNU Library General Public License as published by │
11│ the Free Software Foundation; either version 2 of the License, or (at your │
12│ option) any later version. │
13│ │
14│ This library is distributed in the hope that it will be useful, but │
15│ WITHOUT ANY WARRANTY; without even the implied warranty of │
16│ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library │
17│ General Public License for more details. │
18│ │
19│ You should have received a copy of the GNU Library General Public License │
20│ along with this library; if not, write to the Free Software Foundation, │
21│ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. │
22│ │
23│ Please report all bugs and problems to alexeyp@gmail.com. │
24└──────────────────────────────────────────────────────────────────────────────┘
25*/
26
27#pragma once
28
29#ifdef WIN32
30#include <winsock2.h>
31
32#include <windows.h>
33#endif
34
35#include <sptk5/DataSource.h>
36
37#include <sptk5/FieldList.h>
38#include <sptk5/db/AutoDatabaseConnection.h>
39#include <sptk5/db/QueryParameterList.h>
40#include <sptk5/threads/Locks.h>
41
42namespace sptk {
43
50 : public DataSource
51{
52public:
57 explicit QueryStatementManagement(bool autoPrepare)
58 : m_autoPrepare(autoPrepare)
59 {
60 }
61
63
67 StmtHandle statement() const
68 {
69 return m_statement;
70 }
71
77 bool autoPrepare() const
78 {
79 return m_autoPrepare;
80 }
81
85 bool active() const
86 {
87 return m_active;
88 }
89
93 bool prepared() const
94 {
95 return m_prepared;
96 }
97
101 bool eof() const override
102 {
103 return m_eof;
104 }
105
110 bool bulkMode() const;
111
119
124
129 {
130 return m_db;
131 }
132
137 {
138 connect(db);
139 }
140
141protected:
146
150 void setStatement(StmtHandle statement)
151 {
152 m_statement = statement;
153 }
154
155 void setPrepared(bool prepared)
156 {
157 m_prepared = prepared;
158 }
159
160 void setActive(bool active)
161 {
162 m_active = active;
163 }
164
165 void setEof(bool eof)
166 {
167 m_eof = eof;
168 }
169
174 void setBulkMode(bool _bulkMode);
175
180 void closeStmt(bool freeStatement = false);
181
188 void closeQuery(bool releaseStatement = false);
189
194 {
195 return m_messages;
196 }
197
198 String& getSQL()
199 {
200 return m_sql;
201 }
202
203 const String& getSQL() const
204 {
205 return m_sql;
206 }
207
208 void setSQL(const String& sql)
209 {
210 m_sql = sql;
211 }
212
216 [[noreturn]] void notImplemented(const String& functionName) const;
217
218private:
219 bool m_autoPrepare {true};
220 StmtHandle m_statement {nullptr};
221 bool m_prepared {false};
222 bool m_active {false};
223 bool m_eof {true};
224 bool m_bulkMode {false};
225 String m_sql;
226 Strings m_messages;
227 PoolDatabaseConnection* m_db {nullptr};
228};
229
237class SP_EXPORT Query
239{
240 friend class PoolDatabaseConnection;
241
243
244public:
248 Query() noexcept;
249
261 explicit Query(const DatabaseConnection& db, const String& sql = "", bool autoPrepare = true);
262
274 explicit Query(PoolDatabaseConnection* db, const String& sql = "", bool autoPrepare = true);
275
279 Query(const Query&) = delete;
280
281 Query& operator=(const Query&) = delete;
282
286 ~Query() override;
287
294 Field& operator[](size_t fieldIndex) override
295 {
296 return m_fields[(int) fieldIndex];
297 }
298
302 Field& operator[](const String& fieldName) override
303 {
304 return m_fields[fieldName];
305 }
306
311 size_t fieldCount() const override
312 {
313 return m_fields.size();
314 }
315
321 [[noreturn]] size_t recordCount() const override
322 {
323 notImplemented("recordCount");
324 }
325
329 [[nodiscard]] virtual String sql() const
330 {
331 return getSQL();
332 }
333
339 virtual void sql(const String& _sql);
340
345 {
346 return m_fields;
347 }
348
353 {
354 return m_params;
355 }
356
360 bool readField(const char* fname, Variant& value) override;
361
365 bool writeField(const char* fname, const Variant& fvalue) override;
366
373 bool open() override;
374
380 bool close() override
381 {
382 closeQuery();
383 return true;
384 }
385
389 bool next() override
390 {
391 fetch();
392 return true;
393 }
394
398 virtual void exec()
399 {
400 open();
401 }
402
409 virtual void exec(const String& newSQL)
410 {
411 sql(newSQL);
412 open();
413 }
414
418 void fetch();
419
425 size_t paramCount() const
426 {
427 return m_params.size();
428 }
429
438 QueryParameter& param(const char* paramName) const
439 {
440 return m_params[paramName];
441 }
442
450 QueryParameter& param(const String& paramName) const
451 {
452 return m_params[paramName.c_str()];
453 }
454
460 QueryParameter& param(size_t paramIndex) const
461 {
462 return m_params[paramIndex];
463 }
464
472 [[noreturn]] static void throwError(const String& method, const String& error);
473
474protected:
478 void execute();
479
486 bool loadData() override
487 {
488 return false;
489 }
490
497 bool saveData() override
498 {
499 return false;
500 }
501
502private:
506 QueryParameterList m_params;
507
511 FieldList m_fields {true};
512
520 void sqlParseParameter(const char* paramStart, const char* paramEnd, int& paramNumber, String& sql);
521
522 String parseParameters(const String& _sql);
523
524 const char* readParamater(String& sql, int& paramNumber, const char* paramStart, const char* paramEnd);
525};
526
527using SQuery = std::shared_ptr<Query>;
528
533#define THROW_QUERY_ERROR(query, error) \
534 { \
535 std::stringstream err; \
536 err << error; \
537 throw sptk::DatabaseException(err.str(), __FILE__, __LINE__, query->sql()); \
538 }
539
540constexpr int FETCH_BUFFER_SIZE = 1024;
541
542} // namespace sptk
Definition: DataSource.h:49
Definition: FieldList.h:50
Definition: Field.h:54
Definition: PoolDatabaseConnection.h:98
Definition: PoolDatabaseConnection.h:231
Definition: QueryParameterList.h:57
Definition: QueryParameter.h:44
Definition: Query.h:51
void connect(PoolDatabaseConnection *db)
void closeStmt(bool freeStatement=false)
void closeQuery(bool releaseStatement=false)
StmtHandle statement() const
Definition: Query.h:67
bool eof() const override
Definition: Query.h:101
void setDatabase(PoolDatabaseConnection *db)
void database(PoolDatabaseConnection *db)
Definition: Query.h:136
void notImplemented(const String &functionName) const
Strings & messages()
Definition: Query.h:193
bool prepared() const
Definition: Query.h:93
void setStatement(StmtHandle statement)
Definition: Query.h:150
PoolDatabaseConnection * database() const
Definition: Query.h:128
bool autoPrepare() const
Definition: Query.h:77
bool active() const
Definition: Query.h:85
QueryStatementManagement(bool autoPrepare)
Definition: Query.h:57
void setBulkMode(bool _bulkMode)
Definition: Query.h:239
bool close() override
Definition: Query.h:380
bool next() override
Definition: Query.h:389
bool readField(const char *fname, Variant &value) override
FieldList & fields()
Definition: Query.h:344
void fetch()
Field & operator[](const String &fieldName) override
Definition: Query.h:302
size_t recordCount() const override
Definition: Query.h:321
virtual String sql() const
Definition: Query.h:329
QueryParameter & param(const String &paramName) const
Definition: Query.h:450
void execute()
QueryParameterList & params()
Definition: Query.h:352
QueryParameter & param(const char *paramName) const
Definition: Query.h:438
size_t paramCount() const
Definition: Query.h:425
bool open() override
virtual void exec(const String &newSQL)
Definition: Query.h:409
bool saveData() override
Definition: Query.h:497
Query() noexcept
virtual void sql(const String &_sql)
virtual void exec()
Definition: Query.h:398
QueryParameter & param(size_t paramIndex) const
Definition: Query.h:460
bool loadData() override
Definition: Query.h:486
bool writeField(const char *fname, const Variant &fvalue) override
static void throwError(const String &method, const String &error)
size_t fieldCount() const override
Definition: Query.h:311
Definition: String.h:49
Definition: Strings.h:52
Definition: Variant.h:372

Fri Oct 14 2022 09:58:31: SPTK 5.4.1