sayso-thinking-sphinx 2.0.3.001
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENCE +20 -0
- data/README.textile +251 -0
- data/VERSION +1 -0
- data/features/abstract_inheritance.feature +10 -0
- data/features/alternate_primary_key.feature +27 -0
- data/features/attribute_transformation.feature +22 -0
- data/features/attribute_updates.feature +77 -0
- data/features/deleting_instances.feature +67 -0
- data/features/direct_attributes.feature +11 -0
- data/features/excerpts.feature +21 -0
- data/features/extensible_delta_indexing.feature +9 -0
- data/features/facets.feature +88 -0
- data/features/facets_across_model.feature +29 -0
- data/features/field_sorting.feature +18 -0
- data/features/handling_edits.feature +94 -0
- data/features/retry_stale_indexes.feature +24 -0
- data/features/searching_across_models.feature +20 -0
- data/features/searching_by_index.feature +40 -0
- data/features/searching_by_model.feature +168 -0
- data/features/searching_with_find_arguments.feature +56 -0
- data/features/sphinx_detection.feature +25 -0
- data/features/sphinx_scopes.feature +68 -0
- data/features/step_definitions/alpha_steps.rb +16 -0
- data/features/step_definitions/beta_steps.rb +7 -0
- data/features/step_definitions/common_steps.rb +197 -0
- data/features/step_definitions/extensible_delta_indexing_steps.rb +7 -0
- data/features/step_definitions/facet_steps.rb +96 -0
- data/features/step_definitions/find_arguments_steps.rb +36 -0
- data/features/step_definitions/gamma_steps.rb +15 -0
- data/features/step_definitions/scope_steps.rb +19 -0
- data/features/step_definitions/search_steps.rb +94 -0
- data/features/step_definitions/sphinx_steps.rb +35 -0
- data/features/sti_searching.feature +19 -0
- data/features/support/env.rb +27 -0
- data/features/support/lib/generic_delta_handler.rb +8 -0
- data/features/thinking_sphinx/database.example.yml +3 -0
- data/features/thinking_sphinx/db/fixtures/alphas.rb +10 -0
- data/features/thinking_sphinx/db/fixtures/authors.rb +1 -0
- data/features/thinking_sphinx/db/fixtures/betas.rb +11 -0
- data/features/thinking_sphinx/db/fixtures/boxes.rb +9 -0
- data/features/thinking_sphinx/db/fixtures/categories.rb +1 -0
- data/features/thinking_sphinx/db/fixtures/cats.rb +3 -0
- data/features/thinking_sphinx/db/fixtures/comments.rb +24 -0
- data/features/thinking_sphinx/db/fixtures/developers.rb +31 -0
- data/features/thinking_sphinx/db/fixtures/dogs.rb +3 -0
- data/features/thinking_sphinx/db/fixtures/extensible_betas.rb +10 -0
- data/features/thinking_sphinx/db/fixtures/foxes.rb +3 -0
- data/features/thinking_sphinx/db/fixtures/gammas.rb +10 -0
- data/features/thinking_sphinx/db/fixtures/music.rb +4 -0
- data/features/thinking_sphinx/db/fixtures/people.rb +1001 -0
- data/features/thinking_sphinx/db/fixtures/posts.rb +6 -0
- data/features/thinking_sphinx/db/fixtures/robots.rb +14 -0
- data/features/thinking_sphinx/db/fixtures/tags.rb +27 -0
- data/features/thinking_sphinx/db/migrations/create_alphas.rb +8 -0
- data/features/thinking_sphinx/db/migrations/create_animals.rb +5 -0
- data/features/thinking_sphinx/db/migrations/create_authors.rb +3 -0
- data/features/thinking_sphinx/db/migrations/create_authors_posts.rb +6 -0
- data/features/thinking_sphinx/db/migrations/create_betas.rb +5 -0
- data/features/thinking_sphinx/db/migrations/create_boxes.rb +5 -0
- data/features/thinking_sphinx/db/migrations/create_categories.rb +3 -0
- data/features/thinking_sphinx/db/migrations/create_comments.rb +10 -0
- data/features/thinking_sphinx/db/migrations/create_developers.rb +7 -0
- data/features/thinking_sphinx/db/migrations/create_extensible_betas.rb +5 -0
- data/features/thinking_sphinx/db/migrations/create_gammas.rb +3 -0
- data/features/thinking_sphinx/db/migrations/create_genres.rb +3 -0
- data/features/thinking_sphinx/db/migrations/create_music.rb +6 -0
- data/features/thinking_sphinx/db/migrations/create_people.rb +13 -0
- data/features/thinking_sphinx/db/migrations/create_posts.rb +5 -0
- data/features/thinking_sphinx/db/migrations/create_robots.rb +4 -0
- data/features/thinking_sphinx/db/migrations/create_taggings.rb +5 -0
- data/features/thinking_sphinx/db/migrations/create_tags.rb +4 -0
- data/features/thinking_sphinx/models/alpha.rb +23 -0
- data/features/thinking_sphinx/models/andrew.rb +17 -0
- data/features/thinking_sphinx/models/animal.rb +5 -0
- data/features/thinking_sphinx/models/author.rb +3 -0
- data/features/thinking_sphinx/models/beta.rb +13 -0
- data/features/thinking_sphinx/models/box.rb +8 -0
- data/features/thinking_sphinx/models/cat.rb +3 -0
- data/features/thinking_sphinx/models/category.rb +4 -0
- data/features/thinking_sphinx/models/comment.rb +10 -0
- data/features/thinking_sphinx/models/developer.rb +20 -0
- data/features/thinking_sphinx/models/dog.rb +3 -0
- data/features/thinking_sphinx/models/extensible_beta.rb +9 -0
- data/features/thinking_sphinx/models/fox.rb +5 -0
- data/features/thinking_sphinx/models/gamma.rb +5 -0
- data/features/thinking_sphinx/models/genre.rb +3 -0
- data/features/thinking_sphinx/models/medium.rb +5 -0
- data/features/thinking_sphinx/models/music.rb +8 -0
- data/features/thinking_sphinx/models/person.rb +24 -0
- data/features/thinking_sphinx/models/post.rb +21 -0
- data/features/thinking_sphinx/models/robot.rb +12 -0
- data/features/thinking_sphinx/models/tag.rb +3 -0
- data/features/thinking_sphinx/models/tagging.rb +4 -0
- data/lib/cucumber/thinking_sphinx/external_world.rb +12 -0
- data/lib/cucumber/thinking_sphinx/internal_world.rb +127 -0
- data/lib/cucumber/thinking_sphinx/sql_logger.rb +20 -0
- data/lib/thinking-sphinx.rb +1 -0
- data/lib/thinking_sphinx.rb +301 -0
- data/lib/thinking_sphinx/action_controller.rb +31 -0
- data/lib/thinking_sphinx/active_record.rb +352 -0
- data/lib/thinking_sphinx/active_record/attribute_updates.rb +52 -0
- data/lib/thinking_sphinx/active_record/delta.rb +92 -0
- data/lib/thinking_sphinx/active_record/has_many_association.rb +36 -0
- data/lib/thinking_sphinx/active_record/has_many_association_with_scopes.rb +21 -0
- data/lib/thinking_sphinx/active_record/log_subscriber.rb +61 -0
- data/lib/thinking_sphinx/active_record/scopes.rb +93 -0
- data/lib/thinking_sphinx/adapters/abstract_adapter.rb +87 -0
- data/lib/thinking_sphinx/adapters/mysql_adapter.rb +58 -0
- data/lib/thinking_sphinx/adapters/postgresql_adapter.rb +153 -0
- data/lib/thinking_sphinx/association.rb +169 -0
- data/lib/thinking_sphinx/attribute.rb +389 -0
- data/lib/thinking_sphinx/auto_version.rb +38 -0
- data/lib/thinking_sphinx/bundled_search.rb +44 -0
- data/lib/thinking_sphinx/class_facet.rb +16 -0
- data/lib/thinking_sphinx/configuration.rb +355 -0
- data/lib/thinking_sphinx/context.rb +76 -0
- data/lib/thinking_sphinx/core/string.rb +15 -0
- data/lib/thinking_sphinx/deltas.rb +28 -0
- data/lib/thinking_sphinx/deltas/default_delta.rb +62 -0
- data/lib/thinking_sphinx/deploy/capistrano.rb +101 -0
- data/lib/thinking_sphinx/excerpter.rb +23 -0
- data/lib/thinking_sphinx/facet.rb +127 -0
- data/lib/thinking_sphinx/facet_search.rb +166 -0
- data/lib/thinking_sphinx/field.rb +82 -0
- data/lib/thinking_sphinx/index.rb +157 -0
- data/lib/thinking_sphinx/index/builder.rb +312 -0
- data/lib/thinking_sphinx/index/faux_column.rb +118 -0
- data/lib/thinking_sphinx/join.rb +37 -0
- data/lib/thinking_sphinx/property.rb +185 -0
- data/lib/thinking_sphinx/railtie.rb +46 -0
- data/lib/thinking_sphinx/search.rb +950 -0
- data/lib/thinking_sphinx/search_methods.rb +439 -0
- data/lib/thinking_sphinx/source.rb +163 -0
- data/lib/thinking_sphinx/source/internal_properties.rb +46 -0
- data/lib/thinking_sphinx/source/sql.rb +148 -0
- data/lib/thinking_sphinx/tasks.rb +139 -0
- data/lib/thinking_sphinx/test.rb +55 -0
- data/spec/thinking_sphinx/active_record/delta_spec.rb +128 -0
- data/spec/thinking_sphinx/active_record/has_many_association_spec.rb +72 -0
- data/spec/thinking_sphinx/active_record/scopes_spec.rb +176 -0
- data/spec/thinking_sphinx/active_record_spec.rb +576 -0
- data/spec/thinking_sphinx/adapters/abstract_adapter_spec.rb +145 -0
- data/spec/thinking_sphinx/association_spec.rb +216 -0
- data/spec/thinking_sphinx/attribute_spec.rb +560 -0
- data/spec/thinking_sphinx/auto_version_spec.rb +63 -0
- data/spec/thinking_sphinx/configuration_spec.rb +288 -0
- data/spec/thinking_sphinx/context_spec.rb +128 -0
- data/spec/thinking_sphinx/core/array_spec.rb +9 -0
- data/spec/thinking_sphinx/core/string_spec.rb +9 -0
- data/spec/thinking_sphinx/excerpter_spec.rb +49 -0
- data/spec/thinking_sphinx/facet_search_spec.rb +170 -0
- data/spec/thinking_sphinx/facet_spec.rb +359 -0
- data/spec/thinking_sphinx/field_spec.rb +127 -0
- data/spec/thinking_sphinx/index/builder_spec.rb +508 -0
- data/spec/thinking_sphinx/index/faux_column_spec.rb +36 -0
- data/spec/thinking_sphinx/index_spec.rb +183 -0
- data/spec/thinking_sphinx/search_methods_spec.rb +156 -0
- data/spec/thinking_sphinx/search_spec.rb +1387 -0
- data/spec/thinking_sphinx/source_spec.rb +253 -0
- data/spec/thinking_sphinx/test_spec.rb +20 -0
- data/spec/thinking_sphinx_spec.rb +203 -0
- data/tasks/distribution.rb +33 -0
- data/tasks/testing.rb +80 -0
- metadata +509 -0
@@ -0,0 +1,36 @@
|
|
1
|
+
module ThinkingSphinx
|
2
|
+
module ActiveRecord
|
3
|
+
module HasManyAssociation
|
4
|
+
def search(*args)
|
5
|
+
options = args.extract_options!
|
6
|
+
options[:with] ||= {}
|
7
|
+
options[:with].merge! default_filter
|
8
|
+
|
9
|
+
args << options
|
10
|
+
@reflection.klass.search(*args)
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def attribute_for_foreign_key
|
16
|
+
foreign_key = @reflection.primary_key_name
|
17
|
+
stack = [@reflection.options[:through]].compact
|
18
|
+
|
19
|
+
@reflection.klass.define_indexes
|
20
|
+
(@reflection.klass.sphinx_indexes || []).each do |index|
|
21
|
+
attribute = index.attributes.detect { |attrib|
|
22
|
+
attrib.columns.length == 1 &&
|
23
|
+
attrib.columns.first.__name == foreign_key.to_sym
|
24
|
+
}
|
25
|
+
return attribute unless attribute.nil?
|
26
|
+
end
|
27
|
+
|
28
|
+
raise "Missing Attribute for Foreign Key #{foreign_key}"
|
29
|
+
end
|
30
|
+
|
31
|
+
def default_filter
|
32
|
+
{attribute_for_foreign_key.unique_name => @owner.id}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module ThinkingSphinx
|
2
|
+
module ActiveRecord
|
3
|
+
module HasManyAssociationWithScopes
|
4
|
+
def method_missing(method, *args, &block)
|
5
|
+
if responds_to_scope(method)
|
6
|
+
@reflection.klass.
|
7
|
+
search(:with => default_filter).
|
8
|
+
send(method, *args, &block)
|
9
|
+
else
|
10
|
+
super
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
def responds_to_scope(scope)
|
16
|
+
@reflection.klass.respond_to?(:sphinx_scopes) &&
|
17
|
+
@reflection.klass.sphinx_scopes.include?(scope)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'active_support/log_subscriber'
|
2
|
+
|
3
|
+
module ThinkingSphinx
|
4
|
+
module ActiveRecord
|
5
|
+
class LogSubscriber < ActiveSupport::LogSubscriber
|
6
|
+
def self.runtime=(value)
|
7
|
+
Thread.current['thinking_sphinx_query_runtime'] = value
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.runtime
|
11
|
+
Thread.current['thinking_sphinx_query_runtime'] ||= 0
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.reset_runtime
|
15
|
+
rt, self.runtime = runtime, 0
|
16
|
+
rt
|
17
|
+
end
|
18
|
+
|
19
|
+
def initialize
|
20
|
+
super
|
21
|
+
@odd_or_even = false
|
22
|
+
end
|
23
|
+
|
24
|
+
def query(event)
|
25
|
+
self.class.runtime += event.duration
|
26
|
+
return unless logger.debug?
|
27
|
+
|
28
|
+
identifier = color('Sphinx Query (%.1fms)' % event.duration, GREEN, true)
|
29
|
+
query = event.payload[:query]
|
30
|
+
query = color query, nil, true if odd?
|
31
|
+
|
32
|
+
debug " #{identifier} #{query}"
|
33
|
+
end
|
34
|
+
|
35
|
+
def message(event)
|
36
|
+
return unless logger.debug?
|
37
|
+
|
38
|
+
identifier = color 'Sphinx', GREEN, true
|
39
|
+
message = event.payload[:message]
|
40
|
+
message = color message, nil, true if odd?
|
41
|
+
|
42
|
+
debug " #{identifier} #{message}"
|
43
|
+
end
|
44
|
+
|
45
|
+
def odd?
|
46
|
+
@odd_or_even = !@odd_or_even
|
47
|
+
end
|
48
|
+
|
49
|
+
def logger
|
50
|
+
return @logger if defined? @logger
|
51
|
+
self.logger = ::ActiveRecord::Base.logger
|
52
|
+
end
|
53
|
+
|
54
|
+
def logger=(logger)
|
55
|
+
@logger = logger
|
56
|
+
end
|
57
|
+
|
58
|
+
attach_to :thinking_sphinx
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
module ThinkingSphinx
|
2
|
+
module ActiveRecord
|
3
|
+
module Scopes
|
4
|
+
def self.included(base)
|
5
|
+
base.class_eval do
|
6
|
+
extend ThinkingSphinx::ActiveRecord::Scopes::ClassMethods
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
module ClassMethods
|
11
|
+
|
12
|
+
# Similar to ActiveRecord's default_scope method Thinking Sphinx supports
|
13
|
+
# a default_sphinx_scope. For example:
|
14
|
+
#
|
15
|
+
# default_sphinx_scope :some_sphinx_named_scope
|
16
|
+
#
|
17
|
+
# The scope is automatically applied when the search method is called. It
|
18
|
+
# will only be applied if it is an existing sphinx_scope.
|
19
|
+
def default_sphinx_scope(sphinx_scope_name)
|
20
|
+
add_sphinx_scopes_support_to_has_many_associations
|
21
|
+
@default_sphinx_scope = sphinx_scope_name
|
22
|
+
end
|
23
|
+
|
24
|
+
# Returns the default_sphinx_scope or nil if none is set.
|
25
|
+
def get_default_sphinx_scope
|
26
|
+
@default_sphinx_scope
|
27
|
+
end
|
28
|
+
|
29
|
+
# Returns true if the current Model has a default_sphinx_scope. Also checks if
|
30
|
+
# the default_sphinx_scope actually is a scope.
|
31
|
+
def has_default_sphinx_scope?
|
32
|
+
!@default_sphinx_scope.nil? && sphinx_scopes.include?(@default_sphinx_scope)
|
33
|
+
end
|
34
|
+
|
35
|
+
# Similar to ActiveRecord's named_scope method Thinking Sphinx supports
|
36
|
+
# scopes. For example:
|
37
|
+
#
|
38
|
+
# sphinx_scope(:latest_first) {
|
39
|
+
# {:order => 'created_at DESC, @relevance DESC'}
|
40
|
+
# }
|
41
|
+
#
|
42
|
+
# Usage:
|
43
|
+
#
|
44
|
+
# @articles = Article.latest_first.search 'pancakes'
|
45
|
+
#
|
46
|
+
def sphinx_scope(method, &block)
|
47
|
+
add_sphinx_scopes_support_to_has_many_associations
|
48
|
+
|
49
|
+
@sphinx_scopes ||= []
|
50
|
+
@sphinx_scopes << method
|
51
|
+
|
52
|
+
singleton_class.instance_eval do
|
53
|
+
define_method(method) do |*args|
|
54
|
+
options = {:classes => classes_option}
|
55
|
+
options.merge! block.call(*args)
|
56
|
+
|
57
|
+
ThinkingSphinx::Search.new(options)
|
58
|
+
end
|
59
|
+
|
60
|
+
define_method("#{method}_without_default".to_sym) do |*args|
|
61
|
+
options = {:classes => classes_option, :ignore_default => true}
|
62
|
+
options.merge! block.call(*args)
|
63
|
+
|
64
|
+
ThinkingSphinx::Search.new(options)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
# This returns an Array of all defined scopes. The default
|
70
|
+
# scope shows as :default.
|
71
|
+
def sphinx_scopes
|
72
|
+
@sphinx_scopes || []
|
73
|
+
end
|
74
|
+
|
75
|
+
def remove_sphinx_scopes
|
76
|
+
sphinx_scopes.each do |scope|
|
77
|
+
singleton_class.send(:undef_method, scope)
|
78
|
+
end
|
79
|
+
|
80
|
+
sphinx_scopes.clear
|
81
|
+
end
|
82
|
+
|
83
|
+
def add_sphinx_scopes_support_to_has_many_associations
|
84
|
+
scope_mixin = ::ThinkingSphinx::ActiveRecord::HasManyAssociationWithScopes
|
85
|
+
|
86
|
+
::ActiveRecord::Associations::HasManyAssociation.send(:include, scope_mixin)
|
87
|
+
::ActiveRecord::Associations::HasManyThroughAssociation.send(:include, scope_mixin)
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
module ThinkingSphinx
|
2
|
+
class AbstractAdapter
|
3
|
+
def initialize(model)
|
4
|
+
@model = model
|
5
|
+
end
|
6
|
+
|
7
|
+
def setup
|
8
|
+
# Deliberately blank - subclasses should do something though. Well, if
|
9
|
+
# they need to.
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.detect(model)
|
13
|
+
adapter = adapter_for_model model
|
14
|
+
case adapter
|
15
|
+
when :mysql
|
16
|
+
ThinkingSphinx::MysqlAdapter.new model
|
17
|
+
when :postgresql
|
18
|
+
ThinkingSphinx::PostgreSQLAdapter.new model
|
19
|
+
when Class
|
20
|
+
adapter.new model
|
21
|
+
else
|
22
|
+
raise "Invalid Database Adapter: Sphinx only supports MySQL and PostgreSQL, not #{adapter}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.adapter_for_model(model)
|
27
|
+
case ThinkingSphinx.database_adapter
|
28
|
+
when String
|
29
|
+
ThinkingSphinx.database_adapter.to_sym
|
30
|
+
when NilClass
|
31
|
+
standard_adapter_for_model model
|
32
|
+
when Proc
|
33
|
+
ThinkingSphinx.database_adapter.call model
|
34
|
+
else
|
35
|
+
ThinkingSphinx.database_adapter
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def self.standard_adapter_for_model(model)
|
40
|
+
case model.connection.class.name
|
41
|
+
when "ActiveRecord::ConnectionAdapters::MysqlAdapter",
|
42
|
+
"ActiveRecord::ConnectionAdapters::MysqlplusAdapter",
|
43
|
+
"ActiveRecord::ConnectionAdapters::Mysql2Adapter",
|
44
|
+
"ActiveRecord::ConnectionAdapters::NullDBAdapter"
|
45
|
+
:mysql
|
46
|
+
when "ActiveRecord::ConnectionAdapters::PostgreSQLAdapter"
|
47
|
+
:postgresql
|
48
|
+
when "ActiveRecord::ConnectionAdapters::JdbcAdapter"
|
49
|
+
case model.connection.config[:adapter]
|
50
|
+
when "jdbcmysql"
|
51
|
+
:mysql
|
52
|
+
when "jdbcpostgresql"
|
53
|
+
:postgresql
|
54
|
+
else
|
55
|
+
model.connection.config[:adapter]
|
56
|
+
end
|
57
|
+
else
|
58
|
+
model.connection.class.name
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def quote_with_table(column)
|
63
|
+
"#{@model.quoted_table_name}.#{@model.connection.quote_column_name(column)}"
|
64
|
+
end
|
65
|
+
|
66
|
+
def bigint_pattern
|
67
|
+
/bigint/i
|
68
|
+
end
|
69
|
+
|
70
|
+
def downcase(clause)
|
71
|
+
"LOWER(#{clause})"
|
72
|
+
end
|
73
|
+
|
74
|
+
def case(expression, pairs, default)
|
75
|
+
"CASE #{expression} " +
|
76
|
+
pairs.keys.inject('') { |string, key|
|
77
|
+
string + "WHEN '#{key}' THEN #{pairs[key]} "
|
78
|
+
} + "ELSE #{default} END"
|
79
|
+
end
|
80
|
+
|
81
|
+
protected
|
82
|
+
|
83
|
+
def connection
|
84
|
+
@connection ||= @model.connection
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module ThinkingSphinx
|
2
|
+
class MysqlAdapter < AbstractAdapter
|
3
|
+
def setup
|
4
|
+
# Does MySQL actually need to do anything?
|
5
|
+
end
|
6
|
+
|
7
|
+
def sphinx_identifier
|
8
|
+
"mysql"
|
9
|
+
end
|
10
|
+
|
11
|
+
def concatenate(clause, separator = ' ')
|
12
|
+
"CONCAT_WS('#{separator}', #{clause})"
|
13
|
+
end
|
14
|
+
|
15
|
+
def group_concatenate(clause, separator = ' ')
|
16
|
+
"GROUP_CONCAT(DISTINCT IFNULL(#{clause}, '0') SEPARATOR '#{separator}')"
|
17
|
+
end
|
18
|
+
|
19
|
+
def cast_to_string(clause)
|
20
|
+
"CAST(#{clause} AS CHAR)"
|
21
|
+
end
|
22
|
+
|
23
|
+
def cast_to_datetime(clause)
|
24
|
+
"UNIX_TIMESTAMP(#{clause})"
|
25
|
+
end
|
26
|
+
|
27
|
+
def cast_to_unsigned(clause)
|
28
|
+
"CAST(#{clause} AS UNSIGNED)"
|
29
|
+
end
|
30
|
+
|
31
|
+
def convert_nulls(clause, default = '')
|
32
|
+
default = "'#{default}'" if default.is_a?(String)
|
33
|
+
|
34
|
+
"IFNULL(#{clause}, #{default})"
|
35
|
+
end
|
36
|
+
|
37
|
+
def boolean(value)
|
38
|
+
value ? 1 : 0
|
39
|
+
end
|
40
|
+
|
41
|
+
def crc(clause, blank_to_null = false)
|
42
|
+
clause = "NULLIF(#{clause},'')" if blank_to_null
|
43
|
+
"CRC32(#{clause})"
|
44
|
+
end
|
45
|
+
|
46
|
+
def utf8_query_pre
|
47
|
+
"SET NAMES utf8"
|
48
|
+
end
|
49
|
+
|
50
|
+
def time_difference(diff)
|
51
|
+
"DATE_SUB(NOW(), INTERVAL #{diff} SECOND)"
|
52
|
+
end
|
53
|
+
|
54
|
+
def utc_query_pre
|
55
|
+
"SET TIME_ZONE = '+0:00'"
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,153 @@
|
|
1
|
+
module ThinkingSphinx
|
2
|
+
class PostgreSQLAdapter < AbstractAdapter
|
3
|
+
def setup
|
4
|
+
create_array_accum_function
|
5
|
+
create_crc32_function
|
6
|
+
end
|
7
|
+
|
8
|
+
def sphinx_identifier
|
9
|
+
"pgsql"
|
10
|
+
end
|
11
|
+
|
12
|
+
def concatenate(clause, separator = ' ')
|
13
|
+
if clause[/^COALESCE/]
|
14
|
+
clause.split('), ').join(") || '#{separator}' || ")
|
15
|
+
else
|
16
|
+
clause.split(', ').collect { |field|
|
17
|
+
"CAST(COALESCE(#{field}, '') as varchar)"
|
18
|
+
}.join(" || '#{separator}' || ")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def group_concatenate(clause, separator = ' ')
|
23
|
+
"array_to_string(array_accum(COALESCE(#{clause}, '0')), '#{separator}')"
|
24
|
+
end
|
25
|
+
|
26
|
+
def cast_to_string(clause)
|
27
|
+
clause
|
28
|
+
end
|
29
|
+
|
30
|
+
def cast_to_datetime(clause)
|
31
|
+
"cast(extract(epoch from #{clause}) as int)"
|
32
|
+
end
|
33
|
+
|
34
|
+
def cast_to_unsigned(clause)
|
35
|
+
clause
|
36
|
+
end
|
37
|
+
|
38
|
+
def convert_nulls(clause, default = '')
|
39
|
+
default = case default
|
40
|
+
when String
|
41
|
+
"'#{default}'"
|
42
|
+
when NilClass
|
43
|
+
'NULL'
|
44
|
+
when Fixnum
|
45
|
+
"#{default}::bigint"
|
46
|
+
else
|
47
|
+
default
|
48
|
+
end
|
49
|
+
|
50
|
+
"COALESCE(#{clause}, #{default})"
|
51
|
+
end
|
52
|
+
|
53
|
+
def boolean(value)
|
54
|
+
value ? 'TRUE' : 'FALSE'
|
55
|
+
end
|
56
|
+
|
57
|
+
def crc(clause, blank_to_null = false)
|
58
|
+
clause = "NULLIF(#{clause},'')" if blank_to_null
|
59
|
+
"crc32(#{clause})"
|
60
|
+
end
|
61
|
+
|
62
|
+
def utf8_query_pre
|
63
|
+
nil
|
64
|
+
end
|
65
|
+
|
66
|
+
def time_difference(diff)
|
67
|
+
"current_timestamp - interval '#{diff} seconds'"
|
68
|
+
end
|
69
|
+
|
70
|
+
def utc_query_pre
|
71
|
+
"SET TIME ZONE 'UTC'"
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
|
76
|
+
def execute(command, output_error = false)
|
77
|
+
connection.execute "begin"
|
78
|
+
connection.execute "savepoint ts"
|
79
|
+
begin
|
80
|
+
connection.execute command
|
81
|
+
rescue StandardError => err
|
82
|
+
puts err if output_error
|
83
|
+
connection.execute "rollback to savepoint ts"
|
84
|
+
end
|
85
|
+
connection.execute "release savepoint ts"
|
86
|
+
connection.execute "commit"
|
87
|
+
end
|
88
|
+
|
89
|
+
def create_array_accum_function
|
90
|
+
if connection.raw_connection.respond_to?(:server_version) && connection.raw_connection.server_version > 80200
|
91
|
+
execute <<-SQL
|
92
|
+
CREATE AGGREGATE array_accum (anyelement)
|
93
|
+
(
|
94
|
+
sfunc = array_append,
|
95
|
+
stype = anyarray,
|
96
|
+
initcond = '{}'
|
97
|
+
);
|
98
|
+
SQL
|
99
|
+
else
|
100
|
+
execute <<-SQL
|
101
|
+
CREATE AGGREGATE array_accum
|
102
|
+
(
|
103
|
+
basetype = anyelement,
|
104
|
+
sfunc = array_append,
|
105
|
+
stype = anyarray,
|
106
|
+
initcond = '{}'
|
107
|
+
);
|
108
|
+
SQL
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
def create_crc32_function
|
113
|
+
execute "CREATE LANGUAGE 'plpgsql';"
|
114
|
+
function = <<-SQL
|
115
|
+
CREATE OR REPLACE FUNCTION crc32(word text)
|
116
|
+
RETURNS bigint AS $$
|
117
|
+
DECLARE tmp bigint;
|
118
|
+
DECLARE i int;
|
119
|
+
DECLARE j int;
|
120
|
+
DECLARE byte_length int;
|
121
|
+
DECLARE word_array bytea;
|
122
|
+
BEGIN
|
123
|
+
IF COALESCE(word, '') = '' THEN
|
124
|
+
return 0;
|
125
|
+
END IF;
|
126
|
+
|
127
|
+
i = 0;
|
128
|
+
tmp = 4294967295;
|
129
|
+
byte_length = bit_length(word) / 8;
|
130
|
+
word_array = decode(replace(word, E'\\\\', E'\\\\\\\\'), 'escape');
|
131
|
+
LOOP
|
132
|
+
tmp = (tmp # get_byte(word_array, i))::bigint;
|
133
|
+
i = i + 1;
|
134
|
+
j = 0;
|
135
|
+
LOOP
|
136
|
+
tmp = ((tmp >> 1) # (3988292384 * (tmp & 1)))::bigint;
|
137
|
+
j = j + 1;
|
138
|
+
IF j >= 8 THEN
|
139
|
+
EXIT;
|
140
|
+
END IF;
|
141
|
+
END LOOP;
|
142
|
+
IF i >= byte_length THEN
|
143
|
+
EXIT;
|
144
|
+
END IF;
|
145
|
+
END LOOP;
|
146
|
+
return (tmp # 4294967295);
|
147
|
+
END
|
148
|
+
$$ IMMUTABLE LANGUAGE plpgsql;
|
149
|
+
SQL
|
150
|
+
execute function, true
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|