Class JdbcConnection

All Implemented Interfaces:
DataExceptionContributor, JsonSerializable, RecordSerializable, XmlSerializable, JdbcConnectionFactory, Serializable

public class JdbcConnection extends FoundationObject implements JdbcConnectionFactory
Describes a database (driver, URL, credentials and driver properties), creates JDBC connections to it and loads its table, schema and query metadata.
See Also:
  • Constructor Details

    • JdbcConnection

      public JdbcConnection()
  • Method Details

    • getListener

      public JdbcConnectionListener getListener()
      Returns the listener notified as loadTables(LoadTablesRequest) progresses; never null.
    • setListener

      public JdbcConnection setListener(JdbcConnectionListener listener)
      Sets the listener notified as loadTables(LoadTablesRequest) progresses; null installs a no-op listener.
    • getName

      public String getName()
    • setName

      public JdbcConnection setName(String name)
    • getConnectionFactory

      public JdbcConnectionFactory getConnectionFactory()
      Returns the factory createConnection() uses in place of the driver, URL, credentials and properties, or null.
    • setConnectionFactory

      public JdbcConnection setConnectionFactory(JdbcConnectionFactory connectionFactory)
      Sets a factory createConnection() uses in place of the driver, URL, credentials and properties; null removes it.
    • getDriverClassName

      public String getDriverClassName()
    • setDriverClassName

      public JdbcConnection setDriverClassName(String driverClassName)
    • getUrl

      public String getUrl()
    • setUrl

      public JdbcConnection setUrl(String url)
    • getUsername

      public String getUsername()
    • setUsername

      public JdbcConnection setUsername(String username)
    • getEncodedPassword

      public String getEncodedPassword()
      Returns the password as encoded by the password encoder (hex by default).
    • setEncodedPassword

      public JdbcConnection setEncodedPassword(String password)
      Sets the password already encoded with the password encoder.
    • getPlainTextPassword

      public String getPlainTextPassword()
      Returns the password decoded with the password encoder.
    • setPlainTextPassword

      public JdbcConnection setPlainTextPassword(String password)
      Encodes the password with the password encoder and stores it.
    • setPasswordEncoder

      public JdbcConnection setPasswordEncoder(Encoder<String,String> passwordEncoder)
      Sets the encoder used to store the password (defaults to Encoder.HEX); null stores it as plain text. An already stored password is not re-encoded, so set the encoder first.
    • getPasswordEncoder

      public Encoder<String,String> getPasswordEncoder()
      Returns the encoder used to store the password (defaults to Encoder.HEX).
    • isUseCatalog

      public boolean isUseCatalog()
      Indicates if each table's catalog is passed to the column, key and index metadata lookups instead of null (default is false).
    • setUseCatalog

      public JdbcConnection setUseCatalog(boolean useCatalog)
      Indicates if each table's catalog is passed to the column, key and index metadata lookups instead of null (default is false).
    • getProperties

      public List<Property> getProperties()
      Returns the extra properties passed to the JDBC driver by createConnection().
    • addProperty

      public JdbcConnection addProperty(Property property)
      Adds a property passed to the JDBC driver by createConnection().
    • addProperty

      public JdbcConnection addProperty(String name, String value)
      Adds a property passed to the JDBC driver by createConnection().
    • getTable

      public JdbcTable getTable(String name)
      Returns the first table with the given name, ignoring case and schema, or null if none matches.
    • getTable

      public JdbcTable getTable(String schemaName, String name)
      Returns the table with the given schema and name, ignoring case, or null; a null schema matches only tables without one.
    • getTable

      public JdbcTable getTable(String catalogName, String schemaName, String name)
      Returns the table with the given catalog, schema and name, ignoring case, or null; a null or empty catalog never matches.
    • getTables

      public List<JdbcTable> getTables()
    • getTablesSorted

      public List<JdbcTable> getTablesSorted()
      Returns a list of of tables sorted alphabetically.
    • getTablesSortedTopologically

      public List<JdbcTable> getTablesSortedTopologically()
      Returns a list of of tables sorted alphabetically, but with dependencies first. If table A depends on table B, table B will be placed above table A in the returned list.
    • addTable

      public JdbcConnection addTable(JdbcTable table)
      Adds the table and sets this as its connection.
    • getQueries

      public List<JdbcQuery> getQueries()
    • addQuery

      public JdbcConnection addQuery(JdbcQuery query)
      Adds the query and sets this as its connection.
    • addQuery

      public JdbcConnection addQuery(String name, String query)
      Adds a named SQL query; loadQueries() reads its result columns.
    • getJavaTypeOverrides

      public Map<String,Class<?>> getJavaTypeOverrides()
    • setJavaTypeOverride

      public JdbcConnection setJavaTypeOverride(String databaseTypeName, Class<?> javaType)
      Uses the given Java type for table and query columns of this database type name instead of the mapper's choice.
    • getJavaTypeOverride

      public Class<?> getJavaTypeOverride(String databaseTypeName)
      Returns the Java type override for the database type name, ignoring case, or null if there is none.
    • getSqlToJavaTypeMapper

      public JdbcValueReader getSqlToJavaTypeMapper()
      Returns the strategy that picks each column's Java type from its SQL type (defaults to JdbcValueReader.OPINIONATED).
    • setSqlToJavaTypeMapper

      public JdbcConnection setSqlToJavaTypeMapper(JdbcValueReader sqlToJavaTypeMapper)
      Sets the strategy that picks each column's Java type from its SQL type; null restores JdbcValueReader.OPINIONATED.
    • getIsCatalogAtStart

      public Boolean getIsCatalogAtStart()
      Indicates if the catalog appears at the start of a qualified table name; null until loadCatalogAndSchemas() runs.
    • getSupportsCatalogsInDataManipulation

      public Boolean getSupportsCatalogsInDataManipulation()
      Indicates if the database accepts catalog names in data manipulation statements; null until loadCatalogAndSchemas() runs.
    • getSupportsCatalogsInTableDefinitions

      public Boolean getSupportsCatalogsInTableDefinitions()
      Indicates if the database accepts catalog names in table definition statements; null until loadCatalogAndSchemas() runs.
    • getSupportsCatalogsInPrivilegeDefinitions

      public Boolean getSupportsCatalogsInPrivilegeDefinitions()
      Indicates if the database accepts catalog names in privilege definition statements; null until loadCatalogAndSchemas() runs.
    • getCatalogNames

      public List<String> getCatalogNames()
      Returns the database's catalog names, as filled by loadCatalogAndSchemas().
    • addSchema

      public JdbcConnection addSchema(JdbcSchema jdbcSchema)
    • getSchemas

      public List<JdbcSchema> getSchemas()
      Returns the database's schemas, as filled by loadCatalogAndSchemas().
    • createConnection

      public Connection createConnection()
      Description copied from interface: JdbcConnectionFactory
      Returns a connection to use; factories from JdbcConnectionFactory.wrap(Connection) return the same connection on every call.
      Specified by:
      createConnection in interface JdbcConnectionFactory
    • loadTables

      public JdbcConnection loadTables()
      Loads the tables matching name pattern % in any catalog and schema, with their columns, keys and indexes, replacing those loaded before.
    • loadTables

      public JdbcConnection loadTables(String catalog, String schemaPattern, String tableNamePattern, String... types)
      Loads the tables matching the given DatabaseMetaData.getTables arguments, with their columns, keys and indexes, replacing those loaded before.
    • loadTables

      public JdbcConnection loadTables(LoadTablesRequest request)
      Loads the tables and details selected by the request, replacing those loaded before and notifying the listener at each step.
    • loadQueries

      public JdbcConnection loadQueries()
      Runs each query with its parameters' example values and replaces its columns with those of the result.
    • getQueryColumns

      public static List<JdbcQueryColumn> getQueryColumns(ResultSetMetaData resultSetMetaData, JdbcConnection jdbcConnection)
      Describes each result column, choosing its Java type from the connection's type overrides or SQL-to-Java type mapper.
    • getSqlOrderBy

      public static String getSqlOrderBy(String column)
      Returns an ORDER BY clause (with a leading space) for the column, descending if its name starts with -, or an empty string if it is empty; the name is not quoted or escaped.
    • loadCatalogAndSchemas

      public JdbcConnection loadCatalogAndSchemas()
      Replaces the catalog names and schemas with those reported by the database and records its catalog support flags.
    • toRecord

      public Record toRecord()
      Description copied from interface: RecordSerializable
      Converts this object's state to a record that RecordSerializable.fromRecord(Record) can load.
      Specified by:
      toRecord in interface RecordSerializable
      Overrides:
      toRecord in class Bean
    • fromRecord

      public JdbcConnection fromRecord(Record source)
      Description copied from interface: RecordSerializable
      Loads this instance's state from a record and returns this (for fluid API call chaining). For fluid API call chaining, the overridden method should change the declared return type to its class.
      Specified by:
      fromRecord in interface RecordSerializable
      Overrides:
      fromRecord in class Bean
      Parameters:
      source -
      Returns:
      this instance.