KDb

SybaseCursor.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2007 Sharan Rao <sharanrao@gmail.com>
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20
21#include "SybaseCursor.h"
22#include "SybaseConnection.h"
23#include "SybaseConnection_p.h"
24
25
26#include "KDbError.h"
27#include "KDbUtils.h"
28
29#include <limits.h>
30#include <cstring>
31
32#include <sqldb.h>
33
34SybaseCursor::SybaseCursor(KDbConnection* conn, const KDbEscapedString& sql, int cursor_options)
35 : KDbCursor(conn, sql, cursor_options)
36 , d(new SybaseCursorData(conn))
37{
38
39 //m_options |= Buffered;
40
41 d->dbProcess = static_cast<SybaseConnection*>(conn)->d->dbProcess;
42// sybaseDebug() << "SybaseCursor: constructor for query statement";
43}
44
45SybaseCursor::SybaseCursor(KDbConnection* conn, KDbQuerySchema* query, int options)
46 : KDbCursor(conn, query, options)
47 , d(new SybaseCursorData(conn))
48{
49 // m_options |= Buffered;
50
51 d->dbProcess = static_cast<SybaseConnection*>(conn)->d->dbProcess;
52// sybaseDebug() << "SybaseCursor: constructor for query statement";
53}
54
55SybaseCursor::~SybaseCursor()
56{
57 close();
58}
59
60bool SybaseCursor::drv_open(const KDbEscapedString& sql)
61{
62
63 /* Pseudo Code
64 *
65 * Execute Query
66 * If no error
67 * Store Result in buffer ( d-> ?? )
68 * Store fieldcount ( no. of columns ) in m_fieldCount
69 * Set m_fieldsToStoreInRecord equal to m_fieldCount
70 * Store number of rows in d->numRows
71 * Set pointer at 0 ( m_at = 0 )
72 *
73 * Set opened flag as true ( m_opened = true )
74 * Set numberoOfRecordsInbuffer as d->numRows ( m_records_in_buf = d->numRows )
75 * Set Buffering Complete flag = true
76 * Set After Last flag = false
77 *
78 */
79
80 // clear all previous results ( if remaining )
81 if (dbcancel(d->dbProcess) == FAIL)
82 sybaseWarning() << "drv_open" << "dead DBPROCESS ?";
83
84 // insert into command buffer
85 dbcmd(d->dbProcess, sql.toByteArray());
86 // execute query
87 dbsqlexec(d->dbProcess);
88
89 if (dbresults(d->dbProcess) == SUCCEED) {
90 // result set goes directly into dbProcess' buffer
91 m_fieldCount = dbnumcols(d->dbProcess);
93
94 // only relevant if buffering will ever work
95 // <ignore>
96 d->numRows = DBLASTROW(d->dbProcess); // only true if buffering enabled
97 m_records_in_buf = d->numRows;
99 // </ignore>
100
101 m_afterLast = false;
102 m_opened = true;
103 m_at = 0;
104
105 return true;
106 }
107
108 setError(ERR_DB_SPECIFIC, static_cast<SybaseConnection*>(connection())->d->errmsg);
109 return false;
110}
111
112
113bool SybaseCursor::drv_close()
114{
115
116 m_opened = false;
117 d->numRows = 0;
118 return true;
119}
120
121//! @todo SybaseCursor::drv_moveFirst()
122/*bool SybaseCursor::drv_moveFirst() {
123 return true;
124}*/
125
126void SybaseCursor::drv_getNextRecord()
127{
128// sybaseDebug();
129
130 // no buffering , and we don't know how many rows are there in result set
131
132 if (dbnextrow(d->dbProcess) != NO_MORE_ROWS)
134 else {
136 }
137
138}
139
140
141QVariant SybaseCursor::value(int pos)
142{
143 if (!d->dbProcess || pos >= m_fieldCount)
144 return QVariant();
145
147 ? m_visibleFieldsExpanded->at(pos)->field : 0;
148
149 // db-library indexes its columns from 1
150 pos = pos + 1;
151
152 long int columnDataLength = dbdatlen(d->dbProcess, pos);
153
154 // 512 is
155 // 1. the length used internally in dblib for allocating data to each column in function dbprrow()
156 // 2. it's greater than all the values returned in the dblib internal function _get_printable_size
157 long int pointerLength = qMax(columnDataLength , (long int)512);
158
159 BYTE* columnValue = new unsigned char[pointerLength + 1] ;
160
161 // convert to string representation. All values are convertible to string
162 dbconvert(d->dbProcess , dbcoltype(d->dbProcess , pos), dbdata(d->dbProcess , pos), columnDataLength , (SYBCHAR), columnValue, -2);
163
164 QVariant returnValue = KDbcstringToVariant((const char*)columnValue , f, strlen((const char*)columnValue));
165
166 delete[] columnValue;
167
168 return returnValue;
169}
170
171
172/* As with sqlite, the DB library returns all values (including numbers) as
173 strings. So just put that string in a QVariant and let KDb deal with it.
174 */
175bool SybaseCursor::drv_storeCurrentRecord(KDbRecordData* data) const
176{
177// sybaseDebug() << "Position is" << (long)m_at;
178// if (d->numRows<=0)
179// return false;
180
181 const int fieldsExpandedCount = m_visibleFieldsExpanded
182 ? m_visibleFieldsExpanded->count() : INT_MAX;
183 const int realCount = qMin(fieldsExpandedCount, m_fieldsToStoreInRecord);
184 for (int i = 0; i < realCount; i++) {
186 if (m_visibleFieldsExpanded && !f)
187 continue;
188
189 long int columnDataLength = dbdatlen(d->dbProcess, i + 1);
190
191 // 512 is
192 // 1. the length used internally in dblib for allocating data to each column in function dbprrow()
193 // 2. it's greater than all the values returned in the dblib internal function _get_printable_size
194 long int pointerLength = qMax(columnDataLength , (long int)512);
195
196 BYTE* columnValue = new unsigned char[pointerLength + 1] ;
197
198 // convert to string representation. All values are convertible to string
199 dbconvert(d->dbProcess , dbcoltype(d->dbProcess , i + 1), dbdata(d->dbProcess , i + 1), columnDataLength , (SYBCHAR), columnValue, -2);
200
201 (*data)[i] = KDbcstringToVariant((const char*)columnValue , f, strlen((const char*)columnValue));
202
203 delete[] columnValue;
204 }
205 return true;
206}
207
208void SybaseCursor::drv_appendCurrentRecordToBuffer()
209{
210}
211
212
213void SybaseCursor::drv_bufferMovePointerNext()
214{
215 //dbgetrow( d->dbProcess, m_at + 1 );
216}
217
218void SybaseCursor::drv_bufferMovePointerPrev()
219{
220 //dbgetrow( d->dbProcess, m_at - 1 );
221}
222
223
224void SybaseCursor::drv_bufferMovePointerTo(qint64 to)
225{
226 //dbgetrow( d->dbProcess, to );
227}
228
229const char** SybaseCursor::recordData() const
230{
231 //! @todo
232 return 0;
233}
234
235int SybaseCursor::serverResult()
236{
237 return d->res;
238}
239
240QString SybaseCursor::serverResultName() const
241{
242 return QString();
243}
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
FetchResult m_fetchResult
result of a record fetching
Definition KDbCursor.h:316
KDbConnection * connection()
virtual bool close()
Specialized string for escaping.
Meta-data for a field.
Definition KDbField.h:72
KDbQuerySchema provides information about database query.
Structure for storing single record with type information.
Provides database connection, allowing queries and data modification.
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
const_reference at(qsizetype i) const const
qsizetype count() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:20:59 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.