Line data Source code
1 : /* File: data_diagramelement_iterator.c; Copyright and License: see below */
2 :
3 : #include "storage/data_diagramelement_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 : * The "order by id" is important to get reproducable results, e.g. for json export
13 : */
14 : const char *const DATA_DIAGRAMELEMENT_ITERATOR_SELECT_DIAGRAMELEMENTS_BY_DIAGRAM_ID =
15 : "SELECT id,diagram_id,classifier_id,display_flags,focused_feature_id,uuid FROM diagramelements "
16 : "WHERE diagram_id=? ORDER BY id ASC;";
17 :
18 : /*
19 : * The "order by id" is important to get reproducable results, e.g. for json export
20 : */
21 : const char *const DATA_DIAGRAMELEMENT_ITERATOR_SELECT_DIAGRAMELEMENTS_BY_CLASSIFIER_ID =
22 : "SELECT id,diagram_id,classifier_id,display_flags,focused_feature_id,uuid FROM diagramelements "
23 : "WHERE classifier_id=? ORDER BY id ASC;";
24 :
25 : /*!
26 : * \brief the column id of the result where this parameter is stored: id
27 : */
28 : static const int RESULT_DIAGRAMELEMENT_ID_COLUMN = 0;
29 :
30 : /*!
31 : * \brief the column id of the result where this parameter is stored: diagram_id
32 : */
33 : static const int RESULT_DIAGRAMELEMENT_DIAGRAM_ID_COLUMN = 1;
34 :
35 : /*!
36 : * \brief the column id of the result where this parameter is stored: classifier_id
37 : */
38 : static const int RESULT_DIAGRAMELEMENT_CLASSIFIER_ID_COLUMN = 2;
39 :
40 : /*!
41 : * \brief the column id of the result where this parameter is stored: display_flags
42 : */
43 : static const int RESULT_DIAGRAMELEMENT_DISPLAY_FLAGS_COLUMN = 3;
44 :
45 : /*!
46 : * \brief the column id of the result where this parameter is stored: focused_feature_id
47 : */
48 : static const int RESULT_DIAGRAMELEMENT_FOCUSED_FEATURE_ID_COLUMN = 4;
49 :
50 : /*!
51 : * \brief the column id of the result where this parameter is stored: uuid
52 : */
53 : static const int RESULT_DIAGRAMELEMENT_UUID_COLUMN = 5;
54 :
55 164 : void data_diagramelement_iterator_init_empty ( data_diagramelement_iterator_t *this_ )
56 : {
57 164 : U8_TRACE_BEGIN();
58 :
59 164 : data_database_borrowed_stmt_init_void( &((*this_).statement) );
60 164 : (*this_).is_at_end = true;
61 :
62 164 : U8_TRACE_END();
63 164 : }
64 :
65 164 : u8_error_t data_diagramelement_iterator_reinit ( data_diagramelement_iterator_t *this_,
66 : data_database_borrowed_stmt_t statement )
67 : {
68 164 : U8_TRACE_BEGIN();
69 164 : assert( data_database_borrowed_stmt_is_valid( &statement ) );
70 164 : u8_error_t result = U8_ERROR_NONE;
71 :
72 : /* destroy old state */
73 164 : result = data_diagramelement_iterator_destroy( this_ );
74 :
75 : /* init new state */
76 164 : (*this_).statement = statement;
77 164 : (*this_).is_at_end = false;
78 164 : result |= data_diagramelement_iterator_private_step_to_next( this_ );
79 :
80 164 : U8_TRACE_END_ERR(result);
81 164 : return result;
82 : }
83 :
84 328 : u8_error_t data_diagramelement_iterator_destroy ( data_diagramelement_iterator_t *this_ )
85 : {
86 328 : U8_TRACE_BEGIN();
87 328 : u8_error_t result = U8_ERROR_NONE;
88 :
89 328 : result |= data_database_borrowed_stmt_destroy( &((*this_).statement) );
90 328 : (*this_).is_at_end = true;
91 :
92 328 : U8_TRACE_END_ERR(result);
93 328 : return result;
94 : }
95 :
96 206 : bool data_diagramelement_iterator_has_next ( const data_diagramelement_iterator_t *this_ )
97 : {
98 206 : return ( ! (*this_).is_at_end );
99 : }
100 :
101 42 : u8_error_t data_diagramelement_iterator_next ( data_diagramelement_iterator_t *this_, data_diagramelement_t *out_diagramelement )
102 : {
103 42 : U8_TRACE_BEGIN();
104 42 : assert( NULL != out_diagramelement );
105 42 : assert( data_database_borrowed_stmt_is_valid( &((*this_).statement) ) );
106 42 : u8_error_t result = U8_ERROR_NONE;
107 :
108 42 : if ( ! (*this_).is_at_end )
109 : {
110 42 : sqlite3_stmt *const sql_statement = data_database_borrowed_stmt_get_statement( &((*this_).statement) );
111 42 : result |= data_diagramelement_init( out_diagramelement,
112 42 : sqlite3_column_int64( sql_statement, RESULT_DIAGRAMELEMENT_ID_COLUMN ),
113 42 : sqlite3_column_int64( sql_statement, RESULT_DIAGRAMELEMENT_DIAGRAM_ID_COLUMN ),
114 42 : sqlite3_column_int64( sql_statement, RESULT_DIAGRAMELEMENT_CLASSIFIER_ID_COLUMN ),
115 42 : sqlite3_column_int64( sql_statement, RESULT_DIAGRAMELEMENT_DISPLAY_FLAGS_COLUMN ),
116 42 : sqlite3_column_int64( sql_statement, RESULT_DIAGRAMELEMENT_FOCUSED_FEATURE_ID_COLUMN ),
117 42 : (const char*) sqlite3_column_text( sql_statement, RESULT_DIAGRAMELEMENT_UUID_COLUMN )
118 : );
119 42 : if ( SQLITE_NULL == sqlite3_column_type( sql_statement, RESULT_DIAGRAMELEMENT_FOCUSED_FEATURE_ID_COLUMN ) )
120 : {
121 23 : data_diagramelement_set_focused_feature_row ( out_diagramelement, DATA_ROW_VOID );
122 : }
123 42 : data_diagramelement_trace( out_diagramelement );
124 :
125 : /* step to next */
126 42 : result |= data_diagramelement_iterator_private_step_to_next( this_ );
127 : }
128 : else
129 : {
130 0 : U8_TRACE_INFO( "iterator already at end" );
131 0 : (*this_).is_at_end = true;
132 0 : result |= U8_ERROR_INVALID_REQUEST;
133 : }
134 :
135 42 : U8_TRACE_END_ERR( result );
136 42 : return result;
137 : }
138 :
139 206 : u8_error_t data_diagramelement_iterator_private_step_to_next ( data_diagramelement_iterator_t *this_ )
140 : {
141 206 : U8_TRACE_BEGIN();
142 206 : assert( data_database_borrowed_stmt_is_valid( &((*this_).statement) ) );
143 206 : u8_error_t result = U8_ERROR_NONE;
144 :
145 : /* do one step, check if is_at_end */
146 : {
147 : int sqlite_err;
148 206 : U8_TRACE_INFO( "sqlite3_step()" );
149 206 : sqlite_err = sqlite3_step( data_database_borrowed_stmt_get_statement( &((*this_).statement) ) );
150 206 : if ( SQLITE_DONE == sqlite_err )
151 : {
152 163 : U8_TRACE_INFO( "sqlite3_step finished: SQLITE_DONE" );
153 163 : (*this_).is_at_end = true;
154 : }
155 43 : else if ( SQLITE_ROW == sqlite_err )
156 : {
157 43 : (*this_).is_at_end = false;
158 : }
159 : else
160 : {
161 0 : U8_LOG_ERROR_INT( "sqlite3_step failed:", sqlite_err );
162 0 : (*this_).is_at_end = true;
163 0 : result |= data_diagramelement_iterator_destroy( this_ );
164 0 : result |= U8_ERROR_AT_DB;
165 : }
166 : }
167 :
168 206 : U8_TRACE_END_ERR(result);
169 206 : return result;
170 : }
171 :
172 :
173 : /*
174 : Copyright 2024-2026 Andreas Warnke
175 :
176 : Licensed under the Apache License, Version 2.0 (the "License");
177 : you may not use this file except in compliance with the License.
178 : You may obtain a copy of the License at
179 :
180 : http://www.apache.org/licenses/LICENSE-2.0
181 :
182 : Unless required by applicable law or agreed to in writing, software
183 : distributed under the License is distributed on an "AS IS" BASIS,
184 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
185 : See the License for the specific language governing permissions and
186 : limitations under the License.
187 : */
|