Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read Slice(UInt8) as String if given as type #221

Merged
merged 2 commits into from
Jan 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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