Module: Sequel::QueryBlocker

Defined in:
lib/sequel/extensions/query_blocker.rb

Defined Under Namespace

Classes: BlockedQuery

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(db) ⇒ Object

[View source]

65
66
67
68
69
# File 'lib/sequel/extensions/query_blocker.rb', line 65

def self.extended(db)
  db.instance_exec do
    @blocked_query_scopes ||= {}
  end
end

Instance Method Details

#allow_queries(opts = OPTS, &block) ⇒ Object

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.

[View source]

105
106
107
# File 'lib/sequel/extensions/query_blocker.rb', line 105

def allow_queries(opts=OPTS, &block)
  _allow_or_block_queries(false, opts, &block)
end

#block_queries(opts = OPTS, &block) ⇒ Object

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.

[View source]

119
120
121
# File 'lib/sequel/extensions/query_blocker.rb', line 119

def block_queries(opts=OPTS, &block)
  _allow_or_block_queries(true, opts, &block)
end

#block_queries?Boolean

Whether queries are currently blocked.

Returns:

  • (Boolean)
[View source]

93
94
95
96
97
98
99
100
# File 'lib/sequel/extensions/query_blocker.rb', line 93

def block_queries?
  b = @blocked_query_scopes
  b.fetch(Fiber.current) do
    b.fetch(Thread.current) do
      b.fetch(:global, false)
    end
  end
end

#log_connection_yield(sql, conn, args = nil) ⇒ Object

Check whether queries are blocked before executing them.

[View source]

83
84
85
86
87
88
89
90
# File 'lib/sequel/extensions/query_blocker.rb', line 83

def log_connection_yield(sql, conn, args=nil)
  # All database adapters should be calling this method around
  # query execution (otherwise the queries would not get logged),
  # ensuring the blocking is checked.  Any database adapter issuing
  # a query without calling this method is considered buggy.
  check_blocked_queries!
  super
end

#valid_connection?(conn) ⇒ Boolean

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.

Returns:

  • (Boolean)
[View source]

76
77
78
79
80
# File 'lib/sequel/extensions/query_blocker.rb', line 76

def valid_connection?(conn)
  super
rescue BlockedQuery
  true
end