class ThinkingSphinx::Source

Attributes

attributes[RW]
base[R]
conditions[RW]
database_configuration[R]
fields[RW]
groupings[RW]
index[R]
joins[RW]
model[RW]
options[RW]

Public Class Methods

new(index, options = {}) click to toggle source
# File lib/thinking_sphinx/source.rb, line 13
def initialize(index, options = {})
  @index        = index
  @model        = index.model
  @fields       = []
  @attributes   = []
  @joins        = []
  @conditions   = []
  @groupings    = []
  @options      = options
  @associations = {}
  @database_configuration = @model.connection.
    instance_variable_get(:@config).clone

  @base = join_dependency_class.new(
    @model, [], initial_joins
  )

  add_internal_attributes_and_facets
end

Public Instance Methods

association(key) click to toggle source

Gets the association stack for a specific key.

# File lib/thinking_sphinx/source.rb, line 72
def association(key)
  @associations[key] ||= Association.children(@model, key)
end
delta?() click to toggle source
# File lib/thinking_sphinx/source.rb, line 66
def delta?
  !@index.delta_object.nil?
end
name() click to toggle source
# File lib/thinking_sphinx/source.rb, line 33
def name
  index.name
end
to_riddle_for_core(offset, position) click to toggle source
# File lib/thinking_sphinx/source.rb, line 37
def to_riddle_for_core(offset, position)
  source = Riddle::Configuration::SQLSource.new(
    "#{index.core_name}_#{position}", adapter.sphinx_identifier
  )

  set_source_database_settings  source
  set_source_fields             source
  set_source_attributes         source, offset
  set_source_settings           source
  set_source_sql                source, offset

  source
end
to_riddle_for_delta(offset, position) click to toggle source
# File lib/thinking_sphinx/source.rb, line 51
def to_riddle_for_delta(offset, position)
  source = Riddle::Configuration::SQLSource.new(
    "#{index.delta_name}_#{position}", adapter.sphinx_identifier
  )
  source.parent = "#{index.core_name}_#{position}"

  set_source_database_settings  source
  set_source_fields             source
  set_source_attributes         source, offset, true
  set_source_settings           source
  set_source_sql                source, offset, true

  source
end

Private Instance Methods

adapter() click to toggle source
# File lib/thinking_sphinx/source.rb, line 78
def adapter
  @adapter ||= @model.sphinx_database_adapter
end
all_associations() click to toggle source

Returns all associations used amongst all the fields and attributes. This includes all associations between the model and what the actual columns are from.

# File lib/thinking_sphinx/source.rb, line 149
def all_associations
  @all_associations ||= (
    # field associations
    @fields.collect { |field|
      field.associations.values
    }.flatten +
    # attribute associations
    @attributes.collect { |attrib|
      attrib.associations.values if attrib.include_as_association?
    }.compact.flatten +
    # explicit joins
    @joins.collect { |join|
      join.associations
    }.flatten
  ).uniq.collect { |assoc|
    # get ancestors as well as column-level associations
    assoc.ancestors
  }.flatten.uniq
end
available_attributes() click to toggle source
# File lib/thinking_sphinx/source.rb, line 82
def available_attributes
  attributes.select { |attrib| attrib.available? }
end
initial_joins() click to toggle source
# File lib/thinking_sphinx/source.rb, line 181
def initial_joins
  if rails_3_1?
    []
  else
    nil
  end
end
join_dependency_class() click to toggle source
# File lib/thinking_sphinx/source.rb, line 173
def join_dependency_class
  if rails_3_1?
    ::ActiveRecord::Associations::JoinDependency
  else
    ::ActiveRecord::Associations::ClassMethods::JoinDependency
  end
end
rails_3_1?() click to toggle source
# File lib/thinking_sphinx/source.rb, line 189
def rails_3_1?
  ::ActiveRecord::Associations.constants.include?(:JoinDependency) ||
  ::ActiveRecord::Associations.constants.include?('JoinDependency')
end
set_source_attributes(source, offset, delta = false) click to toggle source
# File lib/thinking_sphinx/source.rb, line 110
def set_source_attributes(source, offset, delta = false)
  available_attributes.each do |attrib|
    source.send(attrib.type_to_config) << attrib.config_value(offset, delta)
  end
end
set_source_database_settings(source) click to toggle source
# File lib/thinking_sphinx/source.rb, line 86
def set_source_database_settings(source)
  config = @database_configuration

  source.sql_host = config[:host]           || "localhost"
  source.sql_user = config[:username]       || config[:user] || ENV['USER']
  source.sql_pass = (config[:password].to_s || "").gsub('#', '\#')
  source.sql_db   = config[:database]
  source.sql_port = config[:port]
  source.sql_sock = config[:socket]

  # MySQL SSL support
  source.mysql_ssl_ca   = config[:sslca]   if config[:sslca]
  source.mysql_ssl_cert = config[:sslcert] if config[:sslcert]
  source.mysql_ssl_key  = config[:sslkey]  if config[:sslkey]
end
set_source_fields(source) click to toggle source
# File lib/thinking_sphinx/source.rb, line 102
def set_source_fields(source)
  fields.each do |field|
    source.sql_file_field   << field.unique_name if field.file?
    source.sql_field_string << field.unique_name if field.with_attribute?
    source.sql_field_str2wordcount << field.unique_name if field.with_wordcount?
  end
end
set_source_settings(source) click to toggle source
# File lib/thinking_sphinx/source.rb, line 131
def set_source_settings(source)
  config = ThinkingSphinx::Configuration.instance
  config.source_options.each do |key, value|
    source.send("#{key}=".to_sym, value)
  end

  source_options = ThinkingSphinx::Configuration::SourceOptions
  @options.each do |key, value|
    if source_options.include?(key.to_s) && !value.nil?
      source.send("#{key}=".to_sym, value)
    end
  end
end
set_source_sql(source, offset, delta = false) click to toggle source
# File lib/thinking_sphinx/source.rb, line 116
def set_source_sql(source, offset, delta = false)
  source.sql_query        = to_sql(:offset => offset, :delta => delta).gsub(/\n/, ' ')
  source.sql_query_range  = to_sql_query_range(:delta => delta)
  source.sql_query_info   = to_sql_query_info(offset)

  source.sql_query_pre += send(!delta ? :sql_query_pre_for_core : :sql_query_pre_for_delta)

  if @index.local_options[:group_concat_max_len]
    source.sql_query_pre << "SET SESSION group_concat_max_len = #{@index.local_options[:group_concat_max_len]}"
  end

  source.sql_query_pre += [adapter.utf8_query_pre].compact if utf8?
  source.sql_query_pre << adapter.utc_query_pre
end
utf8?() click to toggle source
# File lib/thinking_sphinx/source.rb, line 169
def utf8?
  @index.options[:charset_type] =~ /utf-8|zh_cn.utf-8/
end