Class JDBCMetaDataBasedModelElementFinder

java.lang.Object
net.sf.jailer.modelbuilder.JDBCMetaDataBasedModelElementFinder
All Implemented Interfaces:
ModelElementFinder

public class JDBCMetaDataBasedModelElementFinder extends Object implements ModelElementFinder
Finds associations and tables by analyzing the JDBC meta data.
Author:
Ralf Wisser
  • Field Details

  • Constructor Details

    • JDBCMetaDataBasedModelElementFinder

      public JDBCMetaDataBasedModelElementFinder()
  • Method Details

    • findAssociations

      public Collection<Association> findAssociations(DataModel dataModel, Map<Association,String[]> namingSuggestion, Session session, ExecutionContext executionContext) throws Exception
      Finds associations by reading the databases meta-data.
      Specified by:
      findAssociations in interface ModelElementFinder
      Parameters:
      dataModel - model containing already known elements
      namingSuggestion - to put naming suggestions for associations into
      session - the statement executor for executing SQL-statements
      executionContext - the execution context
      Returns:
      found associations
      Throws:
      Exception - on each error
    • getImportedKeys

      public static ResultSet getImportedKeys(Session session, String schema, String table, boolean withCaching) throws SQLException
      Gets the imported keys (foreign keys) for a table, optionally using a cache.
      Parameters:
      session - the database session
      schema - the schema name
      table - the table name
      withCaching - if true, use a cached result set if available
      Returns:
      a result set describing the imported keys
      Throws:
      SQLException
    • getExportedKeys

      public static ResultSet getExportedKeys(Session session, String schema, String table) throws SQLException
      Gets the exported keys (referenced by foreign keys in other tables) for a table.
      Parameters:
      session - the database session
      schema - the schema name
      table - the table name
      Returns:
      a result set describing the exported keys
      Throws:
      SQLException
    • findTables

      public Set<Table> findTables(Session session, ExecutionContext executionContext) throws Exception
      Finds all tables in the DB schema.
      Specified by:
      findTables in interface ModelElementFinder
      Parameters:
      session - the statement executor for executing SQL-statements
      executionContext - the execution context
      Returns:
      the set of tables found in the schema
      Throws:
      Exception - on each error
    • findPartitions

      public static Set<String> findPartitions(Session session, String introspectionSchema)
      Finds all table names that are partitions in the given schema.
      Parameters:
      session - the database session
      introspectionSchema - the schema to search for partitions
      Returns:
      a set of table names that are partitions
    • getPrimaryKeys

      public static ResultSet getPrimaryKeys(Session session, String schema, String table, boolean withCaching) throws SQLException
      Retrieves primary key information for a given table.
      Parameters:
      session - the database session
      schema - the schema name
      table - the table name
      withCaching - if true, use a cached result set if available
      Returns:
      a result set describing the primary keys
      Throws:
      SQLException
    • getTables

      public static ResultSet getTables(Session session, String schemaPattern, String tableNamePattern, String[] types) throws SQLException
      Calls DatabaseMetaData.getTables(String, String, String, String[]). Uses schemaPattern as catalogPattern on MySQL.
      Parameters:
      session - the database session
      schemaPattern - the schema name pattern
      tableNamePattern - the table name pattern
      types - the list of table types to include
      Returns:
      a result set describing the matching tables
      Throws:
      SQLException
    • getComments

      public Map<Pair<Table,Column>,String> getComments()
      Finds comments for all model elements.
      Specified by:
      getComments in interface ModelElementFinder
      Returns:
      map from element (table, column) to the comment
    • getIndexes

      public static ResultSet getIndexes(Session session, String schemaPattern, String tableNamePattern) throws SQLException
      Calls DatabaseMetaData.getIndexInfo(String, String, String, boolean, boolean). Uses schemaPattern as catalogPattern on MySQL.
      Parameters:
      session - the database session
      schemaPattern - the schema name pattern
      tableNamePattern - the table name pattern
      Returns:
      a result set describing the indexes of matching tables
      Throws:
      SQLException
    • getColumns

      public static ResultSet getColumns(Session session, String schemaPattern, String tableNamePattern, String columnNamePattern, boolean withCaching, boolean onlyIfCached, String tableType) throws SQLException
      Calls DatabaseMetaData.getColumns(String, String, String, String). Uses schemaPattern as catalogPattern on MySQL.
      Parameters:
      session - the database session
      schemaPattern - the schema name pattern
      tableNamePattern - the table name pattern
      columnNamePattern - the column name pattern
      withCaching - if true, use a cached result set if available
      onlyIfCached - if true, return only from cache; skip if cache is not populated
      tableType - the table type (e.g. "TABLE", "SYNONYM")
      Returns:
      a result set describing the matching columns
      Throws:
      SQLException
    • getProcedures

      public static ResultSet getProcedures(Session session, String schemaPattern, String functionPattern) throws SQLException
      Calls DatabaseMetaData.getProcedures(String, String, String). Uses schemaPattern as catalogPattern on MySQL.
      Parameters:
      session - the database session
      schemaPattern - the schema name pattern
      functionPattern - the procedure/function name pattern
      Returns:
      a result set describing matching procedures and functions
      Throws:
      SQLException
    • getSchemas

      public static List<String> getSchemas(Session session, String userName)
      Finds all schemas in DB.
      Parameters:
      session - the statement executor for executing SQL-statements
      userName - schema with this name may be empty
      Returns:
      sorted list of schema names
    • getCatalogsWithSchemas

      public static List<String> getCatalogsWithSchemas(Session session)
      Finds all catalogs with all schemas in DB.
      Parameters:
      session - the statement executor for executing SQL-statements
      Returns:
      sorted list of catalog.schema name pairs, or an empty list if not supported
    • getDefaultSchema

      public static String getDefaultSchema(Session session, String userName)
      Gets default schema of DB.
      Parameters:
      session - the statement executor for executing SQL-statements
      userName - schema with this name may be empty
      Returns:
      the default schema name
    • getDefaultSchema

      public static String getDefaultSchema(Session session, String userName, List<String> schemaNames)
      Gets default schema of DB.
      Parameters:
      session - the statement executor for executing SQL-statements
      userName - schema with this name may be empty
      schemaNames - optional list of known schema names to use for resolution
      Returns:
      the default schema name
    • findColumns

      public List<Column> findColumns(Table table, Session session, ExecutionContext executionContext) throws SQLException
      Finds the Columns of a given Table.
      Specified by:
      findColumns in interface ModelElementFinder
      Parameters:
      table - the table
      session - the statement executor for executing SQL-statements
      executionContext - the execution context
      Returns:
      the list of columns found for the given table
      Throws:
      Exception - on each error
      SQLException
    • filterLength

      public static int filterLength(int length, int precision, String typeName, int type, DBMS dbms, int origLength)
      Filters the length attribute of a column in a DBMS specific way.
      Parameters:
      length - the length as given from driver
      precision - the precision as given from driver
      typeName - the SQL type name
      type - the sql type
      dbms - the DBMS
      origLength - the original unfiltered length
      Returns:
      filtered length
    • filterPrecision

      public static int filterPrecision(int length, int precision, String typeName, int type, DBMS dbms, int origLength)
      Filters the precision attribute of a column in a DBMS specific way.
      Parameters:
      length - the length as given from driver
      precision - the precision as given from driver
      typeName - the SQL type name
      type - the sql type
      dbms - the DBMS
      origLength - the original unfiltered length
      Returns:
      filtered precision
    • filterType

      public static String filterType(String sqlType, int length, String typeName, int type, DBMS dbms, int origLength)
      Filters the type attribute of a column in a DBMS specific way.
      Parameters:
      sqlType - the SQL type name as given from driver
      length - the length as given from driver
      typeName - the native type name as given from driver
      type - the sql type code
      dbms - the DBMS
      origLength - the original unfiltered length
      Returns:
      filtered type name
    • toColumn

      public static Column toColumn(ResultSetMetaData metaData, int i, Session session) throws SQLException
      Converts a column from ResultSetMetaData into a Column model object.
      Parameters:
      metaData - the result set meta data
      i - the 1-based column index
      session - the database session
      Returns:
      the corresponding Column
      Throws:
      SQLException
    • resetCaches

      public static void resetCaches(Session session)
      Resets all cached metadata for the given session.
      Parameters:
      session - the session whose caches should be cleared
    • toString

      public String toString()
      Gets description.
      Overrides:
      toString in class Object