Class: Arel::SqlCompiler::MsSQLCompiler

Inherits:
GenericCompiler
  • Object
show all
Defined in:
lib/arel/engines/sql/compilers/mssql_compiler.rb

Instance Method Summary collapse

Instance Method Details

#build_clausesObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/arel/engines/sql/compilers/mssql_compiler.rb', line 26

def build_clauses
  joins   = relation.joins(self)
  wheres  = relation.where_clauses
  groups  = relation.group_clauses
  havings = relation.having_clauses
  orders  = relation.order_clauses

  clauses = [ "",
    joins,
    ("WHERE     #{wheres.join(' AND ')}" unless wheres.empty?),
    ("GROUP BY  #{groups.join(', ')}" unless groups.empty?),
    ("HAVING    #{havings.join(' AND ')}" unless havings.empty?),
    ("ORDER BY  #{orders.join(', ')}" unless orders.empty?)
  ].compact.join ' '

  clauses << " #{locked}" unless locked.blank?
  clauses unless clauses.blank?
end

#select_sqlObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/arel/engines/sql/compilers/mssql_compiler.rb', line 4

def select_sql
  projections = @relation.projections
  offset = relation.skipped
  limit = relation.taken
  if Count === projections.first && projections.size == 1 &&
    (relation.taken.present? || relation.wheres.present?) && relation.joins(self).blank?
    subquery = [
      "SELECT * FROM #{relation.from_clauses}", build_clauses
    ].join ' '
    @engine.connection.add_limit_offset!(subquery, :limit => limit, :offset => offset) if offset || limit
    query = "SELECT COUNT(*) AS count_id FROM (#{subquery}) AS subquery"
  else
    query = [
      "SELECT     #{relation.select_clauses.join(', ')}",
      "FROM       #{relation.from_clauses}",
      build_clauses
    ].compact.join ' '
    @engine.connection.add_limit_offset!(query, :limit => limit, :offset => offset) if offset || limit
  end
  query
end