aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/postgresql/column.rb
blob: a579746815f15e729e33ee1f6fbbf3ce41f68c6f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
require 'active_record/connection_adapters/postgresql/cast'

module ActiveRecord
  module ConnectionAdapters
    # PostgreSQL-specific extensions to column definitions in a table.
    class PostgreSQLColumn < Column #:nodoc:
      attr_accessor :array

      def initialize(name, default, cast_type, sql_type = nil, null = true, default_function = nil)
        if sql_type =~ /\[\]$/
          @array = true
          super(name, default, cast_type, sql_type[0..sql_type.length - 3], null)
        else
          @array = false
          super(name, default, cast_type, sql_type, null)
        end

        @default_function = default_function
      end

      # :stopdoc:
      class << self
        include PostgreSQL::Cast

        # Loads pg_array_parser if available. String parsing can be
        # performed quicker by a native extension, which will not create
        # a large amount of Ruby objects that will need to be garbage
        # collected. pg_array_parser has a C and Java extension
        begin
          require 'pg_array_parser'
          include PgArrayParser
        rescue LoadError
          require 'active_record/connection_adapters/postgresql/array_parser'
          include PostgreSQL::ArrayParser
        end
      end
      # :startdoc:
    end
  end
end