Sha256: 2010b6e093be8825ca496df9db73de02c9feab02af6f308e493b2c38e24e4ed4

Contents?: true

Size: 1.91 KB

Versions: 5

Compression:

Stored size: 1.91 KB

Contents

class Upsert
  module PG_Connection
    # @private
    # activerecord-3.2.5/lib/active_record/connection_adapters/postgresql_adapter.rb#column_definitions
    class ColumnDefinition
      class << self
        def auto_increment_primary_key(connection, table_name)
          res = connection.exec <<-EOS
SELECT attr.attname, seq.relname
FROM pg_class      seq,
   pg_attribute  attr,
   pg_depend     dep,
   pg_namespace  name,
   pg_constraint cons
WHERE seq.oid           = dep.objid
AND seq.relkind       = 'S'
AND attr.attrelid     = dep.refobjid
AND attr.attnum       = dep.refobjsubid
AND attr.attrelid     = cons.conrelid
AND attr.attnum       = cons.conkey[1]
AND cons.contype      = 'p'
AND dep.refobjid      = '#{connection.quote_ident(table_name.to_s)}'::regclass
EOS
          if hit = res.first
            hit['attname']
          end
        end

        def all(connection, table_name)
          auto_increment_primary_key = auto_increment_primary_key(connection, table_name)
          res = connection.exec <<-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.to_s)}'::regclass
 AND a.attnum > 0 AND NOT a.attisdropped
ORDER BY a.attnum
EOS
          res.reject do |row|
            row['name'] == auto_increment_primary_key
          end.map do |row|
            new connection, row['name'], row['sql_type'], row['default']
          end
        end
      end

      attr_reader :name
      attr_reader :input_name
      attr_reader :sql_type
      attr_reader :default
      
      def initialize(connection, raw_name, sql_type, default)
        @name = connection.quote_ident raw_name
        @input_name = connection.quote_ident "#{raw_name}_input"
        @sql_type = sql_type
        @default = default
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
upsert-0.3.3 lib/upsert/pg_connection/column_definition.rb
upsert-0.3.2 lib/upsert/pg_connection/column_definition.rb
upsert-0.3.1 lib/upsert/pg_connection/column_definition.rb
upsert-0.3.0 lib/upsert/pg_connection/column_definition.rb
upsert-0.2.2 lib/upsert/pg_connection/column_definition.rb