Skip to content

Commit 886438c

Browse files
authored
Merge pull request #471 from sparklemotion/remove-types
Remove row array wrappers
2 parents d7f5f6b + 002443e commit 886438c

File tree

5 files changed

+32
-82
lines changed

5 files changed

+32
-82
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,22 @@ db.prepare("SELECT * FROM items") do |stmt|
6969
end
7070
```
7171

72+
- Removed `types` and `fields` readers on row objects.
73+
Deprecated code looks like this:
74+
75+
```ruby
76+
row = @db.execute("select * from foo")
77+
assert_equal ["blob"], row.first.types
78+
```
79+
80+
If you would like to access the "types" associated with a returned query,
81+
use a prepared statement like this:
82+
83+
```ruby
84+
@db.prepare("select * from foo") do |v|
85+
assert_equal ["blob"], v.types
86+
end
87+
```
7288

7389
## 1.7.0 / 2023-12-27
7490

lib/sqlite3/database.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ def readonly?
676676
# but only retries up to the indicated number of +milliseconds+.
677677
# This is an alternative to #busy_timeout, which holds the GVL
678678
# while SQLite sleeps and retries.
679-
def busy_handler_timeout=( milliseconds )
679+
def busy_handler_timeout=(milliseconds)
680680
timeout_seconds = milliseconds.fdiv(1000)
681681

682682
busy_handler do |count|

lib/sqlite3/resultset.rb

Lines changed: 2 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -9,51 +9,6 @@ module SQLite3
99
class ResultSet
1010
include Enumerable
1111

12-
class ArrayWithTypesAndFields < Array # :nodoc:
13-
attr_writer :types
14-
attr_writer :fields
15-
16-
def types
17-
warn(<<~EOWARN) if $VERBOSE
18-
#{caller(1..1).first} is calling `#{self.class}#types` which is deprecated and will be removed in sqlite3 version 2.0.0. Please call the `types` method on the SQLite3::ResultSet object that created this object.
19-
EOWARN
20-
@types
21-
end
22-
23-
def fields
24-
warn(<<~EOWARN) if $VERBOSE
25-
#{caller(1..1).first} is calling `#{self.class}#fields` which is deprecated and will be removed in sqlite3 version 2.0.0. Please call the `columns` method on the SQLite3::ResultSet object that created this object.
26-
EOWARN
27-
@fields
28-
end
29-
end
30-
31-
# The class of which we return an object in case we want a Hash as
32-
# result.
33-
class HashWithTypesAndFields < Hash # :nodoc:
34-
attr_writer :types
35-
attr_writer :fields
36-
37-
def types
38-
warn(<<~EOWARN) if $VERBOSE
39-
#{caller(1..1).first} is calling `#{self.class}#types` which is deprecated and will be removed in sqlite3 version 2.0.0. Please call the `types` method on the SQLite3::ResultSet object that created this object.
40-
EOWARN
41-
@types
42-
end
43-
44-
def fields
45-
warn(<<~EOWARN) if $VERBOSE
46-
#{caller(1..1).first} is calling `#{self.class}#fields` which is deprecated and will be removed in sqlite3 version 2.0.0. Please call the `columns` method on the SQLite3::ResultSet object that created this object.
47-
EOWARN
48-
@fields
49-
end
50-
51-
def [] key
52-
key = fields[key] if key.is_a? Numeric
53-
super(key)
54-
end
55-
end
56-
5712
# Create a new ResultSet attached to the given database, using the
5813
# given sql text.
5914
def initialize db, stmt
@@ -85,17 +40,7 @@ def eof?
8540
# For hashes, the column names are the keys of the hash, and the column
8641
# types are accessible via the +types+ property.
8742
def next
88-
row = @stmt.step
89-
return nil if @stmt.done?
90-
91-
# FIXME: the `fields` and `types` methods are deprecated on this
92-
# object for version 2.0, so we can safely remove this branch
93-
# as well.
94-
row = ArrayWithTypesAndFields.new(row)
95-
96-
row.fields = @stmt.columns
97-
row.types = @stmt.types
98-
row
43+
@stmt.step
9944
end
10045

10146
# Required by the Enumerable mixin. Provides an internal iterator over the
@@ -141,14 +86,7 @@ def next_hash
14186
row = @stmt.step
14287
return nil if @stmt.done?
14388

144-
# FIXME: this can be switched to a regular hash in 2.0
145-
row = HashWithTypesAndFields[*@stmt.columns.zip(row).flatten]
146-
147-
# FIXME: these methods are deprecated for version 2.0, so we can remove
148-
# this code in 2.0
149-
row.fields = @stmt.columns
150-
row.types = @stmt.types
151-
row
89+
Hash[*@stmt.columns.zip(row).flatten]
15290
end
15391
end
15492

test/test_integration_pending.rb

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ def test_busy_handler_timeout_releases_gvl
7979
work = []
8080

8181
Thread.new do
82-
while true
82+
loop do
8383
sleep 0.1
84-
work << '.'
84+
work << "."
8585
end
8686
end
8787
sleep 1
@@ -91,25 +91,23 @@ def test_busy_handler_timeout_releases_gvl
9191
busy.lock
9292

9393
t = Thread.new do
94-
begin
95-
db2 = SQLite3::Database.open( "test.db" )
96-
db2.transaction( :exclusive ) do
97-
busy.lock
98-
end
99-
ensure
100-
db2.close if db2
94+
db2 = SQLite3::Database.open("test.db")
95+
db2.transaction(:exclusive) do
96+
busy.lock
10197
end
98+
ensure
99+
db2&.close
102100
end
103101
sleep 1
104102

105-
assert_raises( SQLite3::BusyException ) do
106-
work << '|'
103+
work << "|"
104+
assert_raises(SQLite3::BusyException) do
107105
@db.execute "insert into foo (b) values ( 'from 2' )"
108106
end
109107

110108
busy.unlock
111109
t.join
112110

113-
assert work.size - work.find_index('|') > 3
111+
assert_operator work.size - work.find_index("|"), :>, 3
114112
end
115113
end

test/test_statement.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,12 +136,10 @@ def test_bind_blob
136136
stmt = SQLite3::Statement.new(@db, "insert into foo(text) values (?)")
137137
stmt.bind_param(1, SQLite3::Blob.new("hello"))
138138
stmt.execute
139-
row = @db.execute("select * from foo")
140139
stmt.close
141-
142-
assert_equal ["hello"], row.first
143-
capture_io do # hush deprecation warning
144-
assert_equal ["blob"], row.first.types
140+
@db.prepare("select * from foo") do |v|
141+
assert_equal ["hello"], v.first
142+
assert_equal ["blob"], v.types
145143
end
146144
end
147145

0 commit comments

Comments
 (0)