PGconn

class
Superclass
rb_cObject
Included Modules
rb_mPGconstants

The PostgreSQL connection class. The interface for this class is based on libpq, the C application programmer's interface to PostgreSQL. Some familiarity with libpq is recommended, but not necessary.

For example, to send query to the database on the localhost:

require 'pg'
conn = PG::Connection.open(:dbname => 'test')
res = conn.exec_params('SELECT $1 AS a, $2 AS b, $3 AS c', [1, 2, nil])
# Equivalent to:
#  res  = conn.exec('SELECT 1 AS a, 2 AS b, NULL AS c')

See the PG::Result class for information on working with the results of a query.

Constants

CONNECT_ARGUMENT_ORDER

The order the options are passed to the ::connect method.

Public Class Methods

anchor
PG::Connection.conndefaults() → Array

Returns an array of hashes. Each hash has the keys:

:keyword

the name of the option

:envvar

the environment variable to fall back to

:compiled

the compiled in option as a secondary fallback

:val

the option's current value, or nil if not known

:label

the label for the field

:dispchar

“” for normal, “D” for debug, and “*” for password

:dispsize

field size

static VALUE
pgconn_s_conndefaults(VALUE self)
{
        PQconninfoOption *options = PQconndefaults();
        VALUE ary = rb_ary_new();
        VALUE hash;
        int i = 0;

        UNUSED( self );

        for(i = 0; options[i].keyword != NULL; i++) {
                hash = rb_hash_new();
                if(options[i].keyword)
                        rb_hash_aset(hash, ID2SYM(rb_intern("keyword")), rb_str_new2(options[i].keyword));
                if(options[i].envvar)
                        rb_hash_aset(hash, ID2SYM(rb_intern("envvar")), rb_str_new2(options[i].envvar));
                if(options[i].compiled)
                        rb_hash_aset(hash, ID2SYM(rb_intern("compiled")), rb_str_new2(options[i].compiled));
                if(options[i].val)
                        rb_hash_aset(hash, ID2SYM(rb_intern("val")), rb_str_new2(options[i].val));
                if(options[i].label)
                        rb_hash_aset(hash, ID2SYM(rb_intern("label")), rb_str_new2(options[i].label));
                if(options[i].dispchar)
                        rb_hash_aset(hash, ID2SYM(rb_intern("dispchar")), rb_str_new2(options[i].dispchar));
                rb_hash_aset(hash, ID2SYM(rb_intern("dispsize")), INT2NUM(options[i].dispsize));
                rb_ary_push(ary, hash);
        }
        PQconninfoFree(options);
        return ary;
}
anchor
PG::Connection.connect_start(connection_hash) → conn
PG::Connection.connect_start(connection_string) → conn
PG::Connection.connect_start(host, port, options, tty, dbname, login, password) → conn

This is an asynchronous version of PG::Connection.connect().

Use connect_poll to poll the status of the connection.

NOTE: this does not set the connection's client_encoding for you if Encoding.default_internal is set. To set it after the connection is established, call internal_encoding=. You can also set it automatically by setting ENV, or include the 'options' connection parameter.

static VALUE
pgconn_s_connect_start( int argc, VALUE *argv, VALUE klass )
{
        PGconn *conn = NULL;
        VALUE rb_conn;
        VALUE conninfo;
        VALUE error;

        /*
         * PG::Connection.connect_start must act as both alloc() and initialize()
         * because it is not invoked by calling new().
         */
        rb_conn  = pgconn_s_allocate( klass );
        conninfo = rb_funcall2( klass, rb_intern("parse_connect_args"), argc, argv );
        conn     = gvl_PQconnectStart( StringValuePtr(conninfo) );

        if( conn == NULL )
                rb_raise(rb_ePGerror, "PQconnectStart() unable to allocate structure");

        Check_Type(rb_conn, T_DATA);
        DATA_PTR(rb_conn) = conn;

        if ( PQstatus(conn) == CONNECTION_BAD ) {
                error = rb_exc_new2(rb_eConnectionBad, PQerrorMessage(conn));
                rb_iv_set(error, "@connection", rb_conn);
                rb_exc_raise(error);
        }

        if ( rb_block_given_p() ) {
                return rb_ensure( rb_yield, rb_conn, pgconn_finish, rb_conn );
        }
        return rb_conn;
}
anchor
PG::Connection.encrypt_password( password, username ) → String

This function is intended to be used by client applications that send commands like: +ALTER USER joe PASSWORD 'pwd'+. The arguments are the cleartext password, and the SQL name of the user it is for.

Return value is the encrypted password.

static VALUE
pgconn_s_encrypt_password(VALUE self, VALUE password, VALUE username)
{
        char *encrypted = NULL;
        VALUE rval = Qnil;

        UNUSED( self );

        Check_Type(password, T_STRING);
        Check_Type(username, T_STRING);

        encrypted = PQencryptPassword(StringValuePtr(password), StringValuePtr(username));
        rval = rb_str_new2( encrypted );
        PQfreemem( encrypted );

        OBJ_INFECT( rval, password );
        OBJ_INFECT( rval, username );

        return rval;
}
anchor
escape_bytea( string ) → String

Connection instance method for versions of 8.1 and higher of libpq uses PQescapeByteaConn, which is safer. Avoid calling as a class method, the class method uses the deprecated PQescapeBytea() API function.

Use the instance method version of this function, it is safer than the class method.

Escapes binary data for use within an SQL command with the type bytea.

