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,30 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+
3
+ class TC_OpenClose < Test::Unit::TestCase
4
+ def test_create_close
5
+ begin
6
+ db = SQLite3::Database.new( "test-create.db" )
7
+ assert File.exist?( "test-create.db" )
8
+ assert_nothing_raised { db.close }
9
+ ensure
10
+ File.delete( "test-create.db" ) rescue nil
11
+ end
12
+ end
13
+
14
+ def test_open_close
15
+ begin
16
+ File.open( "test-open.db", "w" ) { |f| }
17
+ assert File.exist?( "test-open.db" )
18
+ db = SQLite3::Database.new( "test-open.db" )
19
+ assert_nothing_raised { db.close }
20
+ ensure
21
+ File.delete( "test-open.db" ) rescue nil
22
+ end
23
+ end
24
+
25
+ def test_bad_open
26
+ assert_raise( SQLite3::CantOpenException ) do
27
+ SQLite3::Database.new( "." )
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,111 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+
3
+ require 'thread'
4
+ require 'benchmark'
5
+
6
+ class TC_Integration_Pending < Test::Unit::TestCase
7
+ def setup
8
+ @db = SQLite3::Database.new( "test.db" )
9
+ @db.transaction do
10
+ @db.execute "create table foo ( a integer primary key, b text )"
11
+ @db.execute "insert into foo ( b ) values ( 'foo' )"
12
+ @db.execute "insert into foo ( b ) values ( 'bar' )"
13
+ @db.execute "insert into foo ( b ) values ( 'baz' )"
14
+ end
15
+ end
16
+
17
+ def teardown
18
+ @db.close
19
+ File.delete( "test.db" )
20
+ end
21
+
22
+ def test_busy_handler_outwait
23
+ busy = Mutex.new
24
+ busy.lock
25
+ handler_call_count = 0
26
+
27
+ t = Thread.new(busy) do |locker|
28
+ begin
29
+ db2 = SQLite3::Database.open( "test.db" )
30
+ db2.transaction( :exclusive ) do
31
+ locker.lock
32
+ end
33
+ ensure
34
+ db2.close if db2
35
+ end
36
+ end
37
+
38
+ @db.busy_handler do |data,count|
39
+ handler_call_count += 1
40
+ busy.unlock
41
+ true
42
+ end
43
+
44
+ assert_nothing_raised do
45
+ @db.execute "insert into foo (b) values ( 'from 2' )"
46
+ end
47
+
48
+ t.join
49
+
50
+ assert_equal 1, handler_call_count
51
+ end
52
+
53
+ def test_busy_handler_impatient
54
+ busy = Mutex.new
55
+ busy.lock
56
+ handler_call_count = 0
57
+
58
+ t = Thread.new do
59
+ begin
60
+ db2 = SQLite3::Database.open( "test.db" )
61
+ db2.transaction( :exclusive ) do
62
+ busy.lock
63
+ end
64
+ ensure
65
+ db2.close if db2
66
+ end
67
+ end
68
+
69
+ @db.busy_handler do |data, count|
70
+ handler_call_count += 1
71
+ false
72
+ end
73
+
74
+ assert_raise( SQLite3::BusyException ) do
75
+ @db.execute "insert into foo (b) values ( 'from 2' )"
76
+ end
77
+
78
+ busy.unlock
79
+ t.join
80
+
81
+ assert_equal 1, handler_call_count
82
+ end
83
+
84
+ def test_busy_timeout
85
+ @db.busy_timeout 1000
86
+ busy = Mutex.new
87
+ busy.lock
88
+
89
+ t = Thread.new do
90
+ begin
91
+ db2 = SQLite3::Database.open( "test.db" )
92
+ db2.transaction( :exclusive ) do
93
+ busy.lock
94
+ end
95
+ ensure
96
+ db2.close if db2
97
+ end
98
+ end
99
+
100
+ time = Benchmark.measure do
101
+ assert_raise( SQLite3::BusyException ) do
102
+ @db.execute "insert into foo (b) values ( 'from 2' )"
103
+ end
104
+ end
105
+
106
+ busy.unlock
107
+ t.join
108
+
109
+ assert time.real*1000 >= 1000
110
+ end
111
+ end
@@ -0,0 +1,159 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+
3
+ class TC_ResultSet < 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
+ @stmt = @db.prepare( "select * from foo where a in ( ?, ? )" )
13
+ @result = @stmt.execute
14
+ end
15
+
16
+ def teardown
17
+ @stmt.close
18
+ @db.close
19
+ File.delete( "test.db" )
20
+ end
21
+
22
+ def test_reset_unused
23
+ assert_nothing_raised { @result.reset }
24
+ assert @result.to_a.empty?
25
+ end
26
+
27
+ def test_reset_used
28
+ @result.to_a
29
+ assert_nothing_raised { @result.reset }
30
+ assert @result.to_a.empty?
31
+ end
32
+
33
+ def test_reset_with_bind
34
+ @result.to_a
35
+ assert_nothing_raised { @result.reset( 1, 2 ) }
36
+ assert_equal 2, @result.to_a.length
37
+ end
38
+
39
+ def test_eof_inner
40
+ @result.reset( 1 )
41
+ assert !@result.eof?
42
+ end
43
+
44
+ def test_eof_edge
45
+ @result.reset( 1 )
46
+ @result.next # to first row
47
+ @result.next # to end of result set
48
+ assert @result.eof?
49
+ end
50
+
51
+ def test_next_eof
52
+ @result.reset( 1 )
53
+ assert_not_nil @result.next
54
+ assert_nil @result.next
55
+ end
56
+
57
+ def test_next_no_type_translation_no_hash
58
+ @result.reset( 1 )
59
+ assert_equal [ "1", "foo" ], @result.next
60
+ end
61
+
62
+ def test_next_type_translation
63
+ @db.type_translation = true
64
+ @result.reset( 1 )
65
+ assert_equal [ 1, "foo" ], @result.next
66
+ end
67
+
68
+ def test_next_type_translation_with_untyped_column
69
+ @db.type_translation = true
70
+ @db.query( "select count(*) from foo" ) do |result|
71
+ assert_equal ["3"], result.next
72
+ end
73
+ end
74
+
75
+ def test_type_translation_with_null_column
76
+ @db.type_translation = true
77
+ @db.execute "create table bar ( a integer, b time, c string )"
78
+ @db.execute "insert into bar (a, b, c) values (NULL, '1974-07-25 14:39:00', 'hello')"
79
+ @db.execute "insert into bar (a, b, c) values (1, NULL, 'hello')"
80
+ @db.execute "insert into bar (a, b, c) values (2, '1974-07-25 14:39:00', NULL)"
81
+ @db.query( "select * from bar" ) do |result|
82
+ assert_equal [nil, Time.local(1974, 7, 25, 14, 39, 0), 'hello'], result.next
83
+ assert_equal [1, nil, 'hello'], result.next
84
+ assert_equal [2, Time.local(1974, 7, 25, 14, 39, 0), nil], result.next
85
+ end
86
+ end
87
+
88
+ def test_date_and_time_translation
89
+ @db.type_translation = true
90
+ @db.execute "create table bar ( a date, b datetime, c time, d timestamp )"
91
+ @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')"
92
+ @db.query( "select * from bar" ) do |result|
93
+ result = result.next
94
+ assert result[0].is_a?(Date)
95
+ assert result[1].is_a?(DateTime)
96
+ assert result[2].is_a?(Time)
97
+ assert result[3].is_a?(Time)
98
+ end
99
+ end
100
+
101
+ def test_next_results_as_hash
102
+ @db.results_as_hash = true
103
+ @result.reset( 1 )
104
+ assert_equal( { "a" => "1", "b" => "foo", 0 => "1", 1 => "foo" },
105
+ @result.next )
106
+ end
107
+
108
+ def test_tainted_results_as_hash
109
+ @db.results_as_hash = true
110
+ @result.reset( 1 )
111
+ row = @result.next
112
+ row.each do |_, v|
113
+ assert_equal true, v.tainted?
114
+ end
115
+ end
116
+
117
+ def test_tainted_row_values
118
+ @result.reset( 1 )
119
+ row = @result.next
120
+ row.each do |v|
121
+ assert_equal true, v.tainted?
122
+ end
123
+ end
124
+
125
+ def test_each
126
+ called = 0
127
+ @result.reset( 1, 2 )
128
+ @result.each { |row| called += 1 }
129
+ assert_equal 2, called
130
+ end
131
+
132
+ def test_enumerable
133
+ @result.reset( 1, 2 )
134
+ assert_equal 2, @result.to_a.length
135
+ end
136
+
137
+ def test_types
138
+ assert_equal [ "integer", "text" ], @result.types
139
+ end
140
+
141
+ def test_columns
142
+ assert_equal [ "a", "b" ], @result.columns
143
+ end
144
+
145
+ def test_close
146
+ stmt = @db.prepare( "select * from foo" )
147
+ result = stmt.execute
148
+ assert !result.closed?
149
+ result.close
150
+ assert result.closed?
151
+ assert stmt.closed?
152
+ assert_raise( SQLite3::Exception ) { result.reset }
153
+ assert_raise( SQLite3::Exception ) { result.next }
154
+ assert_raise( SQLite3::Exception ) { result.each }
155
+ assert_raise( SQLite3::Exception ) { result.close }
156
+ assert_raise( SQLite3::Exception ) { result.types }
157
+ assert_raise( SQLite3::Exception ) { result.columns }
158
+ end
159
+ end
@@ -0,0 +1,195 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+
3
+ class TC_Statement < 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
+ @stmt = @db.prepare( "select * from foo where a in ( ?, :named )" )
13
+ end
14
+
15
+ def teardown
16
+ @stmt.close
17
+ @db.close
18
+ File.delete( "test.db" )
19
+ end
20
+
21
+ def test_remainder_empty
22
+ assert_equal "", @stmt.remainder
23
+ end
24
+
25
+ def test_remainder_nonempty
26
+ called = false
27
+ @db.prepare( "select * from foo;\n blah" ) do |stmt|
28
+ called = true
29
+ assert_equal "\n blah", stmt.remainder
30
+ end
31
+ assert called
32
+ end
33
+
34
+ def test_bind_params_empty
35
+ assert_nothing_raised { @stmt.bind_params }
36
+ assert @stmt.execute!.empty?
37
+ end
38
+
39
+ def test_bind_params_array
40
+ @stmt.bind_params 1, 2
41
+ assert_equal 2, @stmt.execute!.length
42
+ end
43
+
44
+ def test_bind_params_hash
45
+ @stmt.bind_params ":named" => 2
46
+ assert_equal 1, @stmt.execute!.length
47
+ end
48
+
49
+ def test_bind_params_hash_without_colon
50
+ @stmt.bind_params "named" => 2
51
+ assert_equal 1, @stmt.execute!.length
52
+ end
53
+
54
+ def test_bind_params_hash_as_symbol
55
+ @stmt.bind_params :named => 2
56
+ assert_equal 1, @stmt.execute!.length
57
+ end
58
+
59
+ def test_bind_params_mixed
60
+ @stmt.bind_params( 1, ":named" => 2 )
61
+ assert_equal 2, @stmt.execute!.length
62
+ end
63
+
64
+ def test_bind_param_by_index
65
+ @stmt.bind_params( 1, 2 )
66
+ assert_equal 2, @stmt.execute!.length
67
+ end
68
+
69
+ def test_bind_param_by_name_bad
70
+ assert_raise( SQLite3::Exception ) { @stmt.bind_param( "@named", 2 ) }
71
+ end
72
+
73
+ def test_bind_param_by_name_good
74
+ @stmt.bind_param( ":named", 2 )
75
+ assert_equal 1, @stmt.execute!.length
76
+ end
77
+
78
+ def test_bind_param_with_various_types
79
+ @db.transaction do
80
+ @db.execute "create table all_types ( a integer primary key, b float, c string, d integer )"
81
+ @db.execute "insert into all_types ( b, c, d ) values ( 1.4, 'hello', 68719476735 )"
82
+ end
83
+
84
+ assert_equal 1, @db.execute( "select * from all_types where b = ?", 1.4 ).length
85
+ assert_equal 1, @db.execute( "select * from all_types where c = ?", 'hello').length
86
+ assert_equal 1, @db.execute( "select * from all_types where d = ?", 68719476735).length
87
+ end
88
+
89
+ def test_execute_no_bind_no_block
90
+ assert_instance_of SQLite3::ResultSet, @stmt.execute
91
+ end
92
+
93
+ def test_execute_with_bind_no_block
94
+ assert_instance_of SQLite3::ResultSet, @stmt.execute( 1, 2 )
95
+ end
96
+
97
+ def test_execute_no_bind_with_block
98
+ called = false
99
+ @stmt.execute { |row| called = true }
100
+ assert called
101
+ end
102
+
103
+ def test_execute_with_bind_with_block
104
+ called = 0
105
+ @stmt.execute( 1, 2 ) { |row| called += 1 }
106
+ assert_equal 1, called
107
+ end
108
+
109
+ def test_reexecute
110
+ r = @stmt.execute( 1, 2 )
111
+ assert_equal 2, r.to_a.length
112
+ assert_nothing_raised { r = @stmt.execute( 1, 2 ) }
113
+ assert_equal 2, r.to_a.length
114
+ end
115
+
116
+ def test_execute_bang_no_bind_no_block
117
+ assert @stmt.execute!.empty?
118
+ end
119
+
120
+ def test_execute_bang_with_bind_no_block
121
+ assert_equal 2, @stmt.execute!( 1, 2 ).length
122
+ end
123
+
124
+ def test_execute_bang_no_bind_with_block
125
+ called = 0
126
+ @stmt.execute! { |row| called += 1 }
127
+ assert_equal 0, called
128
+ end
129
+
130
+ def test_execute_bang_with_bind_with_block
131
+ called = 0
132
+ @stmt.execute!( 1, 2 ) { |row| called += 1 }
133
+ assert_equal 2, called
134
+ end
135
+
136
+ def test_columns
137
+ c1 = @stmt.columns
138
+ c2 = @stmt.columns
139
+ assert_same c1, c2
140
+ assert_equal 2, c1.length
141
+ end
142
+
143
+ def test_columns_computed
144
+ called = false
145
+ @db.prepare( "select count(*) from foo" ) do |stmt|
146
+ called = true
147
+ assert_equal [ "count(*)" ], stmt.columns
148
+ end
149
+ assert called
150
+ end
151
+
152
+ def test_types
153
+ t1 = @stmt.types
154
+ t2 = @stmt.types
155
+ assert_same t1, t2
156
+ assert_equal 2, t1.length
157
+ end
158
+
159
+ def test_types_computed
160
+ called = false
161
+ @db.prepare( "select count(*) from foo" ) do |stmt|
162
+ called = true
163
+ assert_equal [ nil ], stmt.types
164
+ end
165
+ assert called
166
+ end
167
+
168
+ def test_close
169
+ stmt = @db.prepare( "select * from foo" )
170
+ assert !stmt.closed?
171
+ stmt.close
172
+ assert stmt.closed?
173
+ assert_raise( SQLite3::Exception ) { stmt.execute }
174
+ assert_raise( SQLite3::Exception ) { stmt.execute! }
175
+ assert_raise( SQLite3::Exception ) { stmt.close }
176
+ assert_raise( SQLite3::Exception ) { stmt.bind_params 5 }
177
+ assert_raise( SQLite3::Exception ) { stmt.bind_param 1, 5 }
178
+ assert_raise( SQLite3::Exception ) { stmt.columns }
179
+ assert_raise( SQLite3::Exception ) { stmt.types }
180
+ end
181
+
182
+ def test_committing_tx_with_statement_active
183
+ called = false
184
+ @db.prepare( "select count(*) from foo" ) do |stmt|
185
+ called = true
186
+ count = stmt.execute!.first.first.to_i
187
+ @db.transaction do
188
+ @db.execute "insert into foo ( b ) values ( 'hello' )"
189
+ end
190
+ new_count = stmt.execute!.first.first.to_i
191
+ assert_equal new_count, count+1
192
+ end
193
+ assert called
194
+ end
195
+ end