sqlite3-ruby 1.3.0.beta.2-x86-mswin32-60

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/API_CHANGES.rdoc +48 -0
  2. data/CHANGELOG.rdoc +108 -0
  3. data/ChangeLog.cvs +88 -0
  4. data/LICENSE +27 -0
  5. data/Manifest.txt +44 -0
  6. data/README.rdoc +54 -0
  7. data/Rakefile +10 -0
  8. data/ext/sqlite3/database.c +693 -0
  9. data/ext/sqlite3/database.h +15 -0
  10. data/ext/sqlite3/exception.c +94 -0
  11. data/ext/sqlite3/exception.h +8 -0
  12. data/ext/sqlite3/extconf.rb +39 -0
  13. data/ext/sqlite3/sqlite3.c +33 -0
  14. data/ext/sqlite3/sqlite3_ruby.h +43 -0
  15. data/ext/sqlite3/statement.c +419 -0
  16. data/ext/sqlite3/statement.h +16 -0
  17. data/faq/faq.rb +145 -0
  18. data/faq/faq.yml +426 -0
  19. data/lib/sqlite3.rb +10 -0
  20. data/lib/sqlite3/1.8/sqlite3_native.so +0 -0
  21. data/lib/sqlite3/1.9/sqlite3_native.so +0 -0
  22. data/lib/sqlite3/constants.rb +49 -0
  23. data/lib/sqlite3/database.rb +568 -0
  24. data/lib/sqlite3/errors.rb +44 -0
  25. data/lib/sqlite3/pragmas.rb +280 -0
  26. data/lib/sqlite3/resultset.rb +126 -0
  27. data/lib/sqlite3/statement.rb +146 -0
  28. data/lib/sqlite3/translator.rb +114 -0
  29. data/lib/sqlite3/value.rb +57 -0
  30. data/lib/sqlite3/version.rb +16 -0
  31. data/setup.rb +1333 -0
  32. data/tasks/faq.rake +9 -0
  33. data/tasks/gem.rake +31 -0
  34. data/tasks/native.rake +31 -0
  35. data/tasks/vendor_sqlite3.rake +107 -0
  36. data/test/helper.rb +3 -0
  37. data/test/test_database.rb +291 -0
  38. data/test/test_deprecated.rb +25 -0
  39. data/test/test_encoding.rb +115 -0
  40. data/test/test_integration.rb +545 -0
  41. data/test/test_integration_open_close.rb +30 -0
  42. data/test/test_integration_pending.rb +113 -0
  43. data/test/test_integration_resultset.rb +183 -0
  44. data/test/test_integration_statement.rb +194 -0
  45. data/test/test_sqlite3.rb +9 -0
  46. data/test/test_statement.rb +207 -0
  47. metadata +181 -0
