Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions ext/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ module PG
# optional headers/functions
have_func 'PQresultMemorySize', 'libpq-fe.h' # since PostgreSQL-12
have_func 'timegm'
have_func 'rb_enc_interned_str' # since ruby-3.0
have_func 'rb_io_wait' # since ruby-3.0
have_func 'rb_io_descriptor' # since ruby-3.1
have_func 'rb_hash_new_capa' # since ruby-3.2
Expand Down
5 changes: 5 additions & 0 deletions ext/pg_result.c
Original file line number Diff line number Diff line change
Expand Up @@ -474,9 +474,14 @@ static VALUE pg_cstr_to_sym(char *cstr, unsigned int flags, int enc_idx)
fname = rb_str_intern(fname);
}
} else {
#ifdef HAVE_RB_ENC_INTERNED_STR
rb_encoding *enc = rb_enc_from_index(enc_idx);
fname = rb_enc_interned_str(cstr, strlen(cstr), enc);
#else
fname = rb_str_new2(cstr);
PG_ENCODING_SET_NOCHECK(fname, enc_idx);
fname = rb_obj_freeze(fname);
#endif
}
return fname;
}
Expand Down
10 changes: 10 additions & 0 deletions spec/pg/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,16 @@
it "can retrieve field names" do
res = @conn.exec('SELECT 1 AS a, 2 AS "B"')
expect(res.fields).to eq(["a", "B"])
expect(res.fields).to all(be_frozen)
end

it "shares string field names across results" do
skip "requires ruby-3.0" if RUBY_VERSION < "3.0"

first = @conn.exec("SELECT 1 AS shared_column")
second = @conn.exec("SELECT 2 AS shared_column")

expect(first.fields.first).to equal(second.fields.first)
end

it "can retrieve field names as symbols" do
Expand Down
Loading