LCOV - code coverage report
Current view: top level - src/sl3 - dataset.cpp (source / functions) Coverage Total Hit
Test: coverage.info.cleaned Lines: 100.0 % 60 60
Test Date: 2026-03-17 13:39:52 Functions: 100.0 % 10 10
Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /******************************************************************************
       2                 :             :  ------------- Copyright (c) 2009-2023 H a r a l d  A c h i t z ---------------
       3                 :             :  ---------- < h a r a l d dot a c h i t z at g m a i l dot c o m > ------------
       4                 :             :  ---- This Source Code Form is subject to the terms of the Mozilla Public -----
       5                 :             :  ---- License, v. 2.0. If a copy of the MPL was not distributed with this -----
       6                 :             :  ---------- file, You can obtain one at http://mozilla.org/MPL/2.0/. ----------
       7                 :             :  ******************************************************************************/
       8                 :             : 
       9                 :             : #include <sl3/dataset.hpp>
      10                 :             : 
      11                 :             : #include <sqlite3.h>
      12                 :             : 
      13                 :             : #include <algorithm>
      14                 :             : #include <iterator>
      15                 :             : #include <sl3/error.hpp>
      16                 :             : #include <stdexcept>
      17                 :             : 
      18                 :             : #include "utils.hpp"
      19                 :             : 
      20                 :             : namespace sl3
      21                 :             : {
      22                 :           1 :   Dataset::Dataset () noexcept
      23                 :           1 :   : _fieldtypes ()
      24                 :           1 :   , _names ()
      25                 :             :   {
      26                 :           1 :   }
      27                 :          53 :   Dataset::Dataset (Types types)
      28                 :          53 :   : _fieldtypes (std::move (types))
      29                 :         106 :   , _names ()
      30                 :             :   {
      31                 :          53 :   }
      32                 :             : 
      33                 :           2 :   Dataset::Dataset (Dataset&& other) noexcept (
      34                 :             :       std::is_nothrow_move_constructible<Container<DbValues>>::value
      35                 :             :       && std::is_nothrow_move_constructible<Types>::value
      36                 :           2 :       && std::is_nothrow_move_constructible<std::vector<std::string>>::value)
      37                 :           2 :   : Container<std::vector<DbValues>> (std::move (other))
      38                 :           2 :   , _fieldtypes (std::move (other._fieldtypes))
      39                 :           4 :   , _names (std::move (other._names))
      40                 :             :   {
      41                 :           2 :   }
      42                 :             : 
      43                 :             :   void
      44                 :           2 :   Dataset::reset ()
      45                 :             :   {
      46                 :           2 :     _names.clear ();
      47                 :           2 :     _cont.clear ();
      48                 :           2 :   }
      49                 :             : 
      50                 :             :   void
      51                 :           1 :   Dataset::reset (const Types& types)
      52                 :             :   {
      53                 :           1 :     _fieldtypes = types;
      54                 :           1 :     reset ();
      55                 :           1 :   }
      56                 :             : 
      57                 :             :   void
      58                 :           8 :   Dataset::merge (const Dataset& other)
      59                 :             :   {
      60                 :           8 :     if (!other._names.empty ())
      61                 :             :       {
      62                 :           8 :         if (!_names.empty () && _names != other._names)
      63                 :           1 :           throw ErrTypeMisMatch ();
      64                 :             :       }
      65                 :             : 
      66                 :           7 :     if (_fieldtypes.size () != other._fieldtypes.size ())
      67                 :           1 :       throw ErrTypeMisMatch ();
      68                 :             : 
      69                 :          15 :     for (std::size_t i = 0; i < _fieldtypes.size (); ++i)
      70                 :             :       {
      71                 :          12 :         if (_fieldtypes[i] != Type::Variant)
      72                 :             :           {
      73                 :           6 :             if (_fieldtypes[i] != other._fieldtypes[i])
      74                 :           3 :               throw ErrTypeMisMatch ();
      75                 :             :           }
      76                 :             :       }
      77                 :             : 
      78                 :           3 :     _cont.insert (_cont.end (), other._cont.begin (), other._cont.end ());
      79                 :           3 :   }
      80                 :             : 
      81                 :             :   void
      82                 :           4 :   Dataset::merge (const DbValues& row)
      83                 :             :   {
      84                 :           4 :     if (_fieldtypes.size () > 0 && _fieldtypes.size () != row.size ())
      85                 :             :       {
      86                 :           1 :         throw ErrTypeMisMatch ();
      87                 :             :       }
      88                 :             : 
      89                 :           9 :     for (std::size_t i = 0; i < _fieldtypes.size (); ++i)
      90                 :             :       {
      91                 :           7 :         if (_fieldtypes[i] != Type::Variant)
      92                 :             :           {
      93                 :           4 :             if (_fieldtypes[i] != row[i].dbtype ())
      94                 :             :               {
      95                 :           1 :                 throw ErrTypeMisMatch ();
      96                 :             :               }
      97                 :             :           }
      98                 :             :       }
      99                 :             : 
     100                 :           2 :     _cont.push_back (DbValues (row));
     101                 :           2 :   }
     102                 :             : 
     103                 :             :   size_t
     104                 :          13 :   Dataset::getIndex (const std::string& name) const
     105                 :             :   {
     106                 :             :     using namespace std;
     107                 :          13 :     auto pos = find (_names.begin (), _names.end (), name);
     108                 :          13 :     if (pos == _names.end ())
     109                 :           1 :       throw ErrOutOfRange ("Field name " + name + " not found");
     110                 :             : 
     111                 :          36 :     return as_size_t (distance (_names.begin (), pos));
     112                 :             :   }
     113                 :             : 
     114                 :             :   void
     115                 :           3 :   Dataset::sort (const std::vector<size_t>& idxs, DbValueSort cmp)
     116                 :             :   {
     117                 :           5 :     ASSERT_EXCEPT (cmp, ErrNullValueAccess);
     118                 :             : 
     119                 :           8 :     auto lessValues = [&] (const DbValues& a, const DbValues& b) -> bool {
     120                 :          11 :       for (auto cur : idxs)
     121                 :             :         {
     122                 :           9 :           if (cmp (a.at (cur), b.at (cur)))
     123                 :           6 :             return true;
     124                 :           7 :           else if (cmp (b.at (cur), a.at (cur)))
     125                 :           4 :             return false;
     126                 :             :         }
     127                 :           2 :       return false;
     128                 :           2 :     };
     129                 :             : 
     130                 :           2 :     std::sort (begin (), end (), lessValues);
     131                 :           2 :   }
     132                 :             : }
        

Generated by: LCOV version 2.0-1