kexi

cursor.h

00001 /* This file is part of the KDE project
00002    Copyright (C) 2003-2006 Jaroslaw Staniek <js@iidea.pl>
00003 
00004    This program is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this program; see the file COPYING.  If not, write to
00016    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #ifndef KEXIDB_CURSOR_H
00021 #define KEXIDB_CURSOR_H
00022 
00023 #include <qstring.h>
00024 #include <qvariant.h>
00025 #include <qptrvector.h>
00026 #include <qvaluevector.h>
00027 
00028 #include <kexidb/connection.h>
00029 #include <kexidb/object.h>
00030 
00031 namespace KexiDB {
00032 
00033 class RowEditBuffer;
00034 
00036 
00066 class KEXI_DB_EXPORT Cursor: public QObject, public Object
00067 {
00068     Q_OBJECT
00069 
00070     public:
00072         enum Options {
00073             NoOptions = 0,
00074             Buffered = 1
00075         };
00076 
00077         virtual ~Cursor();
00078 
00080         inline Connection* connection() const { return m_conn; }
00081 
00084         bool open();
00085 
00089         bool reopen();
00090 
00091 //      /*! Opens the cursor using \a statement. 
00092 //       Omit \a statement if cursor is already initialized with statement 
00093 //       at creation time. If \a statement is not empty, existing statement
00094 //       (if any) is overwritten. */
00095 //      bool open( const QString& statement = QString::null );
00096 
00099         virtual bool close();
00100 
00103         inline QuerySchema *query() const { return m_query; }
00104 
00106         QValueList<QVariant> queryParameters() const;
00107 
00109         void setQueryParameters(const QValueList<QVariant>& params);
00110 
00113         inline QString rawStatement() const { return m_rawStatement; }
00114 
00117         inline uint options() const { return m_options; }
00118 
00120         inline bool isOpened() const { return m_opened; }
00121 
00123         bool isBuffered() const;
00124 
00130         void setBuffered(bool buffered);
00131 
00135         bool moveFirst();
00136 
00140         virtual bool moveLast();
00141 
00143         virtual bool moveNext();
00144 
00147         virtual bool movePrev();
00148 
00150         bool eof() const;
00151 
00153         bool bof() const;
00154 
00160         Q_LLONG at() const;
00161 
00164         inline uint fieldCount() const { return m_query ? m_logicalFieldCount : m_fieldCount; }
00165 
00172         inline bool containsROWIDInfo() const { return m_containsROWIDInfo; }
00173 
00180         virtual QVariant value(uint i) = 0;
00181 
00183         virtual const char ** rowData() const = 0;
00184 
00199         void setOrderByColumnList(const QStringList& columnNames);
00200 
00202         void setOrderByColumnList(const QString& column1, const QString& column2 = QString::null, 
00203             const QString& column3 = QString::null, const QString& column4 = QString::null, 
00204             const QString& column5 = QString::null);
00205 
00208         QueryColumnInfo::Vector orderByColumnList() const;
00209 
00215         virtual void storeCurrentRow(RowData &data) const = 0;
00216 
00217         bool updateRow(RowData& data, RowEditBuffer& buf, bool useROWID = false);
00218 
00219         bool insertRow(RowData& data, RowEditBuffer& buf, bool getROWID = false);
00220 
00221         bool deleteRow(RowData& data, bool useROWID = false);
00222 
00223         bool deleteAllRows();
00224 
00232         virtual int serverResult() { return 0; }
00233         
00239         virtual QString serverResultName() { return QString::null; }
00240 
00246         virtual QString serverErrorMsg() { return QString::null; }
00247     
00249         QString debugString() const;
00250         
00252         void debug() const;
00253 
00254     protected:
00256         typedef enum FetchResult { FetchError=0, FetchOK=1, FetchEnd=2 };
00257 
00259         Cursor(Connection* conn, const QString& statement, uint options = NoOptions );
00260 
00262         Cursor(Connection* conn, QuerySchema& query, uint options = NoOptions );
00263 
00264         void init();
00265 
00268         bool getNextRecord();
00269 
00270         /* Note for driver developers: this method should initialize engine-specific cursor's
00271          resources using m_sql statement. It is not required to store \a statement somewhere
00272          in your Cursor subclass (it is already stored in m_query or m_rawStatement, 
00273          depending query type) - only pass it to proper engine's function. */
00274         virtual bool drv_open() = 0;
00275 
00276         virtual bool drv_close() = 0;
00277 //      virtual bool drv_moveFirst() = 0;
00278         virtual void drv_getNextRecord() = 0;
00279 //unused        virtual bool drv_getPrevRecord() = 0;
00280 
00293         virtual void drv_appendCurrentRecordToBuffer() = 0;
00297         virtual void drv_bufferMovePointerNext() = 0;
00299         virtual void drv_bufferMovePointerPrev() = 0;
00302         virtual void drv_bufferMovePointerTo(Q_LLONG at) = 0;
00303 
00304         /*DISABLED: ! This is called only once in open(), after successful drv_open().
00305             Reimplement this if you need (or not) to do get the first record after drv_open(),
00306             eg. to know if there are any records in table. Value returned by this method
00307             will be assigned to m_readAhead.
00308             Default implementation just calls drv_getNextRecord(). */
00309 
00312         virtual void drv_clearBuffer() {}
00313 
00315         void clearBuffer();
00316 
00319         virtual void drv_clearServerResult() = 0;
00320 
00321         QGuardedPtr<Connection> m_conn;
00322         QuerySchema *m_query;
00323 //      CursorData *m_data;
00324         QString m_rawStatement;
00325         bool m_opened : 1;
00326 //js (m_at==0 is enough)        bool m_beforeFirst : 1;
00327         bool m_atLast : 1;
00328         bool m_afterLast : 1;
00329 //      bool m_atLast;
00330         bool m_validRecord : 1; 
00331         bool m_containsROWIDInfo : 1;
00332         Q_LLONG m_at;
00333         uint m_fieldCount; 
00334         uint m_logicalFieldCount;  
00335         uint m_options; 
00336         char m_result; 
00337         
00338         //<members related to buffering>
00339         int m_records_in_buf;          
00340         bool m_buffering_completed : 1;   
00341         //</members related to buffering>
00342 
00344         QueryColumnInfo::Vector* m_fieldsExpanded;
00345 
00347         QueryColumnInfo::Vector* m_orderByColumnList;
00348 
00349         QValueList<QVariant>* m_queryParameters;
00350 
00351     private:
00352         bool m_readAhead : 1;
00353         
00354         //<members related to buffering>
00355         bool m_at_buffer : 1;             
00356         //</members related to buffering>
00357 
00358 
00359         class Private;
00360         Private *d;
00361 };
00362 
00363 } //namespace KexiDB
00364 
00365 #endif
KDE Home | KDE Accessibility Home | Description of Access Keys