Line data Source code
1 : /* File: data_diagram_iterator.c; Copyright and License: see below */
2 :
3 : #include "storage/data_diagram_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_DIAGRAM_ITERATOR_SELECT_DIAGRAMS_BY_PARENT_ID =
12 : "SELECT id,parent_id,diagram_type,stereotype,name,description,list_order,display_flags,uuid "
13 : "FROM diagrams WHERE parent_id=? ORDER BY list_order ASC;";
14 :
15 : const char *const DATA_DIAGRAM_ITERATOR_SELECT_DIAGRAMS_BY_PARENT_ID_NULL =
16 : "SELECT id,parent_id,diagram_type,stereotype,name,description,list_order,display_flags,uuid "
17 : "FROM diagrams WHERE parent_id IS NULL ORDER BY list_order ASC;";
18 :
19 : const char *const DATA_DIAGRAM_ITERATOR_SELECT_DIAGRAMS_BY_CLASSIFIER_ID =
20 : "SELECT diagrams.id,diagrams.parent_id,diagrams.diagram_type,diagrams.stereotype,"
21 : "diagrams.name,diagrams.description,diagrams.list_order,diagrams.display_flags,diagrams.uuid "
22 : "FROM diagrams "
23 : "INNER JOIN diagramelements ON diagramelements.diagram_id=diagrams.id "
24 : "WHERE diagramelements.classifier_id=? "
25 : "GROUP BY diagrams.id " /* filter duplicates if a classifier exists twice in a diagram */
26 : "ORDER BY diagrams.list_order ASC;";
27 :
28 : /*!
29 : * \brief the column id of the result where this parameter is stored: id
30 : */
31 : static const int RESULT_DIAGRAM_ID_COLUMN = 0;
32 :
33 : /*!
34 : * \brief the column id of the result where this parameter is stored: parent_id
35 : */
36 : static const int RESULT_DIAGRAM_PARENT_ID_COLUMN = 1;
37 :
38 : /*!
39 : * \brief the column id of the result where this parameter is stored: type
40 : */
41 : static const int RESULT_DIAGRAM_TYPE_COLUMN = 2;
42 :
43 : /*!
44 : * \brief the column id of the result where this parameter is stored: stereotype
45 : */
46 : static const int RESULT_DIAGRAM_STEREOTYPE_COLUMN = 3;
47 :
48 : /*!
49 : * \brief the column id of the result where this parameter is stored: name
50 : */
51 : static const int RESULT_DIAGRAM_NAME_COLUMN = 4;
52 :
53 : /*!
54 : * \brief the column id of the result where this parameter is stored: description
55 : */
56 : static const int RESULT_DIAGRAM_DESCRIPTION_COLUMN = 5;
57 :
58 : /*!
59 : * \brief the column id of the result where this parameter is stored: list_order
60 : */
61 : static const int RESULT_DIAGRAM_LIST_ORDER_COLUMN = 6;
62 :
63 : /*!
64 : * \brief the column id of the result where this parameter is stored: display_flags
65 : */
66 : static const int RESULT_DIAGRAM_DISPLAY_FLAGS_COLUMN = 7;
67 :
68 : /*!
69 : * \brief the column id of the result where this parameter is stored: uuid
70 : */
71 : static const int RESULT_DIAGRAM_UUID_COLUMN = 8;
72 :
73 189 : u8_error_t data_diagram_iterator_init_empty ( data_diagram_iterator_t *this_ )
74 : {
75 189 : U8_TRACE_BEGIN();
76 189 : u8_error_t result = U8_ERROR_NONE;
77 :
78 189 : data_database_borrowed_stmt_init_void( &((*this_).statement) );
79 189 : (*this_).is_at_end = true;
80 :
81 189 : U8_TRACE_END_ERR(result);
82 189 : return result;
83 : }
84 :
85 189 : u8_error_t data_diagram_iterator_reinit ( data_diagram_iterator_t *this_,
86 : data_database_borrowed_stmt_t statement )
87 : {
88 189 : U8_TRACE_BEGIN();
89 189 : assert( data_database_borrowed_stmt_is_valid( &statement ) );
90 189 : u8_error_t result = U8_ERROR_NONE;
91 :
92 : /* destroy old state */
93 189 : result = data_diagram_iterator_destroy( this_ );
94 :
95 : /* init new state */
96 189 : (*this_).statement = statement;
97 189 : (*this_).is_at_end = false;
98 189 : result |= data_diagram_iterator_private_step_to_next( this_ );
99 :
100 189 : U8_TRACE_END_ERR(result);
101 189 : return result;
102 : }
103 :
104 378 : u8_error_t data_diagram_iterator_destroy ( data_diagram_iterator_t *this_ )
105 : {
106 378 : U8_TRACE_BEGIN();
107 378 : u8_error_t result = U8_ERROR_NONE;
108 :
109 378 : result |= data_database_borrowed_stmt_destroy( &((*this_).statement) );
110 378 : (*this_).is_at_end = true;
111 :
112 378 : U8_TRACE_END_ERR(result);
113 378 : return result;
114 : }
115 :
116 221 : bool data_diagram_iterator_has_next ( const data_diagram_iterator_t *this_ )
117 : {
118 221 : return ( ! (*this_).is_at_end );
119 : }
120 :
121 43 : u8_error_t data_diagram_iterator_next ( data_diagram_iterator_t *this_, data_diagram_t *out_diagram )
122 : {
123 43 : U8_TRACE_BEGIN();
124 43 : assert( NULL != out_diagram );
125 43 : assert( data_database_borrowed_stmt_is_valid( &((*this_).statement) ) );
126 43 : u8_error_t result = U8_ERROR_NONE;
127 :
128 43 : if ( ! (*this_).is_at_end )
129 : {
130 41 : sqlite3_stmt *const sql_statement = data_database_borrowed_stmt_get_statement( &((*this_).statement) );
131 82 : result |= data_diagram_init( out_diagram,
132 41 : sqlite3_column_int64( sql_statement, RESULT_DIAGRAM_ID_COLUMN ),
133 41 : sqlite3_column_int64( sql_statement, RESULT_DIAGRAM_PARENT_ID_COLUMN ),
134 41 : sqlite3_column_int( sql_statement, RESULT_DIAGRAM_TYPE_COLUMN ),
135 41 : (const char*) sqlite3_column_text( sql_statement, RESULT_DIAGRAM_STEREOTYPE_COLUMN ),
136 41 : (const char*) sqlite3_column_text( sql_statement, RESULT_DIAGRAM_NAME_COLUMN ),
137 41 : (const char*) sqlite3_column_text( sql_statement, RESULT_DIAGRAM_DESCRIPTION_COLUMN ),
138 : sqlite3_column_int( sql_statement, RESULT_DIAGRAM_LIST_ORDER_COLUMN ),
139 41 : sqlite3_column_int64( sql_statement, RESULT_DIAGRAM_DISPLAY_FLAGS_COLUMN ),
140 41 : (const char*) sqlite3_column_text( sql_statement, RESULT_DIAGRAM_UUID_COLUMN )
141 : );
142 41 : if ( SQLITE_NULL == sqlite3_column_type( sql_statement, RESULT_DIAGRAM_PARENT_ID_COLUMN ) )
143 : {
144 38 : data_diagram_set_parent_row_id( out_diagram, DATA_ROW_VOID );
145 : }
146 41 : data_diagram_trace( out_diagram );
147 :
148 : /* step to next */
149 41 : result |= data_diagram_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 43 : U8_TRACE_END_ERR( result );
159 43 : return result;
160 : }
161 :
162 230 : u8_error_t data_diagram_iterator_private_step_to_next ( data_diagram_iterator_t *this_ )
163 : {
164 230 : U8_TRACE_BEGIN();
165 230 : assert( data_database_borrowed_stmt_is_valid( &((*this_).statement) ) );
166 230 : u8_error_t result = U8_ERROR_NONE;
167 :
168 : /* do one step, check if is_at_end */
169 : {
170 : int sqlite_err;
171 230 : U8_TRACE_INFO( "sqlite3_step()" );
172 230 : sqlite_err = sqlite3_step( data_database_borrowed_stmt_get_statement( &((*this_).statement) ) );
173 230 : if ( SQLITE_DONE == sqlite_err )
174 : {
175 184 : U8_TRACE_INFO( "sqlite3_step finished: SQLITE_DONE" );
176 184 : (*this_).is_at_end = true;
177 : }
178 46 : else if ( SQLITE_ROW == sqlite_err )
179 : {
180 46 : (*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_diagram_iterator_destroy( this_ );
187 0 : result |= U8_ERROR_AT_DB;
188 : }
189 : }
190 :
191 230 : U8_TRACE_END_ERR(result);
192 230 : return result;
193 : }
194 :
195 :
196 : /*
197 : Copyright 2024-2024 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 : */
|