Class TypeInfoCache

  • All Implemented Interfaces:
    TypeInfo

    public class TypeInfoCache
    extends java.lang.Object
    implements TypeInfo
    • Field Detail

      • LOGGER

        private static final java.util.logging.Logger LOGGER
      • pgNameToSQLType

        private final java.util.Map<java.lang.String,​java.lang.Integer> pgNameToSQLType
      • oidToSQLType

        private final java.util.Map<java.lang.Integer,​java.lang.Integer> oidToSQLType
      • pgNameToJavaClass

        private final java.util.Map<java.lang.String,​java.lang.String> pgNameToJavaClass
      • oidToPgName

        private final java.util.Map<java.lang.Integer,​java.lang.String> oidToPgName
      • pgNameToOid

        private final java.util.Map<java.lang.String,​java.lang.Integer> pgNameToOid
      • javaArrayTypeToOid

        private final java.util.Map<java.lang.String,​java.lang.Integer> javaArrayTypeToOid
      • pgNameToPgObject

        private final java.util.Map<java.lang.String,​java.lang.Class<? extends PGobject>> pgNameToPgObject
      • pgArrayToPgType

        private final java.util.Map<java.lang.Integer,​java.lang.Integer> pgArrayToPgType
      • arrayOidToDelimiter

        private final java.util.Map<java.lang.Integer,​java.lang.Character> arrayOidToDelimiter
      • unknownLength

        private final int unknownLength
      • getOidStatementSimple

        private java.sql.PreparedStatement getOidStatementSimple
      • getOidStatementComplexNonArray

        private java.sql.PreparedStatement getOidStatementComplexNonArray
      • getOidStatementComplexArray

        private java.sql.PreparedStatement getOidStatementComplexArray
      • getNameStatement

        private java.sql.PreparedStatement getNameStatement
      • getArrayElementOidStatement

        private java.sql.PreparedStatement getArrayElementOidStatement
      • getArrayDelimiterStatement

        private java.sql.PreparedStatement getArrayDelimiterStatement
      • getTypeInfoStatement

        private java.sql.PreparedStatement getTypeInfoStatement
      • getAllTypeInfoStatement

        private java.sql.PreparedStatement getAllTypeInfoStatement
      • types

        private static final java.lang.Object[][] types
      • TYPE_ALIASES

        private static final java.util.concurrent.ConcurrentMap<java.lang.String,​java.lang.String> TYPE_ALIASES
        PG maps several alias to real type names. When we do queries against pg_catalog, we must use the real type, not an alias, so use this mapping.

        Additional values used at runtime (including case variants) will be added to the map.

    • Constructor Detail

      • TypeInfoCache

        public TypeInfoCache​(BaseConnection conn,
                             int unknownLength)
    • Method Detail

      • addCoreType

        public void addCoreType​(java.lang.String pgTypeName,
                                java.lang.Integer oid,
                                java.lang.Integer sqlType,
                                java.lang.String javaClass,
                                java.lang.Integer arrayOid)
        Specified by:
        addCoreType in interface TypeInfo
      • addDataType

        public void addDataType​(java.lang.String type,
                                java.lang.Class<? extends PGobject> klass)
                         throws java.sql.SQLException
        Specified by:
        addDataType in interface TypeInfo
        Throws:
        java.sql.SQLException
      • getPGTypeOidsWithSQLTypes

        public java.util.Iterator<java.lang.Integer> getPGTypeOidsWithSQLTypes()
        Specified by:
        getPGTypeOidsWithSQLTypes in interface TypeInfo
      • getSQLTypeQuery

        private static java.lang.String getSQLTypeQuery​(boolean typoidParam)
      • getSQLTypeFromQueryResult

        private static int getSQLTypeFromQueryResult​(java.sql.ResultSet rs)
                                              throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • prepareGetAllTypeInfoStatement

        private java.sql.PreparedStatement prepareGetAllTypeInfoStatement()
                                                                   throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • cacheSQLTypes

        public void cacheSQLTypes()
                           throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • prepareGetTypeInfoStatement

        private java.sql.PreparedStatement prepareGetTypeInfoStatement()
                                                                throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • getSQLType

        public int getSQLType​(java.lang.String pgTypeName)
                       throws java.sql.SQLException
        Description copied from interface: TypeInfo
        Look up the SQL typecode for a given postgresql type name.
        Specified by:
        getSQLType in interface TypeInfo
        Parameters:
        pgTypeName - the server type name to look up
        Returns:
        the SQL type code (a constant from Types) for the type
        Throws:
        java.sql.SQLException - if an error occurs when retrieving sql type
      • getJavaArrayType

        public int getJavaArrayType​(java.lang.String className)
                             throws java.sql.SQLException
        Specified by:
        getJavaArrayType in interface TypeInfo
        Throws:
        java.sql.SQLException
      • getSQLType

        public int getSQLType​(int typeOid)
                       throws java.sql.SQLException
        Description copied from interface: TypeInfo
        Look up the SQL typecode for a given type oid.
        Specified by:
        getSQLType in interface TypeInfo
        Parameters:
        typeOid - the type's OID
        Returns:
        the SQL type code (a constant from Types) for the type
        Throws:
        java.sql.SQLException - if an error occurs when retrieving sql type
      • getOidStatement

        private java.sql.PreparedStatement getOidStatement​(java.lang.String pgTypeName)
                                                    throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • getPGType

        public int getPGType​(java.lang.String pgTypeName)
                      throws java.sql.SQLException
        Description copied from interface: TypeInfo
        Look up the oid for a given postgresql type name. This is the inverse of TypeInfo.getPGType(int).
        Specified by:
        getPGType in interface TypeInfo
        Parameters:
        pgTypeName - the server type name to look up
        Returns:
        the type's OID, or 0 if unknown
        Throws:
        java.sql.SQLException - if an error occurs when retrieving PG type
      • getPGType

        public java.lang.String getPGType​(int oid)
                                   throws java.sql.SQLException
        Description copied from interface: TypeInfo
        Look up the postgresql type name for a given oid. This is the inverse of TypeInfo.getPGType(String).
        Specified by:
        getPGType in interface TypeInfo
        Parameters:
        oid - the type's OID
        Returns:
        the server type name for that OID or null if unknown
        Throws:
        java.sql.SQLException - if an error occurs when retrieving PG type
      • prepareGetNameStatement

        private java.sql.PreparedStatement prepareGetNameStatement()
                                                            throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • getPGArrayType

        public int getPGArrayType​(java.lang.String elementTypeName)
                           throws java.sql.SQLException
        Description copied from interface: TypeInfo
        Determine the oid of the given base postgresql type's array type.
        Specified by:
        getPGArrayType in interface TypeInfo
        Parameters:
        elementTypeName - the base type's
        Returns:
        the array type's OID, or 0 if unknown
        Throws:
        java.sql.SQLException - if an error occurs when retrieving array type
      • convertArrayToBaseOid

        protected int convertArrayToBaseOid​(int oid)
        Return the oid of the array's base element if it's an array, if not return the provided oid. This doesn't do any database lookups, so it's only useful for the originally provided type mappings. This is fine for it's intended uses where we only have intimate knowledge of types that are already known to the driver.
        Parameters:
        oid - input oid
        Returns:
        oid of the array's base element or the provided oid (if not array)
      • getArrayDelimiter

        public char getArrayDelimiter​(int oid)
                               throws java.sql.SQLException
        Description copied from interface: TypeInfo
        Determine the delimiter for the elements of the given array type oid.
        Specified by:
        getArrayDelimiter in interface TypeInfo
        Parameters:
        oid - the array type's OID
        Returns:
        the base type's array type delimiter
        Throws:
        java.sql.SQLException - if an error occurs when retrieving array delimiter
      • prepareGetArrayDelimiterStatement

        private java.sql.PreparedStatement prepareGetArrayDelimiterStatement()
                                                                      throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • getPGArrayElement

        public int getPGArrayElement​(int oid)
                              throws java.sql.SQLException
        Description copied from interface: TypeInfo
        Look up the oid of an array's base type given the array's type oid.
        Specified by:
        getPGArrayElement in interface TypeInfo
        Parameters:
        oid - the array type's OID
        Returns:
        the base type's OID, or 0 if unknown
        Throws:
        java.sql.SQLException - if an error occurs when retrieving array element
      • prepareGetArrayElementOidStatement

        private java.sql.PreparedStatement prepareGetArrayElementOidStatement()
                                                                       throws java.sql.SQLException
        Throws:
        java.sql.SQLException
      • getPGobject

        public java.lang.Class<? extends PGobject> getPGobject​(java.lang.String type)
        Specified by:
        getPGobject in interface TypeInfo
      • getJavaClass

        public java.lang.String getJavaClass​(int oid)
                                      throws java.sql.SQLException
        Specified by:
        getJavaClass in interface TypeInfo
        Throws:
        java.sql.SQLException
      • getTypeForAlias

        public java.lang.String getTypeForAlias​(java.lang.String alias)
        Specified by:
        getTypeForAlias in interface TypeInfo
      • getPrecision

        public int getPrecision​(int oid,
                                int typmod)
        Specified by:
        getPrecision in interface TypeInfo
      • getScale

        public int getScale​(int oid,
                            int typmod)
        Specified by:
        getScale in interface TypeInfo
      • isCaseSensitive

        public boolean isCaseSensitive​(int oid)
        Specified by:
        isCaseSensitive in interface TypeInfo
      • isSigned

        public boolean isSigned​(int oid)
        Specified by:
        isSigned in interface TypeInfo
      • getDisplaySize

        public int getDisplaySize​(int oid,
                                  int typmod)
        Specified by:
        getDisplaySize in interface TypeInfo
      • requiresQuoting

        public boolean requiresQuoting​(int oid)
                                throws java.sql.SQLException
        Specified by:
        requiresQuoting in interface TypeInfo
        Throws:
        java.sql.SQLException
      • requiresQuotingSqlType

        public boolean requiresQuotingSqlType​(int sqlType)
                                       throws java.sql.SQLException
        Returns true if particular sqlType requires quoting. This method is used internally by the driver, so it might disappear without notice.
        Specified by:
        requiresQuotingSqlType in interface TypeInfo
        Parameters:
        sqlType - sql type as in java.sql.Types
        Returns:
        true if the type requires quoting
        Throws:
        java.sql.SQLException - if something goes wrong
      • longOidToInt

        public int longOidToInt​(long oid)
                         throws java.sql.SQLException
        Description copied from interface: TypeInfo
        Java Integers are signed 32-bit integers, but oids are unsigned 32-bit integers. We therefore read them as positive long values and then force them into signed integers (wrapping around into negative values when required) or we'd be unable to correctly handle the upper half of the oid space.

        This function handles the mapping of uint32-values in the long to java integers, and throws for values that are out of range.

        Specified by:
        longOidToInt in interface TypeInfo
        Parameters:
        oid - the oid as a long.
        Returns:
        the (internal) signed integer representation of the (unsigned) oid.
        Throws:
        java.sql.SQLException - if the long has a value outside of the range representable by uint32
      • intOidToLong

        public long intOidToLong​(int oid)
        Description copied from interface: TypeInfo
        Java Integers are signed 32-bit integers, but oids are unsigned 32-bit integers. We must therefore first map the (internal) integer representation to a positive long value before sending it to postgresql, or we would be unable to correctly handle the upper half of the oid space because these negative values are disallowed as OID values.
        Specified by:
        intOidToLong in interface TypeInfo
        Parameters:
        oid - the (signed) integer oid to convert into a long.
        Returns:
        the non-negative value of this oid, stored as a java long.