LCOV - code coverage report
Current view: top level - libgnucash/backend/sql - gnc-sql-result.hpp (source / functions) Coverage Total Hit
Test: gnucash.info Lines: 71.4 % 21 15
Test Date: 2026-11-02 18:19:43 Functions: 61.5 % 13 8
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /***********************************************************************\
       2              :  * gnc-sql-result.hpp: Encapsulate the results of a SQL query.         *
       3              :  *                                                                     *
       4              :  * Copyright 2016 John Ralls <jralls@ceridwen.us>                      *
       5              :  *                                                                     *
       6              :  * This program is free software; you can redistribute it and/or       *
       7              :  * modify it under the terms of the GNU General Public License as      *
       8              :  * published by the Free Software Foundation; either version 2 of      *
       9              :  * the License, or (at your option) any later version.                 *
      10              :  *                                                                     *
      11              :  * This program is distributed in the hope that it will be useful,     *
      12              :  * but WITHOUT ANY WARRANTY; without even the implied warranty of      *
      13              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the       *
      14              :  * GNU General Public License for more details.                        *
      15              :  *                                                                     *
      16              :  * You should have received a copy of the GNU General Public License   *
      17              :  * along with this program; if not, contact:                           *
      18              :  *                                                                     *
      19              :  * Free Software Foundation           Voice:  +1-617-542-5942          *
      20              :  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652          *
      21              :  * Boston, MA  02110-1301,  USA       gnu@gnu.org                      *
      22              : \***********************************************************************/
      23              : 
      24              : #ifndef __GNC_SQL_RESULT_HPP__
      25              : #define __GNC_SQL_RESULT_HPP__
      26              : 
      27              : #include <qof.h>
      28              : 
      29              : #include <cstdint>
      30              : #include <optional>
      31              : #include <string>
      32              : #include <vector>
      33              : 
      34              : class GncSqlRow;
      35              : /**
      36              :  * Pure virtual class to iterate over a query result set.
      37              :  */
      38              : 
      39              : class GncSqlResult
      40              : {
      41              : public:
      42            0 :     virtual ~GncSqlResult() = default;
      43              :     virtual uint64_t size() const noexcept = 0;
      44              :     virtual GncSqlRow& begin() = 0;
      45              :     virtual GncSqlRow& end() = 0;
      46              :     friend GncSqlRow;
      47              : protected:
      48              :     class IteratorImpl {
      49              :     public:
      50            0 :         virtual ~IteratorImpl() = default;
      51              :         virtual GncSqlRow& operator++() = 0;
      52              :         virtual GncSqlResult* operator*() = 0;
      53              :         virtual std::optional<int64_t> get_int_at_col (const char* col) const = 0;
      54              :         virtual std::optional<double> get_float_at_col (const char* col) const = 0;
      55              :         virtual std::optional<double> get_double_at_col (const char* col) const = 0;
      56              :         virtual std::optional<std::string> get_string_at_col (const char* col) const = 0;
      57              :         virtual std::optional<time64> get_time64_at_col (const char* col) const = 0;
      58              :         virtual bool is_col_null (const char* col) const noexcept = 0;
      59              :     };
      60              : };
      61              : 
      62              : /**
      63              :  * Row of SQL Query results.
      64              :  *
      65              :  * This is a "pointer" class of a pimpl pattern, the implementation being
      66              :  * GncSqlResult::IteratorImpl. It's designed to present a std::forward_iterator
      67              :  * like interface for use with range-for while allowing for wrapping a C API.
      68              :  *
      69              :  * Important Implementation Note: Operator++() as written requires that the
      70              :  * sentinel GncSqlRow returned by GncSqlResult::end() has m_impl = nullptr in
      71              :  * order to terminate the loop condition. This is a bit of a hack and might be a
      72              :  * problem with a different SQL interface library from libdbi.
      73              :  *
      74              :  * Note that GncSqlResult::begin and GncSqlRow::operator++() return
      75              :  * GncSqlRow&. This is necessary for operator++() to be called: Using operator
      76              :  * ++() on a pointer performs pointer arithmetic rather than calling the
      77              :  * pointed-to-class's operator++() and C++'s range-for uses operator++()
      78              :  * directly on whatever begin() gives it.
      79              :  */
      80              : class GncSqlRow
      81              : {
      82              : public:
      83            0 :     GncSqlRow (GncSqlResult::IteratorImpl* iter) : m_iter{iter} {}
      84            0 :     ~GncSqlRow() { }
      85              :     GncSqlRow& operator++();
      86          359 :     GncSqlRow& operator*() { return *this; }
      87              :     friend bool operator!=(const GncSqlRow&, const GncSqlRow&);
      88          978 :     std::optional<int64_t> get_int_at_col (const char* col) const {
      89          978 :         return m_iter->get_int_at_col (col); }
      90           74 :     std::optional<double> get_float_at_col (const char* col) const {
      91           74 :         return m_iter->get_float_at_col (col); }
      92           74 :     std::optional<double> get_double_at_col (const char* col) const {
      93           74 :         return m_iter->get_double_at_col (col); }
      94         1471 :     std::optional<std::string> get_string_at_col (const char* col) const {
      95         1471 :         return m_iter->get_string_at_col (col); }
      96            0 :     std::optional<time64> get_time64_at_col (const char* col) const {
      97            0 :         return m_iter->get_time64_at_col (col); }
      98           87 :     bool is_col_null (const char* col) const noexcept {
      99           87 :         return m_iter->is_col_null (col); }
     100              : private:
     101              :     GncSqlResult::IteratorImpl* m_iter;
     102              : };
     103              : 
     104          984 : inline bool operator!=(const GncSqlRow& lr, const GncSqlRow& rr) {
     105          984 :     return lr.m_iter != rr.m_iter;
     106              : }
     107              : 
     108           34 : inline bool operator==(const GncSqlRow& lr, const GncSqlRow& rr) {
     109           34 :     return !(lr != rr);
     110              : }
     111              : 
     112              : 
     113              : #endif //__GNC_SQL_RESULT_HPP__
        

Generated by: LCOV version 2.0-1