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

0001 /* This file is part of the KDE project
0002    Copyright (C) 2016 Jarosław Staniek <staniek@kde.org>
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 #ifndef KDB_SQLRESULT_H
0021 #define KDB_SQLRESULT_H
0022 
0023 #include "kdb_export.h"
0024 #include <QSharedPointer>
0025 #include <QString>
0026 
0027 class KDbConnection;
0028 class KDbField;
0029 class KDbRecordData;
0030 class KDbResult;
0031 class KDbSqlField;
0032 class KDbSqlRecord;
0033 
0034 /**
0035  * The KDbSqlResult class abstracts result of a raw SQL query preparation by KDbConnection::prepareSql()
0036  *
0037  * The KDbSqlResult object provides low-level access to information about fields of the result and
0038  * can fetch records by actual execution of the prepared query.
0039  *
0040  * @note the KDbSqlResult object should be deleted before closing the database connection that
0041  * created it. This is needed because the connection is used by the object to retrieve data or to
0042  * obtain status information.
0043  */
0044 class KDB_EXPORT KDbSqlResult
0045 {
0046 public:
0047     KDbSqlResult();
0048 
0049     virtual ~KDbSqlResult();
0050 
0051     //! @return connection for this result
0052     virtual KDbConnection *connection() const { return nullptr; }
0053 
0054     //! @return number of fields in this result
0055     virtual int fieldsCount() = 0;
0056 
0057     //! @return field @a index from this result
0058     virtual KDbSqlField *field(int index) /*Q_REQUIRED_RESULT*/ = 0;
0059 
0060     //! Creates a KDb field for field @a index and returns it
0061     //! On failure returns @c nullptr.
0062     //! @a tableName is the table name and may be used to retrieve information but may
0063     //! be ignored as well if the KDbSqlResult already has field metadata available.
0064     virtual KDbField* createField(const QString &tableName, int index) /*Q_REQUIRED_RESULT*/ = 0;
0065 
0066     /**
0067      * Fetches one record.
0068      *
0069      * @return a shared pointer to the record or a null pointer if there is no record to fetch or
0070      * on error.
0071      * Check lastResult() for detailed result. Ownership is transferred to the caller.
0072      */
0073     virtual QSharedPointer<KDbSqlRecord> fetchRecord() /*Q_REQUIRED_RESULT*/ = 0;
0074 
0075     //! Convenience method. Fetches one record and all values into @a data.
0076     //! @return record data object and passes its ownership
0077     //! @c nullptr is returned on error or when there is no record to fetch.
0078     //! Check lastResult() for errors.
0079     Q_REQUIRED_RESULT KDbRecordData *fetchRecordData();
0080 
0081     //! @return result of last operation on this SQL result
0082     virtual KDbResult lastResult() = 0;
0083 
0084     /*! @return unique identifier of the most recently inserted record.
0085      Typically this is just primary key value. This identifier could be reused when we want
0086      to reference just inserted record. If there was no insertion recently performed for
0087      the result, std::numeric_limits<quint64>::max() is returned. */
0088     virtual quint64 lastInsertRecordId() { return std::numeric_limits<quint64>::max(); }
0089 private:
0090     Q_DISABLE_COPY(KDbSqlResult)
0091 };
0092 
0093 #endif