Module: ThinkingSphinx::Source::SQL

Included in:
ThinkingSphinx::Source
Defined in:
lib/thinking_sphinx/source/sql.rb

Instance Method Summary collapse

Instance Method Details

#crc_columnObject



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/thinking_sphinx/source/sql.rb', line 114

def crc_column
  if @model.table_exists? &&
    @model.column_names.include?(@model.inheritance_column)
    
    adapter.cast_to_unsigned(adapter.convert_nulls(
      adapter.crc(adapter.quote_with_table(@model.inheritance_column), true),
      @model.to_crc32
    ))
  else
    @model.to_crc32.to_s
  end
end

#quote_column(column) ⇒ Object



110
111
112
# File 'lib/thinking_sphinx/source/sql.rb', line 110

def quote_column(column)
  @model.connection.quote_column_name(column)
end

#sql_group_clauseObject



84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/thinking_sphinx/source/sql.rb', line 84

def sql_group_clause
  internal_groupings = []
  if @model.column_names.include?(@model.inheritance_column)
     internal_groupings << "#{@model.quoted_table_name}.#{quote_column(@model.inheritance_column)}"
  end

  (
    ["#{@model.quoted_table_name}.#{quote_column(@model.primary_key)}"] + 
    @fields.collect     { |field|     field.to_group_sql     }.compact +
    @attributes.collect { |attribute| attribute.to_group_sql }.compact +
    @groupings + internal_groupings
  ).join(", ")
end

#sql_query_pre_for_coreObject



98
99
100
101
102
103
104
# File 'lib/thinking_sphinx/source/sql.rb', line 98

def sql_query_pre_for_core
  if self.delta? && !@index.delta_object.reset_query(@model).blank?
    [@index.delta_object.reset_query(@model)]
  else
    []
  end
end

#sql_query_pre_for_deltaObject



106
107
108
# File 'lib/thinking_sphinx/source/sql.rb', line 106

def sql_query_pre_for_delta
  [""]
end

#sql_select_clause(offset) ⇒ Object



59
60
61
62
63
64
65
66
67
# File 'lib/thinking_sphinx/source/sql.rb', line 59

def sql_select_clause(offset)
  unique_id_expr = ThinkingSphinx.unique_id_expression(offset)

  (
    ["#{@model.quoted_table_name}.#{quote_column(@model.primary_key)} #{unique_id_expr} AS #{quote_column(@model.primary_key)} "] + 
    @fields.collect     { |field|     field.to_select_sql     } +
    @attributes.collect { |attribute| attribute.to_select_sql }
  ).compact.join(", ")
end

#sql_where_clause(options) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/thinking_sphinx/source/sql.rb', line 69

def sql_where_clause(options)
  logic = []
  logic += [
    "#{@model.quoted_table_name}.#{quote_column(@model.primary_key)} >= $start",
    "#{@model.quoted_table_name}.#{quote_column(@model.primary_key)} <= $end"
  ] unless @index.options[:disable_range]

  if self.delta? && !@index.delta_object.clause(@model, options[:delta]).blank?
    logic << "#{@index.delta_object.clause(@model, options[:delta])}"
  end

  logic += (@conditions || [])
  logic.empty? ? "" : "WHERE #{logic.join(' AND ')}"
end

#to_sql(options = {}) ⇒ Object

Generates the big SQL statement to get the data back for all the fields and attributes, using all the relevant association joins. If you want the version filtered for delta values, send through :delta => true in the options. Won’t do much though if the index isn’t set up to support a delta sibling.

Examples:

source.to_sql
source.to_sql(:delta => true)


15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/thinking_sphinx/source/sql.rb', line 15

def to_sql(options={})
  sql = <<-SQL
SELECT #{ sql_select_clause options[:offset] }
FROM #{ @model.quoted_table_name }
  #{ all_associations.collect { |assoc| assoc.to_sql }.join(' ') }
#{ sql_where_clause(options) }
GROUP BY #{ sql_group_clause }
  SQL

  sql += " ORDER BY NULL" if adapter.sphinx_identifier == "mysql"
  sql
end

#to_sql_query_info(offset) ⇒ Object

Simple helper method for the query info SQL - which is a statement that returns the single row for a corresponding id.



54
55
56
57
# File 'lib/thinking_sphinx/source/sql.rb', line 54

def to_sql_query_info(offset)
  "SELECT * FROM #{@model.quoted_table_name} WHERE " +
  "#{quote_column(@model.primary_key)} = (($id - #{offset}) / #{ThinkingSphinx.indexed_models.size})"
end

#to_sql_query_range(options = {}) ⇒ Object

Simple helper method for the query range SQL - which is a statement that returns minimum and maximum id values. These can be filtered by delta - so pass in :delta => true to get the delta version of the SQL.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/thinking_sphinx/source/sql.rb', line 32

def to_sql_query_range(options={})
  return nil if @index.options[:disable_range]
  
  min_statement = adapter.convert_nulls(
    "MIN(#{quote_column(@model.primary_key)})", 1
  )
  max_statement = adapter.convert_nulls(
    "MAX(#{quote_column(@model.primary_key)})", 1
  )

  sql = "SELECT #{min_statement}, #{max_statement} " +
        "FROM #{@model.quoted_table_name} "
  if self.delta? && !@index.delta_object.clause(@model, options[:delta]).blank?
    sql << "WHERE #{@index.delta_object.clause(@model, options[:delta])}"
  end

  sql
end