spatial_adapter 1.2.0 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/.gitignore +3 -0
  2. data/Gemfile +3 -0
  3. data/README.rdoc +30 -17
  4. data/Rakefile +11 -0
  5. data/init.rb +1 -0
  6. data/lib/spatial_adapter/base/mysql/adapter.rb +54 -0
  7. data/lib/spatial_adapter/base/mysql/spatial_column.rb +12 -0
  8. data/lib/spatial_adapter/base/mysql.rb +9 -0
  9. data/lib/spatial_adapter/common/schema_dumper.rb +12 -12
  10. data/lib/spatial_adapter/common/spatial_column.rb +4 -4
  11. data/lib/spatial_adapter/common/table_definition.rb +8 -10
  12. data/lib/spatial_adapter/common.rb +10 -0
  13. data/lib/spatial_adapter/jdbcmysql.rb +50 -0
  14. data/lib/spatial_adapter/mysql.rb +43 -85
  15. data/lib/spatial_adapter/mysql2.rb +39 -86
  16. data/lib/spatial_adapter/postgresql.rb +315 -324
  17. data/lib/spatial_adapter/version.rb +3 -0
  18. data/lib/spatial_adapter.rb +3 -7
  19. data/spatial_adapter.gemspec +39 -0
  20. data/spec/db/jdbcmysql_raw.rb +70 -0
  21. data/spec/db/mysql2_raw.rb +9 -9
  22. data/spec/db/mysql_raw.rb +9 -9
  23. data/spec/jdbcmysql_spec.rb +25 -0
  24. data/spec/mysql2_spec.rb +31 -0
  25. data/spec/mysql_spec.rb +17 -0
  26. data/spec/postgresql/connection_adapter_spec.rb +34 -39
  27. data/spec/postgresql/migration_spec.rb +51 -51
  28. data/spec/postgresql/models_spec.rb +37 -37
  29. data/spec/postgresql/schema_dumper_spec.rb +12 -12
  30. data/spec/postgresql_spec.rb +5 -0
  31. data/spec/{shared_examples.rb → shared/common_model_actions_spec.rb} +2 -2
  32. data/spec/shared/mysql_connection_adapter_spec.rb +110 -0
  33. data/spec/{mysql/migration_spec.rb → shared/mysql_migration_spec.rb} +17 -17
  34. data/spec/shared/mysql_models_spec.rb +58 -0
  35. data/spec/{mysql/schema_dumper_spec.rb → shared/mysql_schema_dumper_spec.rb} +24 -27
  36. data/spec/spec_helper.rb +25 -21
  37. metadata +131 -84
  38. data/VERSION +0 -1
  39. data/spec/README.txt +0 -22
  40. data/spec/mysql/connection_adapter_spec.rb +0 -106
  41. data/spec/mysql/models_spec.rb +0 -65
  42. data/spec/mysql2/connection_adapter_spec.rb +0 -106
  43. data/spec/mysql2/migration_spec.rb +0 -64
  44. data/spec/mysql2/models_spec.rb +0 -65
  45. data/spec/mysql2/schema_dumper_spec.rb +0 -56
@@ -1,97 +1,50 @@
1
1
  require 'spatial_adapter'
2
+ require 'spatial_adapter/base/mysql'
2
3
  require 'active_record/connection_adapters/mysql2_adapter'
3
4
 