@@ -0,0 +1,30 @@
1
+ require '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,113 @@
1
+ require '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
+ sleep 1
69
+
70
+ @db.busy_handler do |count|
71
+ handler_call_count += 1
72
+ false
73
+ end
74
+
75
+ assert_raise( SQLite3::BusyException ) do
76
+ @db.execute "insert into foo (b) values ( 'from 2' )"
77
+ end
78
+
79
+ busy.unlock
80
+ t.join
81
+
82
+ assert_equal 1, handler_call_count
83
+ end
84
+
85
+ def test_busy_timeout
86
+ @db.busy_timeout 1000
87
+ busy = Mutex.new
88
+ busy.lock
89
+
90
+ t = Thread.new do
91
+ begin
92
+ db2 = SQLite3::Database.open( "test.db" )
93
+ db2.transaction( :exclusive ) do
94
+ busy.lock
95
+ end
96
+ ensure
97
+ db2.close if db2
98
+ end
99
+ end
100
+
101
+ sleep 1
102
+ time = Benchmark.measure do
103
+ assert_raise( SQLite3::BusyException ) do
104
+ @db.execute "insert into foo (b) values ( 'from 2' )"
105
+ end
106
+ end
107
+
108
+ busy.unlock
109
+ t.join
110
+
111
+ assert time.real*1000 >= 1000
112
+ end
113
+ end
@@ -0,0 +1,183 @@
1
+ require 'helper'
2
+
3
+ class TC_ResultSet < Test::Unit::TestCase
4
+ def setup
5
+ @db = SQLite3::Database.new(":memory:")
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
+ end
20
+
21
+ def test_reset_unused
22
+ assert_nothing_raised { @result.reset }
23
+ assert @result.to_a.empty?
24
+ end
25
+
26
+ def test_reset_used
27
+ @result.to_a
28
+ assert_nothing_raised { @result.reset }
29
+ assert @result.to_a.empty?
30
+ end
31
+
32
+ def test_reset_with_bind
33
+ @result.to_a
34
+ assert_nothing_raised { @result.reset( 1, 2 ) }
35
+ assert_equal 2, @result.to_a.length
36
+ end
37
+
38
+ def test_eof_inner
39
+ @result.reset( 1 )
40
+ assert !@result.eof?
41
+ end
42
+
43
+ def test_eof_edge
44
+ @result.reset( 1 )
45
+ @result.next # to first row
46
+ @result.next # to end of result set
47
+ assert @result.eof?
48
+ end
49
+
50
+ def test_next_eof
51
+ @result.reset( 1 )
52
+ assert_not_nil @result.next
53
+ assert_nil @result.next
54
+ end
55
+
56
+ def test_next_no_type_translation_no_hash
57
+ @result.reset( 1 )
58
+ assert_equal [ 1, "foo" ], @result.next
59
+ end
60
+
61
+ def test_next_type_translation
62
+ @db.type_translation = true
63
+ @result.reset( 1 )
64
+ assert_equal [ 1, "foo" ], @result.next
65
+ end
66
+
67
+ def test_next_type_translation_with_untyped_column
68
+ @db.type_translation = true
69
+ @db.query( "select count(*) from foo" ) do |result|
70
+ assert_equal [3], result.next
71
+ end
72
+ end
73
+
74
+ def test_type_translation_execute
75
+ @db.type_translation = true
76
+ @db.execute "create table bar ( a integer, b america )"
77
+ @db.execute "insert into bar (a, b) values (NULL, '1974-07-25 14:39:00')"
78
+
79
+ @db.translator.add_translator('america') do |type, thing|
80
+ 'america'
81
+ end
82
+
83
+ assert_equal [[nil, 'america']], @db.execute("select * from bar")
84
+ end
85
+
86
+ def test_type_translation_with_null_column
87
+ @db.type_translation = true
88
+ @db.execute "create table bar ( a integer, b time, c string )"
89
+ @db.execute "insert into bar (a, b, c) values (NULL, '1974-07-25 14:39:00', 'hello')"
90
+ @db.execute "insert into bar (a, b, c) values (1, NULL, 'hello')"
91
+ @db.execute "insert into bar (a, b, c) values (2, '1974-07-25 14:39:00', NULL)"
92
+ @db.query( "select * from bar" ) do |result|
93
+ assert_equal [nil, Time.local(1974, 7, 25, 14, 39, 0), 'hello'], result.next
94
+ assert_equal [1, nil, 'hello'], result.next
95
+ assert_equal [2, Time.local(1974, 7, 25, 14, 39, 0), nil], result.next
96
+ end
97
+ end
98
+
99
+ def test_date_and_time_translation
100
+ @db.type_translation = true
101
+ @db.execute "create table bar ( a date, b datetime, c time, d timestamp )"
102
+ @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')"
103
+ @db.query( "select * from bar" ) do |result|
104
+ result = result.next
105
+ assert result[0].is_a?(Date)
106
+ assert result[1].is_a?(DateTime)
107
+ assert result[2].is_a?(Time)
108
+ assert result[3].is_a?(Time)
109
+ end
110
+ end
111
+
112
+ def test_real_translation
113
+ @db.type_translation = true
114
+ @db.execute('create table foo_real(a real)')
115
+ @db.execute('insert into foo_real values (42)' )
116
+ @db.query('select a, sum(a), typeof(a), typeof(sum(a)) from foo_real') do |result|
117
+ result = result.next
118
+ assert result[0].is_a?(Float)
119
+ assert result[1].is_a?(Float)
120
+ assert result[2].is_a?(String)
121
+ assert result[3].is_a?(String)
122
+ end
123
+ end
124
+
125
+ def test_next_results_as_hash
126
+ @db.results_as_hash = true
127
+ @result.reset( 1 )
128
+ assert_equal( { "a" => 1, "b" => "foo", 0 => 1, 1 => "foo" },
129
+ @result.next )
130
+ end
131
+
132
+ def test_tainted_results_as_hash
133
+ @db.results_as_hash = true
134
+ @result.reset( 1 )
135
+ row = @result.next
136
+ row.each do |_, v|
137
+ assert(v.tainted?) if String === v
138
+ end
139
+ end
140
+
141
+ def test_tainted_row_values
142
+ @result.reset( 1 )
143
+ row = @result.next
144
+ row.each do |v|
145
+ assert(v.tainted?) if String === v
146
+ end
147
+ end
148
+
149
+ def test_each
150
+ called = 0
151
+ @result.reset( 1, 2 )
152
+ @result.each { |row| called += 1 }
153
+ assert_equal 2, called
154
+ end
155
+
156
+ def test_enumerable
157
+ @result.reset( 1, 2 )
158
+ assert_equal 2, @result.to_a.length
159
+ end
160
+
161
+ def test_types
162
+ assert_equal [ "integer", "text" ], @result.types
163
+ end
164
+
165
+ def test_columns
166
+ assert_equal [ "a", "b" ], @result.columns
167
+ end
168
+
169
+ def test_close
170
+ stmt = @db.prepare( "select * from foo" )
171
+ result = stmt.execute
172
+ assert !result.closed?
173
+ result.close
174
+ assert result.closed?
175
+ assert stmt.closed?
176
+ assert_raise( SQLite3::Exception ) { result.reset }
177
+ assert_raise( SQLite3::Exception ) { result.next }
178
+ assert_raise( SQLite3::Exception ) { result.each }
179
+ assert_raise( SQLite3::Exception ) { result.close }
180
+ assert_raise( SQLite3::Exception ) { result.types }
181
+ assert_raise( SQLite3::Exception ) { result.columns }
182
+ end
183
+ end
@@ -0,0 +1,194 @@
1
+ require File.join(File.dirname(__FILE__), 'helper')
2
+
3
+ class TC_Statement < Test::Unit::TestCase
4
+ def setup
5
+ @db = SQLite3::Database.new(":memory:")
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
+ end
19
+
20
+ def test_remainder_empty
21
+ assert_equal "", @stmt.remainder
22
+ end
23
+
24
+ def test_remainder_nonempty
25
+ called = false
26
+ @db.prepare( "select * from foo;\n blah" ) do |stmt|
27
+ called = true
28
+ assert_equal "\n blah", stmt.remainder
29
+ end
30
+ assert called
31
+ end
32
+
33
+ def test_bind_params_empty
34
+ assert_nothing_raised { @stmt.bind_params }
35
+ assert @stmt.execute!.empty?
36
+ end
37
+
38
+ def test_bind_params_array
39
+ @stmt.bind_params 1, 2
40
+ assert_equal 2, @stmt.execute!.length
41
+ end
42
+
43
+ def test_bind_params_hash
44
+ @stmt.bind_params ":named" => 2
45
+ assert_equal 1, @stmt.execute!.length
46
+ end
47
+
48
+ def test_bind_params_hash_without_colon
49
+ @stmt.bind_params "named" => 2
50
+ assert_equal 1, @stmt.execute!.length
51
+ end
52
+
53
+ def test_bind_params_hash_as_symbol
54
+ @stmt.bind_params :named => 2
55
+ assert_equal 1, @stmt.execute!.length
56
+ end
57
+
58
+ def test_bind_params_mixed
59
+ @stmt.bind_params( 1, ":named" => 2 )
60
+ assert_equal 2, @stmt.execute!.length
61
+ end
62
+
63
+ def test_bind_param_by_index
64
+ @stmt.bind_params( 1, 2 )
65
+ assert_equal 2, @stmt.execute!.length
66
+ end
67
+
68
+ def test_bind_param_by_name_bad
69
+ assert_raise( SQLite3::Exception ) { @stmt.bind_param( "@named", 2 ) }
70
+ end
71
+
72
+ def test_bind_param_by_name_good
73
+ @stmt.bind_param( ":named", 2 )
74
+ assert_equal 1, @stmt.execute!.length
75
+ end
76
+
77
+ def test_bind_param_with_various_types
78
+ @db.transaction do
79
+ @db.execute "create table all_types ( a integer primary key, b float, c string, d integer )"
80
+ @db.execute "insert into all_types ( b, c, d ) values ( 1.4, 'hello', 68719476735 )"
81
+ end
82
+
83
+ assert_equal 1, @db.execute( "select * from all_types where b = ?", 1.4 ).length
84
+ assert_equal 1, @db.execute( "select * from all_types where c = ?", 'hello').length
85
+ assert_equal 1, @db.execute( "select * from all_types where d = ?", 68719476735).length
86
+ end
87
+
88
+ def test_execute_no_bind_no_block
89
+ assert_instance_of SQLite3::ResultSet, @stmt.execute
90
+ end
91
+
92
+ def test_execute_with_bind_no_block
93
+ assert_instance_of SQLite3::ResultSet, @stmt.execute( 1, 2 )
94
+ end
95
+
96
+ def test_execute_no_bind_with_block
97
+ called = false
98
+ @stmt.execute { |row| called = true }
99
+ assert called
100
+ end
101
+
102
+ def test_execute_with_bind_with_block
103
+ called = 0
104
+ @stmt.execute( 1, 2 ) { |row| called += 1 }
105
+ assert_equal 1, called
106
+ end
107
+
108
+ def test_reexecute
109
+ r = @stmt.execute( 1, 2 )
110
+ assert_equal 2, r.to_a.length
111
+ assert_nothing_raised { r = @stmt.execute( 1, 2 ) }
112
+ assert_equal 2, r.to_a.length
113
+ end
114
+
115
+ def test_execute_bang_no_bind_no_block
116
+ assert @stmt.execute!.empty?
117
+ end
118
+
119
+ def test_execute_bang_with_bind_no_block
120
+ assert_equal 2, @stmt.execute!( 1, 2 ).length
121
+ end
122
+
123
+ def test_execute_bang_no_bind_with_block
124
+ called = 0
125
+ @stmt.execute! { |row| called += 1 }
126
+ assert_equal 0, called
127
+ end
128
+
129
+ def test_execute_bang_with_bind_with_block
130
+ called = 0
131
+ @stmt.execute!( 1, 2 ) { |row| called += 1 }
132
+ assert_equal 2, called
133
+ end
134
+
135
+ def test_columns
136
+ c1 = @stmt.columns
137
+ c2 = @stmt.columns
138
+ assert_same c1, c2
139
+ assert_equal 2, c1.length
140
+ end
141
+
142
+ def test_columns_computed
143
+ called = false
144
+ @db.prepare( "select count(*) from foo" ) do |stmt|
145
+ called = true
146
+ assert_equal [ "count(*)" ], stmt.columns
147
+ end
148
+ assert called
149
+ end
150
+
151
+ def test_types
152
+ t1 = @stmt.types
153
+ t2 = @stmt.types
154
+ assert_same t1, t2
155
+ assert_equal 2, t1.length
156
+ end
157
+
158
+ def test_types_computed
159
+ called = false
160
+ @db.prepare( "select count(*) from foo" ) do |stmt|
161
+ called = true
162
+ assert_equal [ nil ], stmt.types
163
+ end
164
+ assert called
165
+ end
166
+
167
+ def test_close
168
+ stmt = @db.prepare( "select * from foo" )
169
+ assert !stmt.closed?
170
+ stmt.close
171
+ assert stmt.closed?
172
+ assert_raise( SQLite3::Exception ) { stmt.execute }
173
+ assert_raise( SQLite3::Exception ) { stmt.execute! }
174
+ assert_raise( SQLite3::Exception ) { stmt.close }
175
+ assert_raise( SQLite3::Exception ) { stmt.bind_params 5 }
176
+ assert_raise( SQLite3::Exception ) { stmt.bind_param 1, 5 }
177
+ assert_raise( SQLite3::Exception ) { stmt.columns }
178
+ assert_raise( SQLite3::Exception ) { stmt.types }
179
+ end
180
+
181
+ def test_committing_tx_with_statement_active
182
+ called = false
183
+ @db.prepare( "select count(*) from foo" ) do |stmt|
184
+ called = true
185
+ count = stmt.execute!.first.first.to_i
186
+ @db.transaction do
187
+ @db.execute "insert into foo ( b ) values ( 'hello' )"
188
+ end
189
+ new_count = stmt.execute!.first.first.to_i
190
+ assert_equal new_count, count+1
191
+ end
192
+ assert called
193
+ end
194
+ end