Sha256: 42ccc35541d0ca9b258b59c277e7f9f31697b048bc873af788af844c1f79a230

Contents?: true

Size: 1.67 KB

Versions: 4

Compression:

Stored size: 1.67 KB

Contents

module Arel
  module SqlCompiler
    class MSSQLCompiler < GenericCompiler
      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

      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
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
activerecord-jdbc-adapter-1.2.9.1 lib/arel/engines/sql/compilers/mssql_compiler.rb
activerecord-jdbc-adapter-1.3.0.beta2 lib/arel/engines/sql/compilers/mssql_compiler.rb
activerecord-jdbc-adapter-1.3.0.beta1 lib/arel/engines/sql/compilers/mssql_compiler.rb
activerecord-jdbc-adapter-1.2.9 lib/arel/engines/sql/compilers/mssql_compiler.rb