LCOV - code coverage report
Current view: top level - data/include/storage - data_database.inl (source / functions) Hit Total Coverage
Test: crystal-facet-uml_v1.57.0_covts Lines: 70 87 80.5 %
Date: 2024-04-07 11:14:42 Functions: 12 13 92.3 %

          Line data    Source code
       1             : /* File: data_database.inl; Copyright and License: see below */
       2             : 
       3             : #include "u8/u8_log.h"
       4             : #include <assert.h>
       5             : 
       6             : /* ================================ Lifecycle ================================ */
       7             : 
       8          10 : static inline u8_error_t data_database_open ( data_database_t *this_, const char* db_file_path )
       9             : {
      10          10 :     assert( db_file_path != NULL );
      11             :     /* there should not be pending transactions when calling open */
      12          10 :     assert( (*this_).transaction_recursion == 0 );
      13             : 
      14             :     const u8_error_t err
      15          10 :         = data_database_private_open( this_, db_file_path, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE );
      16          10 :     return err;
      17             : }
      18             : 
      19           0 : static inline u8_error_t data_database_open_read_only ( data_database_t *this_, const char* db_file_path )
      20             : {
      21           0 :     assert( db_file_path != NULL );
      22             :     /* there should not be pending transactions when calling open */
      23           0 :     assert( (*this_).transaction_recursion == 0 );
      24             : 
      25             :     const u8_error_t err
      26           0 :         = data_database_private_open( this_, db_file_path, SQLITE_OPEN_READONLY );
      27           0 :     return err;
      28             : }
      29             : 
      30          46 : static inline u8_error_t data_database_open_in_memory ( data_database_t *this_ )
      31             : {
      32             :     /* there should not be pending transactions when calling open */
      33          46 :     assert( (*this_).transaction_recursion == 0 );
      34             : 
      35          46 :     const char* const IN_MEMORY_FILENAME = ":memory:"; /* magic filename, see https://www.sqlite.org/c3ref/open.html */
      36             :     const u8_error_t err
      37          46 :         = data_database_private_open( this_,
      38             :                                       IN_MEMORY_FILENAME,
      39             :                                       SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY
      40             :                                     );
      41          46 :     return err;
      42             : }
      43             : 
      44        2887 : static inline bool data_database_is_open( data_database_t *this_ )
      45             : {
      46             :     bool result;
      47             :     u8_error_t locking_error;
      48        2887 :     locking_error = data_database_lock_on_write( this_ );
      49        2887 :     result = (*this_).db_state != DATA_DATABASE_STATE_CLOSED;
      50        2887 :     locking_error |= data_database_unlock_on_write( this_ );
      51        2887 :     return result;
      52             : }
      53             : 
      54             : /* ================================ Actions on DB ================================ */
      55             : 
      56        1839 : static inline sqlite3 *data_database_get_database_ptr ( data_database_t *this_ )
      57             : {
      58        1839 :     return (*this_).db;
      59             : }
      60             : 
      61        1064 : static inline u8_error_t data_database_private_exec_sql( data_database_t *this_, const char* sql_command, bool ignore_errors )
      62             : {
      63        1064 :     assert( sql_command != NULL );
      64             :     int sqlite_err;
      65        1064 :     char *error_msg = NULL;
      66        1064 :     u8_error_t result = U8_ERROR_NONE;
      67        1064 :     sqlite3 *const db = (*this_).db;
      68             : 
      69        1064 :     U8_LOG_EVENT_STR( "sqlite3_exec:", sql_command );
      70        1064 :     sqlite_err = sqlite3_exec( db, sql_command, NULL, NULL, &error_msg );
      71             : 
      72        1064 :     if ( SQLITE_READONLY == sqlite_err )
      73             :     {
      74           0 :         if ( ! ignore_errors )
      75             :         {
      76           0 :             U8_LOG_WARNING_STR( "sqlite3_exec() failed:", sql_command );
      77             :         }
      78           0 :         result |=  U8_ERROR_READ_ONLY_DB;
      79             :     }
      80        1064 :     else if ( SQLITE_OK != sqlite_err )
      81             :     {
      82         448 :         if ( ! ignore_errors )
      83             :         {
      84           0 :             U8_LOG_ERROR_STR( "sqlite3_exec() failed:", sql_command );
      85           0 :             U8_LOG_ERROR_INT( "sqlite3_exec() failed:", sqlite_err );
      86             :         }
      87         448 :         result |= U8_ERROR_AT_DB;
      88             :     }
      89        1064 :     if ( error_msg != NULL )
      90             :     {
      91         448 :         U8_TRACE_INFO_STR( "sqlite3_exec() failed:", error_msg );
      92         448 :         sqlite3_free( error_msg );
      93         448 :         error_msg = NULL;
      94             :     }
      95        1064 :     return result;
      96             : }
      97             : 
      98        2311 : static inline u8_error_t data_database_prepare_statement ( data_database_t *this_,
      99             :                                                            const char *string_statement,
     100             :                                                            unsigned int string_size,
     101             :                                                            sqlite3_stmt **out_statement_ptr )
     102             : {
     103        2311 :     assert( NULL != string_statement );
     104        2311 :     assert( NULL != out_statement_ptr );
     105        2311 :     u8_error_t result = U8_ERROR_NONE;
     106             :     const char *first_unused_statement_char;
     107             :     int sqlite_err;
     108        2311 :     sqlite3 *const db = (*this_).db;
     109             : 
     110        2311 :     U8_TRACE_INFO_STR( "sqlite3_prepare_v2():", string_statement );
     111        2311 :     sqlite_err = sqlite3_prepare_v2( db,
     112             :                                      string_statement,
     113             :                                      string_size,
     114             :                                      out_statement_ptr,
     115             :                                      &first_unused_statement_char
     116             :                                    );
     117        2311 :     if (( SQLITE_OK != sqlite_err )
     118        2311 :         || ( first_unused_statement_char != &(string_statement[string_size-1]) ))
     119             :     {
     120           0 :         U8_LOG_ERROR_STR( "sqlite3_prepare_v2() failed:", string_statement );
     121           0 :         U8_LOG_ERROR_INT( "sqlite3_prepare_v2() failed:", sqlite_err );
     122           0 :         U8_LOG_ERROR_STR( "sqlite3_prepare_v2() failed:", sqlite3_errmsg( db ) );
     123           0 :         result |= U8_ERROR_AT_DB;
     124             :     }
     125             : 
     126        2311 :     return result;
     127             : }
     128             : 
     129        2311 : static inline u8_error_t data_database_finalize_statement ( data_database_t *this_, sqlite3_stmt *statement_ptr )
     130             : {
     131        2311 :     assert( NULL != statement_ptr );
     132        2311 :     u8_error_t result = U8_ERROR_NONE;
     133             :     int sqlite_err;
     134             : 
     135        2311 :     U8_TRACE_INFO_STR( "sqlite3_finalize():", sqlite3_sql(statement_ptr) );
     136        2311 :     sqlite_err = sqlite3_finalize( statement_ptr );
     137        2311 :     if ( SQLITE_OK != sqlite_err )
     138             :     {
     139           0 :         U8_LOG_ERROR_STR( "sqlite3_finalize() failed:", sqlite3_sql(statement_ptr) );
     140           0 :         U8_LOG_ERROR_INT( "sqlite3_finalize() failed:", sqlite_err );
     141           0 :         result |= U8_ERROR_AT_DB;
     142             :     }
     143             : 
     144        2311 :     return result;
     145             : }
     146             : 
     147             : /* ================================ Information ================================ */
     148             : 
     149          30 : static inline const char *data_database_get_filename_ptr ( data_database_t *this_ )
     150             : {
     151          30 :     return ( data_database_is_open( this_ ) ? utf8stringbuf_get_string( (*this_).db_file_name ) : NULL );
     152             : }
     153             : 
     154             : /* ================================ Change Listener ================================ */
     155             : 
     156         803 : static inline data_change_notifier_t *data_database_get_notifier_ptr ( data_database_t *this_ )
     157             : {
     158         803 :     return &((*this_).notifier);
     159             : }
     160             : 
     161         115 : static inline void data_database_private_clear_db_listener_list( data_database_t *this_ )
     162             : {
     163        2415 :     for( int index = 0; index < DATA_DATABASE_MAX_LISTENERS; index ++ )
     164             :     {
     165        2300 :         (*this_).listener_list[index] = NULL;
     166             :     }
     167         115 : }
     168             : 
     169             : /* ================================ Lifecycle Lock ================================ */
     170             : 
     171        3652 : static inline u8_error_t data_database_lock_on_write ( data_database_t *this_ )
     172             : {
     173        3652 :     assert( (*this_).locked_on_write == false );
     174        3652 :     u8_error_t result = U8_ERROR_NONE;
     175             : 
     176        3652 :     g_mutex_lock ( &((*this_).lock_on_write) );
     177        3652 :     (*this_).locked_on_write = true;
     178             : 
     179        3652 :     return result;
     180             : }
     181             : 
     182        3652 : static inline u8_error_t data_database_unlock_on_write ( data_database_t *this_ )
     183             : {
     184        3652 :     assert( (*this_).locked_on_write == true );
     185        3652 :     u8_error_t result = U8_ERROR_NONE;
     186             : 
     187        3652 :     (*this_).locked_on_write = false;
     188        3652 :     g_mutex_unlock ( &((*this_).lock_on_write) );
     189             : 
     190        3652 :     return result;
     191             : }
     192             : 
     193             : 
     194             : /*
     195             : Copyright 2016-2024 Andreas Warnke
     196             : 
     197             : Licensed under the Apache License, Version 2.0 (the "License");
     198             : you may not use this file except in compliance with the License.
     199             : You may obtain a copy of the License at
     200             : 
     201             :     http://www.apache.org/licenses/LICENSE-2.0
     202             : 
     203             : Unless required by applicable law or agreed to in writing, software
     204             : distributed under the License is distributed on an "AS IS" BASIS,
     205             : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     206             : See the License for the specific language governing permissions and
     207             : limitations under the License.
     208             : */

Generated by: LCOV version 1.16