~ubuntu-branches/ubuntu/vivid/ruby-sequel/vivid

« back to all changes in this revision

Viewing changes to lib/sequel/adapters/utils/emulate_offset_with_row_number.rb

  • Committer: Package Import Robot
  • Author(s): Dmitry Borodaenko, Dmitry Borodaenko, Cédric Boutillier
  • Date: 2013-08-10 18:38:17 UTC
  • mfrom: (1.1.8)
  • Revision ID: package-import@ubuntu.com-20130810183817-iqanz804j32i5myi
Tags: 4.1.1-1
[ Dmitry Borodaenko ]
* New upstream release.
* Standards-Version upgraded to 3.9.4 (no changes).
* Added Build-Depend on ruby-sqlite3.

[ Cédric Boutillier ]
* debian/control: remove obsolete DM-Upload-Allowed flag.
* use canonical URI in Vcs-* fields.
* debian/copyright: use DEP5 copyright-format/1.0 official URL for Format
  field.
* Update debian/watch. Thanks Bart Martens.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
module Sequel
2
2
  module EmulateOffsetWithRowNumber
3
 
    # When a subselect that uses :offset is used in IN or NOT IN,
4
 
    # use a nested subselect that only includes the first column
5
 
    # instead of the ROW_NUMBER column added by the emulated offset support.
6
 
    def complex_expression_sql_append(sql, op, args)
7
 
      case op
8
 
      when :IN, :"NOT IN"
9
 
        ds = args.at(1)
10
 
        if ds.is_a?(Sequel::Dataset) && ds.opts[:offset]
11
 
          c = ds.opts[:select].first
12
 
          case c
13
 
          when Symbol
14
 
            t, cl, a = split_symbol(c)
15
 
            if a
16
 
              c = SQL::Identifier.new(a)
17
 
            elsif t
18
 
              c = SQL::Identifier.new(cl)
19
 
            end
20
 
          when SQL::AliasedExpression
21
 
            c = SQL::Identifier.new(c.aliaz)
22
 
          when SQL::QualifiedIdentifier
23
 
            c = SQL::Identifier.new(c.column)
24
 
          end
25
 
          super(sql, op, [args.at(0), ds.from_self.select(c)])
26
 
        else
27
 
          super
28
 
        end
29
 
      else
30
 
        super
31
 
      end
32
 
    end
33
 
 
34
3
    # Emulate OFFSET support with the ROW_NUMBER window function
35
4
    # 
36
5
    # The implementation is ugly, cloning the current dataset and modifying
47
16
        raise(Error, "#{db.database_type} requires an order be provided if using an offset")
48
17
      end
49
18
 
 
19
      columns = clone(:append_sql=>'').columns
50
20
      dsa1 = dataset_alias(1)
51
21
      rn = row_number_column
52
22
      sql = @opts[:append_sql] || ''
54
24
        unordered.
55
25
        select_append{ROW_NUMBER(:over, :order=>order){}.as(rn)}.
56
26
        from_self(:alias=>dsa1).
 
27
        select(*columns).
57
28
        limit(@opts[:limit]).
58
29
        where(SQL::Identifier.new(rn) > o).
59
30
        order(rn))
67
38
    def default_offset_order
68
39
      clone(:append_sql=>'').columns
69
40
    end
70
 
 
71
 
    # This emulation adds an extra row number column that should be
72
 
    # eliminated.
73
 
    def offset_returns_row_number_column?
74
 
      true
75
 
    end
76
41
  end
77
42
end