aboutsummaryrefslogtreecommitdiffstats
path: root/test/visitors/test_mysql.rb
blob: 487db325e813863cacf698c2a4738b9827d74ae8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require 'helper'

module Arel
  module Visitors
    describe 'the mysql visitor' do
      before do
        @visitor = MySQL.new Table.engine.connection_pool
      end

      it 'squashes parenthesis on multiple unions' do
        subnode = Nodes::Union.new 'left', 'right'
        node    = Nodes::Union.new subnode, 'topright'
        assert_equal 1, @visitor.accept(node).scan('(').length

        subnode = Nodes::Union.new 'left', 'right'
        node    = Nodes::Union.new 'topleft', subnode
        assert_equal 1, @visitor.accept(node).scan('(').length
      end

      ###
      # :'(
      # http://dev.mysql.com/doc/refman/5.0/en/select.html#id3482214
      it 'defaults limit to 18446744073709551615' do
        stmt = Nodes::SelectStatement.new
        stmt.offset = Nodes::Offset.new(1)
        sql = @visitor.accept(stmt)
        sql.must_be_like "SELECT FROM DUAL LIMIT 18446744073709551615 OFFSET 1"
      end

      it "should escape LIMIT" do
        sc = Arel::Nodes::UpdateStatement.new
        sc.limit = Nodes::Limit.new("omg")
        assert_equal("UPDATE NULL LIMIT 'omg'", @visitor.accept(sc))
      end

      it 'uses DUAL for empty from' do
        stmt = Nodes::SelectStatement.new
        sql = @visitor.accept(stmt)
        sql.must_be_like "SELECT FROM DUAL"
      end

      describe 'locking' do
        it 'defaults to FOR UPDATE when locking' do
          node = Nodes::Lock.new(Arel.sql('FOR UPDATE'))
          @visitor.accept(node).must_be_like "FOR UPDATE"
        end

        it 'allows a custom string to be used as a lock' do
          node = Nodes::Lock.new(Arel.sql('LOCK IN SHARE MODE'))
          @visitor.accept(node).must_be_like "LOCK IN SHARE MODE"
        end
      end
    end
  end
end