/
main_adapter.rb
167 lines (132 loc) · 6 KB
/
main_adapter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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
119
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# -----------------------------------------------------------------------------
#
# Mysql2Spatial adapter for ActiveRecord
#
# -----------------------------------------------------------------------------
# Copyright 2010 Daniel Azuma
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of the copyright holder, nor the names of any other
# contributors to this software, may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# -----------------------------------------------------------------------------
# :stopdoc:
module ActiveRecord
module ConnectionAdapters
module Mysql2SpatialAdapter
class MainAdapter < ConnectionAdapters::Mysql2Adapter
NATIVE_DATABASE_TYPES = Mysql2Adapter::NATIVE_DATABASE_TYPES.merge(spatial: { name: 'geometry' })
def initialize(*args_)
super
# Rails 3.2 way of defining the visitor: do so in the constructor
if defined?(@visitor) && @visitor
@visitor = ::Arel::Visitors::MySQL2Spatial.new(self)
end
end
def set_rgeo_factory_settings(factory_settings_)
@rgeo_factory_settings = factory_settings_
end
def adapter_name
Mysql2SpatialAdapter::ADAPTER_NAME
end
def spatial_column_constructor(name_)
::RGeo::ActiveRecord::DEFAULT_SPATIAL_COLUMN_CONSTRUCTORS[name_]
end
def native_database_types
NATIVE_DATABASE_TYPES
end
def quote(value_, column_=nil)
if ::RGeo::Feature::Geometry.check_type(value_)
"GeomFromWKB(0x#{::RGeo::WKRep::WKBGenerator.new(:hex_format => true).generate(value_)},#{value_.srid})"
else
super
end
end
def type_to_sql(type_, limit_=nil, precision_=nil, scale_=nil)
if (info_ = spatial_column_constructor(type_.to_sym))
type_ = limit_[:type] || type_ if limit_.is_a?(::Hash)
type_ = 'geometry' if type_.to_s == 'spatial'
type_ = type_.to_s.gsub('_', '').upcase
end
super(type_, limit_, precision_, scale_)
end
def add_index(table_name_, column_name_, options_={})
if options_[:spatial]
index_name_ = index_name(table_name_, :column => Array(column_name_))
if ::Hash === options_
index_name_ = options_[:name] || index_name_
end
execute "CREATE SPATIAL INDEX #{index_name_} ON #{table_name_} (#{Array(column_name_).join(", ")})"
else
super
end
end
def columns(table_name_, name_=nil)
result_ = @connection.query "SHOW FULL FIELDS FROM #{quote_table_name(table_name_)}"
columns_ = []
result_.each(:symbolize_keys => true, :as => :hash) do |field_|
columns_ << SpatialColumn.new(@rgeo_factory_settings, table_name_.to_s,
field_[:Field], field_[:Default], lookup_cast_type(field_[:Type]), field_[:Type], field_[:Null] == "YES", field_[:Collation], field_[:Extra])
end
columns_
end
# Returns an array of indexes for the given table.
def indexes(table_name_, name_=nil)
indexes_ = []
current_index_ = nil
result_ = execute("SHOW KEYS FROM #{quote_table_name(table_name_)}", name_)
result_.each(:symbolize_keys => true, :as => :hash) do |row_|
if current_index_ != row_[:Key_name]
next if row_[:Key_name] == 'PRIMARY' # skip the primary key
current_index_ = row_[:Key_name]
mysql_index_type = row_[:Index_type].downcase.to_sym
index_type = INDEX_TYPES.include?(mysql_index_type) ? mysql_index_type : nil
index_using = INDEX_USINGS.include?(mysql_index_type) ? mysql_index_type : nil
options = [row_[:Table], row_[:Key_name], row_[:Non_unique].to_i == 0, [], [], nil, nil, index_type, index_using]
indexes_ << if mysql_index_type == :spatial
options.push(true)
::RGeo::ActiveRecord::SpatialIndexDefinition.new(*options)
else
IndexDefinition.new(*options)
end
end
last_index_ = indexes_.last
last_index_.columns << row_[:Column_name]
last_index_.lengths << row_[:Sub_part] unless mysql_index_type == :spatial
end
indexes_
end
protected
def initialize_type_map(m)
super
register_class_with_limit m, %r(geometry)i, Type::Spatial
m.alias_type %r(point)i, 'geometry'
m.alias_type %r(linestring)i, 'geometry'
m.alias_type %r(polygon)i, 'geometry'
end
end
end
end
end
# :startdoc: