001: /**
002: * Objective Database Abstraction Layer (ODAL)
003: * Copyright (c) 2004, The ODAL Development Group
004: * All rights reserved.
005: * For definition of the ODAL Development Group please refer to LICENCE.txt file
006: *
007: * Distributable under LGPL license.
008: * See terms of license at gnu.org.
009: */package com.completex.objective.components.persistency.meta.impl;
010:
011: import com.completex.objective.components.persistency.MetaColumn;
012: import com.completex.objective.components.persistency.MetaTable;
013: import com.completex.objective.components.persistency.ColumnType;
014: import com.completex.objective.components.persistency.meta.MetaModel;
015: import com.completex.objective.components.persistency.meta.ModelLoader;
016: import com.completex.objective.util.TypeUtil;
017:
018: import java.sql.Connection;
019: import java.sql.DatabaseMetaData;
020: import java.sql.ResultSet;
021: import java.sql.ResultSetMetaData;
022: import java.sql.SQLException;
023: import java.sql.Statement;
024: import java.util.Collection;
025: import java.util.LinkedHashMap;
026:
027: /**
028: * @author Gennady Krizhevsky
029: */
030: public abstract class DatabaseModelLoaderImpl extends
031: AbstractDatabaseModelLoader implements ModelLoader {
032:
033: private String schema;
034: private String catalog;
035:
036: abstract protected Connection getConnection() throws SQLException;
037:
038: protected DatabaseModelLoaderImpl(String schema, String catalog,
039: String filterPattern) {
040: this .schema = schema;
041: this .catalog = catalog;
042: this .filterPattern = filterPattern;
043: }
044:
045: public MetaModel load(MetaModel model) throws Exception {
046: debug("DatabaseModelLoaderImpl.load:: enter");
047: if (model == null) {
048: model = new MetaModel(filterPattern);
049: }
050: Connection connection = getConnection();
051: DatabaseMetaData metaData = connection.getMetaData();
052: ResultSet tablesResultSet = null;
053: ResultSet columnsResultSet = null;
054: ResultSet primaryKeysResultSet = null;
055: ResultSet typeInfoResultSet = null;
056: debug("model = " + model);
057: info("schema = " + schema);
058: try {
059: typeInfoResultSet = metaData.getTypeInfo();
060: while (typeInfoResultSet.next()) {
061: debug("TYPE_NAME: "
062: + typeInfoResultSet.getString("TYPE_NAME"));
063: }
064:
065: /*
066: 1. TYPE_CAT String => the type's catalog (may be null)
067: 2. TYPE_SCHEM String => type's schema (may be null)
068: 3. TYPE_NAME String => type name
069: 4. CLASS_NAME String => Java class name
070: 5. DATA_TYPE int => type value defined in java.sql.Types. One of JAVA_OBJECT, STRUCT, or DISTINCT
071: 6. REMARKS String => explanatory comment on the type
072: 7. BASE_TYPE short => type code of the source type of a DISTINCT type or the type that implements the user-generated reference type of the SELF_REFERENCING_COLUMN of a structured type as defined in java.sql.Types (null if DATA_TYPE is not DISTINCT or not STRUCT with REFERENCE_GENERATION = USER_DEFINED)
073: */
074:
075: tablesResultSet = metaData.getTables(null, schema, "%",
076: new String[] { "TABLE" });
077: while (tablesResultSet.next()) {
078: String tableName = tablesResultSet
079: .getString("TABLE_NAME");
080:
081: for (int i = 1; i < tablesResultSet.getMetaData()
082: .getColumnCount(); i++) {
083: Object name = tablesResultSet.getObject(i);
084: debug("Tables col name " + name);
085: }
086:
087: if (!model.isTableAllowed(tableName)) {
088: continue;
089: }
090:
091: MetaTable table = defineTable(model, tableName);
092: if (table == null) {
093: continue;
094: }
095:
096: Statement statement = connection.createStatement();
097: ResultSet rsColsInfo = statement
098: .executeQuery("select * from " + tableName
099: + " where 0=1");
100: ResultSetMetaData rsMetaColsInfo = rsColsInfo
101: .getMetaData();
102:
103: LinkedHashMap colsInfo = new LinkedHashMap();
104: for (int i = 1; i <= rsMetaColsInfo.getColumnCount(); i++) {
105: String columnName = rsMetaColsInfo.getColumnName(i);
106:
107: debug("Columns col " + "name ["
108: + rsMetaColsInfo.getColumnName(i)
109: + "]; isAutoIncrement ["
110: + rsMetaColsInfo.isAutoIncrement(i) + "]");
111: ColumnInfo columnInfo = new ColumnInfo(columnName,
112: rsMetaColsInfo.isAutoIncrement(i),
113: rsMetaColsInfo.getColumnType(i),
114: rsMetaColsInfo.getColumnTypeName(i));
115: colsInfo.put(columnName, columnInfo);
116: debug(columnInfo.toString());
117: }
118:
119: rsColsInfo.close();
120: statement.close();
121:
122: columnsResultSet = metaData.getColumns(null, schema,
123: tableName, null);
124: int columnIndex = 0;
125: while (columnsResultSet.next()) {
126:
127: String columnName = columnsResultSet
128: .getString("COLUMN_NAME");
129: String columnDefaultValue = columnsResultSet
130: .getString("COLUMN_DEF");
131: int columnSize = columnsResultSet
132: .getInt("COLUMN_SIZE");
133: int decimalDigits = columnsResultSet
134: .getInt("DECIMAL_DIGITS");
135: ColumnInfo columnInfo = ((ColumnInfo) colsInfo
136: .get(columnName));
137:
138: String nullable = columnsResultSet
139: .getString("IS_NULLABLE");
140: boolean required = !TypeUtil.S2b(nullable,
141: TypeUtil.YES_NO);
142: String dataType = String
143: .valueOf(columnInfo.jdbcType);
144: ColumnType columnType = columnType(dataType,
145: columnSize, decimalDigits, required);
146: MetaColumn column = defineColumn(model, table,
147: columnName, columnInfo.jdbcType,
148: columnType, nullable, columnsResultSet
149: .getString("REMARKS"),
150: columnDefaultValue, columnSize,
151: decimalDigits, columnInfo);
152: column.setColumnIndex(columnIndex++);
153: }
154: closeRs(columnsResultSet);
155: columnsResultSet = null;
156: // debug("Columns for tableName [" + tableName + "]: " + model.getTable(tableName));
157: primaryKeysResultSet = metaData.getPrimaryKeys(null,
158: schema, tableName);
159: String pkColumnName;
160: while (primaryKeysResultSet.next()) {
161: pkColumnName = primaryKeysResultSet
162: .getString("COLUMN_NAME");
163: definePrimaryKey(model, tableName, pkColumnName);
164: }
165: Collection foreignKeys = getForeignKeys(model,
166: metaData, schema, tableName);
167: if (foreignKeys.size() > 0) {
168: populateForeignKeysToModel(model, tableName,
169: foreignKeys);
170: }
171:
172: closeRs(primaryKeysResultSet);
173: primaryKeysResultSet = null;
174: }
175: closeRs(tablesResultSet);
176: tablesResultSet = null;
177: } catch (SQLException e) {
178: throw new Exception("Cannot get database meta-data", e);
179: } finally {
180: closeRs(tablesResultSet);
181: closeRs(columnsResultSet);
182: closeRs(primaryKeysResultSet);
183: closeRs(typeInfoResultSet);
184: debug("DatabaseModelLoaderImpl.load:: exit");
185: }
186: return model;
187: }
188:
189: public String getSchema() {
190: return schema;
191: }
192:
193: public String getCatalog() {
194: return catalog;
195: }
196:
197: }
|