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 149 : u8_error_t data_diagramelement_iterator_init_empty ( data_diagramelement_iterator_t *this_ ) 56 : { 57 149 : U8_TRACE_BEGIN(); 58 149 : u8_error_t result = U8_ERROR_NONE; 59 : 60 149 : data_database_borrowed_stmt_init_void( &((*this_).statement) ); 61 149 : (*this_).is_at_end = true; 62 : 63 149 : U8_TRACE_END_ERR(result); 64 149 : return result; 65 : } 66 : 67 149 : u8_error_t data_diagramelement_iterator_reinit ( data_diagramelement_iterator_t *this_, 68 : data_database_borrowed_stmt_t statement ) 69 : { 70 149 : U8_TRACE_BEGIN(); 71 149 : assert( data_database_borrowed_stmt_is_valid( &statement ) ); 72 149 : u8_error_t result = U8_ERROR_NONE; 73 : 74 : /* destroy old state */ 75 149 : result = data_diagramelement_iterator_destroy( this_ ); 76 : 77 : /* init new state */ 78 149 : (*this_).statement = statement; 79 149 : (*this_).is_at_end = false; 80 149 : result |= data_diagramelement_iterator_private_step_to_next( this_ ); 81 : 82 149 : U8_TRACE_END_ERR(result); 83 149 : return result; 84 : } 85 : 86 298 : u8_error_t data_diagramelement_iterator_destroy ( data_diagramelement_iterator_t *this_ ) 87 : { 88 298 : U8_TRACE_BEGIN(); 89 298 : u8_error_t result = U8_ERROR_NONE; 90 : 91 298 : result |= data_database_borrowed_stmt_destroy( &((*this_).statement) ); 92 298 : (*this_).is_at_end = true; 93 : 94 298 : U8_TRACE_END_ERR(result); 95 298 : return result; 96 : } 97 : 98 164 : bool data_diagramelement_iterator_has_next ( const data_diagramelement_iterator_t *this_ ) 99 : { 100 164 : return ( ! (*this_).is_at_end ); 101 : } 102 : 103 15 : u8_error_t data_diagramelement_iterator_next ( data_diagramelement_iterator_t *this_, data_diagramelement_t *out_diagramelement ) 104 : { 105 15 : U8_TRACE_BEGIN(); 106 15 : assert( NULL != out_diagramelement ); 107 15 : assert( data_database_borrowed_stmt_is_valid( &((*this_).statement) ) ); 108 15 : u8_error_t result = U8_ERROR_NONE; 109 : 110 15 : if ( ! (*this_).is_at_end ) 111 : { 112 15 : sqlite3_stmt *const sql_statement = data_database_borrowed_stmt_get_statement( &((*this_).statement) ); 113 15 : result |= data_diagramelement_init( out_diagramelement, 114 15 : sqlite3_column_int64( sql_statement, RESULT_DIAGRAMELEMENT_ID_COLUMN ), 115 15 : sqlite3_column_int64( sql_statement, RESULT_DIAGRAMELEMENT_DIAGRAM_ID_COLUMN ), 116 15 : sqlite3_column_int64( sql_statement, RESULT_DIAGRAMELEMENT_CLASSIFIER_ID_COLUMN ), 117 15 : sqlite3_column_int64( sql_statement, RESULT_DIAGRAMELEMENT_DISPLAY_FLAGS_COLUMN ), 118 15 : sqlite3_column_int64( sql_statement, RESULT_DIAGRAMELEMENT_FOCUSED_FEATURE_ID_COLUMN ), 119 15 : (const char*) sqlite3_column_text( sql_statement, RESULT_DIAGRAMELEMENT_UUID_COLUMN ) 120 : ); 121 15 : if ( SQLITE_NULL == sqlite3_column_type( sql_statement, RESULT_DIAGRAMELEMENT_FOCUSED_FEATURE_ID_COLUMN ) ) 122 : { 123 4 : data_diagramelement_set_focused_feature_row_id ( out_diagramelement, DATA_ROW_VOID ); 124 : } 125 15 : data_diagramelement_trace( out_diagramelement ); 126 : 127 : /* step to next */ 128 15 : result |= data_diagramelement_iterator_private_step_to_next( this_ ); 129 : } 130 : else 131 : { 132 0 : U8_TRACE_INFO( "iterator already at end" ); 133 0 : (*this_).is_at_end = true; 134 0 : result |= U8_ERROR_INVALID_REQUEST; 135 : } 136 : 137 15 : U8_TRACE_END_ERR( result ); 138 15 : return result; 139 : } 140 : 141 164 : u8_error_t data_diagramelement_iterator_private_step_to_next ( data_diagramelement_iterator_t *this_ ) 142 : { 143 164 : U8_TRACE_BEGIN(); 144 164 : assert( data_database_borrowed_stmt_is_valid( &((*this_).statement) ) ); 145 164 : u8_error_t result = U8_ERROR_NONE; 146 : 147 : /* do one step, check if is_at_end */ 148 : { 149 : int sqlite_err; 150 164 : U8_TRACE_INFO( "sqlite3_step()" ); 151 164 : sqlite_err = sqlite3_step( data_database_borrowed_stmt_get_statement( &((*this_).statement) ) ); 152 164 : if ( SQLITE_DONE == sqlite_err ) 153 : { 154 148 : U8_TRACE_INFO( "sqlite3_step finished: SQLITE_DONE" ); 155 148 : (*this_).is_at_end = true; 156 : } 157 16 : else if ( SQLITE_ROW == sqlite_err ) 158 : { 159 16 : (*this_).is_at_end = false; 160 : } 161 : else 162 : { 163 0 : U8_LOG_ERROR_INT( "sqlite3_step failed:", sqlite_err ); 164 0 : (*this_).is_at_end = true; 165 0 : result |= data_diagramelement_iterator_destroy( this_ ); 166 0 : result |= U8_ERROR_AT_DB; 167 : } 168 : } 169 : 170 164 : U8_TRACE_END_ERR(result); 171 164 : return result; 172 : } 173 : 174 : 175 : /* 176 : Copyright 2024-2024 Andreas Warnke 177 : 178 : Licensed under the Apache License, Version 2.0 (the "License"); 179 : you may not use this file except in compliance with the License. 180 : You may obtain a copy of the License at 181 : 182 : http://www.apache.org/licenses/LICENSE-2.0 183 : 184 : Unless required by applicable law or agreed to in writing, software 185 : distributed under the License is distributed on an "AS IS" BASIS, 186 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 187 : See the License for the specific language governing permissions and 188 : limitations under the License. 189 : */