5 package com.quantum.sql.metadata;
7 import java.sql.Connection;
8 import java.sql.DatabaseMetaData;
9 import java.sql.ResultSet;
10 import java.sql.ResultSetMetaData;
11 import java.sql.SQLException;
12 import java.sql.Statement;
14 import com.quantum.util.StringMatrix;
17 * Groups functions that allow extracting data from a JDBC connection
18 * in the form of StringMatrix objects
23 public class MetaDataJDBCInterface {
26 * @param con : A valid (open) connection to an JDBC database
27 * @param schema : Schema of the table. Must be null if not given
28 * @param table : Name of the table.
29 * @return a StringMatrix with the info of the columns' metadata, null if error
31 public static StringMatrix getColumns(Connection con, String schema, String table)
34 DatabaseMetaData meta = con.getMetaData();
36 StringMatrix columns = null;
38 set = meta.getColumns(null, schema, table, null);
39 columns = fillMatrix(set);
42 } catch (SQLException e) {
44 if (columns == null) {
45 columns = new StringMatrix();
46 String query = "SELECT * FROM \"" + table + "\" WHERE (1 = 0)"; //$NON-NLS-1$ //$NON-NLS-2$
47 Statement stmt = con.createStatement();
49 ResultSet rset = stmt.getResultSet();
50 ResultSetMetaData rsMetaData = rset.getMetaData();
51 int columnCount = rsMetaData.getColumnCount();
52 String headMatrix[] = {"TABLE_CAT","TABLE_SCHEM","TABLE_NAME","COLUMN_NAME","DATA_TYPE","TYPE_NAME","COLUMN_SIZE",
53 "BUFFER_LENGTH","DECIMAL_DIGITS","NUM_PREC_RADIX","NULLABLE","REMARKS",
54 "COLUMN_DEF","SQL_DATA_TYPE","SQL_DATETIME_SUB","CHAR_OCTET_LENGTH",
55 "ORDINAL_POSITION","IS_NULLABLE","SCOPE_CATLOG","SCOPE_SCHEMA","SCOPE_TABLE","SOURCE_DATA_TYPE"};
56 columns.addMatrixHeader(headMatrix);
57 for (int i = 0; i < columnCount; i++) {
58 columns.add(null, i); /* TABLE_CAT */
59 columns.add(schema, i); /* TABLE_SCHEM */
60 columns.add(table, i); /* TABLE_NAME */
61 columns.add(rsMetaData.getColumnName(i+1), i); /* COLUMN_NAME */
62 columns.add(String.valueOf(rsMetaData.getColumnType(i+1)), i); /* DATA_TYPE */
63 columns.add(rsMetaData.getColumnTypeName(i+1), i); /* TYPE_NAME */
64 columns.add(String.valueOf(rsMetaData.getPrecision(i+1)), i); /* COLUMN_SIZE */
65 columns.add(null, i); /* BUFFER_LENGTH */
66 columns.add(String.valueOf(rsMetaData.getScale(i+1)), i); /* DECIMAL_DIGITS */
67 columns.add(null, i); /* NUM_PREC_RADIX */
68 int isNullable = rsMetaData.isNullable(i+1);
69 columns.add(String.valueOf(isNullable), i); /* NULLABLE */
70 columns.add(null, i); /* REMARKS */
71 columns.add(null, i); /* COLUMN_DEF */
72 columns.add(null, i); /* SQL_DATA_TYPE */
73 columns.add(null, i); /* SQL_DATETIME_SUB */
74 columns.add(String.valueOf(rsMetaData.getColumnDisplaySize(i+1)), i); /* CHAR_OCTET_LENGTH */
75 columns.add(String.valueOf(i+1), i); /* ORDINAL_POSITION */
76 columns.add(isNullable == DatabaseMetaData.columnNullable ? "YES" : "NO", i );
77 columns.add(null, i); /* SCOPE_CATLOG */
78 columns.add(null, i); /* SCOPE_SCHEMA */
79 columns.add(null, i); /* SCOPE_TABLE */
80 columns.add(null, i); /* SOURCE_DATA_TYPE */
87 * @param con : A valid (open) connection to an JDBC database
88 * @param schema : Schema of the table. Must be null if not given
89 * @param table : Name of the table.
90 * @return a StringMatrix with the info of the primary keys
92 public static StringMatrix getPrimaryKeys(Connection con, String schema, String table) {
94 StringMatrix keys = new StringMatrix();
96 DatabaseMetaData meta = con.getMetaData();
97 ResultSet set = meta.getPrimaryKeys(null, schema, table);
98 keys = fillMatrix(set);
100 } catch (SQLException e) {
107 * @param con : A valid (open) connection to an JDBC database
108 * @param schema : Schema of the table. Must be null if not given
109 * @param table : Name of the table.
110 * @param imported : Determines if the foreign keys are the imported or exported ones
111 * @return a StringMatrix with the info of the foreign keys
113 public static StringMatrix getForeignKeys(Connection con, String schema, String table, boolean imported ) {
115 ResultSet set = null;
116 StringMatrix keys = new StringMatrix();
118 DatabaseMetaData meta = con.getMetaData();
120 set = meta.getImportedKeys(null, schema, table);
122 set = meta.getExportedKeys(null, schema, table);
124 keys = fillMatrix(set);
126 } catch (SQLException e) {
133 * @param con : A valid (open) connection to an JDBC database
134 * @param schema : Schema of the table. Must be null if not given
135 * @param table : Name of the table.
136 * @return a StringMatrix with the info of the indexes on that table
138 public static StringMatrix getIndexInfo(Connection con, String schema, String table) {
140 ResultSet set = null;
141 DatabaseMetaData meta;
142 StringMatrix keys = new StringMatrix();
144 meta = con.getMetaData();
145 set = meta.getIndexInfo(null, schema, table, false, false);
146 keys = fillMatrix(set);
148 } catch (SQLException e) {
158 * @return a filled StringMatrix with the set results
159 * @throws SQLException
161 public static StringMatrix fillMatrix(ResultSet set) throws SQLException {
162 int columnCount = set.getMetaData().getColumnCount();
163 StringMatrix keys = new StringMatrix();
164 for (int i = 1; i <= columnCount; i++) {
165 keys.addHeader(set.getMetaData().getColumnName(i));
169 for (int i = 1; i <= columnCount; i++) {
170 keys.add(set.getString(i), row);
182 public static StringMatrix getBestRowId(Connection con, String schema, String table)
183 throws SQLException {
185 ResultSet set = null;
186 DatabaseMetaData meta = con.getMetaData();
187 set = meta.getBestRowIdentifier(null, schema, table, DatabaseMetaData.bestRowSession, true);
188 StringMatrix keys = fillMatrix(set);