简体   繁体   English

如何通过jdbc获取postgres中的数组基类型

[英]How to get array base type in postgres via jdbc

How to get base type / dimension of an array column in Postgres by Java? 如何在Postgres中获取数组列的基类型/维度?

I have a tables that contain arrays like int[][] and text[] . 我有一个包含int[][]text[]等数组的表。

When I traverse the metadata from JDBC I can only get type as java.sql.Array . 当我从JDBC遍历元数据时,我只能将类型作为java.sql.Array Even in information_schema.columns it stored simply as ARRAY. 即使在information_schema.columns它也只是存储为ARRAY。

How can I know the base type and its dimension? 我如何知道基本类型及其维度?

I'm now working on tool to dump table info. 我现在正在研究转储表信息的工具。

For the array base type, DatabaseMetaData.getColumns() returns a ResultSet containing column metadata. 对于数组基类型, DatabaseMetaData.getColumns()返回包含列元数据的ResultSet One of the columns returned is TYPE_NAME . 返回的其中一列是TYPE_NAME This appears to contain the name of the array base type, prefixed with an underscore. 这似乎包含数组基类型的名称,前缀为下划线。 For example, _int4 or _text . 例如, _int4_text There is some additional information about the type in pg_type that may be helpful. 有一些关于pg_type中类型的其他信息可能会有所帮助。

For the dimensions, it appears unlikely that they will be in the metadata. 对于维度,它们似乎不太可能在元数据中。 From the documentation : 文档

However, the current implementation ignores any supplied array size limits, ie, the behavior is the same as for arrays of unspecified length. 但是,当前实现忽略任何提供的数组大小限制,即行为与未指定长度的数组相同。

The current implementation does not enforce the declared number of dimensions either. 当前实现也不强制声明的维数。 Arrays of a particular element type are all considered to be of the same type, regardless of size or number of dimensions. 无论尺寸大小或数量如何,特定元素类型的数组都被认为是相同类型的。 So, declaring the array size or number of dimensions in CREATE TABLE is simply documentation; 因此,在CREATE TABLE中声明数组大小或维数是简单的文档; it does not affect run-time behavior. 它不会影响运行时行为。

The array_dims function will return the current dimensions of an array value. array_dims函数将返回数组值的当前维度。 But since this could be different for every row in the table, I doubt this will help you. 但由于表格中的每一行都有所不同,我怀疑这会对你有所帮助。

UPDATE : It appears the dimensions are available in the metadata. 更新 :似乎元数据中提供了维度。 See @a_horse_with_no_name's answer . 请参阅@ a_horse_with_no_name的答案

In addition to what @Joe already cleared up , you can use pg_typof() to get type information from PostgreSQL. 除了@Joe已经清理的内容之外 ,您还可以使用pg_typof()从PostgreSQL中获取类型信息。

Given a 2-dimensional array of integer in this example: 在此示例中给定一个2维的整数数组:

SELECT pg_typeof(a)::text AS type
      ,(SELECT typname FROM pg_type WHERE oid = pg_typeof(a)) AS base_type
      ,array_dims(a) AS dims
FROM   (SELECT '{{11,12,13},{21,22,23}}'::int[]) x(a);

   type    | base_type |    dims
-----------+-----------+------------
 integer[] | _int4     | [1:2][1:3]

Note that the array dimensions ( dims ) can be different for each value. 请注意,每个值的数组维度( dims )可以不同。 Postgres does not enforce dimensions at present (up to and incl. v9.3). Postgres目前不强制执行维度(直到并包括v9.3)。
As documented in the manual , pg_attribute.attndims is of limited use here: 如手册中所述pg_attribute.attndims在这里使用有限:

Number of dimensions, if the column is an array type; 如果列是数组类型,则维数; otherwise 0. (Presently, the number of dimensions of an array is not enforced, so any nonzero value effectively means "it's an array".) 否则为0.(目前,未强制执行数组的维数,因此任何非零值都有效地表示“它是一个数组”。)

You can query pg_attributes directly: 您可以直接查询pg_attributes

select att.attname, 
       att.attndims, 
       pg_catalog.format_type(atttypid, NULL) as display_type 
from pg_attribute att 
  join pg_class tbl on tbl.oid = att.attrelid  
  join pg_namespace ns on tbl.relnamespace = ns.oid  
where tbl.relname = 'your_table_name'  
  and ns.nspname = 'table_schema'

SQLFiddle example: http://sqlfiddle.com/#!12/50301/1 SQLFiddle示例: http ://sqlfiddle.com/#!12/50301/1

Note that format_type() will actually return integer[] even though the column was defined as int[][] but the attndims column will carry the information you want. 请注意,即使列定义为int[][]format_type()实际上也会返回integer[] int[][]attndims列将包含您想要的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM