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

Add Result.res_status as a class method and extend Result#res_status … #508

Merged
merged 1 commit into from
Mar 24, 2023
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
33 changes: 29 additions & 4 deletions ext/pg_result.c
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,8 @@ static void pgresult_init_fnames(VALUE self)
* * +PGRES_SINGLE_TUPLE+
* * +PGRES_PIPELINE_SYNC+
* * +PGRES_PIPELINE_ABORTED+
*
* Use <tt>res.res_status</tt> to retrieve the string representation.
*/
static VALUE
pgresult_result_status(VALUE self)
Expand All @@ -536,16 +538,38 @@ pgresult_result_status(VALUE self)

/*
* call-seq:
* res.res_status( status ) -> String
* PG::Result.res_status( status ) -> String
*
* Returns the string representation of +status+.
*
*/
static VALUE
pgresult_res_status(VALUE self, VALUE status)
pgresult_s_res_status(VALUE self, VALUE status)
{
return rb_utf8_str_new_cstr(PQresStatus(NUM2INT(status)));
}

/*
* call-seq:
* res.res_status -> String
* res.res_status( status ) -> String
*
* Returns the string representation of the status of the result or of the provided +status+.
*
*/
static VALUE
pgresult_res_status(int argc, VALUE *argv, VALUE self)
{
t_pg_result *this = pgresult_get_this_safe(self);
VALUE ret = rb_str_new2(PQresStatus(NUM2INT(status)));
VALUE ret;

if( argc == 0 ){
ret = rb_str_new2(PQresStatus(PQresultStatus(this->pgresult)));
}else if( argc == 1 ){
ret = rb_str_new2(PQresStatus(NUM2INT(argv[0])));
}else{
rb_raise(rb_eArgError, "only 0 or 1 arguments expected");
}
PG_ENCODING_SET_NOCHECK(ret, this->enc_idx);
return ret;
}
Expand Down Expand Up @@ -1632,7 +1656,8 @@ init_pg_result(void)

/****** PG::Result INSTANCE METHODS: libpq ******/
rb_define_method(rb_cPGresult, "result_status", pgresult_result_status, 0);
rb_define_method(rb_cPGresult, "res_status", pgresult_res_status, 1);
rb_define_method(rb_cPGresult, "res_status", pgresult_res_status, -1);
rb_define_singleton_method(rb_cPGresult, "res_status", pgresult_s_res_status, 1);
rb_define_method(rb_cPGresult, "error_message", pgresult_error_message, 0);
rb_define_alias( rb_cPGresult, "result_error_message", "error_message");
#ifdef HAVE_PQRESULTVERBOSEERRORMESSAGE
Expand Down
20 changes: 20 additions & 0 deletions spec/pg/result_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@

describe PG::Result do

it "provides res_status" do
str = PG::Result.res_status(PG::PGRES_EMPTY_QUERY)
expect( str ).to eq("PGRES_EMPTY_QUERY")
expect( str.encoding ).to eq(Encoding::UTF_8)

res = @conn.exec("SELECT 1")
expect( res.res_status ).to eq("PGRES_TUPLES_OK")
expect( res.res_status(PG::PGRES_FATAL_ERROR) ).to eq("PGRES_FATAL_ERROR")

expect{ res.res_status(1,2) }.to raise_error(ArgumentError)
end

describe :field_name_type do
let!(:res) { @conn.exec('SELECT 1 AS a, 2 AS "B"') }

Expand Down Expand Up @@ -433,6 +445,14 @@
expect( res.values ).to eq( [ ["bar"], ["bar2"] ] )
end

it "provides the result status" do
res = @conn.exec("SELECT 1")
expect( res.result_status ).to eq(PG::PGRES_TUPLES_OK)

res = @conn.exec("")
expect( res.result_status ).to eq(PG::PGRES_EMPTY_QUERY)
end

it "can retrieve field names" do
res = @conn.exec('SELECT 1 AS a, 2 AS "B"')
expect(res.fields).to eq(["a", "B"])
Expand Down