Skip to content

Commit

Permalink
Refactor and support asking for String?
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewmcgarvey authored and will committed Jan 12, 2021
1 parent 24e9091 commit d4abb0c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
4 changes: 4 additions & 0 deletions spec/pg/driver_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ describe PG::Driver do
db.query "select email from users limit 1" do |rs|
assert_single_read rs, String, "foo@example.com"
end

db.query "select email from users limit 1" do |rs|
assert_single_read rs, String?, "foo@example.com"
end
end
end

Expand Down
19 changes: 15 additions & 4 deletions src/pg/result_set.cr
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,24 @@ class PG::ResultSet < ::DB::ResultSet
end

def read(t : String.class) : String
value = read
if value.is_a?(String)
value = read(String | Slice(UInt8))

case value
when Slice(UInt8)
String.new(value)
else
value
elsif value.is_a?(Slice(UInt8))
end
end

def read(t : String?.class) : String?
value = read(String | Slice(UInt8) | Nil)

case value
when Slice(UInt8)
String.new(value)
else
raise "#{self.class}#read returned a #{value.class}. A String was expected."
value
end
end

Expand Down

0 comments on commit d4abb0c

Please sign in to comment.