Sha256: 56438d91b7337634c033953859833f87a1bc01597363dda15b7ea914a8f11c67

Contents?: true

Size: 1.98 KB

Versions: 5

Compression:

Stored size: 1.98 KB

Contents

# frozen_string_literal: true

require_relative 'adapter/database_statements'
require_relative 'adapter/oid'
require_relative 'adapter/quoting'
require_relative 'adapter/schema_creation'
require_relative 'adapter/schema_definitions'
require_relative 'adapter/schema_dumper'
require_relative 'adapter/schema_statements'

module Torque
  module PostgreSQL
    module Adapter
      include Quoting
      include DatabaseStatements
      include SchemaStatements

      # :nodoc:
      class DeduplicatableArray < ::Array
        def deduplicate
          map { |value| -value }
        end

        alias :-@ :deduplicate
      end

      # Get the current PostgreSQL version as a Gem Version.
      def version
        @version ||= Gem::Version.new(
          select_value('SELECT version()').match(/#{Adapter::ADAPTER_NAME} ([\d\.]+)/)[1]
        )
      end

      # Add `inherits` to the list of extracted table options
      def extract_table_options!(options)
        super.merge(options.extract!(:inherits))
      end

      # Allow filtered bulk insert by adding the where clause. This method is
      # only used by +InsertAll+, so it somewhat safe to override it
      def build_insert_sql(insert)
        super.tap do |sql|
          if insert.update_duplicates? && insert.where_condition?
            if insert.returning
              sql.sub!(' RETURNING ', " WHERE #{insert.where} RETURNING ")
            else
              sql << " WHERE #{insert.where}"
            end
          end
        end
      end

      # Extend the extract default value to support array
      def extract_value_from_default(default)
        return super unless Torque::PostgreSQL.config.use_extended_defaults
        return super unless default&.match(/ARRAY\[(.*?)\](?:::"?([\w. ]+)"?(?:\[\])+)?$/)

        arr = $1.split(/(?!\B\[[^\]]*), ?(?![^\[]*\]\B)/)
        DeduplicatableArray.new(arr.map(&method(:extract_value_from_default)))
      end
    end

    ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend Adapter
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
torque-postgresql-2.2.4 lib/torque/postgresql/adapter.rb
torque-postgresql-2.2.3 lib/torque/postgresql/adapter.rb
torque-postgresql-2.2.2 lib/torque/postgresql/adapter.rb
torque-postgresql-2.2.1 lib/torque/postgresql/adapter.rb
torque-postgresql-2.2.0 lib/torque/postgresql/adapter.rb