Method: PG::Connection#quote_ident

Defined in:
ext/pg_connection.c

#quote_ident(str) ⇒ String #quote_ident(array) ⇒ String #PG::Connection.quote_ident(str) ⇒ String #PG::Connection.quote_ident(array) ⇒ 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 conn.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.

If the parameter is an Array, then all it’s values are separately quoted and then joined by a “.” character. This can be used for identifiers in the form “schema”.“table”.“column” .

This method is functional identical to the encoder PG::TextEncoder::Identifier .

If the instance method form is used and the input string character encoding is different to the connection encoding, then the string is converted to this encoding, so that the returned string is always encoded as PG::Connection#internal_encoding .

In the singleton form (PG::Connection.quote_ident) the character encoding of the result string is set to the character encoding of the input string.

Overloads:

  • #quote_ident(str) ⇒ String

    Returns:

    • (String)
  • #quote_ident(array) ⇒ String

    Returns:

    • (String)
  • #PG::Connection.quote_ident(str) ⇒ String

    Returns:

    • (String)
  • #PG::Connection.quote_ident(array) ⇒ String

    Returns:

    • (String)


3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
# File 'ext/pg_connection.c', line 3142

static VALUE
pgconn_s_quote_ident(VALUE self, VALUE str_or_array)
{
  VALUE ret;
  int enc_idx;

  if( rb_obj_is_kind_of(self, rb_cPGconn) ){
    enc_idx = pg_get_connection(self)->enc_idx;
  }else{
    enc_idx = RB_TYPE_P(str_or_array, T_STRING) ? ENCODING_GET( str_or_array ) : rb_ascii8bit_encindex();
  }
  pg_text_enc_identifier(NULL, str_or_array, NULL, &ret, enc_idx);

  return ret;
}