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.



386
387
388
389
390
391
392
# File 'lib/postgis_adapter.rb', line 386

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.



384
385
386
# File 'lib/postgis_adapter.rb', line 384

def spatial
  @spatial
end

#sridObject

Returns the value of attribute srid.



383
384
385
# File 'lib/postgis_adapter.rb', line 383

def srid
  @srid
end

#with_mObject

Returns the value of attribute with_m.



383
384
385
# File 'lib/postgis_adapter.rb', line 383

def with_m
  @with_m
end

#with_zObject

Returns the value of attribute with_z.



383
384
385
# File 'lib/postgis_adapter.rb', line 383

def with_z
  @with_z
end

Instance Method Details

#to_sql(table_name) ⇒ Object



394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
# File 'lib/postgis_adapter.rb', line 394

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