Class: Arel::SqlCompiler::GenericCompiler

Inherits:
Object
  • Object
show all
Defined in:
lib/arel/engines/sql/relations/compiler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(relation) ⇒ GenericCompiler

Returns a new instance of GenericCompiler.



6
7
8
# File 'lib/arel/engines/sql/relations/compiler.rb', line 6

def initialize(relation)
  @relation = relation
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (protected)



74
75
76
# File 'lib/arel/engines/sql/relations/compiler.rb', line 74

def method_missing(method, *args, &block)
  relation.send(method, *args, &block)
end

Instance Attribute Details

#relationObject (readonly)

Returns the value of attribute relation.



4
5
6
# File 'lib/arel/engines/sql/relations/compiler.rb', line 4

def relation
  @relation
end

Instance Method Details

#add_limit_on_delete(taken) ⇒ Object



32
33
34
# File 'lib/arel/engines/sql/relations/compiler.rb', line 32

def add_limit_on_delete(taken)
  "LIMIT #{taken}"
end

#delete_sqlObject



24
25
26
27
28
29
30
# File 'lib/arel/engines/sql/relations/compiler.rb', line 24

def delete_sql
  build_query \
    "DELETE",
    "FROM #{table_sql}",
    ("WHERE #{wheres.collect(&:to_sql).join(' AND ')}" unless wheres.blank? ),
    (add_limit_on_delete(taken)                        unless taken.blank?  )
end

#insert_sql(include_returning = true) ⇒ Object



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
# File 'lib/arel/engines/sql/relations/compiler.rb', line 36

def insert_sql(include_returning = true)
  insertion_attributes_values_sql = if record.is_a?(Value)
    record.value
  else
    attributes = record.keys.sort_by do |attribute|
      attribute.name.to_s
    end

    first = attributes.collect do |key|
      engine.quote_column_name(key.name)
    end.join(', ')

    second = attributes.collect do |key|
      key.format(record[key])
    end.join(', ')

    build_query "(#{first})", "VALUES (#{second})"
  end

  build_query \
    "INSERT",
    "INTO #{table_sql}",
    insertion_attributes_values_sql,
    ("RETURNING #{engine.quote_column_name(primary_key)}" if include_returning && compiler.supports_insert_with_returning?)
end

#select_sqlObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/arel/engines/sql/relations/compiler.rb', line 10

def select_sql
  query = build_query \
    "SELECT     #{select_clauses.join(', ')}",
    "FROM       #{from_clauses}",
    (joins(self)                                   unless joins(self).blank? ),
    ("WHERE     #{where_clauses.join(' AND ')}"    unless wheres.blank?      ),
    ("GROUP BY  #{group_clauses.join(', ')}"       unless groupings.blank?   ),
    ("HAVING    #{having_clauses.join(' AND ')}"      unless havings.blank?     ),
    ("ORDER BY  #{order_clauses.join(', ')}"       unless orders.blank?      )
    engine.add_limit_offset!(query,{ :limit => taken, :offset => skipped }) if taken || skipped
    query << " #{locked}" unless locked.blank?
    query
end

#supports_insert_with_returning?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/arel/engines/sql/relations/compiler.rb', line 62

def supports_insert_with_returning?
  false
end

#update_sqlObject



66
67
68
69
70
71
# File 'lib/arel/engines/sql/relations/compiler.rb', line 66

def update_sql
  build_query \
    "UPDATE #{table_sql} SET",
    assignment_sql,
    build_update_conditions_sql
end