2012-08-06 40 views
1

我正在实现PostgreSQL的驱动程序,并获取RowDescription消息和一些DataRow消息来响应数据库查询。但是,如何获取返回的列的类型?例如。第一栏应该是int,第二栏应该是varchar(20)如何从RowDescription消息中看到PostgreSQL列类型?

下面是一些打印从RowDescription

[RowDescription] 2 rows 
[Row] id 
tableObjId: 16393 
attrNr: 1 
objId: 23 
dataTypeSz: 4 
typeModifier: -1 
formatCode: 0 
[Row] name 
tableObjId: 16393 
attrNr: 2 
objId: 1043 
dataTypeSz: -1 
typeModifier: 24 
formatCode: 0 

,为DataRow

[DataRow] 2 columns 
data: 2 
data: Jonas 
[DataRow] 2 columns 
data: 76 
data: Anders 

有什么建议?这是我需要在任何系统表中查找的东西吗?

回答

2

objId是对列类型的参考pg_type.oid。 23 int4和1043 varcharselect * from pg_type where oid in (23,1043)

第二栏应注明在VARCHAR限定符的类型修饰符,虽然我不明白为什么这是24而不是20,也许因为这是varlena的长度返回的结构,其前缀长度为32位。无论如何,这就是typmod的工作原理:如果你做select pg_catalog.format_type(1043, 24),你会看到输出是character varying(20)documentation for PQfmod表示typmod解释是类型特定的。