File indexing completed on 2024-04-21 15:29:53

0001 /* This file is part of the KDE project
0002    Copyright (C) 2008 Sharan Rao <sharanrao@gmail.com>
0003 
0004    This program is free software; you can redistribute it and/or
0005    modify it under the terms of the GNU Library General Public
0006    License as published by the Free Software Foundation; either
0007    version 2 of the License, or (at your option) any later version.
0008 
0009    This program is distributed in the hope that it will be useful,
0010    but WITHOUT ANY WARRANTY; without even the implied warranty of
0011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012    Library General Public License for more details.
0013 
0014    You should have received a copy of the GNU Library General Public License
0015    along with this program; see the file COPYING.  If not, write to
0016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018 */
0019 
0020 #include "XbaseCursor.h"
0021 #include "XbaseConnection.h"
0022 
0023 #include "KDbError.h"
0024 #include "KDb.h"
0025 
0026 #include <limits.h>
0027 
0028 class KDbxBaseCursorData {
0029   public:
0030     explicit xBaseCursorData(KDbCursor* cursor = nullptr)
0031       : internalCursor(cursor)
0032     {
0033     }
0034 
0035     KDbCursor* internalCursor;
0036 
0037 };
0038 
0039 xBaseCursor::xBaseCursor(KDbConnection* conn, KDbCursor* internalCursor, const KDbEscapedString& sql, int cursor_options)
0040   : KDbCursor(conn,sql,cursor_options)
0041   , d( new xBaseCursorData(internalCursor) )
0042 {
0043   init();
0044 }
0045 
0046 xBaseCursor::xBaseCursor(KDbConnection* conn, KDbCursor* internalCursor, KDbQuerySchema* query, int options)
0047   : KDbCursor( conn, query, options )
0048   , d( new xBaseCursorData(internalCursor) )
0049 {
0050   init();
0051 }
0052 
0053 xBaseCursor::~xBaseCursor() {
0054   close();
0055 }
0056 
0057 void xBaseCursor::init() {
0058 
0059   if (d->internalCursor) {
0060     m_options |= d->internalCursor->options();
0061   }
0062   // SQLite does buffering. So all calls to moving the cursor to SQLite will read from the buffer
0063   // ( if the rows are already buffered ) inside. Any point in double buffering ?
0064   setBuffered(false);
0065 }
0066 
0067 bool xBaseCursor::drv_open(const KDbEscapedString& sql)
0068 {
0069 // xbaseDebug() << m_sql;
0070   if (!d->internalCursor) {
0071     return false;
0072   }
0073   return d->internalCursor->open();
0074 }
0075 
0076 bool xBaseCursor::drv_close() {
0077   if (!d->internalCursor) {
0078     return false;
0079   }
0080   m_opened = false;
0081   KDbConnection* internalConn = d->internalCursor->connection();
0082   internalConn->deleteCursor( d->internalCursor );
0083   return true;
0084 }
0085 
0086 void xBaseCursor::drv_getNextRecord() {
0087   if (!d->internalCursor) {
0088     m_fetchResult = FetchResult::Error;
0089     return;
0090   }
0091 
0092   if ( !d->internalCursor->moveNext() ) {
0093     if ( d->internalCursor->eof() )
0094       m_fetchResult = FetchResult::End;
0095     else
0096       m_fetchResult = FetchResult::Error;
0097   } else {
0098     m_fetchResult = FetchResult::Ok;
0099     m_fieldCount = d->internalCursor->fieldCount();
0100     m_fieldsToStoreInRecord = m_fieldCount;
0101   }
0102 }
0103 
0104 QVariant xBaseCursor::value(int pos) {
0105   if (!d->internalCursor) {
0106     // Construct an invalid QVariant
0107     return QVariant();
0108   }
0109   return d->internalCursor->value(pos);
0110 }
0111 
0112 
0113 bool xBaseCursor::drv_storeCurrentRecord(KDbRecordData* data) const
0114 {
0115   if (!d->internalCursor) {
0116     return false;
0117   }
0118 
0119   KDbRecordData* rData = d->internalCursor->storeCurrentRecord();
0120   if (!rData) {
0121     return false;
0122   }
0123   *data = *rData;
0124   return true;
0125 }
0126 
0127 void xBaseCursor::drv_appendCurrentRecordToBuffer() {
0128 }
0129 
0130 
0131 void xBaseCursor::drv_bufferMovePointerNext() {
0132 }
0133 
0134 void xBaseCursor::drv_bufferMovePointerPrev() {
0135 }
0136 
0137 
0138 void xBaseCursor::drv_bufferMovePointerTo(qint64 to) {
0139   Q_UNUSED(to);
0140 }
0141 
0142 const char** xBaseCursor::recordData() const {
0143   if (!d->internalCursor) {
0144     return nullptr;
0145   }
0146   return d->internalCursor->recordData();
0147 }
0148 
0149 int xBaseCursor::serverResult()
0150 {
0151   if (!d->internalCursor) {
0152     // Any better value to return ?
0153     return -1;
0154   }
0155   return d->internalCursor->serverResult();
0156 }
0157 
0158 QString xBaseCursor::serverResultName() const
0159 {
0160   if (!d->internalCursor) {
0161     return QString();
0162   }
0163   return d->internalCursor->serverResultName();
0164 }
0165 
0166 //! @todo xBaseCursor::drv_clearServerResult()
0167 /*void xBaseCursor::drv_clearServerResult()
0168 {
0169 }*/
0170 
0171 QString xBaseCursor::serverErrorMsg()
0172 {
0173   if (!d->internalCursor) {
0174     return QString();
0175   }
0176   return d->internalCursor->serverErrorMsg();
0177 }