Certain byte values must be escaped (but all byte values may be escaped) when used as part of a bytea literal in an SQL statement. In general, to escape a byte, it is converted into the three digit octal number equal to the octet value, and preceded by two backslashes. The single quote (') and backslash () characters have special alternative escape sequences. escape_bytea performs this operation, escaping only the minimally required bytes.

Consider using #exec_params, which avoids the need for passing values inside of SQL commands.

static VALUE
pgconn_s_escape_bytea(VALUE self, VALUE str)
{
        unsigned char *from, *to;
        size_t from_len, to_len;
        VALUE ret;

        Check_Type(str, T_STRING);
        from      = (unsigned char*)RSTRING_PTR(str);
        from_len  = RSTRING_LEN(str);

        if(rb_obj_class(self) == rb_cPGconn) {
                to = PQescapeByteaConn(pg_get_pgconn(self), from, from_len, &to_len);
        } else {
                to = PQescapeBytea( from, from_len, &to_len);
        }

        ret = rb_str_new((char*)to, to_len - 1);
        OBJ_INFECT(ret, str);
        PQfreemem(to);
        return ret;
}
anchor
escape_string( str ) → String

Connection instance method for versions of 8.1 and higher of libpq uses PQescapeStringConn, which is safer. Avoid calling as a class method, the class method uses the deprecated PQescapeString() API function.

Returns a SQL-safe version of the String str. This is the preferred way to make strings safe for inclusion in SQL queries.

Consider using #exec_params, which avoids the need for passing values inside of SQL commands.

Encoding of escaped string will be equal to client encoding of connection.

static VALUE
pgconn_s_escape(VALUE self, VALUE string)
{
        char *escaped;
        size_t size;
        int error;
        VALUE result;
#ifdef M17N_SUPPORTED
        rb_encoding* enc;
#endif

        Check_Type(string, T_STRING);

        escaped = ALLOC_N(char, RSTRING_LEN(string) * 2 + 1);
        if(rb_obj_class(self) == rb_cPGconn) {
                size = PQescapeStringConn(pg_get_pgconn(self), escaped,
                        RSTRING_PTR(string), RSTRING_LEN(string), &error);
                if(error) {
                        xfree(escaped);
                        rb_raise(rb_ePGerror, "%s", PQerrorMessage(pg_get_pgconn(self)));
                }
        } else {
                size = PQescapeString(escaped, RSTRING_PTR(string), (int)RSTRING_LEN(string));
        }
        result = rb_str_new(escaped, size);
        xfree(escaped);
        OBJ_INFECT(result, string);

#ifdef M17N_SUPPORTED
        if ( rb_obj_class(self) == rb_cPGconn ) {
                enc = pg_conn_enc_get( pg_get_pgconn(self) );
        } else {
                enc = rb_enc_get(string);
        }
        rb_enc_associate(result, enc);
#endif

        return result;
}
anchor
PG::Connection.new → conn
PG::Connection.new(connection_hash) → conn
PG::Connection.new(connection_string) → conn
PG::Connection.new(host, port, options, tty, dbname, user, password) → conn

Create a connection to the specified server.

host

server hostname

hostaddr

server address (avoids hostname lookup, overrides host)

port

server port number

dbname

connecting database name

user

login user name

password

login password

connect_timeout

maximum time to wait for connection to succeed

options

backend options

tty

(ignored in newer versions of PostgreSQL)

sslmode

(disable|allow|prefer|require)

krbsrvname

kerberos service name

gsslib

GSS library to use for GSSAPI authentication

service

service name to use for additional parameters

Examples:

# Connect using all defaults
PG::Connection.new

# As a Hash
PG::Connection.new( :dbname => 'test', :port => 5432 )

# As a String
PG::Connection.new( "dbname=test port=5432" )

# As an Array
PG::Connection.new( nil, 5432, nil, nil, 'test', nil, nil )

If the Ruby default internal encoding is set (i.e., Encoding.default_internal != nil), the connection will have its client_encoding set accordingly.

Raises a PG::Error if the connection fails.

static VALUE
pgconn_init(int argc, VALUE *argv, VALUE self)
{
        PGconn *conn = NULL;
        VALUE conninfo;
        VALUE error;

        conninfo = rb_funcall2( rb_cPGconn, rb_intern("parse_connect_args"), argc, argv );
        conn = gvl_PQconnectdb(StringValuePtr(conninfo));

        if(conn == NULL)
                rb_raise(rb_ePGerror, "PQconnectdb() unable to allocate structure");

        Check_Type(self, T_DATA);
        DATA_PTR(self) = conn;

        if (PQstatus(conn) == CONNECTION_BAD) {
                error = rb_exc_new2(rb_eConnectionBad, PQerrorMessage(conn));
                rb_iv_set(error, "@connection", self);
                rb_exc_raise(error);
        }

#ifdef M17N_SUPPORTED
        pgconn_set_default_encoding( self );
#endif

        if (rb_block_given_p()) {
                return rb_ensure(rb_yield, self, pgconn_finish, self);
        }
        return self;
}
anchor
parse_connect_args( *args )

Parse the connection args into a connection-parameter string. See ::new for valid arguments.

# File lib/pg/connection.rb, line 34
def self::parse_connect_args( *args )
        return '' if args.empty?

        # This will be swapped soon for code that makes options like those required for
        # PQconnectdbParams()/PQconnectStartParams(). For now, stick to an options string for
        # PQconnectdb()/PQconnectStart().

        # Parameter 'fallback_application_name' was introduced in PostgreSQL 9.0
        # together with PQescapeLiteral().
        if PG::Connection.instance_methods.find{|m| m.to_sym == :escape_literal }
                appname = $0.sub(/^(.{30}).{4,}(.{30})$/){ $1+"..."+$2 }
                appname = PG::Connection.quote_connstr( appname )
                connopts = ["fallback_application_name=#{appname}"]
        else
                connopts = []
        end

        # Handle an options hash first
        if args.last.is_a?( Hash )
                opthash = args.pop
                opthash.each do |key, val|
                        connopts.push( "%s=%s" % [key, PG::Connection.quote_connstr(val)] )
                end
        end

        # Option string style
        if args.length == 1 && args.first.to_s.index( '=' )
                connopts.unshift( args.first )

        # Append positional parameters
        else
                args.each_with_index do |val, i|
                        next unless val # Skip nil placeholders

                        key = CONNECT_ARGUMENT_ORDER[ i ] or
                                raise ArgumentError, "Extra positional parameter %d: %p" % [ i+1, val ]
                        connopts.push( "%s=%s" % [key, PG::Connection.quote_connstr(val.to_s)] )
                end
        end

        return connopts.join(' ')
end
anchor
PG::Connection.ping(connection_hash) → Fixnum
PG::Connection.ping(connection_string) → Fixnum
PG::Connection.ping(host, port, options, tty, dbname, login, password) → Fixnum

Check server status.

Returns one of:

PQPING_OK

server is accepting connections

PQPING_REJECT

server is alive but rejecting connections

PQPING_NO_RESPONSE

could not establish connection

PQPING_NO_ATTEMPT

connection not attempted (bad params)

static VALUE
pgconn_s_ping( int argc, VALUE *argv, VALUE klass )
{
        PGPing ping;
        VALUE conninfo;

        conninfo = rb_funcall2( klass, rb_intern("parse_connect_args"), argc, argv );
        ping     = PQping( StringValuePtr(conninfo) );

        return INT2FIX((int)ping);
}
anchor
quote_connstr( value )

Quote the given value for use in a connection-parameter string.

# File lib/pg/connection.rb, line 27
def self::quote_connstr( value )
        return "'" + value.to_s.gsub( /[\']/ ) {|m| '\' + m } + "'"
end
anchor
quote_ident( str ) → String

Returns a string that is safe for inclusion in a SQL query as an identifier. Note: this is not a quote function for values, but for identifiers.

For example, in a typical SQL query: SELECT FOO FROM MYTABLE The identifier FOO is folded to lower case, so it actually means foo. If you really want to access the case-sensitive field name FOO, use this function like PG::Connection.quote_ident('FOO'), which will return "FOO" (with double-quotes). PostgreSQL will see the double-quotes, and it will not fold to lower case.

Similarly, this function also protects against special characters, and other things that might allow SQL injection if the identifier comes from an untrusted source.

static VALUE
pgconn_s_quote_ident(VALUE self, VALUE in_str)
{
        VALUE ret;
        char *str = StringValuePtr(in_str);
        /* result size at most NAMEDATALEN*2 plus surrounding
         * double-quotes. */
        char buffer[NAMEDATALEN*2+2];
        unsigned int i=0,j=0;
#ifdef M17N_SUPPORTED
        rb_encoding* enc;
#endif

        UNUSED( self );

        if(strlen(str) >= NAMEDATALEN) {
                rb_raise(rb_eArgError,
                        "Input string is longer than NAMEDATALEN-1 (%d)",
                        NAMEDATALEN-1);
        }
        buffer[j++] = '"';
        for(i = 0; i < strlen(str) && str[i]; i++) {
                if(str[i] == '"')
                        buffer[j++] = '"';
                buffer[j++] = str[i];
        }
        buffer[j++] = '"';
        ret = rb_str_new(buffer,j);
        OBJ_INFECT(ret, in_str);

#ifdef M17N_SUPPORTED
        if ( rb_obj_class(self) == rb_cPGconn ) {
                enc = pg_conn_enc_get( pg_get_pgconn(self) );
        } else {
                enc = rb_enc_get(in_str);
        }
        rb_enc_associate(ret, enc);
#endif

        return ret;
}
anchor

Converts an escaped string representation of binary data into binary data — the reverse of escape_bytea. This is needed when retrieving bytea data in text format, but not when retrieving it in binary format.

static VALUE
pgconn_s_unescape_bytea(VALUE self, VALUE str)
{
        unsigned char *from, *to;
        size_t to_len;
        VALUE ret;

        UNUSED( self );

        Check_Type(str, T_STRING);
        from = (unsigned char*)StringValuePtr(str);

        to = PQunescapeBytea(from, &to_len);

        ret = rb_str_new((char*)to, to_len);
        OBJ_INFECT(ret, str);
        PQfreemem(to);
        return ret;
}

Public Instance Methods

anchor
async_exec(sql [, params, result_format ] ) → PG::Result
async_exec(sql [, params, result_format ] ) {|pg_result| block }

This function has the same behavior as exec, but is implemented using the asynchronous command processing API of libpq.

static VALUE
pgconn_async_exec(int argc, VALUE *argv, VALUE self)
{
        VALUE rb_pgresult = Qnil;

        /* remove any remaining results from the queue */
        pgconn_block( 0, NULL, self ); /* wait for input (without blocking) before reading the last result */
        pgconn_get_last_result( self );

        pgconn_send_query( argc, argv, self );
        pgconn_block( 0, NULL, self );
        rb_pgresult = pgconn_get_last_result( self );

        if ( rb_block_given_p() ) {
                return rb_ensure( rb_yield, rb_pgresult, pg_result_clear, rb_pgresult );
        }
        return rb_pgresult;
}
Also aliased as: async_query
anchor
async_query(*args)
Alias for: async_exec
anchor
backend_pid() → Fixnum

Returns the process ID of the backend server process for this connection. Note that this is a PID on database server host.

static VALUE
pgconn_backend_pid(VALUE self)
{
        return INT2NUM(PQbackendPID(pg_get_pgconn(self)));
}
anchor
block( [ timeout ] ) → Boolean

Blocks until the server is no longer busy, or until the optional timeout is reached, whichever comes first. timeout is measured in seconds and can be fractional.

Returns false if timeout is reached, true otherwise.

If true is returned, conn.is_busy will return false and conn.get_result will not block.

static VALUE
pgconn_block( int argc, VALUE *argv, VALUE self ) {
        PGconn *conn = pg_get_pgconn( self );

        /* If WIN32 and Ruby 1.9 do not use rb_thread_select() which sometimes hangs
         * and does not wait (nor sleep) any time even if timeout is given.
         * Instead use the Winsock events and rb_w32_wait_events(). */

        struct timeval timeout;
        struct timeval *ptimeout = NULL;
        VALUE timeout_in;
        double timeout_sec;
        void *ret;

        if ( rb_scan_args(argc, argv, "01", &timeout_in) == 1 ) {
                timeout_sec = NUM2DBL( timeout_in );
                timeout.tv_sec = (time_t)timeout_sec;
                timeout.tv_usec = (suseconds_t)((timeout_sec - (long)timeout_sec) * 1e6);
                ptimeout = &timeout;
        }

        ret = wait_socket_readable( conn, ptimeout, get_result_readable);

        if( !ret )
                return Qfalse;

        return Qtrue;
}
anchor
cancel() → String

Requests cancellation of the command currently being processed. (Only implemented in PostgreSQL >= 8.0)

Returns nil on success, or a string containing the error message if a failure occurs.

static VALUE
pgconn_cancel(VALUE self)
{
#ifdef HAVE_PQGETCANCEL
        char errbuf[256];
        PGcancel *cancel;
        VALUE retval;
        int ret;

        cancel = PQgetCancel(pg_get_pgconn(self));
        if(cancel == NULL)
                rb_raise(rb_ePGerror,"Invalid connection!");

        ret = gvl_PQcancel(cancel, errbuf, 256);
        if(ret == 1)
                retval = Qnil;
        else
                retval = rb_str_new2(errbuf);

        PQfreeCancel(cancel);
        return retval;
#else
        rb_notimplement();
#endif
}
anchor
client_encoding=(p1)
Alias for: set_client_encoding
anchor
close()
Alias for: finish
anchor
conndefaults()

Returns an array of Hashes with connection defaults. See ::conndefaults for details.

# File lib/pg/connection.rb, line 171
def conndefaults
        return self.class.conndefaults
end
anchor
connect_poll() → Fixnum

Returns one of:

PGRES_POLLING_READING

wait until the socket is ready to read

PGRES_POLLING_WRITING

wait until the socket is ready to write

PGRES_POLLING_FAILED

the asynchronous connection has failed

PGRES_POLLING_OK

the asynchronous connection is ready

Example:

conn = PG::Connection.connect_start("dbname=mydatabase")
socket = conn.socket_io
status = conn.connect_poll
while(status != PG::PGRES_POLLING_OK) do
  # do some work while waiting for the connection to complete
  if(status == PG::PGRES_POLLING_READING)
    if(not select([socket], [], [], 10.0))
      raise "Asynchronous connection timed out!"
    end
  elsif(status == PG::PGRES_POLLING_WRITING)
    if(not select([], [socket], [], 10.0))
      raise "Asynchronous connection timed out!"
    end
  end
  status = conn.connect_poll
end
# now conn.status == CONNECTION_OK, and connection
# is ready.
static VALUE
pgconn_connect_poll(VALUE self)
{
        PostgresPollingStatusType status;
        status = gvl_PQconnectPoll(pg_get_pgconn(self));
        return INT2FIX((int)status);
}
anchor
connection_needs_password() → Boolean

Returns true if the authentication method required a password, but none was available. false otherwise.

static VALUE
pgconn_connection_needs_password(VALUE self)
{
        return PQconnectionNeedsPassword(pg_get_pgconn(self)) ? Qtrue : Qfalse;
}
anchor
connection_used_password() → Boolean

Returns true if the authentication method used a caller-supplied password, false otherwise.

static VALUE
pgconn_connection_used_password(VALUE self)
{
        return PQconnectionUsedPassword(pg_get_pgconn(self)) ? Qtrue : Qfalse;
}
anchor
consume_input()

If input is available from the server, consume it. After calling consume_input, you can check is_busy or notifies to see if the state has changed.

static VALUE
pgconn_consume_input(self)
        VALUE self;
{
        VALUE error;
        PGconn *conn = pg_get_pgconn(self);
        /* returns 0 on error */
        if(PQconsumeInput(conn) == 0) {
                error = rb_exc_new2(rb_eConnectionBad, PQerrorMessage(conn));
                rb_iv_set(error, "@connection", self);
                rb_exc_raise(error);
        }
        return Qnil;
}
anchor
copy_data( sql ) {|sql_result| ... } → PG::Result

Execute a copy process for transfering data to or from the server.

This issues the SQL COPY command via exec. The response to this (if there is no error in the command) is a PG::Result object that is passed to the block, bearing a status code of PGRES_COPY_OUT or PGRES_COPY_IN (depending on the specified copy direction). The application should then use put_copy_data or get_copy_data to receive or transmit data rows and should return from the block when finished.

copy_data returns another PG::Result object when the data transfer is complete. An exception is raised if some problem was encountered, so it isn't required to make use of any of them. At this point further SQL commands can be issued via exec. (It is not possible to execute other SQL commands using the same connection while the COPY operation is in progress.)

This method ensures, that the copy process is properly terminated in case of client side or server side failures. Therefore, in case of blocking mode of operation, copy_data is preferred to raw calls of put_copy_data, get_copy_data and put_copy_end.

Example with CSV input format:

conn.exec "create table my_table (a text,b text,c text,d text,e text)"
conn.copy_data "COPY my_table FROM STDOUT CSV" do
  conn.put_copy_data "some,csv,data,to,copy\n"
  conn.put_copy_data "more,csv,data,to,copy\n"
end

This creates my_table and inserts two rows.

Example with CSV output format:

conn.copy_data "COPY my_table TO STDOUT CSV" do
  while row=conn.get_copy_data
    p row
  end
end

This prints all rows of my_table to stdout:

"some,csv,data,to,copy\n"
"more,csv,data,to,copy\n"
# File lib/pg/connection.rb, line 119
def copy_data( sql )
        res = exec( sql )

        case res.result_status
        when PGRES_COPY_IN
                begin
                        yield res
                rescue Exception => err
                        errmsg = "%s while copy data: %s" % [ err.class.name, err.message ]
                        put_copy_end( errmsg )
                        get_result
                        raise
                else
                        put_copy_end
                        get_last_result
                end

        when PGRES_COPY_OUT
                begin
                        yield res
                rescue Exception => err
                        cancel
                        while get_copy_data
                        end
                        while get_result
                        end
                        raise
                else
                        res = get_last_result
                        if res.result_status != PGRES_COMMAND_OK
                                while get_copy_data
                                end
                                while get_result
                                end
                                raise PG::NotAllCopyDataRetrieved, "Not all COPY data retrieved"
                        end
                        res
                end

        else
                raise ArgumentError, "SQL command is no COPY statement: #{sql}"
        end
end
anchor
db()

Returns the connected database name.

static VALUE
pgconn_db(VALUE self)
{
        char *db = PQdb(pg_get_pgconn(self));
        if (!db) return Qnil;
        return rb_tainted_str_new2(db);
}
anchor
describe_portal( portal_name ) → PG::Result

Retrieve information about the portal portal_name.

static VALUE
pgconn_describe_portal(self, stmt_name)
        VALUE self, stmt_name;
{
        PGresult *result;
        VALUE rb_pgresult;
        PGconn *conn = pg_get_pgconn(self);
        char *stmt;
        if(stmt_name == Qnil) {
                stmt = NULL;
        }
        else {
                Check_Type(stmt_name, T_STRING);
                stmt = StringValuePtr(stmt_name);
        }
        result = gvl_PQdescribePortal(conn, stmt);
        rb_pgresult = pg_new_result(result, self);
        pg_result_check(rb_pgresult);
        return rb_pgresult;
}
anchor
describe_prepared( statement_name ) → PG::Result

Retrieve information about the prepared statement statement_name.

static VALUE
pgconn_describe_prepared(VALUE self, VALUE stmt_name)
{
        PGresult *result;
        VALUE rb_pgresult;
        PGconn *conn = pg_get_pgconn(self);
        char *stmt;
        if(stmt_name == Qnil) {
                stmt = NULL;
        }
        else {
                Check_Type(stmt_name, T_STRING);
                stmt = StringValuePtr(stmt_name);
        }
        result = gvl_PQdescribePrepared(conn, stmt);
        rb_pgresult = pg_new_result(result, self);
        pg_result_check(rb_pgresult);
        return rb_pgresult;
}
anchor
error_message → String

Returns the error message about connection.

static VALUE
pgconn_error_message(VALUE self)
{
        char *error = PQerrorMessage(pg_get_pgconn(self));
        if (!error) return Qnil;
        return rb_tainted_str_new2(error);
}
anchor
escape(p1)
Alias for: escape_string
anchor
escape_bytea( string ) → String

Connection instance method for versions of 8.1 and higher of libpq uses PQescapeByteaConn, which is safer. Avoid calling as a class method, the class method uses the deprecated PQescapeBytea() API function.

Use the instance method version of this function, it is safer than the class method.

Escapes binary data for use within an SQL command with the type bytea.

Certain byte values must be escaped (but all byte values may be escaped) when used as part of a bytea literal in an SQL statement. In general, to escape a byte, it is converted into the three digit octal number equal to the octet value, and preceded by two backslashes. The single quote (') and backslash () characters have special alternative escape sequences. escape_bytea performs this operation, escaping only the minimally required bytes.

Consider using #exec_params, which avoids the need for passing values inside of SQL commands.

static VALUE
pgconn_s_escape_bytea(VALUE self, VALUE str)
{
        unsigned char *from, *to;
        size_t from_len, to_len;
        VALUE ret;

        Check_Type(str, T_STRING);
        from      = (unsigned char*)RSTRING_PTR(str);
        from_len  = RSTRING_LEN(str);

        if(rb_obj_class(self) == rb_cPGconn) {
                to = PQescapeByteaConn(pg_get_pgconn(self), from, from_len, &to_len);
        } else {
                to = PQescapeBytea( from, from_len, &to_len);
        }

        ret = rb_str_new((char*)to, to_len - 1);
        OBJ_INFECT(ret, str);
        PQfreemem(to);
        return ret;
}
anchor
escape_identifier( str ) → String

Escape an arbitrary String str as an identifier.

This method does the same as quote_ident, but uses libpq to process the string.

static VALUE
pgconn_escape_identifier(VALUE self, VALUE string)
{
        PGconn *conn = pg_get_pgconn(self);
        char *escaped = NULL;
        VALUE error;
        VALUE result = Qnil;

        Check_Type(string, T_STRING);

        escaped = PQescapeIdentifier(conn, RSTRING_PTR(string), RSTRING_LEN(string));
        if (escaped == NULL)
        {
                error = rb_exc_new2(rb_ePGerror, PQerrorMessage(conn));
                rb_iv_set(error, "@connection", self);
                rb_exc_raise(error);
                return Qnil;
        }
        result = rb_str_new2(escaped);
        PQfreemem(escaped);
        OBJ_INFECT(result, string);

#ifdef M17N_SUPPORTED
        rb_enc_associate(result, pg_conn_enc_get( pg_get_pgconn(self) ));
#endif

        return result;
}
anchor
escape_literal( str ) → String

Escape an arbitrary String str as a literal.

static VALUE
pgconn_escape_literal(VALUE self, VALUE string)
{
        PGconn *conn = pg_get_pgconn(self);
        char *escaped = NULL;
        VALUE error;
        VALUE result = Qnil;

        Check_Type(string, T_STRING);

        escaped = PQescapeLiteral(conn, RSTRING_PTR(string), RSTRING_LEN(string));
        if (escaped == NULL)
        {
                error = rb_exc_new2(rb_ePGerror, PQerrorMessage(conn));
                rb_iv_set(error, "@connection", self);
                rb_exc_raise(error);
                return Qnil;
        }
        result = rb_str_new2(escaped);
        PQfreemem(escaped);
        OBJ_INFECT(result, string);

#ifdef M17N_SUPPORTED
        rb_enc_associate(result, pg_conn_enc_get( pg_get_pgconn(self) ));
#endif

        return result;
}
anchor
escape_string( str ) → String

Connection instance method for versions of 8.1 and higher of libpq uses PQescapeStringConn, which is safer. Avoid calling as a class method, the class method uses the deprecated PQescapeString() API function.

Returns a SQL-safe version of the String str. This is the preferred way to make strings safe for inclusion in SQL queries.

Consider using #exec_params, which avoids the need for passing values inside of SQL commands.

Encoding of escaped string will be equal to client encoding of connection.

static VALUE
pgconn_s_escape(VALUE self, VALUE string)
{
        char *escaped;
        size_t size;
        int error;
        VALUE result;
#ifdef M17N_SUPPORTED
        rb_encoding* enc;
#endif

        Check_Type(string, T_STRING);

        escaped = ALLOC_N(char, RSTRING_LEN(string) * 2 + 1);
        if(rb_obj_class(self) == rb_cPGconn) {
                size = PQescapeStringConn(pg_get_pgconn(self), escaped,
                        RSTRING_PTR(string), RSTRING_LEN(string), &error);
                if(error) {
                        xfree(escaped);
                        rb_raise(rb_ePGerror, "%s", PQerrorMessage(pg_get_pgconn(self)));
                }
        } else {
                size = PQescapeString(escaped, RSTRING_PTR(string), (int)RSTRING_LEN(string));
        }
        result = rb_str_new(escaped, size);
        xfree(escaped);
        OBJ_INFECT(result, string);

#ifdef M17N_SUPPORTED
        if ( rb_obj_class(self) == rb_cPGconn ) {
                enc = pg_conn_enc_get( pg_get_pgconn(self) );
        } else {
                enc = rb_enc_get(string);
        }
        rb_enc_associate(result, enc);
#endif

        return result;
}
Also aliased as: escape
anchor
exec(sql) → PG::Result
exec(sql) {|pg_result| block }

Sends SQL query request specified by sql to PostgreSQL. Returns a PG::Result instance on success. On failure, it raises a PG::Error.

For backward compatibility, if you pass more than one parameter to this method, it will call exec_params for you. New code should explicitly use exec_params if argument placeholders are used.

If the optional code block is given, it will be passed result as an argument, and the PG::Result object will automatically be cleared when the block terminates. In this instance, conn.exec returns the value of the block.

exec is implemented on the synchronous command processing API of libpq, whereas async_exec is implemented on the asynchronous API. exec is somewhat faster that async_exec, but blocks any signals to be processed until the query is finished. This is most notably visible by a delayed reaction to Control+C. Both methods ensure that other threads can process while waiting for the server to complete the request.

static VALUE
pgconn_exec(int argc, VALUE *argv, VALUE self)
{
        PGconn *conn = pg_get_pgconn(self);
        PGresult *result = NULL;
        VALUE rb_pgresult;

        /* If called with no parameters, use PQexec */
        if ( argc == 1 ) {
                Check_Type(argv[0], T_STRING);

                result = gvl_PQexec(conn, StringValuePtr(argv[0]));
                rb_pgresult = pg_new_result(result, self);
                pg_result_check(rb_pgresult);
                if (rb_block_given_p()) {
                        return rb_ensure(rb_yield, rb_pgresult, pg_result_clear, rb_pgresult);
                }
                return rb_pgresult;
        }

        /* Otherwise, just call #exec_params instead for backward-compatibility */
        else {
                return pgconn_exec_params( argc, argv, self );
        }

}
Also aliased as: query
anchor
exec_params(sql, params[, result_format ] ) → PG::Result
exec_params(sql, params[, result_format ] ) {|pg_result| block }

Sends SQL query request specified by sql to PostgreSQL using placeholders for parameters.

Returns a PG::Result instance on success. On failure, it raises a PG::Error.

params is an array of the bind parameters for the SQL query. Each element of the params array may be either:

a hash of the form:
  {:value  => String (value of bind parameter)
   :type   => Fixnum (oid of type of bind parameter)
   :format => Fixnum (0 for text, 1 for binary)
  }
or, it may be a String. If it is a string, that is equivalent to the hash:
  { :value => <string value>, :type => 0, :format => 0 }

PostgreSQL bind parameters are represented as $1, $1, $2, etc., inside the SQL query. The 0th element of the params array is bound to $1, the 1st element is bound to $2, etc. nil is treated as NULL.

If the types are not specified, they will be inferred by PostgreSQL. Instead of specifying type oids, it's recommended to simply add explicit casts in the query to ensure that the right type is used.

For example: “SELECT $1::int”

The optional result_format should be 0 for text results, 1 for binary.

If the optional code block is given, it will be passed result as an argument, and the PG::Result object will automatically be cleared when the block terminates. In this instance, conn.exec returns the value of the block.

static VALUE
pgconn_exec_params( int argc, VALUE *argv, VALUE self )
{
        PGconn *conn = pg_get_pgconn(self);
        PGresult *result = NULL;
        VALUE rb_pgresult;
        VALUE command, params, in_res_fmt;
        VALUE param, param_type, param_value, param_format;
        VALUE param_value_tmp;
        VALUE sym_type, sym_value, sym_format;
        VALUE gc_array;
        int i=0;
        int nParams;
        Oid *paramTypes;
        char ** paramValues;
        int *paramLengths;
        int *paramFormats;
        int resultFormat;

        rb_scan_args(argc, argv, "12", &command, &params, &in_res_fmt);

        /*
         * Handle the edge-case where the caller is coming from #exec, but passed an explict +nil+
         * for the second parameter.
         */
        if ( NIL_P(params) ) {
                return pgconn_exec( 1, argv, self );
                }

        Check_Type(params, T_ARRAY);

        if ( NIL_P(in_res_fmt) ) {
                resultFormat = 0;
        }
        else {
                resultFormat = NUM2INT(in_res_fmt);
        }

        gc_array = rb_ary_new();
        rb_gc_register_address(&gc_array);

        sym_type = ID2SYM(rb_intern("type"));
        sym_value = ID2SYM(rb_intern("value"));
        sym_format = ID2SYM(rb_intern("format"));
        nParams = (int)RARRAY_LEN(params);
        paramTypes = ALLOC_N(Oid, nParams);
        paramValues = ALLOC_N(char *, nParams);
        paramLengths = ALLOC_N(int, nParams);
        paramFormats = ALLOC_N(int, nParams);

        for ( i = 0; i < nParams; i++ ) {
                param = rb_ary_entry(params, i);
                if (TYPE(param) == T_HASH) {
                        param_type = rb_hash_aref(param, sym_type);
                        param_value_tmp = rb_hash_aref(param, sym_value);
                        if(param_value_tmp == Qnil)
                                param_value = param_value_tmp;
                        else
                                param_value = rb_obj_as_string(param_value_tmp);
                        param_format = rb_hash_aref(param, sym_format);
                }
                else {
                        param_type = Qnil;
                        if(param == Qnil)
                                param_value = param;
                        else
                                param_value = rb_obj_as_string(param);
                        param_format = Qnil;
                }

                if(param_type == Qnil)
                        paramTypes[i] = 0;
                else
                        paramTypes[i] = NUM2INT(param_type);

                if(param_value == Qnil) {
                        paramValues[i] = NULL;
                        paramLengths[i] = 0;
                }
                else {
                        Check_Type(param_value, T_STRING);
                        /* make sure param_value doesn't get freed by the GC */
                        rb_ary_push(gc_array, param_value);
                        paramValues[i] = StringValuePtr(param_value);
                        paramLengths[i] = (int)RSTRING_LEN(param_value);
                }

                if(param_format == Qnil)
                        paramFormats[i] = 0;
                else
                        paramFormats[i] = NUM2INT(param_format);
        }

        result = gvl_PQexecParams(conn, StringValuePtr(command), nParams, paramTypes,
                (const char * const *)paramValues, paramLengths, paramFormats, resultFormat);

        rb_gc_unregister_address(&gc_array);

        xfree(paramTypes);
        xfree(paramValues);
        xfree(paramLengths);
        xfree(paramFormats);

        rb_pgresult = pg_new_result(result, self);
        pg_result_check(rb_pgresult);

        if (rb_block_given_p()) {
                return rb_ensure(rb_yield, rb_pgresult, pg_result_clear, rb_pgresult);
        }

        return rb_pgresult;
}
anchor
exec_prepared(statement_name [, params, result_format ] ) → PG::Result
exec_prepared(statement_name [, params, result_format ] ) {|pg_result| block }

Execute prepared named statement specified by statement_name. Returns a PG::Result instance on success. On failure, it raises a PG::Error.

params is an array of the optional bind parameters for the SQL query. Each element of the params array may be either:

a hash of the form:
  {:value  => String (value of bind parameter)
   :format => Fixnum (0 for text, 1 for binary)
  }
or, it may be a String. If it is a string, that is equivalent to the hash:
  { :value => <string value>, :format => 0 }

PostgreSQL bind parameters are represented as $1, $1, $2, etc., inside the SQL query. The 0th element of the params array is bound to $1, the 1st element is bound to $2, etc. nil is treated as NULL.

The optional result_format should be 0 for text results, 1 for binary.

If the optional code block is given, it will be passed result as an argument, and the PG::Result object will automatically be cleared when the block terminates. In this instance, conn.exec_prepared returns the value of the block.

static VALUE
pgconn_exec_prepared(int argc, VALUE *argv, VALUE self)
{
        PGconn *conn = pg_get_pgconn(self);
        PGresult *result = NULL;
        VALUE rb_pgresult;
        VALUE name, params, in_res_fmt;
        VALUE param, param_value, param_format;
        VALUE param_value_tmp;
        VALUE sym_value, sym_format;
        VALUE gc_array;
        int i = 0;
        int nParams;
        char ** paramValues;
        int *paramLengths;
        int *paramFormats;
        int resultFormat;


        rb_scan_args(argc, argv, "12", &name, &params, &in_res_fmt);
        Check_Type(name, T_STRING);

        if(NIL_P(params)) {
                params = rb_ary_new2(0);
                resultFormat = 0;
        }
        else {
                Check_Type(params, T_ARRAY);
        }

        if(NIL_P(in_res_fmt)) {
                resultFormat = 0;
        }
        else {
                resultFormat = NUM2INT(in_res_fmt);
        }

        gc_array = rb_ary_new();
        rb_gc_register_address(&gc_array);
        sym_value = ID2SYM(rb_intern("value"));
        sym_format = ID2SYM(rb_intern("format"));
        nParams = (int)RARRAY_LEN(params);
        paramValues = ALLOC_N(char *, nParams);
        paramLengths = ALLOC_N(int, nParams);
        paramFormats = ALLOC_N(int, nParams);
        for(i = 0; i < nParams; i++) {
                param = rb_ary_entry(params, i);
                if (TYPE(param) == T_HASH) {
                        param_value_tmp = rb_hash_aref(param, sym_value);
                        if(param_value_tmp == Qnil)
                                param_value = param_value_tmp;
                        else
                                param_value = rb_obj_as_string(param_value_tmp);
                        param_format = rb_hash_aref(param, sym_format);
                }
                else {
                        if(param == Qnil)
                                param_value = param;
                        else
                                param_value = rb_obj_as_string(param);
                        param_format = INT2NUM(0);
                }
                if(param_value == Qnil) {
                        paramValues[i] = NULL;
                        paramLengths[i] = 0;
                }
                else {
                        Check_Type(param_value, T_STRING);
                        /* make sure param_value doesn't get freed by the GC */
                        rb_ary_push(gc_array, param_value);
                        paramValues[i] = StringValuePtr(param_value);
                        paramLengths[i] = (int)RSTRING_LEN(param_value);
                }

                if(param_format == Qnil)
                        paramFormats[i] = 0;
                else
                        paramFormats[i] = NUM2INT(param_format);
        }

        result = gvl_PQexecPrepared(conn, StringValuePtr(name), nParams,
                (const char * const *)paramValues, paramLengths, paramFormats,
                resultFormat);

        rb_gc_unregister_address(&gc_array);

        xfree(paramValues);
        xfree(paramLengths);
        xfree(paramFormats);

        rb_pgresult = pg_new_result(result, self);
        pg_result_check(rb_pgresult);
        if (rb_block_given_p()) {
                return rb_ensure(rb_yield, rb_pgresult,
                        pg_result_clear, rb_pgresult);
        }
        return rb_pgresult;
}
anchor
external_encoding() → Encoding

Return the server_encoding of the connected database as a Ruby Encoding object. The SQL_ASCII encoding is mapped to to ASCII_8BIT.

static VALUE
pgconn_external_encoding(VALUE self)
{
        PGconn *conn = pg_get_pgconn( self );
        VALUE encoding = rb_iv_get( self, "@external_encoding" );
        rb_encoding *enc = NULL;
        const char *pg_encname = NULL;

        /* Use cached value if found */
        if ( RTEST(encoding) ) return encoding;

        pg_encname = PQparameterStatus( conn, "server_encoding" );
        enc = pg_get_pg_encname_as_rb_encoding( pg_encname );
        encoding = rb_enc_from_encoding( enc );

        rb_iv_set( self, "@external_encoding", encoding );

        return encoding;
}
anchor
finish

Closes the backend connection.

static VALUE
pgconn_finish( VALUE self )
{
        pgconn_close_socket_io( self );
        PQfinish( pg_get_pgconn(self) );
        DATA_PTR( self ) = NULL;
        return Qnil;
}
Also aliased as: close
anchor
finished? → boolean

Returns true if the backend connection has been closed.

static VALUE
pgconn_finished_p( VALUE self )
{
        if ( DATA_PTR(self) ) return Qfalse;
        return Qtrue;
}
anchor
flush() → Boolean

Attempts to flush any queued output data to the server. Returns true if data is successfully flushed, false if not (can only return false if connection is nonblocking. Raises PG::Error if some other failure occurred.

static VALUE
pgconn_flush(self)
        VALUE self;
{
        PGconn *conn = pg_get_pgconn(self);
        int ret;
        VALUE error;
        ret = PQflush(conn);
        if(ret == -1) {
                error = rb_exc_new2(rb_ePGerror, PQerrorMessage(conn));
                rb_iv_set(error, "@connection", self);
                rb_exc_raise(error);
        }
        return (ret) ? Qfalse : Qtrue;
}
anchor
get_client_encoding() → String

Returns the client encoding as a String.

static VALUE
pgconn_get_client_encoding(VALUE self)
{
        char *encoding = (char *)pg_encoding_to_char(PQclientEncoding(pg_get_pgconn(self)));
        return rb_tainted_str_new2(encoding);
}
anchor
get_copy_data( [ async = false ] ) → String

Return a string containing one row of data, nil if the copy is done, or false if the call would block (only possible if async is true).

See also copy_data.

static VALUE
pgconn_get_copy_data(int argc, VALUE *argv, VALUE self )
{
        VALUE async_in;
        VALUE error;
        VALUE result_str;
        int ret;
        int async;
        char *buffer;
        PGconn *conn = pg_get_pgconn(self);

        if (rb_scan_args(argc, argv, "01", &async_in) == 0)
                async = 0;
        else
                async = (async_in == Qfalse || async_in == Qnil) ? 0 : 1;

        ret = gvl_PQgetCopyData(conn, &buffer, async);
        if(ret == -2) { /* error */
                error = rb_exc_new2(rb_ePGerror, PQerrorMessage(conn));
                rb_iv_set(error, "@connection", self);
                rb_exc_raise(error);
        }
        if(ret == -1) { /* No data left */
                return Qnil;
        }
        if(ret == 0) { /* would block */
                return Qfalse;
        }
        result_str = rb_tainted_str_new(buffer, ret);
        PQfreemem(buffer);
        return result_str;
}
anchor
get_last_result( ) → PG::Result

This function retrieves all available results on the current connection (from previously issued asynchronous commands like +send_query()+) and returns the last non-NULL result, or nil if no results are available.

This function is similar to get_result except that it is designed to get one and only one result.

static VALUE
pgconn_get_last_result(VALUE self)
{
        PGconn *conn = pg_get_pgconn(self);
        VALUE rb_pgresult = Qnil;
        PGresult *cur, *prev;


        cur = prev = NULL;
        while ((cur = gvl_PQgetResult(conn)) != NULL) {
                int status;

                if (prev) PQclear(prev);
                prev = cur;

                status = PQresultStatus(cur);
                if (status == PGRES_COPY_OUT || status == PGRES_COPY_IN)
                        break;
        }

        if (prev) {
                rb_pgresult = pg_new_result( prev, self );
                pg_result_check(rb_pgresult);
        }

        return rb_pgresult;
}
anchor
get_result() → PG::Result
get_result() {|pg_result| block }

Blocks waiting for the next result from a call to send_query (or another asynchronous command), and returns it. Returns nil if no more results are available.

Note: call this function repeatedly until it returns nil, or else you will not be able to issue further commands.

If the optional code block is given, it will be passed result as an argument, and the PG::Result object will automatically be cleared when the block terminates. In this instance, conn.exec returns the value of the block.

static VALUE
pgconn_get_result(VALUE self)
{
        PGconn *conn = pg_get_pgconn(self);
        PGresult *result;
        VALUE rb_pgresult;

        result = gvl_PQgetResult(conn);
        if(result == NULL)
                return Qnil;
        rb_pgresult = pg_new_result(result, self);
        if (rb_block_given_p()) {
                return rb_ensure(rb_yield, rb_pgresult,
                        pg_result_clear, rb_pgresult);
        }
        return rb_pgresult;
}
anchor
host()

Returns the connected server name.

static VALUE
pgconn_host(VALUE self)
{
        char *host = PQhost(pg_get_pgconn(self));
        if (!host) return Qnil;
        return rb_tainted_str_new2(host);
}
anchor
internal_encoding → Encoding

defined in Ruby 1.9 or later.

Returns:

  • an Encoding - client_encoding of the connection as a Ruby Encoding object.

  • nil - the client_encoding is 'SQL_ASCII'

static VALUE
pgconn_internal_encoding(VALUE self)
{
        PGconn *conn = pg_get_pgconn( self );
        rb_encoding *enc = pg_conn_enc_get( conn );

        if ( enc ) {
                return rb_enc_from_encoding( enc );
        } else {
                return Qnil;
        }
}
anchor
internal_encoding = value

A wrapper of set_client_encoding. defined in Ruby 1.9 or later.

value can be one of:

  • an Encoding

  • a String - a name of Encoding

  • nil - sets the client_encoding to SQL_ASCII.

static VALUE
pgconn_internal_encoding_set(VALUE self, VALUE enc)
{
        if (NIL_P(enc)) {
                pgconn_set_client_encoding( self, rb_usascii_str_new_cstr("SQL_ASCII") );
                return enc;
        }
        else if ( TYPE(enc) == T_STRING && strcasecmp("JOHAB", RSTRING_PTR(enc)) == 0 ) {
                pgconn_set_client_encoding(self, rb_usascii_str_new_cstr("JOHAB"));
                return enc;
        }
        else {
                rb_encoding *rbenc = rb_to_encoding( enc );
                const char *name = pg_get_rb_encoding_as_pg_encoding( rbenc );

                if ( PQsetClientEncoding(pg_get_pgconn( self ), name) == -1 ) {
                        VALUE server_encoding = pgconn_external_encoding( self );
                        rb_raise( rb_eEncCompatError, "incompatible character encodings: %s and %s",
                                          rb_enc_name(rb_to_encoding(server_encoding)), name );
                }
                return enc;
        }

        rb_raise( rb_ePGerror, "unknown encoding: %s", RSTRING_PTR(rb_inspect(enc)) );

        return Qnil;
}
anchor
is_busy() → Boolean

Returns true if a command is busy, that is, if PQgetResult would block. Otherwise returns false.

static VALUE
pgconn_is_busy(self)
        VALUE self;
{
        return gvl_PQisBusy(pg_get_pgconn(self)) ? Qtrue : Qfalse;
}
anchor
isnonblocking() → Boolean

Returns true if a command is busy, that is, if PQgetResult would block. Otherwise returns false.

static VALUE
pgconn_isnonblocking(self)
        VALUE self;
{
        return PQisnonblocking(pg_get_pgconn(self)) ? Qtrue : Qfalse;
}
Also aliased as: nonblocking?
anchor
lo_close( lo_desc ) → nil

Closes the postgres large object of lo_desc.

static VALUE
pgconn_loclose(VALUE self, VALUE in_lo_desc)
{
        PGconn *conn = pg_get_pgconn(self);
        int lo_desc = NUM2INT(in_lo_desc);

        if(lo_close(conn,lo_desc) < 0)
                rb_raise(rb_ePGerror,"lo_close failed");

        return Qnil;
}
Also aliased as: loclose
anchor
lo_creat( [mode] ) → Fixnum

Creates a large object with mode mode. Returns a large object Oid. On failure, it raises PG::Error.

static VALUE
pgconn_locreat(int argc, VALUE *argv, VALUE self)
{
        Oid lo_oid;
        int mode;
        VALUE nmode;
        PGconn *conn = pg_get_pgconn(self);

        if (rb_scan_args(argc, argv, "01", &nmode) == 0)
                mode = INV_READ;
        else
                mode = NUM2INT(nmode);

        lo_oid = lo_creat(conn, mode);
        if (lo_oid == 0)
                rb_raise(rb_ePGerror, "lo_creat failed");

        return INT2FIX(lo_oid);
}
Also aliased as: locreat
anchor
lo_create( oid ) → Fixnum

Creates a large object with oid oid. Returns the large object Oid. On failure, it raises PG::Error.

static VALUE
pgconn_locreate(VALUE self, VALUE in_lo_oid)
{
        Oid ret, lo_oid;
        PGconn *conn = pg_get_pgconn(self);
        lo_oid = NUM2INT(in_lo_oid);

        ret = lo_create(conn, lo_oid);
        if (ret == InvalidOid)
                rb_raise(rb_ePGerror, "lo_create failed");

        return INT2FIX(ret);
}
Also aliased as: locreate
anchor
lo_export( oid, file ) → nil

Saves a large object of oid to a file.

static VALUE
pgconn_loexport(VALUE self, VALUE lo_oid, VALUE filename)
{
        PGconn *conn = pg_get_pgconn(self);
        int oid;
        Check_Type(filename, T_STRING);

        oid = NUM2INT(lo_oid);
        if (oid < 0) {
                rb_raise(rb_ePGerror, "invalid large object oid %d",oid);
        }

        if (lo_export(conn, oid, StringValuePtr(filename)) < 0) {
                rb_raise(rb_ePGerror, "%s", PQerrorMessage(conn));
        }
        return Qnil;
}
Also aliased as: loexport
anchor
lo_import(file) → Fixnum

Import a file to a large object. Returns a large object Oid.

On failure, it raises a PG::Error.

static VALUE
pgconn_loimport(VALUE self, VALUE filename)
{
        Oid lo_oid;

        PGconn *conn = pg_get_pgconn(self);

        Check_Type(filename, T_STRING);

        lo_oid = lo_import(conn, StringValuePtr(filename));
        if (lo_oid == 0) {
                rb_raise(rb_ePGerror, "%s", PQerrorMessage(conn));
        }
        return INT2FIX(lo_oid);
}
Also aliased as: loimport
anchor
lo_lseek( lo_desc, offset, whence ) → Fixnum

Move the large object pointer lo_desc to offset offset. Valid values for whence are SEEK_SET, SEEK_CUR, and SEEK_END. (Or 0, 1, or 2.)

static VALUE
pgconn_lolseek(VALUE self, VALUE in_lo_desc, VALUE offset, VALUE whence)
{
        PGconn *conn = pg_get_pgconn(self);
        int lo_desc = NUM2INT(in_lo_desc);
        int ret;

        if((ret = lo_lseek(conn, lo_desc, NUM2INT(offset), NUM2INT(whence))) < 0) {
                rb_raise(rb_ePGerror, "lo_lseek failed");
        }

        return INT2FIX(ret);
}
Also aliased as: lolseek , lo_seek , loseek
anchor
lo_open( oid, [mode] ) → Fixnum

Open a large object of oid. Returns a large object descriptor instance on success. The mode argument specifies the mode for the opened large object,which is either INV_READ, or INV_WRITE.

If mode is omitted, the default is INV_READ.

static VALUE
pgconn_loopen(int argc, VALUE *argv, VALUE self)
{
        Oid lo_oid;
        int fd, mode;
        VALUE nmode, selfid;
        PGconn *conn = pg_get_pgconn(self);

        rb_scan_args(argc, argv, "11", &selfid, &nmode);
        lo_oid = NUM2INT(selfid);
        if(NIL_P(nmode))
                mode = INV_READ;
        else
                mode = NUM2INT(nmode);

        if((fd = lo_open(conn, lo_oid, mode)) < 0) {
                rb_raise(rb_ePGerror, "can't open large object: %s", PQerrorMessage(conn));
        }
        return INT2FIX(fd);
}
Also aliased as: loopen
anchor
lo_read( lo_desc, len ) → String

Attempts to read len bytes from large object lo_desc, returns resulting data.

static VALUE
pgconn_loread(VALUE self, VALUE in_lo_desc, VALUE in_len)
{
        int ret;
  PGconn *conn = pg_get_pgconn(self);
        int len = NUM2INT(in_len);
        int lo_desc = NUM2INT(in_lo_desc);
        VALUE str;
        char *buffer;

  buffer = ALLOC_N(char, len);
        if(buffer == NULL)
                rb_raise(rb_eNoMemError, "ALLOC failed!");

        if (len < 0){
                rb_raise(rb_ePGerror,"nagative length %d given", len);
        }

        if((ret = lo_read(conn, lo_desc, buffer, len)) < 0)
                rb_raise(rb_ePGerror, "lo_read failed");

        if(ret == 0) {
                xfree(buffer);
                return Qnil;
        }

        str = rb_tainted_str_new(buffer, ret);
        xfree(buffer);

        return str;
}
Also aliased as: loread
anchor
lo_seek(p1, p2, p3)
Alias for: lo_lseek
anchor
lo_tell( lo_desc ) → Fixnum

Returns the current position of the large object lo_desc.

static VALUE
pgconn_lotell(VALUE self, VALUE in_lo_desc)
{
        int position;
        PGconn *conn = pg_get_pgconn(self);
        int lo_desc = NUM2INT(in_lo_desc);

        if((position = lo_tell(conn, lo_desc)) < 0)
                rb_raise(rb_ePGerror,"lo_tell failed");

        return INT2FIX(position);
}
Also aliased as: lotell
anchor
lo_truncate( lo_desc, len ) → nil

Truncates the large object lo_desc to size len.

static VALUE
pgconn_lotruncate(VALUE self, VALUE in_lo_desc, VALUE in_len)
{
        PGconn *conn = pg_get_pgconn(self);
        int lo_desc = NUM2INT(in_lo_desc);
        size_t len = NUM2INT(in_len);

        if(lo_truncate(conn,lo_desc,len) < 0)
                rb_raise(rb_ePGerror,"lo_truncate failed");

        return Qnil;
}
Also aliased as: lotruncate
anchor anchor
lo_write( lo_desc, buffer ) → Fixnum

Writes the string buffer to the large object lo_desc. Returns the number of bytes written.

static VALUE
pgconn_lowrite(VALUE self, VALUE in_lo_desc, VALUE buffer)
{
        int n;
        PGconn *conn = pg_get_pgconn(self);
        int fd = NUM2INT(in_lo_desc);

        Check_Type(buffer, T_STRING);

        if( RSTRING_LEN(buffer) < 0) {
                rb_raise(rb_ePGerror, "write buffer zero string");
        }
        if((n = lo_write(conn, fd, StringValuePtr(buffer),
                                RSTRING_LEN(buffer))) < 0) {
                rb_raise(rb_ePGerror, "lo_write failed: %s", PQerrorMessage(conn));
        }

        return INT2FIX(n);
}
Also aliased as: lowrite
anchor
loclose(p1)
Alias for: lo_close
anchor
locreat(p1 = v1)
Alias for: lo_creat
anchor
locreate(p1)
Alias for: lo_create
anchor
loexport(p1, p2)
Alias for: lo_export
anchor
loimport(p1)
Alias for: lo_import
anchor
lolseek(p1, p2, p3)
Alias for: lo_lseek
anchor
loopen(p1, p2 = v2)
Alias for: lo_open
anchor
loread(p1, p2)
Alias for: lo_read
anchor
loseek(p1, p2, p3)
Alias for: lo_lseek
anchor
lotell(p1)
Alias for: lo_tell
anchor
lotruncate(p1, p2)
Alias for: lo_truncate
anchor anchor
lowrite(p1, p2)
Alias for: lo_write
anchor
make_empty_pgresult( status ) → PG::Result

Constructs and empty PG::Result with status status. status may be 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
pgconn_make_empty_pgresult(VALUE self, VALUE status)
{
        PGresult *result;
        VALUE rb_pgresult;
        PGconn *conn = pg_get_pgconn(self);
        result = PQmakeEmptyPGresult(conn, NUM2INT(status));
        rb_pgresult = pg_new_result(result, self);
        pg_result_check(rb_pgresult);
        return rb_pgresult;
}
anchor
nonblocking?()
Alias for: isnonblocking
anchor
notifies()

Returns a hash of the unprocessed notifications. If there is no unprocessed notifier, it returns nil.

static VALUE
pgconn_notifies(VALUE self)
{
        PGconn* conn = pg_get_pgconn(self);
        PGnotify *notification;
        VALUE hash;
        VALUE sym_relname, sym_be_pid, sym_extra;
        VALUE relname, be_pid, extra;

        sym_relname = ID2SYM(rb_intern("relname"));
        sym_be_pid = ID2SYM(rb_intern("be_pid"));
        sym_extra = ID2SYM(rb_intern("extra"));

        notification = gvl_PQnotifies(conn);
        if (notification == NULL) {
                return Qnil;
        }

        hash = rb_hash_new();
        relname = rb_tainted_str_new2(notification->relname);
        be_pid = INT2NUM(notification->be_pid);
        extra = rb_tainted_str_new2(notification->extra);
#ifdef M17N_SUPPORTED
        ENCODING_SET( relname, rb_enc_to_index(pg_conn_enc_get( conn )) );
        ENCODING_SET( extra, rb_enc_to_index(pg_conn_enc_get( conn )) );
#endif

        rb_hash_aset(hash, sym_relname, relname);
        rb_hash_aset(hash, sym_be_pid, be_pid);
        rb_hash_aset(hash, sym_extra, extra);

        PQfreemem(notification);
        return hash;
}
anchor
notifies_wait(p1 = v1)
Alias for: wait_for_notify
anchor
options()

Returns backend option string.

static VALUE
pgconn_options(VALUE self)
{
        char *options = PQoptions(pg_get_pgconn(self));
        if (!options) return Qnil;
        return rb_tainted_str_new2(options);
}
anchor
parameter_status( param_name ) → String

Returns the setting of parameter param_name, where param_name is one of

  • server_version

  • server_encoding

  • client_encoding

  • is_superuser

  • session_authorization

  • DateStyle

  • TimeZone

  • integer_datetimes

  • standard_conforming_strings

Returns nil if the value of the parameter is not known.

static VALUE
pgconn_parameter_status(VALUE self, VALUE param_name)
{
        const char *ret = PQparameterStatus(pg_get_pgconn(self), StringValuePtr(param_name));
        if(ret == NULL)
                return Qnil;
        else
                return rb_tainted_str_new2(ret);
}
anchor
pass()

Returns the authenticated user name.

static VALUE
pgconn_pass(VALUE self)
{
        char *user = PQpass(pg_get_pgconn(self));
        if (!user) return Qnil;
        return rb_tainted_str_new2(user);
}
anchor
port()

Returns the connected server port number.

static VALUE
pgconn_port(VALUE self)
{
        char* port = PQport(pg_get_pgconn(self));
        return INT2NUM(atol(port));
}
anchor
prepare(stmt_name, sql [, param_types ] ) → PG::Result

Prepares statement sql with name name to be executed later. Returns a PG::Result instance on success. On failure, it raises a PG::Error.

param_types is an optional parameter to specify the Oids of the types of the parameters.

If the types are not specified, they will be inferred by PostgreSQL. Instead of specifying type oids, it's recommended to simply add explicit casts in the query to ensure that the right type is used.

For example: “SELECT $1::int”

PostgreSQL bind parameters are represented as $1, $1, $2, etc., inside the SQL query.

static VALUE
pgconn_prepare(int argc, VALUE *argv, VALUE self)
{
        PGconn *conn = pg_get_pgconn(self);
        PGresult *result = NULL;
        VALUE rb_pgresult;
        VALUE name, command, in_paramtypes;
        VALUE param;
        int i = 0;
        int nParams = 0;
        Oid *paramTypes = NULL;

        rb_scan_args(argc, argv, "21", &name, &command, &in_paramtypes);
        Check_Type(name, T_STRING);
        Check_Type(command, T_STRING);

        if(! NIL_P(in_paramtypes)) {
                Check_Type(in_paramtypes, T_ARRAY);
                nParams = (int)RARRAY_LEN(in_paramtypes);
                paramTypes = ALLOC_N(Oid, nParams);
                for(i = 0; i < nParams; i++) {
                        param = rb_ary_entry(in_paramtypes, i);
                        Check_Type(param, T_FIXNUM);
                        if(param == Qnil)
                                paramTypes[i] = 0;
                        else
                                paramTypes[i] = NUM2INT(param);
                }
        }
        result = gvl_PQprepare(conn, StringValuePtr(name), StringValuePtr(command),
                        nParams, paramTypes);

        xfree(paramTypes);

        rb_pgresult = pg_new_result(result, self);
        pg_result_check(rb_pgresult);
        return rb_pgresult;
}
anchor
protocol_version → Integer

The 3.0 protocol will normally be used when communicating with PostgreSQL 7.4 or later servers; pre-7.4 servers support only protocol 2.0. (Protocol 1.0 is obsolete and not supported by libpq.)

static VALUE
pgconn_protocol_version(VALUE self)
{
        return INT2NUM(PQprotocolVersion(pg_get_pgconn(self)));
}
anchor
put_copy_data( buffer ) → Boolean

Transmits buffer as copy data to the server. Returns true if the data was sent, false if it was not sent (false is only possible if the connection is in nonblocking mode, and this command would block).

Raises an exception if an error occurs.

See also copy_data.

static VALUE
pgconn_put_copy_data(self, buffer)
        VALUE self, buffer;
{
        int ret;
        VALUE error;
        PGconn *conn = pg_get_pgconn(self);
        Check_Type(buffer, T_STRING);

        ret = gvl_PQputCopyData(conn, RSTRING_PTR(buffer), (int)RSTRING_LEN(buffer));
        if(ret == -1) {
                error = rb_exc_new2(rb_ePGerror, PQerrorMessage(conn));
                rb_iv_set(error, "@connection", self);
                rb_exc_raise(error);
        }
        return (ret) ? Qtrue : Qfalse;
}
anchor
put_copy_end( [ error_message ] ) → Boolean

Sends end-of-data indication to the server.

error_message is an optional parameter, and if set, forces the COPY command to fail with the string error_message.

Returns true if the end-of-data was sent, false if it was not sent (false is only possible if the connection is in nonblocking mode, and this command would block).

static VALUE
pgconn_put_copy_end(int argc, VALUE *argv, VALUE self)
{
        VALUE str;
        VALUE error;
        int ret;
        char *error_message = NULL;
        PGconn *conn = pg_get_pgconn(self);

        if (rb_scan_args(argc, argv, "01", &str) == 0)
                error_message = NULL;
        else
                error_message = StringValuePtr(str);

        ret = gvl_PQputCopyEnd(conn, error_message);
        if(ret == -1) {
                error = rb_exc_new2(rb_ePGerror, PQerrorMessage(conn));
                rb_iv_set(error, "@connection", self);
                rb_exc_raise(error);
        }
        return (ret) ? Qtrue : Qfalse;
}
anchor
query(*args)
Alias for: exec
anchor
PG::Connection.quote_ident( str ) → String
quote_ident( str ) → String

Returns a string that is safe for inclusion in a SQL query as an identifier. Note: this is not a quote function for values, but for identifiers.

For example, in a typical SQL query: SELECT FOO FROM MYTABLE The identifier FOO is folded to lower case, so it actually means foo. If you really want to access the case-sensitive field name FOO, use this function like PG::Connection.quote_ident('FOO'), which will return "FOO" (with double-quotes). PostgreSQL will see the double-quotes, and it will not fold to lower case.

Similarly, this function also protects against special characters, and other things that might allow SQL injection if the identifier comes from an untrusted source.

static VALUE
pgconn_s_quote_ident(VALUE self, VALUE in_str)
{
        VALUE ret;
        char *str = StringValuePtr(in_str);
        /* result size at most NAMEDATALEN*2 plus surrounding
         * double-quotes. */
        char buffer[NAMEDATALEN*2+2];
        unsigned int i=0,j=0;
#ifdef M17N_SUPPORTED
        rb_encoding* enc;
#endif

        UNUSED( self );

        if(strlen(str) >= NAMEDATALEN) {
                rb_raise(rb_eArgError,
                        "Input string is longer than NAMEDATALEN-1 (%d)",
                        NAMEDATALEN-1);
        }
        buffer[j++] = '"';
        for(i = 0; i < strlen(str) && str[i]; i++) {
                if(str[i] == '"')
                        buffer[j++] = '"';
                buffer[j++] = str[i];
        }
        buffer[j++] = '"';
        ret = rb_str_new(buffer,j);
        OBJ_INFECT(ret, in_str);

#ifdef M17N_SUPPORTED
        if ( rb_obj_class(self) == rb_cPGconn ) {
                enc = pg_conn_enc_get( pg_get_pgconn(self) );
        } else {
                enc = rb_enc_get(in_str);
        }
        rb_enc_associate(ret, enc);
#endif

        return ret;
}
anchor
reset()

Resets the backend connection. This method closes the backend connection and tries to re-connect.

static VALUE
pgconn_reset( VALUE self )
{
        pgconn_close_socket_io( self );
        gvl_PQreset( pg_get_pgconn(self) );
        return self;
}
anchor
reset_poll → Fixnum

Checks the status of a connection reset operation. See connect_start and connect_poll for usage information and return values.

static VALUE
pgconn_reset_poll(VALUE self)
{
        PostgresPollingStatusType status;
        status = gvl_PQresetPoll(pg_get_pgconn(self));
        return INT2FIX((int)status);
}
anchor
reset_start() → nil

Initiate a connection reset in a nonblocking manner. This will close the current connection and attempt to reconnect using the same connection parameters. Use reset_poll to check the status of the connection reset.

static VALUE
pgconn_reset_start(VALUE self)
{
        pgconn_close_socket_io( self );
        if(gvl_PQresetStart(pg_get_pgconn(self)) == 0)
                rb_raise(rb_eUnableToSend, "reset has failed");
        return Qnil;
}
anchor
send_describe_portal( portal_name ) → nil

Asynchronously send command to the server. Does not block. Use in combination with conn.get_result.

static VALUE
pgconn_send_describe_portal(VALUE self, VALUE portal)
{
        VALUE error;
        PGconn *conn = pg_get_pgconn(self);
        /* returns 0 on failure */
        if(gvl_PQsendDescribePortal(conn,StringValuePtr(portal)) == 0) {
                error = rb_exc_new2(rb_eUnableToSend, PQerrorMessage(conn));
                rb_iv_set(error, "@connection", self);
                rb_exc_raise(error);
        }
        return Qnil;
}
anchor
send_describe_prepared( statement_name ) → nil

Asynchronously send command to the server. Does not block. Use in combination with conn.get_result.

static VALUE
pgconn_send_describe_prepared(VALUE self, VALUE stmt_name)
{
        VALUE error;
        PGconn *conn = pg_get_pgconn(self);
        /* returns 0 on failure */
        if(gvl_PQsendDescribePrepared(conn,StringValuePtr(stmt_name)) == 0) {
                error = rb_exc_new2(rb_eUnableToSend, PQerrorMessage(conn));
                rb_iv_set(error, "@connection", self);
                rb_exc_raise(error);
        }
        return Qnil;
}
anchor
send_prepare( stmt_name, sql [, param_types ] ) → nil

Prepares statement sql with name name to be executed later. Sends prepare command asynchronously, and returns immediately. On failure, it raises a PG::Error.

param_types is an optional parameter to specify the Oids of the types of the parameters.

If the types are not specified, they will be inferred by PostgreSQL. Instead of specifying type oids, it's recommended to simply add explicit casts in the query to ensure that the right type is used.

For example: “SELECT $1::int”

PostgreSQL bind parameters are represented as $1, $1, $2, etc., inside the SQL query.

static VALUE
pgconn_send_prepare(int argc, VALUE *argv, VALUE self)
{
        PGconn *conn = pg_get_pgconn(self);
        int result;
        VALUE name, command, in_paramtypes;
        VALUE param;
        VALUE error;
        int i = 0;
        int nParams = 0;
        Oid *paramTypes = NULL;

        rb_scan_args(argc, argv, "21", &name, &command, &in_paramtypes);
        Check_Type(name, T_STRING);
        Check_Type(command, T_STRING);

        if(! NIL_P(in_paramtypes)) {
                Check_Type(in_paramtypes, T_ARRAY);
                nParams = (int)RARRAY_LEN(in_paramtypes);
                paramTypes = ALLOC_N(Oid, nParams);
                for(i = 0; i < nParams; i++) {
                        param = rb_ary_entry(in_paramtypes, i);
                        Check_Type(param, T_FIXNUM);
                        if(param == Qnil)
                                paramTypes[i] = 0;
                        else
                                paramTypes[i] = NUM2INT(param);
                }
        }
        result = gvl_PQsendPrepare(conn, StringValuePtr(name), StringValuePtr(command),
                        nParams, paramTypes);

        xfree(paramTypes);

        if(result == 0) {
                error = rb_exc_new2(rb_eUnableToSend, PQerrorMessage(conn));
                rb_iv_set(error, "@connection", self);
                rb_exc_raise(error);
        }
        return Qnil;
}
anchor
send_query(sql [, params, result_format ] ) → nil

Sends SQL query request specified by sql to PostgreSQL for asynchronous processing, and immediately returns. On failure, it raises a PG::Error.

params is an optional array of the bind parameters for the SQL query. Each element of the params array may be either:

a hash of the form:
  {:value  => String (value of bind parameter)
   :type   => Fixnum (oid of type of bind parameter)
   :format => Fixnum (0 for text, 1 for binary)
  }
or, it may be a String. If it is a string, that is equivalent to the hash:
  { :value => <string value>, :type => 0, :format => 0 }

PostgreSQL bind parameters are represented as $1, $1, $2, etc., inside the SQL query. The 0th element of the params array is bound to $1, the 1st element is bound to $2, etc. nil is treated as NULL.

If the types are not specified, they will be inferred by PostgreSQL. Instead of specifying type oids, it's recommended to simply add explicit casts in the query to ensure that the right type is used.

For example: “SELECT $1::int”

The optional result_format should be 0 for text results, 1 for binary.

static VALUE
pgconn_send_query(int argc, VALUE *argv, VALUE self)
{
        PGconn *conn = pg_get_pgconn(self);
        int result;
        VALUE command, params, in_res_fmt;
        VALUE param, param_type, param_value, param_format;
        VALUE param_value_tmp;
        VALUE sym_type, sym_value, sym_format;
        VALUE gc_array;
        VALUE error;
        int i=0;
        int nParams;
        Oid *paramTypes;
        char ** paramValues;
        int *paramLengths;
        int *paramFormats;
        int resultFormat;

        rb_scan_args(argc, argv, "12", &command, &params, &in_res_fmt);
        Check_Type(command, T_STRING);

        /* If called with no parameters, use PQsendQuery */
        if(NIL_P(params)) {
                if(gvl_PQsendQuery(conn,StringValuePtr(command)) == 0) {
                        error = rb_exc_new2(rb_eUnableToSend, PQerrorMessage(conn));
                        rb_iv_set(error, "@connection", self);
                        rb_exc_raise(error);
                }
                return Qnil;
        }

        /* If called with parameters, and optionally result_format,
         * use PQsendQueryParams
         */
        Check_Type(params, T_ARRAY);

        if(NIL_P(in_res_fmt)) {
                resultFormat = 0;
        }
        else {
                resultFormat = NUM2INT(in_res_fmt);
        }

        gc_array = rb_ary_new();
        rb_gc_register_address(&gc_array);
        sym_type = ID2SYM(rb_intern("type"));
        sym_value = ID2SYM(rb_intern("value"));
        sym_format = ID2SYM(rb_intern("format"));
        nParams = (int)RARRAY_LEN(params);
        paramTypes = ALLOC_N(Oid, nParams);
        paramValues = ALLOC_N(char *, nParams);
        paramLengths = ALLOC_N(int, nParams);
        paramFormats = ALLOC_N(int, nParams);
        for(i = 0; i < nParams; i++) {
                param = rb_ary_entry(params, i);
                if (TYPE(param) == T_HASH) {
                        param_type = rb_hash_aref(param, sym_type);
                        param_value_tmp = rb_hash_aref(param, sym_value);
                        if(param_value_tmp == Qnil)
                                param_value = param_value_tmp;
                        else
                                param_value = rb_obj_as_string(param_value_tmp);
                        param_format = rb_hash_aref(param, sym_format);
                }
                else {
                        param_type = INT2NUM(0);
                        if(param == Qnil)
                                param_value = param;
                        else
                                param_value = rb_obj_as_string(param);
                        param_format = INT2NUM(0);
                }

                if(param_type == Qnil)
                        paramTypes[i] = 0;
                else
                        paramTypes[i] = NUM2INT(param_type);

                if(param_value == Qnil) {
                        paramValues[i] = NULL;
                        paramLengths[i] = 0;
                }
                else {
                        Check_Type(param_value, T_STRING);
                        /* make sure param_value doesn't get freed by the GC */
                        rb_ary_push(gc_array, param_value);
                        paramValues[i] = StringValuePtr(param_value);
                        paramLengths[i] = (int)RSTRING_LEN(param_value);
                }

                if(param_format == Qnil)
                        paramFormats[i] = 0;
                else
                        paramFormats[i] = NUM2INT(param_format);
        }

        result = gvl_PQsendQueryParams(conn, StringValuePtr(command), nParams, paramTypes,
                (const char * const *)paramValues, paramLengths, paramFormats, resultFormat);

        rb_gc_unregister_address(&gc_array);

        xfree(paramTypes);
        xfree(paramValues);
        xfree(paramLengths);
        xfree(paramFormats);

        if(result == 0) {
                error = rb_exc_new2(rb_eUnableToSend, PQerrorMessage(conn));
                rb_iv_set(error, "@connection", self);
                rb_exc_raise(error);
        }
        return Qnil;
}
anchor
send_query_prepared( statement_name [, params, result_format ] )
→ nil

Execute prepared named statement specified by statement_name asynchronously, and returns immediately. On failure, it raises a PG::Error.

params is an array of the optional bind parameters for the SQL query. Each element of the params array may be either:

a hash of the form:
  {:value  => String (value of bind parameter)
   :format => Fixnum (0 for text, 1 for binary)
  }
or, it may be a String. If it is a string, that is equivalent to the hash:
  { :value => <string value>, :format => 0 }

PostgreSQL bind parameters are represented as $1, $1, $2, etc., inside the SQL query. The 0th element of the params array is bound to $1, the 1st element is bound to $2, etc. nil is treated as NULL.

The optional result_format should be 0 for text results, 1 for binary.

static VALUE
pgconn_send_query_prepared(int argc, VALUE *argv, VALUE self)
{
        PGconn *conn = pg_get_pgconn(self);
        int result;
        VALUE name, params, in_res_fmt;
        VALUE param, param_value, param_format;
        VALUE param_value_tmp;
        VALUE sym_value, sym_format;
        VALUE gc_array;
        VALUE error;
        int i = 0;
        int nParams;
        char ** paramValues;
        int *paramLengths;
        int *paramFormats;
        int resultFormat;

        rb_scan_args(argc, argv, "12", &name, &params, &in_res_fmt);
        Check_Type(name, T_STRING);

        if(NIL_P(params)) {
                params = rb_ary_new2(0);
                resultFormat = 0;
        }
        else {
                Check_Type(params, T_ARRAY);
        }

        if(NIL_P(in_res_fmt)) {
                resultFormat = 0;
        }
        else {
                resultFormat = NUM2INT(in_res_fmt);
        }

        gc_array = rb_ary_new();
        rb_gc_register_address(&gc_array);
        sym_value = ID2SYM(rb_intern("value"));
        sym_format = ID2SYM(rb_intern("format"));
        nParams = (int)RARRAY_LEN(params);
        paramValues = ALLOC_N(char *, nParams);
        paramLengths = ALLOC_N(int, nParams);
        paramFormats = ALLOC_N(int, nParams);
        for(i = 0; i < nParams; i++) {
                param = rb_ary_entry(params, i);
                if (TYPE(param) == T_HASH) {
                        param_value_tmp = rb_hash_aref(param, sym_value);
                        if(param_value_tmp == Qnil)
                                param_value = param_value_tmp;
                        else
                                param_value = rb_obj_as_string(param_value_tmp);
                        param_format = rb_hash_aref(param, sym_format);
                }
                else {
                        if(param == Qnil)
                                param_value = param;
                        else
                                param_value = rb_obj_as_string(param);
                        param_format = INT2NUM(0);
                }

                if(param_value == Qnil) {
                        paramValues[i] = NULL;
                        paramLengths[i] = 0;
                }
                else {
                        Check_Type(param_value, T_STRING);
                        /* make sure param_value doesn't get freed by the GC */
                        rb_ary_push(gc_array, param_value);
                        paramValues[i] = StringValuePtr(param_value);
                        paramLengths[i] = (int)RSTRING_LEN(param_value);
                }

                if(param_format == Qnil)
                        paramFormats[i] = 0;
                else
                        paramFormats[i] = NUM2INT(param_format);
        }

        result = gvl_PQsendQueryPrepared(conn, StringValuePtr(name), nParams,
                (const char * const *)paramValues, paramLengths, paramFormats,
                resultFormat);

        rb_gc_unregister_address(&gc_array);

        xfree(paramValues);
        xfree(paramLengths);
        xfree(paramFormats);

        if(result == 0) {
                error = rb_exc_new2(rb_eUnableToSend, PQerrorMessage(conn));
                rb_iv_set(error, "@connection", self);
                rb_exc_raise(error);
        }
        return Qnil;
}
anchor
server_version → Integer

The number is formed by converting the major, minor, and revision numbers into two-decimal-digit numbers and appending them together. For example, version 7.4.2 will be returned as 70402, and version 8.1 will be returned as 80100 (leading zeroes are not shown). Zero is returned if the connection is bad.

static VALUE
pgconn_server_version(VALUE self)
{
        return INT2NUM(PQserverVersion(pg_get_pgconn(self)));
}
anchor
set_client_encoding( encoding )

Sets the client encoding to the encoding String.

static VALUE
pgconn_set_client_encoding(VALUE self, VALUE str)
{
        PGconn *conn = pg_get_pgconn( self );

        Check_Type(str, T_STRING);

        if ( (PQsetClientEncoding(conn, StringValuePtr(str))) == -1 ) {
                rb_raise(rb_ePGerror, "invalid encoding name: %s",StringValuePtr(str));
        }

        return Qnil;
}
Also aliased as: client_encoding=
anchor
set_default_encoding() → Encoding

If Ruby has its Encoding.default_internal set, set PostgreSQL's client_encoding to match. Returns the new Encoding, or nil if the default internal encoding wasn't set.

static VALUE
pgconn_set_default_encoding( VALUE self )
{
        PGconn *conn = pg_get_pgconn( self );
        rb_encoding *enc;
        const char *encname;

        if (( enc = rb_default_internal_encoding() )) {
                encname = pg_get_rb_encoding_as_pg_encoding( enc );
                if ( PQsetClientEncoding(conn, encname) != 0 )
                        rb_warn( "Failed to set the default_internal encoding to %s: '%s'",
                                 encname, PQerrorMessage(conn) );
                return rb_enc_from_encoding( enc );
        } else {
                return Qnil;
        }
}
anchor
set_error_verbosity( verbosity ) → Fixnum

Sets connection's verbosity to verbosity and returns the previous setting. Available settings are:

  • PQERRORS_TERSE

  • PQERRORS_DEFAULT

  • PQERRORS_VERBOSE

static VALUE
pgconn_set_error_verbosity(VALUE self, VALUE in_verbosity)
{
        PGconn *conn = pg_get_pgconn(self);
        PGVerbosity verbosity = NUM2INT(in_verbosity);
        return INT2FIX(PQsetErrorVerbosity(conn, verbosity));
}
anchor
set_notice_processor {|message| ... } → Proc

See set_notice_receiver for the desription of what this and the notice_processor methods do.

This function takes a new block to act as the notice processor and returns the Proc object previously set, or nil if it was previously the default. The block should accept a single String object.

If you pass no arguments, it will reset the handler to the default.

static VALUE
pgconn_set_notice_processor(VALUE self)
{
        VALUE proc, old_proc;
        PGconn *conn = pg_get_pgconn(self);

        /* If default_notice_processor is unset, assume that the current
         * notice processor is the default, and save it to a global variable.
         * This should not be a problem because the default processor is
         * always the same, so won't vary among connections.
         */
        if(default_notice_processor == NULL)
                default_notice_processor = PQsetNoticeProcessor(conn, NULL, NULL);

        old_proc = rb_iv_get(self, "@notice_processor");
        if( rb_block_given_p() ) {
                proc = rb_block_proc();
                PQsetNoticeProcessor(conn, gvl_notice_processor_proxy, (void *)self);
        } else {
                /* if no block is given, set back to default */
                proc = Qnil;
                PQsetNoticeProcessor(conn, default_notice_processor, NULL);
        }

        rb_iv_set(self, "@notice_processor", proc);
        return old_proc;
}
anchor
set_notice_receiver {|result| ... } → Proc

Notice and warning messages generated by the server are not returned by the query execution functions, since they do not imply failure of the query. Instead they are passed to a notice handling function, and execution continues normally after the handler returns. The default notice handling function prints the message on stderr, but the application can override this behavior by supplying its own handling function.

For historical reasons, there are two levels of notice handling, called the notice receiver and notice processor. The default behavior is for the notice receiver to format the notice and pass a string to the notice processor for printing. However, an application that chooses to provide its own notice receiver will typically ignore the notice processor layer and just do all the work in the notice receiver.

This function takes a new block to act as the handler, which should accept a single parameter that will be a PG::Result object, and returns the Proc object previously set, or nil if it was previously the default.

If you pass no arguments, it will reset the handler to the default.

Note: The result passed to the block should not be used outside of the block, since the corresponding C object could be freed after the block finishes.

static VALUE
pgconn_set_notice_receiver(VALUE self)
{
        VALUE proc, old_proc;
        PGconn *conn = pg_get_pgconn(self);

        /* If default_notice_receiver is unset, assume that the current
         * notice receiver is the default, and save it to a global variable.
         * This should not be a problem because the default receiver is
         * always the same, so won't vary among connections.
         */
        if(default_notice_receiver == NULL)
                default_notice_receiver = PQsetNoticeReceiver(conn, NULL, NULL);

        old_proc = rb_iv_get(self, "@notice_receiver");
        if( rb_block_given_p() ) {
                proc = rb_block_proc();
                PQsetNoticeReceiver(conn, gvl_notice_receiver_proxy, (void *)self);
        } else {
                /* if no block is given, set back to default */
                proc = Qnil;
                PQsetNoticeReceiver(conn, default_notice_receiver, NULL);
        }

        rb_iv_set(self, "@notice_receiver", proc);
        return old_proc;
}
anchor
set_single_row_mode → self

To enter single-row mode, call this method immediately after a successful call of #send_query (or a sibling function). This mode selection is effective only for the currently executing query. Then call #get_result repeatedly, until it returns nil.

Each (but the last) received Result has exactly one row and a Result#result_status of PGRES_SINGLE_TUPLE. The last row has zero rows and is used to indicate a successful execution of the query. All of these Result objects will contain the same row description data (column names, types, etc) that an ordinary Result object for the query would have.

Caution: While processing a query, the server may return some rows and then encounter an error, causing the query to be aborted. Ordinarily, pg discards any such rows and reports only the error. But in single-row mode, those rows will have already been returned to the application. Hence, the application will see some Result objects followed by an Error raised in get_result. For proper transactional behavior, the application must be designed to discard or undo whatever has been done with the previously-processed rows, if the query ultimately fails.

Example:

conn.send_query( "your SQL command" )
conn.set_single_row_mode
loop do
  res = conn.get_result or break
  res.check
  res.each do |row|
    # do something with the received row
  end
end
static VALUE
pgconn_set_single_row_mode(VALUE self)
{
        PGconn *conn = pg_get_pgconn(self);
        VALUE error;

        if( PQsetSingleRowMode(conn) == 0 )
        {
                error = rb_exc_new2(rb_ePGerror, PQerrorMessage(conn));
                rb_iv_set(error, "@connection", self);
                rb_exc_raise(error);
        }

        return self;
}
anchor
setnonblocking(Boolean) → nil

Sets the nonblocking status of the connection. In the blocking state, calls to send_query will block until the message is sent to the server, but will not wait for the query results. In the nonblocking state, calls to send_query will return an error if the socket is not ready for writing. Note: This function does not affect exec, because that function doesn't return until the server has processed the query and returned the results. Returns nil.

static VALUE
pgconn_setnonblocking(self, state)
        VALUE self, state;
{
        int arg;
        VALUE error;
        PGconn *conn = pg_get_pgconn(self);
        if(state == Qtrue)
                arg = 1;
        else if (state == Qfalse)
                arg = 0;
        else
                rb_raise(rb_eArgError, "Boolean value expected");

        if(PQsetnonblocking(conn, arg) == -1) {
                error = rb_exc_new2(rb_ePGerror, PQerrorMessage(conn));
                rb_iv_set(error, "@connection", self);
                rb_exc_raise(error);
        }
        return Qnil;
}
anchor
socket() → Fixnum

Returns the socket's file descriptor for this connection. IO.for_fd() can be used to build a proper IO object to the socket. If you do so, you will likely also want to set autoclose=false on it to prevent Ruby from closing the socket to PostgreSQL if it goes out of scope. Alternatively, you can use socket_io, which creates an IO that's associated with the connection object itself, and so won't go out of scope until the connection does.

Note: On Windows the file descriptor is not really usable, since it can not be used to build a Ruby IO object.

static VALUE
pgconn_socket(VALUE self)
{
        int sd;
        if( (sd = PQsocket(pg_get_pgconn(self))) < 0)
                rb_raise(rb_eConnectionBad, "PQsocket() can't get socket descriptor");
        return INT2NUM(sd);
}
anchor
socket_io() → IO

Fetch a memoized IO object created from the Connection's underlying socket. This object can be used for IO.select to wait for events while running asynchronous API calls.

Using this instead of socket avoids the problem of the underlying connection being closed by Ruby when an IO created using IO.for_fd(conn.socket) goes out of scope.

This method can also be used on Windows but requires Ruby-2.0+.

static VALUE
pgconn_socket_io(VALUE self)
{
        int sd;
        int ruby_sd;
        ID id_autoclose = rb_intern("autoclose=");
        VALUE socket_io = rb_iv_get( self, "@socket_io" );

        if ( !RTEST(socket_io) ) {
                if( (sd = PQsocket(pg_get_pgconn(self))) < 0)
                        rb_raise(rb_eConnectionBad, "PQsocket() can't get socket descriptor");

                #ifdef _WIN32
                        ruby_sd = rb_w32_wrap_io_handle((HANDLE)(intptr_t)sd, O_RDWR|O_BINARY|O_NOINHERIT);
                #else
                        ruby_sd = sd;
                #endif

                socket_io = rb_funcall( rb_cIO, rb_intern("for_fd"), 1, INT2NUM(ruby_sd) );

                /* Disable autoclose feature, when supported */
                if( rb_respond_to(socket_io, id_autoclose) ){
                        rb_funcall( socket_io, id_autoclose, 1, Qfalse );
                }

                rb_iv_set( self, "@socket_io", socket_io );
        }

        return socket_io;
}
anchor
status()

Returns status of connection : CONNECTION_OK or CONNECTION_BAD

static VALUE
pgconn_status(VALUE self)
{
        return INT2NUM(PQstatus(pg_get_pgconn(self)));
}
anchor
trace( stream ) → nil

Enables tracing message passing between backend. The trace message will be written to the stream stream, which must implement a method fileno that returns a writable file descriptor.

static VALUE
pgconn_trace(VALUE self, VALUE stream)
{
        VALUE fileno;
        FILE *new_fp;
        int old_fd, new_fd;
        VALUE new_file;

        if(rb_respond_to(stream,rb_intern("fileno")) == Qfalse)
                rb_raise(rb_eArgError, "stream does not respond to method: fileno");

        fileno = rb_funcall(stream, rb_intern("fileno"), 0);
        if(fileno == Qnil)
                rb_raise(rb_eArgError, "can't get file descriptor from stream");

        /* Duplicate the file descriptor and re-open
         * it. Then, make it into a ruby File object
         * and assign it to an instance variable.
         * This prevents a problem when the File
         * object passed to this function is closed
         * before the connection object is. */
        old_fd = NUM2INT(fileno);
        new_fd = dup(old_fd);
        new_fp = fdopen(new_fd, "w");

        if(new_fp == NULL)
                rb_raise(rb_eArgError, "stream is not writable");

        new_file = rb_funcall(rb_cIO, rb_intern("new"), 1, INT2NUM(new_fd));
        rb_iv_set(self, "@trace_stream", new_file);

        PQtrace(pg_get_pgconn(self), new_fp);
        return Qnil;
}
anchor
transaction { |conn| ... } → result of the block

Executes a BEGIN at the start of the block, and a COMMIT at the end of the block, or ROLLBACK if any exception occurs.

static VALUE
pgconn_transaction(VALUE self)
{
        PGconn *conn = pg_get_pgconn(self);
        PGresult *result;
        VALUE rb_pgresult;
        VALUE block_result = Qnil;
        int status;

        if (rb_block_given_p()) {
                result = gvl_PQexec(conn, "BEGIN");
                rb_pgresult = pg_new_result(result, self);
                pg_result_check(rb_pgresult);
                block_result = rb_protect(rb_yield, self, &status);
                if(status == 0) {
                        result = gvl_PQexec(conn, "COMMIT");
                        rb_pgresult = pg_new_result(result, self);
                        pg_result_check(rb_pgresult);
                }
                else {
                        /* exception occurred, ROLLBACK and re-raise */
                        result = gvl_PQexec(conn, "ROLLBACK");
                        rb_pgresult = pg_new_result(result, self);
                        pg_result_check(rb_pgresult);
                        rb_jump_tag(status);
                }

        }
        else {
                /* no block supplied? */
                rb_raise(rb_eArgError, "Must supply block for PG::Connection#transaction");
        }
        return block_result;
}
anchor
transaction_status()

returns one of the following statuses:

PQTRANS_IDLE    = 0 (connection idle)
PQTRANS_ACTIVE  = 1 (command in progress)
PQTRANS_INTRANS = 2 (idle, within transaction block)
PQTRANS_INERROR = 3 (idle, within failed transaction)
PQTRANS_UNKNOWN = 4 (cannot determine status)
static VALUE
pgconn_transaction_status(VALUE self)
{
        return INT2NUM(PQtransactionStatus(pg_get_pgconn(self)));
}
anchor
tty()

Returns the connected pgtty. (Obsolete)

static VALUE
pgconn_tty(VALUE self)
{
        char *tty = PQtty(pg_get_pgconn(self));
        if (!tty) return Qnil;
        return rb_tainted_str_new2(tty);
}
anchor
PG::Connection.unescape_bytea( string )

Converts an escaped string representation of binary data into binary data — the reverse of escape_bytea. This is needed when retrieving bytea data in text format, but not when retrieving it in binary format.

static VALUE
pgconn_s_unescape_bytea(VALUE self, VALUE str)
{
        unsigned char *from, *to;
        size_t to_len;
        VALUE ret;

        UNUSED( self );

        Check_Type(str, T_STRING);
        from = (unsigned char*)StringValuePtr(str);

        to = PQunescapeBytea(from, &to_len);

        ret = rb_str_new((char*)to, to_len);
        OBJ_INFECT(ret, str);
        PQfreemem(to);
        return ret;
}
anchor
untrace() → nil

Disables the message tracing.

static VALUE
pgconn_untrace(VALUE self)
{
        VALUE trace_stream;
        PQuntrace(pg_get_pgconn(self));
        trace_stream = rb_iv_get(self, "@trace_stream");
        rb_funcall(trace_stream, rb_intern("close"), 0);
        rb_iv_set(self, "@trace_stream", Qnil);
        return Qnil;
}
anchor
user()

Returns the authenticated user name.

static VALUE
pgconn_user(VALUE self)
{
        char *user = PQuser(pg_get_pgconn(self));
        if (!user) return Qnil;
        return rb_tainted_str_new2(user);
}
anchor
wait_for_notify( [ timeout ] ) → String
wait_for_notify( [ timeout ] ) { |event, pid| block }
wait_for_notify( [ timeout ] ) { |event, pid, payload| block } # PostgreSQL 9.0

Blocks while waiting for notification(s), or until the optional timeout is reached, whichever comes first. timeout is measured in seconds and can be fractional.

Returns nil if timeout is reached, the name of the NOTIFY event otherwise. If used in block form, passes the name of the NOTIFY event and the generating pid into the block.

Under PostgreSQL 9.0 and later, if the notification is sent with the optional payload string, it will be given to the block as the third argument.

static VALUE
pgconn_wait_for_notify(int argc, VALUE *argv, VALUE self)
{
        PGconn *conn = pg_get_pgconn( self );
        PGnotify *pnotification;
        struct timeval timeout;
        struct timeval *ptimeout = NULL;
        VALUE timeout_in = Qnil, relname = Qnil, be_pid = Qnil, extra = Qnil;
        double timeout_sec;

        rb_scan_args( argc, argv, "01", &timeout_in );

        if ( RTEST(timeout_in) ) {
                timeout_sec = NUM2DBL( timeout_in );
                timeout.tv_sec = (time_t)timeout_sec;
                timeout.tv_usec = (suseconds_t)( (timeout_sec - (long)timeout_sec) * 1e6 );
                ptimeout = &timeout;
        }

        pnotification = (PGnotify*) wait_socket_readable( conn, ptimeout, notify_readable);

        /* Return nil if the select timed out */
        if ( !pnotification ) return Qnil;

        relname = rb_tainted_str_new2( pnotification->relname );
#ifdef M17N_SUPPORTED
        ENCODING_SET( relname, rb_enc_to_index(pg_conn_enc_get( conn )) );
#endif
        be_pid = INT2NUM( pnotification->be_pid );
#ifdef HAVE_ST_NOTIFY_EXTRA
        if ( *pnotification->extra ) {
                extra = rb_tainted_str_new2( pnotification->extra );
#ifdef M17N_SUPPORTED
                ENCODING_SET( extra, rb_enc_to_index(pg_conn_enc_get( conn )) );
#endif
        }
#endif
        PQfreemem( pnotification );

        if ( rb_block_given_p() )
                rb_yield_values( 3, relname, be_pid, extra );

        return relname;
}
Also aliased as: notifies_wait