假设数据库存在以下函数:
-- Function: for_test(integer, integer) --DROP FUNCTION for_test(integer, integer); CREATE OR REPLACE FUNCTION for_test(INOUT param1 integer, INOUT param2 integer) RETURNS record AS $BODY$ begin select id,def_skill_id into param1,param2 from gangs where id = param1; end $BODY$ LANGUAGE plpgsql VOLATILE COST 100; ALTER FUNCTION for_test(integer, integer) OWNER TO postgres;
c++调用:
Mordor::PQ::Result rt1 = conn->execute("SELECT * from for_test(1,2)"); int a = (int)rt1.getType((size_t)0); int id = rt1.get<int>((size_t)0, (size_t)0); std::cerr << a << " " << id << std::endl;这里用到了Mordor库,它是对libpq的一个简单封装,这不是重点,关键是查询函数是要用
SELECT * from for_test(1,2)
而不是
SELECT for_test(1,2)
后者返回的是一个OID为2249的复合类型。
文献参考:
http://postgresql.1045698.n5.nabble.com/getting-composite-types-info-from-libpq-td3306738.html