Statement.h
1/* Copyright (C) 2006-2026 by Tommi Maekitalo
2 Copyright (C) 2006-2026 by Marc Boris Duerner
3 SPDX-License-Identifier: LGPL-2.1-or-later WITH mif-exception
4*/
5
6#ifndef PT_DB_STATEMENT_H
7#define PT_DB_STATEMENT_H
8
9#include <Pt/SmartPtr.h>
10#include <Pt/Signal.h>
11#include <Pt/Date.h>
12#include <Pt/Time.h>
13#include <Pt/DateTime.h>
14#include <Pt/Db/Api.h>
15#include <Pt/Db/Cursor.h>
16#include <Pt/Db/IStatement.h>
17#include <Pt/Db/IConnection.h>
18#include <Pt/Db/Row.h>
19#include <string>
20
21
22namespace Pt {
23
24namespace Db {
25
26 class Connection;
27 class Result;
28 class Row;
29
64 class PT_DB_API Statement
65 {
66 public:
69 typedef IStatement::size_type size_type;
70
71 private:
73
74 public:
78 : _stmt(stmt)
79 { }
80
81 ~Statement();
82
88 { _stmt->clear(); return *this; }
89
90
95 Statement& setNull(const std::string& col)
96 { _stmt->setNull(col); return *this; }
97
102 Statement& set(const std::string& col, bool data)
103 { _stmt->setBool(col, data); return *this; }
104
109 Statement& set(const std::string& col, int data)
110 { _stmt->setInt(col, data); return *this; }
111
116 Statement& set(const std::string& col, unsigned data)
117 { _stmt->setUnsigned(col, data); return *this; }
118
123 Statement& set(const std::string& col, float data)
124 { _stmt->setFloat(col, data); return *this; }
125
130 Statement& set(const std::string& col, double data)
131 { _stmt->setDouble(col, data); return *this; }
132
137 Statement& set(const std::string& col, char data)
138 { _stmt->setChar(col, data); return *this; }
139
144 Statement& set(const std::string& col, const std::string& data)
145 { _stmt->setString(col, data); return *this; }
146
151 Statement& set(const std::string& col, const Blob& data)
152 { _stmt->setBlob(col, data); return *this; }
153
158 Statement& set(const std::string& col, const char* data)
159 { data == 0 ? _stmt->setNull(col)
160 : _stmt->setString(col, data); return *this; }
161
166 Statement& set(const std::string& col, const Date& data)
167 { _stmt->setDate(col, data); return *this; }
168
173 Statement& set(const std::string& col, const Time& data)
174 { _stmt->setTime(col, data); return *this; }
175
180 Statement& set(const std::string& col, const DateTime& data)
181 {
182 _stmt->setDatetime(col, data);
183 return *this;
184 }
185
189
192 void cancel();
193
194 public:
198
202
208
212
215 long long lastInsertId() const;
216
217 public:
221
229
235
239
245
249
250 public:
253 bool operator!() const
254 { return !_stmt; }
255
258 const IStatement* getImpl() const
259 { return &*_stmt; }
260
264 { return _stmt.get(); }
265 };
266
267} // namespace Db
268
269} // namespace Pt
270
271#endif // PT_DB_STATEMENT_H
Combined Date and Time value.
Definition DateTime.h:66
Date expressed in year, month, and day.
Definition Date.h:104
Copy-on-write binary large object.
Definition Blob.h:144
Database session for a registered driver.
Definition Connection.h:97
Batch cursor for large query results.
Definition Cursor.h:58
Prepared-statement backend.
Definition IStatement.h:33
Buffered query result with random-access rows.
Definition Result.h:42
One row of a buffered query result.
Definition Row.h:34
Prepared SQL statement with named host variables.
Definition Statement.h:65
void cancel()
Cancels any pending asynchronous operation.
Statement & setNull(const std::string &col)
Sets the host variable col to NULL.
Definition Statement.h:95
Statement & set(const std::string &col, float data)
Sets the host variable col to data.
Definition Statement.h:123
Statement & clear()
Sets all host variables to NULL.
Definition Statement.h:87
long long lastInsertId() const
Returns the generated row id of the last insert.
Statement & set(const std::string &col, bool data)
Sets the host variable col to data.
Definition Statement.h:102
Statement & set(const std::string &col, const Date &data)
Sets the host variable col to data.
Definition Statement.h:166
Result endSelect()
Completes an asynchronous select.
void beginExecute()
Starts asynchronous execution.
Statement & set(const std::string &col, double data)
Sets the host variable col to data.
Definition Statement.h:130
size_type execute()
Executes the statement and returns the number of rows changed.
bool operator!() const
Returns true if this object is not bound to a statement.
Definition Statement.h:253
Statement & set(const std::string &col, const char *data)
Sets the host variable col to data, or to NULL if data is 0.
Definition Statement.h:158
Statement(IStatement *stmt=0)
Takes ownership of the backend stmt.
Definition Statement.h:77
Value selectValue()
Executes the statement and returns the first column of the first row.
Statement & set(const std::string &col, const DateTime &data)
Sets the host variable col to data.
Definition Statement.h:180
Signal & selectFinished()
Signal emitted when an asynchronous select completes.
Statement & set(const std::string &col, unsigned data)
Sets the host variable col to data.
Definition Statement.h:116
Cursor getCursor(size_type batchSize)
Opens a batch cursor that fetches batchSize rows at a time.
Row selectRow()
Executes the statement and returns the first row.
Statement & set(const std::string &col, char data)
Sets the host variable col to data.
Definition Statement.h:137
Statement & set(const std::string &col, const Blob &data)
Sets the host variable col to data.
Definition Statement.h:151
const IStatement * getImpl() const
Returns the backend implementation.
Definition Statement.h:258
size_type endExecute()
Completes asynchronous execution.
Signal & executeFinished()
Signal emitted when asynchronous execution completes.
IStatement * impl()
Returns the backend implementation.
Definition Statement.h:263
Statement & set(const std::string &col, int data)
Sets the host variable col to data.
Definition Statement.h:109
void beginSelect()
Starts an asynchronous select.
Statement & set(const std::string &col, const std::string &data)
Sets the host variable col to data.
Definition Statement.h:144
Result select()
Executes the statement and returns a buffered result.
IStatement::size_type size_type
Size type of this statement.
Definition Statement.h:69
Statement & set(const std::string &col, const Time &data)
Sets the host variable col to data.
Definition Statement.h:173
One column value of a query result.
Definition Value.h:35
Multicast Signal to call multiple slots.
Definition Signal.h:190
Policy based smart pointer.
Definition SmartPtr.h:462
Time expressed in hours, minutes, seconds and milliseconds.
Definition Time.h:98
Portable SQL database access.
Core module.
Definition Allocator.h:33