LCOV - code coverage report
Current view: top level - data/include/storage - data_database.inl (source / functions) Hit Total Coverage
Test: crystal-facet-uml_v1.42.0_alltests Lines: 52 62 83.9 %
Date: 2022-10-01 11:23:42 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /* File: data_database.inl; Copyright and License: see below */
       2             : 
       3             : #include "tslog/tslog.h"
       4             : #include <assert.h>
       5             : 
       6          10 : static inline u8_error_t data_database_open ( data_database_t *this_, const char* db_file_path )
       7             : {
       8          10 :     assert( db_file_path != NULL );
       9             :     /* there should not be pending transactions when calling open */
      10          10 :     assert( (*this_).transaction_recursion == 0 );
      11             : 
      12          10 :     const u8_error_t err
      13          10 :         = data_database_private_open( this_, db_file_path, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE );
      14          10 :     return err;
      15             : }
      16             : 
      17           0 : static inline u8_error_t data_database_open_read_only ( data_database_t *this_, const char* db_file_path )
      18             : {
      19           0 :     assert( db_file_path != NULL );
      20             :     /* there should not be pending transactions when calling open */
      21           0 :     assert( (*this_).transaction_recursion == 0 );
      22             : 
      23           0 :     const u8_error_t err
      24           0 :         = data_database_private_open( this_, db_file_path, SQLITE_OPEN_READONLY );
      25           0 :     return err;
      26             : }
      27             : 
      28          41 : static inline u8_error_t data_database_open_in_memory ( data_database_t *this_ )
      29             : {
      30             :     /* there should not be pending transactions when calling open */
      31          41 :     assert( (*this_).transaction_recursion == 0 );
      32             : 
      33          41 :     const char* const IN_MEMORY_FILENAME = ":memory:"; /* magic filename, see https://www.sqlite.org/c3ref/open.html */
      34          41 :     const u8_error_t err
      35          41 :         = data_database_private_open( this_,
      36             :                                       IN_MEMORY_FILENAME,
      37             :                                       SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY
      38             :                                     );
      39          41 :     return err;
      40             : }
      41             : 
      42      241393 : static inline sqlite3 *data_database_get_database_ptr ( data_database_t *this_ )
      43             : {
      44      241393 :     return (*this_).db;
      45             : }
      46             : 
      47       74688 : static inline data_change_notifier_t *data_database_get_notifier_ptr ( data_database_t *this_ )
      48             : {
      49       74688 :     return &((*this_).notifier);
      50             : }
      51             : 
      52          30 : static inline const char *data_database_get_filename_ptr ( data_database_t *this_ )
      53             : {
      54          30 :     return ( data_database_is_open( this_ ) ? utf8stringbuf_get_string( (*this_).db_file_name ) : NULL );
      55             : }
      56             : 
      57          54 : static inline void data_database_private_clear_db_listener_list( data_database_t *this_ )
      58             : {
      59        2310 :     for( int index = 0; index < DATA_DATABASE_MAX_LISTENERS; index ++ )
      60             :     {
      61        2200 :         (*this_).listener_list[index] = NULL;
      62             :     }
      63             : }
      64             : 
      65      225187 : static inline u8_error_t data_database_private_lock ( data_database_t *this_ )
      66             : {
      67      225187 :     u8_error_t result = U8_ERROR_NONE;
      68             : 
      69       75349 :     g_mutex_lock ( &((*this_).private_lock) );
      70             : 
      71       75803 :     return result;
      72             : }
      73             : 
      74      225187 : static inline u8_error_t data_database_private_unlock ( data_database_t *this_ )
      75             : {
      76      225187 :     u8_error_t result = U8_ERROR_NONE;
      77             : 
      78       75693 :     g_mutex_unlock ( &((*this_).private_lock) );
      79             : 
      80       75501 :     return result;
      81             : }
      82             : 
      83      224413 : static inline bool data_database_is_open( data_database_t *this_ )
      84             : {
      85      224413 :     bool result;
      86      224413 :     u8_error_t locking_error;
      87      224413 :     locking_error = data_database_private_lock( this_ );
      88      224413 :     result = (*this_).db_state != DATA_DATABASE_STATE_CLOSED;
      89      224413 :     locking_error |= data_database_private_unlock( this_ );
      90      224413 :     return result;
      91             : }
      92             : 
      93         816 : static inline u8_error_t data_database_private_exec_sql( data_database_t *this_, const char* sql_command, bool ignore_errors )
      94             : {
      95         816 :     assert( sql_command != NULL );
      96         816 :     int sqlite_err;
      97         816 :     char *error_msg = NULL;
      98         816 :     u8_error_t result = U8_ERROR_NONE;
      99         816 :     sqlite3 *const db = (*this_).db;
     100             : 
     101         816 :     TSLOG_EVENT_STR( "sqlite3_exec:", sql_command );
     102         816 :     sqlite_err = sqlite3_exec( db, sql_command, NULL, NULL, &error_msg );
     103             : 
     104         816 :     if ( SQLITE_READONLY == sqlite_err )
     105             :     {
     106           0 :         if ( ! ignore_errors )
     107             :         {
     108           0 :             TSLOG_WARNING_STR( "sqlite3_exec() failed:", sql_command );
     109             :         }
     110             :         result |=  U8_ERROR_READ_ONLY_DB;
     111             :     }
     112         816 :     else if ( SQLITE_OK != sqlite_err )
     113             :     {
     114         306 :         if ( ! ignore_errors )
     115             :         {
     116           0 :             TSLOG_ERROR_STR( "sqlite3_exec() failed:", sql_command );
     117           0 :             TSLOG_ERROR_INT( "sqlite3_exec() failed:", sqlite_err );
     118             :         }
     119             :         result |= U8_ERROR_AT_DB;
     120             :     }
     121         816 :     if ( error_msg != NULL )
     122             :     {
     123         306 :         TRACE_INFO_STR( "sqlite3_exec() failed:", error_msg );
     124         306 :         sqlite3_free( error_msg );
     125         306 :         error_msg = NULL;
     126             :     }
     127         816 :     return result;
     128             : }
     129             : 
     130             : 
     131             : /*
     132             : Copyright 2016-2022 Andreas Warnke
     133             : 
     134             : Licensed under the Apache License, Version 2.0 (the "License");
     135             : you may not use this file except in compliance with the License.
     136             : You may obtain a copy of the License at
     137             : 
     138             :     http://www.apache.org/licenses/LICENSE-2.0
     139             : 
     140             : Unless required by applicable law or agreed to in writing, software
     141             : distributed under the License is distributed on an "AS IS" BASIS,
     142             : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     143             : See the License for the specific language governing permissions and
     144             : limitations under the License.
     145             : */

Generated by: LCOV version 1.14