Sha256: c0ea262fae0d4c316a1acad424a0e85d554297b3a54f941f1f359a5c856e743d

Contents?: true

Size: 1.79 KB

Versions: 4

Compression:

Stored size: 1.79 KB

Contents

class Upsert
  class ColumnDefinition
    # @private
    class Postgresql < ColumnDefinition
      class << self
        # activerecord-3.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb#column_definitions
        def all(connection, quoted_table_name)
          res = connection.execute <<-EOS
  SELECT a.attname AS name, format_type(a.atttypid, a.atttypmod) AS sql_type, d.adsrc AS default
  FROM pg_attribute a LEFT JOIN pg_attrdef d
  ON a.attrelid = d.adrelid AND a.attnum = d.adnum
  WHERE a.attrelid = '#{quoted_table_name}'::regclass
  AND a.attnum > 0 AND NOT a.attisdropped
  EOS
          res.map do |row|
            new connection, row['name'], row['sql_type'], row['default']
          end.sort_by do |cd|
            cd.name
          end
        end
      end

      # NOTE not using this because it can't be indexed
      # def equality(left, right)
      #   "#{left} IS NOT DISTINCT FROM #{right}"
      # end

      HSTORE_DETECTOR = /hstore/i

      def initialize(*)
        super
        @hstore_query = !!(sql_type =~ HSTORE_DETECTOR)
      end

      def hstore?
        @hstore_query
      end

      def arg_type
        if hstore?
          'text'
        else
          # JDBC uses prepared statements and properly sends date objects (which are otherwise escaped)
          RUBY_PLATFORM == "java" ? sql_type : super
        end
      end

      def to_setter_value
        if hstore?
          "#{quoted_setter_name}::hstore"
        else
          super
        end
      end

      def to_setter
        if hstore?
          # http://stackoverflow.com/questions/9317971/adding-a-key-to-an-empty-hstore-column
          "#{quoted_name} = COALESCE(#{quoted_name}, hstore(array[]::varchar[])) || #{to_setter_value}"
        else
          super
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
upsert-2.9.10-java lib/upsert/column_definition/postgresql.rb
upsert-2.9.10 lib/upsert/column_definition/postgresql.rb
upsert-2.9.9-universal-java-11 lib/upsert/column_definition/postgresql.rb
upsert-2.9.9 lib/upsert/column_definition/postgresql.rb