sqlite3 1.3.8-x64-mingw32
Sign up to get free protection for your applications and to get access to all the features.
- data/.gemtest +0 -0
- data/API_CHANGES.rdoc +50 -0
- data/CHANGELOG.rdoc +265 -0
- data/ChangeLog.cvs +88 -0
- data/Gemfile +15 -0
- data/LICENSE +27 -0
- data/Manifest.txt +52 -0
- data/README.rdoc +95 -0
- data/Rakefile +10 -0
- data/ext/sqlite3/backup.c +168 -0
- data/ext/sqlite3/backup.h +15 -0
- data/ext/sqlite3/database.c +822 -0
- data/ext/sqlite3/database.h +15 -0
- data/ext/sqlite3/exception.c +94 -0
- data/ext/sqlite3/exception.h +8 -0
- data/ext/sqlite3/extconf.rb +51 -0
- data/ext/sqlite3/sqlite3.c +40 -0
- data/ext/sqlite3/sqlite3_ruby.h +53 -0
- data/ext/sqlite3/statement.c +439 -0
- data/ext/sqlite3/statement.h +16 -0
- data/faq/faq.rb +145 -0
- data/faq/faq.yml +426 -0
- data/lib/sqlite3.rb +10 -0
- data/lib/sqlite3/2.0/sqlite3_native.so +0 -0
- data/lib/sqlite3/constants.rb +49 -0
- data/lib/sqlite3/database.rb +579 -0
- data/lib/sqlite3/errors.rb +44 -0
- data/lib/sqlite3/pragmas.rb +280 -0
- data/lib/sqlite3/resultset.rb +195 -0
- data/lib/sqlite3/statement.rb +144 -0
- data/lib/sqlite3/translator.rb +118 -0
- data/lib/sqlite3/value.rb +57 -0
- data/lib/sqlite3/version.rb +25 -0
- data/setup.rb +1333 -0
- data/tasks/faq.rake +9 -0
- data/tasks/gem.rake +37 -0
- data/tasks/native.rake +45 -0
- data/tasks/vendor_sqlite3.rake +87 -0
- data/test/helper.rb +18 -0
- data/test/test_backup.rb +33 -0
- data/test/test_collation.rb +82 -0
- data/test/test_database.rb +350 -0
- data/test/test_database_readonly.rb +29 -0
- data/test/test_deprecated.rb +44 -0
- data/test/test_encoding.rb +121 -0
- data/test/test_integration.rb +555 -0
- data/test/test_integration_open_close.rb +30 -0
- data/test/test_integration_pending.rb +115 -0
- data/test/test_integration_resultset.rb +159 -0
- data/test/test_integration_statement.rb +194 -0
- data/test/test_result_set.rb +37 -0
- data/test/test_sqlite3.rb +9 -0
- data/test/test_statement.rb +256 -0
- data/test/test_statement_execute.rb +35 -0
- metadata +246 -0
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
module SQLite3
|
4
|
+
class TestDatabaseReadonly < SQLite3::TestCase
|
5
|
+
def setup
|
6
|
+
File.unlink 'test-readonly.db' if File.exists?('test-readonly.db')
|
7
|
+
@db = SQLite3::Database.new('test-readonly.db')
|
8
|
+
@db.execute("CREATE TABLE foos (id integer)")
|
9
|
+
@db.close
|
10
|
+
end
|
11
|
+
|
12
|
+
def teardown
|
13
|
+
@db.close unless @db.closed?
|
14
|
+
File.unlink 'test-readonly.db'
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_open_readonly_database
|
18
|
+
@db = SQLite3::Database.new('test-readonly.db', :readonly => true)
|
19
|
+
assert @db.readonly?
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_insert_readonly_database
|
23
|
+
@db = SQLite3::Database.new('test-readonly.db', :readonly => true)
|
24
|
+
assert_raise(SQLite3::ReadOnlyException) do
|
25
|
+
@db.execute("INSERT INTO foos (id) VALUES (12)")
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
module SQLite3
|
4
|
+
class TestDeprecated < SQLite3::TestCase
|
5
|
+
attr_reader :db
|
6
|
+
|
7
|
+
def setup
|
8
|
+
super
|
9
|
+
@warn_before = $-w
|
10
|
+
$-w = false
|
11
|
+
@db = SQLite3::Database.new(':memory:')
|
12
|
+
@db.execute 'CREATE TABLE test_table (name text, age int)'
|
13
|
+
end
|
14
|
+
|
15
|
+
def teardown
|
16
|
+
super
|
17
|
+
$-w = @warn_before
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_query_with_many_bind_params_not_nil
|
21
|
+
assert_equal [[1, 2]], db.query('select ?, ?', 1, 2).to_a
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_execute_with_many_bind_params_not_nil
|
25
|
+
assert_equal [[1, 2]], @db.execute("select ?, ?", 1, 2).to_a
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_query_with_many_bind_params
|
29
|
+
assert_equal [[nil, 1]], @db.query("select ?, ?", nil, 1).to_a
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_query_with_nil_bind_params
|
33
|
+
assert_equal [['foo']], @db.query("select 'foo'", nil).to_a
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_execute_with_many_bind_params
|
37
|
+
assert_equal [[nil, 1]], @db.execute("select ?, ?", nil, 1)
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_execute_with_nil_bind_params
|
41
|
+
assert_equal [['foo']], @db.execute("select 'foo'", nil)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'helper'
|
4
|
+
|
5
|
+
module SQLite3
|
6
|
+
class TestEncoding < SQLite3::TestCase
|
7
|
+
def setup
|
8
|
+
@db = SQLite3::Database.new(':memory:')
|
9
|
+
@create = "create table ex(id int, data string)"
|
10
|
+
@insert = "insert into ex(id, data) values (?, ?)"
|
11
|
+
@db.execute(@create);
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_default_internal_is_honored
|
15
|
+
warn_before = $-w
|
16
|
+
$-w = false
|
17
|
+
|
18
|
+
before_enc = Encoding.default_internal
|
19
|
+
|
20
|
+
str = "壁に耳あり、障子に目あり"
|
21
|
+
stmt = @db.prepare('insert into ex(data) values (?)')
|
22
|
+
stmt.bind_param 1, str
|
23
|
+
stmt.step
|
24
|
+
|
25
|
+
Encoding.default_internal = 'EUC-JP'
|
26
|
+
string = @db.execute('select data from ex').first.first
|
27
|
+
|
28
|
+
assert_equal Encoding.default_internal, string.encoding
|
29
|
+
assert_equal str.encode('EUC-JP'), string
|
30
|
+
assert_equal str, string.encode(str.encoding)
|
31
|
+
ensure
|
32
|
+
Encoding.default_internal = before_enc
|
33
|
+
$-w = warn_before
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_blob_is_binary
|
37
|
+
str = "猫舌"
|
38
|
+
@db.execute('create table foo(data text)')
|
39
|
+
stmt = @db.prepare('insert into foo(data) values (?)')
|
40
|
+
stmt.bind_param(1, SQLite3::Blob.new(str))
|
41
|
+
stmt.step
|
42
|
+
|
43
|
+
string = @db.execute('select data from foo').first.first
|
44
|
+
assert_equal Encoding.find('ASCII-8BIT'), string.encoding
|
45
|
+
assert_equal str, string.force_encoding('UTF-8')
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_blob_is_ascii8bit
|
49
|
+
str = "猫舌"
|
50
|
+
@db.execute('create table foo(data text)')
|
51
|
+
stmt = @db.prepare('insert into foo(data) values (?)')
|
52
|
+
stmt.bind_param(1, str.dup.force_encoding("ASCII-8BIT"))
|
53
|
+
stmt.step
|
54
|
+
|
55
|
+
string = @db.execute('select data from foo').first.first
|
56
|
+
assert_equal Encoding.find('ASCII-8BIT'), string.encoding
|
57
|
+
assert_equal str, string.force_encoding('UTF-8')
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_blob_with_eucjp
|
61
|
+
str = "猫舌".encode("EUC-JP")
|
62
|
+
@db.execute('create table foo(data text)')
|
63
|
+
stmt = @db.prepare('insert into foo(data) values (?)')
|
64
|
+
stmt.bind_param(1, SQLite3::Blob.new(str))
|
65
|
+
stmt.step
|
66
|
+
|
67
|
+
string = @db.execute('select data from foo').first.first
|
68
|
+
assert_equal Encoding.find('ASCII-8BIT'), string.encoding
|
69
|
+
assert_equal str, string.force_encoding('EUC-JP')
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_db_with_eucjp
|
73
|
+
db = SQLite3::Database.new(':memory:'.encode('EUC-JP'))
|
74
|
+
assert_equal(Encoding.find('UTF-8'), db.encoding)
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_db_with_utf16
|
78
|
+
utf16 = ([1].pack("I") == [1].pack("N")) ? "UTF-16BE" : "UTF-16LE"
|
79
|
+
|
80
|
+
db = SQLite3::Database.new(':memory:'.encode(utf16))
|
81
|
+
assert_equal(Encoding.find(utf16), db.encoding)
|
82
|
+
end
|
83
|
+
|
84
|
+
def test_statement_eucjp
|
85
|
+
str = "猫舌"
|
86
|
+
@db.execute("insert into ex(data) values ('#{str}')".encode('EUC-JP'))
|
87
|
+
row = @db.execute("select data from ex")
|
88
|
+
assert_equal @db.encoding, row.first.first.encoding
|
89
|
+
assert_equal str, row.first.first
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_statement_utf8
|
93
|
+
str = "猫舌"
|
94
|
+
@db.execute("insert into ex(data) values ('#{str}')")
|
95
|
+
row = @db.execute("select data from ex")
|
96
|
+
assert_equal @db.encoding, row.first.first.encoding
|
97
|
+
assert_equal str, row.first.first
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_encoding
|
101
|
+
assert_equal Encoding.find("UTF-8"), @db.encoding
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_utf_8
|
105
|
+
str = "猫舌"
|
106
|
+
@db.execute(@insert, [10, str])
|
107
|
+
row = @db.execute("select data from ex")
|
108
|
+
assert_equal @db.encoding, row.first.first.encoding
|
109
|
+
assert_equal str, row.first.first
|
110
|
+
end
|
111
|
+
|
112
|
+
def test_euc_jp
|
113
|
+
str = "猫舌".encode('EUC-JP')
|
114
|
+
@db.execute(@insert, [10, str])
|
115
|
+
row = @db.execute("select data from ex")
|
116
|
+
assert_equal @db.encoding, row.first.first.encoding
|
117
|
+
assert_equal str.encode('UTF-8'), row.first.first
|
118
|
+
end
|
119
|
+
|
120
|
+
end if RUBY_VERSION >= '1.9.1'
|
121
|
+
end
|
@@ -0,0 +1,555 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TC_Database_Integration < 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
|
+
end
|
13
|
+
|
14
|
+
def teardown
|
15
|
+
@db.close
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_table_info_with_type_translation_active
|
19
|
+
assert_nothing_raised { @db.table_info("foo") }
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_table_info_with_defaults_for_version_3_3_8_and_higher
|
23
|
+
@db.transaction do
|
24
|
+
@db.execute "create table defaults_test ( a string default NULL, b string default 'Hello' )"
|
25
|
+
data = @db.table_info( "defaults_test" )
|
26
|
+
assert_equal({"name" => "a", "type" => "string", "dflt_value" => nil, "notnull" => 0, "cid" => 0, "pk" => 0},
|
27
|
+
data[0])
|
28
|
+
assert_equal({"name" => "b", "type" => "string", "dflt_value" => "Hello", "notnull" => 0, "cid" => 1, "pk" => 0},
|
29
|
+
data[1])
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_table_info_without_defaults_for_version_3_3_8_and_higher
|
34
|
+
@db.transaction do
|
35
|
+
@db.execute "create table no_defaults_test ( a integer default 1, b integer )"
|
36
|
+
data = @db.table_info( "no_defaults_test" )
|
37
|
+
assert_equal({"name" => "a", "type" => "integer", "dflt_value" => "1", "notnull" => 0, "cid" => 0, "pk" => 0},
|
38
|
+
data[0])
|
39
|
+
assert_equal({"name" => "b", "type" => "integer", "dflt_value" => nil, "notnull" => 0, "cid" => 1, "pk" => 0},
|
40
|
+
data[1])
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_complete_fail
|
45
|
+
assert !@db.complete?( "select * from foo" )
|
46
|
+
end
|
47
|
+
def test_complete_success
|
48
|
+
assert @db.complete?( "select * from foo;" )
|
49
|
+
end
|
50
|
+
|
51
|
+
# FIXME: do people really need UTF16 sql statements?
|
52
|
+
#def test_complete_fail_utf16
|
53
|
+
# assert !@db.complete?( "select * from foo".to_utf16(false), true )
|
54
|
+
#end
|
55
|
+
|
56
|
+
# FIXME: do people really need UTF16 sql statements?
|
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
|
+
# FIXME: do people really need UTF16 error messages?
|
66
|
+
#def test_errmsg_utf16
|
67
|
+
# msg = Iconv.conv('UTF-16', 'UTF-8', 'not an error')
|
68
|
+
# assert_equal msg, @db.errmsg(true)
|
69
|
+
#end
|
70
|
+
|
71
|
+
def test_errcode
|
72
|
+
assert_equal 0, @db.errcode
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_trace
|
76
|
+
result = nil
|
77
|
+
@db.trace { |sql| result = sql }
|
78
|
+
@db.execute "select * from foo"
|
79
|
+
assert_equal "select * from foo", result
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_authorizer_okay
|
83
|
+
@db.authorizer { |type,a,b,c,d| 0 }
|
84
|
+
rows = @db.execute "select * from foo"
|
85
|
+
assert_equal 3, rows.length
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_authorizer_error
|
89
|
+
@db.authorizer { |type,a,b,c,d| 1 }
|
90
|
+
assert_raise( SQLite3::AuthorizationException ) do
|
91
|
+
@db.execute "select * from foo"
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_authorizer_silent
|
96
|
+
@db.authorizer { |type,a,b,c,d| 2 }
|
97
|
+
rows = @db.execute "select * from foo"
|
98
|
+
assert rows.empty?
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_prepare_invalid_syntax
|
102
|
+
assert_raise( SQLite3::SQLException ) do
|
103
|
+
@db.prepare "select from foo"
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_prepare_invalid_column
|
108
|
+
assert_raise( SQLite3::SQLException ) do
|
109
|
+
@db.prepare "select k from foo"
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_prepare_invalid_table
|
114
|
+
assert_raise( SQLite3::SQLException ) do
|
115
|
+
@db.prepare "select * from barf"
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def test_prepare_no_block
|
120
|
+
stmt = @db.prepare "select * from foo"
|
121
|
+
assert stmt.respond_to?(:execute)
|
122
|
+
stmt.close
|
123
|
+
end
|
124
|
+
|
125
|
+
def test_prepare_with_block
|
126
|
+
called = false
|
127
|
+
@db.prepare "select * from foo" do |stmt|
|
128
|
+
called = true
|
129
|
+
assert stmt.respond_to?(:execute)
|
130
|
+
end
|
131
|
+
assert called
|
132
|
+
end
|
133
|
+
|
134
|
+
def test_execute_no_block_no_bind_no_match
|
135
|
+
rows = @db.execute( "select * from foo where a > 100" )
|
136
|
+
assert rows.empty?
|
137
|
+
end
|
138
|
+
|
139
|
+
def test_execute_with_block_no_bind_no_match
|
140
|
+
called = false
|
141
|
+
@db.execute( "select * from foo where a > 100" ) do |row|
|
142
|
+
called = true
|
143
|
+
end
|
144
|
+
assert !called
|
145
|
+
end
|
146
|
+
|
147
|
+
def test_execute_no_block_with_bind_no_match
|
148
|
+
rows = @db.execute( "select * from foo where a > ?", 100 )
|
149
|
+
assert rows.empty?
|
150
|
+
end
|
151
|
+
|
152
|
+
def test_execute_with_block_with_bind_no_match
|
153
|
+
called = false
|
154
|
+
@db.execute( "select * from foo where a > ?", 100 ) do |row|
|
155
|
+
called = true
|
156
|
+
end
|
157
|
+
assert !called
|
158
|
+
end
|
159
|
+
|
160
|
+
def test_execute_no_block_no_bind_with_match
|
161
|
+
rows = @db.execute( "select * from foo where a = 1" )
|
162
|
+
assert_equal 1, rows.length
|
163
|
+
end
|
164
|
+
|
165
|
+
def test_execute_with_block_no_bind_with_match
|
166
|
+
called = 0
|
167
|
+
@db.execute( "select * from foo where a = 1" ) do |row|
|
168
|
+
called += 1
|
169
|
+
end
|
170
|
+
assert_equal 1, called
|
171
|
+
end
|
172
|
+
|
173
|
+
def test_execute_no_block_with_bind_with_match
|
174
|
+
rows = @db.execute( "select * from foo where a = ?", 1 )
|
175
|
+
assert_equal 1, rows.length
|
176
|
+
end
|
177
|
+
|
178
|
+
def test_execute_with_block_with_bind_with_match
|
179
|
+
called = 0
|
180
|
+
@db.execute( "select * from foo where a = ?", 1 ) do |row|
|
181
|
+
called += 1
|
182
|
+
end
|
183
|
+
assert_equal 1, called
|
184
|
+
end
|
185
|
+
|
186
|
+
def test_execute2_no_block_no_bind_no_match
|
187
|
+
columns, *rows = @db.execute2( "select * from foo where a > 100" )
|
188
|
+
assert rows.empty?
|
189
|
+
assert_equal [ "a", "b" ], columns
|
190
|
+
end
|
191
|
+
|
192
|
+
def test_execute2_with_block_no_bind_no_match
|
193
|
+
called = 0
|
194
|
+
@db.execute2( "select * from foo where a > 100" ) do |row|
|
195
|
+
assert [ "a", "b" ], row unless called == 0
|
196
|
+
called += 1
|
197
|
+
end
|
198
|
+
assert_equal 1, called
|
199
|
+
end
|
200
|
+
|
201
|
+
def test_execute2_no_block_with_bind_no_match
|
202
|
+
columns, *rows = @db.execute2( "select * from foo where a > ?", 100 )
|
203
|
+
assert rows.empty?
|
204
|
+
assert_equal [ "a", "b" ], columns
|
205
|
+
end
|
206
|
+
|
207
|
+
def test_execute2_with_block_with_bind_no_match
|
208
|
+
called = 0
|
209
|
+
@db.execute2( "select * from foo where a > ?", 100 ) do |row|
|
210
|
+
assert_equal [ "a", "b" ], row unless called == 0
|
211
|
+
called += 1
|
212
|
+
end
|
213
|
+
assert_equal 1, called
|
214
|
+
end
|
215
|
+
|
216
|
+
def test_execute2_no_block_no_bind_with_match
|
217
|
+
columns, *rows = @db.execute2( "select * from foo where a = 1" )
|
218
|
+
assert_equal 1, rows.length
|
219
|
+
assert_equal [ "a", "b" ], columns
|
220
|
+
end
|
221
|
+
|
222
|
+
def test_execute2_with_block_no_bind_with_match
|
223
|
+
called = 0
|
224
|
+
@db.execute2( "select * from foo where a = 1" ) do |row|
|
225
|
+
assert_equal [ 1, "foo" ], row unless called == 0
|
226
|
+
called += 1
|
227
|
+
end
|
228
|
+
assert_equal 2, called
|
229
|
+
end
|
230
|
+
|
231
|
+
def test_execute2_no_block_with_bind_with_match
|
232
|
+
columns, *rows = @db.execute2( "select * from foo where a = ?", 1 )
|
233
|
+
assert_equal 1, rows.length
|
234
|
+
assert_equal [ "a", "b" ], columns
|
235
|
+
end
|
236
|
+
|
237
|
+
def test_execute2_with_block_with_bind_with_match
|
238
|
+
called = 0
|
239
|
+
@db.execute2( "select * from foo where a = ?", 1 ) do |row|
|
240
|
+
called += 1
|
241
|
+
end
|
242
|
+
assert_equal 2, called
|
243
|
+
end
|
244
|
+
|
245
|
+
def test_execute_batch_empty
|
246
|
+
assert_nothing_raised { @db.execute_batch "" }
|
247
|
+
end
|
248
|
+
|
249
|
+
def test_execute_batch_no_bind
|
250
|
+
@db.transaction do
|
251
|
+
@db.execute_batch <<-SQL
|
252
|
+
create table bar ( a, b, c );
|
253
|
+
insert into bar values ( 'one', 2, 'three' );
|
254
|
+
insert into bar values ( 'four', 5, 'six' );
|
255
|
+
insert into bar values ( 'seven', 8, 'nine' );
|
256
|
+
SQL
|
257
|
+
end
|
258
|
+
rows = @db.execute( "select * from bar" )
|
259
|
+
assert_equal 3, rows.length
|
260
|
+
end
|
261
|
+
|
262
|
+
def test_execute_batch_with_bind
|
263
|
+
@db.execute_batch( <<-SQL, [1] )
|
264
|
+
create table bar ( a, b, c );
|
265
|
+
insert into bar values ( 'one', 2, ? );
|
266
|
+
insert into bar values ( 'four', 5, ? );
|
267
|
+
insert into bar values ( 'seven', 8, ? );
|
268
|
+
SQL
|
269
|
+
rows = @db.execute( "select * from bar" ).map { |a,b,c| c }
|
270
|
+
assert_equal [1, 1, 1], rows
|
271
|
+
end
|
272
|
+
|
273
|
+
def test_query_no_block_no_bind_no_match
|
274
|
+
result = @db.query( "select * from foo where a > 100" )
|
275
|
+
assert_nil result.next
|
276
|
+
result.close
|
277
|
+
end
|
278
|
+
|
279
|
+
def test_query_with_block_no_bind_no_match
|
280
|
+
r = nil
|
281
|
+
@db.query( "select * from foo where a > 100" ) do |result|
|
282
|
+
assert_nil result.next
|
283
|
+
r = result
|
284
|
+
end
|
285
|
+
assert r.closed?
|
286
|
+
end
|
287
|
+
|
288
|
+
def test_query_no_block_with_bind_no_match
|
289
|
+
result = @db.query( "select * from foo where a > ?", 100 )
|
290
|
+
assert_nil result.next
|
291
|
+
result.close
|
292
|
+
end
|
293
|
+
|
294
|
+
def test_query_with_block_with_bind_no_match
|
295
|
+
r = nil
|
296
|
+
@db.query( "select * from foo where a > ?", 100 ) do |result|
|
297
|
+
assert_nil result.next
|
298
|
+
r = result
|
299
|
+
end
|
300
|
+
assert r.closed?
|
301
|
+
end
|
302
|
+
|
303
|
+
def test_query_no_block_no_bind_with_match
|
304
|
+
result = @db.query( "select * from foo where a = 1" )
|
305
|
+
assert_not_nil result.next
|
306
|
+
assert_nil result.next
|
307
|
+
result.close
|
308
|
+
end
|
309
|
+
|
310
|
+
def test_query_with_block_no_bind_with_match
|
311
|
+
r = nil
|
312
|
+
@db.query( "select * from foo where a = 1" ) do |result|
|
313
|
+
assert_not_nil result.next
|
314
|
+
assert_nil result.next
|
315
|
+
r = result
|
316
|
+
end
|
317
|
+
assert r.closed?
|
318
|
+
end
|
319
|
+
|
320
|
+
def test_query_no_block_with_bind_with_match
|
321
|
+
result = @db.query( "select * from foo where a = ?", 1 )
|
322
|
+
assert_not_nil result.next
|
323
|
+
assert_nil result.next
|
324
|
+
result.close
|
325
|
+
end
|
326
|
+
|
327
|
+
def test_query_with_block_with_bind_with_match
|
328
|
+
r = nil
|
329
|
+
@db.query( "select * from foo where a = ?", 1 ) do |result|
|
330
|
+
assert_not_nil result.next
|
331
|
+
assert_nil result.next
|
332
|
+
r = result
|
333
|
+
end
|
334
|
+
assert r.closed?
|
335
|
+
end
|
336
|
+
|
337
|
+
def test_get_first_row_no_bind_no_match
|
338
|
+
result = @db.get_first_row( "select * from foo where a=100" )
|
339
|
+
assert_nil result
|
340
|
+
end
|
341
|
+
|
342
|
+
def test_get_first_row_no_bind_with_match
|
343
|
+
result = @db.get_first_row( "select * from foo where a=1" )
|
344
|
+
assert_equal [ 1, "foo" ], result
|
345
|
+
end
|
346
|
+
|
347
|
+
def test_get_first_row_with_bind_no_match
|
348
|
+
result = @db.get_first_row( "select * from foo where a=?", 100 )
|
349
|
+
assert_nil result
|
350
|
+
end
|
351
|
+
|
352
|
+
def test_get_first_row_with_bind_with_match
|
353
|
+
result = @db.get_first_row( "select * from foo where a=?", 1 )
|
354
|
+
assert_equal [ 1, "foo" ], result
|
355
|
+
end
|
356
|
+
|
357
|
+
def test_get_first_value_no_bind_no_match
|
358
|
+
result = @db.get_first_value( "select b, a from foo where a=100" )
|
359
|
+
assert_nil result
|
360
|
+
end
|
361
|
+
|
362
|
+
def test_get_first_value_no_bind_with_match
|
363
|
+
result = @db.get_first_value( "select b, a from foo where a=1" )
|
364
|
+
assert_equal "foo", result
|
365
|
+
end
|
366
|
+
|
367
|
+
def test_get_first_value_with_bind_no_match
|
368
|
+
result = @db.get_first_value( "select b, a from foo where a=?", 100 )
|
369
|
+
assert_nil result
|
370
|
+
end
|
371
|
+
|
372
|
+
def test_get_first_value_with_bind_with_match
|
373
|
+
result = @db.get_first_value( "select b, a from foo where a=?", 1 )
|
374
|
+
assert_equal "foo", result
|
375
|
+
end
|
376
|
+
|
377
|
+
def test_last_insert_row_id
|
378
|
+
@db.execute "insert into foo ( b ) values ( 'test' )"
|
379
|
+
assert_equal 4, @db.last_insert_row_id
|
380
|
+
@db.execute "insert into foo ( b ) values ( 'again' )"
|
381
|
+
assert_equal 5, @db.last_insert_row_id
|
382
|
+
end
|
383
|
+
|
384
|
+
def test_changes
|
385
|
+
@db.execute "insert into foo ( b ) values ( 'test' )"
|
386
|
+
assert_equal 1, @db.changes
|
387
|
+
@db.execute "delete from foo where 1=1"
|
388
|
+
assert_equal 4, @db.changes
|
389
|
+
end
|
390
|
+
|
391
|
+
def test_total_changes
|
392
|
+
assert_equal 3, @db.total_changes
|
393
|
+
@db.execute "insert into foo ( b ) values ( 'test' )"
|
394
|
+
@db.execute "delete from foo where 1=1"
|
395
|
+
assert_equal 8, @db.total_changes
|
396
|
+
end
|
397
|
+
|
398
|
+
def test_transaction_nest
|
399
|
+
assert_raise( SQLite3::SQLException ) do
|
400
|
+
@db.transaction do
|
401
|
+
@db.transaction do
|
402
|
+
end
|
403
|
+
end
|
404
|
+
end
|
405
|
+
end
|
406
|
+
|
407
|
+
def test_transaction_rollback
|
408
|
+
@db.transaction
|
409
|
+
@db.execute_batch <<-SQL
|
410
|
+
insert into foo (b) values ( 'test1' );
|
411
|
+
insert into foo (b) values ( 'test2' );
|
412
|
+
insert into foo (b) values ( 'test3' );
|
413
|
+
insert into foo (b) values ( 'test4' );
|
414
|
+
SQL
|
415
|
+
assert_equal 7, @db.get_first_value("select count(*) from foo").to_i
|
416
|
+
@db.rollback
|
417
|
+
assert_equal 3, @db.get_first_value("select count(*) from foo").to_i
|
418
|
+
end
|
419
|
+
|
420
|
+
def test_transaction_commit
|
421
|
+
@db.transaction
|
422
|
+
@db.execute_batch <<-SQL
|
423
|
+
insert into foo (b) values ( 'test1' );
|
424
|
+
insert into foo (b) values ( 'test2' );
|
425
|
+
insert into foo (b) values ( 'test3' );
|
426
|
+
insert into foo (b) values ( 'test4' );
|
427
|
+
SQL
|
428
|
+
assert_equal 7, @db.get_first_value("select count(*) from foo").to_i
|
429
|
+
@db.commit
|
430
|
+
assert_equal 7, @db.get_first_value("select count(*) from foo").to_i
|
431
|
+
end
|
432
|
+
|
433
|
+
def test_transaction_rollback_in_block
|
434
|
+
assert_raise( SQLite3::SQLException ) do
|
435
|
+
@db.transaction do
|
436
|
+
@db.rollback
|
437
|
+
end
|
438
|
+
end
|
439
|
+
end
|
440
|
+
|
441
|
+
def test_transaction_commit_in_block
|
442
|
+
assert_raise( SQLite3::SQLException ) do
|
443
|
+
@db.transaction do
|
444
|
+
@db.commit
|
445
|
+
end
|
446
|
+
end
|
447
|
+
end
|
448
|
+
|
449
|
+
def test_transaction_active
|
450
|
+
assert !@db.transaction_active?
|
451
|
+
@db.transaction
|
452
|
+
assert @db.transaction_active?
|
453
|
+
@db.commit
|
454
|
+
assert !@db.transaction_active?
|
455
|
+
end
|
456
|
+
|
457
|
+
def test_transaction_implicit_rollback
|
458
|
+
assert !@db.transaction_active?
|
459
|
+
@db.transaction
|
460
|
+
@db.execute('create table bar (x CHECK(1 = 0))')
|
461
|
+
assert @db.transaction_active?
|
462
|
+
assert_raises( SQLite3::ConstraintException ) do
|
463
|
+
@db.execute("insert or rollback into bar (x) VALUES ('x')")
|
464
|
+
end
|
465
|
+
assert !@db.transaction_active?
|
466
|
+
end
|
467
|
+
|
468
|
+
def test_interrupt
|
469
|
+
@db.create_function( "abort", 1 ) do |func,x|
|
470
|
+
@db.interrupt
|
471
|
+
func.result = x
|
472
|
+
end
|
473
|
+
|
474
|
+
assert_raise( SQLite3::InterruptException ) do
|
475
|
+
@db.execute "select abort(a) from foo"
|
476
|
+
end
|
477
|
+
end
|
478
|
+
|
479
|
+
def test_create_function
|
480
|
+
@db.create_function( "munge", 1 ) do |func,x|
|
481
|
+
func.result = ">>>#{x}<<<"
|
482
|
+
end
|
483
|
+
|
484
|
+
value = @db.get_first_value( "select munge(b) from foo where a=1" )
|
485
|
+
assert_match( />>>.*<<</, value )
|
486
|
+
end
|
487
|
+
|
488
|
+
def test_create_aggregate_without_block
|
489
|
+
step = proc do |ctx,a|
|
490
|
+
ctx[:sum] ||= 0
|
491
|
+
ctx[:sum] += a.to_i
|
492
|
+
end
|
493
|
+
|
494
|
+
final = proc { |ctx| ctx.result = ctx[:sum] }
|
495
|
+
|
496
|
+
@db.create_aggregate( "accumulate", 1, step, final )
|
497
|
+
|
498
|
+
value = @db.get_first_value( "select accumulate(a) from foo" )
|
499
|
+
assert_equal 6, value
|
500
|
+
end
|
501
|
+
|
502
|
+
def test_create_aggregate_with_block
|
503
|
+
@db.create_aggregate( "accumulate", 1 ) do
|
504
|
+
step do |ctx,a|
|
505
|
+
ctx[:sum] ||= 0
|
506
|
+
ctx[:sum] += a.to_i
|
507
|
+
end
|
508
|
+
|
509
|
+
finalize { |ctx| ctx.result = ctx[:sum] }
|
510
|
+
end
|
511
|
+
|
512
|
+
value = @db.get_first_value( "select accumulate(a) from foo" )
|
513
|
+
assert_equal 6, value
|
514
|
+
end
|
515
|
+
|
516
|
+
def test_create_aggregate_with_no_data
|
517
|
+
@db.create_aggregate( "accumulate", 1 ) do
|
518
|
+
step do |ctx,a|
|
519
|
+
ctx[:sum] ||= 0
|
520
|
+
ctx[:sum] += a.to_i
|
521
|
+
end
|
522
|
+
|
523
|
+
finalize { |ctx| ctx.result = ctx[:sum] || 0 }
|
524
|
+
end
|
525
|
+
|
526
|
+
value = @db.get_first_value(
|
527
|
+
"select accumulate(a) from foo where a = 100" )
|
528
|
+
assert_equal 0, value
|
529
|
+
end
|
530
|
+
|
531
|
+
def test_create_aggregate_handler
|
532
|
+
handler = Class.new do
|
533
|
+
class << self
|
534
|
+
def arity; 1; end
|
535
|
+
def text_rep; SQLite3::Constants::TextRep::ANY; end
|
536
|
+
def name; "multiply"; end
|
537
|
+
end
|
538
|
+
def step(ctx, a)
|
539
|
+
ctx[:buffer] ||= 1
|
540
|
+
ctx[:buffer] *= a.to_i
|
541
|
+
end
|
542
|
+
def finalize(ctx); ctx.result = ctx[:buffer]; end
|
543
|
+
end
|
544
|
+
|
545
|
+
@db.create_aggregate_handler( handler )
|
546
|
+
value = @db.get_first_value( "select multiply(a) from foo" )
|
547
|
+
assert_equal 6, value
|
548
|
+
end
|
549
|
+
|
550
|
+
def test_bind_array_parameter
|
551
|
+
result = @db.get_first_value( "select b from foo where a=? and b=?",
|
552
|
+
[ 1, "foo" ] )
|
553
|
+
assert_equal "foo", result
|
554
|
+
end
|
555
|
+
end
|