Skip to content

Commit

Permalink
Warn on rows call in sqlite
Browse files Browse the repository at this point in the history
Track situations where the value returned may be inaccurate and warn on those usages.
  • Loading branch information
rbt committed Nov 30, 2021
1 parent 109ed17 commit 2742e84
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
14 changes: 14 additions & 0 deletions lib/DBDish/SQLite/StatementHandle.pm6
Expand Up @@ -12,6 +12,8 @@ has $!statement_handle;
has $!param-count;
has Int $!row_status;
has $!field_count;
has Bool $!warn-on-inaccurate-rows = True;
has Bool $!rows-is-accurate = False;

method !handle-error(Int $status) {
unless $status == SQLITE_OK {
Expand Down Expand Up @@ -46,6 +48,16 @@ method execute(*@params is raw --> DBDish::StatementHandle) {
self!set-err($!row_status, sqlite3_errmsg($!conn));
}
}
my role IntTrue { method Bool { self.defined } };

# Override DBDish::StatementHandle to throw this error
method rows() {
if (not $!rows-is-accurate and $!warn-on-inaccurate-rows) {
warn "SQLite rows result may not be accurate. "
}

self._rows();
}

method _row() {
my $list = ();
Expand All @@ -71,6 +83,8 @@ method _row() {
$!affected_rows++;
self.reset-err;
if ($!row_status = sqlite3_step($!statement_handle)) == SQLITE_DONE {
# Only after retrieving the final record is the rows value considered accurate.
$!rows-is-accurate = True;
self.finish;
}
}
Expand Down
4 changes: 3 additions & 1 deletion lib/DBDish/StatementHandle.pm6
Expand Up @@ -71,7 +71,9 @@ submethod DESTROY() {
self.dispose;
}

method rows {
method rows() {self._rows}

method _rows {
my constant TRUE_ZERO = 0 but IntTrue;
$!affected_rows.defined
?? $!affected_rows || TRUE_ZERO
Expand Down

0 comments on commit 2742e84

Please sign in to comment.