Class: ActiveRecord::ConnectionAdapters::PostgreSQLGeometryColumnDefinition
- Inherits:
-
ColumnDefinition
- Object
- ColumnDefinition
- ActiveRecord::ConnectionAdapters::PostgreSQLGeometryColumnDefinition
- Defined in:
- lib/active_record/postgresql_extensions/geometry.rb
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Returns the value of attribute base.
-
#column_name ⇒ Object
readonly
Returns the value of attribute column_name.
-
#default ⇒ Object
Returns the value of attribute default.
-
#null ⇒ Object
Returns the value of attribute null.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #geometry_column_index(table_name) ⇒ Object
- #geometry_columns_entry(table_name) ⇒ Object
-
#initialize(base, column_name, opts) ⇒ PostgreSQLGeometryColumnDefinition
constructor
A new instance of PostgreSQLGeometryColumnDefinition.
- #table_constraints ⇒ Object
- #to_sql ⇒ Object
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 [:ndims].blank? [:ndims] = if [:geometry_type].to_s.upcase =~ /M$/ 3 else 2 end end assert_valid_spatial_column_type([:spatial_column_type]) assert_valid_geometry_type([:geometry_type]) assert_valid_ndims([:ndims], [:geometry_type]) column_type = if ActiveRecord::PostgreSQLExtensions::PostGIS.VERSION[:lib] < '2.0' [:spatial_column_type] else column_args = [ [:geometry_type].to_s.upcase ] if ![ 0, -1 ].include?([:srid]) column_args << [:srid] end "#{[:spatial_column_type]}(#{column_args.join(', ')})" end super(base, column_name, column_type) @default = [:default] @null = [:null] if [:add_constraints] && ( ActiveRecord::PostgreSQLExtensions::PostGIS.VERSION[:lib] < '2.0' || [:force_constraints] ) table_constraints << PostgreSQLCheckConstraint.new( base, "ST_srid(#{base.quote_column_name(column_name)}) = (#{[:srid].to_i})", :name => "enforce_srid_#{column_name}" ) table_constraints << PostgreSQLCheckConstraint.new( base, "ST_ndims(#{base.quote_column_name(column_name)}) = #{[:ndims].to_i}", :name => "enforce_dims_#{column_name}" ) if [:geometry_type].to_s.upcase != 'GEOMETRY' table_constraints << PostgreSQLCheckConstraint.new( base, "geometrytype(#{base.quote_column_name(column_name)}) = '#{[: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
#base ⇒ Object (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_name ⇒ Object (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 |
#default ⇒ Object
Returns the value of attribute default.
49 50 51 |
# File 'lib/active_record/postgresql_extensions/geometry.rb', line 49 def default @default end |
#null ⇒ Object
Returns the value of attribute null.
49 50 51 |
# File 'lib/active_record/postgresql_extensions/geometry.rb', line 49 def null @null end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
48 49 50 |
# File 'lib/active_record/postgresql_extensions/geometry.rb', line 48 def @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 [:create_gist_index] current_scoped_schema, current_table_name = extract_schema_and_table_names(table_name) index_name = if [:create_gist_index].is_a?(String) [: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 [:add_geometry_columns_entry] && [: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), [:ndims].to_i, [:srid].to_i, base.quote([:geometry_type].to_s.upcase) ) ] end |
#table_constraints ⇒ Object
181 182 183 |
# File 'lib/active_record/postgresql_extensions/geometry.rb', line 181 def table_constraints @table_constraints ||= [] end |
#to_sql ⇒ Object
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}" = {} [:null] = null unless null.nil? [:default] = default unless default.nil? (column_sql, ) unless type.to_sym == :primary_key column_sql end |