/*
 * call-seq:
 *    PGconn.quote( obj )
 *    PGconn.quote( obj ) { |obj| ... }
 *    PGconn.format( obj )
 *    PGconn.format( obj ) { |obj| ... }
 * 
 * If _obj_ is a Number, String, Array, +nil+, +true+, or +false+ then
 * #quote returns a String representation of that object safe for use in PostgreSQL.
 * 
 * If _obj_ is not one of the above classes and a block is supplied to #quote,
 * the block is invoked, passing along the object. The return value from the
 * block is returned as a string.
 *
 * If _obj_ is not one of the recognized classes andno block is supplied,
 * a PGError is raised.
 */
static VALUE
pgconn_s_quote(self, obj)
    VALUE self, obj;
{
    char* quoted;
    int size;
    VALUE result;

    if (TYPE(obj) == T_STRING) {
        /* length * 2 because every char could require escaping */
        /* + 2 for the quotes, + 1 for the null terminator */
        quoted = ALLOCA_N(char, RSTRING(obj)->len * 2 + 2 + 1);
        size = PQescapeString(quoted + 1, RSTRING(obj)->ptr, RSTRING(obj)->len);
        *quoted = *(quoted + size + 1) = SINGLE_QUOTE;
        result = rb_str_new(quoted, size + 2);
        OBJ_INFECT(result, obj);
        return result;
    }
    else {
        return pgconn_s_format(self, obj);
    }
}