KDb

MysqlCursor.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2003 Joseph Wenninger<jowenn@kde.org>
3 Copyright (C) 2005-2016 Jarosław Staniek <staniek@kde.org>
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this program; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19*/
20
21#include "MysqlCursor.h"
22#include "MysqlConnection.h"
23#include "MysqlConnection_p.h"
24#include "KDbError.h"
25#include "KDb.h"
26#include "KDbRecordData.h"
27
28#include <limits.h>
29
30#define BOOL bool
31
32MysqlCursor::MysqlCursor(KDbConnection* conn, const KDbEscapedString& sql,
33 KDbCursor::Options options)
34 : KDbCursor(conn, sql, options | KDbCursor::Option::Buffered)
35 , d(new MysqlCursorData(conn))
36{
37}
38
39MysqlCursor::MysqlCursor(KDbConnection* conn, KDbQuerySchema* query, KDbCursor::Options options)
40 : KDbCursor(conn, query, options | KDbCursor::Option::Buffered)
41 , d(new MysqlCursorData(conn))
42{
43}
44
45MysqlCursor::~MysqlCursor()
46{
47 close();
48 delete d;
49}
50
51bool MysqlCursor::drv_open(const KDbEscapedString& sql)
52{
53 if (mysql_real_query(d->mysql, sql.constData(), sql.length()) == 0) {
54 if (mysql_errno(d->mysql) == 0) {
55 //! @todo Add option somewhere so we can use more optimal mysql_num_rows().
56 //! In this case mysql_num_rows() does not work however.
57 d->mysqlres = mysql_store_result(d->mysql);
58 m_fieldCount = mysql_num_fields(d->mysqlres);
60 d->numRows = mysql_num_rows(d->mysqlres);
61
62 m_records_in_buf = d->numRows;
64 return true;
65 }
66 }
67
68 storeResult();
69 return false;
70}
71
72bool MysqlCursor::drv_close()
73{
74 mysql_free_result(d->mysqlres);
75 d->mysqlres = nullptr;
76 d->mysqlrow = nullptr;
77 d->lengths = nullptr;
78 d->numRows = 0;
79 return true;
80}
81
82void MysqlCursor::drv_getNextRecord()
83{
84 if (at() >= d->numRows) {
86 }
87 else if (at() < 0) {
88 // control will reach here only when at() < 0 ( which is usually -1 )
89 // -1 is same as "1 beyond the End"
91 }
92 else { // 0 <= at() < d->numRows
93 d->lengths = mysql_fetch_lengths(d->mysqlres);
95 }
96}
97
98// This isn't going to work right now as it uses d->mysqlrow
99QVariant MysqlCursor::value(int pos)
100{
101 if (!d->mysqlrow || pos >= m_fieldCount || d->mysqlrow[pos] == nullptr)
102 return QVariant();
103
105 ? m_visibleFieldsExpanded->at(pos)->field() : nullptr;
106
107//! @todo js: use MYSQL_FIELD::type here!
108
109 bool ok;
110 return KDb::cstringToVariant(d->mysqlrow[pos], f ? f->type() : KDbField::Text,
111 &ok, d->lengths[pos]);
112}
113
114/* As with sqlite, the DB library returns all values (including numbers) as
115 strings. So just put that string in a QVariant and let KDb deal with it.
116 */
117bool MysqlCursor::drv_storeCurrentRecord(KDbRecordData* data) const
118{
119// mysqlDebug() << "position is " << (long)m_at;
120 if (d->numRows == 0)
121 return false;
122
123 if (!m_visibleFieldsExpanded) {//simple version: without types
124 for (int i = 0; i < m_fieldCount; ++i) {
125 (*data)[i] = QString::fromUtf8(d->mysqlrow[i], d->lengths[i]);
126 }
127 return true;
128 }
129 for (int i = 0; i < m_fieldCount; ++i) {
130 KDbField *f = m_visibleFieldsExpanded->at(i)->field();
131 bool ok;
132 (*data)[i] = KDb::cstringToVariant(d->mysqlrow[i], f ? f->type() : KDbField::Text,
133 &ok, d->lengths[i]);
134 if (!ok) {
135 return false;
136 }
137 }
138 return true;
139}
140
141void MysqlCursor::drv_appendCurrentRecordToBuffer()
142{
143}
144
145
146void MysqlCursor::drv_bufferMovePointerNext()
147{
148 d->mysqlrow = mysql_fetch_row(d->mysqlres);
149 d->lengths = mysql_fetch_lengths(d->mysqlres);
150}
151
152void MysqlCursor::drv_bufferMovePointerPrev()
153{
154 mysql_data_seek(d->mysqlres, m_at - 1);
155 d->mysqlrow = mysql_fetch_row(d->mysqlres);
156 d->lengths = mysql_fetch_lengths(d->mysqlres);
157}
158
159
160void MysqlCursor::drv_bufferMovePointerTo(qint64 to)
161{
162 mysql_data_seek(d->mysqlres, to);
163 d->mysqlrow = mysql_fetch_row(d->mysqlres);
164 d->lengths = mysql_fetch_lengths(d->mysqlres);
165}
166
167const char** MysqlCursor::recordData() const
168{
169 //! @todo
170 return nullptr;
171}
172
173QString MysqlCursor::serverResultName() const
174{
175 return MysqlConnectionInternal::serverResultName(d->mysql);
176}
177
178void MysqlCursor::storeResult()
179{
180 d->storeResult(&m_result);
181}
Provides database connection, allowing queries and data modification.
Provides database cursor functionality.
Definition KDbCursor.h:69
@ End
at the end of data
@ Ok
the data is fetched
int m_fieldsToStoreInRecord
Used by storeCurrentRecord(), reimplement if needed.
Definition KDbCursor.h:302
int m_fieldCount
cached field count information
Definition KDbCursor.h:301
int m_records_in_buf
number of records currently stored in the buffer
Definition KDbCursor.h:319
bool m_buffering_completed
true if we already have all records stored in the buffer
Definition KDbCursor.h:320
KDbQueryColumnInfo::Vector * m_visibleFieldsExpanded
Useful e.g. for value(int) method to obtain access to schema definition.
Definition KDbCursor.h:324
qint64 at() const
Definition KDbCursor.h:161
FetchResult m_fetchResult
result of a record fetching
Definition KDbCursor.h:316
virtual bool close()
Specialized string for escaping.
Meta-data for a field.
Definition KDbField.h:72
Type type() const
Definition KDbField.cpp:379
KDbQuerySchema provides information about database query.
Structure for storing single record with type information.
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
KDB_EXPORT QVariant cstringToVariant(const char *data, KDbField::Type type, bool *ok, int length=-1, KDb::Signedness signedness=KDb::Signed)
const_reference at(qsizetype i) const const
qsizetype count() const const
QString fromUtf8(QByteArrayView str)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 12 2024 12:05:27 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.