Class: ActiveRecord::ConnectionAdapters::PostgreSQLColumnDefinition

Inherits:
ColumnDefinition
  • Object
show all
Defined in:
lib/spatial_adapter/postgresql.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base = nil, name = nil, type = nil, limit = nil, default = nil, null = nil, srid = -1,, with_z = false, with_m = false, geographic = false) ⇒ PostgreSQLColumnDefinition

Returns a new instance of PostgreSQLColumnDefinition.



279
280
281
282
283
284
285
286
287
# File 'lib/spatial_adapter/postgresql.rb', line 279

def initialize(base = nil, name = nil, type=nil, limit=nil, default=nil, null=nil, srid=-1, with_z=false, with_m=false, geographic=false)
  super(base, name, type, limit, default, null)
  @table_name = nil
  @spatial = true
  @srid = srid
  @with_z = with_z
  @with_m = with_m
  @geographic = geographic
end

Instance Attribute Details

#geographicObject

Returns the value of attribute geographic.



276
277
278
# File 'lib/spatial_adapter/postgresql.rb', line 276

def geographic
  @geographic
end

#spatialObject (readonly)

Returns the value of attribute spatial.



277
278
279
# File 'lib/spatial_adapter/postgresql.rb', line 277

def spatial
  @spatial
end

#sridObject

Returns the value of attribute srid.



276
277
278
# File 'lib/spatial_adapter/postgresql.rb', line 276

def srid
  @srid
end

#table_nameObject

Returns the value of attribute table_name.



275
276
277
# File 'lib/spatial_adapter/postgresql.rb', line 275

def table_name
  @table_name
end

#with_mObject

Returns the value of attribute with_m.



276
277
278
# File 'lib/spatial_adapter/postgresql.rb', line 276

def with_m
  @with_m
end

#with_zObject

Returns the value of attribute with_z.



276
277
278
# File 'lib/spatial_adapter/postgresql.rb', line 276

def with_z
  @with_z
end

Instance Method Details

#sql_typeObject



289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/spatial_adapter/postgresql.rb', line 289

def sql_type
  if geographic
    type_sql = base.geometry_data_types[type.to_sym][:name]
    type_sql += "Z" if with_z
    type_sql += "M" if with_m
    # SRID is not yet supported (defaults to 4326)
    #type_sql += ", #{srid}" if (srid && srid != -1)
    type_sql = "geography(#{type_sql})"
    type_sql
  else
    super
  end
end

#to_sqlObject



303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/spatial_adapter/postgresql.rb', line 303

def to_sql
  if spatial && !geographic
    type_sql = base.geometry_data_types[type.to_sym][:name]
    type_sql += "M" if with_m and !with_z
    if with_m and with_z
      dimension = 4 
    elsif with_m or with_z
      dimension = 3
    else
      dimension = 2
    end
  
    column_sql = "SELECT AddGeometryColumn('#{table_name}','#{name}',#{srid},'#{type_sql}',#{dimension})"
    column_sql += ";ALTER TABLE #{table_name} ALTER #{name} SET NOT NULL" if null == false
    column_sql
  else
    super
  end
end