sqlite3-full 1.3.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +7 -0
  2. data/.gemtest +0 -0
  3. data/API_CHANGES.rdoc +50 -0
  4. data/CHANGELOG.rdoc +278 -0
  5. data/ChangeLog.cvs +88 -0
  6. data/Gemfile +15 -0
  7. data/LICENSE +34 -0
  8. data/Manifest.txt +52 -0
  9. data/README.rdoc +90 -0
  10. data/Rakefile +10 -0
  11. data/ext/sqlite3/backup.c +168 -0
  12. data/ext/sqlite3/backup.h +15 -0
  13. data/ext/sqlite3/database.c +825 -0
  14. data/ext/sqlite3/database.h +15 -0
  15. data/ext/sqlite3/exception.c +94 -0
  16. data/ext/sqlite3/exception.h +8 -0
  17. data/ext/sqlite3/extconf.rb +86 -0
  18. data/ext/sqlite3/sqlite3.c +97 -0
  19. data/ext/sqlite3/sqlite3_ruby.h +52 -0
  20. data/ext/sqlite3/sqlite3amalgamation.c +153367 -0
  21. data/ext/sqlite3/statement.c +447 -0
  22. data/ext/sqlite3/statement.h +16 -0
  23. data/faq/faq.rb +145 -0
  24. data/faq/faq.yml +426 -0
  25. data/lib/sqlite3/constants.rb +49 -0
  26. data/lib/sqlite3/database.rb +590 -0
  27. data/lib/sqlite3/errors.rb +44 -0
  28. data/lib/sqlite3/pragmas.rb +280 -0
  29. data/lib/sqlite3/resultset.rb +195 -0
  30. data/lib/sqlite3/statement.rb +144 -0
  31. data/lib/sqlite3/translator.rb +118 -0
  32. data/lib/sqlite3/value.rb +57 -0
  33. data/lib/sqlite3/version.rb +25 -0
  34. data/lib/sqlite3.rb +10 -0
  35. data/setup.rb +1333 -0
  36. data/tasks/faq.rake +9 -0
  37. data/tasks/gem.rake +38 -0
  38. data/tasks/native.rake +52 -0
  39. data/tasks/vendor_sqlite3.rake +91 -0
  40. data/test/helper.rb +18 -0
  41. data/test/test_backup.rb +33 -0
  42. data/test/test_collation.rb +82 -0
  43. data/test/test_database.rb +367 -0
  44. data/test/test_database_readonly.rb +29 -0
  45. data/test/test_deprecated.rb +44 -0
  46. data/test/test_encoding.rb +153 -0
  47. data/test/test_integration.rb +572 -0
  48. data/test/test_integration_open_close.rb +30 -0
  49. data/test/test_integration_pending.rb +115 -0
  50. data/test/test_integration_resultset.rb +159 -0
  51. data/test/test_integration_statement.rb +194 -0
  52. data/test/test_result_set.rb +37 -0
  53. data/test/test_sqlite3.rb +9 -0
  54. data/test/test_statement.rb +260 -0
  55. data/test/test_statement_execute.rb +35 -0
  56. metadata +205 -0
