Sha256: 17038d872dd5c48daf1fad9d6322b34670724693045f25ee54bb788472120242

Contents?: true

Size: 1.67 KB

Versions: 27

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

27 entries across 27 versions & 7 rubygems

Version Path
cmoran92-activerecord-jdbc-adapter-1.2.1.2 lib/arel/engines/sql/compilers/mssql_compiler.rb
activerecord-jdbc-adapter-1.2.8 lib/arel/engines/sql/compilers/mssql_compiler.rb
activerecord-jdbc-adapter-1.2.5 lib/arel/engines/sql/compilers/mssql_compiler.rb
tgbyte-activerecord-jdbc-adapter-1.2.2.5 lib/arel/engines/sql/compilers/mssql_compiler.rb
tgbyte-activerecord-jdbc-adapter-1.2.2.4 lib/arel/engines/sql/compilers/mssql_compiler.rb
tgbyte-activerecord-jdbc-adapter-1.2.2.3 lib/arel/engines/sql/compilers/mssql_compiler.rb
tgbyte-activerecord-jdbc-adapter-1.2.2.2 lib/arel/engines/sql/compilers/mssql_compiler.rb
activerecord-jdbc-adapter-1.2.2.1 lib/arel/engines/sql/compilers/mssql_compiler.rb
activerecord-jdbc-adapter-onsite-1.2.2 lib/arel/engines/sql/compilers/mssql_compiler.rb
cmoran92-activerecord-jdbc-adapter-1.2.1.1 lib/arel/engines/sql/compilers/mssql_compiler.rb
intentmedia-activerecord-jdbc-adapter-1.1.1.1 lib/arel/engines/sql/compilers/mssql_compiler.rb
activerecord-jdbc-adapter-1.2.2 lib/arel/engines/sql/compilers/mssql_compiler.rb
cmoran92-activerecord-jdbc-adapter-1.2.1 lib/arel/engines/sql/compilers/mssql_compiler.rb
activerecord-jdbc-adapter-1.2.1 lib/arel/engines/sql/compilers/mssql_compiler.rb
c3-activerecord-jdbc-adapter-1.1.3.1 lib/arel/engines/sql/compilers/mssql_compiler.rb
activerecord-jdbc-adapter-1.2.0 lib/arel/engines/sql/compilers/mssql_compiler.rb
c3-activerecord-jdbc-adapter-1.1.3 lib/arel/engines/sql/compilers/mssql_compiler.rb
jashmenn-activerecord-jdbc-adapter-1.2.0.0 lib/arel/engines/sql/compilers/mssql_compiler.rb
jashmenn-activerecord-jdbc-adapter-1.2.0 lib/arel/engines/sql/compilers/mssql_compiler.rb
activerecord-jdbc-adapter-1.1.3 lib/arel/engines/sql/compilers/mssql_compiler.rb