sqlite3 1.5.0.rc1-x86_64-darwin

Sign up to get free protection for your applications and to get access to all the features.
Files changed (60) hide show
  1. checksums.yaml +7 -0
  2. data/.gemtest +0 -0
  3. data/API_CHANGES.md +49 -0
  4. data/CHANGELOG.md +419 -0
  5. data/CONTRIBUTING.md +24 -0
  6. data/ChangeLog.cvs +88 -0
  7. data/Gemfile +3 -0
  8. data/LICENSE +27 -0
  9. data/LICENSE-DEPENDENCIES +20 -0
  10. data/README.md +233 -0
  11. data/ext/sqlite3/aggregator.c +273 -0
  12. data/ext/sqlite3/aggregator.h +12 -0
  13. data/ext/sqlite3/backup.c +168 -0
  14. data/ext/sqlite3/backup.h +15 -0
  15. data/ext/sqlite3/database.c +853 -0
  16. data/ext/sqlite3/database.h +17 -0
  17. data/ext/sqlite3/exception.c +98 -0
  18. data/ext/sqlite3/exception.h +8 -0
  19. data/ext/sqlite3/extconf.rb +159 -0
  20. data/ext/sqlite3/sqlite3.c +163 -0
  21. data/ext/sqlite3/sqlite3_ruby.h +45 -0
  22. data/ext/sqlite3/statement.c +442 -0
  23. data/ext/sqlite3/statement.h +16 -0
  24. data/faq/faq.md +431 -0
  25. data/faq/faq.rb +145 -0
  26. data/faq/faq.yml +426 -0
  27. data/lib/sqlite3/2.6/sqlite3_native.bundle +0 -0
  28. data/lib/sqlite3/2.7/sqlite3_native.bundle +0 -0
  29. data/lib/sqlite3/3.0/sqlite3_native.bundle +0 -0
  30. data/lib/sqlite3/3.1/sqlite3_native.bundle +0 -0
  31. data/lib/sqlite3/constants.rb +50 -0
  32. data/lib/sqlite3/database.rb +741 -0
  33. data/lib/sqlite3/errors.rb +35 -0
  34. data/lib/sqlite3/pragmas.rb +595 -0
  35. data/lib/sqlite3/resultset.rb +187 -0
  36. data/lib/sqlite3/statement.rb +145 -0
  37. data/lib/sqlite3/translator.rb +118 -0
  38. data/lib/sqlite3/value.rb +57 -0
  39. data/lib/sqlite3/version.rb +25 -0
  40. data/lib/sqlite3.rb +15 -0
  41. data/test/helper.rb +27 -0
  42. data/test/test_backup.rb +33 -0
  43. data/test/test_collation.rb +82 -0
  44. data/test/test_database.rb +538 -0
  45. data/test/test_database_flags.rb +95 -0
  46. data/test/test_database_readonly.rb +36 -0
  47. data/test/test_database_readwrite.rb +41 -0
  48. data/test/test_deprecated.rb +44 -0
  49. data/test/test_encoding.rb +155 -0
  50. data/test/test_integration.rb +507 -0
  51. data/test/test_integration_aggregate.rb +336 -0
  52. data/test/test_integration_open_close.rb +30 -0
  53. data/test/test_integration_pending.rb +115 -0
  54. data/test/test_integration_resultset.rb +142 -0
  55. data/test/test_integration_statement.rb +194 -0
  56. data/test/test_result_set.rb +37 -0
  57. data/test/test_sqlite3.rb +25 -0
  58. data/test/test_statement.rb +263 -0
  59. data/test/test_statement_execute.rb +35 -0
  60. metadata +190 -0
