module Sequel::QueryBlocker

Public Class Methods

extended(db) click to toggle source
   # File lib/sequel/extensions/query_blocker.rb
65 def self.extended(db)
66   db.instance_exec do
67     @blocked_query_scopes ||= {}
68   end
69 end

Public Instance Methods

allow_queries(opts=OPTS, &block) click to toggle source

Allow queries inside the block. Only useful if they are already blocked for the same scope. Useful for blocking queries generally, and only allowing them in specific places. Takes the same :scope option as block_queries.

    # File lib/sequel/extensions/query_blocker.rb
105 def allow_queries(opts=OPTS, &block)
106   _allow_or_block_queries(false, opts, &block)
107 end
block_queries(opts=OPTS, &block) click to toggle source

Reject (raise an BlockedQuery exception) if there is an attempt to execute a query/statement inside the block.

The :scope option indicates which queries are rejected inside the block:

:global

This is the default, and rejects all queries.

:thread

Reject all queries in the current thread.

:fiber

Reject all queries in the current fiber.

Thread

Reject all queries in the given thread.

Fiber

Reject all queries in the given fiber.

    # File lib/sequel/extensions/query_blocker.rb
119 def block_queries(opts=OPTS, &block)
120   _allow_or_block_queries(true, opts, &block)
121 end
block_queries?() click to toggle source

Whether queries are currently blocked.

    # File lib/sequel/extensions/query_blocker.rb
 93 def block_queries?
 94   b = @blocked_query_scopes
 95   b.fetch(Fiber.current) do
 96     b.fetch(Thread.current) do
 97       b.fetch(:global, false)
 98     end
 99   end
100 end
log_connection_yield(sql, conn, args=nil) click to toggle source

Check whether queries are blocked before executing them.

Calls superclass method
   # File lib/sequel/extensions/query_blocker.rb
83 def log_connection_yield(sql, conn, args=nil)
84   # All database adapters should be calling this method around
85   # query execution (otherwise the queries would not get logged),
86   # ensuring the blocking is checked.  Any database adapter issuing
87   # a query without calling this method is considered buggy.
88   check_blocked_queries!
89   super
90 end
valid_connection?(conn) click to toggle source

If checking a connection for validity, and a BlockedQuery exception is raised, treat it as a valid connection. You cannot check whether the connection is valid without issuing a query, and if queries are blocked, you need to assume it is valid or assume it is not. Since it most cases it will be valid, this assumes validity.

Calls superclass method
   # File lib/sequel/extensions/query_blocker.rb
76 def valid_connection?(conn)
77   super
78 rescue BlockedQuery
79   true
80 end

Private Instance Methods

_allow_or_block_queries(value, opts) { || ... } click to toggle source

Internals of block_queries and allow_queries.

    # File lib/sequel/extensions/query_blocker.rb
126 def _allow_or_block_queries(value, opts)
127   scope = query_blocker_scope(opts)
128   prev_value = nil
129   scopes = @blocked_query_scopes
130 
131   begin
132     Sequel.synchronize do
133       prev_value = scopes[scope]
134       scopes[scope] = value
135     end
136 
137     yield
138   ensure
139     Sequel.synchronize do
140       if prev_value.nil?
141         scopes.delete(scope)
142       else
143         scopes[scope] = prev_value
144       end
145     end
146   end
147 end
check_blocked_queries!() click to toggle source

Raise a BlockQuery exception if queries are currently blocked.

    # File lib/sequel/extensions/query_blocker.rb
166 def check_blocked_queries!
167   raise BlockedQuery, "cannot execute query inside a block_queries block" if block_queries?
168 end
query_blocker_scope(opts) click to toggle source

The scope for the query block, either :global, or a Thread or Fiber instance.

    # File lib/sequel/extensions/query_blocker.rb
150 def query_blocker_scope(opts)
151   case scope = opts[:scope]
152   when nil
153     :global
154   when :global, Thread, Fiber
155     scope
156   when :thread
157     Thread.current
158   when :fiber
159     Fiber.current
160   else
161     raise Sequel::Error, "invalid scope given to block_queries: #{scope.inspect}"
162   end
163 end