A regular expression for the types of queries to cache. Any queries not matching this regular expression are not cached.
The StatementCache instance for this connection. Note that each connection has a separate StatementCache, because prepared statements are connection-specific.
Set the #statement_cache for the connection, using the database's :statement_cache_opts option.
# File lib/sequel/extensions/pg_statement_cache.rb, line 230 def self.extended(c) c.instance_variable_set(:@statement_cache, StatementCache.new(c.sequel_db.opts[:statement_cache_opts] || {}){|name| c.deallocate(name)}) end
Deallocate on the server the prepared statement with the given name.
# File lib/sequel/extensions/pg_statement_cache.rb, line 241 def deallocate(name) begin execute("DEALLOCATE #{name}") rescue PGError # table probably got removed, just ignore it end end
pg seems to already use the db method (but not the @db instance variable), so use the #sequel_db method to access the related Sequel::Database object.
# File lib/sequel/extensions/pg_statement_cache.rb, line 236 def sequel_db @db end
If the sql query string is one we should cache, cache it. If the query already has a related prepared statement with it, execute the prepared statement instead of executing the query normally.
# File lib/sequel/extensions/pg_statement_cache.rb, line 254 def execute_query(sql, args=nil) if sql =~ DML_RE if name = statement_cache.fetch(sql){|stmt_name| sequel_db.log_yield("PREPARE #{stmt_name} AS #{sql}"){prepare(stmt_name, sql)}} if args sequel_db.log_yield("EXECUTE #{name} (#{sql})", args){exec_prepared(name, args)} else sequel_db.log_yield("EXECUTE #{name} (#{sql})"){exec_prepared(name)} end else super end else super end end