dbd-sqlite3 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/ChangeLog +3694 -0
- data/LICENSE +25 -0
- data/README +271 -0
- data/lib/dbd/SQLite3.rb +122 -0
- data/lib/dbd/sqlite3/database.rb +192 -0
- data/lib/dbd/sqlite3/statement.rb +72 -0
- data/test/DBD_TESTS +48 -0
- data/test/dbd/general/test_database.rb +157 -0
- data/test/dbd/general/test_statement.rb +240 -0
- data/test/dbd/general/test_types.rb +253 -0
- data/test/dbd/sqlite3/base.rb +33 -0
- data/test/dbd/sqlite3/test_database.rb +26 -0
- data/test/dbd/sqlite3/test_driver.rb +67 -0
- data/test/dbd/sqlite3/test_statement.rb +57 -0
- data/test/dbd/sqlite3/up.sql +23 -0
- data/test/ts_dbd.rb +118 -0
- metadata +89 -0
@@ -0,0 +1,253 @@
|
|
1
|
+
@class = Class.new(DBDConfig.testbase(DBDConfig.current_dbtype)) do
|
2
|
+
def skip_bit
|
3
|
+
# FIXME this test fails because DBI's type system blows goats.
|
4
|
+
@sth = nil
|
5
|
+
|
6
|
+
assert_nothing_raised do
|
7
|
+
@sth = @dbh.prepare("insert into bit_test (mybit) values (?)")
|
8
|
+
@sth.bind_param(1, 0, DBI::SQL_TINYINT)
|
9
|
+
@sth.execute
|
10
|
+
# if dbtype == "postgresql"
|
11
|
+
# @sth.execute("0")
|
12
|
+
# else
|
13
|
+
# @sth.execute(0)
|
14
|
+
# end
|
15
|
+
@sth.finish
|
16
|
+
end
|
17
|
+
|
18
|
+
assert_nothing_raised do
|
19
|
+
@sth = @dbh.prepare("select * from bit_test")
|
20
|
+
@sth.execute
|
21
|
+
row = @sth.fetch
|
22
|
+
@sth.finish
|
23
|
+
|
24
|
+
assert_equal [0], row
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
# FIXME
|
29
|
+
# Ideally, this test should be split across the DBI tests and DBD, but for
|
30
|
+
# now testing against the DBDs really doesn't cost us anything other than
|
31
|
+
# debugging time if something breaks.
|
32
|
+
def test_bind_coltype
|
33
|
+
# ensure type conv didn't get turned off somewhere.
|
34
|
+
assert(DBI.convert_types)
|
35
|
+
assert(@dbh.convert_types)
|
36
|
+
|
37
|
+
assert_nothing_raised do
|
38
|
+
@sth = @dbh.prepare("select name, age from names order by age")
|
39
|
+
assert(@sth.convert_types) # again
|
40
|
+
@sth.execute
|
41
|
+
@sth.bind_coltype(2, DBI::Type::Varchar)
|
42
|
+
assert_equal(
|
43
|
+
[
|
44
|
+
["Joe", "19"],
|
45
|
+
["Bob", "21"],
|
46
|
+
["Jim", "30"],
|
47
|
+
], @sth.fetch_all
|
48
|
+
)
|
49
|
+
@sth.finish
|
50
|
+
end
|
51
|
+
|
52
|
+
# just to be sure..
|
53
|
+
assert_nothing_raised do
|
54
|
+
@sth = @dbh.prepare("select name, age from names order by age")
|
55
|
+
@sth.execute
|
56
|
+
@sth.bind_coltype(2, DBI::Type::Float)
|
57
|
+
@sth.fetch_all.collect { |x| assert_kind_of(Float, x[1]) }
|
58
|
+
@sth.finish
|
59
|
+
end
|
60
|
+
|
61
|
+
# now, let's check some failure cases
|
62
|
+
@sth = @dbh.prepare("select name, age from names order by age")
|
63
|
+
|
64
|
+
# can't bind_coltype before execute
|
65
|
+
assert_raise(DBI::InterfaceError) { @sth.bind_coltype(1, DBI::Type::Float) }
|
66
|
+
# can't index < 1
|
67
|
+
assert_raise(DBI::InterfaceError) { @sth.bind_coltype(0, DBI::Type::Float) }
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_noconv
|
71
|
+
# XXX this test will fail the whole test suite miserably if it fails at any point.
|
72
|
+
assert(DBI.convert_types)
|
73
|
+
|
74
|
+
DBI.convert_types = false
|
75
|
+
@sth.finish rescue nil
|
76
|
+
@dbh.disconnect
|
77
|
+
set_base_dbh
|
78
|
+
|
79
|
+
assert(!@dbh.convert_types)
|
80
|
+
|
81
|
+
assert_nothing_raised do
|
82
|
+
@sth = @dbh.prepare("select * from names order by age")
|
83
|
+
assert(!@sth.convert_types)
|
84
|
+
@sth.execute
|
85
|
+
assert_equal(
|
86
|
+
[
|
87
|
+
["Joe", "19"],
|
88
|
+
["Bob", "21"],
|
89
|
+
["Jim", "30"],
|
90
|
+
], @sth.fetch_all
|
91
|
+
)
|
92
|
+
@sth.finish
|
93
|
+
end
|
94
|
+
|
95
|
+
DBI.convert_types = true
|
96
|
+
@sth.finish rescue nil
|
97
|
+
@dbh.disconnect
|
98
|
+
set_base_dbh
|
99
|
+
|
100
|
+
assert(DBI.convert_types)
|
101
|
+
assert(@dbh.convert_types)
|
102
|
+
|
103
|
+
assert_nothing_raised do
|
104
|
+
@sth = @dbh.prepare("select * from names order by age")
|
105
|
+
assert(@sth.convert_types)
|
106
|
+
@sth.execute
|
107
|
+
assert_equal(
|
108
|
+
[
|
109
|
+
["Joe", 19],
|
110
|
+
["Bob", 21],
|
111
|
+
["Jim", 30],
|
112
|
+
], @sth.fetch_all
|
113
|
+
)
|
114
|
+
@sth.finish
|
115
|
+
end
|
116
|
+
|
117
|
+
@dbh.convert_types = false
|
118
|
+
|
119
|
+
assert_nothing_raised do
|
120
|
+
@sth = @dbh.prepare("select * from names order by age")
|
121
|
+
assert(!@sth.convert_types)
|
122
|
+
@sth.execute
|
123
|
+
assert_equal(
|
124
|
+
[
|
125
|
+
["Joe", "19"],
|
126
|
+
["Bob", "21"],
|
127
|
+
["Jim", "30"],
|
128
|
+
], @sth.fetch_all
|
129
|
+
)
|
130
|
+
@sth.finish
|
131
|
+
end
|
132
|
+
|
133
|
+
@dbh.convert_types = true
|
134
|
+
|
135
|
+
assert_nothing_raised do
|
136
|
+
@sth = @dbh.prepare("select * from names order by age")
|
137
|
+
assert(@sth.convert_types)
|
138
|
+
@sth.convert_types = false
|
139
|
+
@sth.execute
|
140
|
+
assert_equal(
|
141
|
+
[
|
142
|
+
["Joe", "19"],
|
143
|
+
["Bob", "21"],
|
144
|
+
["Jim", "30"],
|
145
|
+
], @sth.fetch_all
|
146
|
+
)
|
147
|
+
@sth.finish
|
148
|
+
end
|
149
|
+
rescue Exception => e
|
150
|
+
DBI.convert_types = true
|
151
|
+
@sth.finish
|
152
|
+
@dbh.disconnect
|
153
|
+
set_base_dbh
|
154
|
+
raise e
|
155
|
+
end
|
156
|
+
|
157
|
+
def test_null
|
158
|
+
assert_nothing_raised do
|
159
|
+
@sth = @dbh.prepare('insert into names (name, age) values (?, ?)')
|
160
|
+
@sth.execute("'NULL'", 201)
|
161
|
+
@sth.execute(nil, 202)
|
162
|
+
@sth.execute("NULL", 203)
|
163
|
+
@sth.finish
|
164
|
+
end
|
165
|
+
|
166
|
+
assert_nothing_raised do
|
167
|
+
@sth = @dbh.prepare('select * from names where age > 200 order by age')
|
168
|
+
@sth.execute
|
169
|
+
assert_equal(["'NULL'", 201], @sth.fetch)
|
170
|
+
assert_equal([nil, 202], @sth.fetch)
|
171
|
+
assert_equal(["NULL", 203], @sth.fetch)
|
172
|
+
@sth.finish
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
def test_time
|
177
|
+
@sth = nil
|
178
|
+
t = nil
|
179
|
+
assert_nothing_raised do
|
180
|
+
@sth = @dbh.prepare("insert into time_test (mytime) values (?)")
|
181
|
+
t = Time.now
|
182
|
+
@sth.execute(t)
|
183
|
+
@sth.finish
|
184
|
+
end
|
185
|
+
|
186
|
+
assert_nothing_raised do
|
187
|
+
@sth = @dbh.prepare("select * from time_test")
|
188
|
+
@sth.execute
|
189
|
+
row = @sth.fetch
|
190
|
+
assert_kind_of DateTime, row[0]
|
191
|
+
assert_equal t.hour, row[0].hour
|
192
|
+
assert_equal t.min, row[0].min
|
193
|
+
assert_equal t.sec, row[0].sec
|
194
|
+
@sth.finish
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
def test_timestamp
|
199
|
+
@sth = nil
|
200
|
+
# We omit fractional second testing here -- timestamp precision
|
201
|
+
# is a very slippery, dependent on driver and driver version.
|
202
|
+
t = DBI::Timestamp.new(2008, 3, 8, 10, 39, 1)
|
203
|
+
assert_nothing_raised do
|
204
|
+
@sth = @dbh.prepare("insert into timestamp_test (mytimestamp) values (?)")
|
205
|
+
@sth.execute(t)
|
206
|
+
@sth.finish
|
207
|
+
end
|
208
|
+
|
209
|
+
assert_nothing_raised do
|
210
|
+
@sth = @dbh.prepare("select * from timestamp_test")
|
211
|
+
@sth.execute
|
212
|
+
row = @sth.fetch
|
213
|
+
assert_kind_of DateTime, row[0]
|
214
|
+
assert_equal t.year, row[0].year
|
215
|
+
assert_equal t.month, row[0].month
|
216
|
+
assert_equal t.day, row[0].day
|
217
|
+
assert_equal t.hour, row[0].hour
|
218
|
+
assert_equal t.min, row[0].min
|
219
|
+
assert_equal t.sec, row[0].sec
|
220
|
+
# omit fractional tests
|
221
|
+
@sth.finish
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
def test_boolean_return
|
226
|
+
@sth = nil
|
227
|
+
|
228
|
+
unless dbtype == "odbc" # ODBC has no boolean type
|
229
|
+
assert_nothing_raised do
|
230
|
+
@sth = @dbh.prepare("insert into boolean_test (num, mybool) values (?, ?)")
|
231
|
+
@sth.execute(1, true)
|
232
|
+
@sth.execute(2, false)
|
233
|
+
@sth.finish
|
234
|
+
end
|
235
|
+
|
236
|
+
assert_nothing_raised do
|
237
|
+
@sth = @dbh.prepare("select * from boolean_test order by num")
|
238
|
+
@sth.execute
|
239
|
+
|
240
|
+
pairs = @sth.fetch_all
|
241
|
+
|
242
|
+
assert_equal(
|
243
|
+
[
|
244
|
+
[1, true],
|
245
|
+
[2, false],
|
246
|
+
], pairs
|
247
|
+
)
|
248
|
+
|
249
|
+
@sth.finish
|
250
|
+
end
|
251
|
+
end
|
252
|
+
end
|
253
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'fileutils'
|
3
|
+
|
4
|
+
DBDConfig.set_testbase(:sqlite3, Class.new(Test::Unit::TestCase) do
|
5
|
+
def dbtype
|
6
|
+
"sqlite3"
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_base
|
10
|
+
if @dbh # FIXME for some reason, @dbh isn't initialized in some cases. investigate.
|
11
|
+
assert_equal(@dbh.driver_name, "SQLite3")
|
12
|
+
assert_kind_of(DBI::DBD::SQLite3::Database, @dbh.instance_variable_get(:@handle))
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def set_base_dbh
|
17
|
+
config = DBDConfig.get_config['sqlite3']
|
18
|
+
@dbh = DBI.connect('dbi:SQLite3:'+config['dbname'], nil, nil, { })
|
19
|
+
end
|
20
|
+
|
21
|
+
def setup
|
22
|
+
set_base_dbh
|
23
|
+
DBDConfig.inject_sql(@dbh, dbtype, "dbd/sqlite3/up.sql")
|
24
|
+
end
|
25
|
+
|
26
|
+
def teardown
|
27
|
+
@sth.finish if(@sth && !@sth.finished?)
|
28
|
+
@dbh.disconnect if @dbh.connected?
|
29
|
+
config = DBDConfig.get_config['sqlite3']
|
30
|
+
FileUtils.rm_f(config['dbname'])
|
31
|
+
end
|
32
|
+
end
|
33
|
+
)
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class TestDatabase < DBDConfig.testbase(:sqlite3)
|
2
|
+
def test_disconnect
|
3
|
+
assert_nil @dbh.disconnect
|
4
|
+
assert_nil @dbh.instance_variable_get("@db")
|
5
|
+
end
|
6
|
+
|
7
|
+
def test_columns
|
8
|
+
assert_equal [
|
9
|
+
{
|
10
|
+
:name => "name",
|
11
|
+
:default => nil,
|
12
|
+
:nullable => true,
|
13
|
+
:sql_type => 100,
|
14
|
+
:precision => 255,
|
15
|
+
:type_name => "varchar"
|
16
|
+
},
|
17
|
+
{
|
18
|
+
:name => "age",
|
19
|
+
:default => nil,
|
20
|
+
:nullable => true,
|
21
|
+
:sql_type => 4,
|
22
|
+
:type_name => "integer"
|
23
|
+
}
|
24
|
+
], @dbh.columns("names")
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
class TestDriver < DBDConfig.testbase(:sqlite3)
|
2
|
+
def test_require
|
3
|
+
require 'dbd/SQLite3'
|
4
|
+
assert_kind_of Module, DBI
|
5
|
+
assert_kind_of Module, DBI::DBD
|
6
|
+
assert_kind_of Module, DBI::DBD::SQLite3
|
7
|
+
assert_kind_of Class, DBI::DBD::SQLite3::Driver
|
8
|
+
assert_kind_of Class, DBI::DBD::SQLite3::Database
|
9
|
+
assert_kind_of Class, DBI::DBD::SQLite3::Statement
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_connect
|
13
|
+
config = DBDConfig.get_config['sqlite3']
|
14
|
+
|
15
|
+
# this tests DBI more than SQLite, but makes sure our chain works with it.
|
16
|
+
dbh = DBI.connect("dbi:SQLite3:" + config['dbname'], nil, nil, {})
|
17
|
+
assert dbh
|
18
|
+
assert_kind_of DBI::DatabaseHandle, dbh
|
19
|
+
|
20
|
+
# first argument should be a string
|
21
|
+
assert_raise(DBI::InterfaceError) do
|
22
|
+
DBI::DBD::SQLite3::Driver.new.connect(nil, nil, nil, { })
|
23
|
+
end
|
24
|
+
|
25
|
+
# that string should have some frackin' length
|
26
|
+
assert_raise(DBI::InterfaceError) do
|
27
|
+
DBI::DBD::SQLite3::Driver.new.connect("", nil, nil, { })
|
28
|
+
end
|
29
|
+
|
30
|
+
# last argument should be a hash
|
31
|
+
assert_raise(DBI::InterfaceError) do
|
32
|
+
DBI::DBD::SQLite3::Driver.new.connect(config['dbname'], nil, nil, nil)
|
33
|
+
end
|
34
|
+
|
35
|
+
dbh = nil
|
36
|
+
driver = nil
|
37
|
+
assert_nothing_raised do
|
38
|
+
driver = DBI::DBD::SQLite3::Driver.new
|
39
|
+
dbh = driver.connect(config['dbname'], nil, nil, { })
|
40
|
+
end
|
41
|
+
|
42
|
+
assert_kind_of DBI::DBD::SQLite3::Driver, driver
|
43
|
+
assert_kind_of DBI::DBD::SQLite3::Database, dbh
|
44
|
+
|
45
|
+
assert !dbh.instance_variable_get("@autocommit")
|
46
|
+
|
47
|
+
dbh = nil
|
48
|
+
driver = nil
|
49
|
+
assert_nothing_raised do
|
50
|
+
dbh = DBI::DBD::SQLite3::Driver.new.connect(config['dbname'], nil, nil, { "AutoCommit" => true, "sqlite_full_column_names" => true })
|
51
|
+
end
|
52
|
+
|
53
|
+
assert dbh
|
54
|
+
assert dbh.instance_variable_get("@attr")
|
55
|
+
assert_kind_of SQLite3::Database, dbh.instance_variable_get("@db")
|
56
|
+
|
57
|
+
assert File.exists?(config['dbname'])
|
58
|
+
end
|
59
|
+
|
60
|
+
def setup
|
61
|
+
end
|
62
|
+
|
63
|
+
def teardown
|
64
|
+
config = DBDConfig.get_config['sqlite3']
|
65
|
+
FileUtils.rm_f(config['dbname'])
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
class TestStatement < DBDConfig.testbase(:sqlite3)
|
2
|
+
def test_constructor
|
3
|
+
sth = DBI::DBD::SQLite3::Statement.new("select * from names", @dbh.instance_variable_get("@handle").instance_variable_get("@db"))
|
4
|
+
|
5
|
+
assert_kind_of DBI::DBD::SQLite3::Statement, sth
|
6
|
+
assert sth.instance_variable_get("@db")
|
7
|
+
assert_kind_of ::SQLite3::Database, sth.instance_variable_get("@db")
|
8
|
+
assert_equal(@dbh.instance_variable_get("@handle").instance_variable_get("@db"), sth.instance_variable_get("@db"))
|
9
|
+
assert_kind_of ::SQLite3::Statement, sth.instance_variable_get("@stmt")
|
10
|
+
assert_nil(@sth.instance_variable_get("@result"))
|
11
|
+
|
12
|
+
sth.finish
|
13
|
+
|
14
|
+
sth = @dbh.prepare("select * from names")
|
15
|
+
|
16
|
+
assert_kind_of DBI::StatementHandle, sth
|
17
|
+
sth.finish
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_bind_param
|
21
|
+
sth = DBI::DBD::SQLite3::Statement.new("select * from names", @dbh.instance_variable_get("@handle").instance_variable_get("@db"))
|
22
|
+
|
23
|
+
assert_raise(DBI::InterfaceError) do
|
24
|
+
sth.bind_param(:foo, "monkeys")
|
25
|
+
end
|
26
|
+
|
27
|
+
sth.finish
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_column_info
|
31
|
+
@sth = nil
|
32
|
+
|
33
|
+
assert_nothing_raised do
|
34
|
+
@sth = @dbh.prepare("select * from names")
|
35
|
+
@sth.execute
|
36
|
+
end
|
37
|
+
|
38
|
+
assert_kind_of Array, @sth.column_info
|
39
|
+
assert_kind_of DBI::ColumnInfo, @sth.column_info[0]
|
40
|
+
assert_kind_of DBI::ColumnInfo, @sth.column_info[1]
|
41
|
+
assert_equal [
|
42
|
+
{
|
43
|
+
:name => "name",
|
44
|
+
:sql_type => 12,
|
45
|
+
:precision => 255,
|
46
|
+
:type_name => "varchar"
|
47
|
+
},
|
48
|
+
{
|
49
|
+
:name => "age",
|
50
|
+
:sql_type => 4,
|
51
|
+
:type_name => "integer"
|
52
|
+
}
|
53
|
+
], @sth.column_info
|
54
|
+
|
55
|
+
@sth.finish
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
create table names (name varchar(255), age integer);
|
2
|
+
---
|
3
|
+
insert into names (name, age) values ("Bob", 21);
|
4
|
+
---
|
5
|
+
insert into names (name, age) values ("Joe", 19);
|
6
|
+
---
|
7
|
+
insert into names (name, age) values ("Jim", 30);
|
8
|
+
---
|
9
|
+
create table precision_test (text_field varchar(20) primary key not null, integer_field decimal(2,1));
|
10
|
+
---
|
11
|
+
create view view_names as select * from names;
|
12
|
+
---
|
13
|
+
create table blob_test (name varchar(255));
|
14
|
+
---
|
15
|
+
create table boolean_test (num integer, mybool boolean);
|
16
|
+
---
|
17
|
+
create table time_test (mytime time);
|
18
|
+
---
|
19
|
+
create table timestamp_test (mytimestamp timestamp);
|
20
|
+
---
|
21
|
+
create table bit_test (mybit bit);
|
22
|
+
---
|
23
|
+
create table field_types_test (foo integer not null primary key default 1);
|