Skip to content

Commit e3f95ca

Browse files
committed
Add statement_timeout= method that leverages progress_handler to interrupt long-running statements
1 parent 5361528 commit e3f95ca

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

lib/sqlite3/database.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,21 @@ def translate_from_db types, row
749749
@type_translator.call types, row
750750
end
751751

752+
def statement_timeout=( milliseconds )
753+
timeout_seconds = milliseconds.fdiv(1000)
754+
755+
progress_handler do
756+
now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
757+
if @statement_timeout_deadline.nil?
758+
@statement_timeout_deadline = now + timeout_seconds
759+
elsif now > @statement_timeout_deadline
760+
next false
761+
else
762+
true
763+
end
764+
end
765+
end
766+
752767
private
753768

754769
NULL_TRANSLATOR = lambda { |_, row| row }

0 commit comments

Comments
 (0)