Module: ThinkingSphinx::Source::SQL
- Included in:
- ThinkingSphinx::Source
- Defined in:
- lib/thinking_sphinx/source/sql.rb
Instance Method Summary collapse
- #crc_column ⇒ Object
- #internal_class_column ⇒ Object
- #quote_column(column) ⇒ Object
- #sql_group_clause ⇒ Object
- #sql_query_pre_for_core ⇒ Object
- #sql_query_pre_for_delta ⇒ Object
- #sql_select_clause(offset) ⇒ Object
- #sql_where_clause(options) ⇒ Object
-
#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.
-
#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.
-
#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.
- #type_values ⇒ Object
- #types_to_crcs ⇒ Object
- #types_to_hash ⇒ Object
Instance Method Details
#crc_column ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/thinking_sphinx/source/sql.rb', line 118 def crc_column if @model.table_exists? && @model.column_names.include?(@model.inheritance_column) types = types_to_crcs return @model.to_crc32.to_s if types.empty? adapter.case(adapter.convert_nulls( adapter.quote_with_table(@model.inheritance_column), @model.name), types, @model.to_crc32) else @model.to_crc32.to_s end end |
#internal_class_column ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/thinking_sphinx/source/sql.rb', line 133 def internal_class_column quoted_name = "'#{@model.name}'" if @model.table_exists? && @model.column_names.include?(@model.inheritance_column) types = types_to_hash return quoted_name if types.empty? adapter.case(adapter.convert_nulls( adapter.quote_with_table(@model.inheritance_column), @model.name), types, quoted_name) else quoted_name end end |
#quote_column(column) ⇒ Object
114 115 116 |
# File 'lib/thinking_sphinx/source/sql.rb', line 114 def quote_column(column) @model.connection.quote_column_name(column) end |
#sql_group_clause ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/thinking_sphinx/source/sql.rb', line 88 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_for_sphinx)}"] + @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_core ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/thinking_sphinx/source/sql.rb', line 102 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_delta ⇒ Object
110 111 112 |
# File 'lib/thinking_sphinx/source/sql.rb', line 110 def sql_query_pre_for_delta [""] end |
#sql_select_clause(offset) ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/thinking_sphinx/source/sql.rb', line 63 def sql_select_clause(offset) unique_id_expr = ThinkingSphinx.unique_id_expression(adapter, offset) ( ["#{@model.quoted_table_name}.#{quote_column(@model.primary_key_for_sphinx)} #{unique_id_expr} AS #{quote_column(@model.primary_key_for_sphinx)} "] + @fields.collect { |field| field.to_select_sql } + @attributes.collect { |attribute| attribute.to_select_sql } ).compact.join(", ") end |
#sql_where_clause(options) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/thinking_sphinx/source/sql.rb', line 73 def sql_where_clause() logic = [] 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" ] unless @index.[:disable_range] if self.delta? && !@index.delta_object.clause(@model, [:delta]).blank? logic << "#{@index.delta_object.clause(@model, [:delta])}" end logic += (@conditions || []) 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 27 28 29 30 |
# File 'lib/thinking_sphinx/source/sql.rb', line 15 def to_sql(={}) relation = @model.unscoped pre_select = 'SQL_NO_CACHE ' if adapter.sphinx_identifier == "mysql" relation = relation.select( pre_select.to_s + sql_select_clause([:offset]) ) all_associations.each do |assoc| relation = relation.joins(assoc.arel_join) end relation = relation.where(sql_where_clause()) relation = relation.group(sql_group_clause) relation = relation.order('NULL') if adapter.sphinx_identifier == "mysql" relation.to_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.
58 59 60 61 |
# File 'lib/thinking_sphinx/source/sql.rb', line 58 def to_sql_query_info(offset) "SELECT * FROM #{@model.quoted_table_name} WHERE " + "#{quote_column(@model.primary_key_for_sphinx)} = (($id - #{offset}) / #{ThinkingSphinx.context.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.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/thinking_sphinx/source/sql.rb', line 36 def to_sql_query_range(={}) return nil if @index.[:disable_range] min_statement = adapter.convert_nulls( "MIN(#{quote_column(@model.primary_key_for_sphinx)})", 1 ) max_statement = adapter.convert_nulls( "MAX(#{quote_column(@model.primary_key_for_sphinx)})", 1 ) sql = "SELECT #{min_statement}, #{max_statement} " + "FROM #{@model.quoted_table_name} " if self.delta? && !@index.delta_object.clause(@model, [:delta]).blank? sql << "WHERE #{@index.delta_object.clause(@model, [:delta])}" end sql end |
#type_values ⇒ Object
150 151 152 153 154 155 156 157 |
# File 'lib/thinking_sphinx/source/sql.rb', line 150 def type_values return @model.sphinx_types unless @model.sphinx_types.nil? @model.connection.select_values <<-SQL SELECT DISTINCT #{@model.inheritance_column} FROM #{@model.table_name} SQL end |
#types_to_crcs ⇒ Object
159 160 161 162 163 164 |
# File 'lib/thinking_sphinx/source/sql.rb', line 159 def types_to_crcs type_values.compact.inject({}) { |hash, type| hash[type] = type.to_crc32 hash } end |
#types_to_hash ⇒ Object
166 167 168 169 170 171 |
# File 'lib/thinking_sphinx/source/sql.rb', line 166 def types_to_hash type_values.compact.inject({}) { |hash, type| hash[type] = "'#{type}'" hash } end |