sqlite3-ruby 1.2.3-x86-mingw32
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of sqlite3-ruby might be problematic. Click here for more details.
- data/README.rdoc +51 -0
- data/doc/faq/faq.html +408 -0
- data/doc/faq/faq.rb +145 -0
- data/doc/faq/faq.yml +426 -0
- data/ext/sqlite3_api/MANIFEST +4 -0
- data/ext/sqlite3_api/Makefile +142 -0
- data/ext/sqlite3_api/extconf.rb +16 -0
- data/ext/sqlite3_api/mkmf.log +66 -0
- data/ext/sqlite3_api/sqlite3_api.i +358 -0
- data/ext/sqlite3_api/sqlite3_api.so +0 -0
- data/ext/sqlite3_api/sqlite3_api_wrap.c +3094 -0
- data/ext/sqlite3_api/sqlite3_api_wrap.o +0 -0
- data/ext/sqlite3_api/win32/build.bat +7 -0
- data/lib/sqlite3.rb +1 -0
- data/lib/sqlite3/constants.rb +49 -0
- data/lib/sqlite3/database.rb +715 -0
- data/lib/sqlite3/driver/dl/api.rb +152 -0
- data/lib/sqlite3/driver/dl/driver.rb +307 -0
- data/lib/sqlite3/driver/native/driver.rb +211 -0
- data/lib/sqlite3/errors.rb +68 -0
- data/lib/sqlite3/pragmas.rb +271 -0
- data/lib/sqlite3/resultset.rb +176 -0
- data/lib/sqlite3/statement.rb +230 -0
- data/lib/sqlite3/translator.rb +109 -0
- data/lib/sqlite3/value.rb +57 -0
- data/lib/sqlite3/version.rb +14 -0
- data/test/bm.rb +140 -0
- data/test/driver/dl/tc_driver.rb +292 -0
- data/test/mocks.rb +45 -0
- data/test/native-vs-dl.rb +126 -0
- data/test/tc_database.rb +198 -0
- data/test/tc_errors.rb +21 -0
- data/test/tc_integration.rb +1044 -0
- data/test/tests.rb +6 -0
- metadata +96 -0
data/test/mocks.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
gem 'mocha'
|
3
|
+
|
4
|
+
require 'mocha'
|
5
|
+
|
6
|
+
class Driver < Mocha::Mock
|
7
|
+
def initialize
|
8
|
+
super
|
9
|
+
stubs( :open ).returns([0, 'cookie'])
|
10
|
+
stubs( :close ).returns(0)
|
11
|
+
stubs( :complete? ).returns(0)
|
12
|
+
stubs( :errmsg ).returns('')
|
13
|
+
stubs( :errcode ).returns(0)
|
14
|
+
stubs( :trace ).returns(nil)
|
15
|
+
stubs( :set_authorizer ).returns(0)
|
16
|
+
stubs( :prepare ).returns([0, 'stmt', 'remainder'])
|
17
|
+
stubs( :finalize ).returns(0)
|
18
|
+
stubs( :changes ).returns(14)
|
19
|
+
stubs( :total_changes ).returns(28)
|
20
|
+
stubs( :interrupt ).returns(0)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class MockResultSet < Mocha::Mock
|
25
|
+
def initialize
|
26
|
+
super
|
27
|
+
stubs( :each ).yields(['foo'])
|
28
|
+
stubs( :columns ).returns(['name'])
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class Statement < Mocha::Mock
|
33
|
+
attr_reader :handle
|
34
|
+
attr_reader :sql
|
35
|
+
attr_reader :last_result
|
36
|
+
|
37
|
+
def initialize( handle, sql )
|
38
|
+
super()
|
39
|
+
@handle = handle
|
40
|
+
@sql = sql
|
41
|
+
stubs( :close ).returns(0)
|
42
|
+
stubs( :remainder ).returns('')
|
43
|
+
stubs( :execute ).returns(MockResultSet.new)
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,126 @@
|
|
1
|
+
$:.unshift "../lib", "../ext/sqlite3_api"
|
2
|
+
|
3
|
+
require 'sqlite3'
|
4
|
+
|
5
|
+
require 'benchmark'
|
6
|
+
|
7
|
+
N = 1000
|
8
|
+
|
9
|
+
$VERBOSE=nil
|
10
|
+
|
11
|
+
puts "database creation..."
|
12
|
+
Benchmark.bm( 7 ) do |x|
|
13
|
+
x.report('dl') do
|
14
|
+
N.times do
|
15
|
+
File.delete "test.db" rescue nil
|
16
|
+
SQLite3::Database.open( "test.db", :driver => "DL" ).close
|
17
|
+
end
|
18
|
+
end
|
19
|
+
x.report('native') do
|
20
|
+
N.times do
|
21
|
+
File.delete "test.db" rescue nil
|
22
|
+
SQLite3::Database.open( "test.db", :driver => "Native" ).close
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
File.delete "test.db" rescue nil
|
27
|
+
|
28
|
+
SQLite3::Database.open( "test.db" ).close
|
29
|
+
|
30
|
+
puts
|
31
|
+
puts "database open..."
|
32
|
+
Benchmark.bm( 7 ) do |x|
|
33
|
+
x.report('dl') do
|
34
|
+
N.times do
|
35
|
+
SQLite3::Database.open( "test.db", :driver => "DL" ).close
|
36
|
+
end
|
37
|
+
end
|
38
|
+
x.report('native') do
|
39
|
+
N.times do
|
40
|
+
SQLite3::Database.open( "test.db", :driver => "Native" ).close
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
File.delete "test.db" rescue nil
|
45
|
+
|
46
|
+
dl = SQLite3::Database.open( "test-dl.db", :driver => "DL" )
|
47
|
+
native = SQLite3::Database.open( "test-native.db", :driver => "Native" )
|
48
|
+
|
49
|
+
dl.execute "create table foo (a,b)"
|
50
|
+
native.execute "create table foo (a,b)"
|
51
|
+
|
52
|
+
puts
|
53
|
+
puts "insertions"
|
54
|
+
Benchmark.bm( 7 ) do |x|
|
55
|
+
x.report('dl') do
|
56
|
+
dl.transaction do
|
57
|
+
N.times do |i|
|
58
|
+
dl.execute "insert into foo values (#{i}, #{i+1})"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
x.report('native') do
|
63
|
+
native.transaction do
|
64
|
+
N.times do |i|
|
65
|
+
native.execute "insert into foo values (#{i}, #{i+1})"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
puts
|
72
|
+
puts "insertions using prepared statement"
|
73
|
+
Benchmark.bm( 7 ) do |x|
|
74
|
+
x.report('dl') do
|
75
|
+
dl.transaction do
|
76
|
+
dl.prepare "insert into foo values (?,?)" do |stmt|
|
77
|
+
N.times { |i| stmt.execute i, i+1 }
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
x.report('native') do
|
82
|
+
native.transaction do
|
83
|
+
native.prepare( "insert into foo values (?,?)" ) do |stmt|
|
84
|
+
N.times { |i| stmt.execute i, i+1 }
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
dl.close
|
91
|
+
native.close
|
92
|
+
File.delete "test-dl.db" rescue nil
|
93
|
+
File.delete "test-native.db" rescue nil
|
94
|
+
|
95
|
+
dl = SQLite3::Database.open( "test-dl.db", :driver => "DL" )
|
96
|
+
native = SQLite3::Database.open( "test-native.db", :driver => "Native" )
|
97
|
+
|
98
|
+
dl.execute "create table foo (a,b)"
|
99
|
+
dl.execute "insert into foo values (1,2)"
|
100
|
+
dl.execute "insert into foo values (3,4)"
|
101
|
+
dl.execute "insert into foo values (5,6)"
|
102
|
+
|
103
|
+
native.execute "create table foo (a,b)"
|
104
|
+
native.execute "insert into foo values (1,2)"
|
105
|
+
native.execute "insert into foo values (3,4)"
|
106
|
+
native.execute "insert into foo values (5,6)"
|
107
|
+
|
108
|
+
puts
|
109
|
+
puts "queries"
|
110
|
+
Benchmark.bm( 7 ) do |x|
|
111
|
+
x.report('dl') do
|
112
|
+
N.times do
|
113
|
+
dl.execute "select * from foo"
|
114
|
+
end
|
115
|
+
end
|
116
|
+
x.report('native') do
|
117
|
+
N.times do
|
118
|
+
native.execute "select * from foo"
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
dl.close
|
124
|
+
native.close
|
125
|
+
File.delete "test-dl.db" rescue nil
|
126
|
+
File.delete "test-native.db" rescue nil
|
data/test/tc_database.rb
ADDED
@@ -0,0 +1,198 @@
|
|
1
|
+
$:.unshift "../lib"
|
2
|
+
|
3
|
+
require 'sqlite3/database'
|
4
|
+
require 'test/unit'
|
5
|
+
|
6
|
+
require 'mocks'
|
7
|
+
|
8
|
+
class TC_Database_Init < Test::Unit::TestCase
|
9
|
+
def test_new
|
10
|
+
# any_instance fails here...
|
11
|
+
driver = Driver.new
|
12
|
+
driver.expects(:open).once.with('foo.db', false).returns([0, 'cookie'])
|
13
|
+
Driver.stubs(:new).returns(driver)
|
14
|
+
db = SQLite3::Database.new( 'foo.db', :driver => Driver )
|
15
|
+
assert !db.closed?
|
16
|
+
assert !db.results_as_hash
|
17
|
+
assert !db.type_translation
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_open
|
21
|
+
driver = Driver.new
|
22
|
+
driver.expects(:open).once.with('foo.db', false).returns([0, 'cookie'])
|
23
|
+
Driver.stubs(:new).returns(driver)
|
24
|
+
db = SQLite3::Database.open( "foo.db", :driver => Driver )
|
25
|
+
assert !db.closed?
|
26
|
+
assert !db.results_as_hash
|
27
|
+
assert !db.type_translation
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_with_type_translation
|
31
|
+
db = SQLite3::Database.open( "foo.db", :driver => Driver,
|
32
|
+
:type_translation => true )
|
33
|
+
assert db.type_translation
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_with_results_as_hash
|
37
|
+
db = SQLite3::Database.open( "foo.db", :driver => Driver,
|
38
|
+
:results_as_hash => true )
|
39
|
+
assert db.results_as_hash
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_with_type_translation_and_results_as_hash
|
43
|
+
db = SQLite3::Database.open( "foo.db", :driver => Driver,
|
44
|
+
:results_as_hash => true,
|
45
|
+
:type_translation => true )
|
46
|
+
assert db.results_as_hash
|
47
|
+
assert db.type_translation
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
class TC_Database < Test::Unit::TestCase
|
52
|
+
def setup
|
53
|
+
@db = SQLite3::Database.open( "foo.db",
|
54
|
+
:driver => Driver, :statement_factory => Statement )
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_quote
|
58
|
+
assert_equal "''one''two''three''", SQLite3::Database.quote(
|
59
|
+
"'one'two'three'" )
|
60
|
+
end
|
61
|
+
|
62
|
+
def test_complete
|
63
|
+
Driver.any_instance.expects(:complete?)
|
64
|
+
@db.complete? "foo"
|
65
|
+
end
|
66
|
+
|
67
|
+
def test_errmsg
|
68
|
+
Driver.any_instance.expects(:errmsg)
|
69
|
+
@db.errmsg
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_errcode
|
73
|
+
Driver.any_instance.expects(:errcode)
|
74
|
+
@db.errcode
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_translator
|
78
|
+
translator = @db.translator
|
79
|
+
assert_instance_of SQLite3::Translator, translator
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_close
|
83
|
+
Driver.any_instance.expects(:close).returns(0)
|
84
|
+
@db.close
|
85
|
+
assert @db.closed?
|
86
|
+
Driver.any_instance.expects(:close).never
|
87
|
+
@db.close
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_trace
|
91
|
+
Driver.any_instance.expects(:trace).with('cookie', 15)
|
92
|
+
@db.trace( 15 ) { "foo" }
|
93
|
+
# assert_equal 1, driver.mock_blocks[:trace].length
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_authorizer
|
97
|
+
Driver.any_instance.expects(:set_authorizer).with('cookie', 15).returns(0)
|
98
|
+
@db.authorizer( 15 ) { "foo" }
|
99
|
+
# assert_equal 1, driver.mock_blocks[:set_authorizer].length
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_prepare_no_block
|
103
|
+
Statement.any_instance.expects(:close).never
|
104
|
+
assert_nothing_raised { @db.prepare( "foo" ) }
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_prepare_with_block
|
108
|
+
called = false
|
109
|
+
# any_instance fails here...
|
110
|
+
statement = Statement.new('cookie', 'foo')
|
111
|
+
statement.expects(:close).once
|
112
|
+
Statement.stubs(:new).returns(statement)
|
113
|
+
@db.prepare( "foo" ) { |stmt| called = true }
|
114
|
+
assert called
|
115
|
+
end
|
116
|
+
|
117
|
+
def test_execute_no_block
|
118
|
+
# any_instance fails here...
|
119
|
+
statement = Statement.new('cookie', 'foo')
|
120
|
+
statement.expects(:execute).with('bar', 'baz').returns(MockResultSet.new)
|
121
|
+
Statement.stubs(:new).returns(statement)
|
122
|
+
MockResultSet.any_instance.stubs(:inject).returns([['foo']])
|
123
|
+
result = @db.execute( "foo", "bar", "baz" )
|
124
|
+
assert_equal [["foo"]], result
|
125
|
+
end
|
126
|
+
|
127
|
+
def test_execute_with_block
|
128
|
+
called = false
|
129
|
+
# any_instance fails here...
|
130
|
+
statement = Statement.new('cookie', 'foo')
|
131
|
+
statement.expects(:execute).with('bar', 'baz').returns(MockResultSet.new)
|
132
|
+
Statement.stubs(:new).returns(statement)
|
133
|
+
@db.execute( "foo", "bar", "baz" ) do |row|
|
134
|
+
called = true
|
135
|
+
assert_equal ["foo"], row
|
136
|
+
end
|
137
|
+
|
138
|
+
assert called
|
139
|
+
end
|
140
|
+
|
141
|
+
def test_execute2_no_block
|
142
|
+
# any_instance fails here...
|
143
|
+
statement = Statement.new('cookie', 'foo')
|
144
|
+
statement.expects(:execute).with('bar', 'baz').returns(MockResultSet.new)
|
145
|
+
Statement.stubs(:new).returns(statement)
|
146
|
+
MockResultSet.any_instance.stubs(:inject).returns([['name'], ['foo']])
|
147
|
+
result = @db.execute2( "foo", "bar", "baz" )
|
148
|
+
assert_equal [["name"],["foo"]], result
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_execute2_with_block
|
152
|
+
called = false
|
153
|
+
parts = [ ["name"],["foo"] ]
|
154
|
+
# any_instance fails here...
|
155
|
+
statement = Statement.new('cookie', 'foo')
|
156
|
+
statement.expects(:execute).with('bar', 'baz').returns(MockResultSet.new)
|
157
|
+
Statement.stubs(:new).returns(statement)
|
158
|
+
@db.execute2( "foo", "bar", "baz" ) do |row|
|
159
|
+
called = true
|
160
|
+
assert_equal parts.shift, row
|
161
|
+
end
|
162
|
+
|
163
|
+
assert called
|
164
|
+
end
|
165
|
+
|
166
|
+
def test_execute_batch
|
167
|
+
# any_instance fails here...
|
168
|
+
statement = Statement.new('cookie', 'foo')
|
169
|
+
statement.expects(:execute).with('bar', 'baz').returns(MockResultSet.new)
|
170
|
+
Statement.stubs(:new).returns(statement)
|
171
|
+
@db.execute_batch( "foo", "bar", "baz" )
|
172
|
+
end
|
173
|
+
|
174
|
+
def test_get_first_row
|
175
|
+
result = @db.get_first_row( "foo", "bar", "baz" )
|
176
|
+
assert_equal ["foo"], result
|
177
|
+
end
|
178
|
+
|
179
|
+
def test_get_first_value
|
180
|
+
result = @db.get_first_value( "foo", "bar", "baz" )
|
181
|
+
assert_equal "foo", result
|
182
|
+
end
|
183
|
+
|
184
|
+
def test_changes
|
185
|
+
Driver.any_instance.expects(:changes).returns(14)
|
186
|
+
assert_equal 14, @db.changes
|
187
|
+
end
|
188
|
+
|
189
|
+
def test_total_changes
|
190
|
+
Driver.any_instance.expects(:total_changes).returns(28)
|
191
|
+
assert_equal 28, @db.total_changes
|
192
|
+
end
|
193
|
+
|
194
|
+
def test_interrupt
|
195
|
+
Driver.any_instance.expects(:interrupt)
|
196
|
+
@db.interrupt
|
197
|
+
end
|
198
|
+
end
|
data/test/tc_errors.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
$:.unshift "../lib"
|
2
|
+
|
3
|
+
require 'sqlite3/errors'
|
4
|
+
require 'test/unit'
|
5
|
+
require 'mocha'
|
6
|
+
|
7
|
+
class TC_Errors < Test::Unit::TestCase
|
8
|
+
(1..26).each do |code|
|
9
|
+
define_method( "test_error_code_%02d" % code ) do
|
10
|
+
db = stub('database', :errmsg => 'message')
|
11
|
+
begin
|
12
|
+
SQLite3::Error.check( code, db )
|
13
|
+
rescue SQLite3::Exception => e
|
14
|
+
assert_instance_of SQLite3::EXCEPTIONS[code], e
|
15
|
+
assert_equal code, e.code
|
16
|
+
assert_equal code, e.class.code
|
17
|
+
assert_equal "message", e.message
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,1044 @@
|
|
1
|
+
$:.unshift "#{File.dirname(__FILE__)}/../lib"
|
2
|
+
$:.unshift "#{File.dirname(__FILE__)}/../ext/sqlite3_api"
|
3
|
+
|
4
|
+
require 'test/unit'
|
5
|
+
require 'benchmark'
|
6
|
+
require 'sqlite3/database'
|
7
|
+
require 'thread'
|
8
|
+
|
9
|
+
class String
|
10
|
+
def to_utf16(terminate=false)
|
11
|
+
self.split(//).map { |c| c[0] }.pack("v*") +
|
12
|
+
(terminate ? "\0\0" : "")
|
13
|
+
end
|
14
|
+
|
15
|
+
def from_utf16
|
16
|
+
result = ""
|
17
|
+
length.times do |i|
|
18
|
+
result << self[i,1] if i % 2 == 0 && self[i] != 0
|
19
|
+
end
|
20
|
+
result
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
module Integration
|
25
|
+
|
26
|
+
drivers_to_test = ( ENV["SQLITE3_DRIVERS"] || "Native" ).split(',')
|
27
|
+
drivers_to_test.each do |driver|
|
28
|
+
|
29
|
+
# == TC_OpenClose =========================================================
|
30
|
+
|
31
|
+
test_case = Class.new( Test::Unit::TestCase ) do
|
32
|
+
define_method( "test_create_close" ) do
|
33
|
+
begin
|
34
|
+
db = SQLite3::Database.new( "test-create.db",
|
35
|
+
:driver => driver )
|
36
|
+
assert File.exist?( "test-create.db" )
|
37
|
+
assert_nothing_raised { db.close }
|
38
|
+
ensure
|
39
|
+
File.delete( "test-create.db" ) rescue nil
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
define_method( "test_open_close" ) do
|
44
|
+
begin
|
45
|
+
File.open( "test-open.db", "w" ) { |f| }
|
46
|
+
assert File.exist?( "test-open.db" )
|
47
|
+
db = SQLite3::Database.new( "test-open.db",
|
48
|
+
:driver => driver )
|
49
|
+
assert_nothing_raised { db.close }
|
50
|
+
ensure
|
51
|
+
File.delete( "test-open.db" ) rescue nil
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
define_method( "test_bad_open" ) do
|
56
|
+
assert_raise( SQLite3::CantOpenException ) do
|
57
|
+
SQLite3::Database.new( ".", :driver => driver )
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
const_set( "TC_OpenClose_#{driver}", test_case )
|
62
|
+
|
63
|
+
# == TC_Database ==========================================================
|
64
|
+
|
65
|
+
test_case = Class.new( Test::Unit::TestCase ) do
|
66
|
+
define_method( "setup" ) do
|
67
|
+
@db = SQLite3::Database.new( "test.db", :driver=>driver )
|
68
|
+
@db.transaction do
|
69
|
+
@db.execute "create table foo ( a integer primary key, b text )"
|
70
|
+
@db.execute "insert into foo ( b ) values ( 'foo' )"
|
71
|
+
@db.execute "insert into foo ( b ) values ( 'bar' )"
|
72
|
+
@db.execute "insert into foo ( b ) values ( 'baz' )"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
define_method( "teardown" ) do
|
77
|
+
@db.close
|
78
|
+
File.delete( "test.db" )
|
79
|
+
end
|
80
|
+
|
81
|
+
define_method( "test_table_info_with_type_translation_active" ) do
|
82
|
+
@db.type_translation = true
|
83
|
+
assert_nothing_raised { @db.table_info("foo") }
|
84
|
+
end
|
85
|
+
|
86
|
+
define_method( "test_table_info_with_defaults_for_version_3_3_8_and_higher" ) do
|
87
|
+
@db.transaction do
|
88
|
+
@db.execute "create table defaults_test ( a string default NULL, b string default 'Hello' )"
|
89
|
+
data = @db.table_info( "defaults_test" )
|
90
|
+
assert_equal({"name" => "a", "type" => "string", "dflt_value" => nil, "notnull" => "0", "cid" => "0", "pk" => "0"},
|
91
|
+
data[0])
|
92
|
+
assert_equal({"name" => "b", "type" => "string", "dflt_value" => "Hello", "notnull" => "0", "cid" => "1", "pk" => "0"},
|
93
|
+
data[1])
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
define_method( "test_table_info_without_defaults_for_version_3_3_8_and_higher" ) do
|
98
|
+
@db.transaction do
|
99
|
+
@db.execute "create table no_defaults_test ( a integer default 1, b integer )"
|
100
|
+
data = @db.table_info( "no_defaults_test" )
|
101
|
+
assert_equal({"name" => "a", "type" => "integer", "dflt_value" => "1", "notnull" => "0", "cid" => "0", "pk" => "0"},
|
102
|
+
data[0])
|
103
|
+
assert_equal({"name" => "b", "type" => "integer", "dflt_value" => nil, "notnull" => "0", "cid" => "1", "pk" => "0"},
|
104
|
+
data[1])
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
define_method( "test_complete_fail" ) do
|
109
|
+
assert !@db.complete?( "select * from foo" )
|
110
|
+
end
|
111
|
+
define_method( "test_complete_success" ) do
|
112
|
+
assert @db.complete?( "select * from foo;" )
|
113
|
+
end
|
114
|
+
|
115
|
+
define_method( "test_complete_fail_utf16" ) do
|
116
|
+
assert !@db.complete?( "select * from foo".to_utf16(false), true )
|
117
|
+
end
|
118
|
+
|
119
|
+
define_method( "test_complete_success_utf16" ) do
|
120
|
+
assert @db.complete?( "select * from foo;".to_utf16(true), true )
|
121
|
+
end
|
122
|
+
|
123
|
+
define_method( "test_errmsg" ) do
|
124
|
+
assert_equal "not an error", @db.errmsg
|
125
|
+
end
|
126
|
+
|
127
|
+
define_method( "test_errmsg_utf16" ) do
|
128
|
+
assert_equal "not an error".to_utf16, @db.errmsg(true)
|
129
|
+
end
|
130
|
+
|
131
|
+
define_method( "test_errcode" ) do
|
132
|
+
assert_equal 0, @db.errcode
|
133
|
+
end
|
134
|
+
|
135
|
+
define_method( "test_trace" ) do
|
136
|
+
result = nil
|
137
|
+
@db.trace( "data" ) { |data,sql| result = [ data, sql ]; 0 }
|
138
|
+
@db.execute "select * from foo"
|
139
|
+
assert_equal ["data","select * from foo"], result
|
140
|
+
end
|
141
|
+
|
142
|
+
define_method( "test_authorizer_okay" ) do
|
143
|
+
@db.authorizer( "data" ) { |data,type,a,b,c,d| 0 }
|
144
|
+
rows = @db.execute "select * from foo"
|
145
|
+
assert_equal 3, rows.length
|
146
|
+
end
|
147
|
+
|
148
|
+
define_method( "test_authorizer_error" ) do
|
149
|
+
@db.authorizer( "data" ) { |data,type,a,b,c,d| 1 }
|
150
|
+
assert_raise( SQLite3::AuthorizationException ) do
|
151
|
+
@db.execute "select * from foo"
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
# FIXME: this test is failing with sqlite3 3.2.5
|
156
|
+
# define_method( "test_authorizer_silent" ) do
|
157
|
+
# @db.authorizer( "data" ) { |data,type,a,b,c,d| 2 }
|
158
|
+
# rows = @db.execute "select * from foo"
|
159
|
+
# assert rows.empty?
|
160
|
+
# end
|
161
|
+
|
162
|
+
define_method( "test_prepare_invalid_syntax" ) do
|
163
|
+
assert_raise( SQLite3::SQLException ) do
|
164
|
+
@db.prepare "select from foo"
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
define_method( "test_prepare_invalid_column" ) do
|
169
|
+
assert_raise( SQLite3::SQLException ) do
|
170
|
+
@db.prepare "select k from foo"
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
define_method( "test_prepare_invalid_table" ) do
|
175
|
+
assert_raise( SQLite3::SQLException ) do
|
176
|
+
@db.prepare "select * from barf"
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
define_method( "test_prepare_no_block" ) do
|
181
|
+
stmt = @db.prepare "select * from foo"
|
182
|
+
assert stmt.respond_to?(:execute)
|
183
|
+
stmt.close
|
184
|
+
end
|
185
|
+
|
186
|
+
define_method( "test_prepare_with_block" ) do
|
187
|
+
called = false
|
188
|
+
@db.prepare "select * from foo" do |stmt|
|
189
|
+
called = true
|
190
|
+
assert stmt.respond_to?(:execute)
|
191
|
+
end
|
192
|
+
assert called
|
193
|
+
end
|
194
|
+
|
195
|
+
define_method( "test_execute_no_block_no_bind_no_match" ) do
|
196
|
+
rows = @db.execute( "select * from foo where a > 100" )
|
197
|
+
assert rows.empty?
|
198
|
+
end
|
199
|
+
|
200
|
+
define_method( "test_execute_with_block_no_bind_no_match" ) do
|
201
|
+
called = false
|
202
|
+
@db.execute( "select * from foo where a > 100" ) do |row|
|
203
|
+
called = true
|
204
|
+
end
|
205
|
+
assert !called
|
206
|
+
end
|
207
|
+
|
208
|
+
define_method( "test_execute_no_block_with_bind_no_match" ) do
|
209
|
+
rows = @db.execute( "select * from foo where a > ?", 100 )
|
210
|
+
assert rows.empty?
|
211
|
+
end
|
212
|
+
|
213
|
+
define_method( "test_execute_with_block_with_bind_no_match" ) do
|
214
|
+
called = false
|
215
|
+
@db.execute( "select * from foo where a > ?", 100 ) do |row|
|
216
|
+
called = true
|
217
|
+
end
|
218
|
+
assert !called
|
219
|
+
end
|
220
|
+
|
221
|
+
define_method( "test_execute_no_block_no_bind_with_match" ) do
|
222
|
+
rows = @db.execute( "select * from foo where a = 1" )
|
223
|
+
assert_equal 1, rows.length
|
224
|
+
end
|
225
|
+
|
226
|
+
define_method( "test_execute_with_block_no_bind_with_match" ) do
|
227
|
+
called = 0
|
228
|
+
@db.execute( "select * from foo where a = 1" ) do |row|
|
229
|
+
called += 1
|
230
|
+
end
|
231
|
+
assert_equal 1, called
|
232
|
+
end
|
233
|
+
|
234
|
+
define_method( "test_execute_no_block_with_bind_with_match" ) do
|
235
|
+
rows = @db.execute( "select * from foo where a = ?", 1 )
|
236
|
+
assert_equal 1, rows.length
|
237
|
+
end
|
238
|
+
|
239
|
+
define_method( "test_execute_with_block_with_bind_with_match" ) do
|
240
|
+
called = 0
|
241
|
+
@db.execute( "select * from foo where a = ?", 1 ) do |row|
|
242
|
+
called += 1
|
243
|
+
end
|
244
|
+
assert_equal 1, called
|
245
|
+
end
|
246
|
+
|
247
|
+
define_method( "test_execute2_no_block_no_bind_no_match" ) do
|
248
|
+
columns, *rows = @db.execute2( "select * from foo where a > 100" )
|
249
|
+
assert rows.empty?
|
250
|
+
assert [ "a", "b" ], columns
|
251
|
+
end
|
252
|
+
|
253
|
+
define_method( "test_execute2_with_block_no_bind_no_match" ) do
|
254
|
+
called = 0
|
255
|
+
@db.execute2( "select * from foo where a > 100" ) do |row|
|
256
|
+
assert [ "a", "b" ], row unless called == 0
|
257
|
+
called += 1
|
258
|
+
end
|
259
|
+
assert_equal 1, called
|
260
|
+
end
|
261
|
+
|
262
|
+
define_method( "test_execute2_no_block_with_bind_no_match" ) do
|
263
|
+
columns, *rows = @db.execute2( "select * from foo where a > ?", 100 )
|
264
|
+
assert rows.empty?
|
265
|
+
assert [ "a", "b" ], columns
|
266
|
+
end
|
267
|
+
|
268
|
+
define_method( "test_execute2_with_block_with_bind_no_match" ) do
|
269
|
+
called = 0
|
270
|
+
@db.execute2( "select * from foo where a > ?", 100 ) do |row|
|
271
|
+
assert [ "a", "b" ], row unless called == 0
|
272
|
+
called += 1
|
273
|
+
end
|
274
|
+
assert_equal 1, called
|
275
|
+
end
|
276
|
+
|
277
|
+
define_method( "test_execute2_no_block_no_bind_with_match" ) do
|
278
|
+
columns, *rows = @db.execute2( "select * from foo where a = 1" )
|
279
|
+
assert_equal 1, rows.length
|
280
|
+
assert [ "a", "b" ], columns
|
281
|
+
end
|
282
|
+
|
283
|
+
define_method( "test_execute2_with_block_no_bind_with_match" ) do
|
284
|
+
called = 0
|
285
|
+
@db.execute2( "select * from foo where a = 1" ) do |row|
|
286
|
+
assert [ "a", "b" ], row unless called == 0
|
287
|
+
called += 1
|
288
|
+
end
|
289
|
+
assert_equal 2, called
|
290
|
+
end
|
291
|
+
|
292
|
+
define_method( "test_execute2_no_block_with_bind_with_match" ) do
|
293
|
+
columns, *rows = @db.execute2( "select * from foo where a = ?", 1 )
|
294
|
+
assert_equal 1, rows.length
|
295
|
+
assert [ "a", "b" ], columns
|
296
|
+
end
|
297
|
+
|
298
|
+
define_method( "test_execute2_with_block_with_bind_with_match" ) do
|
299
|
+
called = 0
|
300
|
+
@db.execute2( "select * from foo where a = ?", 1 ) do |row|
|
301
|
+
called += 1
|
302
|
+
end
|
303
|
+
assert_equal 2, called
|
304
|
+
end
|
305
|
+
|
306
|
+
define_method( "test_execute_batch_empty" ) do
|
307
|
+
assert_nothing_raised { @db.execute_batch "" }
|
308
|
+
end
|
309
|
+
|
310
|
+
define_method( "test_execute_batch_no_bind" ) do
|
311
|
+
@db.transaction do
|
312
|
+
@db.execute_batch <<-SQL
|
313
|
+
create table bar ( a, b, c );
|
314
|
+
insert into bar values ( 'one', 2, 'three' );
|
315
|
+
insert into bar values ( 'four', 5, 'six' );
|
316
|
+
insert into bar values ( 'seven', 8, 'nine' );
|
317
|
+
SQL
|
318
|
+
end
|
319
|
+
rows = @db.execute( "select * from bar" )
|
320
|
+
assert_equal 3, rows.length
|
321
|
+
end
|
322
|
+
|
323
|
+
define_method( "test_execute_batch_with_bind" ) do
|
324
|
+
@db.execute_batch( <<-SQL, 1 )
|
325
|
+
create table bar ( a, b, c );
|
326
|
+
insert into bar values ( 'one', 2, ? );
|
327
|
+
insert into bar values ( 'four', 5, ? );
|
328
|
+
insert into bar values ( 'seven', 8, ? );
|
329
|
+
SQL
|
330
|
+
rows = @db.execute( "select * from bar" ).map { |a,b,c| c }
|
331
|
+
assert_equal %w{1 1 1}, rows
|
332
|
+
end
|
333
|
+
|
334
|
+
define_method( "test_query_no_block_no_bind_no_match" ) do
|
335
|
+
result = @db.query( "select * from foo where a > 100" )
|
336
|
+
assert_nil result.next
|
337
|
+
result.close
|
338
|
+
end
|
339
|
+
|
340
|
+
define_method( "test_query_with_block_no_bind_no_match" ) do
|
341
|
+
r = nil
|
342
|
+
@db.query( "select * from foo where a > 100" ) do |result|
|
343
|
+
assert_nil result.next
|
344
|
+
r = result
|
345
|
+
end
|
346
|
+
assert r.closed?
|
347
|
+
end
|
348
|
+
|
349
|
+
define_method( "test_query_no_block_with_bind_no_match" ) do
|
350
|
+
result = @db.query( "select * from foo where a > ?", 100 )
|
351
|
+
assert_nil result.next
|
352
|
+
result.close
|
353
|
+
end
|
354
|
+
|
355
|
+
define_method( "test_query_with_block_with_bind_no_match" ) do
|
356
|
+
r = nil
|
357
|
+
@db.query( "select * from foo where a > ?", 100 ) do |result|
|
358
|
+
assert_nil result.next
|
359
|
+
r = result
|
360
|
+
end
|
361
|
+
assert r.closed?
|
362
|
+
end
|
363
|
+
|
364
|
+
define_method( "test_query_no_block_no_bind_with_match" ) do
|
365
|
+
result = @db.query( "select * from foo where a = 1" )
|
366
|
+
assert_not_nil result.next
|
367
|
+
assert_nil result.next
|
368
|
+
result.close
|
369
|
+
end
|
370
|
+
|
371
|
+
define_method( "test_query_with_block_no_bind_with_match" ) do
|
372
|
+
r = nil
|
373
|
+
@db.query( "select * from foo where a = 1" ) do |result|
|
374
|
+
assert_not_nil result.next
|
375
|
+
assert_nil result.next
|
376
|
+
r = result
|
377
|
+
end
|
378
|
+
assert r.closed?
|
379
|
+
end
|
380
|
+
|
381
|
+
define_method( "test_query_no_block_with_bind_with_match" ) do
|
382
|
+
result = @db.query( "select * from foo where a = ?", 1 )
|
383
|
+
assert_not_nil result.next
|
384
|
+
assert_nil result.next
|
385
|
+
result.close
|
386
|
+
end
|
387
|
+
|
388
|
+
define_method( "test_query_with_block_with_bind_with_match" ) do
|
389
|
+
r = nil
|
390
|
+
@db.query( "select * from foo where a = ?", 1 ) do |result|
|
391
|
+
assert_not_nil result.next
|
392
|
+
assert_nil result.next
|
393
|
+
r = result
|
394
|
+
end
|
395
|
+
assert r.closed?
|
396
|
+
end
|
397
|
+
|
398
|
+
define_method( "test_get_first_row_no_bind_no_match" ) do
|
399
|
+
result = @db.get_first_row( "select * from foo where a=100" )
|
400
|
+
assert_nil result
|
401
|
+
end
|
402
|
+
|
403
|
+
define_method( "test_get_first_row_no_bind_with_match" ) do
|
404
|
+
result = @db.get_first_row( "select * from foo where a=1" )
|
405
|
+
assert_equal [ "1", "foo" ], result
|
406
|
+
end
|
407
|
+
|
408
|
+
define_method( "test_get_first_row_with_bind_no_match" ) do
|
409
|
+
result = @db.get_first_row( "select * from foo where a=?", 100 )
|
410
|
+
assert_nil result
|
411
|
+
end
|
412
|
+
|
413
|
+
define_method( "test_get_first_row_with_bind_with_match" ) do
|
414
|
+
result = @db.get_first_row( "select * from foo where a=?", 1 )
|
415
|
+
assert_equal [ "1", "foo" ], result
|
416
|
+
end
|
417
|
+
|
418
|
+
define_method( "test_get_first_value_no_bind_no_match" ) do
|
419
|
+
result = @db.get_first_value( "select b, a from foo where a=100" )
|
420
|
+
assert_nil result
|
421
|
+
end
|
422
|
+
|
423
|
+
define_method( "test_get_first_value_no_bind_with_match" ) do
|
424
|
+
result = @db.get_first_value( "select b, a from foo where a=1" )
|
425
|
+
assert_equal "foo", result
|
426
|
+
end
|
427
|
+
|
428
|
+
define_method( "test_get_first_value_with_bind_no_match" ) do
|
429
|
+
result = @db.get_first_value( "select b, a from foo where a=?", 100 )
|
430
|
+
assert_nil result
|
431
|
+
end
|
432
|
+
|
433
|
+
define_method( "test_get_first_value_with_bind_with_match" ) do
|
434
|
+
result = @db.get_first_value( "select b, a from foo where a=?", 1 )
|
435
|
+
assert_equal "foo", result
|
436
|
+
end
|
437
|
+
|
438
|
+
define_method( "test_last_insert_row_id" ) do
|
439
|
+
@db.execute "insert into foo ( b ) values ( 'test' )"
|
440
|
+
assert_equal 4, @db.last_insert_row_id
|
441
|
+
@db.execute "insert into foo ( b ) values ( 'again' )"
|
442
|
+
assert_equal 5, @db.last_insert_row_id
|
443
|
+
end
|
444
|
+
|
445
|
+
define_method( "test_changes" ) do
|
446
|
+
@db.execute "insert into foo ( b ) values ( 'test' )"
|
447
|
+
assert_equal 1, @db.changes
|
448
|
+
@db.execute "delete from foo where 1=1"
|
449
|
+
assert_equal 4, @db.changes
|
450
|
+
end
|
451
|
+
|
452
|
+
define_method( "test_total_changes" ) do
|
453
|
+
assert_equal 3, @db.total_changes
|
454
|
+
@db.execute "insert into foo ( b ) values ( 'test' )"
|
455
|
+
@db.execute "delete from foo where 1=1"
|
456
|
+
assert_equal 8, @db.total_changes
|
457
|
+
end
|
458
|
+
|
459
|
+
define_method( "test_transaction_nest" ) do
|
460
|
+
assert_raise( SQLite3::SQLException ) do
|
461
|
+
@db.transaction do
|
462
|
+
@db.transaction do
|
463
|
+
end
|
464
|
+
end
|
465
|
+
end
|
466
|
+
end
|
467
|
+
|
468
|
+
define_method( "test_transaction_rollback" ) do
|
469
|
+
@db.transaction
|
470
|
+
@db.execute_batch <<-SQL
|
471
|
+
insert into foo (b) values ( 'test1' );
|
472
|
+
insert into foo (b) values ( 'test2' );
|
473
|
+
insert into foo (b) values ( 'test3' );
|
474
|
+
insert into foo (b) values ( 'test4' );
|
475
|
+
SQL
|
476
|
+
assert_equal 7, @db.get_first_value("select count(*) from foo").to_i
|
477
|
+
@db.rollback
|
478
|
+
assert_equal 3, @db.get_first_value("select count(*) from foo").to_i
|
479
|
+
end
|
480
|
+
|
481
|
+
define_method( "test_transaction_commit" ) do
|
482
|
+
@db.transaction
|
483
|
+
@db.execute_batch <<-SQL
|
484
|
+
insert into foo (b) values ( 'test1' );
|
485
|
+
insert into foo (b) values ( 'test2' );
|
486
|
+
insert into foo (b) values ( 'test3' );
|
487
|
+
insert into foo (b) values ( 'test4' );
|
488
|
+
SQL
|
489
|
+
assert_equal 7, @db.get_first_value("select count(*) from foo").to_i
|
490
|
+
@db.commit
|
491
|
+
assert_equal 7, @db.get_first_value("select count(*) from foo").to_i
|
492
|
+
end
|
493
|
+
|
494
|
+
define_method( "test_transaction_rollback_in_block" ) do
|
495
|
+
assert_raise( SQLite3::SQLException ) do
|
496
|
+
@db.transaction do
|
497
|
+
@db.rollback
|
498
|
+
end
|
499
|
+
end
|
500
|
+
end
|
501
|
+
|
502
|
+
define_method( "test_transaction_commit_in_block" ) do
|
503
|
+
assert_raise( SQLite3::SQLException ) do
|
504
|
+
@db.transaction do
|
505
|
+
@db.commit
|
506
|
+
end
|
507
|
+
end
|
508
|
+
end
|
509
|
+
|
510
|
+
define_method( "test_transaction_active" ) do
|
511
|
+
assert !@db.transaction_active?
|
512
|
+
@db.transaction
|
513
|
+
assert @db.transaction_active?
|
514
|
+
@db.commit
|
515
|
+
assert !@db.transaction_active?
|
516
|
+
end
|
517
|
+
|
518
|
+
define_method( "no_tests_at" ) do |file,line,method|
|
519
|
+
warn "[(#{self.class}):#{file}:#{line}] no tests for #{method}"
|
520
|
+
end
|
521
|
+
|
522
|
+
define_method( "test_interrupt" ) do
|
523
|
+
@db.create_function( "abort", 1 ) do |func,x|
|
524
|
+
@db.interrupt
|
525
|
+
func.result = x
|
526
|
+
end
|
527
|
+
|
528
|
+
assert_raise( SQLite3::SQLException ) do
|
529
|
+
@db.execute "select abort(a) from foo"
|
530
|
+
end
|
531
|
+
end
|
532
|
+
|
533
|
+
define_method( "test_busy_handler_outwait" ) do
|
534
|
+
busy = Mutex.new
|
535
|
+
busy.lock
|
536
|
+
handler_call_count = 0
|
537
|
+
|
538
|
+
t = Thread.new do
|
539
|
+
begin
|
540
|
+
db2 = SQLite3::Database.open( "test.db", :driver=>driver )
|
541
|
+
db2.transaction( :exclusive ) do
|
542
|
+
busy.lock
|
543
|
+
end
|
544
|
+
ensure
|
545
|
+
db2.close if db2
|
546
|
+
end
|
547
|
+
end
|
548
|
+
|
549
|
+
@db.busy_handler do |data,count|
|
550
|
+
handler_call_count += 1
|
551
|
+
busy.unlock
|
552
|
+
true
|
553
|
+
end
|
554
|
+
|
555
|
+
assert_nothing_raised do
|
556
|
+
@db.execute "insert into foo (b) values ( 'from 2' )"
|
557
|
+
end
|
558
|
+
|
559
|
+
t.join
|
560
|
+
|
561
|
+
assert_equal 1, handler_call_count
|
562
|
+
end
|
563
|
+
|
564
|
+
define_method( "test_busy_handler_impatient" ) do
|
565
|
+
busy = Mutex.new
|
566
|
+
busy.lock
|
567
|
+
handler_call_count = 0
|
568
|
+
|
569
|
+
t = Thread.new do
|
570
|
+
begin
|
571
|
+
db2 = SQLite3::Database.open( "test.db", :driver=>driver )
|
572
|
+
db2.transaction( :exclusive ) do
|
573
|
+
busy.lock
|
574
|
+
end
|
575
|
+
ensure
|
576
|
+
db2.close if db2
|
577
|
+
end
|
578
|
+
end
|
579
|
+
|
580
|
+
@db.busy_handler do |data, count|
|
581
|
+
handler_call_count += 1
|
582
|
+
false
|
583
|
+
end
|
584
|
+
|
585
|
+
assert_raise( SQLite3::BusyException ) do
|
586
|
+
@db.execute "insert into foo (b) values ( 'from 2' )"
|
587
|
+
end
|
588
|
+
|
589
|
+
busy.unlock
|
590
|
+
t.join
|
591
|
+
|
592
|
+
assert_equal 1, handler_call_count
|
593
|
+
end
|
594
|
+
|
595
|
+
define_method( "test_busy_timeout" ) do
|
596
|
+
@db.busy_timeout 1000
|
597
|
+
busy = Mutex.new
|
598
|
+
busy.lock
|
599
|
+
|
600
|
+
t = Thread.new do
|
601
|
+
begin
|
602
|
+
db2 = SQLite3::Database.open( "test.db", :driver=>driver )
|
603
|
+
db2.transaction( :exclusive ) do
|
604
|
+
busy.lock
|
605
|
+
end
|
606
|
+
ensure
|
607
|
+
db2.close if db2
|
608
|
+
end
|
609
|
+
end
|
610
|
+
|
611
|
+
time = Benchmark.measure do
|
612
|
+
assert_raise( SQLite3::BusyException ) do
|
613
|
+
@db.execute "insert into foo (b) values ( 'from 2' )"
|
614
|
+
end
|
615
|
+
end
|
616
|
+
|
617
|
+
busy.unlock
|
618
|
+
t.join
|
619
|
+
|
620
|
+
assert time.real*1000 >= 1000
|
621
|
+
end
|
622
|
+
|
623
|
+
define_method( "test_create_function" ) do
|
624
|
+
@db.create_function( "munge", 1 ) do |func,x|
|
625
|
+
func.result = ">>>#{x}<<<"
|
626
|
+
end
|
627
|
+
|
628
|
+
value = @db.get_first_value( "select munge(b) from foo where a=1" )
|
629
|
+
assert_match( />>>.*<<</, value )
|
630
|
+
end
|
631
|
+
|
632
|
+
define_method( "test_create_aggregate_without_block" ) do
|
633
|
+
step = proc do |ctx,a|
|
634
|
+
ctx[:sum] ||= 0
|
635
|
+
ctx[:sum] += a.to_i
|
636
|
+
end
|
637
|
+
|
638
|
+
final = proc { |ctx| ctx.result = ctx[:sum] }
|
639
|
+
|
640
|
+
@db.create_aggregate( "accumulate", 1, step, final )
|
641
|
+
|
642
|
+
value = @db.get_first_value( "select accumulate(a) from foo" )
|
643
|
+
assert_equal "6", value
|
644
|
+
end
|
645
|
+
|
646
|
+
define_method( "test_create_aggregate_with_block" ) do
|
647
|
+
@db.create_aggregate( "accumulate", 1 ) do
|
648
|
+
step do |ctx,a|
|
649
|
+
ctx[:sum] ||= 0
|
650
|
+
ctx[:sum] += a.to_i
|
651
|
+
end
|
652
|
+
|
653
|
+
finalize { |ctx| ctx.result = ctx[:sum] }
|
654
|
+
end
|
655
|
+
|
656
|
+
value = @db.get_first_value( "select accumulate(a) from foo" )
|
657
|
+
assert_equal "6", value
|
658
|
+
end
|
659
|
+
|
660
|
+
define_method( "test_create_aggregate_with_no_data" ) do
|
661
|
+
@db.create_aggregate( "accumulate", 1 ) do
|
662
|
+
step do |ctx,a|
|
663
|
+
ctx[:sum] ||= 0
|
664
|
+
ctx[:sum] += a.to_i
|
665
|
+
end
|
666
|
+
|
667
|
+
finalize { |ctx| ctx.result = ctx[:sum] || 0 }
|
668
|
+
end
|
669
|
+
|
670
|
+
value = @db.get_first_value(
|
671
|
+
"select accumulate(a) from foo where a = 100" )
|
672
|
+
assert_equal "0", value
|
673
|
+
end
|
674
|
+
|
675
|
+
define_method( "test_create_aggregate_handler" ) do
|
676
|
+
handler = Class.new do
|
677
|
+
class << self
|
678
|
+
define_method( "arity" ) { 1 }
|
679
|
+
define_method( "text_rep" ) { SQLite3::Constants::TextRep::ANY }
|
680
|
+
define_method( "name" ) { "multiply" }
|
681
|
+
end
|
682
|
+
define_method( "step" ) do |ctx,a|
|
683
|
+
ctx[:buffer] ||= 1
|
684
|
+
ctx[:buffer] *= a.to_i
|
685
|
+
end
|
686
|
+
define_method( "finalize" ) { |ctx| ctx.result = ctx[:buffer] }
|
687
|
+
end
|
688
|
+
|
689
|
+
@db.create_aggregate_handler( handler )
|
690
|
+
value = @db.get_first_value( "select multiply(a) from foo" )
|
691
|
+
assert_equal "6", value
|
692
|
+
end
|
693
|
+
|
694
|
+
define_method( "test_bind_array_parameter" ) do
|
695
|
+
result = @db.get_first_value( "select b from foo where a=? and b=?",
|
696
|
+
[ 1, "foo" ] )
|
697
|
+
assert_equal "foo", result
|
698
|
+
end
|
699
|
+
end
|
700
|
+
const_set( "TC_Database_#{driver}", test_case )
|
701
|
+
|
702
|
+
# == TC_Statement =========================================================
|
703
|
+
|
704
|
+
test_case = Class.new( Test::Unit::TestCase ) do
|
705
|
+
define_method( "setup" ) do
|
706
|
+
@db = SQLite3::Database.new( "test.db", :driver=>driver )
|
707
|
+
@db.transaction do
|
708
|
+
@db.execute "create table foo ( a integer primary key, b text )"
|
709
|
+
@db.execute "insert into foo ( b ) values ( 'foo' )"
|
710
|
+
@db.execute "insert into foo ( b ) values ( 'bar' )"
|
711
|
+
@db.execute "insert into foo ( b ) values ( 'baz' )"
|
712
|
+
end
|
713
|
+
@stmt = @db.prepare( "select * from foo where a in ( ?, :named )" )
|
714
|
+
end
|
715
|
+
|
716
|
+
define_method( "teardown" ) do
|
717
|
+
@stmt.close
|
718
|
+
@db.close
|
719
|
+
File.delete( "test.db" )
|
720
|
+
end
|
721
|
+
|
722
|
+
define_method( "test_remainder_empty" ) do
|
723
|
+
assert_equal "", @stmt.remainder
|
724
|
+
end
|
725
|
+
|
726
|
+
define_method( "test_remainder_nonempty" ) do
|
727
|
+
called = false
|
728
|
+
@db.prepare( "select * from foo;\n blah" ) do |stmt|
|
729
|
+
called = true
|
730
|
+
assert_equal "\n blah", stmt.remainder
|
731
|
+
end
|
732
|
+
assert called
|
733
|
+
end
|
734
|
+
|
735
|
+
define_method( "test_bind_params_empty" ) do
|
736
|
+
assert_nothing_raised { @stmt.bind_params }
|
737
|
+
assert @stmt.execute!.empty?
|
738
|
+
end
|
739
|
+
|
740
|
+
define_method( "test_bind_params_array" ) do
|
741
|
+
@stmt.bind_params 1, 2
|
742
|
+
assert_equal 2, @stmt.execute!.length
|
743
|
+
end
|
744
|
+
|
745
|
+
define_method( "test_bind_params_hash" ) do
|
746
|
+
@stmt.bind_params ":named" => 2
|
747
|
+
assert_equal 1, @stmt.execute!.length
|
748
|
+
end
|
749
|
+
|
750
|
+
define_method( "test_bind_params_hash_without_colon" ) do
|
751
|
+
@stmt.bind_params "named" => 2
|
752
|
+
assert_equal 1, @stmt.execute!.length
|
753
|
+
end
|
754
|
+
|
755
|
+
define_method( "test_bind_params_hash_as_symbol" ) do
|
756
|
+
@stmt.bind_params :named => 2
|
757
|
+
assert_equal 1, @stmt.execute!.length
|
758
|
+
end
|
759
|
+
|
760
|
+
define_method( "test_bind_params_mixed" ) do
|
761
|
+
@stmt.bind_params( 1, ":named" => 2 )
|
762
|
+
assert_equal 2, @stmt.execute!.length
|
763
|
+
end
|
764
|
+
|
765
|
+
define_method( "test_bind_param_by_index" ) do
|
766
|
+
@stmt.bind_params( 1, 2 )
|
767
|
+
assert_equal 2, @stmt.execute!.length
|
768
|
+
end
|
769
|
+
|
770
|
+
define_method( "test_bind_param_by_name_bad" ) do
|
771
|
+
assert_raise( SQLite3::Exception ) { @stmt.bind_param( "@named", 2 ) }
|
772
|
+
end
|
773
|
+
|
774
|
+
define_method( "test_bind_param_by_name_good" ) do
|
775
|
+
@stmt.bind_param( ":named", 2 )
|
776
|
+
assert_equal 1, @stmt.execute!.length
|
777
|
+
end
|
778
|
+
|
779
|
+
define_method( "test_bind_param_with_various_types" ) do
|
780
|
+
@db.transaction do
|
781
|
+
@db.execute "create table all_types ( a integer primary key, b float, c string, d integer )"
|
782
|
+
@db.execute "insert into all_types ( b, c, d ) values ( 1.4, 'hello', 68719476735 )"
|
783
|
+
end
|
784
|
+
|
785
|
+
assert_equal 1, @db.execute( "select * from all_types where b = ?", 1.4 ).length
|
786
|
+
assert_equal 1, @db.execute( "select * from all_types where c = ?", 'hello').length
|
787
|
+
assert_equal 1, @db.execute( "select * from all_types where d = ?", 68719476735).length
|
788
|
+
end
|
789
|
+
|
790
|
+
define_method( "test_execute_no_bind_no_block" ) do
|
791
|
+
assert_instance_of SQLite3::ResultSet, @stmt.execute
|
792
|
+
end
|
793
|
+
|
794
|
+
define_method( "test_execute_with_bind_no_block" ) do
|
795
|
+
assert_instance_of SQLite3::ResultSet, @stmt.execute( 1, 2 )
|
796
|
+
end
|
797
|
+
|
798
|
+
define_method( "test_execute_no_bind_with_block" ) do
|
799
|
+
called = false
|
800
|
+
@stmt.execute { |row| called = true }
|
801
|
+
assert called
|
802
|
+
end
|
803
|
+
|
804
|
+
define_method( "test_execute_with_bind_with_block" ) do
|
805
|
+
called = 0
|
806
|
+
@stmt.execute( 1, 2 ) { |row| called += 1 }
|
807
|
+
assert_equal 1, called
|
808
|
+
end
|
809
|
+
|
810
|
+
define_method( "test_reexecute" ) do
|
811
|
+
r = @stmt.execute( 1, 2 )
|
812
|
+
assert_equal 2, r.to_a.length
|
813
|
+
assert_nothing_raised { r = @stmt.execute( 1, 2 ) }
|
814
|
+
assert_equal 2, r.to_a.length
|
815
|
+
end
|
816
|
+
|
817
|
+
define_method( "test_execute_bang_no_bind_no_block" ) do
|
818
|
+
assert @stmt.execute!.empty?
|
819
|
+
end
|
820
|
+
|
821
|
+
define_method( "test_execute_bang_with_bind_no_block" ) do
|
822
|
+
assert_equal 2, @stmt.execute!( 1, 2 ).length
|
823
|
+
end
|
824
|
+
|
825
|
+
define_method( "test_execute_bang_no_bind_with_block" ) do
|
826
|
+
called = 0
|
827
|
+
@stmt.execute! { |row| called += 1 }
|
828
|
+
assert_equal 0, called
|
829
|
+
end
|
830
|
+
|
831
|
+
define_method( "test_execute_bang_with_bind_with_block" ) do
|
832
|
+
called = 0
|
833
|
+
@stmt.execute!( 1, 2 ) { |row| called += 1 }
|
834
|
+
assert_equal 2, called
|
835
|
+
end
|
836
|
+
|
837
|
+
define_method( "test_columns" ) do
|
838
|
+
c1 = @stmt.columns
|
839
|
+
c2 = @stmt.columns
|
840
|
+
assert_same c1, c2
|
841
|
+
assert_equal 2, c1.length
|
842
|
+
end
|
843
|
+
|
844
|
+
define_method( "test_columns_computed" ) do
|
845
|
+
called = false
|
846
|
+
@db.prepare( "select count(*) from foo" ) do |stmt|
|
847
|
+
called = true
|
848
|
+
assert_equal [ "count(*)" ], stmt.columns
|
849
|
+
end
|
850
|
+
assert called
|
851
|
+
end
|
852
|
+
|
853
|
+
define_method( "test_types" ) do
|
854
|
+
t1 = @stmt.types
|
855
|
+
t2 = @stmt.types
|
856
|
+
assert_same t1, t2
|
857
|
+
assert_equal 2, t1.length
|
858
|
+
end
|
859
|
+
|
860
|
+
define_method( "test_types_computed" ) do
|
861
|
+
called = false
|
862
|
+
@db.prepare( "select count(*) from foo" ) do |stmt|
|
863
|
+
called = true
|
864
|
+
assert_equal [ nil ], stmt.types
|
865
|
+
end
|
866
|
+
assert called
|
867
|
+
end
|
868
|
+
|
869
|
+
define_method( "test_close" ) do
|
870
|
+
stmt = @db.prepare( "select * from foo" )
|
871
|
+
assert !stmt.closed?
|
872
|
+
stmt.close
|
873
|
+
assert stmt.closed?
|
874
|
+
assert_raise( SQLite3::Exception ) { stmt.execute }
|
875
|
+
assert_raise( SQLite3::Exception ) { stmt.execute! }
|
876
|
+
assert_raise( SQLite3::Exception ) { stmt.close }
|
877
|
+
assert_raise( SQLite3::Exception ) { stmt.bind_params 5 }
|
878
|
+
assert_raise( SQLite3::Exception ) { stmt.bind_param 1, 5 }
|
879
|
+
assert_raise( SQLite3::Exception ) { stmt.columns }
|
880
|
+
assert_raise( SQLite3::Exception ) { stmt.types }
|
881
|
+
end
|
882
|
+
|
883
|
+
define_method( "test_committing_tx_with_statement_active" ) do
|
884
|
+
called = false
|
885
|
+
@db.prepare( "select count(*) from foo" ) do |stmt|
|
886
|
+
called = true
|
887
|
+
count = stmt.execute!.first.first.to_i
|
888
|
+
@db.transaction do
|
889
|
+
@db.execute "insert into foo ( b ) values ( 'hello' )"
|
890
|
+
end
|
891
|
+
new_count = stmt.execute!.first.first.to_i
|
892
|
+
assert_equal new_count, count+1
|
893
|
+
end
|
894
|
+
assert called
|
895
|
+
end
|
896
|
+
end
|
897
|
+
const_set( "TC_Statement_#{driver}", test_case )
|
898
|
+
|
899
|
+
# == TC_ResultSet =========================================================
|
900
|
+
|
901
|
+
test_case = Class.new( Test::Unit::TestCase ) do
|
902
|
+
define_method( "setup" ) do
|
903
|
+
@db = SQLite3::Database.new( "test.db", :driver=>driver )
|
904
|
+
@db.transaction do
|
905
|
+
@db.execute "create table foo ( a integer primary key, b text )"
|
906
|
+
@db.execute "insert into foo ( b ) values ( 'foo' )"
|
907
|
+
@db.execute "insert into foo ( b ) values ( 'bar' )"
|
908
|
+
@db.execute "insert into foo ( b ) values ( 'baz' )"
|
909
|
+
end
|
910
|
+
@stmt = @db.prepare( "select * from foo where a in ( ?, ? )" )
|
911
|
+
@result = @stmt.execute
|
912
|
+
end
|
913
|
+
|
914
|
+
define_method( "teardown" ) do
|
915
|
+
@stmt.close
|
916
|
+
@db.close
|
917
|
+
File.delete( "test.db" )
|
918
|
+
end
|
919
|
+
|
920
|
+
define_method( "test_reset_unused" ) do
|
921
|
+
assert_nothing_raised { @result.reset }
|
922
|
+
assert @result.to_a.empty?
|
923
|
+
end
|
924
|
+
|
925
|
+
define_method( "test_reset_used" ) do
|
926
|
+
@result.to_a
|
927
|
+
assert_nothing_raised { @result.reset }
|
928
|
+
assert @result.to_a.empty?
|
929
|
+
end
|
930
|
+
|
931
|
+
define_method( "test_reset_with_bind" ) do
|
932
|
+
@result.to_a
|
933
|
+
assert_nothing_raised { @result.reset( 1, 2 ) }
|
934
|
+
assert_equal 2, @result.to_a.length
|
935
|
+
end
|
936
|
+
|
937
|
+
define_method( "test_eof_inner" ) do
|
938
|
+
@result.reset( 1 )
|
939
|
+
assert !@result.eof?
|
940
|
+
end
|
941
|
+
|
942
|
+
define_method( "test_eof_edge" ) do
|
943
|
+
@result.reset( 1 )
|
944
|
+
@result.next # to first row
|
945
|
+
@result.next # to end of result set
|
946
|
+
assert @result.eof?
|
947
|
+
end
|
948
|
+
|
949
|
+
define_method( "test_next_eof" ) do
|
950
|
+
@result.reset( 1 )
|
951
|
+
assert_not_nil @result.next
|
952
|
+
assert_nil @result.next
|
953
|
+
end
|
954
|
+
|
955
|
+
define_method( "test_next_no_type_translation_no_hash" ) do
|
956
|
+
@result.reset( 1 )
|
957
|
+
assert_equal [ "1", "foo" ], @result.next
|
958
|
+
end
|
959
|
+
|
960
|
+
define_method( "test_next_type_translation" ) do
|
961
|
+
@db.type_translation = true
|
962
|
+
@result.reset( 1 )
|
963
|
+
assert_equal [ 1, "foo" ], @result.next
|
964
|
+
end
|
965
|
+
|
966
|
+
define_method( "test_next_type_translation_with_untyped_column" ) do
|
967
|
+
@db.type_translation = true
|
968
|
+
@db.query( "select count(*) from foo" ) do |result|
|
969
|
+
assert_equal ["3"], result.next
|
970
|
+
end
|
971
|
+
end
|
972
|
+
|
973
|
+
define_method( "test_type_translation_with_null_column" ) do
|
974
|
+
@db.type_translation = true
|
975
|
+
@db.execute "create table bar ( a integer, b time, c string )"
|
976
|
+
@db.execute "insert into bar (a, b, c) values (NULL, '1974-07-25 14:39:00', 'hello')"
|
977
|
+
@db.execute "insert into bar (a, b, c) values (1, NULL, 'hello')"
|
978
|
+
@db.execute "insert into bar (a, b, c) values (2, '1974-07-25 14:39:00', NULL)"
|
979
|
+
@db.query( "select * from bar" ) do |result|
|
980
|
+
assert_equal [nil, Time.local(1974, 7, 25, 14, 39, 0), 'hello'], result.next
|
981
|
+
assert_equal [1, nil, 'hello'], result.next
|
982
|
+
assert_equal [2, Time.local(1974, 7, 25, 14, 39, 0), nil], result.next
|
983
|
+
end
|
984
|
+
end
|
985
|
+
|
986
|
+
define_method( "test_date_and_time_translation" ) do
|
987
|
+
@db.type_translation = true
|
988
|
+
@db.execute "create table bar ( a date, b datetime, c time, d timestamp )"
|
989
|
+
@db.execute "insert into bar (a, b, c, d) values ('1999-01-08', '1997-12-17 07:37:16', '07:37:16', '2004-10-19 10:23:54')"
|
990
|
+
@db.query( "select * from bar" ) do |result|
|
991
|
+
result = result.next
|
992
|
+
assert result[0].is_a?(Date)
|
993
|
+
assert result[1].is_a?(DateTime)
|
994
|
+
assert result[2].is_a?(Time)
|
995
|
+
assert result[3].is_a?(Time)
|
996
|
+
end
|
997
|
+
end
|
998
|
+
|
999
|
+
define_method( "test_next_results_as_hash" ) do
|
1000
|
+
@db.results_as_hash = true
|
1001
|
+
@result.reset( 1 )
|
1002
|
+
assert_equal( { "a" => "1", "b" => "foo", 0 => "1", 1 => "foo" },
|
1003
|
+
@result.next )
|
1004
|
+
end
|
1005
|
+
|
1006
|
+
define_method( "test_each" ) do
|
1007
|
+
called = 0
|
1008
|
+
@result.reset( 1, 2 )
|
1009
|
+
@result.each { |row| called += 1 }
|
1010
|
+
assert_equal 2, called
|
1011
|
+
end
|
1012
|
+
|
1013
|
+
define_method( "test_enumerable" ) do
|
1014
|
+
@result.reset( 1, 2 )
|
1015
|
+
assert_equal 2, @result.to_a.length
|
1016
|
+
end
|
1017
|
+
|
1018
|
+
define_method( "test_types" ) do
|
1019
|
+
assert_equal [ "integer", "text" ], @result.types
|
1020
|
+
end
|
1021
|
+
|
1022
|
+
define_method( "test_columns" ) do
|
1023
|
+
assert_equal [ "a", "b" ], @result.columns
|
1024
|
+
end
|
1025
|
+
|
1026
|
+
define_method( "test_close" ) do
|
1027
|
+
stmt = @db.prepare( "select * from foo" )
|
1028
|
+
result = stmt.execute
|
1029
|
+
assert !result.closed?
|
1030
|
+
result.close
|
1031
|
+
assert result.closed?
|
1032
|
+
assert stmt.closed?
|
1033
|
+
assert_raise( SQLite3::Exception ) { result.reset }
|
1034
|
+
assert_raise( SQLite3::Exception ) { result.next }
|
1035
|
+
assert_raise( SQLite3::Exception ) { result.each }
|
1036
|
+
assert_raise( SQLite3::Exception ) { result.close }
|
1037
|
+
assert_raise( SQLite3::Exception ) { result.types }
|
1038
|
+
assert_raise( SQLite3::Exception ) { result.columns }
|
1039
|
+
end
|
1040
|
+
end
|
1041
|
+
const_set( "TC_ResultSet_#{driver}", test_case )
|
1042
|
+
end
|
1043
|
+
|
1044
|
+
end
|