Type conversion. What do I do with a PostgreSQL OID value in libpq in C?

后端 未结 2 482
情书的邮戳
情书的邮戳 2021-01-17 21:08

I\'m working with the PostgreSQL C API, libpq. I need to be able to convert the values in a PGresult* into their equivalent data types in Ruby. I\'m currently j

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-17 21:29

    To get the type name from an OID, just cast it to regtype:

    SELECT  700::oid::regtype -- real
    

    To get the type of any columns (or variable in plpgsql), use pg_typeof():

    SELECT  pg_typeof(1::real) -- real
    

    Gives you an answer of type regtype which is displayed as text in psql or pgAdmin. You can cast it to text explicitly if needed:

    SELECT  pg_typeof(1::real)::text -- real
    

    There is also this "big list", vulgo catalog table pg_type, where types are registered. This can be big, have a peek:

    SELECT * from pg_type LIMIT 10;
    

    More info in the excellent manual.

提交回复
热议问题