sptk2 logo
SPTK Home Page
MySQLStatement.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#include <mysql.h>
30
31#include <cstdio>
32#include <list>
33#include <string>
34
35#include <sptk5/db/DatabaseField.h>
36#include <sptk5/db/DatabaseStatement.h>
37
38namespace sptk {
39
40class MySQLConnection;
41
42class MySQLStatementField;
43
48 : public DatabaseStatement<MySQLConnection, MYSQL_STMT>
49{
50public:
56 static VariantDataType mySQLTypeToVariantType(enum_field_types mysqlType);
57
63 static enum_field_types variantTypeToMySQLType(VariantDataType dataType);
64
71 static void dateTimeToMySQLDate(MYSQL_TIME& mysqlDate, DateTime timestamp, VariantDataType timeType);
72
79 MySQLStatement(MySQLConnection* connection, String sql, bool autoPrepare);
80
85
90
95
100
105 void enumerateParams(QueryParameterList& queryParams) override;
106
110 void setParameterValues() override;
111
116 void prepare(const String& sql) const;
117
121 void execute(bool) override;
122
127 void bindResult(FieldList& fields);
128
134
138 void close() override;
139
143 void fetch() override;
144
145private:
146 String m_sql;
147 std::vector<MYSQL_BIND> m_paramBuffers;
148 std::vector<unsigned long> m_paramLengths;
149 std::vector<MYSQL_BIND> m_fieldBuffers;
150 std::shared_ptr<MYSQL_STMT> m_stmt;
151 std::shared_ptr<MYSQL_RES> m_result {nullptr};
152 MYSQL_ROW m_row {};
153
158 void readUnpreparedResultRow(FieldList& fields) const;
159
164 void readPreparedResultRow(FieldList& fields);
165
172 static void decodeMySQLTime(Field* _field, const MYSQL_TIME& mysqlTime, VariantDataType fieldType);
173
180 static void decodeMySQLFloat(Field* _field, MYSQL_BIND& bind);
181
182 [[noreturn]] void throwMySQLError() const
183 {
184 throw DatabaseException(mysql_stmt_error(statement()));
185 }
186
187 [[nodiscard]] bool bindVarCharField(MYSQL_BIND& bind, MySQLStatementField* field, size_t fieldIndex,
188 uint32_t dataLength) const;
189};
190
191} // namespace sptk
Definition: DatabaseStatement.h:38
Definition: DateTime.h:86
Definition: FieldList.h:50
Definition: MySQLStatement.h:49
void setParameterValues() override
static enum_field_types variantTypeToMySQLType(VariantDataType dataType)
void readResultRow(FieldList &fields)
void prepare(const String &sql) const
MySQLStatement(MySQLStatement &&)=default
void fetch() override
void bindResult(FieldList &fields)
MySQLStatement(MySQLConnection *connection, String sql, bool autoPrepare)
MySQLStatement(const MySQLStatement &)=delete
static VariantDataType mySQLTypeToVariantType(enum_field_types mysqlType)
MySQLStatement & operator=(const MySQLStatement &)=delete
MySQLStatement & operator=(MySQLStatement &&)=default
static void dateTimeToMySQLDate(MYSQL_TIME &mysqlDate, DateTime timestamp, VariantDataType timeType)
void enumerateParams(QueryParameterList &queryParams) override
void close() override
void execute(bool) override
Definition: QueryParameterList.h:57
Definition: String.h:49
VariantDataType
Definition: VariantData.h:44

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