@@ -0,0 +1,194 @@
1
+ require 'helper'
2
+
3
+ class TC_Statement < SQLite3::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
@@ -0,0 +1,37 @@
1
+ require 'helper'
2
+
3
+ module SQLite3
4
+ class TestResultSet < SQLite3::TestCase
5
+ def test_each_hash
6
+ db = SQLite3::Database.new ':memory:'
7
+ db.execute "create table foo ( a integer primary key, b text )"
8
+ list = ('a'..'z').to_a
9
+ list.each do |t|
10
+ db.execute "insert into foo (b) values (\"#{t}\")"
11
+ end
12
+
13
+ rs = db.prepare('select * from foo').execute
14
+ rs.each_hash do |hash|
15
+ assert_equal list[hash['a'] - 1], hash['b']
16
+ end
17
+ end
18
+
19
+ def test_next_hash
20
+ db = SQLite3::Database.new ':memory:'
21
+ db.execute "create table foo ( a integer primary key, b text )"
22
+ list = ('a'..'z').to_a
23
+ list.each do |t|
24
+ db.execute "insert into foo (b) values (\"#{t}\")"
25
+ end
26
+
27
+ rs = db.prepare('select * from foo').execute
28
+ rows = []
29
+ while row = rs.next_hash
30
+ rows << row
31
+ end
32
+ rows.each do |hash|
33
+ assert_equal list[hash['a'] - 1], hash['b']
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,25 @@
1
+ require 'helper'
2
+
3
+ module SQLite3
4
+ class TestSQLite3 < SQLite3::TestCase
5
+ def test_libversion
6
+ assert_not_nil SQLite3.libversion
7
+ end
8
+
9
+ def test_threadsafe
10
+ assert_not_nil SQLite3.threadsafe
11
+ end
12
+
13
+ def test_threadsafe?
14
+ if SQLite3.threadsafe > 0
15
+ assert SQLite3.threadsafe?
16
+ else
17
+ refute SQLite3.threadsafe?
18
+ end
19
+ end
20
+
21
+ def test_version_strings
22
+ assert_equal(SQLite3::VERSION, SQLite3::VersionProxy::STRING)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,263 @@
1
+ require 'helper'
2
+
3
+ module SQLite3
4
+ class TestStatement < SQLite3::TestCase
5
+ def setup
6
+ @db = SQLite3::Database.new(':memory:')
7
+ @stmt = SQLite3::Statement.new(@db, "select 'foo'")
8
+ end
9
+
10
+ def test_double_close_does_not_segv
11
+ @db.execute 'CREATE TABLE "things" ("number" float NOT NULL)'
12
+
13
+ stmt = @db.prepare 'INSERT INTO things (number) VALUES (?)'
14
+ assert_raises(SQLite3::ConstraintException) { stmt.execute(nil) }
15
+
16
+ stmt.close
17
+
18
+ assert_raises(SQLite3::Exception) { stmt.close }
19
+ end
20
+
21
+ def test_raises_type_error
22
+ assert_raises(TypeError) do
23
+ SQLite3::Statement.new( @db, nil )
24
+ end
25
+ end
26
+
27
+ def test_insert_duplicate_records
28
+ @db.execute 'CREATE TABLE "things" ("name" varchar(20) CONSTRAINT "index_things_on_name" UNIQUE)'
29
+ stmt = @db.prepare("INSERT INTO things(name) VALUES(?)")
30
+ stmt.execute('ruby')
31
+
32
+ exception = assert_raises(SQLite3::ConstraintException) { stmt.execute('ruby') }
33
+ # SQLite 3.8.2 returns new error message:
34
+ # UNIQUE constraint failed: *table_name*.*column_name*
35
+ # Older versions of SQLite return:
36
+ # column *column_name* is not unique
37
+ assert_match(/(column(s)? .* (is|are) not unique|UNIQUE constraint failed: .*)/, exception.message)
38
+ end
39
+
40
+ ###
41
+ # This method may not exist depending on how sqlite3 was compiled
42
+ def test_database_name
43
+ @db.execute('create table foo(text BLOB)')
44
+ @db.execute('insert into foo(text) values (?)',SQLite3::Blob.new('hello'))
45
+ stmt = @db.prepare('select text from foo')
46
+ if stmt.respond_to?(:database_name)
47
+ assert_equal 'main', stmt.database_name(0)
48
+ end
49
+ end
50
+
51
+ def test_prepare_blob
52
+ @db.execute('create table foo(text BLOB)')
53
+ stmt = @db.prepare('insert into foo(text) values (?)')
54
+ stmt.bind_param(1, SQLite3::Blob.new('hello'))
55
+ stmt.step
56
+ stmt.close
57
+ end
58
+
59
+ def test_select_blob
60
+ @db.execute('create table foo(text BLOB)')
61
+ @db.execute('insert into foo(text) values (?)',SQLite3::Blob.new('hello'))
62
+ assert_equal 'hello', @db.execute('select * from foo').first.first
63
+ end
64
+
65
+ def test_new
66
+ assert @stmt
67
+ end
68
+
69
+ def test_new_closed_handle
70
+ @db = SQLite3::Database.new(':memory:')
71
+ @db.close
72
+ assert_raises(ArgumentError) do
73
+ SQLite3::Statement.new(@db, 'select "foo"')
74
+ end
75
+ end
76
+
77
+ def test_new_with_remainder
78
+ stmt = SQLite3::Statement.new(@db, "select 'foo';bar")
79
+ assert_equal 'bar', stmt.remainder
80
+ end
81
+
82
+ def test_empty_remainder
83
+ assert_equal '', @stmt.remainder
84
+ end
85
+
86
+ def test_close
87
+ @stmt.close
88
+ assert @stmt.closed?
89
+ end
90
+
91
+ def test_double_close
92
+ @stmt.close
93
+ assert_raises(SQLite3::Exception) do
94
+ @stmt.close
95
+ end
96
+ end
97
+
98
+ def test_bind_param_string
99
+ stmt = SQLite3::Statement.new(@db, "select ?")
100
+ stmt.bind_param(1, "hello")
101
+ result = nil
102
+ stmt.each { |x| result = x }
103
+ assert_equal ['hello'], result
104
+ end
105
+
106
+ def test_bind_param_int
107
+ stmt = SQLite3::Statement.new(@db, "select ?")
108
+ stmt.bind_param(1, 10)
109
+ result = nil
110
+ stmt.each { |x| result = x }
111
+ assert_equal [10], result
112
+ end
113
+
114
+ def test_bind_nil
115
+ stmt = SQLite3::Statement.new(@db, "select ?")
116
+ stmt.bind_param(1, nil)
117
+ result = nil
118
+ stmt.each { |x| result = x }
119
+ assert_equal [nil], result
120
+ end
121
+
122
+ def test_bind_blob
123
+ @db.execute('create table foo(text BLOB)')
124
+ stmt = SQLite3::Statement.new(@db, 'insert into foo(text) values (?)')
125
+ stmt.bind_param(1, SQLite3::Blob.new('hello'))
126
+ stmt.execute
127
+ row = @db.execute('select * from foo')
128
+
129
+ assert_equal ['hello'], row.first
130
+ assert_equal ['blob'], row.first.types
131
+ end
132
+
133
+ def test_bind_64
134
+ stmt = SQLite3::Statement.new(@db, "select ?")
135
+ stmt.bind_param(1, 2 ** 31)
136
+ result = nil
137
+ stmt.each { |x| result = x }
138
+ assert_equal [2 ** 31], result
139
+ end
140
+
141
+ def test_bind_double
142
+ stmt = SQLite3::Statement.new(@db, "select ?")
143
+ stmt.bind_param(1, 2.2)
144
+ result = nil
145
+ stmt.each { |x| result = x }
146
+ assert_equal [2.2], result
147
+ end
148
+
149
+ def test_named_bind
150
+ stmt = SQLite3::Statement.new(@db, "select :foo")
151
+ stmt.bind_param(':foo', 'hello')
152
+ result = nil
153
+ stmt.each { |x| result = x }
154
+ assert_equal ['hello'], result
155
+ end
156
+
157
+ def test_named_bind_no_colon
158
+ stmt = SQLite3::Statement.new(@db, "select :foo")
159
+ stmt.bind_param('foo', 'hello')
160
+ result = nil
161
+ stmt.each { |x| result = x }
162
+ assert_equal ['hello'], result
163
+ end
164
+
165
+ def test_named_bind_symbol
166
+ stmt = SQLite3::Statement.new(@db, "select :foo")
167
+ stmt.bind_param(:foo, 'hello')
168
+ result = nil
169
+ stmt.each { |x| result = x }
170
+ assert_equal ['hello'], result
171
+ end
172
+
173
+ def test_named_bind_not_found
174
+ stmt = SQLite3::Statement.new(@db, "select :foo")
175
+ assert_raises(SQLite3::Exception) do
176
+ stmt.bind_param('bar', 'hello')
177
+ end
178
+ end
179
+
180
+ def test_each
181
+ r = nil
182
+ @stmt.each do |row|
183
+ r = row
184
+ end
185
+ assert_equal(['foo'], r)
186
+ end
187
+
188
+ def test_reset!
189
+ r = []
190
+ @stmt.each { |row| r << row }
191
+ @stmt.reset!
192
+ @stmt.each { |row| r << row }
193
+ assert_equal [['foo'], ['foo']], r
194
+ end
195
+
196
+ def test_step
197
+ r = @stmt.step
198
+ assert_equal ['foo'], r
199
+ end
200
+
201
+ def test_step_twice
202
+ assert_not_nil @stmt.step
203
+ assert !@stmt.done?
204
+ assert_nil @stmt.step
205
+ assert @stmt.done?
206
+
207
+ @stmt.reset!
208
+ assert !@stmt.done?
209
+ end
210
+
211
+ def test_step_never_moves_past_done
212
+ 10.times { @stmt.step }
213
+ @stmt.done?
214
+ end
215
+
216
+ def test_column_count
217
+ assert_equal 1, @stmt.column_count
218
+ end
219
+
220
+ def test_column_name
221
+ assert_equal "'foo'", @stmt.column_name(0)
222
+ assert_nil @stmt.column_name(10)
223
+ end
224
+
225
+ def test_bind_parameter_count
226
+ stmt = SQLite3::Statement.new(@db, "select ?, ?, ?")
227
+ assert_equal 3, stmt.bind_parameter_count
228
+ end
229
+
230
+ def test_execute_with_varargs
231
+ stmt = @db.prepare('select ?, ?')
232
+ assert_equal [[nil, nil]], stmt.execute(nil, nil).to_a
233
+ end
234
+
235
+ def test_execute_with_hash
236
+ stmt = @db.prepare('select :n, :h')
237
+ assert_equal [[10, nil]], stmt.execute('n' => 10, 'h' => nil).to_a
238
+ end
239
+
240
+ def test_with_error
241
+ @db.execute('CREATE TABLE "employees" ("name" varchar(20) NOT NULL CONSTRAINT "index_employees_on_name" UNIQUE)')
242
+ stmt = @db.prepare("INSERT INTO Employees(name) VALUES(?)")
243
+ stmt.execute('employee-1')
244
+ stmt.execute('employee-1') rescue SQLite3::ConstraintException
245
+ stmt.reset!
246
+ assert stmt.execute('employee-2')
247
+ end
248
+
249
+ def test_clear_bindings!
250
+ stmt = @db.prepare('select ?, ?')
251
+ stmt.bind_param 1, "foo"
252
+ stmt.bind_param 2, "bar"
253
+
254
+ # We can't fetch bound parameters back out of sqlite3, so just call
255
+ # the clear_bindings! method and assert that nil is returned
256
+ stmt.clear_bindings!
257
+
258
+ while x = stmt.step
259
+ assert_equal [nil, nil], x
260
+ end
261
+ end
262
+ end
263
+ end
@@ -0,0 +1,35 @@
1
+ require 'helper'
2
+
3
+ module SQLite3
4
+ class TestStatementExecute < SQLite3::TestCase
5
+ def setup
6
+ @db = SQLite3::Database.new(':memory:')
7
+ @db.execute_batch(
8
+ "CREATE TABLE items (id integer PRIMARY KEY, number integer)")
9
+ end
10
+
11
+ def test_execute_insert
12
+ ps = @db.prepare("INSERT INTO items (number) VALUES (:n)")
13
+ ps.execute('n'=>10)
14
+ assert_equal 1, @db.get_first_value("SELECT count(*) FROM items")
15
+ ps.close
16
+ end
17
+
18
+ def test_execute_update
19
+ @db.execute("INSERT INTO items (number) VALUES (?)", [10])
20
+
21
+ ps = @db.prepare("UPDATE items SET number = :new WHERE number = :old")
22
+ ps.execute('old'=>10, 'new'=>20)
23
+ assert_equal 20, @db.get_first_value("SELECT number FROM items")
24
+ ps.close
25
+ end
26
+
27
+ def test_execute_delete
28
+ @db.execute("INSERT INTO items (number) VALUES (?)", [20])
29
+ ps = @db.prepare("DELETE FROM items WHERE number = :n")
30
+ ps.execute('n' => 20)
31
+ assert_equal 0, @db.get_first_value("SELECT count(*) FROM items")
32
+ ps.close
33
+ end
34
+ end
35
+ end