001/*
002 * HA-JDBC: High-Availability JDBC
003 * Copyright (c) 2004-2007 Paul Ferraro
004 * 
005 * This library is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Lesser General Public License as published by the 
007 * Free Software Foundation; either version 2.1 of the License, or (at your 
008 * option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful, but WITHOUT
011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 
013 * for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public License
016 * along with this library; if not, write to the Free Software Foundation, 
017 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018 * 
019 * Contact: ferraro@users.sourceforge.net
020 */
021package net.sf.hajdbc.cache;
022
023import java.sql.DatabaseMetaData;
024import java.sql.SQLException;
025import java.util.Collection;
026import java.util.List;
027import java.util.Map;
028
029import net.sf.hajdbc.ColumnProperties;
030import net.sf.hajdbc.ForeignKeyConstraint;
031import net.sf.hajdbc.QualifiedName;
032import net.sf.hajdbc.SequenceProperties;
033import net.sf.hajdbc.UniqueConstraint;
034
035/**
036 * Processes database meta data into useful structures.
037 * @author Paul Ferraro
038 */
039public interface DatabaseMetaDataSupport
040{
041        /**
042         * Returns all tables in this database mapped by schema.
043         * @param metaData a DatabaseMetaData implementation
044         * @return a Map of schema name to Collection of table names
045         * @throws SQLException if an error occurs access DatabaseMetaData
046         */
047        public Collection<QualifiedName> getTables(DatabaseMetaData metaData) throws SQLException;
048
049        /**
050         * Returns the columns of the specified table.
051         * @param metaData a DatabaseMetaData implementation
052         * @param table a schema qualified table name
053         * @return a Map of column name to column properties
054         * @throws SQLException if an error occurs access DatabaseMetaData
055         */
056        public Map<String, ColumnProperties> getColumns(DatabaseMetaData metaData, QualifiedName table) throws SQLException;
057
058        /**
059         * Returns the primary key of the specified table.
060         * @param metaData a DatabaseMetaData implementation
061         * @param table a schema qualified table name
062         * @return a unique constraint
063         * @throws SQLException if an error occurs access DatabaseMetaData
064         */
065        public UniqueConstraint getPrimaryKey(DatabaseMetaData metaData, QualifiedName table) throws SQLException;
066
067        /**
068         * Returns the foreign key constraints on the specified table.
069         * @param metaData a DatabaseMetaData implementation
070         * @param table a schema qualified table name
071         * @return a Collection of foreign key constraints.
072         * @throws SQLException if an error occurs access DatabaseMetaData
073         */
074        public Collection<ForeignKeyConstraint> getForeignKeyConstraints(DatabaseMetaData metaData, QualifiedName table) throws SQLException;
075
076        /**
077         * Returns the unique constraints on the specified table - excluding the primary key of the table.
078         * @param metaData a schema qualified table name
079         * @param table a qualified table name
080         * @param primaryKey the primary key of this table
081         * @return a Collection of unique constraints.
082         * @throws SQLException if an error occurs access DatabaseMetaData
083         */
084        public Collection<UniqueConstraint> getUniqueConstraints(DatabaseMetaData metaData, QualifiedName table, UniqueConstraint primaryKey) throws SQLException;
085
086        /**
087         * Returns the schema qualified name of the specified table suitable for use in a data modification language (DML) statement.
088         * @param name a schema qualified name
089         * @return a Collection of unique constraints.
090         */
091        public String qualifyNameForDML(QualifiedName name);
092
093        /**
094         * Returns the schema qualified name of the specified table suitable for use in a data definition language (DDL) statement.
095         * @param name a schema qualified name
096         * @return a Collection of unique constraints.
097         */
098        public String qualifyNameForDDL(QualifiedName name);
099        
100        /**
101         * Returns a collection of sequences using dialect specific logic.
102         * @param metaData database meta data
103         * @return a collection of sequences
104         * @throws SQLException
105         */
106        public Collection<SequenceProperties> getSequences(DatabaseMetaData metaData) throws SQLException;
107        
108        /**
109         * Locates an object from a map keyed by schema qualified name.
110         * @param <T> an object
111         * @param map a map of database 
112         * @param name the name of the object to locate
113         * @param defaultSchemaList a list of default schemas
114         * @return the object with the specified name
115         * @throws SQLException
116         */
117        public <T> T find(Map<String, T> map, String name, List<String> defaultSchemaList) throws SQLException;
118        
119        /**
120         * Identifies any identity columns from the from the specified collection of columns
121         * @param columns the columns of a table
122         * @return a collection of column names
123         * @throws SQLException
124         */
125        public Collection<String> getIdentityColumns(Collection<ColumnProperties> columns) throws SQLException;
126}