Sha256: 3994988615ff57d3ca57e15244632e3668b2b74c997f14880a559c43ef972220

Contents?: true

Size: 1.5 KB

Versions: 8

Compression:

Stored size: 1.5 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, 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 = '#{connection.quote_ident(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

      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
          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

8 entries across 8 versions & 1 rubygems

Version Path
upsert-2.0.1 lib/upsert/column_definition/postgresql.rb
upsert-2.0.0 lib/upsert/column_definition/postgresql.rb
upsert-1.2.0 lib/upsert/column_definition/postgresql.rb
upsert-1.1.7 lib/upsert/column_definition/postgresql.rb
upsert-1.1.6 lib/upsert/column_definition/postgresql.rb
upsert-1.1.5 lib/upsert/column_definition/postgresql.rb
upsert-1.1.4 lib/upsert/column_definition/postgresql.rb
upsert-1.1.3 lib/upsert/column_definition/postgresql.rb