sqlite3-ruby 1.2.5-x86-mswin32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. data/ChangeLog.cvs +88 -0
  2. data/History.txt +68 -0
  3. data/LICENSE +27 -0
  4. data/Manifest.txt +41 -0
  5. data/README.txt +56 -0
  6. data/Rakefile +5 -0
  7. data/ext/sqlite3_api/extconf.rb +10 -0
  8. data/ext/sqlite3_api/sqlite3_api.i +362 -0
  9. data/ext/sqlite3_api/sqlite3_api_wrap.c +5018 -0
  10. data/faq/faq.rb +145 -0
  11. data/faq/faq.yml +426 -0
  12. data/lib/1.8/sqlite3_api.so +0 -0
  13. data/lib/1.9/sqlite3_api.so +0 -0
  14. data/lib/sqlite3.rb +1 -0
  15. data/lib/sqlite3/constants.rb +49 -0
  16. data/lib/sqlite3/database.rb +721 -0
  17. data/lib/sqlite3/driver/dl/api.rb +152 -0
  18. data/lib/sqlite3/driver/dl/driver.rb +307 -0
  19. data/lib/sqlite3/driver/native/driver.rb +219 -0
  20. data/lib/sqlite3/errors.rb +68 -0
  21. data/lib/sqlite3/pragmas.rb +271 -0
  22. data/lib/sqlite3/resultset.rb +180 -0
  23. data/lib/sqlite3/statement.rb +231 -0
  24. data/lib/sqlite3/translator.rb +109 -0
  25. data/lib/sqlite3/value.rb +57 -0
  26. data/lib/sqlite3/version.rb +16 -0
  27. data/setup.rb +1333 -0
  28. data/tasks/benchmark.rake +9 -0
  29. data/tasks/faq.rake +9 -0
  30. data/tasks/gem.rake +32 -0
  31. data/tasks/native.rake +35 -0
  32. data/tasks/vendor_sqlite3.rake +104 -0
  33. data/test/bm.rb +140 -0
  34. data/test/driver/dl/tc_driver.rb +292 -0
  35. data/test/helper.rb +67 -0
  36. data/test/native-vs-dl.rb +126 -0
  37. data/test/test_database.rb +217 -0
  38. data/test/test_errors.rb +17 -0
  39. data/test/test_integration.rb +542 -0
  40. data/test/test_integration_open_close.rb +30 -0
  41. data/test/test_integration_pending.rb +111 -0
  42. data/test/test_integration_resultset.rb +159 -0
  43. data/test/test_integration_statement.rb +195 -0
  44. metadata +143 -0
