Method: Zena::Use::QueryNode::Compiler#process_field

Defined in:
lib/zena/use/query_node.rb

#process_field(field_name) ⇒ Object

Overwrite this and take care to check for valid fields.



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/zena/use/query_node.rb', line 204

def process_field(field_name)
  if fld = @query.attributes_alias[field_name]
    # use custom query alias value defined in select clause: 'custom_a AS validation'
    if processing_filter?
      # We need to use the full original clause, except when the clause contains group functions (MAX, MIN, etc).
      # If the clause is too complex, we need to use something simpler or it will break (custom queries).
      "(#{fld})"
    else
      fld
    end
  elsif processing_filter? && map_def = self.class.filter_fields[field_name]
    # Special filter fields such as 'role', 'tag' or 'class'
    if map_def.kind_of?(String)
      return map_def
    elsif table_def = map_def[:table]
      use_name, source, target, filter = table_def
      table_to_use = add_key_value_table(use_name, target, map_def[:key]) do |tbl_name|
        # This block is only executed once
        filter.gsub(
          'TABLE1', table(source)
        ).gsub(
          'TABLE2', tbl_name
        )
      end
    else
      table_to_use = table
    end
    "#{table_to_use}.#{map_def[:key]}"
  elsif %w{id parent_id project_id section_id user_id}.include?(field_name) ||
    (Node.safe_method_type([field_name]) && Node.column_names.include?(field_name))
    "#{table}.#{field_name}"
  elsif @query.tables.include?('links') &&
       (key = field_name[/^l_(.+)$/,1]) &&
       (key == 'id' ||
        Zena::Use::Relations::LINK_ATTRIBUTES.include?(key.to_sym))
    "#{table('links')}.#{key}"
  elsif field_name == 'random'
    Zena::Db.sql_function(field_name, nil)
  else
    # property or real column

    # FIXME !!!! Why does this happen ?
    return nil if @query.main_class.columns.kind_of?(Array)


    column = @query.main_class.columns[field_name]
    if column && column.indexed?
      if column.index == true
        group_name = column.type
      elsif column.index =~ Property::Index::FIELD_INDEX_REGEXP
        # field in nodes
        return "#{table}.#{$1}"
      else
        group_name = column.index
      end

      index_table = @query.main_class.real_class.index_table_name(group_name)

      # We use the add_key_value_table rule to avoid inserting the
      # same index access twice.

      tbl = add_key_value_table(group_name, index_table, field_name) do |tbl_name|
        # This block is only executed once
        on_clause = "#{tbl_name}.node_id = #{table}.id AND #{tbl_name}.key = #{quote(field_name)}"
        if group_name.to_s =~ /^ml_/
          on_clause << " AND #{tbl_name}.lang = #{quote(visitor.lang)}"
        end
        # no need for distinct, the new table makes a 1-1 relation
        # ON CLAUSE
        on_clause
        
      end

      "#{tbl}.value"
    else
      super # raises an error
    end
  end
end