Skip to content

Commit

Permalink
Support Proc passed to option setup_regexp_function in sqlite adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
paddor committed Dec 23, 2023
1 parent a36cf45 commit 5d9dce9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

* Add auto_cast_date_and_time extension, for casting date and time values using SQL standard functions (jeremyevans)

* Cache regexp objects in the sqlite adapter to improve performance (paddor)
* Improve performance and flexibility of regexp matching in sqlite adapter (paddor)

=== 5.75.0 (2023-12-01)

Expand Down
4 changes: 3 additions & 1 deletion doc/opening_databases.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,9 @@ The following additional options are supported:
:timeout :: the busy timeout to use in milliseconds (default: 5000).
:setup_regexp_function :: Whether to setup a REGEXP function in the underlying SQLite3::Database object. Doing so
allows you to use regexp support in dataset expressions. If ':cached', caches each
unique regex (risk of memory leak).
unique regex (more efficient but risk of memory leak). If a Proc like
+proc{|regex_str,str| ...}+ is provided, it is used to determine whether the string
in question matches.

Note that SQLite memory databases are restricted to a single connection by
default. This is because SQLite does not allow multiple connections to
Expand Down
6 changes: 5 additions & 1 deletion lib/sequel/adapters/sqlite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ def initialize(opts = OPTS)
# :timeout :: how long to wait for the database to be available if it
# is locked, given in milliseconds (default is 5000)
# :setup_regexp_function :: enable use of Regexp objects with SQL
# 'REGEXP' operator (with regexp cache if ':cached')
# 'REGEXP' operator (with regexp cache if ':cached', or
# custom behavior if it's a Proc like
# +proc{|regex_str,str| ...}+)
def connect(server)
opts = server_opts(server)
opts[:database] = ':memory:' if blank_object?(opts[:database])
Expand Down Expand Up @@ -208,6 +210,8 @@ def setup_regexp_function(db, how)
when :cached
cache = Hash.new{|h,k| h[k] = Regexp.new(k)}
cb = proc { |func, regexp_str, str| func.result = cache[regexp_str].match(str) ? 1 : 0 }
when Proc
cb = proc { |func, regexp_str, str| func.result = how.call(regexp_str, str) ? 1 : 0 }
else
cb = proc { |func, regexp_str, str| func.result = Regexp.new(regexp_str).match(str) ? 1 : 0 }
end
Expand Down

0 comments on commit 5d9dce9

Please sign in to comment.