LCOV - code coverage report
Current view: top level - data/source/storage - data_database_reader.c (source / functions) Hit Total Coverage
Test: crystal-facet-uml_v1.57.0_covts Lines: 56 63 88.9 %
Date: 2024-04-07 11:14:42 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /* File: data_database_reader.c; Copyright and License: see below */
       2             : 
       3             : #include "storage/data_database_reader.h"
       4             : #include "u8/u8_trace.h"
       5             : #include "u8/u8_log.h"
       6             : #include "utf8stringbuf/utf8stringbuf.h"
       7             : #include <sqlite3.h>
       8             : #include <assert.h>
       9             : 
      10          91 : u8_error_t data_database_reader_init ( data_database_reader_t *this_, data_database_t *database )
      11             : {
      12          91 :     U8_TRACE_BEGIN();
      13          91 :     assert( NULL != database );
      14          91 :     u8_error_t result = U8_ERROR_NONE;
      15             : 
      16          91 :     (*this_).database = database;
      17          91 :     (*this_).is_open = false;
      18             : 
      19          91 :     data_database_listener_init ( &((*this_).me_as_listener),
      20             :                                   this_,
      21             :                                   (void (*)(void*,data_database_listener_signal_t)) &data_database_reader_db_change_callback
      22             :                                 );
      23          91 :     data_database_add_db_listener( database, &((*this_).me_as_listener) );
      24             : 
      25          91 :     if ( data_database_is_open( database ) )
      26             :     {
      27             :         /* if the database is open, open also the reader */
      28          88 :         result |= data_database_reader_private_open( this_ );
      29             :     }
      30             : 
      31          91 :     U8_TRACE_END_ERR(result);
      32          91 :     return result;
      33             : }
      34             : 
      35          91 : u8_error_t data_database_reader_destroy ( data_database_reader_t *this_ )
      36             : {
      37          91 :     U8_TRACE_BEGIN();
      38          91 :     u8_error_t result = U8_ERROR_NONE;
      39             : 
      40          91 :     if ( (*this_).is_open )
      41             :     {
      42          90 :         result |= data_database_reader_private_close( this_ );
      43             :     }
      44             : 
      45          91 :     data_database_remove_db_listener( (*this_).database, &((*this_).me_as_listener) );
      46             : 
      47          91 :     (*this_).database = NULL;
      48             : 
      49          91 :     U8_TRACE_END_ERR(result);
      50          91 :     return result;
      51             : }
      52             : 
      53           6 : void data_database_reader_db_change_callback ( data_database_reader_t *this_, data_database_listener_signal_t signal_id )
      54             : {
      55           6 :     U8_TRACE_BEGIN();
      56           6 :     u8_error_t result = U8_ERROR_NONE;
      57             : 
      58           6 :     switch ( signal_id )
      59             :     {
      60           2 :         case DATA_DATABASE_LISTENER_SIGNAL_PREPARE_CLOSE:
      61             :         {
      62           2 :             U8_TRACE_INFO( "DATA_DATABASE_LISTENER_SIGNAL_PREPARE_CLOSE" );
      63           2 :             if ( (*this_).is_open )
      64             :             {
      65           2 :                 result |= data_database_reader_private_close( this_ );
      66             :             }
      67             :         }
      68           2 :         break;
      69             : 
      70           4 :         case DATA_DATABASE_LISTENER_SIGNAL_DB_OPENED:
      71             :         {
      72           4 :             U8_TRACE_INFO( "DATA_DATABASE_LISTENER_SIGNAL_DB_OPENED" );
      73           4 :             if ( (*this_).is_open )
      74             :             {
      75           0 :                 result |= data_database_reader_private_close( this_ );
      76             :             }
      77           4 :             result |= data_database_reader_private_open( this_ );
      78             :         }
      79           4 :         break;
      80             : 
      81           0 :         default:
      82             :         {
      83           0 :             U8_LOG_ERROR( "unexpected data_database_listener_signal_t" );
      84             :         }
      85             :     }
      86             : 
      87           6 :     U8_TRACE_END();
      88           6 : }
      89             : 
      90             : /* ================================ private ================================ */
      91             : 
      92          92 : u8_error_t data_database_reader_private_open ( data_database_reader_t *this_ )
      93             : {
      94          92 :     U8_TRACE_BEGIN();
      95          92 :     u8_error_t result = U8_ERROR_NONE;
      96             : 
      97          92 :     if ( ! (*this_).is_open )
      98             :     {
      99          92 :         result |= data_database_classifier_reader_init( &((*this_).temp_classifier_reader), (*this_).database );
     100          92 :         result |= data_database_diagram_reader_init( &((*this_).temp_diagram_reader), (*this_).database );
     101             : 
     102          92 :         if ( result == U8_ERROR_NONE )
     103             :         {
     104          92 :             (*this_).is_open = true;
     105             :         }
     106             :     }
     107             :     else
     108             :     {
     109           0 :         result |= U8_ERROR_INVALID_REQUEST;
     110           0 :         U8_LOG_WARNING( "Database is already open." );
     111             :     }
     112             : 
     113          92 :     U8_TRACE_END_ERR(result);
     114          92 :     return result;
     115             : }
     116             : 
     117          92 : u8_error_t data_database_reader_private_close ( data_database_reader_t *this_ )
     118             : {
     119          92 :     U8_TRACE_BEGIN();
     120          92 :     u8_error_t result = U8_ERROR_NONE;
     121             : 
     122          92 :     if ( (*this_).is_open )
     123             :     {
     124          92 :         result |= data_database_diagram_reader_destroy( &((*this_).temp_diagram_reader) );
     125          92 :         result |= data_database_classifier_reader_destroy( &((*this_).temp_classifier_reader) );
     126             : 
     127          92 :         (*this_).is_open = false;
     128             :     }
     129             :     else
     130             :     {
     131           0 :         result |= U8_ERROR_INVALID_REQUEST;
     132           0 :         U8_LOG_WARNING( "Database was not open." );
     133             :     }
     134             : 
     135          92 :     U8_TRACE_END_ERR(result);
     136          92 :     return result;
     137             : }
     138             : 
     139             : 
     140             : /*
     141             : Copyright 2016-2024 Andreas Warnke
     142             : 
     143             : Licensed under the Apache License, Version 2.0 (the "License");
     144             : you may not use this file except in compliance with the License.
     145             : You may obtain a copy of the License at
     146             : 
     147             :     http://www.apache.org/licenses/LICENSE-2.0
     148             : 
     149             : Unless required by applicable law or agreed to in writing, software
     150             : distributed under the License is distributed on an "AS IS" BASIS,
     151             : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     152             : See the License for the specific language governing permissions and
     153             : limitations under the License.
     154             : */

Generated by: LCOV version 1.16