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 111 : u8_error_t data_database_reader_init ( data_database_reader_t *this_, data_database_t *database )
11 : {
12 111 : U8_TRACE_BEGIN();
13 111 : assert( NULL != database );
14 111 : u8_error_t result = U8_ERROR_NONE;
15 :
16 111 : (*this_).database = database;
17 111 : (*this_).is_open = false;
18 :
19 111 : 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 111 : data_database_add_db_listener( database, &((*this_).me_as_listener) );
24 :
25 111 : if ( data_database_is_open( database ) )
26 : {
27 : /* if the database is open, open also the reader */
28 108 : result |= data_database_reader_private_open( this_ );
29 : }
30 :
31 111 : U8_TRACE_END_ERR(result);
32 111 : return result;
33 : }
34 :
35 111 : u8_error_t data_database_reader_destroy ( data_database_reader_t *this_ )
36 : {
37 111 : U8_TRACE_BEGIN();
38 111 : u8_error_t result = U8_ERROR_NONE;
39 :
40 111 : if ( (*this_).is_open )
41 : {
42 109 : result |= data_database_reader_private_close( this_ );
43 : }
44 :
45 111 : data_database_remove_db_listener( (*this_).database, &((*this_).me_as_listener) );
46 :
47 111 : (*this_).database = NULL;
48 :
49 111 : U8_TRACE_END_ERR(result);
50 111 : return result;
51 : }
52 :
53 7 : void data_database_reader_db_change_callback ( data_database_reader_t *this_, data_database_listener_signal_t signal_id )
54 : {
55 7 : U8_TRACE_BEGIN();
56 7 : u8_error_t result = U8_ERROR_NONE;
57 :
58 7 : switch ( signal_id )
59 : {
60 3 : case DATA_DATABASE_LISTENER_SIGNAL_PREPARE_CLOSE:
61 : {
62 3 : U8_TRACE_INFO( "DATA_DATABASE_LISTENER_SIGNAL_PREPARE_CLOSE" );
63 3 : if ( (*this_).is_open )
64 : {
65 3 : result |= data_database_reader_private_close( this_ );
66 : }
67 : }
68 3 : 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 7 : if ( result != U8_ERROR_NONE )
88 : {
89 0 : U8_LOG_ERROR( "error at closing old and opening new database" );
90 : }
91 :
92 7 : U8_TRACE_END();
93 7 : }
94 :
95 : /* ================================ private ================================ */
96 :
97 112 : u8_error_t data_database_reader_private_open ( data_database_reader_t *this_ )
98 : {
99 112 : U8_TRACE_BEGIN();
100 112 : u8_error_t result = U8_ERROR_NONE;
101 :
102 112 : if ( ! (*this_).is_open )
103 : {
104 112 : result |= data_database_classifier_reader_init( &((*this_).temp_classifier_reader), (*this_).database );
105 112 : result |= data_database_diagram_reader_init( &((*this_).temp_diagram_reader), (*this_).database );
106 :
107 112 : if ( result == U8_ERROR_NONE )
108 : {
109 112 : (*this_).is_open = true;
110 : }
111 : }
112 : else
113 : {
114 0 : result |= U8_ERROR_INVALID_REQUEST;
115 0 : U8_LOG_WARNING( "Database is already open." );
116 : }
117 :
118 112 : U8_TRACE_END_ERR(result);
119 112 : return result;
120 : }
121 :
122 112 : u8_error_t data_database_reader_private_close ( data_database_reader_t *this_ )
123 : {
124 112 : U8_TRACE_BEGIN();
125 112 : u8_error_t result = U8_ERROR_NONE;
126 :
127 112 : if ( (*this_).is_open )
128 : {
129 112 : result |= data_database_diagram_reader_destroy( &((*this_).temp_diagram_reader) );
130 112 : result |= data_database_classifier_reader_destroy( &((*this_).temp_classifier_reader) );
131 :
132 112 : (*this_).is_open = false;
133 : }
134 : else
135 : {
136 0 : result |= U8_ERROR_INVALID_REQUEST;
137 0 : U8_LOG_WARNING( "Database was not open." );
138 : }
139 :
140 112 : U8_TRACE_END_ERR(result);
141 112 : return result;
142 : }
143 :
144 :
145 : /*
146 : Copyright 2016-2026 Andreas Warnke
147 :
148 : Licensed under the Apache License, Version 2.0 (the "License");
149 : you may not use this file except in compliance with the License.
150 : You may obtain a copy of the License at
151 :
152 : http://www.apache.org/licenses/LICENSE-2.0
153 :
154 : Unless required by applicable law or agreed to in writing, software
155 : distributed under the License is distributed on an "AS IS" BASIS,
156 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
157 : See the License for the specific language governing permissions and
158 : limitations under the License.
159 : */
|