Class ArrayType


public class ArrayType extends AbstractDataType
Adapter to handle conversion between PostgreSQL array columns (Types.ARRAY, e.g. integer[], text[]) and their PostgreSQL literal text representation, such as {1,2,3} or {"a","b","c"}.

PostgreSQL reports every array column's sql type name as its element type's own pg_catalog name prefixed with an underscore (for example _int4 for an integer[] column); PostgresqlDataTypeFactory constructs the matching instance for each column automatically, passing that name through unchanged. There is normally no need to instantiate this class directly.

This class round-trips a column's literal text between the driver and the dataset; it does not typecast individual elements to their Java equivalents, and comparisons against an expected dataset value are plain string comparisons. On write, the literal text is split into its top-level elements - honoring double-quoted elements (backslash-escaped internally), backslash-escaped characters outside quotes, and the unquoted NULL keyword - and bound via Connection.createArrayOf(String, Object[]), letting PostgreSQL itself parse and validate each element against the column's actual base type. Only single-dimension arrays are supported for writing; a literal containing a nested array (a multi-dimensional array) is rejected with a TypeCastException. Reading one back for comparison or export works fine either way, since the literal text is never parsed on read.

Since:
3.5.0
Author:
Jeff Jensen
  • Constructor Details

    • ArrayType

      public ArrayType(String sqlTypeName)
      Creates a data type adapter for the given PostgreSQL array sql type name.
      Parameters:
      sqlTypeName - The sql type name reported for the array column, e.g. "_int4" for an integer[] column.
  • Method Details

    • getSqlValue

      public Object getSqlValue(int column, ResultSet resultSet) throws SQLException, TypeCastException
      Description copied from class: DataType
      Returns the specified column value from the specified resultset object.
      Overrides:
      getSqlValue in class AbstractDataType
      Parameters:
      column - the column index to read, starting at 1.
      resultSet - the result set to read the column value from.
      Returns:
      the column value.
      Throws:
      SQLException - if a database access error occurs.
      TypeCastException - if the value cannot be typecast to this DataType.
    • setSqlValue

      public void setSqlValue(Object value, int column, PreparedStatement statement) throws SQLException, TypeCastException
      Description copied from class: DataType
      Set the specified value to the specified prepared statement object.
      Overrides:
      setSqlValue in class AbstractDataType
      Parameters:
      value - the value to bind.
      column - the parameter index to bind to, starting at 1.
      statement - the prepared statement to bind the value on.
      Throws:
      SQLException - if a database access error occurs.
      TypeCastException - if the value cannot be typecast to this DataType.
    • typeCast

      public Object typeCast(Object value) throws TypeCastException
      Description copied from class: DataType
      Returns the specified value typecasted to this DataType
      Specified by:
      typeCast in class DataType
      Parameters:
      value - the value to typecast.
      Returns:
      the typecast value.
      Throws:
      TypeCastException - if the value cannot be typecast to this DataType.
    • getSqlTypeName

      public String getSqlTypeName()
      Returns the sql type name this instance handles.
      Overrides:
      getSqlTypeName in class DataType
      Returns:
      The array sql type name this instance was constructed with, e.g. "_int4".