如何通过 Java 获取 Postgres 中数组列的基本类型/维度?
我有一个表,其中包含像 int[][]
和 text[]
这样的数组。
当我从 JDBC 遍历元数据时,我只能得到 java.sql.Array
类型。
即使在 information_schema.columns
中,它也简单地存储为 ARRAY。
如何知道基类型及其维度?
我现在正在研究转储表信息的工具。
最佳答案
对于数组基类型,DatabaseMetaData.getColumns()
返回包含列元数据的 ResultSet
。返回的列之一是 TYPE_NAME
。这似乎包含数组基本类型的名称,前缀为下划线。例如,_int4
或 _text
。 pg_type 中有一些关于类型的额外信息这可能会有帮助。
对于维度,它们似乎不太可能出现在元数据中。来自documentation :
However, the current implementation ignores any supplied array size limits, i.e., 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; it does not affect run-time behavior.
array_dims
函数将返回数组值的当前维度。但由于表格中的每一行都可能不同,我怀疑这对您有帮助。
更新:这些维度似乎在元数据中可用。参见 @a_horse_with_no_name's answer .
关于java - 如何通过jdbc在postgres中获取数组基类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16619113/