Line data Source code
1 : /* File: data_feature_iterator.c; Copyright and License: see below */
2 :
3 : #include "storage/data_feature_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 features by diagram-id
13 : */
14 : const char *const DATA_FEATURE_ITERATOR_SELECT_FEATURES_BY_DIAGRAM_ID =
15 : "SELECT features.id,features.main_type,features.classifier_id,"
16 : "features.key,features.value,features.description,features.list_order,features.uuid,"
17 : "diagramelements.id " /* diagramelements.id needed only for debugging */
18 : "FROM features INNER JOIN diagramelements ON diagramelements.classifier_id=features.classifier_id "
19 : "WHERE diagramelements.diagram_id=? GROUP BY features.id ORDER BY features.list_order ASC;";
20 :
21 : /*!
22 : * \brief predefined search statement to find features by classifier-id
23 : *
24 : * Order by id to ensure a defined, non-changeing order of relationships in json export
25 : */
26 : const char *const DATA_FEATURE_ITERATOR_SELECT_FEATURES_BY_CLASSIFIER_ID =
27 : "SELECT id,main_type,classifier_id,key,value,description,list_order,uuid,-1 "
28 : "FROM features "
29 : "WHERE classifier_id=? ORDER BY id ASC;";
30 :
31 : /*!
32 : * \brief the column id of the result where this parameter is stored: id
33 : */
34 : static const int RESULT_FEATURE_ID_COLUMN = 0;
35 :
36 : /*!
37 : * \brief the column id of the result where this parameter is stored: main_type
38 : */
39 : static const int RESULT_FEATURE_MAIN_TYPE_COLUMN = 1;
40 :
41 : /*!
42 : * \brief the column id of the result where this parameter is stored: classifier_id
43 : */
44 : static const int RESULT_FEATURE_CLASSIFIER_ID_COLUMN = 2;
45 :
46 : /*!
47 : * \brief the column id of the result where this parameter is stored: key
48 : */
49 : static const int RESULT_FEATURE_KEY_COLUMN = 3;
50 :
51 : /*!
52 : * \brief the column id of the result where this parameter is stored: value
53 : */
54 : static const int RESULT_FEATURE_VALUE_COLUMN = 4;
55 :
56 : /*!
57 : * \brief the column id of the result where this parameter is stored: description
58 : */
59 : static const int RESULT_FEATURE_DESCRIPTION_COLUMN = 5;
60 :
61 : /*!
62 : * \brief the column id of the result where this parameter is stored: list_order
63 : */
64 : static const int RESULT_FEATURE_LIST_ORDER_COLUMN = 6;
65 :
66 : /*!
67 : * \brief the column id of the result where this parameter is stored: uuid
68 : */
69 : static const int RESULT_FEATURE_LIST_UUID_COLUMN = 7;
70 :
71 : /*!
72 : * \brief the column id of the result where this parameter is stored: diagramelements.id
73 : */
74 : static const int RESULT_FEATURE_DIAGRAMELEMENTS_ID_COLUMN = 8;
75 :
76 21 : u8_error_t data_feature_iterator_init_empty ( data_feature_iterator_t *this_ )
77 : {
78 21 : U8_TRACE_BEGIN();
79 21 : u8_error_t result = U8_ERROR_NONE;
80 :
81 21 : data_database_borrowed_stmt_init_void( &((*this_).statement) );
82 21 : (*this_).is_at_end = true;
83 :
84 21 : U8_TRACE_END_ERR(result);
85 21 : return result;
86 : }
87 :
88 21 : u8_error_t data_feature_iterator_reinit ( data_feature_iterator_t *this_,
89 : data_database_borrowed_stmt_t statement )
90 : {
91 21 : U8_TRACE_BEGIN();
92 21 : assert( data_database_borrowed_stmt_is_valid( &statement ) );
93 21 : u8_error_t result = U8_ERROR_NONE;
94 :
95 : /* destroy old state */
96 21 : result = data_feature_iterator_destroy( this_ );
97 :
98 : /* init new state */
99 21 : (*this_).statement = statement;
100 21 : (*this_).is_at_end = false;
101 21 : result |= data_feature_iterator_private_step_to_next( this_ );
102 :
103 21 : U8_TRACE_END_ERR(result);
104 21 : return result;
105 : }
106 :
107 42 : u8_error_t data_feature_iterator_destroy ( data_feature_iterator_t *this_ )
108 : {
109 42 : U8_TRACE_BEGIN();
110 42 : u8_error_t result = U8_ERROR_NONE;
111 :
112 42 : result |= data_database_borrowed_stmt_destroy( &((*this_).statement) );
113 42 : (*this_).is_at_end = true;
114 :
115 42 : U8_TRACE_END_ERR(result);
116 42 : return result;
117 : }
118 :
119 283 : bool data_feature_iterator_has_next ( const data_feature_iterator_t *this_ )
120 : {
121 283 : return ( ! (*this_).is_at_end );
122 : }
123 :
124 275 : u8_error_t data_feature_iterator_next ( data_feature_iterator_t *this_, data_feature_t *out_feature )
125 : {
126 275 : U8_TRACE_BEGIN();
127 275 : assert( NULL != out_feature );
128 275 : assert( data_database_borrowed_stmt_is_valid( &((*this_).statement) ) );
129 275 : u8_error_t result = U8_ERROR_NONE;
130 :
131 275 : if ( ! (*this_).is_at_end )
132 : {
133 273 : sqlite3_stmt *const sql_statement = data_database_borrowed_stmt_get_statement( &((*this_).statement) );
134 :
135 546 : result |= data_feature_init( out_feature,
136 273 : sqlite3_column_int64( sql_statement, RESULT_FEATURE_ID_COLUMN ),
137 273 : sqlite3_column_int( sql_statement, RESULT_FEATURE_MAIN_TYPE_COLUMN ),
138 273 : sqlite3_column_int64( sql_statement, RESULT_FEATURE_CLASSIFIER_ID_COLUMN ),
139 273 : (const char*) sqlite3_column_text( sql_statement, RESULT_FEATURE_KEY_COLUMN ),
140 273 : (const char*) sqlite3_column_text( sql_statement, RESULT_FEATURE_VALUE_COLUMN ),
141 273 : (const char*) sqlite3_column_text( sql_statement, RESULT_FEATURE_DESCRIPTION_COLUMN ),
142 : sqlite3_column_int( sql_statement, RESULT_FEATURE_LIST_ORDER_COLUMN ),
143 273 : (const char*) sqlite3_column_text( sql_statement, RESULT_FEATURE_LIST_UUID_COLUMN )
144 : );
145 273 : U8_TRACE_INFO_INT( "diagramelements.id:", sqlite3_column_int64( sql_statement, RESULT_FEATURE_DIAGRAMELEMENTS_ID_COLUMN ) );
146 273 : data_feature_trace( out_feature );
147 :
148 : /* step to next */
149 273 : result |= data_feature_iterator_private_step_to_next( this_ );
150 : }
151 : else
152 : {
153 2 : U8_TRACE_INFO( "iterator already at end" );
154 2 : (*this_).is_at_end = true;
155 2 : result |= U8_ERROR_INVALID_REQUEST;
156 : }
157 :
158 275 : U8_TRACE_END_ERR( result );
159 275 : return result;
160 : }
161 :
162 294 : u8_error_t data_feature_iterator_private_step_to_next ( data_feature_iterator_t *this_ )
163 : {
164 294 : U8_TRACE_BEGIN();
165 294 : assert( data_database_borrowed_stmt_is_valid( &((*this_).statement) ) );
166 294 : u8_error_t result = U8_ERROR_NONE;
167 :
168 : /* do one step, check if is_at_end */
169 : {
170 : int sqlite_err;
171 294 : U8_TRACE_INFO( "sqlite3_step()" );
172 294 : sqlite_err = sqlite3_step( data_database_borrowed_stmt_get_statement( &((*this_).statement) ) );
173 294 : if ( SQLITE_DONE == sqlite_err )
174 : {
175 20 : U8_TRACE_INFO( "sqlite3_step finished: SQLITE_DONE" );
176 20 : (*this_).is_at_end = true;
177 : }
178 274 : else if ( SQLITE_ROW == sqlite_err )
179 : {
180 274 : (*this_).is_at_end = false;
181 : }
182 : else
183 : {
184 0 : U8_LOG_ERROR_INT( "sqlite3_step failed:", sqlite_err );
185 0 : (*this_).is_at_end = true;
186 0 : result |= data_feature_iterator_destroy( this_ );
187 0 : result |= U8_ERROR_AT_DB;
188 : }
189 : }
190 :
191 294 : U8_TRACE_END_ERR(result);
192 294 : return result;
193 : }
194 :
195 :
196 : /*
197 : Copyright 2024-2025 Andreas Warnke
198 :
199 : Licensed under the Apache License, Version 2.0 (the "License");
200 : you may not use this file except in compliance with the License.
201 : You may obtain a copy of the License at
202 :
203 : http://www.apache.org/licenses/LICENSE-2.0
204 :
205 : Unless required by applicable law or agreed to in writing, software
206 : distributed under the License is distributed on an "AS IS" BASIS,
207 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
208 : See the License for the specific language governing permissions and
209 : limitations under the License.
210 : */
|