Line data Source code
1 : /* File: data_visible_classifier_iterator.c; Copyright and License: see below */
2 :
3 : #include "storage/data_visible_classifier_iterator.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 : #include <stdint.h>
10 :
11 : /*!
12 : * \brief predefined search statement to find visible classifiers by diagram-id
13 : */
14 : const char *const DATA_VISIBLE_CLASSIFIER_ITERATOR_SELECT_BY_DIAGRAM_ID =
15 : "SELECT classifiers.id,classifiers.main_type,classifiers.stereotype,"
16 : "classifiers.name,classifiers.description,classifiers.x_order,classifiers.y_order,classifiers.list_order,"
17 : "classifiers.uuid,"
18 : "diagramelements.id,diagramelements.diagram_id,diagramelements.display_flags,diagramelements.focused_feature_id,"
19 : "diagramelements.uuid "
20 : "FROM classifiers "
21 : "INNER JOIN diagramelements ON diagramelements.classifier_id=classifiers.id "
22 : "WHERE diagramelements.diagram_id=? "
23 : "ORDER BY diagramelements.id ASC;";
24 : /* To ensure reporducible results of json esports, ordering by a unique key is required here. */
25 : /* Ordering by 2 keys did not produce the expected results with sqlite3 3.34.1 */
26 : /* "ORDER BY classifiers.list_order ASC,diagramelements.id ASC;"; */
27 : /* see also https://sqlite.org/forum/forumpost/e1033dab18c262ac4b36cdf7c65bf87a5aaaecab3b3ba100e4588fc30e50f9fb */
28 :
29 : /*!
30 : * \brief the column id of the result where this parameter is stored: id
31 : */
32 : static const int RESULT_CLASSIFIER_ID_COLUMN = 0;
33 :
34 : /*!
35 : * \brief the column id of the result where this parameter is stored: main_type
36 : */
37 : static const int RESULT_CLASSIFIER_MAIN_TYPE_COLUMN = 1;
38 :
39 : /*!
40 : * \brief the column id of the result where this parameter is stored: stereotype
41 : */
42 : static const int RESULT_CLASSIFIER_STEREOTYPE_COLUMN = 2;
43 :
44 : /*!
45 : * \brief the column id of the result where this parameter is stored: name
46 : */
47 : static const int RESULT_CLASSIFIER_NAME_COLUMN = 3;
48 :
49 : /*!
50 : * \brief the column id of the result where this parameter is stored: description
51 : */
52 : static const int RESULT_CLASSIFIER_DESCRIPTION_COLUMN = 4;
53 :
54 : /*!
55 : * \brief the column id of the result where this parameter is stored: x_order
56 : */
57 : static const int RESULT_CLASSIFIER_X_ORDER_COLUMN = 5;
58 :
59 : /*!
60 : * \brief the column id of the result where this parameter is stored: y_order
61 : */
62 : static const int RESULT_CLASSIFIER_Y_ORDER_COLUMN = 6;
63 :
64 : /*!
65 : * \brief the column id of the result where this parameter is stored: list_order
66 : */
67 : static const int RESULT_CLASSIFIER_LIST_ORDER_COLUMN = 7;
68 :
69 : /*!
70 : * \brief the column id of the result where this parameter is stored: uuid
71 : */
72 : static const int RESULT_CLASSIFIER_UUID_COLUMN = 8;
73 :
74 : /*!
75 : * \brief the column id of the result where this parameter is stored: diagramelements.id
76 : */
77 : static const int RESULT_DIAGRAMELEMENT_ID_COLUMN = 9;
78 :
79 : /*!
80 : * \brief the column id of the result where this parameter is stored: diagramelements.diagram_id
81 : */
82 : static const int RESULT_DIAGRAMELEMENT_DIAGRAM_ID_COLUMN = 10;
83 :
84 : /*!
85 : * \brief the column id of the result where this parameter is stored: diagramelements.display_flags
86 : */
87 : static const int RESULT_DIAGRAMELEMENT_DISPLAY_FLAGS_COLUMN = 11;
88 :
89 : /*!
90 : * \brief the column id of the result where this parameter is stored: diagramelements.focused_feature_id
91 : */
92 : static const int RESULT_DIAGRAMELEMENT_FOCUSED_FEATURE_ID_COLUMN = 12;
93 :
94 : /*!
95 : * \brief the column id of the result where this parameter is stored: diagramelements.uuid
96 : */
97 : static const int RESULT_DIAGRAMELEMENT_UUID_COLUMN = 13;
98 :
99 12 : void data_visible_classifier_iterator_init_empty ( data_visible_classifier_iterator_t *this_ )
100 : {
101 12 : U8_TRACE_BEGIN();
102 :
103 12 : data_database_borrowed_stmt_init_void( &((*this_).statement) );
104 12 : (*this_).is_at_end = true;
105 :
106 12 : U8_TRACE_END();
107 12 : }
108 :
109 12 : u8_error_t data_visible_classifier_iterator_reinit ( data_visible_classifier_iterator_t *this_,
110 : data_database_borrowed_stmt_t statement )
111 : {
112 12 : U8_TRACE_BEGIN();
113 12 : assert( data_database_borrowed_stmt_is_valid( &statement ) );
114 12 : u8_error_t result = U8_ERROR_NONE;
115 :
116 : /* destroy old state */
117 12 : result = data_visible_classifier_iterator_destroy( this_ );
118 :
119 : /* init new state */
120 12 : (*this_).statement = statement;
121 12 : (*this_).is_at_end = false;
122 12 : result |= data_visible_classifier_iterator_private_step_to_next( this_ );
123 :
124 12 : U8_TRACE_END_ERR(result);
125 12 : return result;
126 : }
127 :
128 24 : u8_error_t data_visible_classifier_iterator_destroy ( data_visible_classifier_iterator_t *this_ )
129 : {
130 24 : U8_TRACE_BEGIN();
131 24 : u8_error_t result = U8_ERROR_NONE;
132 :
133 24 : result |= data_database_borrowed_stmt_destroy( &((*this_).statement) );
134 24 : (*this_).is_at_end = true;
135 :
136 24 : U8_TRACE_END_ERR(result);
137 24 : return result;
138 : }
139 :
140 200 : bool data_visible_classifier_iterator_has_next ( const data_visible_classifier_iterator_t *this_ )
141 : {
142 200 : return ( ! (*this_).is_at_end );
143 : }
144 :
145 192 : u8_error_t data_visible_classifier_iterator_next ( data_visible_classifier_iterator_t *this_, data_visible_classifier_t *out_visible_classifier )
146 : {
147 192 : U8_TRACE_BEGIN();
148 192 : assert( NULL != out_visible_classifier );
149 192 : assert( data_database_borrowed_stmt_is_valid( &((*this_).statement) ) );
150 192 : u8_error_t result = U8_ERROR_NONE;
151 :
152 192 : if ( ! (*this_).is_at_end )
153 : {
154 191 : sqlite3_stmt *const sql_statement = data_database_borrowed_stmt_get_statement( &((*this_).statement) );
155 :
156 191 : data_visible_classifier_init_empty( out_visible_classifier );
157 :
158 : data_classifier_t *current_classifier;
159 191 : current_classifier = data_visible_classifier_get_classifier_ptr( out_visible_classifier );
160 191 : data_row_t classifier_id = sqlite3_column_int64( sql_statement, RESULT_CLASSIFIER_ID_COLUMN );
161 191 : result |= data_classifier_reinit( current_classifier,
162 : classifier_id,
163 191 : sqlite3_column_int( sql_statement, RESULT_CLASSIFIER_MAIN_TYPE_COLUMN ),
164 191 : (const char*) sqlite3_column_text( sql_statement, RESULT_CLASSIFIER_STEREOTYPE_COLUMN ),
165 191 : (const char*) sqlite3_column_text( sql_statement, RESULT_CLASSIFIER_NAME_COLUMN ),
166 191 : (const char*) sqlite3_column_text( sql_statement, RESULT_CLASSIFIER_DESCRIPTION_COLUMN ),
167 191 : sqlite3_column_int( sql_statement, RESULT_CLASSIFIER_X_ORDER_COLUMN ),
168 191 : sqlite3_column_int( sql_statement, RESULT_CLASSIFIER_Y_ORDER_COLUMN ),
169 191 : sqlite3_column_int( sql_statement, RESULT_CLASSIFIER_LIST_ORDER_COLUMN ),
170 191 : (const char*) sqlite3_column_text( sql_statement, RESULT_CLASSIFIER_UUID_COLUMN )
171 : );
172 :
173 : data_diagramelement_t *current_diag_element;
174 191 : current_diag_element = data_visible_classifier_get_diagramelement_ptr( out_visible_classifier );
175 191 : result |= data_diagramelement_reinit( current_diag_element,
176 191 : sqlite3_column_int64( sql_statement, RESULT_DIAGRAMELEMENT_ID_COLUMN ),
177 191 : sqlite3_column_int64( sql_statement, RESULT_DIAGRAMELEMENT_DIAGRAM_ID_COLUMN ),
178 : classifier_id,
179 191 : sqlite3_column_int64( sql_statement, RESULT_DIAGRAMELEMENT_DISPLAY_FLAGS_COLUMN ),
180 191 : sqlite3_column_int64( sql_statement, RESULT_DIAGRAMELEMENT_FOCUSED_FEATURE_ID_COLUMN ),
181 191 : (const char*) sqlite3_column_text( sql_statement, RESULT_DIAGRAMELEMENT_UUID_COLUMN )
182 : );
183 191 : if ( SQLITE_NULL == sqlite3_column_type( sql_statement, RESULT_DIAGRAMELEMENT_FOCUSED_FEATURE_ID_COLUMN ) )
184 : {
185 188 : data_diagramelement_set_focused_feature_row ( current_diag_element, DATA_ROW_VOID );
186 : }
187 :
188 191 : data_classifier_trace( current_classifier );
189 191 : data_diagramelement_trace( current_diag_element );
190 :
191 : /* step to next */
192 191 : result |= data_visible_classifier_iterator_private_step_to_next( this_ );
193 : }
194 : else
195 : {
196 1 : U8_TRACE_INFO( "iterator already at end" );
197 1 : (*this_).is_at_end = true;
198 1 : result |= U8_ERROR_INVALID_REQUEST;
199 : }
200 :
201 192 : U8_TRACE_END_ERR( result );
202 192 : return result;
203 : }
204 :
205 203 : u8_error_t data_visible_classifier_iterator_private_step_to_next ( data_visible_classifier_iterator_t *this_ )
206 : {
207 203 : U8_TRACE_BEGIN();
208 203 : assert( data_database_borrowed_stmt_is_valid( &((*this_).statement) ) );
209 203 : u8_error_t result = U8_ERROR_NONE;
210 :
211 : /* do one step, check if is_at_end */
212 : {
213 : int sqlite_err;
214 203 : U8_TRACE_INFO( "sqlite3_step()" );
215 203 : sqlite_err = sqlite3_step( data_database_borrowed_stmt_get_statement( &((*this_).statement) ) );
216 203 : if ( SQLITE_DONE == sqlite_err )
217 : {
218 11 : U8_TRACE_INFO( "sqlite3_step finished: SQLITE_DONE" );
219 11 : (*this_).is_at_end = true;
220 : }
221 192 : else if ( SQLITE_ROW == sqlite_err )
222 : {
223 192 : (*this_).is_at_end = false;
224 : }
225 : else
226 : {
227 0 : U8_LOG_ERROR_INT( "sqlite3_step failed:", sqlite_err );
228 0 : (*this_).is_at_end = true;
229 0 : result |= data_visible_classifier_iterator_destroy( this_ );
230 0 : result |= U8_ERROR_AT_DB;
231 : }
232 : }
233 :
234 203 : U8_TRACE_END_ERR(result);
235 203 : return result;
236 : }
237 :
238 :
239 : /*
240 : Copyright 2024-2026 Andreas Warnke
241 :
242 : Licensed under the Apache License, Version 2.0 (the "License");
243 : you may not use this file except in compliance with the License.
244 : You may obtain a copy of the License at
245 :
246 : http://www.apache.org/licenses/LICENSE-2.0
247 :
248 : Unless required by applicable law or agreed to in writing, software
249 : distributed under the License is distributed on an "AS IS" BASIS,
250 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
251 : See the License for the specific language governing permissions and
252 : limitations under the License.
253 : */
|