sqlite-ruby 2.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README +34 -0
- data/doc/faq/faq.html +390 -0
- data/doc/faq/faq.rb +145 -0
- data/doc/faq/faq.yml +453 -0
- data/ext/extconf.rb +8 -0
- data/ext/sqlite-api.c +1412 -0
- data/lib/sqlite/database.rb +682 -0
- data/lib/sqlite/parsed_statement.rb +233 -0
- data/lib/sqlite/pragmas.rb +236 -0
- data/lib/sqlite/resultset.rb +168 -0
- data/lib/sqlite/statement.rb +145 -0
- data/lib/sqlite/translator.rb +135 -0
- data/lib/sqlite/version.rb +45 -0
- data/lib/sqlite.rb +34 -0
- data/test/db/fixtures.sql +25 -0
- data/test/tc_api_core.rb +201 -0
- data/test/tc_arrayfields.rb +74 -0
- data/test/tc_database.rb +335 -0
- data/test/tc_parsed_statement.rb +160 -0
- data/test/tc_pragmas.rb +207 -0
- data/test/tc_translator.rb +115 -0
- data/test/tc_type_translation.rb +55 -0
- data/test/tests.rb +133 -0
- metadata +67 -0
data/test/tc_database.rb
ADDED
@@ -0,0 +1,335 @@
|
|
1
|
+
#--
|
2
|
+
# =============================================================================
|
3
|
+
# Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
|
4
|
+
# All rights reserved.
|
5
|
+
#
|
6
|
+
# Redistribution and use in source and binary forms, with or without
|
7
|
+
# modification, are permitted provided that the following conditions are met:
|
8
|
+
#
|
9
|
+
# * Redistributions of source code must retain the above copyright notice,
|
10
|
+
# this list of conditions and the following disclaimer.
|
11
|
+
#
|
12
|
+
# * Redistributions in binary form must reproduce the above copyright
|
13
|
+
# notice, this list of conditions and the following disclaimer in the
|
14
|
+
# documentation and/or other materials provided with the distribution.
|
15
|
+
#
|
16
|
+
# * The names of its contributors may not be used to endorse or promote
|
17
|
+
# products derived from this software without specific prior written
|
18
|
+
# permission.
|
19
|
+
#
|
20
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
21
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
22
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
23
|
+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
24
|
+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
25
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
26
|
+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
27
|
+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
28
|
+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
29
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
30
|
+
# =============================================================================
|
31
|
+
#++
|
32
|
+
|
33
|
+
$:.unshift "lib"
|
34
|
+
|
35
|
+
require 'sqlite'
|
36
|
+
require 'test/unit'
|
37
|
+
|
38
|
+
class TC_Database < Test::Unit::TestCase
|
39
|
+
|
40
|
+
def setup
|
41
|
+
@db = SQLite::Database.open( "db/fixtures.db" )
|
42
|
+
end
|
43
|
+
|
44
|
+
def teardown
|
45
|
+
@db.close
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_constants
|
49
|
+
assert_equal "constant", defined?( SQLite::Version::MAJOR )
|
50
|
+
assert_equal "constant", defined?( SQLite::Version::MINOR )
|
51
|
+
assert_equal "constant", defined?( SQLite::Version::TINY )
|
52
|
+
assert_equal "constant", defined?( SQLite::Version::STRING )
|
53
|
+
|
54
|
+
expected = [ SQLite::Version::MAJOR, SQLite::Version::MINOR,
|
55
|
+
SQLite::Version::TINY ].join( "." )
|
56
|
+
|
57
|
+
assert_equal expected, SQLite::Version::STRING
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_execute_no_block
|
61
|
+
rows = @db.execute( "select * from A order by name limit 2" )
|
62
|
+
|
63
|
+
assert_equal [ [nil, "6"], ["Amber", "5"] ], rows
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_execute_with_block
|
67
|
+
expect = [ [nil, "6"], ["Amber", "5"] ]
|
68
|
+
@db.execute( "select * from A order by name limit 2" ) do |row|
|
69
|
+
assert_equal expect.shift, row
|
70
|
+
end
|
71
|
+
assert expect.empty?
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_execute2_no_block
|
75
|
+
columns, *rows = @db.execute2( "select * from A order by name limit 2" )
|
76
|
+
|
77
|
+
assert_equal [ "name", "age" ], columns
|
78
|
+
assert_equal [ [nil, "6"], ["Amber", "5"] ], rows
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_execute2_with_block
|
82
|
+
expect = [ ["name", "age"], [nil, "6"], ["Amber", "5"] ]
|
83
|
+
@db.execute2( "select * from A order by name limit 2" ) do |row|
|
84
|
+
assert_equal expect.shift, row
|
85
|
+
end
|
86
|
+
assert expect.empty?
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_bind_vars
|
90
|
+
rows = @db.execute( "select * from A where name = ?", "Amber" )
|
91
|
+
assert_equal [ ["Amber", "5"] ], rows
|
92
|
+
rows = @db.execute( "select * from A where name = ?", 15 )
|
93
|
+
assert_equal [], rows
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_result_hash
|
97
|
+
@db.results_as_hash = true
|
98
|
+
rows = @db.execute( "select * from A where name = ?", "Amber" )
|
99
|
+
assert_equal [ {"name"=>"Amber", 0=>"Amber", "age"=>"5", 1=>"5"} ], rows
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_result_hash_types
|
103
|
+
@db.results_as_hash = true
|
104
|
+
rows = @db.execute( "select * from A where name = ?", "Amber" )
|
105
|
+
assert_equal [ "VARCHAR(60)", "INTEGER" ], rows[0].types
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_query
|
109
|
+
@db.query( "select * from A where name = ?", "Amber" ) do |result|
|
110
|
+
row = result.next
|
111
|
+
assert_equal [ "Amber", "5"], row
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def test_metadata
|
116
|
+
@db.query( "select * from A where name = ?", "Amber" ) do |result|
|
117
|
+
assert_equal [ "name", "age" ], result.columns
|
118
|
+
assert_equal [ "VARCHAR(60)", "INTEGER" ], result.types
|
119
|
+
assert_equal [ "Amber", "5"], result.next
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_get_first_row
|
124
|
+
row = @db.get_first_row( "select * from A order by name" )
|
125
|
+
assert_equal [ nil, "6" ], row
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_get_first_value
|
129
|
+
age = @db.get_first_value( "select age from A order by name" )
|
130
|
+
assert_equal "6", age
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_create_function
|
134
|
+
@db.create_function( "maim", 1 ) do |func, value|
|
135
|
+
if value.nil?
|
136
|
+
func.set_result nil
|
137
|
+
else
|
138
|
+
func.set_result value.split(//).sort.join
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
value = @db.get_first_value( "select maim(name) from A where name='Amber'" )
|
143
|
+
assert_equal "Abemr", value
|
144
|
+
end
|
145
|
+
|
146
|
+
def test_create_aggregate
|
147
|
+
step = proc do |func, value|
|
148
|
+
func[ :total ] ||= 0
|
149
|
+
func[ :total ] += ( value ? value.length : 0 )
|
150
|
+
end
|
151
|
+
|
152
|
+
finalize = proc do |func|
|
153
|
+
func.set_result( func[ :total ] || 0 )
|
154
|
+
end
|
155
|
+
|
156
|
+
@db.create_aggregate( "lengths", 1, step, finalize )
|
157
|
+
|
158
|
+
value = @db.get_first_value( "select lengths(name) from A" )
|
159
|
+
assert_equal "33", value
|
160
|
+
end
|
161
|
+
|
162
|
+
def test_set_error
|
163
|
+
@db.create_function( "barf", 1 ) do |func, value|
|
164
|
+
func.set_error "oops! I did it again"
|
165
|
+
end
|
166
|
+
|
167
|
+
assert_raise( SQLite::Exceptions::SQLException ) do
|
168
|
+
@db.get_first_value( "select barf(name) from A where name='Amber'" )
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
def test_context_on_nonaggregate
|
173
|
+
@db.create_function( "barf1", 1 ) do |func, value|
|
174
|
+
assert_raise( SQLite::Exceptions::MisuseException ) do
|
175
|
+
func['hello']
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
@db.create_function( "barf2", 1 ) do |func, value|
|
180
|
+
assert_raise( SQLite::Exceptions::MisuseException ) do
|
181
|
+
func['hello'] = "world"
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
@db.create_function( "barf3", 1 ) do |func, value|
|
186
|
+
assert_raise( SQLite::Exceptions::MisuseException ) do
|
187
|
+
func.count
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
@db.get_first_value( "select barf1(name) from A where name='Amber'" )
|
192
|
+
@db.get_first_value( "select barf2(name) from A where name='Amber'" )
|
193
|
+
@db.get_first_value( "select barf3(name) from A where name='Amber'" )
|
194
|
+
end
|
195
|
+
|
196
|
+
class LengthsAggregate
|
197
|
+
def self.function_type
|
198
|
+
:numeric
|
199
|
+
end
|
200
|
+
|
201
|
+
def self.arity
|
202
|
+
1
|
203
|
+
end
|
204
|
+
|
205
|
+
def self.name
|
206
|
+
"lengths"
|
207
|
+
end
|
208
|
+
|
209
|
+
def initialize
|
210
|
+
@total = 0
|
211
|
+
end
|
212
|
+
|
213
|
+
def step( ctx, name )
|
214
|
+
@total += ( name ? name.length : 0 )
|
215
|
+
end
|
216
|
+
|
217
|
+
def finalize( ctx )
|
218
|
+
ctx.set_result( @total )
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
def test_create_aggregate_handler
|
223
|
+
@db.create_aggregate_handler LengthsAggregate
|
224
|
+
|
225
|
+
result = @db.get_first_value( "select lengths(name) from A" )
|
226
|
+
assert_equal "33", result
|
227
|
+
end
|
228
|
+
|
229
|
+
def test_prepare
|
230
|
+
stmt = @db.prepare( "select * from A" )
|
231
|
+
assert_equal "", stmt.remainder
|
232
|
+
assert_equal [ "name", "age" ], stmt.columns
|
233
|
+
assert_equal [ "VARCHAR(60)", "INTEGER" ], stmt.types
|
234
|
+
stmt.execute do |result|
|
235
|
+
row = result.next
|
236
|
+
assert_equal [ "Zephyr", "1" ], row
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
def test_execute_batch
|
241
|
+
count = @db.get_first_value( "select count(*) from A" ).to_i
|
242
|
+
|
243
|
+
@db.execute_batch( %q{--- query number one
|
244
|
+
insert into A ( age, name ) values ( 200, 'test' );
|
245
|
+
/* query number
|
246
|
+
* two */
|
247
|
+
insert into A ( age, name ) values ( 201, 'test2' );
|
248
|
+
insert into A ( age, name ) values ( 202, /* comment here */ 'test3' )} )
|
249
|
+
new_count = @db.get_first_value( "select count(*) from A" ).to_i
|
250
|
+
assert_equal 3, new_count - count
|
251
|
+
|
252
|
+
@db.execute_batch( %q{--- query number one
|
253
|
+
delete from A where age = 200;
|
254
|
+
/* query number
|
255
|
+
* two */
|
256
|
+
delete from A where age = 201;
|
257
|
+
delete from /* comment */ A where age = 202;} )
|
258
|
+
|
259
|
+
new_count = @db.get_first_value( "select count(*) from A" ).to_i
|
260
|
+
assert_equal new_count, count
|
261
|
+
end
|
262
|
+
|
263
|
+
def test_transaction_block_errors
|
264
|
+
assert_raise( SQLite::Exceptions::SQLException ) do
|
265
|
+
@db.transaction do
|
266
|
+
@db.commit
|
267
|
+
end
|
268
|
+
end
|
269
|
+
|
270
|
+
assert_raise( SQLite::Exceptions::SQLException ) do
|
271
|
+
@db.transaction do
|
272
|
+
@db.rollback
|
273
|
+
end
|
274
|
+
end
|
275
|
+
end
|
276
|
+
|
277
|
+
def test_transaction_errors
|
278
|
+
assert_raise( SQLite::Exceptions::SQLException ) do
|
279
|
+
@db.commit
|
280
|
+
end
|
281
|
+
assert_raise( SQLite::Exceptions::SQLException ) do
|
282
|
+
@db.rollback
|
283
|
+
end
|
284
|
+
end
|
285
|
+
|
286
|
+
def test_transaction_block_good
|
287
|
+
count = @db.get_first_value( "select count(*) from A" ).to_i
|
288
|
+
begin
|
289
|
+
@db.transaction do |db|
|
290
|
+
assert @db.transaction_active?
|
291
|
+
db.execute( "insert into A values ( 'bogus', 1 )" )
|
292
|
+
sub_count = db.get_first_value( "select count(*) from A" ).to_i
|
293
|
+
assert_equal count+1, sub_count
|
294
|
+
raise "testing rollback..."
|
295
|
+
end
|
296
|
+
rescue Exception
|
297
|
+
end
|
298
|
+
new_count = @db.get_first_value( "select count(*) from A" ).to_i
|
299
|
+
assert_equal count, new_count
|
300
|
+
|
301
|
+
@db.transaction do |db|
|
302
|
+
db.execute( "insert into A values ( 'bogus', 1 )" )
|
303
|
+
sub_count = db.get_first_value( "select count(*) from A" ).to_i
|
304
|
+
assert_equal count+1, sub_count
|
305
|
+
end
|
306
|
+
new_count = @db.get_first_value( "select count(*) from A" ).to_i
|
307
|
+
assert_equal count+1, new_count
|
308
|
+
|
309
|
+
@db.execute( "delete from A where name = ?", "bogus" )
|
310
|
+
end
|
311
|
+
|
312
|
+
def test_transaction_explicit
|
313
|
+
count = @db.get_first_value( "select count(*) from A" ).to_i
|
314
|
+
|
315
|
+
@db.transaction
|
316
|
+
assert @db.transaction_active?
|
317
|
+
@db.execute( "insert into A values ( 'bogus', 1 )" )
|
318
|
+
sub_count = @db.get_first_value( "select count(*) from A" ).to_i
|
319
|
+
assert_equal count+1, sub_count
|
320
|
+
@db.rollback
|
321
|
+
sub_count = @db.get_first_value( "select count(*) from A" ).to_i
|
322
|
+
assert_equal count, sub_count
|
323
|
+
|
324
|
+
@db.transaction
|
325
|
+
@db.execute( "insert into A values ( 'bogus', 1 )" )
|
326
|
+
sub_count = @db.get_first_value( "select count(*) from A" ).to_i
|
327
|
+
assert_equal count+1, sub_count
|
328
|
+
@db.commit
|
329
|
+
sub_count = @db.get_first_value( "select count(*) from A" ).to_i
|
330
|
+
assert_equal count+1, sub_count
|
331
|
+
|
332
|
+
@db.execute( "delete from A where name = ?", "bogus" )
|
333
|
+
end
|
334
|
+
|
335
|
+
end
|
@@ -0,0 +1,160 @@
|
|
1
|
+
#--
|
2
|
+
# =============================================================================
|
3
|
+
# Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
|
4
|
+
# All rights reserved.
|
5
|
+
#
|
6
|
+
# Redistribution and use in source and binary forms, with or without
|
7
|
+
# modification, are permitted provided that the following conditions are met:
|
8
|
+
#
|
9
|
+
# * Redistributions of source code must retain the above copyright notice,
|
10
|
+
# this list of conditions and the following disclaimer.
|
11
|
+
#
|
12
|
+
# * Redistributions in binary form must reproduce the above copyright
|
13
|
+
# notice, this list of conditions and the following disclaimer in the
|
14
|
+
# documentation and/or other materials provided with the distribution.
|
15
|
+
#
|
16
|
+
# * The names of its contributors may not be used to endorse or promote
|
17
|
+
# products derived from this software without specific prior written
|
18
|
+
# permission.
|
19
|
+
#
|
20
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
21
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
22
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
23
|
+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
24
|
+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
25
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
26
|
+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
27
|
+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
28
|
+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
29
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
30
|
+
# =============================================================================
|
31
|
+
#++
|
32
|
+
|
33
|
+
$:.unshift "lib"
|
34
|
+
|
35
|
+
require 'sqlite/parsed_statement'
|
36
|
+
require 'test/unit'
|
37
|
+
|
38
|
+
class TC_ParsedStatement < Test::Unit::TestCase
|
39
|
+
|
40
|
+
def test_trailing
|
41
|
+
sql = %q{first; and second}
|
42
|
+
stmt = SQLite::ParsedStatement.new( sql )
|
43
|
+
assert_equal "first", stmt.sql
|
44
|
+
assert_equal " and second", stmt.trailing
|
45
|
+
|
46
|
+
sql = %q{first}
|
47
|
+
stmt = SQLite::ParsedStatement.new( sql )
|
48
|
+
assert_equal "first", stmt.sql
|
49
|
+
assert_equal "", stmt.trailing
|
50
|
+
end
|
51
|
+
|
52
|
+
def test_text_only_statement
|
53
|
+
sql = %q{select * from some.table t where ( t.b = 'a string' )}
|
54
|
+
|
55
|
+
stmt = SQLite::ParsedStatement.new( sql )
|
56
|
+
expected = "select * from some.table t where ( t.b = 'a string' )"
|
57
|
+
|
58
|
+
assert_equal expected, stmt.sql
|
59
|
+
assert_equal expected, stmt.to_s
|
60
|
+
assert_equal expected, stmt.to_str
|
61
|
+
|
62
|
+
stmt.bind_params
|
63
|
+
assert_equal expected, stmt.to_s
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_bind_single_positional_param
|
67
|
+
sql = %q{select * from some.table t where ( t.b = ? )}
|
68
|
+
|
69
|
+
expected = "select * from some.table t where ( t.b = NULL )"
|
70
|
+
stmt = SQLite::ParsedStatement.new( sql )
|
71
|
+
assert_equal expected, stmt.to_s
|
72
|
+
|
73
|
+
expected = "select * from some.table t where ( t.b = 'a string' )"
|
74
|
+
|
75
|
+
stmt = SQLite::ParsedStatement.new( sql )
|
76
|
+
stmt.bind_params( "a string" )
|
77
|
+
assert_equal expected, stmt.to_s
|
78
|
+
|
79
|
+
stmt = SQLite::ParsedStatement.new( sql )
|
80
|
+
stmt.bind_param( 1, "a string" )
|
81
|
+
assert_equal expected, stmt.to_s
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_bind_multiple_positional_params
|
85
|
+
sql = %q{? and ? and ?}
|
86
|
+
expected = "'one' and NULL and 'O''Reilly'"
|
87
|
+
|
88
|
+
stmt = SQLite::ParsedStatement.new( sql )
|
89
|
+
stmt.bind_param( 1, "one" )
|
90
|
+
stmt.bind_param( 3, "O'Reilly" )
|
91
|
+
stmt.bind_param( 4, "ignored" )
|
92
|
+
assert_equal expected, stmt.to_s
|
93
|
+
|
94
|
+
stmt = SQLite::ParsedStatement.new( sql )
|
95
|
+
stmt.bind_params( "one", nil, "O'Reilly", "ignored" )
|
96
|
+
assert_equal expected, stmt.to_s
|
97
|
+
end
|
98
|
+
|
99
|
+
def test_syntax_bind_positional_params
|
100
|
+
sql = %q{? and ? and ?1}
|
101
|
+
expected = "'one' and NULL and 'one'"
|
102
|
+
|
103
|
+
stmt = SQLite::ParsedStatement.new( sql )
|
104
|
+
stmt.bind_param( 1, "one" )
|
105
|
+
stmt.bind_param( 3, "O'Reilly" )
|
106
|
+
stmt.bind_param( 4, "ignored" )
|
107
|
+
assert_equal expected, stmt.to_s
|
108
|
+
|
109
|
+
stmt = SQLite::ParsedStatement.new( sql )
|
110
|
+
stmt.bind_params( "one", nil, "O'Reilly", "ignored" )
|
111
|
+
assert_equal expected, stmt.to_s
|
112
|
+
|
113
|
+
sql = %q{:2 and ? and ?1 and :4:}
|
114
|
+
expected = "NULL and 'O''Reilly' and 'one' and 'ignored'"
|
115
|
+
stmt = SQLite::ParsedStatement.new( sql )
|
116
|
+
stmt.bind_params( "one", nil, "O'Reilly", "ignored" )
|
117
|
+
assert_equal expected, stmt.to_s
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_bind_named_params
|
121
|
+
sql = %q{:name and :spouse:}
|
122
|
+
expected = "'joe' and NULL"
|
123
|
+
|
124
|
+
stmt = SQLite::ParsedStatement.new( sql )
|
125
|
+
stmt.bind_param( "name", "joe" )
|
126
|
+
assert_equal expected, stmt.to_s
|
127
|
+
|
128
|
+
stmt = SQLite::ParsedStatement.new( sql )
|
129
|
+
stmt.bind_params( "name"=>"joe", "spouse"=>nil )
|
130
|
+
assert_equal expected, stmt.to_s
|
131
|
+
|
132
|
+
stmt = SQLite::ParsedStatement.new( sql )
|
133
|
+
stmt.bind_params( "name"=>"joe", "spouse"=>"jane" )
|
134
|
+
assert_equal "'joe' and 'jane'", stmt.to_s
|
135
|
+
end
|
136
|
+
|
137
|
+
def test_mixed_params
|
138
|
+
sql = %q{:name and :spouse: and ?2 and ? and :1 and :2:}
|
139
|
+
stmt = SQLite::ParsedStatement.new( sql )
|
140
|
+
stmt.bind_params( "one", 2, "three",
|
141
|
+
"name"=>"joe", "spouse"=>"jane" )
|
142
|
+
|
143
|
+
assert_equal "'joe' and 'jane' and 2 and 'three' and 'one' and 2", stmt.to_s
|
144
|
+
end
|
145
|
+
|
146
|
+
def test_sql
|
147
|
+
sql = %q{:name and :spouse: and ?2 and ? and :1 and :2:}
|
148
|
+
stmt = SQLite::ParsedStatement.new( sql )
|
149
|
+
assert_equal ":name and :spouse and :2 and :3 and :1 and :2", stmt.sql
|
150
|
+
end
|
151
|
+
|
152
|
+
def test_placeholders
|
153
|
+
sql = %q{:name and :spouse: and ? and ?5 and :12 and :15:}
|
154
|
+
|
155
|
+
stmt = SQLite::ParsedStatement.new( sql )
|
156
|
+
assert_equal 6, stmt.placeholders.length
|
157
|
+
assert( ( [ "name", "spouse", 1, 5, 12, 15 ] - stmt.placeholders ).empty? )
|
158
|
+
end
|
159
|
+
|
160
|
+
end
|
data/test/tc_pragmas.rb
ADDED
@@ -0,0 +1,207 @@
|
|
1
|
+
#--
|
2
|
+
# =============================================================================
|
3
|
+
# Copyright (c) 2004, Jamis Buck (jgb3@email.byu.edu)
|
4
|
+
# All rights reserved.
|
5
|
+
#
|
6
|
+
# Redistribution and use in source and binary forms, with or without
|
7
|
+
# modification, are permitted provided that the following conditions are met:
|
8
|
+
#
|
9
|
+
# * Redistributions of source code must retain the above copyright notice,
|
10
|
+
# this list of conditions and the following disclaimer.
|
11
|
+
#
|
12
|
+
# * Redistributions in binary form must reproduce the above copyright
|
13
|
+
# notice, this list of conditions and the following disclaimer in the
|
14
|
+
# documentation and/or other materials provided with the distribution.
|
15
|
+
#
|
16
|
+
# * The names of its contributors may not be used to endorse or promote
|
17
|
+
# products derived from this software without specific prior written
|
18
|
+
# permission.
|
19
|
+
#
|
20
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
21
|
+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
22
|
+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
23
|
+
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
|
24
|
+
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
25
|
+
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
26
|
+
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
27
|
+
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
28
|
+
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
29
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
30
|
+
# =============================================================================
|
31
|
+
#++
|
32
|
+
|
33
|
+
$:.unshift "lib"
|
34
|
+
|
35
|
+
require 'sqlite'
|
36
|
+
require 'test/unit'
|
37
|
+
|
38
|
+
class TC_Pragmas < Test::Unit::TestCase
|
39
|
+
|
40
|
+
def setup
|
41
|
+
@db = SQLite::Database.open( "db/fixtures.db" )
|
42
|
+
end
|
43
|
+
|
44
|
+
def teardown
|
45
|
+
@db.close
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_integrity_check
|
49
|
+
assert_nothing_raised do
|
50
|
+
@db.integrity_check
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_cache_size
|
55
|
+
size = @db.cache_size
|
56
|
+
assert_instance_of Fixnum, size
|
57
|
+
@db.cache_size = size + 100
|
58
|
+
new_size = @db.cache_size
|
59
|
+
assert_equal size+100, new_size
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_default_cache_size
|
63
|
+
size = @db.default_cache_size
|
64
|
+
assert_instance_of Fixnum, size
|
65
|
+
@db.default_cache_size = size + 100
|
66
|
+
new_size = @db.default_cache_size
|
67
|
+
assert_equal size+100, new_size
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_synchronous
|
71
|
+
assert_raise( SQLite::Exceptions::DatabaseException ) do
|
72
|
+
@db.synchronous = "bogus"
|
73
|
+
end
|
74
|
+
|
75
|
+
assert_nothing_raised do
|
76
|
+
@db.synchronous = "full"
|
77
|
+
@db.synchronous = 2
|
78
|
+
@db.synchronous = "normal"
|
79
|
+
@db.synchronous = 1
|
80
|
+
@db.synchronous = "off"
|
81
|
+
@db.synchronous = 0
|
82
|
+
end
|
83
|
+
|
84
|
+
assert_equal "0", @db.synchronous
|
85
|
+
end
|
86
|
+
|
87
|
+
def test_default_synchronous
|
88
|
+
assert_raise( SQLite::Exceptions::DatabaseException ) do
|
89
|
+
@db.default_synchronous = "bogus"
|
90
|
+
end
|
91
|
+
|
92
|
+
assert_nothing_raised do
|
93
|
+
@db.default_synchronous = "full"
|
94
|
+
@db.default_synchronous = 2
|
95
|
+
@db.default_synchronous = "normal"
|
96
|
+
@db.default_synchronous = 1
|
97
|
+
@db.default_synchronous = "off"
|
98
|
+
@db.default_synchronous = 0
|
99
|
+
end
|
100
|
+
|
101
|
+
assert_equal "0", @db.default_synchronous
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_temp_store
|
105
|
+
assert_raise( SQLite::Exceptions::DatabaseException ) do
|
106
|
+
@db.temp_store = "bogus"
|
107
|
+
end
|
108
|
+
|
109
|
+
assert_nothing_raised do
|
110
|
+
@db.temp_store = "memory"
|
111
|
+
@db.temp_store = 2
|
112
|
+
@db.temp_store = "file"
|
113
|
+
@db.temp_store = 1
|
114
|
+
@db.temp_store = "default"
|
115
|
+
@db.temp_store = 0
|
116
|
+
end
|
117
|
+
|
118
|
+
assert_equal "0", @db.temp_store
|
119
|
+
end
|
120
|
+
|
121
|
+
def test_default_temp_store
|
122
|
+
assert_raise( SQLite::Exceptions::DatabaseException ) do
|
123
|
+
@db.default_temp_store = "bogus"
|
124
|
+
end
|
125
|
+
|
126
|
+
assert_nothing_raised do
|
127
|
+
@db.default_temp_store = "memory"
|
128
|
+
@db.default_temp_store = 2
|
129
|
+
@db.default_temp_store = "file"
|
130
|
+
@db.default_temp_store = 1
|
131
|
+
@db.default_temp_store = "default"
|
132
|
+
@db.default_temp_store = 0
|
133
|
+
end
|
134
|
+
|
135
|
+
assert_equal "0", @db.default_temp_store
|
136
|
+
end
|
137
|
+
|
138
|
+
def test_full_column_names
|
139
|
+
assert_raise( SQLite::Exceptions::DatabaseException ) do
|
140
|
+
@db.full_column_names = "sure"
|
141
|
+
end
|
142
|
+
|
143
|
+
assert_raise( SQLite::Exceptions::DatabaseException ) do
|
144
|
+
@db.full_column_names = :yes
|
145
|
+
end
|
146
|
+
|
147
|
+
assert_nothing_raised do
|
148
|
+
@db.full_column_names = "yes"
|
149
|
+
@db.full_column_names = "no"
|
150
|
+
@db.full_column_names = 1
|
151
|
+
@db.full_column_names = 0
|
152
|
+
@db.full_column_names = true
|
153
|
+
@db.full_column_names = false
|
154
|
+
@db.full_column_names = nil
|
155
|
+
@db.full_column_names = "y"
|
156
|
+
@db.full_column_names = "n"
|
157
|
+
@db.full_column_names = "t"
|
158
|
+
@db.full_column_names = "f"
|
159
|
+
end
|
160
|
+
|
161
|
+
assert !@db.full_column_names
|
162
|
+
end
|
163
|
+
|
164
|
+
def test_parser_trace
|
165
|
+
# apparently, the parser_trace pragma always returns true...?
|
166
|
+
assert @db.parser_trace
|
167
|
+
#@db.parser_trace = false
|
168
|
+
#assert !@db.parser_trace
|
169
|
+
end
|
170
|
+
|
171
|
+
def test_vdbe_trace
|
172
|
+
@db.vdbe_trace = true
|
173
|
+
assert @db.vdbe_trace
|
174
|
+
@db.vdbe_trace = false
|
175
|
+
assert !@db.vdbe_trace
|
176
|
+
end
|
177
|
+
|
178
|
+
def test_database_list
|
179
|
+
assert_equal ["main","temp"], @db.database_list.map { |i| i[1] }
|
180
|
+
end
|
181
|
+
|
182
|
+
def test_foreign_key_list
|
183
|
+
list = @db.foreign_key_list( "D" )
|
184
|
+
assert_equal 1, list.size
|
185
|
+
assert_equal "B", list.first[2]
|
186
|
+
end
|
187
|
+
|
188
|
+
def test_index_info
|
189
|
+
info = @db.index_info( "B_idx" )
|
190
|
+
assert_equal 1, info.size
|
191
|
+
assert_equal "name", info.first[2]
|
192
|
+
end
|
193
|
+
|
194
|
+
def test_index_list
|
195
|
+
list = @db.index_list( "B" )
|
196
|
+
assert_equal 1, list.size
|
197
|
+
assert_equal "B_idx", list.first[1]
|
198
|
+
end
|
199
|
+
|
200
|
+
def test_table_info
|
201
|
+
info = @db.table_info( "A" )
|
202
|
+
assert_equal 2, info.size
|
203
|
+
assert_equal "name", info[0][1]
|
204
|
+
assert_equal "age", info[1][1]
|
205
|
+
end
|
206
|
+
|
207
|
+
end
|