Class: DataMapper::SQLFinders::SQLBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/data_mapper/sql_finders/sql_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(adapter, query) ⇒ SQLBuilder

Returns a new instance of SQLBuilder.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/data_mapper/sql_finders/sql_builder.rb', line 4

def initialize(adapter, query)
  @adapter                      = adapter
  @query                        = query
  @model                        = @query.model
  @parts, @sql_values           = @query.respond_to?(:sql) ? @query.sql : [{}, []]
  @fields                       = @query.fields
  @conditions                   = @query.conditions
  @qualify                      = @query.links.any? || !@parts[:from].nil?
  @conditions_stmt, @qry_values = @adapter.send(:conditions_statement, @conditions, @qualify)
  @order_by                     = @query.order
  @limit                        = @query.limit
  @offset                       = @query.offset
  @bind_values                  = @sql_values + @qry_values
  @group_by = if @query.unique?
    @fields.select { |property| property.kind_of?(Property) && @model.properties[property.name] }
  end
end

Instance Method Details

#select_statementObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/data_mapper/sql_finders/sql_builder.rb', line 22

def select_statement
  return @adapter.send(:select_statement_without_query, @query) unless @query.kind_of?(SQLFinders::Query)

  statement = [
    columns_fragment,
    from_fragment,
    join_fragment,
    where_fragment,
    group_fragment,
    order_fragment
  ].compact.join(" ")

  @adapter.send(:add_limit_offset!, statement, @limit, @offset, @bind_values)

  return statement, @bind_values
end