dawanda-sqlite3 1.3.3
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 +184 -0
- data/ChangeLog.cvs +88 -0
- data/LICENSE +27 -0
- data/Manifest.txt +50 -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 +762 -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 +41 -0
- data/ext/sqlite3/sqlite3.c +40 -0
- data/ext/sqlite3/sqlite3_ruby.h +44 -0
- data/ext/sqlite3/statement.c +418 -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/constants.rb +49 -0
- data/lib/sqlite3/database.rb +587 -0
- data/lib/sqlite3/errors.rb +44 -0
- data/lib/sqlite3/pragmas.rb +280 -0
- data/lib/sqlite3/resultset.rb +126 -0
- data/lib/sqlite3/statement.rb +148 -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 +31 -0
- data/tasks/native.rake +61 -0
- data/tasks/vendor_sqlite3.rake +104 -0
- data/test/helper.rb +3 -0
- data/test/test_backup.rb +33 -0
- data/test/test_collation.rb +82 -0
- data/test/test_database.rb +319 -0
- data/test/test_database_readonly.rb +29 -0
- data/test/test_deprecated.rb +37 -0
- data/test/test_encoding.rb +119 -0
- data/test/test_integration.rb +544 -0
- data/test/test_integration_open_close.rb +30 -0
- data/test/test_integration_pending.rb +115 -0
- data/test/test_integration_resultset.rb +156 -0
- data/test/test_integration_statement.rb +194 -0
- data/test/test_sqlite3.rb +9 -0
- data/test/test_statement.rb +213 -0
- data/test/test_statement_execute.rb +35 -0
- metadata +184 -0
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
module SQLite3
|
4
|
+
class TestDatabaseReadonly < Test::Unit::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,37 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
module SQLite3
|
4
|
+
class TestDeprecated < Test::Unit::TestCase
|
5
|
+
def setup
|
6
|
+
super
|
7
|
+
@warn_before = $-w
|
8
|
+
$-w = false
|
9
|
+
@db = SQLite3::Database.new(':memory:')
|
10
|
+
end
|
11
|
+
|
12
|
+
def teardown
|
13
|
+
super
|
14
|
+
$-w = @warn_before
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_execute_with_many_bind_params_not_nil
|
18
|
+
assert_equal [[1, 2]], @db.execute("select ?, ?", 1, 2).to_a
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_query_with_many_bind_params
|
22
|
+
assert_equal [[nil, 1]], @db.query("select ?, ?", nil, 1).to_a
|
23
|
+
end
|
24
|
+
|
25
|
+
def test_query_with_nil_bind_params
|
26
|
+
assert_equal [['foo']], @db.query("select 'foo'", nil).to_a
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_execute_with_many_bind_params
|
30
|
+
assert_equal [[nil, 1]], @db.execute("select ?, ?", nil, 1)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_execute_with_nil_bind_params
|
34
|
+
assert_equal [['foo']], @db.execute("select 'foo'", nil)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,119 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
|
3
|
+
require 'helper'
|
4
|
+
|
5
|
+
module SQLite3
|
6
|
+
class TestEncoding < Test::Unit::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
|
+
db = SQLite3::Database.new(':memory:'.encode('UTF-16LE'))
|
79
|
+
assert_equal(Encoding.find('UTF-16LE'), db.encoding)
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_statement_eucjp
|
83
|
+
str = "猫舌"
|
84
|
+
@db.execute("insert into ex(data) values ('#{str}')".encode('EUC-JP'))
|
85
|
+
row = @db.execute("select data from ex")
|
86
|
+
assert_equal @db.encoding, row.first.first.encoding
|
87
|
+
assert_equal str, row.first.first
|
88
|
+
end
|
89
|
+
|
90
|
+
def test_statement_utf8
|
91
|
+
str = "猫舌"
|
92
|
+
@db.execute("insert into ex(data) values ('#{str}')")
|
93
|
+
row = @db.execute("select data from ex")
|
94
|
+
assert_equal @db.encoding, row.first.first.encoding
|
95
|
+
assert_equal str, row.first.first
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_encoding
|
99
|
+
assert_equal Encoding.find("UTF-8"), @db.encoding
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_utf_8
|
103
|
+
str = "猫舌"
|
104
|
+
@db.execute(@insert, [10, str])
|
105
|
+
row = @db.execute("select data from ex")
|
106
|
+
assert_equal @db.encoding, row.first.first.encoding
|
107
|
+
assert_equal str, row.first.first
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_euc_jp
|
111
|
+
str = "猫舌".encode('EUC-JP')
|
112
|
+
@db.execute(@insert, [10, str])
|
113
|
+
row = @db.execute("select data from ex")
|
114
|
+
assert_equal @db.encoding, row.first.first.encoding
|
115
|
+
assert_equal str.encode('UTF-8'), row.first.first
|
116
|
+
end
|
117
|
+
|
118
|
+
end if RUBY_VERSION >= '1.9.1'
|
119
|
+
end
|
@@ -0,0 +1,544 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TC_Database_Integration < 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
|
+
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_interrupt
|
458
|
+
@db.create_function( "abort", 1 ) do |func,x|
|
459
|
+
@db.interrupt
|
460
|
+
func.result = x
|
461
|
+
end
|
462
|
+
|
463
|
+
assert_raise( SQLite3::InterruptException ) do
|
464
|
+
@db.execute "select abort(a) from foo"
|
465
|
+
end
|
466
|
+
end
|
467
|
+
|
468
|
+
def test_create_function
|
469
|
+
@db.create_function( "munge", 1 ) do |func,x|
|
470
|
+
func.result = ">>>#{x}<<<"
|
471
|
+
end
|
472
|
+
|
473
|
+
value = @db.get_first_value( "select munge(b) from foo where a=1" )
|
474
|
+
assert_match( />>>.*<<</, value )
|
475
|
+
end
|
476
|
+
|
477
|
+
def test_create_aggregate_without_block
|
478
|
+
step = proc do |ctx,a|
|
479
|
+
ctx[:sum] ||= 0
|
480
|
+
ctx[:sum] += a.to_i
|
481
|
+
end
|
482
|
+
|
483
|
+
final = proc { |ctx| ctx.result = ctx[:sum] }
|
484
|
+
|
485
|
+
@db.create_aggregate( "accumulate", 1, step, final )
|
486
|
+
|
487
|
+
value = @db.get_first_value( "select accumulate(a) from foo" )
|
488
|
+
assert_equal 6, value
|
489
|
+
end
|
490
|
+
|
491
|
+
def test_create_aggregate_with_block
|
492
|
+
@db.create_aggregate( "accumulate", 1 ) do
|
493
|
+
step do |ctx,a|
|
494
|
+
ctx[:sum] ||= 0
|
495
|
+
ctx[:sum] += a.to_i
|
496
|
+
end
|
497
|
+
|
498
|
+
finalize { |ctx| ctx.result = ctx[:sum] }
|
499
|
+
end
|
500
|
+
|
501
|
+
value = @db.get_first_value( "select accumulate(a) from foo" )
|
502
|
+
assert_equal 6, value
|
503
|
+
end
|
504
|
+
|
505
|
+
def test_create_aggregate_with_no_data
|
506
|
+
@db.create_aggregate( "accumulate", 1 ) do
|
507
|
+
step do |ctx,a|
|
508
|
+
ctx[:sum] ||= 0
|
509
|
+
ctx[:sum] += a.to_i
|
510
|
+
end
|
511
|
+
|
512
|
+
finalize { |ctx| ctx.result = ctx[:sum] || 0 }
|
513
|
+
end
|
514
|
+
|
515
|
+
value = @db.get_first_value(
|
516
|
+
"select accumulate(a) from foo where a = 100" )
|
517
|
+
assert_equal 0, value
|
518
|
+
end
|
519
|
+
|
520
|
+
def test_create_aggregate_handler
|
521
|
+
handler = Class.new do
|
522
|
+
class << self
|
523
|
+
def arity; 1; end
|
524
|
+
def text_rep; SQLite3::Constants::TextRep::ANY; end
|
525
|
+
def name; "multiply"; end
|
526
|
+
end
|
527
|
+
def step(ctx, a)
|
528
|
+
ctx[:buffer] ||= 1
|
529
|
+
ctx[:buffer] *= a.to_i
|
530
|
+
end
|
531
|
+
def finalize(ctx); ctx.result = ctx[:buffer]; end
|
532
|
+
end
|
533
|
+
|
534
|
+
@db.create_aggregate_handler( handler )
|
535
|
+
value = @db.get_first_value( "select multiply(a) from foo" )
|
536
|
+
assert_equal 6, value
|
537
|
+
end
|
538
|
+
|
539
|
+
def test_bind_array_parameter
|
540
|
+
result = @db.get_first_value( "select b from foo where a=? and b=?",
|
541
|
+
[ 1, "foo" ] )
|
542
|
+
assert_equal "foo", result
|
543
|
+
end
|
544
|
+
end
|