forked from pat/thinking-sphinx
/
abstract_adapter.rb
87 lines (77 loc) · 2.22 KB
/
abstract_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
module ThinkingSphinx
class AbstractAdapter
def initialize(model)
@model = model
end
def setup
# Deliberately blank - subclasses should do something though. Well, if
# they need to.
end
def self.detect(model)
adapter = adapter_for_model model
case adapter
when :mysql
ThinkingSphinx::MysqlAdapter.new model
when :postgresql
ThinkingSphinx::PostgreSQLAdapter.new model
when Class
adapter.new model
else
raise "Invalid Database Adapter: Sphinx only supports MySQL and PostgreSQL, not #{adapter}"
end
end
def self.adapter_for_model(model)
case ThinkingSphinx.database_adapter
when String
ThinkingSphinx.database_adapter.to_sym
when NilClass
standard_adapter_for_model model
when Proc
ThinkingSphinx.database_adapter.call model
else
ThinkingSphinx.database_adapter
end
end
def self.standard_adapter_for_model(model)
case model.connection.class.name
when "ActiveRecord::ConnectionAdapters::MysqlAdapter",
"ActiveRecord::ConnectionAdapters::MysqlplusAdapter",
"ActiveRecord::ConnectionAdapters::Mysql2Adapter",
"ActiveRecord::ConnectionAdapters::NullDBAdapter"
:mysql
when "ActiveRecord::ConnectionAdapters::PostgreSQLAdapter"
:postgresql
when "ActiveRecord::ConnectionAdapters::JdbcAdapter"
case model.connection.config[:adapter]
when "jdbcmysql"
:mysql
when "jdbcpostgresql"
:postgresql
else
model.connection.config[:adapter].to_sym
end
else
model.connection.class.name
end
end
def quote_with_table(column)
"#{@model.quoted_table_name}.#{@model.connection.quote_column_name(column)}"
end
def bigint_pattern
/bigint/i
end
def downcase(clause)
"LOWER(#{clause})"
end
def case(expression, pairs, default)
"CASE #{expression} " +
pairs.keys.inject('') { |string, key|
string + "WHEN '#{key}' THEN #{pairs[key]} "
} + "ELSE #{default} END"
end
protected
def connection
@connection ||= @model.connection
end
end
end