KDb

MysqlCursor.cpp
1 /* This file is part of the KDE project
2  Copyright (C) 2003 Joseph Wenninger<[email protected]>
3  Copyright (C) 2005-2016 JarosÅ‚aw Staniek <[email protected]>
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 
32 MysqlCursor::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 
39 MysqlCursor::MysqlCursor(KDbConnection* conn, KDbQuerySchema* query, KDbCursor::Options options)
40  : KDbCursor(conn, query, options | KDbCursor::Option::Buffered)
41  , d(new MysqlCursorData(conn))
42 {
43 }
44 
45 MysqlCursor::~MysqlCursor()
46 {
47  close();
48  delete d;
49 }
50 
51 bool 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);
59  m_fieldsToStoreInRecord = m_fieldCount;
60  d->numRows = mysql_num_rows(d->mysqlres);
61 
62  m_records_in_buf = d->numRows;
63  m_buffering_completed = true;
64  return true;
65  }
66  }
67 
68  storeResult();
69  return false;
70 }
71 
72 bool 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 
82 void MysqlCursor::drv_getNextRecord()
83 {
84  if (at() >= d->numRows) {
85  m_fetchResult = FetchResult::End;
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"
90  m_fetchResult = FetchResult::End;
91  }
92  else { // 0 <= at() < d->numRows
93  d->lengths = mysql_fetch_lengths(d->mysqlres);
94  m_fetchResult = FetchResult::Ok;
95  }
96 }
97 
98 // This isn't going to work right now as it uses d->mysqlrow
99 QVariant MysqlCursor::value(int pos)
100 {
101  if (!d->mysqlrow || pos >= m_fieldCount || d->mysqlrow[pos] == nullptr)
102  return QVariant();
103 
104  KDbField *f = (m_visibleFieldsExpanded && pos < m_visibleFieldsExpanded->count())
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  */
117 bool 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 
141 void MysqlCursor::drv_appendCurrentRecordToBuffer()
142 {
143 }
144 
145 
146 void MysqlCursor::drv_bufferMovePointerNext()
147 {
148  d->mysqlrow = mysql_fetch_row(d->mysqlres);
149  d->lengths = mysql_fetch_lengths(d->mysqlres);
150 }
151 
152 void 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 
160 void 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 
167 const char** MysqlCursor::recordData() const
168 {
169  //! @todo
170  return nullptr;
171 }
172 
173 QString MysqlCursor::serverResultName() const
174 {
175  return MysqlConnectionInternal::serverResultName(d->mysql);
176 }
177 
178 void MysqlCursor::storeResult()
179 {
180  d->storeResult(&m_result);
181 }
Provides database cursor functionality.
Definition: KDbCursor.h:68
QString fromUtf8(const char *str, int size)
KDB_EXPORT QVariant cstringToVariant(const char *data, KDbField::Type type, bool *ok, int length=-1, KDb::Signedness signedness=KDb::Signed)
Definition: KDb.cpp:1984
@ Text
Definition: KDbField.h:98
Specialized string for escaping.
const QList< QKeySequence > & close()
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
Type type() const
Definition: KDbField.cpp:379
Structure for storing single record with type information.
Definition: KDbRecordData.h:36
KDbQuerySchema provides information about database query.
Meta-data for a field.
Definition: KDbField.h:71
Provides database connection, allowing queries and data modification.
Definition: KDbConnection.h:51
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon May 8 2023 04:07:51 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.