Sha256: ff8371fdb3c8c0ebba432a39fa2efa7da2eae658c824a6df15e7a232203d27b7

Contents?: true

Size: 1.95 KB

Versions: 19

Compression:

Stored size: 1.95 KB

Contents

module ArJdbc
  module Util
    # Caches table and column name (quoted) outcomes.
    # Uses {ThreadSafe::Cache} as a concurrent lock free (on JRuby) cache backend.
    # The thread_safe gem is a dependency since ActiveSupport 4.0, when using
    # ActiveRecord <= 3.2 one should add `gem 'thread_safe'` into the *Gemfile*
    # as it is not forced (currently) as an explicit gem dependency.
    #
    # Caching can also be disabled by setting the *arjdbc.quoted_cache.disabled*
    # system property = 'true'.
    module QuotedCache

      # @private
      DISABLED = Java::JavaLang::Boolean.getBoolean('arjdbc.quoted_cache.disabled')

      def self.included(base)
        # the thread_safe gem is an ActiveSupport dependency (since 4.0) :
        begin; require 'thread_safe'; rescue LoadError; end unless DISABLED
        if ! DISABLED && defined? ThreadSafe::Cache
          base.const_set :QUOTED_TABLE_NAMES, ThreadSafe::Cache.new
          base.const_set :QUOTED_COLUMN_NAMES, ThreadSafe::Cache.new
        else
          base.const_set :QUOTED_TABLE_NAMES, nil
          base.const_set :QUOTED_COLUMN_NAMES, nil
        end
      end

      # Caches quoted table names, the cache is stored in the class'
      # `QUOTED_TABLE_NAMES` constant.
      # @return [String]
      def quote_table_name(name)
        if cache = self.class::QUOTED_TABLE_NAMES
          unless quoted = cache[name]
            quoted = super
            cache.put_if_absent name, quoted.freeze
          end
          quoted
        else
          super
        end
      end

      # Caches quoted table names, the cache is stored in the class'
      # `QUOTED_COLUMN_NAMES` constant.
      # @return [String]
      def quote_column_name(name)
        if cache = self.class::QUOTED_COLUMN_NAMES
          unless quoted = cache[name]
            quoted = super
            cache.put_if_absent name, quoted.freeze
          end
          quoted
        else
          super
        end
      end

    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
activerecord-jdbc-adapter-1.3.17 lib/arjdbc/util/quoted_cache.rb
activerecord-jdbc-adapter-1.3.16 lib/arjdbc/util/quoted_cache.rb
activerecord-jdbc-adapter-1.3.15 lib/arjdbc/util/quoted_cache.rb
activerecord-jdbc-adapter-1.3.14 lib/arjdbc/util/quoted_cache.rb
activerecord-jdbc-adapter-1.3.13 lib/arjdbc/util/quoted_cache.rb
activerecord-jdbc-adapter-1.3.12 lib/arjdbc/util/quoted_cache.rb
activerecord-jdbc-adapter-1.3.11 lib/arjdbc/util/quoted_cache.rb
activerecord-jdbc-adapter-1.3.10 lib/arjdbc/util/quoted_cache.rb
activerecord-jdbc-adapter-1.3.9 lib/arjdbc/util/quoted_cache.rb
activerecord-jdbc-adapter-1.3.8 lib/arjdbc/util/quoted_cache.rb
activerecord-jdbc-adapter-1.3.7 lib/arjdbc/util/quoted_cache.rb
activerecord-jdbc-adapter-1.3.6 lib/arjdbc/util/quoted_cache.rb
activerecord-jdbc-adapter-1.3.5 lib/arjdbc/util/quoted_cache.rb
activerecord-jdbc-adapter-1.3.4 lib/arjdbc/util/quoted_cache.rb
activerecord-jdbc-adapter-1.3.3 lib/arjdbc/util/quoted_cache.rb
activerecord-jdbc-adapter-1.3.2 lib/arjdbc/util/quoted_cache.rb
activerecord-jdbc-adapter-1.3.1 lib/arjdbc/util/quoted_cache.rb
activerecord-jdbc-adapter-1.3.0 lib/arjdbc/util/quoted_cache.rb
activerecord-jdbc-adapter-1.3.0.rc1 lib/arjdbc/util/quoted_cache.rb