/*
* call-seq:
* conn.escape_bytea( string ) -> String
* PGconn.escape_bytea( string ) -> String # DEPRECATED
*
* 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(CLASS_OF(self) == rb_cPGconn) {
to = PQescapeByteaConn(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;
}