Line data Source code
1 : /* File: data_relationship_iterator.c; Copyright and License: see below */
2 :
3 : #include "storage/data_relationship_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 : const char *const DATA_RELATIONSHIP_ITERATOR_SELECT_RELATIONSHIPS_BY_DIAGRAM_ID =
12 : "SELECT relationships.id,relationships.main_type,relationships.from_classifier_id,relationships.to_classifier_id,"
13 : "relationships.stereotype,relationships.name,relationships.description,relationships.list_order,"
14 : "relationships.from_feature_id,relationships.to_feature_id,relationships.uuid,"
15 : "source.id, dest.id " /* source.id, dest.id needed only for debugging */
16 : "FROM relationships "
17 : "INNER JOIN diagramelements AS source "
18 : "ON source.classifier_id=relationships.from_classifier_id "
19 : "INNER JOIN diagramelements AS dest "
20 : "ON (dest.classifier_id=relationships.to_classifier_id)AND(dest.diagram_id=source.diagram_id) "
21 : "WHERE source.diagram_id=? "
22 : "GROUP BY relationships.id "
23 : "ORDER BY relationships.list_order ASC;";
24 :
25 : /*
26 : * Order by id to ensure a defined, non-changeing order of relationships in json export
27 : */
28 : const char *const DATA_RELATIONSHIP_ITERATOR_SELECT_RELATIONSHIPS_BY_CLASSIFIER_ID =
29 : "SELECT id,main_type,from_classifier_id,to_classifier_id,stereotype,name,description,list_order,"
30 : "from_feature_id,to_feature_id,uuid,-1,-1 "
31 : "FROM relationships "
32 : "WHERE from_classifier_id=? OR to_classifier_id=? "
33 : "ORDER BY id ASC;";
34 :
35 : const char *const DATA_RELATIONSHIP_ITERATOR_SELECT_RELATIONSHIPS_BY_FEATURE_ID =
36 : "SELECT id,main_type,from_classifier_id,to_classifier_id,stereotype,name,description,list_order,"
37 : "from_feature_id,to_feature_id,uuid,-1,-1 "
38 : "FROM relationships "
39 : "WHERE from_feature_id=? OR to_feature_id=?;";
40 :
41 : /*!
42 : * \brief the column id of the result where this parameter is stored: id
43 : */
44 : static const int RESULT_RELATIONSHIP_ID_COLUMN = 0;
45 :
46 : /*!
47 : * \brief the column id of the result where this parameter is stored: main_type
48 : */
49 : static const int RESULT_RELATIONSHIP_MAIN_TYPE_COLUMN = 1;
50 :
51 : /*!
52 : * \brief the column id of the result where this parameter is stored: from_classifier_id
53 : */
54 : static const int RESULT_RELATIONSHIP_FROM_CLASSIFIER_ID_COLUMN = 2;
55 :
56 : /*!
57 : * \brief the column id of the result where this parameter is stored: to_classifier_id
58 : */
59 : static const int RESULT_RELATIONSHIP_TO_CLASSIFIER_ID_COLUMN = 3;
60 :
61 : /*!
62 : * \brief the column id of the result where this parameter is stored: name
63 : */
64 : static const int RESULT_RELATIONSHIP_STEREOTYPE_COLUMN = 4;
65 :
66 : /*!
67 : * \brief the column id of the result where this parameter is stored: name
68 : */
69 : static const int RESULT_RELATIONSHIP_NAME_COLUMN = 5;
70 :
71 : /*!
72 : * \brief the column id of the result where this parameter is stored: description
73 : */
74 : static const int RESULT_RELATIONSHIP_DESCRIPTION_COLUMN = 6;
75 :
76 : /*!
77 : * \brief the column id of the result where this parameter is stored: list_order
78 : */
79 : static const int RESULT_RELATIONSHIP_LIST_ORDER_COLUMN = 7;
80 :
81 : /*!
82 : * \brief the column id of the result where this parameter is stored: from_feature_id
83 : */
84 : static const int RESULT_RELATIONSHIP_FROM_FEATURE_ID_COLUMN = 8;
85 :
86 : /*!
87 : * \brief the column id of the result where this parameter is stored: to_feature_id
88 : */
89 : static const int RESULT_RELATIONSHIP_TO_FEATURE_ID_COLUMN = 9;
90 :
91 : /*!
92 : * \brief the column id of the result where this parameter is stored: uuid
93 : */
94 : static const int RESULT_RELATIONSHIP_UUID_COLUMN = 10;
95 :
96 : /*!
97 : * \brief the column id of the result where this parameter is stored: source diagramelements.id
98 : */
99 : static const int RESULT_RELATIONSHIP_SOURCE_DIAGRAMELEMENTS_ID_COLUMN = 11;
100 :
101 : /*!
102 : * \brief the column id of the result where this parameter is stored: dest diagramelements.id
103 : */
104 : static const int RESULT_RELATIONSHIP_DEST_DIAGRAMELEMENTS_ID_COLUMN = 12;
105 :
106 31 : u8_error_t data_relationship_iterator_init_empty ( data_relationship_iterator_t *this_ )
107 : {
108 31 : U8_TRACE_BEGIN();
109 31 : u8_error_t result = U8_ERROR_NONE;
110 :
111 31 : data_database_borrowed_stmt_init_void( &((*this_).statement) );
112 31 : (*this_).is_at_end = true;
113 :
114 31 : U8_TRACE_END_ERR(result);
115 31 : return result;
116 : }
117 :
118 31 : u8_error_t data_relationship_iterator_reinit ( data_relationship_iterator_t *this_,
119 : data_database_borrowed_stmt_t statement )
120 : {
121 31 : U8_TRACE_BEGIN();
122 31 : assert( data_database_borrowed_stmt_is_valid( &statement ) );
123 31 : u8_error_t result = U8_ERROR_NONE;
124 :
125 : /* destroy old state */
126 31 : result = data_relationship_iterator_destroy( this_ );
127 :
128 : /* init new state */
129 31 : (*this_).statement = statement;
130 31 : (*this_).is_at_end = false;
131 31 : result |= data_relationship_iterator_private_step_to_next( this_ );
132 :
133 31 : U8_TRACE_END_ERR(result);
134 31 : return result;
135 : }
136 :
137 62 : u8_error_t data_relationship_iterator_destroy ( data_relationship_iterator_t *this_ )
138 : {
139 62 : U8_TRACE_BEGIN();
140 62 : u8_error_t result = U8_ERROR_NONE;
141 :
142 62 : result |= data_database_borrowed_stmt_destroy( &((*this_).statement) );
143 62 : (*this_).is_at_end = true;
144 :
145 62 : U8_TRACE_END_ERR(result);
146 62 : return result;
147 : }
148 :
149 428 : bool data_relationship_iterator_has_next ( const data_relationship_iterator_t *this_ )
150 : {
151 428 : return ( ! (*this_).is_at_end );
152 : }
153 :
154 402 : u8_error_t data_relationship_iterator_next ( data_relationship_iterator_t *this_, data_relationship_t *out_relationship )
155 : {
156 402 : U8_TRACE_BEGIN();
157 402 : assert( NULL != out_relationship );
158 402 : assert( data_database_borrowed_stmt_is_valid( &((*this_).statement) ) );
159 402 : u8_error_t result = U8_ERROR_NONE;
160 :
161 402 : if ( ! (*this_).is_at_end )
162 : {
163 401 : sqlite3_stmt *const sql_statement = data_database_borrowed_stmt_get_statement( &((*this_).statement) );
164 :
165 802 : result |= data_relationship_init( out_relationship,
166 401 : sqlite3_column_int64( sql_statement, RESULT_RELATIONSHIP_ID_COLUMN ),
167 401 : sqlite3_column_int64( sql_statement, RESULT_RELATIONSHIP_FROM_CLASSIFIER_ID_COLUMN ),
168 401 : sqlite3_column_int64( sql_statement, RESULT_RELATIONSHIP_FROM_FEATURE_ID_COLUMN ),
169 401 : sqlite3_column_int64( sql_statement, RESULT_RELATIONSHIP_TO_CLASSIFIER_ID_COLUMN ),
170 401 : sqlite3_column_int64( sql_statement, RESULT_RELATIONSHIP_TO_FEATURE_ID_COLUMN ),
171 401 : sqlite3_column_int( sql_statement, RESULT_RELATIONSHIP_MAIN_TYPE_COLUMN ),
172 401 : (const char*) sqlite3_column_text( sql_statement, RESULT_RELATIONSHIP_STEREOTYPE_COLUMN ),
173 401 : (const char*) sqlite3_column_text( sql_statement, RESULT_RELATIONSHIP_NAME_COLUMN ),
174 401 : (const char*) sqlite3_column_text( sql_statement, RESULT_RELATIONSHIP_DESCRIPTION_COLUMN ),
175 : sqlite3_column_int( sql_statement, RESULT_RELATIONSHIP_LIST_ORDER_COLUMN ),
176 401 : (const char*) sqlite3_column_text( sql_statement, RESULT_RELATIONSHIP_UUID_COLUMN )
177 : );
178 401 : if ( SQLITE_NULL == sqlite3_column_type( sql_statement, RESULT_RELATIONSHIP_FROM_FEATURE_ID_COLUMN ) )
179 : {
180 390 : data_relationship_set_from_feature_row_id ( out_relationship, DATA_ROW_VOID );
181 : }
182 401 : if ( SQLITE_NULL == sqlite3_column_type( sql_statement, RESULT_RELATIONSHIP_TO_FEATURE_ID_COLUMN ) )
183 : {
184 394 : data_relationship_set_to_feature_row_id ( out_relationship, DATA_ROW_VOID );
185 : }
186 401 : U8_TRACE_INFO_INT( "(source)diagramelements.id:", sqlite3_column_int64( sql_statement, RESULT_RELATIONSHIP_SOURCE_DIAGRAMELEMENTS_ID_COLUMN ) );
187 401 : U8_TRACE_INFO_INT( "(dest)diagramelements.id:", sqlite3_column_int64( sql_statement, RESULT_RELATIONSHIP_DEST_DIAGRAMELEMENTS_ID_COLUMN ) );
188 401 : data_relationship_trace( out_relationship );
189 :
190 : /* step to next */
191 401 : result |= data_relationship_iterator_private_step_to_next( this_ );
192 : }
193 : else
194 : {
195 1 : U8_TRACE_INFO( "iterator already at end" );
196 1 : (*this_).is_at_end = true;
197 1 : result |= U8_ERROR_INVALID_REQUEST;
198 : }
199 :
200 402 : U8_TRACE_END_ERR( result );
201 402 : return result;
202 : }
203 :
204 432 : u8_error_t data_relationship_iterator_private_step_to_next ( data_relationship_iterator_t *this_ )
205 : {
206 432 : U8_TRACE_BEGIN();
207 432 : assert( data_database_borrowed_stmt_is_valid( &((*this_).statement) ) );
208 432 : u8_error_t result = U8_ERROR_NONE;
209 :
210 : /* do one step, check if is_at_end */
211 : {
212 : int sqlite_err;
213 432 : U8_TRACE_INFO( "sqlite3_step()" );
214 432 : sqlite_err = sqlite3_step( data_database_borrowed_stmt_get_statement( &((*this_).statement) ) );
215 432 : if ( SQLITE_DONE == sqlite_err )
216 : {
217 30 : U8_TRACE_INFO( "sqlite3_step finished: SQLITE_DONE" );
218 30 : (*this_).is_at_end = true;
219 : }
220 402 : else if ( SQLITE_ROW == sqlite_err )
221 : {
222 402 : (*this_).is_at_end = false;
223 : }
224 : else
225 : {
226 0 : U8_LOG_ERROR_INT( "sqlite3_step failed:", sqlite_err );
227 0 : (*this_).is_at_end = true;
228 0 : result |= data_relationship_iterator_destroy( this_ );
229 0 : result |= U8_ERROR_AT_DB;
230 : }
231 : }
232 :
233 432 : U8_TRACE_END_ERR(result);
234 432 : return result;
235 : }
236 :
237 :
238 : /*
239 : Copyright 2024-2024 Andreas Warnke
240 :
241 : Licensed under the Apache License, Version 2.0 (the "License");
242 : you may not use this file except in compliance with the License.
243 : You may obtain a copy of the License at
244 :
245 : http://www.apache.org/licenses/LICENSE-2.0
246 :
247 : Unless required by applicable law or agreed to in writing, software
248 : distributed under the License is distributed on an "AS IS" BASIS,
249 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
250 : See the License for the specific language governing permissions and
251 : limitations under the License.
252 : */
|