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 99 : u8_error_t data_database_reader_init ( data_database_reader_t *this_, data_database_t *database )
11 : {
12 99 : U8_TRACE_BEGIN();
13 99 : assert( NULL != database );
14 99 : u8_error_t result = U8_ERROR_NONE;
15 :
16 99 : (*this_).database = database;
17 99 : (*this_).is_open = false;
18 :
19 99 : 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 99 : data_database_add_db_listener( database, &((*this_).me_as_listener) );
24 :
25 99 : if ( data_database_is_open( database ) )
26 : {
27 : /* if the database is open, open also the reader */
28 96 : result |= data_database_reader_private_open( this_ );
29 : }
30 :
31 99 : U8_TRACE_END_ERR(result);
32 99 : return result;
33 : }
34 :
35 99 : u8_error_t data_database_reader_destroy ( data_database_reader_t *this_ )
36 : {
37 99 : U8_TRACE_BEGIN();
38 99 : u8_error_t result = U8_ERROR_NONE;
39 :
40 99 : if ( (*this_).is_open )
41 : {
42 97 : result |= data_database_reader_private_close( this_ );
43 : }
44 :
45 99 : data_database_remove_db_listener( (*this_).database, &((*this_).me_as_listener) );
46 :
47 99 : (*this_).database = NULL;
48 :
49 99 : U8_TRACE_END_ERR(result);
50 99 : return result;
51 : }
52 :
53 9 : void data_database_reader_db_change_callback ( data_database_reader_t *this_, data_database_listener_signal_t signal_id )
54 : {
55 9 : U8_TRACE_BEGIN();
56 9 : u8_error_t result = U8_ERROR_NONE;
57 :
58 9 : switch ( signal_id )
59 : {
60 4 : case DATA_DATABASE_LISTENER_SIGNAL_PREPARE_CLOSE:
61 : {
62 4 : U8_TRACE_INFO( "DATA_DATABASE_LISTENER_SIGNAL_PREPARE_CLOSE" );
63 4 : if ( (*this_).is_open )
64 : {
65 4 : result |= data_database_reader_private_close( this_ );
66 : }
67 : }
68 4 : break;
69 :
70 5 : case DATA_DATABASE_LISTENER_SIGNAL_DB_OPENED:
71 : {
72 5 : U8_TRACE_INFO( "DATA_DATABASE_LISTENER_SIGNAL_DB_OPENED" );
73 5 : if ( (*this_).is_open )
74 : {
75 0 : result |= data_database_reader_private_close( this_ );
76 : }
77 5 : result |= data_database_reader_private_open( this_ );
78 : }
79 5 : break;
80 :
81 0 : default:
82 : {
83 0 : U8_LOG_ERROR( "unexpected data_database_listener_signal_t" );
84 : }
85 : }
86 :
87 9 : U8_TRACE_END();
88 9 : }
89 :
90 : /* ================================ private ================================ */
91 :
92 101 : u8_error_t data_database_reader_private_open ( data_database_reader_t *this_ )
93 : {
94 101 : U8_TRACE_BEGIN();
95 101 : u8_error_t result = U8_ERROR_NONE;
96 :
97 101 : if ( ! (*this_).is_open )
98 : {
99 101 : result |= data_database_classifier_reader_init( &((*this_).temp_classifier_reader), (*this_).database );
100 101 : result |= data_database_diagram_reader_init( &((*this_).temp_diagram_reader), (*this_).database );
101 :
102 101 : if ( result == U8_ERROR_NONE )
103 : {
104 101 : (*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 101 : U8_TRACE_END_ERR(result);
114 101 : return result;
115 : }
116 :
117 101 : u8_error_t data_database_reader_private_close ( data_database_reader_t *this_ )
118 : {
119 101 : U8_TRACE_BEGIN();
120 101 : u8_error_t result = U8_ERROR_NONE;
121 :
122 101 : if ( (*this_).is_open )
123 : {
124 101 : result |= data_database_diagram_reader_destroy( &((*this_).temp_diagram_reader) );
125 101 : result |= data_database_classifier_reader_destroy( &((*this_).temp_classifier_reader) );
126 :
127 101 : (*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 101 : U8_TRACE_END_ERR(result);
136 101 : return result;
137 : }
138 :
139 :
140 : /*
141 : Copyright 2016-2025 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 : */
|