Skip to content

Commit 395e75f

Browse files
committed
Reimplement execute primitives with iterators
1 parent a369866 commit 395e75f

File tree

1 file changed

+17
-30
lines changed

1 file changed

+17
-30
lines changed

hdr/sqlite_modern_cpp.h

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -135,37 +135,9 @@ namespace sqlite {
135135
used(true);
136136
}
137137

138-
void _extract(std::function<void(void)> call_back) {
139-
int hresult;
140-
_start_execute();
141-
142-
while((hresult = sqlite3_step(_stmt.get())) == SQLITE_ROW) {
143-
call_back();
144-
}
138+
void _extract(std::function<void(void)> call_back);
145139

146-
if(hresult != SQLITE_DONE) {
147-
errors::throw_sqlite_error(hresult, sql());
148-
}
149-
}
150-
151-
void _extract_single_value(std::function<void(void)> call_back) {
152-
int hresult;
153-
_start_execute();
154-
155-
if((hresult = sqlite3_step(_stmt.get())) == SQLITE_ROW) {
156-
call_back();
157-
} else if(hresult == SQLITE_DONE) {
158-
throw errors::no_rows("no rows to extract: exactly 1 row expected", sql(), SQLITE_DONE);
159-
}
160-
161-
if((hresult = sqlite3_step(_stmt.get())) == SQLITE_ROW) {
162-
throw errors::more_rows("not all rows extracted", sql(), SQLITE_ROW);
163-
}
164-
165-
if(hresult != SQLITE_DONE) {
166-
errors::throw_sqlite_error(hresult, sql());
167-
}
168-
}
140+
void _extract_single_value(std::function<void(void)> call_back);
169141

170142
#ifdef _MSC_VER
171143
sqlite3_stmt* _prepare(const std::u16string& sql) {
@@ -378,6 +350,21 @@ namespace sqlite {
378350
inline row_iterator database_binder::end() {
379351
return row_iterator();
380352
}
353+
void database_binder::_extract(std::function<void(void)> call_back) {
354+
for(auto &&row[[maybe_unused]] : *this)
355+
call_back();
356+
}
357+
358+
void database_binder::_extract_single_value(std::function<void(void)> call_back) {
359+
auto iter = begin();
360+
if(iter == end())
361+
throw errors::no_rows("no rows to extract: exactly 1 row expected", sql(), SQLITE_DONE);
362+
363+
call_back();
364+
365+
if(++iter != end())
366+
throw errors::more_rows("not all rows extracted", sql(), SQLITE_ROW);
367+
}
381368

382369
namespace sql_function_binder {
383370
template<

0 commit comments

Comments
 (0)