Class: ActiveRecord::ConnectionAdapters::PostgreSQLGeometryColumnDefinition

Inherits:
ColumnDefinition
  • Object
show all
Defined in:
lib/active_record/postgresql_extensions/geometry.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, column_name, opts) ⇒ PostgreSQLGeometryColumnDefinition

Returns a new instance of PostgreSQLGeometryColumnDefinition.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/active_record/postgresql_extensions/geometry.rb', line 51

def initialize(base, column_name, opts)
  @base = base
  @column_name = column_name

  @options = {
    :spatial_column_type => :geometry,
    :geometry_type => :geometry,
    :add_constraints => true,
    :force_constraints => false,
    :add_geometry_columns_entry => true,
    :create_gist_index => true,
    :srid => ActiveRecord::PostgreSQLExtensions::PostGIS.UNKNOWN_SRID
  }.merge(opts)

  if options[:ndims].blank?
    options[:ndims] = if options[:geometry_type].to_s.upcase =~ /M$/
      3
    else
      2
    end
  end

  assert_valid_spatial_column_type(options[:spatial_column_type])
  assert_valid_geometry_type(options[:geometry_type])
  assert_valid_ndims(options[:ndims], options[:geometry_type])

  column_type = if ActiveRecord::PostgreSQLExtensions::PostGIS.VERSION[:lib] < '2.0'
    options[:spatial_column_type]
  else
    column_args = [ options[:geometry_type].to_s.upcase ]

    if ![ 0, -1 ].include?(options[:srid])
      column_args << options[:srid]
    end

    "#{options[:spatial_column_type]}(#{column_args.join(', ')})"
  end

  super(base, column_name, column_type)

  @default = options[:default]
  @null = options[:null]

  if options[:add_constraints] && (
    ActiveRecord::PostgreSQLExtensions::PostGIS.VERSION[:lib] < '2.0' ||
    options[:force_constraints]
  )
    table_constraints << PostgreSQLCheckConstraint.new(
      base,
      "ST_srid(#{base.quote_column_name(column_name)}) = (#{options[:srid].to_i})",
      :name => "enforce_srid_#{column_name}"
    )

    table_constraints << PostgreSQLCheckConstraint.new(
      base,
      "ST_ndims(#{base.quote_column_name(column_name)}) = #{options[:ndims].to_i}",
      :name => "enforce_dims_#{column_name}"
    )

    if options[:geometry_type].to_s.upcase != 'GEOMETRY'
      table_constraints << PostgreSQLCheckConstraint.new(
        base,
        "geometrytype(#{base.quote_column_name(column_name)}) = '#{options[:geometry_type].to_s.upcase}'::text OR #{base.quote_column_name(column_name)} IS NULL",
        :name => "enforce_geotype_#{column_name}"
      )
    end
  end
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



48
49
50
# File 'lib/active_record/postgresql_extensions/geometry.rb', line 48

def base
  @base
end

#column_nameObject (readonly)

Returns the value of attribute column_name.



48
49
50
# File 'lib/active_record/postgresql_extensions/geometry.rb', line 48

def column_name
  @column_name
end

#defaultObject

Returns the value of attribute default.



49
50
51
# File 'lib/active_record/postgresql_extensions/geometry.rb', line 49

def default
  @default
end

#nullObject

Returns the value of attribute null.



49
50
51
# File 'lib/active_record/postgresql_extensions/geometry.rb', line 49

def null
  @null
end

#optionsObject (readonly)

Returns the value of attribute options.



48
49
50
# File 'lib/active_record/postgresql_extensions/geometry.rb', line 48

def options
  @options
end

Instance Method Details

#geometry_column_index(table_name) ⇒ Object



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/active_record/postgresql_extensions/geometry.rb', line 150

def geometry_column_index(table_name)
  return [] unless options[:create_gist_index]

  current_scoped_schema, current_table_name = extract_schema_and_table_names(table_name)

  index_name = if options[:create_gist_index].is_a?(String)
    options[:create_gist_index]
  else
    "#{current_table_name}_#{column_name}_gist_index"
  end

  [
    PostgreSQLIndexDefinition.new(
      base,
      index_name,
      { current_scoped_schema => current_table_name },
      column_name,
      :using => :gist
    ).to_s
  ]
end

#geometry_columns_entry(table_name) ⇒ Object



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/active_record/postgresql_extensions/geometry.rb', line 120

def geometry_columns_entry(table_name)
  return [] unless options[:add_geometry_columns_entry] &&
    options[:spatial_column_type].to_s != 'geography' &&
    ActiveRecord::PostgreSQLExtensions::PostGIS.VERSION[:lib] < '2.0'

  current_scoped_schema, current_table_name = extract_schema_and_table_names(table_name)

  [
    sprintf(
      "DELETE FROM \"geometry_columns\" WHERE f_table_catalog = '' AND " +
      "f_table_schema = %s AND " +
      "f_table_name = %s AND " +
      "f_geometry_column = %s;",
      base.quote(current_scoped_schema.to_s),
      base.quote(current_table_name.to_s),
      base.quote(column_name.to_s)
    ),

    sprintf(
      "INSERT INTO \"geometry_columns\" VALUES ('', %s, %s, %s, %d, %d, %s);",
      base.quote(current_scoped_schema.to_s),
      base.quote(current_table_name.to_s),
      base.quote(column_name.to_s),
      options[:ndims].to_i,
      options[:srid].to_i,
      base.quote(options[:geometry_type].to_s.upcase)
    )
  ]
end

#table_constraintsObject



181
182
183
# File 'lib/active_record/postgresql_extensions/geometry.rb', line 181

def table_constraints
  @table_constraints ||= []
end

#to_sqlObject



172
173
174
175
176
177
178
179
# File 'lib/active_record/postgresql_extensions/geometry.rb', line 172

def to_sql
  column_sql = "#{base.quote_column_name(name)} #{sql_type}"
  column_options = {}
  column_options[:null] = null unless null.nil?
  column_options[:default] = default unless default.nil?
  add_column_options!(column_sql, column_options) unless type.to_sym == :primary_key
  column_sql
end