From e16396bdd36afa7ebad7df1f90fe2849fb5d07ad Mon Sep 17 00:00:00 2001 From: Michael Sogomonyan Date: Mon, 29 Dec 2014 16:48:48 +0600 Subject: [PATCH] chore(gem): rails 4.0 compatibility --- .../riddle/configuration/distributed_index.rb | 36 ++- .../thinking_sphinx/active_record.rb | 22 +- .../extensions/thinking_sphinx/source/sql.rb | 219 +++++++++--------- 3 files changed, 139 insertions(+), 138 deletions(-) diff --git a/lib/sphinx/integration/extensions/riddle/configuration/distributed_index.rb b/lib/sphinx/integration/extensions/riddle/configuration/distributed_index.rb index 92534aa..eb8dad5 100644 --- a/lib/sphinx/integration/extensions/riddle/configuration/distributed_index.rb +++ b/lib/sphinx/integration/extensions/riddle/configuration/distributed_index.rb @@ -17,29 +17,27 @@ class << self end end - module InstanceMethods - def initialize_with_integration(name) - initialize_without_integration(name) - @mirror_indices = [] - end - - def valid_with_integration? - local_indices.any? || remote_indices.any? || mirror_indices.any? - end + def initialize_with_integration(name) + initialize_without_integration(name) + @mirror_indices = [] + end - def ha_strategy - @ha_strategy if mirror_indices.any? - end + def valid_with_integration? + local_indices.any? || remote_indices.any? || mirror_indices.any? + end - def agent_with_integration - agents = agent_without_integration + def ha_strategy + @ha_strategy if mirror_indices.any? + end - mirror_indices.each do |cluster| - agents << cluster.map { |agent| "#{agent.remote}:#{agent.name}" }.join('|') - end + def agent_with_integration + agents = agent_without_integration - agents + mirror_indices.each do |cluster| + agents << cluster.map { |agent| "#{agent.remote}:#{agent.name}" }.join('|') end + + agents end module ClassMethods @@ -51,4 +49,4 @@ def settings_with_integration end end end -end \ No newline at end of file +end diff --git a/lib/sphinx/integration/extensions/thinking_sphinx/active_record.rb b/lib/sphinx/integration/extensions/thinking_sphinx/active_record.rb index 7fe97cc..d6eac50 100644 --- a/lib/sphinx/integration/extensions/thinking_sphinx/active_record.rb +++ b/lib/sphinx/integration/extensions/thinking_sphinx/active_record.rb @@ -89,17 +89,13 @@ def to_riddle(config_type) end - module InstanceMethods - - # Находится ли запись в сфинксе - # - # index_name - String (default: nil) - # - # Returns boolean - def exists_in_sphinx?(index_name = nil) - return false if new_record? - !self.class.search_count(:index => index_name, :cut_off => 1, :with =>{'@id' => sphinx_document_id}).zero? - end - + # Находится ли запись в сфинксе + # + # index_name - String (default: nil) + # + # Returns boolean + def exists_in_sphinx?(index_name = nil) + return false if new_record? + !self.class.search_count(:index => index_name, :cut_off => 1, :with => {'@id' => sphinx_document_id}).zero? end -end \ No newline at end of file +end diff --git a/lib/sphinx/integration/extensions/thinking_sphinx/source/sql.rb b/lib/sphinx/integration/extensions/thinking_sphinx/source/sql.rb index cd2be38..d9846d4 100644 --- a/lib/sphinx/integration/extensions/thinking_sphinx/source/sql.rb +++ b/lib/sphinx/integration/extensions/thinking_sphinx/source/sql.rb @@ -14,138 +14,145 @@ module Sphinx::Integration::Extensions::ThinkingSphinx::Source::SQL alias_method_chain :sql_select_clause, :prepared_table_name end - module InstanceMethods - - # позволяет использовать Common Table Expressions or CTEs - # Example - # set_property :source_cte => { - # :_contents => <<-SQL - # select blog_posts.id as blog_post_id, array_to_string(array_agg(blog_post_contents.content), \' \') as content - # from blog_posts - # inner join blog_post_contents on blog_post_contents.blog_post_id = blog_posts.id - # where {{where}} - # group by blog_posts.id' - # } - def to_sql_with_cte(options = {}) - sql = to_sql_without_cte(options) - - if @index.local_options.key?(:source_cte) - cte_sql = [] - @index.local_options[:source_cte].each do |name, value| - as_sql = value.gsub('{{where}}', sql_where_clause(options)).gsub("\n", ' ') - cte_sql << "#{name} AS (#{as_sql})" - end - - sql = 'WITH ' + cte_sql.join(', ') + ' ' + sql unless cte_sql.empty? + # позволяет использовать Common Table Expressions or CTEs + # Example + # set_property :source_cte => { + # :_contents => <<-SQL + # select blog_posts.id as blog_post_id, array_to_string(array_agg(blog_post_contents.content), \' \') as content + # from blog_posts + # inner join blog_post_contents on blog_post_contents.blog_post_id = blog_posts.id + # where {{where}} + # group by blog_posts.id' + # } + def to_sql_with_cte(options = {}) + sql = to_sql_without_cte(options) + + if @index.local_options.key?(:source_cte) + cte_sql = [] + @index.local_options[:source_cte].each do |name, value| + as_sql = value.gsub('{{where}}', sql_where_clause(options)).gsub("\n", ' ') + cte_sql << "#{name} AS (#{as_sql})" end - sql + sql = 'WITH ' + cte_sql.join(', ') + ' ' + sql unless cte_sql.empty? end - # Если в имени таблицы дописать _sql, то это будет считаться как джоин через подзапрос. - # Появляются доп опции :as и :query. Скобки при этом вокруг запроса ставить не нужно - # - # Examples - # - # set_property :source_joins => { - # :my_custom_join_sql => { - # :query => "SELECT * FROM join_table WHERE 1 = 1", - # :as => :my_join, - # :type => :inner, - # :on => 'my_join.id = some_table.id' - # } - # } - # - def to_sql_with_joins(*args) - sql = to_sql_without_joins(*args) - - if @index.local_options.key?(:source_joins) - join_sql = [] - @index.local_options[:source_joins].each do |join_table, join_options| - join_table = "(#{join_options.fetch(:query, '')})" if join_options[:query] - - join_sql << "#{join_options[:type].to_s.upcase} JOIN #{join_table} AS #{join_options.fetch(:as, join_table)} ON #{join_options[:on]}" - end - sql.gsub!(" FROM #{model_quoted_table_name}", " FROM #{model_quoted_table_name} #{join_sql.join(' ')}") unless join_sql.empty? + sql + end + + # Если в имени таблицы дописать _sql, то это будет считаться как джоин через подзапрос. + # Появляются доп опции :as и :query. Скобки при этом вокруг запроса ставить не нужно + # + # Examples + # + # set_property :source_joins => { + # :my_custom_join_sql => { + # :query => "SELECT * FROM join_table WHERE 1 = 1", + # :as => :my_join, + # :type => :inner, + # :on => 'my_join.id = some_table.id' + # } + # } + # + def to_sql_with_joins(*args) + sql = to_sql_without_joins(*args) + + if @index.local_options.key?(:source_joins) + join_sql = [] + @index.local_options[:source_joins].each do |join_table, join_options| + join_table = "(#{join_options.fetch(:query, '')})" if join_options[:query] + + join_sql << "#{join_options[:type].to_s.upcase} JOIN #{join_table} AS #{join_options.fetch(:as, join_table)}" \ + " ON #{join_options[:on]}" end - sql + unless join_sql.empty? + sql.gsub!(" FROM #{model_quoted_table_name}", " FROM #{model_quoted_table_name} #{join_sql.join(' ')}") + end end - # NOTE Исправлена регулярка, теперь она отрезает только последний GROUP BY, а не все подряд - def to_sql_with_nogrouping_options(*args) - sql = to_sql_without_nogrouping_options(*args) - sql.gsub!(/ GROUP BY [^\w\)].*$/, '') if @index.local_options.has_key?(:source_no_grouping) - sql.gsub(@model.quoted_table_name, model_quoted_table_name) - end + sql + end - def to_sql_with_limit(*args) - sql = to_sql_without_limit(*args) - sql << " LIMIT #{ @index.local_options[:sql_query_limit]}" if @index.local_options.key?(:sql_query_limit) - sql - end + # NOTE Исправлена регулярка, теперь она отрезает только последний GROUP BY, а не все подряд + def to_sql_with_nogrouping_options(*args) + sql = to_sql_without_nogrouping_options(*args) + sql.gsub!(/ GROUP BY [^\w\)].*$/, '') if @index.local_options.key?(:source_no_grouping) + sql.gsub(@model.quoted_table_name, model_quoted_table_name) + end - def to_sql_with_custom_sql(*args) - sql = to_sql_without_custom_sql(*args) - if @index.local_options[:with_sql] && @index.local_options[:with_sql][:select] - sql = @index.local_options[:with_sql][:select].call(sql) - end - sql - end + def to_sql_with_limit(*args) + sql = to_sql_without_limit(*args) + sql << " LIMIT #{ @index.local_options[:sql_query_limit]}" if @index.local_options.key?(:sql_query_limit) + sql + end - def to_sql_query_range_with_prepare(options = {}) - return '' if @index.options[:disable_range] || (delta? && options[:delta] && @index.local_options[:disable_delta_range]) - return @index.local_options[:sql_query_range] if @index.local_options[:sql_query_range] - sql = to_sql_query_range_without_prepare(options) - sql.gsub(@model.quoted_table_name, model_quoted_table_name) + def to_sql_with_custom_sql(*args) + sql = to_sql_without_custom_sql(*args) + if @index.local_options[:with_sql] && @index.local_options[:with_sql][:select] + sql = @index.local_options[:with_sql][:select].call(sql) end + sql + end - def to_sql_query_info_with_prepared_table_name(offset) - to_sql_query_info_without_prepared_table_name(offset).gsub(@model.quoted_table_name, model_quoted_table_name) + def to_sql_query_range_with_prepare(options = {}) + if @index.options[:disable_range] || (delta? && options[:delta] && @index.local_options[:disable_delta_range]) + return '' end + return @index.local_options[:sql_query_range] if @index.local_options[:sql_query_range] + sql = to_sql_query_range_without_prepare(options) + sql.gsub(@model.quoted_table_name, model_quoted_table_name) + end - def sql_select_clause_with_prepared_table_name(offset) - sql_select_clause_without_prepared_table_name(offset).gsub(@model.quoted_table_name, model_quoted_table_name) - end + def to_sql_query_info_with_prepared_table_name(offset) + to_sql_query_info_without_prepared_table_name(offset).gsub(@model.quoted_table_name, model_quoted_table_name) + end + + def sql_select_clause_with_prepared_table_name(offset) + sql_select_clause_without_prepared_table_name(offset).gsub(@model.quoted_table_name, model_quoted_table_name) + end - def sql_where_clause(options) - logic = [] - unless @index.local_options[:use_own_sql_query_range] + def sql_where_clause(options) + logic = [] + unless @index.local_options[:use_own_sql_query_range] + if (!options[:delta] && !@index.options[:disable_range]) || + (self.delta? && options[:delta] && !@index.local_options[:disable_delta_range]) logic += [ "#{model_quoted_table_name}.#{quote_column(@model.primary_key_for_sphinx)} >= $start", "#{model_quoted_table_name}.#{quote_column(@model.primary_key_for_sphinx)} <= $end" - ] if (!options[:delta] && !@index.options[:disable_range]) || (self.delta? && options[:delta] && !@index.local_options[:disable_delta_range]) - end - - if self.delta? && !@index.delta_object.clause(@model, options[:delta]).blank? - logic << "#{@index.delta_object.clause(@model, options[:delta])}" + ] end + end - logic += (@conditions || []) - logic.empty? ? "" : logic.join(' AND ') + if self.delta? && !@index.delta_object.clause(@model, options[:delta]).blank? + logic << "#{@index.delta_object.clause(@model, options[:delta])}" end - def sql_group_clause - return @groupings.join(", ") if @index.local_options[:force_group_by] + logic += (@conditions || []) + logic.empty? ? '' : logic.join(' AND ') + end - internal_groupings = [] - if @model.column_names.include?(@model.inheritance_column) - internal_groupings << "#{model_quoted_table_name}.#{quote_column(@model.inheritance_column)}" - end + def sql_group_clause + return @groupings.join(', ') if @index.local_options[:force_group_by] - ( - ["#{model_quoted_table_name}.#{quote_column(@model.primary_key_for_sphinx)}"] + - @fields.collect { |field| field.to_group_sql }.compact + - @attributes.collect { |attribute| attribute.to_group_sql }.compact + - @groupings + internal_groupings - ).join(", ") + internal_groupings = [] + if @model.column_names.include?(@model.inheritance_column) + internal_groupings << "#{model_quoted_table_name}.#{quote_column(@model.inheritance_column)}" end - def model_quoted_table_name - @index.local_options.is_a?(Hash) && - @index.local_options.has_key?(:source_table) ? - @model.connection.quote_table_name(@index.local_options[:source_table]) : - @model.quoted_table_name + ( + ["#{model_quoted_table_name}.#{quote_column(@model.primary_key_for_sphinx)}"] + + @fields.map { |field| field.to_group_sql }.compact + + @attributes.map { |attribute| attribute.to_group_sql }.compact + + @groupings + internal_groupings + ).join(', ') + end + + def model_quoted_table_name + if @index.local_options.is_a?(Hash) && @index.local_options.key?(:source_table) + @model.connection.quote_table_name(@index.local_options[:source_table]) + else + @model.quoted_table_name end end -end \ No newline at end of file +end