Sha256: 5086549d1fa832795487a46ca9bfb5fee427181bdd0c0ec7fa1ff35da95770c7

Contents?: true

Size: 1.64 KB

Versions: 14

Compression:

Stored size: 1.64 KB

Contents

require 'active_record/connection_adapters/statement_pool'

module ArJdbc
  module Abstract
    module StatementCache

      # This works a little differently than the AR implementation in that
      # we are storing an actual PreparedStatement object instead of just
      # the name of the prepared statement
      class StatementPool < ActiveRecord::ConnectionAdapters::StatementPool

        private

        def dealloc(statement)
          statement.close
        end

      end

      def initialize(*args) # (connection, logger, config)
        super

        # Only say we support the statement cache if we are using prepared statements
        # and have a max number of statements defined
        statement_limit = self.class.type_cast_config_to_integer(config[:statement_limit])
        @jdbc_statement_cache_enabled = prepared_statements && (statement_limit.nil? || statement_limit > 0)

        @statements = StatementPool.new(statement_limit) # AR (5.0) expects this to be stored as @statements
      end

      # Clears the prepared statements cache.
      def clear_cache!
        @statements.clear
      end

      def delete_cached_statement(sql)
        @statements.delete(sql_key(sql))
      end

      def fetch_cached_statement(sql)
        @statements[sql_key(sql)] ||= @connection.prepare_statement(sql)
      end

      def supports_statement_cache?
        ActiveSupport::Deprecation.deprecation_warning(__method__)
        @jdbc_statement_cache_enabled
      end

      private

      # This should be overridden by the adapter if the sql itself
      # is not enough to make the key unique
      def sql_key(sql)
        sql
      end

    end
  end
end

Version data entries

14 entries across 14 versions & 2 rubygems

Version Path
activerecord-jdbc-adapter-52.8-java lib/arjdbc/abstract/statement_cache.rb
activerecord-jdbc-alt-adapter-52.6.0-java lib/arjdbc/abstract/statement_cache.rb
activerecord-jdbc-adapter-52.7-java lib/arjdbc/abstract/statement_cache.rb
activerecord-jdbc-adapter-52.6-java lib/arjdbc/abstract/statement_cache.rb
activerecord-jdbc-alt-adapter-52.5.1-java lib/arjdbc/abstract/statement_cache.rb
activerecord-jdbc-adapter-52.5-java lib/arjdbc/abstract/statement_cache.rb
activerecord-jdbc-alt-adapter-52.4.0-java lib/arjdbc/abstract/statement_cache.rb
activerecord-jdbc-adapter-52.4-java lib/arjdbc/abstract/statement_cache.rb
activerecord-jdbc-alt-adapter-52.2.3-java lib/arjdbc/abstract/statement_cache.rb
activerecord-jdbc-alt-adapter-52.2.2-java lib/arjdbc/abstract/statement_cache.rb
activerecord-jdbc-adapter-52.3-java lib/arjdbc/abstract/statement_cache.rb
activerecord-jdbc-alt-adapter-52.2.1-java lib/arjdbc/abstract/statement_cache.rb
activerecord-jdbc-alt-adapter-52.2.0-java lib/arjdbc/abstract/statement_cache.rb
activerecord-jdbc-adapter-52.2-java lib/arjdbc/abstract/statement_cache.rb