@@ -0,0 +1,159 @@
1
+ require 'helper'
2
+
3
+ class TC_ResultSet < 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 ( ?, ? )" )
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
+ @result.reset( 1 )
63
+ assert_equal [ 1, "foo" ], @result.next
64
+ end
65
+
66
+ def test_next_type_translation_with_untyped_column
67
+ @db.query( "select count(*) from foo" ) do |result|
68
+ assert_equal [3], result.next
69
+ end
70
+ end
71
+
72
+ def test_type_translation_with_null_column
73
+ time = '1974-07-25 14:39:00'
74
+
75
+ @db.execute "create table bar ( a integer, b time, c string )"
76
+ @db.execute "insert into bar (a, b, c) values (NULL, '#{time}', 'hello')"
77
+ @db.execute "insert into bar (a, b, c) values (1, NULL, 'hello')"
78
+ @db.execute "insert into bar (a, b, c) values (2, '#{time}', NULL)"
79
+ @db.query( "select * from bar" ) do |result|
80
+ assert_equal [nil, time, 'hello'], result.next
81
+ assert_equal [1, nil, 'hello'], result.next
82
+ assert_equal [2, time, nil], result.next
83
+ end
84
+ end
85
+
86
+ def test_real_translation
87
+ @db.execute('create table foo_real(a real)')
88
+ @db.execute('insert into foo_real values (42)' )
89
+ @db.query('select a, sum(a), typeof(a), typeof(sum(a)) from foo_real') do |result|
90
+ result = result.next
91
+ assert result[0].is_a?(Float)
92
+ assert result[1].is_a?(Float)
93
+ assert result[2].is_a?(String)
94
+ assert result[3].is_a?(String)
95
+ end
96
+ end
97
+
98
+ def test_next_results_as_hash
99
+ @db.results_as_hash = true
100
+ @result.reset( 1 )
101
+ hash = @result.next
102
+ assert_equal( { "a" => 1, "b" => "foo" },
103
+ hash )
104
+ assert_equal hash[0], 1
105
+ assert_equal hash[1], "foo"
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(v.tainted?) if String === v
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(v.tainted?) if String === v
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,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,9 @@
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
+ end
9
+ end
@@ -0,0 +1,260 @@
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_blobs
123
+ end
124
+
125
+ def test_bind_64
126
+ stmt = SQLite3::Statement.new(@db, "select ?")
127
+ stmt.bind_param(1, 2 ** 31)
128
+ result = nil
129
+ stmt.each { |x| result = x }
130
+ assert_equal [2 ** 31], result
131
+ end
132
+
133
+ def test_bind_double
134
+ stmt = SQLite3::Statement.new(@db, "select ?")
135
+ stmt.bind_param(1, 2.2)
136
+ result = nil
137
+ stmt.each { |x| result = x }
138
+ assert_equal [2.2], result
139
+ end
140
+
141
+ def test_named_bind
142
+ stmt = SQLite3::Statement.new(@db, "select :foo")
143
+ stmt.bind_param(':foo', 'hello')
144
+ result = nil
145
+ stmt.each { |x| result = x }
146
+ assert_equal ['hello'], result
147
+ end
148
+
149
+ def test_named_bind_no_colon
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_symbol
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_not_found
166
+ stmt = SQLite3::Statement.new(@db, "select :foo")
167
+ assert_raises(SQLite3::Exception) do
168
+ stmt.bind_param('bar', 'hello')
169
+ end
170
+ end
171
+
172
+ def test_each
173
+ r = nil
174
+ @stmt.each do |row|
175
+ r = row
176
+ end
177
+ assert_equal(['foo'], r)
178
+ end
179
+
180
+ def test_reset!
181
+ r = []
182
+ @stmt.each { |row| r << row }
183
+ @stmt.reset!
184
+ @stmt.each { |row| r << row }
185
+ assert_equal [['foo'], ['foo']], r
186
+ end
187
+
188
+ def test_step
189
+ r = @stmt.step
190
+ assert_equal ['foo'], r
191
+ end
192
+
193
+ def test_tainted
194
+ r = @stmt.step
195
+ assert r.first.tainted?
196
+ end
197
+
198
+ def test_step_twice
199
+ assert_not_nil @stmt.step
200
+ assert !@stmt.done?
201
+ assert_nil @stmt.step
202
+ assert @stmt.done?
203
+
204
+ @stmt.reset!
205
+ assert !@stmt.done?
206
+ end
207
+
208
+ def test_step_never_moves_past_done
209
+ 10.times { @stmt.step }
210
+ @stmt.done?
211
+ end
212
+
213
+ def test_column_count
214
+ assert_equal 1, @stmt.column_count
215
+ end
216
+
217
+ def test_column_name
218
+ assert_equal "'foo'", @stmt.column_name(0)
219
+ assert_equal nil, @stmt.column_name(10)
220
+ end
221
+
222
+ def test_bind_parameter_count
223
+ stmt = SQLite3::Statement.new(@db, "select ?, ?, ?")
224
+ assert_equal 3, stmt.bind_parameter_count
225
+ end
226
+
227
+ def test_execute_with_varargs
228
+ stmt = @db.prepare('select ?, ?')
229
+ assert_equal [[nil, nil]], stmt.execute(nil, nil).to_a
230
+ end
231
+
232
+ def test_execute_with_hash
233
+ stmt = @db.prepare('select :n, :h')
234
+ assert_equal [[10, nil]], stmt.execute('n' => 10, 'h' => nil).to_a
235
+ end
236
+
237
+ def test_with_error
238
+ @db.execute('CREATE TABLE "employees" ("name" varchar(20) NOT NULL CONSTRAINT "index_employees_on_name" UNIQUE)')
239
+ stmt = @db.prepare("INSERT INTO Employees(name) VALUES(?)")
240
+ stmt.execute('employee-1')
241
+ stmt.execute('employee-1') rescue SQLite3::ConstraintException
242
+ stmt.reset!
243
+ assert stmt.execute('employee-2')
244
+ end
245
+
246
+ def test_clear_bindings
247
+ stmt = @db.prepare('select ?, ?')
248
+ stmt.bind_param 1, "foo"
249
+ stmt.bind_param 2, "bar"
250
+
251
+ # We can't fetch bound parameters back out of sqlite3, so just call
252
+ # the clear_bindings! method and assert that nil is returned
253
+ stmt.clear_bindings!
254
+
255
+ while x = stmt.step
256
+ assert_equal [nil, nil], x
257
+ end
258
+ end
259
+ end
260
+ 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