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 98 : u8_error_t data_database_reader_init ( data_database_reader_t *this_, data_database_t *database ) 11 : { 12 98 : U8_TRACE_BEGIN(); 13 98 : assert( NULL != database ); 14 98 : u8_error_t result = U8_ERROR_NONE; 15 : 16 98 : (*this_).database = database; 17 98 : (*this_).is_open = false; 18 : 19 98 : 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 98 : data_database_add_db_listener( database, &((*this_).me_as_listener) ); 24 : 25 98 : if ( data_database_is_open( database ) ) 26 : { 27 : /* if the database is open, open also the reader */ 28 95 : result |= data_database_reader_private_open( this_ ); 29 : } 30 : 31 98 : U8_TRACE_END_ERR(result); 32 98 : return result; 33 : } 34 : 35 98 : u8_error_t data_database_reader_destroy ( data_database_reader_t *this_ ) 36 : { 37 98 : U8_TRACE_BEGIN(); 38 98 : u8_error_t result = U8_ERROR_NONE; 39 : 40 98 : if ( (*this_).is_open ) 41 : { 42 97 : result |= data_database_reader_private_close( this_ ); 43 : } 44 : 45 98 : data_database_remove_db_listener( (*this_).database, &((*this_).me_as_listener) ); 46 : 47 98 : (*this_).database = NULL; 48 : 49 98 : U8_TRACE_END_ERR(result); 50 98 : 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 99 : u8_error_t data_database_reader_private_open ( data_database_reader_t *this_ ) 93 : { 94 99 : U8_TRACE_BEGIN(); 95 99 : u8_error_t result = U8_ERROR_NONE; 96 : 97 99 : if ( ! (*this_).is_open ) 98 : { 99 99 : result |= data_database_classifier_reader_init( &((*this_).temp_classifier_reader), (*this_).database ); 100 99 : result |= data_database_diagram_reader_init( &((*this_).temp_diagram_reader), (*this_).database ); 101 : 102 99 : if ( result == U8_ERROR_NONE ) 103 : { 104 99 : (*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 99 : U8_TRACE_END_ERR(result); 114 99 : return result; 115 : } 116 : 117 99 : u8_error_t data_database_reader_private_close ( data_database_reader_t *this_ ) 118 : { 119 99 : U8_TRACE_BEGIN(); 120 99 : u8_error_t result = U8_ERROR_NONE; 121 : 122 99 : if ( (*this_).is_open ) 123 : { 124 99 : result |= data_database_diagram_reader_destroy( &((*this_).temp_diagram_reader) ); 125 99 : result |= data_database_classifier_reader_destroy( &((*this_).temp_classifier_reader) ); 126 : 127 99 : (*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 99 : U8_TRACE_END_ERR(result); 136 99 : 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 : */