Class: DataMapper::Query

Inherits:
Object show all
Defined in:
lib/data_mapper/query.rb

Overview

This class handles option parsing and SQL generation.

Constant Summary collapse

OPTIONS =

These are the standard finder options

[
  :select, :offset, :limit, :include, :reload, :conditions, :join, :order, :after_row_materialization
]

Instance Method Summary collapse

Constructor Details

#initialize(adapter, klass, options = {}) ⇒ Query

Returns a new instance of Query.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/data_mapper/query.rb', line 11

def initialize(adapter, klass, options = {})
  # Set some of the standard options
  @adapter, @klass = adapter, klass
  @from = @adapter.table(@klass)
  @parameters = []
  @joins = []
  
  # Parse simple options
  @limit =          options.fetch(:limit, nil)
  @offset =         options.fetch(:offset, nil)
  @reload =         options.fetch(:reload, false)
  @order =          options.fetch(:order, nil)
  @after_row_materialization = options.fetch(:after_row_materialization, nil)
  
  # Parse :include option
  @includes = case include_options = options[:include]
    when Array then include_options.dup
    when Symbol then [include_options]
    when NilClass then []
    else raise ArgumentError.new(":include must be an Array, Symbol or nil, but was #{include_options.inspect}")
  end
  
  # Include lazy columns if specified in :include option
  @columns = @from.columns.select do |column|
    !column.lazy? || @includes.delete(column.name)
  end
  
  # Qualify columns with their table name if joins are present
  @qualify = !@includes.empty?
  
  # Generate SQL for joins
  @includes.each do |association_name|
    association = @from.associations[association_name]
    @joins << association.to_sql
    @columns += association.associated_table.columns.select do |column|
      !column.lazy?
    end
  end
  
  # Prepare conditions for parsing
  @conditions = []
  
  # Each non-standard option is assumed to be a column
  options.each_pair do |k,v|
    unless OPTIONS.include?(k)
      append_condition(k, v)
    end
  end
  
  # If a :conditions option is present, parse it
  if conditions_option = options[:conditions]
    if conditions_option.is_a?(String)
      @conditions << conditions_option
    else
      append_condition(*conditions_option)
    end
  end
  
  # If the table is paranoid, add a filter to the conditions
  if @from.paranoid?
    @conditions << "#{@from.paranoid_column.to_sql(qualify?)} IS NULL OR #{@from.paranoid_column.to_sql(qualify?)} > #{@adapter.class::SYNTAX[:now]}"
  end
  
end

Instance Method Details

#parametersObject

Parameters for query



86
87
88
# File 'lib/data_mapper/query.rb', line 86

def parameters
  @parameters
end

#to_sqlObject

SQL for query



77
78
79
80
81
82
83
# File 'lib/data_mapper/query.rb', line 77

def to_sql
  sql = "SELECT #{columns.map { |column| column.to_sql(qualify?) }.join(', ')} FROM #{from.to_sql}"
  
  sql << " " << joins.join($/) unless joins.empty?
  sql << " WHERE (#{conditions.join(") AND (")})" unless conditions.empty?
  return sql
end