4
- ActiveRecord::ConnectionAdapters::Mysql2Adapter.class_eval do
5
- include SpatialAdapter
6
-
7
- def supports_geographic?
8
- false
9
- end
10
-
11
- alias :original_native_database_types :native_database_types
12
- def native_database_types
13
- original_native_database_types.merge!(geometry_data_types)
14
- end
15
-
16
- alias :original_quote :quote
17
- #Redefines the quote method to add behaviour for when a Geometry is encountered ; used when binding variables in find_by methods
18
- def quote(value, column = nil)
19
- if value.kind_of?(GeoRuby::SimpleFeatures::Geometry)
20
- "GeomFromWKB(0x#{value.as_hex_wkb},#{value.srid})"
21
- else
22
- original_quote(value,column)
23
- end
24
- end
25
-
26
- #Redefinition of columns to add the information that a column is geometric
27
- def columns(table_name, name = nil)#:nodoc:
28
- sql = "SHOW FIELDS FROM #{quote_table_name(table_name)}"
29
- columns = []
30
- result = execute(sql, name)
31
- result.each do |field|
32
- klass = field[1] =~ /geometry|point|linestring|polygon|multipoint|multilinestring|multipolygon|geometrycollection/i ? ActiveRecord::ConnectionAdapters::SpatialMysql2Column : ActiveRecord::ConnectionAdapters::Mysql2Column
33
- columns << klass.new(field[0], field[4], field[1], field[2] == "YES")
34
- end
35
- columns
36
- end
37
-
38
-
39
- #operations relative to migrations
40
-
41
- #Redefines add_index to support the case where the index is spatial
42
- #If the :spatial key in the options table is true, then the sql string for a spatial index is created
43
- def add_index(table_name,column_name,options = {})
44
- index_name = options[:name] || index_name(table_name,:column => Array(column_name))
45
-
46
- if options[:spatial]
47
- execute "CREATE SPATIAL INDEX #{index_name} ON #{table_name} (#{Array(column_name).join(", ")})"
48
- else
49
- super
50
- end
51
- end
52
-
53
- #Check the nature of the index : If it is SPATIAL, it is indicated in the IndexDefinition object (redefined to add the spatial flag in spatial_adapter_common.rb)
54
- def indexes(table_name, name = nil)#:nodoc:
55
- indexes = []
56
- current_index = nil
57
- (execute("SHOW KEYS FROM #{table_name}", name) || []).each do |row|
58
- if current_index != row[2]
59
- next if row[2] == "PRIMARY" # skip the primary key
60
- current_index = row[2]
61
- indexes << ActiveRecord::ConnectionAdapters::IndexDefinition.new(row[0], row[2], row[1] == "0", [], row[10] == "SPATIAL")
5
+ module ActiveRecord::ConnectionAdapters
6
+ class SpatialMysql2Column < Mysql2Column
7
+ include SpatialAdapter::SpatialColumn
8
+ extend SpatialAdapter::Base::Mysql::SpatialColumn
9
+ end
10
+
11
+ class Mysql2Adapter
12
+ include SpatialAdapter::Base::Mysql::Adapter
13
+
14
+ #Redefinition of columns to add the information that a column is geometric
15
+ def columns(table_name, name = nil)#:nodoc:
16
+ show_fields_from(table_name, name).map do |field|
17
+ klass = \
18
+ if field[1] =~ GEOMETRY_REGEXP
19
+ ActiveRecord::ConnectionAdapters::SpatialMysql2Column
20
+ else
21
+ ActiveRecord::ConnectionAdapters::Mysql2Column
22
+ end
23
+ klass.new(field[0], field[4], field[1], field[2] == "YES")
62
24
  end
63
- indexes.last.columns << row[4]
64
- end
65
- indexes
66
- end
67
-
68
- #Get the table creation options : Only the engine for now. The text encoding could also be parsed and returned here.
69
- def options_for(table)
70
- result = execute("show table status like '#{table}'")
71
- engine = result.first[1]
72
- if engine !~ /inno/i #inno is default so do nothing for it in order not to clutter the migration
73
- "ENGINE=#{engine}"
74
- else
75
- nil
76
25
  end
77
- end
78
- end
79
-
80
26
 
81
- module ActiveRecord
82
- module ConnectionAdapters
83
- class SpatialMysql2Column < Mysql2Column
84
- include SpatialAdapter::SpatialColumn
85
-
86
- #MySql-specific geometry string parsing. By default, MySql returns geometries in strict wkb format with "0" characters in the first 4 positions.
87
- def self.string_to_geometry(string)
88
- return string unless string.is_a?(String)
89
- begin
90
- GeoRuby::SimpleFeatures::Geometry.from_ewkb(string[4..-1])
91
- rescue Exception => exception
92
- nil
27
+ # Check the nature of the index : If it is SPATIAL, it is indicated in the
28
+ # IndexDefinition object (redefined to add the spatial flag in
29
+ # spatial_adapter_common.rb)
30
+ def indexes(table_name, name = nil)#:nodoc:
31
+ indexes = []
32
+ current_index = nil
33
+ show_keys_from(table_name, name).each do |row|
34
+ if current_index != row[2]
35
+ next if row[2] == "PRIMARY" # skip the primary key
36
+ current_index = row[2]
37
+ indexes << ActiveRecord::ConnectionAdapters::IndexDefinition \
38
+ .new(row[0], row[2], row[1] == "0", [], row[10] == "SPATIAL")
93
39
  end
40
+ indexes.last.columns << row[4]
94
41
  end
42
+ indexes
43
+ end
44
+
45
+ def options_for(table)
46
+ engine = show_table_status_like(table).first[1]
47
+ engine !~ /inno/i ? "ENGINE=#{engine}" : nil
95
48
  end
96
49
  end
97
50
  end