Skip to content

Commit

Permalink
Read Slice(UInt8) as String if given as type
Browse files Browse the repository at this point in the history
Fixes #43
  • Loading branch information
matthewmcgarvey authored and will committed Jan 12, 2021
1 parent cafe0fd commit 24e9091
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
13 changes: 13 additions & 0 deletions spec/pg/driver_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,19 @@ describe PG::Driver do
end
end

it "treats Slice(UInt8) as String" do
with_db do |db|
db.exec "create extension if not exists citext with schema public"
db.exec "drop table if exists users"
db.exec "create table users (email citext)"
db.exec "insert into users values ($1)", "foo@example.com"

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

describe "transactions" do
it "can read inside transaction and rollback after" do
with_db do |db|
Expand Down
11 changes: 11 additions & 0 deletions src/pg/result_set.cr
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,17 @@ class PG::ResultSet < ::DB::ResultSet
end
end

def read(t : String.class) : String
value = read
if value.is_a?(String)
value
elsif value.is_a?(Slice(UInt8))
String.new(value)
else
raise "#{self.class}#read returned a #{value.class}. A String was expected."
end
end

private def read_array(t : T.class) : T forall T
col_bytesize = conn.read_i32
if col_bytesize == -1
Expand Down

0 comments on commit 24e9091

Please sign in to comment.