/* * call-seq: * res.cmd_status() -> String * * Returns the status string of the last query command. */ static VALUE pgresult_cmd_status(VALUE self) { VALUE ret = rb_tainted_str_new2(PQcmdStatus(pgresult_get(self))); ASSOCIATE_INDEX(ret, self); return ret; }
/* * call-seq: * res.error_message() -> String * * Returns the error message of the command as a string. */ static VALUE pgresult_error_message(VALUE self) { VALUE ret = rb_tainted_str_new2(PQresultErrorMessage(pgresult_get(self))); ASSOCIATE_INDEX(ret, self); return ret; }
/* * call-seq: * res.clear() -> nil * * Clears the PG::Result object as the result of the query. */ VALUE pg_result_clear(VALUE self) { PQclear(pgresult_get(self)); DATA_PTR(self) = NULL; return Qnil; }
/* * call-seq: * res.cmd_tuples() -> Fixnum * * Returns the number of tuples (rows) affected by the SQL command. * * If the SQL command that generated the PG::Result was not one of: * * +INSERT+ * * +UPDATE+ * * +DELETE+ * * +MOVE+ * * +FETCH+ * or if no tuples were affected, <tt>0</tt> is returned. */ static VALUE pgresult_cmd_tuples(VALUE self) { long n; n = strtol(PQcmdTuples(pgresult_get(self)),NULL, 10); return INT2NUM(n); }
/* * Make a Ruby array out of the encoded values from the specified * column in the given result. */ static VALUE make_column_result_array( VALUE self, int col ) { PGresult *result = pgresult_get( self ); int rows = PQntuples( result ); int i; VALUE val = Qnil; VALUE results = rb_ary_new2( rows ); if ( col >= PQnfields(result) ) rb_raise( rb_eIndexError, "no column %d in result", col ); for ( i=0; i < rows; i++ ) { val = rb_tainted_str_new( PQgetvalue(result, i, col), PQgetlength(result, i, col) ); #ifdef M17N_SUPPORTED /* associate client encoding for text format only */ if ( 0 == PQfformat(result, col) ) { ASSOCIATE_INDEX( val, self ); } else { rb_enc_associate( val, rb_ascii8bit_encoding() ); } #endif rb_ary_store( results, i, val ); } return results; }
/* * call-seq: * res.getvalue( tup_num, field_num ) * * Returns the value in tuple number _tup_num_, field _field_num_, * or +nil+ if the field is +NULL+. */ static VALUE pgresult_getvalue(VALUE self, VALUE tup_num, VALUE field_num) { VALUE val; PGresult *result; int i = NUM2INT(tup_num); int j = NUM2INT(field_num); result = pgresult_get(self); if(i < 0 || i >= PQntuples(result)) { rb_raise(rb_eArgError,"invalid tuple number %d", i); } if(j < 0 || j >= PQnfields(result)) { rb_raise(rb_eArgError,"invalid field number %d", j); } if(PQgetisnull(result, i, j)) return Qnil; val = rb_tainted_str_new(PQgetvalue(result, i, j), PQgetlength(result, i, j)); #ifdef M17N_SUPPORTED /* associate client encoding for text format only */ if ( 0 == PQfformat(result, j) ) { ASSOCIATE_INDEX( val, self ); } else { rb_enc_associate( val, rb_ascii8bit_encoding() ); } #endif return val; }
static VALUE pg_tmbc_fit_to_result( VALUE self, VALUE result ) { int nfields; t_tmbc *this = DATA_PTR( self ); t_typemap *default_tm; VALUE sub_typemap; nfields = PQnfields( pgresult_get(result) ); if ( this->nfields != nfields ) { rb_raise( rb_eArgError, "number of result fields (%d) does not match number of mapped columns (%d)", nfields, this->nfields ); } /* Ensure that the default type map fits equaly. */ default_tm = DATA_PTR( this->typemap.default_typemap ); sub_typemap = default_tm->funcs.fit_to_result( this->typemap.default_typemap, result ); /* Did the default type return the same object ? */ if( sub_typemap == this->typemap.default_typemap ){ return self; } else { /* Our default type map built a new object, so we need to propagate it * and build a copy of this type map and set it as default there.. */ VALUE new_typemap = pg_tmbc_s_allocate( rb_cTypeMapByColumn ); size_t struct_size = sizeof(t_tmbc) + sizeof(struct pg_tmbc_converter) * nfields; t_tmbc *p_new_typemap = (t_tmbc *)xmalloc(struct_size); memcpy( p_new_typemap, this, struct_size ); p_new_typemap->typemap.default_typemap = sub_typemap; DATA_PTR(new_typemap) = p_new_typemap; return new_typemap; } }
/* * call-seq: * res.nparams() -> Fixnum * * Returns the number of parameters of a prepared statement. * Only useful for the result returned by conn.describePrepared */ static VALUE pgresult_nparams(VALUE self) { PGresult *result; result = pgresult_get(self); return INT2FIX(PQnparams(result)); }
/* * call-seq: * res.paramtype( param_number ) -> Oid * * Returns the Oid of the data type of parameter _param_number_. * Only useful for the result returned by conn.describePrepared */ static VALUE pgresult_paramtype(VALUE self, VALUE param_number) { PGresult *result; result = pgresult_get(self); return INT2FIX(PQparamtype(result,NUM2INT(param_number))); }
/* * call-seq: * res.clear() -> nil * * Clears the PG::Result object as the result of the query. * * If PG::Result#autoclear? is true then the result is marked as cleared * and the underlying C struct will be cleared automatically by libpq. * */ VALUE pg_result_clear(VALUE self) { t_pg_result *this = pgresult_get_this(self); if( !this->autoclear ) PQclear(pgresult_get(self)); this->pgresult = NULL; return Qnil; }
/* * call-seq: * res.oid_value() -> Fixnum * * Returns the +oid+ of the inserted row if applicable, * otherwise +nil+. */ static VALUE pgresult_oid_value(VALUE self) { Oid n = PQoidValue(pgresult_get(self)); if (n == InvalidOid) return Qnil; else return INT2FIX(n); }
/* * call-seq: * res.ftype( column_number ) * * Returns the data type associated with _column_number_. * * The integer returned is the internal +OID+ number (in PostgreSQL) * of the type. To get a human-readable value for the type, use the * returned OID and the field's #fmod value with the format_type() SQL * function: * * # Get the type of the second column of the result 'res' * typename = conn. * exec( "SELECT format_type($1,$2)", [res.ftype(1), res.fmod(1)] ). * getvalue( 0, 0 ) * * Raises an ArgumentError if _column_number_ is out of range. */ static VALUE pgresult_ftype(VALUE self, VALUE index) { PGresult* result = pgresult_get(self); int i = NUM2INT(index); if (i < 0 || i >= PQnfields(result)) { rb_raise(rb_eArgError, "invalid field number %d", i); } return INT2NUM(PQftype(result, i)); }
/* * call-seq: * res.each{ |tuple| ... } * * Invokes block for each tuple in the result set. */ static VALUE pgresult_each(VALUE self) { PGresult *result = pgresult_get(self); int tuple_num; for(tuple_num = 0; tuple_num < PQntuples(result); tuple_num++) { rb_yield(pgresult_aref(self, INT2NUM(tuple_num))); } return self; }
/* * call-seq: * res.fformat( column_number ) -> Fixnum * * Returns the format (0 for text, 1 for binary) of column * _column_number_. * * Raises ArgumentError if _column_number_ is out of range. */ static VALUE pgresult_fformat(VALUE self, VALUE column_number) { PGresult *result = pgresult_get(self); int fnumber = NUM2INT(column_number); if (fnumber < 0 || fnumber >= PQnfields(result)) { rb_raise(rb_eArgError, "Column number is out of range: %d", fnumber); } return INT2FIX(PQfformat(result, fnumber)); }
/* * call-seq: * res.field_values( field ) -> array * * Returns an Array of the values from the given _field_ of each tuple in the result. * */ static VALUE pgresult_field_values( VALUE self, VALUE field ) { PGresult *result = pgresult_get( self ); const char *fieldname = StringValuePtr( field ); int fnum = PQfnumber( result, fieldname ); if ( fnum < 0 ) rb_raise( rb_eIndexError, "no such field '%s' in result", fieldname ); return make_column_result_array( self, fnum ); }
/* * call-seq: * res.ftable( column_number ) -> Fixnum * * Returns the Oid of the table from which the column _column_number_ * was fetched. * * Raises ArgumentError if _column_number_ is out of range or if * the Oid is undefined for that column. */ static VALUE pgresult_ftable(VALUE self, VALUE column_number) { Oid n ; int col_number = NUM2INT(column_number); PGresult *pgresult = pgresult_get(self); if( col_number < 0 || col_number >= PQnfields(pgresult)) rb_raise(rb_eArgError,"Invalid column index: %d", col_number); n = PQftable(pgresult, col_number); return INT2FIX(n); }
/* * call-seq: * res.fnumber( name ) -> Fixnum * * Returns the index of the field specified by the string +name+. * The given +name+ is treated like an identifier in an SQL command, that is, * it is downcased unless double-quoted. For example, given a query result * generated from the SQL command: * * result = conn.exec( %{SELECT 1 AS FOO, 2 AS "BAR"} ) * * we would have the results: * * result.fname( 0 ) # => "foo" * result.fname( 1 ) # => "BAR" * result.fnumber( "FOO" ) # => 0 * result.fnumber( "foo" ) # => 0 * result.fnumber( "BAR" ) # => ArgumentError * result.fnumber( %{"BAR"} ) # => 1 * * Raises an ArgumentError if the specified +name+ isn't one of the field names; * raises a TypeError if +name+ is not a String. */ static VALUE pgresult_fnumber(VALUE self, VALUE name) { int n; Check_Type(name, T_STRING); n = PQfnumber(pgresult_get(self), StringValuePtr(name)); if (n == -1) { rb_raise(rb_eArgError,"Unknown field: %s", StringValuePtr(name)); } return INT2FIX(n); }
/* * call-seq: * res.fmod( column_number ) * * Returns the type modifier associated with column _column_number_. See * the #ftype method for an example of how to use this. * * Raises an ArgumentError if _column_number_ is out of range. */ static VALUE pgresult_fmod(VALUE self, VALUE column_number) { PGresult *result = pgresult_get(self); int fnumber = NUM2INT(column_number); int modifier; if (fnumber < 0 || fnumber >= PQnfields(result)) { rb_raise(rb_eArgError, "Column number is out of range: %d", fnumber); } modifier = PQfmod(result,fnumber); return INT2NUM(modifier); }
/* * call-seq: * res.error_field(fieldcode) -> String * * Returns the individual field of an error. * * +fieldcode+ is one of: * * +PG_DIAG_SEVERITY+ * * +PG_DIAG_SQLSTATE+ * * +PG_DIAG_MESSAGE_PRIMARY+ * * +PG_DIAG_MESSAGE_DETAIL+ * * +PG_DIAG_MESSAGE_HINT+ * * +PG_DIAG_STATEMENT_POSITION+ * * +PG_DIAG_INTERNAL_POSITION+ * * +PG_DIAG_INTERNAL_QUERY+ * * +PG_DIAG_CONTEXT+ * * +PG_DIAG_SOURCE_FILE+ * * +PG_DIAG_SOURCE_LINE+ * * +PG_DIAG_SOURCE_FUNCTION+ * * An example: * * begin * conn.exec( "SELECT * FROM nonexistant_table" ) * rescue PG::Error => err * p [ * err.result.error_field( PG::Result::PG_DIAG_SEVERITY ), * err.result.error_field( PG::Result::PG_DIAG_SQLSTATE ), * err.result.error_field( PG::Result::PG_DIAG_MESSAGE_PRIMARY ), * err.result.error_field( PG::Result::PG_DIAG_MESSAGE_DETAIL ), * err.result.error_field( PG::Result::PG_DIAG_MESSAGE_HINT ), * err.result.error_field( PG::Result::PG_DIAG_STATEMENT_POSITION ), * err.result.error_field( PG::Result::PG_DIAG_INTERNAL_POSITION ), * err.result.error_field( PG::Result::PG_DIAG_INTERNAL_QUERY ), * err.result.error_field( PG::Result::PG_DIAG_CONTEXT ), * err.result.error_field( PG::Result::PG_DIAG_SOURCE_FILE ), * err.result.error_field( PG::Result::PG_DIAG_SOURCE_LINE ), * err.result.error_field( PG::Result::PG_DIAG_SOURCE_FUNCTION ), * ] * end * * Outputs: * * ["ERROR", "42P01", "relation \"nonexistant_table\" does not exist", nil, nil, * "15", nil, nil, nil, "path/to/parse_relation.c", "857", "parserOpenTable"] */ static VALUE pgresult_error_field(VALUE self, VALUE field) { PGresult *result = pgresult_get( self ); int fieldcode = NUM2INT( field ); char * fieldstr = PQresultErrorField( result, fieldcode ); VALUE ret = Qnil; if ( fieldstr ) { ret = rb_tainted_str_new2( fieldstr ); ASSOCIATE_INDEX( ret, self ); } return ret; }
/* * call-seq: * res.fname( index ) -> String * * Returns the name of the column corresponding to _index_. */ static VALUE pgresult_fname(VALUE self, VALUE index) { VALUE fname; PGresult *result; int i = NUM2INT(index); result = pgresult_get(self); if (i < 0 || i >= PQnfields(result)) { rb_raise(rb_eArgError,"invalid field number %d", i); } fname = rb_tainted_str_new2(PQfname(result, i)); ASSOCIATE_INDEX(fname, self); return fname; }
/* * call-seq: * res.fields() -> Array * * Returns an array of Strings representing the names of the fields in the result. */ static VALUE pgresult_fields(VALUE self) { PGresult *result = pgresult_get( self ); int n = PQnfields( result ); VALUE fields = rb_ary_new2( n ); int i; for ( i = 0; i < n; i++ ) { VALUE val = rb_tainted_str_new2(PQfname(result, i)); ASSOCIATE_INDEX(val, self); rb_ary_store( fields, i, val ); } return fields; }
/* * call-seq: * res.getlength( tup_num, field_num ) -> Fixnum * * Returns the (String) length of the field in bytes. * * Equivalent to <tt>res.value(<i>tup_num</i>,<i>field_num</i>).length</tt>. */ static VALUE pgresult_getlength(VALUE self, VALUE tup_num, VALUE field_num) { PGresult *result; int i = NUM2INT(tup_num); int j = NUM2INT(field_num); result = pgresult_get(self); if (i < 0 || i >= PQntuples(result)) { rb_raise(rb_eArgError,"invalid tuple number %d", i); } if (j < 0 || j >= PQnfields(result)) { rb_raise(rb_eArgError,"invalid field number %d", j); } return INT2FIX(PQgetlength(result, i, j)); }
/* * call-seq: * res[ n ] -> Hash * * Returns tuple _n_ as a hash. */ static VALUE pgresult_aref(VALUE self, VALUE index) { PGresult *result = pgresult_get(self); int tuple_num = NUM2INT(index); int field_num; VALUE fname; VALUE tuple; if ( tuple_num < 0 || tuple_num >= PQntuples(result) ) rb_raise( rb_eIndexError, "Index %d is out of range", tuple_num ); tuple = rb_hash_new(); for ( field_num = 0; field_num < PQnfields(result); field_num++ ) { fname = rb_tainted_str_new2( PQfname(result,field_num) ); ASSOCIATE_INDEX(fname, self); rb_hash_aset( tuple, fname, pgresult_value(self, result, tuple_num, field_num) ); } return tuple; }
/* * call-seq: * res.each_row { |row| ... } * * Yields each row of the result. The row is a list of column values. */ static VALUE pgresult_each_row(VALUE self) { PGresult* result = (PGresult*) pgresult_get(self); int row; int field; int num_rows = PQntuples(result); int num_fields = PQnfields(result); for ( row = 0; row < num_rows; row++ ) { VALUE new_row = rb_ary_new2(num_fields); /* populate the row */ for ( field = 0; field < num_fields; field++ ) { rb_ary_store( new_row, field, pgresult_value(self, result, row, field) ); } rb_yield( new_row ); } return Qnil; }
/* * call-seq: * res.values -> Array * * Returns all tuples as an array of arrays. */ static VALUE pgresult_values(VALUE self) { PGresult* result = (PGresult*) pgresult_get(self); int row; int field; int num_rows = PQntuples(result); int num_fields = PQnfields(result); VALUE ary = rb_ary_new2(num_rows); for ( row = 0; row < num_rows; row++ ) { VALUE new_row = rb_ary_new2(num_fields); /* populate the row */ for ( field = 0; field < num_fields; field++ ) { if ( PQgetisnull(result, row, field) ) { rb_ary_store( new_row, field, Qnil ); } else { VALUE val = rb_tainted_str_new( PQgetvalue(result, row, field), PQgetlength(result, row, field) ); #ifdef M17N_SUPPORTED /* associate client encoding for text format only */ if ( 0 == PQfformat(result, field) ) { ASSOCIATE_INDEX( val, self ); } else { rb_enc_associate( val, rb_ascii8bit_encoding() ); } #endif rb_ary_store( new_row, field, val ); } } rb_ary_store( ary, row, new_row ); } return ary; }
/* * call-seq: * res[ n ] -> Hash * * Returns tuple _n_ as a hash. */ static VALUE pgresult_aref(VALUE self, VALUE index) { PGresult *result = pgresult_get(self); int tuple_num = NUM2INT(index); int field_num; VALUE fname,val; VALUE tuple; if ( tuple_num < 0 || tuple_num >= PQntuples(result) ) rb_raise( rb_eIndexError, "Index %d is out of range", tuple_num ); tuple = rb_hash_new(); for ( field_num = 0; field_num < PQnfields(result); field_num++ ) { fname = rb_tainted_str_new2( PQfname(result,field_num) ); ASSOCIATE_INDEX(fname, self); if ( PQgetisnull(result, tuple_num, field_num) ) { rb_hash_aset( tuple, fname, Qnil ); } else { val = rb_tainted_str_new( PQgetvalue(result, tuple_num, field_num ), PQgetlength(result, tuple_num, field_num) ); #ifdef M17N_SUPPORTED /* associate client encoding for text format only */ if ( 0 == PQfformat(result, field_num) ) { ASSOCIATE_INDEX( val, self ); } else { rb_enc_associate( val, rb_ascii8bit_encoding() ); } #endif rb_hash_aset( tuple, fname, val ); } } return tuple; }
/* * call-seq: * res.result_status() -> Fixnum * * Returns the status of the query. The status value is one of: * * +PGRES_EMPTY_QUERY+ * * +PGRES_COMMAND_OK+ * * +PGRES_TUPLES_OK+ * * +PGRES_COPY_OUT+ * * +PGRES_COPY_IN+ * * +PGRES_BAD_RESPONSE+ * * +PGRES_NONFATAL_ERROR+ * * +PGRES_FATAL_ERROR+ * * +PGRES_COPY_BOTH+ */ static VALUE pgresult_result_status(VALUE self) { return INT2FIX(PQresultStatus(pgresult_get(self))); }
/* * call-seq: * res.nfields() -> Fixnum * * Returns the number of columns in the query result. */ static VALUE pgresult_nfields(VALUE self) { return INT2NUM(PQnfields(pgresult_get(self))); }
/* * call-seq: * res.ntuples() -> Fixnum * * Returns the number of tuples in the query result. */ static VALUE pgresult_ntuples(VALUE self) { return INT2FIX(PQntuples(pgresult_get(self))); }