File: tc_sqlbind.rb

package info (click to toggle)
ruby-dbi 0.4.5-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 596 kB
  • sloc: ruby: 2,800; perl: 12; makefile: 10
file content (168 lines) | stat: -rw-r--r-- 4,378 bytes parent folder | download | duplicates (3)
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
$: << 'lib'
require 'test/unit'
require "dbi/sql"

# ====================================================================
class TestSqlBind < Test::Unit::TestCase

  def bind(quoter, sql, args)
      ps = DBI::SQL::PreparedStatement.new(quoter, sql)
      ps.bind(DBI::Utils::ConvParam.conv_param('default', *args))
  end

  def test_one
    assert_equal "10", bind(self, "?", [10])
    assert_equal "'hi'", bind(self, "?", ["hi"])
    assert_equal "I 'don''t' know", bind(self, "I ? know", ["don't"])
  end

  def test_many
    assert_equal "WHERE age=12 AND name='Jim'",
      bind(self, "WHERE age=? AND name=?", [12, 'Jim'])
  end

  def test_too_many
    assert_raises (RuntimeError) {
      bind(self, "age=?", [10, 11])
    }
  end

  def test_too_few
    assert_raises (RuntimeError) {
      bind(self, "age in (?, ?, ?)", [10, 11])
    }
  end

  def test_embedded_questions
    assert_equal "10 ? 11", bind(self, "? ?? ?", [10, 11])
    assert_equal "????", bind(self, "????????", [])
  end

  def test_questions_in_param
    assert_equal "WHERE c='connected?'",
      bind(self, "WHERE c=?", ["connected?"])

    assert_equal "WHERE c='connected?' AND d='???'",
      bind(self, "WHERE c=? AND d=?", ["connected?", "???"])
  end

  def test_questions_in_quotes
    assert_equal "WHERE c='connected?' AND d=10",
      bind(self, "WHERE c='connected?' AND d=?", [10])
  end

  def test_comment_dan
    sql = %{--Dan's query\n--random comment\nselect column1, column2\nfrom table1\nwhere somevalue = ?}
    res = %{--Dan's query\n--random comment\nselect column1, column2\nfrom table1\nwhere somevalue = 10}
    assert_equal res, bind(self, sql, [10])
  end

  def test_minus_bug
    sql = "SELECT 1 - 3"
    res = "SELECT 1 - 3"
    assert_equal res, bind(self, sql, []) 
  end

  def test_minus2
    sql = "SELECT * from test --Dan's query" 
    assert_equal sql, bind(self, sql, []) 
  end

  def test_slash
    sql = "SELECT 5 / 4"
    res = "SELECT 5 / 4"
    assert_equal res, bind(self, sql, []) 
  end

  def test_much
    sql = <<ENDSQL
SELECT s.id, cwajga_magic_number((cwajga_product(r.rating) ^ (1 / 1)), MAX(lastplay.lastheard), 5) as magic
INTO TEMP magic_order
FROM song AS s LEFT OUTER JOIN rating AS r ON s.id = r.song LEFT OUTER JOIN last play ON lastplay.song = s.id
WHERE r.name ILIKE 'omega697'
GROUP BY s.id;

SELECT SUM(magic) as total INTO TEMP magic_tot FROM magic_order;

SELECT id, 100.0*magic/total as percent
FROM magic_order, magic_tot
order by percent;
ENDSQL
    res = sql
    assert_equal res, bind(self, sql, [])
  end

  def test_nested_insert
    sql = "insert into logins (user_id, hostmask) values ((select id from users where username = ?), ?)"
    res = sql.sub(/\?/, "1")
    res.sub!(/\?/, "'foo@bar'")
    assert_equal res, bind(self, sql, [1, "foo@bar"])
  end

end

######################################################################
class TestLex < Test::Unit::TestCase

    def tokens(sql)
        DBI::SQL::PreparedStatement.tokens(sql)
    end

  def test_non_strings
    assert_equal ['This is _a t35t'],
      tokens("This is _a t35t")
  end

  def test_simple_strings
    assert_equal ["hi ", "'hello world'"],
      tokens("hi 'hello world'")
    assert_equal ["c = ", "''"],
      tokens("c = ''")
  end

  def test_strings_with_quotes
    assert_equal ["hi ", "'''lo world'"],
      tokens("hi '''lo world'")
    assert_equal ['a', "''''", 'b'],
      tokens("a''''b")
  end

  def test_strings_with_escaped_quotes
    assert_equal ["hi ", "'don\\'t do that'"],
      tokens("hi 'don\\'t do that'")
    assert_equal ['a', "'\\''", 'b'],
      tokens("a'\\''b")
  end

  def test_simple_dq_strings
    assert_equal ["hi ", '"hello world"'],
      tokens('hi "hello world"')
    assert_equal ["c = ", '""'],
      tokens('c = ""')
  end

  def test_dq_strings_with_quotes
    assert_equal ["hi ", '"""lo world"'],
      tokens('hi """lo world"')
    assert_equal ['a', '""""', 'b'],
      tokens('a""""b')
  end

  def test_dq_strings_with_escaped_quotes
    assert_equal ["hi ", '"don\"t do that"'],
      tokens('hi "don\"t do that"')
    assert_equal ['a', '"\""', 'b'],
      tokens('a"\""b')
  end

  def test_qmarks
    assert_equal ["a = ", "?"],
      tokens("a = ?")
    assert_equal ["'?'", " = ", "?"],
      tokens("'?' = ?")
    assert_equal ["'?'", " = ", "??"],
      tokens("'?' = ??")
  end

end