/* * 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_quote(self, obj) VALUE self, obj; { char* quoted; int size,error; 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_LEN(obj) * 2 + 2 + 1); size = PQescapeStringConn(get_pgconn(self),quoted + 1, RSTRING_PTR(obj), RSTRING_LEN(obj), &error); *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); } }