Class: ActiveRecord::ConnectionAdapters::PostgreSQLColumnDefinition

Inherits:
ColumnDefinition
  • Object
show all
Defined in:
lib/postgis_adapter.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) ⇒ PostgreSQLColumnDefinition

Returns a new instance of PostgreSQLColumnDefinition.



351
352
353
354
355
356
357
# File 'lib/postgis_adapter.rb', line 351

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

Instance Attribute Details

#spatialObject (readonly)

Returns the value of attribute spatial.



349
350
351
# File 'lib/postgis_adapter.rb', line 349

def spatial
  @spatial
end

#sridObject

Returns the value of attribute srid.



348
349
350
# File 'lib/postgis_adapter.rb', line 348

def srid
  @srid
end

#with_mObject

Returns the value of attribute with_m.



348
349
350
# File 'lib/postgis_adapter.rb', line 348

def with_m
  @with_m
end

#with_zObject

Returns the value of attribute with_z.



348
349
350
# File 'lib/postgis_adapter.rb', line 348

def with_z
  @with_z
end

Instance Method Details

#to_sql(table_name) ⇒ Object



359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
# File 'lib/postgis_adapter.rb', line 359

def to_sql(table_name)
  if @spatial
    type_sql = type_to_sql(type.to_sym)
    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