Class Index [+]

Quicksearch

Arel::Visitors::MSSQL

Private Instance Methods

determine_order_by(x) click to toggle source
    # File lib/arel/visitors/mssql.rb, line 49
49:       def determine_order_by x
50:         unless x.groups.empty?
51:           "ORDER BY #{x.groups.map { |g| visit g }.join ', ' }"
52:         else
53:           "ORDER BY #{find_left_table_pk(x.froms)}"
54:         end
55:       end
find_left_table_pk(o) click to toggle source

fixme raise exception of there is no pk? fixme!! Table.primary_key will be depricated. What is the replacement??

    # File lib/arel/visitors/mssql.rb, line 67
67:       def find_left_table_pk o
68:         return visit o.primary_key if o.instance_of? Arel::Table
69:         find_left_table_pk o.left if o.kind_of? Arel::Nodes::Join
70:       end
get_offset_limit_clause(o) click to toggle source
    # File lib/arel/visitors/mssql.rb, line 39
39:       def get_offset_limit_clause o
40:         first_row = o.offset ? o.offset.expr.to_i + 1 : 1
41:         last_row  = o.limit ? o.limit.expr.to_i - 1 + first_row : nil
42:         if last_row
43:           " _row_num BETWEEN #{first_row} AND #{last_row}"
44:         else
45:           " _row_num >= #{first_row}"
46:         end
47:       end
row_num_literal(order_by) click to toggle source
    # File lib/arel/visitors/mssql.rb, line 57
57:       def row_num_literal order_by
58:         Nodes::SqlLiteral.new("ROW_NUMBER() OVER (#{order_by}) as _row_num")
59:       end
select_count?(x) click to toggle source
    # File lib/arel/visitors/mssql.rb, line 61
61:       def select_count? x
62:         x.projections.length == 1 && Arel::Nodes::Count === x.projections.first
63:       end
visit_Arel_Nodes_SelectStatement(o) click to toggle source
    # File lib/arel/visitors/mssql.rb, line 13
13:       def visit_Arel_Nodes_SelectStatement o
14:         if !o.limit && !o.offset
15:           return super o
16:         end
17: 
18:         select_order_by = "ORDER BY #{o.orders.map { |x| visit x }.join(', ')}" unless o.orders.empty?
19: 
20:         is_select_count = false
21:         sql = o.cores.map { |x|
22:           core_order_by = select_order_by || determine_order_by(x)
23:           if select_count? x
24:             x.projections = [row_num_literal(core_order_by)]
25:             is_select_count = true
26:           else
27:             x.projections << row_num_literal(core_order_by)
28:           end
29: 
30:           visit_Arel_Nodes_SelectCore x
31:         }.join
32: 
33:         sql = "SELECT _t.* FROM (#{sql}) as _t WHERE #{get_offset_limit_clause(o)}"
34:         # fixme count distinct wouldn't work with limit or offset
35:         sql = "SELECT COUNT(1) as count_id FROM (#{sql}) AS subquery" if is_select_count
36:         sql
37:       end
visit_Arel_Nodes_Top(o) click to toggle source

`top` wouldn’t really work here. I.e. User.select(“distinct first_name“).limit(10) would generate “select top 10 distinct first_name from users”, which is invalid query! it should be “select distinct top 10 first_name from users“

    # File lib/arel/visitors/mssql.rb, line 9
 9:       def visit_Arel_Nodes_Top o
10:         ""
11:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.