class Upsert::ColumnDefinition::Postgresql

@private

Constants

HSTORE_DETECTOR

NOTE not using this because it can't be indexed def equality(left, right)

"#{left} IS NOT DISTINCT FROM #{right}"

end

Public Class Methods

all(connection, quoted_table_name) click to toggle source

activerecord-3.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb#column_definitions

# File lib/upsert/column_definition/postgresql.rb, line 7
        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
new(*) click to toggle source
Calls superclass method Upsert::ColumnDefinition::new
# File lib/upsert/column_definition/postgresql.rb, line 30
def initialize(*)
  super
  @hstore_query = !!(sql_type =~ HSTORE_DETECTOR)
end

Public Instance Methods

arg_type() click to toggle source
Calls superclass method Upsert::ColumnDefinition#arg_type
# File lib/upsert/column_definition/postgresql.rb, line 39
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
hstore?() click to toggle source
# File lib/upsert/column_definition/postgresql.rb, line 35
def hstore?
  @hstore_query
end
to_setter() click to toggle source
Calls superclass method Upsert::ColumnDefinition#to_setter
# File lib/upsert/column_definition/postgresql.rb, line 56
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
to_setter_value() click to toggle source
# File lib/upsert/column_definition/postgresql.rb, line 48
def to_setter_value
  if hstore?
    "#{quoted_setter_name}::hstore"
  else
    super
  end
end