/* * Created on 8/04/2003 * * To change the template for this generated file go to * Window>Preferences>Java>Code Generation>Code and Comments */ package net.sourceforge.phpdt.sql.sql.metadata; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.ResultSet; import java.sql.SQLException; /** * @author panic * * Groups functions that allow extracting data from a JDBC connection in form of StringMatrixs */ public class MetaDataJDBCInterface { /** * @param con : A valid (open) connection to an JDBC database * @param schema : Schema of the table. Must be null if not given * @param table : Name of the table. * @return a StringMatrix with the info of the columns' metadata */ public static StringMatrix getColumns(Connection con, String schema, String table) throws SQLException { DatabaseMetaData meta = con.getMetaData(); ResultSet set = meta.getColumns(null, schema, table, null); StringMatrix columns = fillMatrix(set); set.close(); return columns; } /** * @param con : A valid (open) connection to an JDBC database * @param schema : Schema of the table. Must be null if not given * @param table : Name of the table. * @return a StringMatrix with the info of the primary keys */ public static StringMatrix getPrimaryKeys(Connection con, String schema, String table) throws SQLException { DatabaseMetaData meta = con.getMetaData(); ResultSet set = meta.getPrimaryKeys(null, schema, table); StringMatrix keys = fillMatrix(set); set.close(); return keys; } /** * @param con : A valid (open) connection to an JDBC database * @param schema : Schema of the table. Must be null if not given * @param table : Name of the table. * @param imported : Determines if the foreign keys are the imported or exported ones * @return a StringMatrix with the info of the foreign keys */ public static StringMatrix getForeignKeys(Connection con, String schema, String table, boolean imported ) throws SQLException { ResultSet set = null; DatabaseMetaData meta = con.getMetaData(); if (imported){ set = meta.getImportedKeys(null, schema, table); } else { set = meta.getExportedKeys(null, schema, table); } StringMatrix keys = fillMatrix(set); set.close(); return keys; } /** * @param con : A valid (open) connection to an JDBC database * @param schema : Schema of the table. Must be null if not given * @param table : Name of the table. * @return a StringMatrix with the info of the indexes on that table */ public static StringMatrix getIndexInfo(Connection con, String schema, String table) throws SQLException { ResultSet set = null; DatabaseMetaData meta = con.getMetaData(); set = meta.getIndexInfo(null, schema, table, false, false); StringMatrix keys = fillMatrix(set); set.close(); return keys; } /** * * @param set * @return a filled StringMatrix with the set results * @throws SQLException */ public static StringMatrix fillMatrix(ResultSet set) throws SQLException { int columnCount = set.getMetaData().getColumnCount(); StringMatrix keys = new StringMatrix(); for (int i = 1; i <= columnCount; i++) { keys.addHeader(set.getMetaData().getColumnName(i)); } int row = 0; while (set.next()) { for (int i = 1; i <= columnCount; i++) { keys.add(set.getString(i), row); } row++; } return keys; } }