@@ -0,0 +1,67 @@
1
+ # add lib folder to the path
2
+ $:.unshift File.expand_path(File.join(File.dirname(__FILE__), '..', 'lib'))
3
+
4
+ require 'sqlite3'
5
+
6
+ require 'rubygems'
7
+ require 'test/unit'
8
+
9
+ # define mocks to be used
10
+ require 'mocha'
11
+
12
+ class Driver < Mocha::Mock
13
+ def initialize
14
+ super
15
+ stubs( :open ).returns([0, 'cookie'])
16
+ stubs( :close ).returns(0)
17
+ stubs( :complete? ).returns(0)
18
+ stubs( :errmsg ).returns('')
19
+ stubs( :errcode ).returns(0)
20
+ stubs( :trace ).returns(nil)
21
+ stubs( :set_authorizer ).returns(0)
22
+ stubs( :prepare ).returns([0, 'stmt', 'remainder'])
23
+ stubs( :finalize ).returns(0)
24
+ stubs( :changes ).returns(14)
25
+ stubs( :total_changes ).returns(28)
26
+ stubs( :interrupt ).returns(0)
27
+ end
28
+ end
29
+
30
+ class MockResultSet < Mocha::Mock
31
+ def initialize
32
+ super
33
+ stubs( :each ).yields(['foo'])
34
+ stubs( :columns ).returns(['name'])
35
+ end
36
+ end
37
+
38
+ class Statement < Mocha::Mock
39
+ attr_reader :handle
40
+ attr_reader :sql
41
+ attr_reader :last_result
42
+
43
+ def initialize( handle, sql )
44
+ super()
45
+ @handle = handle
46
+ @sql = sql
47
+ stubs( :close ).returns(0)
48
+ stubs( :remainder ).returns('')
49
+ stubs( :execute ).returns(MockResultSet.new)
50
+ end
51
+ end
52
+
53
+ # UTF conversion extensions
54
+ class String
55
+ def to_utf16(terminate=false)
56
+ self.split(//).map { |c| c[0] }.pack("v*") +
57
+ (terminate ? "\0\0" : "")
58
+ end
59
+
60
+ def from_utf16
61
+ result = ""
62
+ length.times do |i|
63
+ result << self[i,1] if i % 2 == 0 && self[i] != 0
64
+ end
65
+ result
66
+ end
67
+ 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
@@ -0,0 +1,217 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+
3
+ class TC_Database_Init < Test::Unit::TestCase
4
+ def test_new
5
+ # any_instance fails here...
6
+ driver = Driver.new
7
+ driver.expects(:open).once.with('foo.db', false).returns([0, 'cookie'])
8
+ Driver.stubs(:new).returns(driver)
9
+ db = SQLite3::Database.new( 'foo.db', :driver => Driver )
10
+ assert !db.closed?
11
+ assert !db.results_as_hash
12
+ assert !db.type_translation
13
+ end
14
+
15
+ def test_new_with_block
16
+ driver = Driver.new
17
+ driver.expects(:open).once.with('foo.db', false).returns([0, 'cookie'])
18
+ Driver.stubs(:new).returns(driver)
19
+ returned_db = SQLite3::Database.new( "foo.db", :driver => Driver ) do |db|
20
+ assert !db.closed?
21
+ assert !db.results_as_hash
22
+ assert !db.type_translation
23
+ end
24
+ assert returned_db.closed?
25
+ end
26
+
27
+ def test_open
28
+ driver = Driver.new
29
+ driver.expects(:open).once.with('foo.db', false).returns([0, 'cookie'])
30
+ Driver.stubs(:new).returns(driver)
31
+ db = SQLite3::Database.open( "foo.db", :driver => Driver )
32
+ assert !db.closed?
33
+ assert !db.results_as_hash
34
+ assert !db.type_translation
35
+ end
36
+
37
+ def test_open_with_block
38
+ driver = Driver.new
39
+ driver.expects(:open).once.with('foo.db', false).returns([0, 'cookie'])
40
+ Driver.stubs(:new).returns(driver)
41
+ returned_db = SQLite3::Database.open( "foo.db", :driver => Driver ) do |db|
42
+ assert !db.closed?
43
+ assert !db.results_as_hash
44
+ assert !db.type_translation
45
+ end
46
+ assert returned_db.closed?
47
+ end
48
+
49
+ def test_with_type_translation
50
+ db = SQLite3::Database.open( "foo.db", :driver => Driver,
51
+ :type_translation => true )
52
+ assert db.type_translation
53
+ end
54
+
55
+ def test_with_results_as_hash
56
+ db = SQLite3::Database.open( "foo.db", :driver => Driver,
57
+ :results_as_hash => true )
58
+ assert db.results_as_hash
59
+ end
60
+
61
+ def test_with_type_translation_and_results_as_hash
62
+ db = SQLite3::Database.open( "foo.db", :driver => Driver,
63
+ :results_as_hash => true,
64
+ :type_translation => true )
65
+ assert db.results_as_hash
66
+ assert db.type_translation
67
+ end
68
+ end
69
+
70
+ class TC_Database < Test::Unit::TestCase
71
+ def setup
72
+ @db = SQLite3::Database.open( "foo.db",
73
+ :driver => Driver, :statement_factory => Statement )
74
+ end
75
+
76
+ def test_quote
77
+ assert_equal "''one''two''three''", SQLite3::Database.quote(
78
+ "'one'two'three'" )
79
+ end
80
+
81
+ def test_complete
82
+ Driver.any_instance.expects(:complete?)
83
+ @db.complete? "foo"
84
+ end
85
+
86
+ def test_errmsg
87
+ Driver.any_instance.expects(:errmsg)
88
+ @db.errmsg
89
+ end
90
+
91
+ def test_errcode
92
+ Driver.any_instance.expects(:errcode)
93
+ @db.errcode
94
+ end
95
+
96
+ def test_translator
97
+ translator = @db.translator
98
+ assert_instance_of SQLite3::Translator, translator
99
+ end
100
+
101
+ def test_close
102
+ Driver.any_instance.expects(:close).returns(0)
103
+ @db.close
104
+ assert @db.closed?
105
+ Driver.any_instance.expects(:close).never
106
+ @db.close
107
+ end
108
+
109
+ def test_trace
110
+ Driver.any_instance.expects(:trace).with('cookie', 15)
111
+ @db.trace( 15 ) { "foo" }
112
+ # assert_equal 1, driver.mock_blocks[:trace].length
113
+ end
114
+
115
+ def test_authorizer
116
+ Driver.any_instance.expects(:set_authorizer).with('cookie', 15).returns(0)
117
+ @db.authorizer( 15 ) { "foo" }
118
+ # assert_equal 1, driver.mock_blocks[:set_authorizer].length
119
+ end
120
+
121
+ def test_prepare_no_block
122
+ Statement.any_instance.expects(:close).never
123
+ assert_nothing_raised { @db.prepare( "foo" ) }
124
+ end
125
+
126
+ def test_prepare_with_block
127
+ called = false
128
+ # any_instance fails here...
129
+ statement = Statement.new('cookie', 'foo')
130
+ statement.expects(:close).once
131
+ Statement.stubs(:new).returns(statement)
132
+ @db.prepare( "foo" ) { |stmt| called = true }
133
+ assert called
134
+ end
135
+
136
+ def test_execute_no_block
137
+ # any_instance fails here...
138
+ statement = Statement.new('cookie', 'foo')
139
+ statement.expects(:execute).with('bar', 'baz').returns(MockResultSet.new)
140
+ Statement.stubs(:new).returns(statement)
141
+ MockResultSet.any_instance.stubs(:inject).returns([['foo']])
142
+ result = @db.execute( "foo", "bar", "baz" )
143
+ assert_equal [["foo"]], result
144
+ end
145
+
146
+ def test_execute_with_block
147
+ called = false
148
+ # any_instance fails here...
149
+ statement = Statement.new('cookie', 'foo')
150
+ statement.expects(:execute).with('bar', 'baz').returns(MockResultSet.new)
151
+ Statement.stubs(:new).returns(statement)
152
+ @db.execute( "foo", "bar", "baz" ) do |row|
153
+ called = true
154
+ assert_equal ["foo"], row
155
+ end
156
+
157
+ assert called
158
+ end
159
+
160
+ def test_execute2_no_block
161
+ # any_instance fails here...
162
+ statement = Statement.new('cookie', 'foo')
163
+ statement.expects(:execute).with('bar', 'baz').returns(MockResultSet.new)
164
+ Statement.stubs(:new).returns(statement)
165
+ MockResultSet.any_instance.stubs(:inject).returns([['name'], ['foo']])
166
+ result = @db.execute2( "foo", "bar", "baz" )
167
+ assert_equal [["name"],["foo"]], result
168
+ end
169
+
170
+ def test_execute2_with_block
171
+ called = false
172
+ parts = [ ["name"],["foo"] ]
173
+ # any_instance fails here...
174
+ statement = Statement.new('cookie', 'foo')
175
+ statement.expects(:execute).with('bar', 'baz').returns(MockResultSet.new)
176
+ Statement.stubs(:new).returns(statement)
177
+ @db.execute2( "foo", "bar", "baz" ) do |row|
178
+ called = true
179
+ assert_equal parts.shift, row
180
+ end
181
+
182
+ assert called
183
+ end
184
+
185
+ def test_execute_batch
186
+ # any_instance fails here...
187
+ statement = Statement.new('cookie', 'foo')
188
+ statement.expects(:execute).with('bar', 'baz').returns(MockResultSet.new)
189
+ Statement.stubs(:new).returns(statement)
190
+ @db.execute_batch( "foo", "bar", "baz" )
191
+ end
192
+
193
+ def test_get_first_row
194
+ result = @db.get_first_row( "foo", "bar", "baz" )
195
+ assert_equal ["foo"], result
196
+ end
197
+
198
+ def test_get_first_value
199
+ result = @db.get_first_value( "foo", "bar", "baz" )
200
+ assert_equal "foo", result
201
+ end
202
+
203
+ def test_changes
204
+ Driver.any_instance.expects(:changes).returns(14)
205
+ assert_equal 14, @db.changes
206
+ end
207
+
208
+ def test_total_changes
209
+ Driver.any_instance.expects(:total_changes).returns(28)
210
+ assert_equal 28, @db.total_changes
211
+ end
212
+
213
+ def test_interrupt
214
+ Driver.any_instance.expects(:interrupt)
215
+ @db.interrupt
216
+ end
217
+ end
@@ -0,0 +1,17 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+
3
+ class TC_Errors < Test::Unit::TestCase
4
+ (1..26).each do |code|
5
+ define_method( "test_error_code_%02d" % code ) do
6
+ db = stub('database', :errmsg => 'message')
7
+ begin
8
+ SQLite3::Error.check( code, db )
9
+ rescue SQLite3::Exception => e
10
+ assert_instance_of SQLite3::EXCEPTIONS[code], e
11
+ assert_equal code, e.code
12
+ assert_equal code, e.class.code
13
+ assert_equal "message", e.message
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,542 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+
3
+ class TC_Database_Integration < Test::Unit::TestCase
4
+ def setup
5
+ @db = SQLite3::Database.new( "test.db" )
6
+ @db.transaction do
7
+ @db.execute "create table foo ( a integer primary key, b text )"
8
+ @db.execute "insert into foo ( b ) values ( 'foo' )"
9
+ @db.execute "insert into foo ( b ) values ( 'bar' )"
10
+ @db.execute "insert into foo ( b ) values ( 'baz' )"
11
+ end
12
+ end
13
+
14
+ def teardown
15
+ @db.close
16
+ File.delete( "test.db" )
17
+ end
18
+
19
+ def test_table_info_with_type_translation_active
20
+ @db.type_translation = true
21
+ assert_nothing_raised { @db.table_info("foo") }
22
+ end
23
+
24
+ def test_table_info_with_defaults_for_version_3_3_8_and_higher
25
+ @db.transaction do
26
+ @db.execute "create table defaults_test ( a string default NULL, b string default 'Hello' )"
27
+ data = @db.table_info( "defaults_test" )
28
+ assert_equal({"name" => "a", "type" => "string", "dflt_value" => nil, "notnull" => "0", "cid" => "0", "pk" => "0"},
29
+ data[0])
30
+ assert_equal({"name" => "b", "type" => "string", "dflt_value" => "Hello", "notnull" => "0", "cid" => "1", "pk" => "0"},
31
+ data[1])
32
+ end
33
+ end
34
+
35
+ def test_table_info_without_defaults_for_version_3_3_8_and_higher
36
+ @db.transaction do
37
+ @db.execute "create table no_defaults_test ( a integer default 1, b integer )"
38
+ data = @db.table_info( "no_defaults_test" )
39
+ assert_equal({"name" => "a", "type" => "integer", "dflt_value" => "1", "notnull" => "0", "cid" => "0", "pk" => "0"},
40
+ data[0])
41
+ assert_equal({"name" => "b", "type" => "integer", "dflt_value" => nil, "notnull" => "0", "cid" => "1", "pk" => "0"},
42
+ data[1])
43
+ end
44
+ end
45
+
46
+ def test_complete_fail
47
+ assert !@db.complete?( "select * from foo" )
48
+ end
49
+ def test_complete_success
50
+ assert @db.complete?( "select * from foo;" )
51
+ end
52
+
53
+ def test_complete_fail_utf16
54
+ assert !@db.complete?( "select * from foo".to_utf16(false), true )
55
+ end
56
+
57
+ def test_complete_success_utf16
58
+ assert @db.complete?( "select * from foo;".to_utf16(true), true )
59
+ end
60
+
61
+ def test_errmsg
62
+ assert_equal "not an error", @db.errmsg
63
+ end
64
+
65
+ def test_errmsg_utf16
66
+ assert_equal "not an error".to_utf16, @db.errmsg(true)
67
+ end
68
+
69
+ def test_errcode
70
+ assert_equal 0, @db.errcode
71
+ end
72
+
73
+ def test_trace
74
+ result = nil
75
+ @db.trace( "data" ) { |data,sql| result = [ data, sql ]; 0 }
76
+ @db.execute "select * from foo"
77
+ assert_equal ["data","select * from foo"], result
78
+ end
79
+
80
+ def test_authorizer_okay
81
+ @db.authorizer( "data" ) { |data,type,a,b,c,d| 0 }
82
+ rows = @db.execute "select * from foo"
83
+ assert_equal 3, rows.length
84
+ end
85
+
86
+ def test_authorizer_error
87
+ @db.authorizer( "data" ) { |data,type,a,b,c,d| 1 }
88
+ assert_raise( SQLite3::AuthorizationException ) do
89
+ @db.execute "select * from foo"
90
+ end
91
+ end
92
+
93
+ def test_authorizer_silent
94
+ @db.authorizer( "data" ) { |data,type,a,b,c,d| 2 }
95
+ rows = @db.execute "select * from foo"
96
+ assert rows.empty?
97
+ end
98
+
99
+ def test_prepare_invalid_syntax
100
+ assert_raise( SQLite3::SQLException ) do
101
+ @db.prepare "select from foo"
102
+ end
103
+ end
104
+
105
+ def test_prepare_invalid_column
106
+ assert_raise( SQLite3::SQLException ) do
107
+ @db.prepare "select k from foo"
108
+ end
109
+ end
110
+
111
+ def test_prepare_invalid_table
112
+ assert_raise( SQLite3::SQLException ) do
113
+ @db.prepare "select * from barf"
114
+ end
115
+ end
116
+
117
+ def test_prepare_no_block
118
+ stmt = @db.prepare "select * from foo"
119
+ assert stmt.respond_to?(:execute)
120
+ stmt.close
121
+ end
122
+
123
+ def test_prepare_with_block
124
+ called = false
125
+ @db.prepare "select * from foo" do |stmt|
126
+ called = true
127
+ assert stmt.respond_to?(:execute)
128
+ end
129
+ assert called
130
+ end
131
+
132
+ def test_execute_no_block_no_bind_no_match
133
+ rows = @db.execute( "select * from foo where a > 100" )
134
+ assert rows.empty?
135
+ end
136
+
137
+ def test_execute_with_block_no_bind_no_match
138
+ called = false
139
+ @db.execute( "select * from foo where a > 100" ) do |row|
140
+ called = true
141
+ end
142
+ assert !called
143
+ end
144
+
145
+ def test_execute_no_block_with_bind_no_match
146
+ rows = @db.execute( "select * from foo where a > ?", 100 )
147
+ assert rows.empty?
148
+ end
149
+
150
+ def test_execute_with_block_with_bind_no_match
151
+ called = false
152
+ @db.execute( "select * from foo where a > ?", 100 ) do |row|
153
+ called = true
154
+ end
155
+ assert !called
156
+ end
157
+
158
+ def test_execute_no_block_no_bind_with_match
159
+ rows = @db.execute( "select * from foo where a = 1" )
160
+ assert_equal 1, rows.length
161
+ end
162
+
163
+ def test_execute_with_block_no_bind_with_match
164
+ called = 0
165
+ @db.execute( "select * from foo where a = 1" ) do |row|
166
+ called += 1
167
+ end
168
+ assert_equal 1, called
169
+ end
170
+
171
+ def test_execute_no_block_with_bind_with_match
172
+ rows = @db.execute( "select * from foo where a = ?", 1 )
173
+ assert_equal 1, rows.length
174
+ end
175
+
176
+ def test_execute_with_block_with_bind_with_match
177
+ called = 0
178
+ @db.execute( "select * from foo where a = ?", 1 ) do |row|
179
+ called += 1
180
+ end
181
+ assert_equal 1, called
182
+ end
183
+
184
+ def test_execute2_no_block_no_bind_no_match
185
+ columns, *rows = @db.execute2( "select * from foo where a > 100" )
186
+ assert rows.empty?
187
+ assert [ "a", "b" ], columns
188
+ end
189
+
190
+ def test_execute2_with_block_no_bind_no_match
191
+ called = 0
192
+ @db.execute2( "select * from foo where a > 100" ) do |row|
193
+ assert [ "a", "b" ], row unless called == 0
194
+ called += 1
195
+ end
196
+ assert_equal 1, called
197
+ end
198
+
199
+ def test_execute2_no_block_with_bind_no_match
200
+ columns, *rows = @db.execute2( "select * from foo where a > ?", 100 )
201
+ assert rows.empty?
202
+ assert [ "a", "b" ], columns
203
+ end
204
+
205
+ def test_execute2_with_block_with_bind_no_match
206
+ called = 0
207
+ @db.execute2( "select * from foo where a > ?", 100 ) do |row|
208
+ assert [ "a", "b" ], row unless called == 0
209
+ called += 1
210
+ end
211
+ assert_equal 1, called
212
+ end
213
+
214
+ def test_execute2_no_block_no_bind_with_match
215
+ columns, *rows = @db.execute2( "select * from foo where a = 1" )
216
+ assert_equal 1, rows.length
217
+ assert [ "a", "b" ], columns
218
+ end
219
+
220
+ def test_execute2_with_block_no_bind_with_match
221
+ called = 0
222
+ @db.execute2( "select * from foo where a = 1" ) do |row|
223
+ assert [ "a", "b" ], row unless called == 0
224
+ called += 1
225
+ end
226
+ assert_equal 2, called
227
+ end
228
+
229
+ def test_execute2_no_block_with_bind_with_match
230
+ columns, *rows = @db.execute2( "select * from foo where a = ?", 1 )
231
+ assert_equal 1, rows.length
232
+ assert [ "a", "b" ], columns
233
+ end
234
+
235
+ def test_execute2_with_block_with_bind_with_match
236
+ called = 0
237
+ @db.execute2( "select * from foo where a = ?", 1 ) do |row|
238
+ called += 1
239
+ end
240
+ assert_equal 2, called
241
+ end
242
+
243
+ def test_execute_batch_empty
244
+ assert_nothing_raised { @db.execute_batch "" }
245
+ end
246
+
247
+ def test_execute_batch_no_bind
248
+ @db.transaction do
249
+ @db.execute_batch <<-SQL
250
+ create table bar ( a, b, c );
251
+ insert into bar values ( 'one', 2, 'three' );
252
+ insert into bar values ( 'four', 5, 'six' );
253
+ insert into bar values ( 'seven', 8, 'nine' );
254
+ SQL
255
+ end
256
+ rows = @db.execute( "select * from bar" )
257
+ assert_equal 3, rows.length
258
+ end
259
+
260
+ def test_execute_batch_with_bind
261
+ @db.execute_batch( <<-SQL, 1 )
262
+ create table bar ( a, b, c );
263
+ insert into bar values ( 'one', 2, ? );
264
+ insert into bar values ( 'four', 5, ? );
265
+ insert into bar values ( 'seven', 8, ? );
266
+ SQL
267
+ rows = @db.execute( "select * from bar" ).map { |a,b,c| c }
268
+ assert_equal %w{1 1 1}, rows
269
+ end
270
+
271
+ def test_query_no_block_no_bind_no_match
272
+ result = @db.query( "select * from foo where a > 100" )
273
+ assert_nil result.next
274
+ result.close
275
+ end
276
+
277
+ def test_query_with_block_no_bind_no_match
278
+ r = nil
279
+ @db.query( "select * from foo where a > 100" ) do |result|
280
+ assert_nil result.next
281
+ r = result
282
+ end
283
+ assert r.closed?
284
+ end
285
+
286
+ def test_query_no_block_with_bind_no_match
287
+ result = @db.query( "select * from foo where a > ?", 100 )
288
+ assert_nil result.next
289
+ result.close
290
+ end
291
+
292
+ def test_query_with_block_with_bind_no_match
293
+ r = nil
294
+ @db.query( "select * from foo where a > ?", 100 ) do |result|
295
+ assert_nil result.next
296
+ r = result
297
+ end
298
+ assert r.closed?
299
+ end
300
+
301
+ def test_query_no_block_no_bind_with_match
302
+ result = @db.query( "select * from foo where a = 1" )
303
+ assert_not_nil result.next
304
+ assert_nil result.next
305
+ result.close
306
+ end
307
+
308
+ def test_query_with_block_no_bind_with_match
309
+ r = nil
310
+ @db.query( "select * from foo where a = 1" ) do |result|
311
+ assert_not_nil result.next
312
+ assert_nil result.next
313
+ r = result
314
+ end
315
+ assert r.closed?
316
+ end
317
+
318
+ def test_query_no_block_with_bind_with_match
319
+ result = @db.query( "select * from foo where a = ?", 1 )
320
+ assert_not_nil result.next
321
+ assert_nil result.next
322
+ result.close
323
+ end
324
+
325
+ def test_query_with_block_with_bind_with_match
326
+ r = nil
327
+ @db.query( "select * from foo where a = ?", 1 ) do |result|
328
+ assert_not_nil result.next
329
+ assert_nil result.next
330
+ r = result
331
+ end
332
+ assert r.closed?
333
+ end
334
+
335
+ def test_get_first_row_no_bind_no_match
336
+ result = @db.get_first_row( "select * from foo where a=100" )
337
+ assert_nil result
338
+ end
339
+
340
+ def test_get_first_row_no_bind_with_match
341
+ result = @db.get_first_row( "select * from foo where a=1" )
342
+ assert_equal [ "1", "foo" ], result
343
+ end
344
+
345
+ def test_get_first_row_with_bind_no_match
346
+ result = @db.get_first_row( "select * from foo where a=?", 100 )
347
+ assert_nil result
348
+ end
349
+
350
+ def test_get_first_row_with_bind_with_match
351
+ result = @db.get_first_row( "select * from foo where a=?", 1 )
352
+ assert_equal [ "1", "foo" ], result
353
+ end
354
+
355
+ def test_get_first_value_no_bind_no_match
356
+ result = @db.get_first_value( "select b, a from foo where a=100" )
357
+ assert_nil result
358
+ end
359
+
360
+ def test_get_first_value_no_bind_with_match
361
+ result = @db.get_first_value( "select b, a from foo where a=1" )
362
+ assert_equal "foo", result
363
+ end
364
+
365
+ def test_get_first_value_with_bind_no_match
366
+ result = @db.get_first_value( "select b, a from foo where a=?", 100 )
367
+ assert_nil result
368
+ end
369
+
370
+ def test_get_first_value_with_bind_with_match
371
+ result = @db.get_first_value( "select b, a from foo where a=?", 1 )
372
+ assert_equal "foo", result
373
+ end
374
+
375
+ def test_last_insert_row_id
376
+ @db.execute "insert into foo ( b ) values ( 'test' )"
377
+ assert_equal 4, @db.last_insert_row_id
378
+ @db.execute "insert into foo ( b ) values ( 'again' )"
379
+ assert_equal 5, @db.last_insert_row_id
380
+ end
381
+
382
+ def test_changes
383
+ @db.execute "insert into foo ( b ) values ( 'test' )"
384
+ assert_equal 1, @db.changes
385
+ @db.execute "delete from foo where 1=1"
386
+ assert_equal 4, @db.changes
387
+ end
388
+
389
+ def test_total_changes
390
+ assert_equal 3, @db.total_changes
391
+ @db.execute "insert into foo ( b ) values ( 'test' )"
392
+ @db.execute "delete from foo where 1=1"
393
+ assert_equal 8, @db.total_changes
394
+ end
395
+
396
+ def test_transaction_nest
397
+ assert_raise( SQLite3::SQLException ) do
398
+ @db.transaction do
399
+ @db.transaction do
400
+ end
401
+ end
402
+ end
403
+ end
404
+
405
+ def test_transaction_rollback
406
+ @db.transaction
407
+ @db.execute_batch <<-SQL
408
+ insert into foo (b) values ( 'test1' );
409
+ insert into foo (b) values ( 'test2' );
410
+ insert into foo (b) values ( 'test3' );
411
+ insert into foo (b) values ( 'test4' );
412
+ SQL
413
+ assert_equal 7, @db.get_first_value("select count(*) from foo").to_i
414
+ @db.rollback
415
+ assert_equal 3, @db.get_first_value("select count(*) from foo").to_i
416
+ end
417
+
418
+ def test_transaction_commit
419
+ @db.transaction
420
+ @db.execute_batch <<-SQL
421
+ insert into foo (b) values ( 'test1' );
422
+ insert into foo (b) values ( 'test2' );
423
+ insert into foo (b) values ( 'test3' );
424
+ insert into foo (b) values ( 'test4' );
425
+ SQL
426
+ assert_equal 7, @db.get_first_value("select count(*) from foo").to_i
427
+ @db.commit
428
+ assert_equal 7, @db.get_first_value("select count(*) from foo").to_i
429
+ end
430
+
431
+ def test_transaction_rollback_in_block
432
+ assert_raise( SQLite3::SQLException ) do
433
+ @db.transaction do
434
+ @db.rollback
435
+ end
436
+ end
437
+ end
438
+
439
+ def test_transaction_commit_in_block
440
+ assert_raise( SQLite3::SQLException ) do
441
+ @db.transaction do
442
+ @db.commit
443
+ end
444
+ end
445
+ end
446
+
447
+ def test_transaction_active
448
+ assert !@db.transaction_active?
449
+ @db.transaction
450
+ assert @db.transaction_active?
451
+ @db.commit
452
+ assert !@db.transaction_active?
453
+ end
454
+
455
+ def test_interrupt
456
+ @db.create_function( "abort", 1 ) do |func,x|
457
+ @db.interrupt
458
+ func.result = x
459
+ end
460
+
461
+ assert_raise( SQLite3::SQLException ) do
462
+ @db.execute "select abort(a) from foo"
463
+ end
464
+ end
465
+
466
+ def test_create_function
467
+ @db.create_function( "munge", 1 ) do |func,x|
468
+ func.result = ">>>#{x}<<<"
469
+ end
470
+
471
+ value = @db.get_first_value( "select munge(b) from foo where a=1" )
472
+ assert_match( />>>.*<<</, value )
473
+ end
474
+
475
+ def test_create_aggregate_without_block
476
+ step = proc do |ctx,a|
477
+ ctx[:sum] ||= 0
478
+ ctx[:sum] += a.to_i
479
+ end
480
+
481
+ final = proc { |ctx| ctx.result = ctx[:sum] }
482
+
483
+ @db.create_aggregate( "accumulate", 1, step, final )
484
+
485
+ value = @db.get_first_value( "select accumulate(a) from foo" )
486
+ assert_equal "6", value
487
+ end
488
+
489
+ def test_create_aggregate_with_block
490
+ @db.create_aggregate( "accumulate", 1 ) do
491
+ step do |ctx,a|
492
+ ctx[:sum] ||= 0
493
+ ctx[:sum] += a.to_i
494
+ end
495
+
496
+ finalize { |ctx| ctx.result = ctx[:sum] }
497
+ end
498
+
499
+ value = @db.get_first_value( "select accumulate(a) from foo" )
500
+ assert_equal "6", value
501
+ end
502
+
503
+ def test_create_aggregate_with_no_data
504
+ @db.create_aggregate( "accumulate", 1 ) do
505
+ step do |ctx,a|
506
+ ctx[:sum] ||= 0
507
+ ctx[:sum] += a.to_i
508
+ end
509
+
510
+ finalize { |ctx| ctx.result = ctx[:sum] || 0 }
511
+ end
512
+
513
+ value = @db.get_first_value(
514
+ "select accumulate(a) from foo where a = 100" )
515
+ assert_equal "0", value
516
+ end
517
+
518
+ def test_create_aggregate_handler
519
+ handler = Class.new do
520
+ class << self
521
+ def arity; 1; end
522
+ def text_rep; SQLite3::Constants::TextRep::ANY; end
523
+ def name; "multiply"; end
524
+ end
525
+ def step(ctx, a)
526
+ ctx[:buffer] ||= 1
527
+ ctx[:buffer] *= a.to_i
528
+ end
529
+ def finalize(ctx); ctx.result = ctx[:buffer]; end
530
+ end
531
+
532
+ @db.create_aggregate_handler( handler )
533
+ value = @db.get_first_value( "select multiply(a) from foo" )
534
+ assert_equal "6", value
535
+ end
536
+
537
+ def test_bind_array_parameter
538
+ result = @db.get_first_value( "select b from foo where a=? and b=?",
539
+ [ 1, "foo" ] )
540
+ assert_equal "foo", result
541
+ end
542
+ end