All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl Maven / Gradle / Ivy

There is a newer version: 5.6.15.Final
Show newest version
/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later.
 * See the lgpl.txt file in the root directory or .
 */
package org.hibernate.tool.schema.extract.internal;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.StringTokenizer;

import org.hibernate.boot.model.naming.DatabaseIdentifier;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.tool.schema.extract.spi.ExtractionContext;
import org.hibernate.tool.schema.extract.spi.TableInformation;

/**
 * Implementation of the InformationExtractor contract which uses the standard JDBC {@link java.sql.DatabaseMetaData}
 * API for extraction.
 *
 * @author Steve Ebersole
 * @author Gail Badner
 */
public class InformationExtractorJdbcDatabaseMetaDataImpl extends AbstractInformationExtractorImpl {

	public InformationExtractorJdbcDatabaseMetaDataImpl(ExtractionContext extractionContext) {
		super( extractionContext );
	}

	@Override
	protected String getResultSetTableTypesPhysicalTableConstant() {
		return "TABLE";
	}

	@Override
	public  T processCatalogsResultSet(ExtractionContext.ResultSetProcessor processor) throws SQLException {
		try (ResultSet resultSet = getExtractionContext().getJdbcDatabaseMetaData().getCatalogs() ) {
			return processor.process( resultSet );
		}
	}

	@Override
	protected  T processSchemaResultSet(
			String catalog,
			String schemaPattern,
			ExtractionContext.ResultSetProcessor processor) throws SQLException {
		try (ResultSet resultSet = getExtractionContext().getJdbcDatabaseMetaData().getSchemas(
				catalog,
				schemaPattern ) ) {
			return processor.process( resultSet );
		}
	}

	@Override
	protected  T processTableResultSet(
			String catalog,
			String schemaPattern,
			String tableNamePattern,
			String[] types,
			ExtractionContext.ResultSetProcessor processor
	) throws SQLException {
		try (ResultSet resultSet = getExtractionContext().getJdbcDatabaseMetaData().getTables(
				catalog,
				schemaPattern,
				tableNamePattern,
				types)) {
			return processor.process( resultSet );
		}
	}

	@Override
	protected  T processColumnsResultSet(
			String catalog,
			String schemaPattern,
			String tableNamePattern,
			String columnNamePattern,
			ExtractionContext.ResultSetProcessor processor) throws SQLException {
		try (ResultSet resultSet = getExtractionContext().getJdbcDatabaseMetaData().getColumns(
				catalog,
				schemaPattern,
				tableNamePattern,
				columnNamePattern )) {
			return processor.process( resultSet );
		}
	}

	@Override
	protected  T processPrimaryKeysResultSet(
			String catalogFilter,
			String schemaFilter,
			Identifier tableName,
			ExtractionContext.ResultSetProcessor processor) throws SQLException {
		try( ResultSet resultSet = getExtractionContext().getJdbcDatabaseMetaData().getPrimaryKeys(
				catalogFilter,
				schemaFilter,
				tableName.getText() ) ) {
			return processor.process( resultSet );
		}
	}

	@Override
	protected  T processIndexInfoResultSet(
			String catalog,
			String schema,
			String table,
			boolean unique,
			boolean approximate,
			ExtractionContext.ResultSetProcessor processor) throws SQLException {

		try (ResultSet resultSet = getExtractionContext().getJdbcDatabaseMetaData().getIndexInfo(
				catalog,
				schema,
				table,
				unique,
				approximate ) ) {
			return processor.process( resultSet );
		}
	}

	@Override
	protected  T processImportedKeysResultSet(
			String catalog,
			String schema,
			String table,
			ExtractionContext.ResultSetProcessor processor) throws SQLException {
		try (ResultSet resultSet = getExtractionContext().getJdbcDatabaseMetaData().getImportedKeys(
				catalog,
				schema,
				table ) ) {
			return processor.process( resultSet );
		}
	}

	protected void addColumns(TableInformation tableInformation) {
		final ExtractionContext extractionContext = getExtractionContext();
		// We use this dummy query to retrieve the table information through the ResultSetMetaData
		// This is significantly better than to use the DatabaseMetaData especially on Oracle with synonyms enable
		final String tableName = extractionContext.getSqlStringGenerationContext().format(
				// The name comes from the database, so the case is correct
				// But we quote here to avoid issues with reserved words
				tableInformation.getName().quote()
		);

		try {
			extractionContext.getQueryResults(
					"select * from " + tableName + " where 1=0",
					null,
					resultSet -> {
						final ResultSetMetaData metaData = resultSet.getMetaData();
						final int columnCount = metaData.getColumnCount();

						for ( int i = 1; i <= columnCount; i++ ) {
							final String columnName = metaData.getColumnName( i );
							final ColumnInformationImpl columnInformation = new ColumnInformationImpl(
									tableInformation,
									DatabaseIdentifier.toIdentifier( columnName ),
									metaData.getColumnType( i ),
									new StringTokenizer( metaData.getColumnTypeName( i ), "() " ).nextToken(),
									metaData.getPrecision( i ),
									metaData.getScale( i ),
									interpretNullable( metaData.isNullable( i ) )
							);
							tableInformation.addColumn( columnInformation );
						}
						return null;
					}
			);
		}
		catch (SQLException e) {
			throw convertSQLException(
					e,
					"Error accessing column metadata: " + tableInformation.getName().toString()
			);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy