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

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

There is a newer version: 6.5.0.CR2
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.util.HashMap;
import java.util.Map;

import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.relational.QualifiedTableName;
import org.hibernate.engine.jdbc.env.spi.IdentifierHelper;
import org.hibernate.tool.schema.extract.spi.ColumnInformation;
import org.hibernate.tool.schema.extract.spi.ForeignKeyInformation;
import org.hibernate.tool.schema.extract.spi.IndexInformation;
import org.hibernate.tool.schema.extract.spi.InformationExtractor;
import org.hibernate.tool.schema.extract.spi.PrimaryKeyInformation;
import org.hibernate.tool.schema.extract.spi.TableInformation;

/**
 * Provides access to information about existing schema objects (tables, sequences etc) of existing database.
 *
 * @author Christoph Sturm
 * @author Max Rydahl Andersen
 * @author Steve Ebersole
 */
public class TableInformationImpl implements TableInformation {
	private final InformationExtractor extractor;
	private final IdentifierHelper identifierHelper;

	private final QualifiedTableName tableName;
	private final boolean physicalTable;
	private final String comment;

	private PrimaryKeyInformation primaryKey;
	private Map foreignKeys;
	private Map indexes;
	private Map columns = new HashMap<>(  );

	private boolean wasPrimaryKeyLoaded = false; // to avoid multiple db reads since primary key can be null.

	public TableInformationImpl(
			InformationExtractor extractor,
			IdentifierHelper identifierHelper,
			QualifiedTableName tableName,
			boolean physicalTable,
			String comment ) {
		this.extractor = extractor;
		this.identifierHelper = identifierHelper;
		this.tableName = tableName;
		this.physicalTable = physicalTable;
		this.comment = comment;
	}

	@Override
	public QualifiedTableName getName() {
		return tableName;
	}

	@Override
	public boolean isPhysicalTable() {
		return physicalTable;
	}

	@Override
	public String getComment() {
		return comment;
	}

	@Override
	public ColumnInformation getColumn(Identifier columnIdentifier) {
		return columns.get( new Identifier(
				identifierHelper.toMetaDataObjectName( columnIdentifier ),
				false
		) );
	}

	@Override
	public PrimaryKeyInformation getPrimaryKey() {
		if ( ! wasPrimaryKeyLoaded ) {
			primaryKey = extractor.getPrimaryKey( this );
			wasPrimaryKeyLoaded = true;
		}
		return primaryKey;
	}

	@Override
	public Iterable getForeignKeys() {
		return foreignKeys().values();
	}

	protected Map foreignKeys() {
		if ( foreignKeys == null ) {
			final Map fkMap = new HashMap<>();
			final Iterable fks = extractor.getForeignKeys( this );
			for ( ForeignKeyInformation fk : fks ) {
				fkMap.put( fk.getForeignKeyIdentifier(), fk );
			}
			this.foreignKeys = fkMap;
		}
		return foreignKeys;
	}

	@Override
	public ForeignKeyInformation getForeignKey(Identifier fkIdentifier) {
		return foreignKeys().get( new Identifier(
				identifierHelper.toMetaDataObjectName( fkIdentifier ),
				false
		)  );
	}

	@Override
	public Iterable getIndexes() {
		return indexes().values();
	}

	protected Map indexes() {
		if ( indexes == null ) {
			final Map indexMap = new HashMap<>();
			final Iterable indexes = extractor.getIndexes( this );
			for ( IndexInformation index : indexes ) {
				indexMap.put( index.getIndexIdentifier(), index );
			}
			this.indexes = indexMap;
		}
		return indexes;
	}

	@Override
	public void addColumn(ColumnInformation columnIdentifier) {
		columns.put( columnIdentifier.getColumnIdentifier(), columnIdentifier );
	}

	@Override
	public IndexInformation getIndex(Identifier indexName) {
		return indexes().get( new Identifier(
				identifierHelper.toMetaDataObjectName( indexName ),
				false
		) );
	}

	@Override
	public String toString() {
		return "TableInformationImpl(" + tableName.toString() + ')';
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy