org.hibernate.boot.model.relational.Namespace Java Examples

The following examples show how to use org.hibernate.boot.model.relational.Namespace. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GroupedSchemaValidatorImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void validateTables(
		Metadata metadata,
		DatabaseInformation databaseInformation,
		ExecutionOptions options,
		Dialect dialect, Namespace namespace) {

	final NameSpaceTablesInformation tables = databaseInformation.getTablesInformation( namespace );
	for ( Table table : namespace.getTables() ) {
		if ( schemaFilter.includeTable( table ) && table.isPhysicalTable() ) {
			validateTable(
					table,
					tables.getTableInformation( table ),
					metadata,
					options,
					dialect
			);
		}
	}
}
 
Example #2
Source File: IndividuallySchemaValidatorImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void validateTables(
		Metadata metadata,
		DatabaseInformation databaseInformation,
		ExecutionOptions options,
		Dialect dialect,
		Namespace namespace) {
	for ( Table table : namespace.getTables() ) {
		if ( schemaFilter.includeTable( table ) && table.isPhysicalTable() ) {
			final TableInformation tableInformation = databaseInformation.getTableInformation(
					table.getQualifiedTableName()
			);
			validateTable( table, tableInformation, metadata, options, dialect );
		}
	}
}
 
Example #3
Source File: Helper.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public static DatabaseInformation buildDatabaseInformation(
		ServiceRegistry serviceRegistry,
		DdlTransactionIsolator ddlTransactionIsolator,
		Namespace.Name defaultNamespace) {
	final JdbcEnvironment jdbcEnvironment = serviceRegistry.getService( JdbcEnvironment.class );
	try {
		return new DatabaseInformationImpl(
				serviceRegistry,
				jdbcEnvironment,
				ddlTransactionIsolator,
				defaultNamespace
		);
	}
	catch (SQLException e) {
		throw jdbcEnvironment.getSqlExceptionHelper().convert( e, "Unable to build DatabaseInformation" );
	}
}
 
Example #4
Source File: DatabaseInformationImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public DatabaseInformationImpl(
		ServiceRegistry serviceRegistry,
		JdbcEnvironment jdbcEnvironment,
		DdlTransactionIsolator ddlTransactionIsolator,
		Namespace.Name defaultNamespace) throws SQLException {
	this.jdbcEnvironment = jdbcEnvironment;

	this.extractionContext = new ImprovedExtractionContextImpl(
			serviceRegistry,
			jdbcEnvironment,
			ddlTransactionIsolator,
			defaultNamespace.getCatalog(),
			defaultNamespace.getSchema(),
			this
	);

	// todo : make this pluggable
	this.extractor = new InformationExtractorJdbcDatabaseMetaDataImpl( extractionContext );

	// because we do not have defined a way to locate sequence info by name
	initializeSequences();
}
 
Example #5
Source File: SequenceStructure.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
protected void buildSequence(Database database) {
	final int sourceIncrementSize = getSourceIncrementSize();

	final Namespace namespace = database.locateNamespace(
			logicalQualifiedSequenceName.getCatalogName(),
			logicalQualifiedSequenceName.getSchemaName()
	);
	Sequence sequence = namespace.locateSequence( logicalQualifiedSequenceName.getObjectName() );
	if ( sequence != null ) {
		sequence.validate( initialValue, sourceIncrementSize );
	}
	else {
		sequence = namespace.createSequence( logicalQualifiedSequenceName.getObjectName(), initialValue, sourceIncrementSize );
	}

	this.sequenceName = database.getJdbcEnvironment().getQualifiedObjectNameFormatter().format(
			sequence.getName(),
			database.getJdbcEnvironment().getDialect()
	);
}
 
Example #6
Source File: SequenceGenerator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void registerExportables(Database database) {
	final Namespace namespace = database.locateNamespace(
			logicalQualifiedSequenceName.getCatalogName(),
			logicalQualifiedSequenceName.getSchemaName()
	);
	Sequence sequence = namespace.locateSequence( logicalQualifiedSequenceName.getObjectName() );
	if ( sequence != null ) {
		sequence.validate( 1, 1 );
	}
	else {
		sequence = namespace.createSequence(
				logicalQualifiedSequenceName.getObjectName(),
				1,
				1
		);
	}

	final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment();
	final Dialect dialect = jdbcEnvironment.getDialect();

	this.sequenceName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
			sequence.getName(),
			dialect
	);
	this.sql = jdbcEnvironment.getDialect().getSequenceNextValString( sequenceName );
}
 
Example #7
Source File: AbstractSchemaMigrator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected abstract NameSpaceTablesInformation performTablesMigration(
Metadata metadata,
DatabaseInformation existingDatabase,
ExecutionOptions options,
Dialect dialect,
Formatter formatter,
Set<String> exportIdentifiers,
boolean tryToCreateCatalogs,
boolean tryToCreateSchemas,
Set<Identifier> exportedCatalogs,
Namespace namespace, GenerationTarget[] targets);
 
Example #8
Source File: AbstractSchemaMigrator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected void createSchemaAndCatalog(
		DatabaseInformation existingDatabase,
		ExecutionOptions options,
		Dialect dialect,
		Formatter formatter,
		boolean tryToCreateCatalogs,
		boolean tryToCreateSchemas,
		Set<Identifier> exportedCatalogs, Namespace namespace, GenerationTarget[] targets) {
	if ( tryToCreateCatalogs || tryToCreateSchemas ) {
		if ( tryToCreateCatalogs ) {
			final Identifier catalogLogicalName = namespace.getName().getCatalog();
			final Identifier catalogPhysicalName = namespace.getPhysicalName().getCatalog();

			if ( catalogPhysicalName != null && !exportedCatalogs.contains( catalogLogicalName )
					&& !existingDatabase.catalogExists( catalogLogicalName ) ) {
				applySqlStrings(
						false,
						dialect.getCreateCatalogCommand( catalogPhysicalName.render( dialect ) ),
						formatter,
						options,
						targets
				);
				exportedCatalogs.add( catalogLogicalName );
			}
		}

		if ( tryToCreateSchemas
				&& namespace.getPhysicalName().getSchema() != null
				&& !existingDatabase.schemaExists( namespace.getName() ) ) {
			applySqlStrings(
					false,
					dialect.getCreateSchemaCommand( namespace.getPhysicalName().getSchema().render( dialect ) ),
					formatter,
					options,
					targets
			);
		}
	}
}
 
Example #9
Source File: SchemaDropperImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void applyConstraintDropping(
		Namespace namespace,
		Metadata metadata,
		Formatter formatter,
		ExecutionOptions options,
		GenerationTarget... targets) {
	final Dialect dialect = metadata.getDatabase().getJdbcEnvironment().getDialect();

	if ( !dialect.dropConstraints() ) {
		return;
	}

	for ( Table table : namespace.getTables() ) {
		if ( !table.isPhysicalTable() ) {
			continue;
		}
		if ( !schemaFilter.includeTable( table ) ) {
			continue;
		}

		final Iterator fks = table.getForeignKeyIterator();
		while ( fks.hasNext() ) {
			final ForeignKey foreignKey = (ForeignKey) fks.next();
			applySqlStrings(
					dialect.getForeignKeyExporter().getSqlDropStrings( foreignKey, metadata ),
					formatter,
					options,
					targets
			);
		}
	}
}
 
Example #10
Source File: MetadataTest.java    From high-performance-java-persistence with Apache License 2.0 5 votes vote down vote up
@Test
public void testDatabaseMetadata() {
    for(Namespace namespace : MetadataExtractorIntegrator.INSTANCE.getDatabase().getNamespaces()) {
        for( Table table : namespace.getTables()) {
            LOGGER.info( "Table {} has the following columns: {}",
                 table,
                 StreamSupport.stream(
                     Spliterators.spliteratorUnknownSize( table.getColumnIterator(), Spliterator.ORDERED), false)
                 .collect( Collectors.toList()) );
        }
    }
}
 
Example #11
Source File: InFlightMetadataCollectorImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public java.util.Collection<Table> collectTableMappings() {
	ArrayList<Table> tables = new ArrayList<>();
	for ( Namespace namespace : getDatabase().getNamespaces() ) {
		tables.addAll( namespace.getTables() );
	}
	return tables;
}
 
Example #12
Source File: InFlightMetadataCollectorImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Table addTable(
		String schemaName,
		String catalogName,
		String name,
		String subselectFragment,
		boolean isAbstract) {
	final Namespace namespace = getDatabase().locateNamespace(
			getDatabase().toIdentifier( catalogName ),
			getDatabase().toIdentifier( schemaName )
	);

	// annotation binding depends on the "table name" for @Subselect bindings
	// being set into the generated table (mainly to avoid later NPE), but for now we need to keep that :(
	final Identifier logicalName;
	if ( name != null ) {
		logicalName = getDatabase().toIdentifier( name );
	}
	else {
		logicalName = null;
	}

	if ( subselectFragment != null ) {
		return new Table( namespace, logicalName, subselectFragment, isAbstract );
	}
	else {
		Table table = namespace.locateTable( logicalName );
		if ( table != null ) {
			if ( !isAbstract ) {
				table.setAbstract( false );
			}
			return table;
		}
		return namespace.createTable( logicalName, isAbstract );
	}
}
 
Example #13
Source File: InFlightMetadataCollectorImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Table addDenormalizedTable(
		String schemaName,
		String catalogName,
		String name,
		boolean isAbstract,
		String subselectFragment,
		Table includedTable) throws DuplicateMappingException {
	final Namespace namespace = getDatabase().locateNamespace(
			getDatabase().toIdentifier( catalogName ),
			getDatabase().toIdentifier( schemaName )
	);

	// annotation binding depends on the "table name" for @Subselect bindings
	// being set into the generated table (mainly to avoid later NPE), but for now we need to keep that :(
	final Identifier logicalName;
	if ( name != null ) {
		logicalName = getDatabase().toIdentifier( name );
	}
	else {
		logicalName = null;
	}

	if ( subselectFragment != null ) {
		return new DenormalizedTable( namespace, logicalName, subselectFragment, isAbstract, includedTable );
	}
	else {
		Table table = namespace.locateTable( logicalName );
		if ( table != null ) {
			throw new DuplicateMappingException( DuplicateMappingException.Type.TABLE, logicalName.toString() );
		}
		else {
			table = namespace.createDenormalizedTable( logicalName, isAbstract, includedTable );
		}
		return table;
	}
}
 
Example #14
Source File: MetadataImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public java.util.Collection<Table> collectTableMappings() {
	ArrayList<Table> tables = new ArrayList<>();
	for ( Namespace namespace : database.getNamespaces() ) {
		tables.addAll( namespace.getTables() );
	}
	return tables;
}
 
Example #15
Source File: IgniteCacheInitializer.java    From hibernate-ogm-ignite with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Create indexes for {@code @Index} annotations
 * @param queryEntity
 * @param context
 */
private void addUserIndexes(QueryEntity queryEntity, SchemaDefinitionContext context, String tableName) {
	Namespace namespace = context.getDatabase().getDefaultNamespace();
	Optional<Table> tableOptional = namespace.getTables().stream().filter( currentTable -> currentTable.getName().equals( tableName ) ).findFirst();
	if ( tableOptional.isPresent() ) {
		Table table = tableOptional.get();
		for ( Iterator<Index> indexIterator = table.getIndexIterator(); indexIterator.hasNext(); ) {
			Index index = indexIterator.next();
			appendIndex( queryEntity, index, context );
		}
	}
}
 
Example #16
Source File: IgniteCacheInitializer.java    From hibernate-ogm-ignite with GNU Lesser General Public License v2.1 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void addTableInfo(QueryEntity queryEntity, SchemaDefinitionContext context, String tableName) {
	Namespace namespace = context.getDatabase().getDefaultNamespace();
	Optional<Table> tableOptional = namespace.getTables().stream().filter( currentTable -> currentTable.getName().equals( tableName ) ).findFirst();
	if ( tableOptional.isPresent() ) {
		Table table = tableOptional.get();
		for ( Iterator<Column> columnIterator = table.getColumnIterator(); columnIterator.hasNext(); ) {
			Column currentColumn = columnIterator.next();
			String fieldType = fieldType( currentColumn );
			queryEntity.addQueryField( StringHelper.realColumnName( currentColumn.getName() ), fieldType, null );
		}
	}
}
 
Example #17
Source File: Table.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public Table(
		Namespace namespace,
		Identifier physicalTableName,
		boolean isAbstract) {
	this.catalog = namespace.getPhysicalName().getCatalog();
	this.schema = namespace.getPhysicalName().getSchema();
	this.name = physicalTableName;
	this.isAbstract = isAbstract;
}
 
Example #18
Source File: Table.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public Table(Namespace namespace, Identifier physicalTableName, String subselect, boolean isAbstract) {
	this.catalog = namespace.getPhysicalName().getCatalog();
	this.schema = namespace.getPhysicalName().getSchema();
	this.name = physicalTableName;
	this.subselect = subselect;
	this.isAbstract = isAbstract;
}
 
Example #19
Source File: DenormalizedTable.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public DenormalizedTable(
		Namespace namespace,
		Identifier physicalTableName,
		String subselectFragment,
		boolean isAbstract,
		Table includedTable) {
	super( namespace, physicalTableName, subselectFragment, isAbstract );
	this.includedTable = includedTable;
	includedTable.setHasDenormalizedTables();
}
 
Example #20
Source File: DatabaseInformationImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public SequenceInformation getSequenceInformation(Namespace.Name schemaName, Identifier sequenceName) {
	return getSequenceInformation( new QualifiedSequenceName( schemaName, sequenceName ) );
}
 
Example #21
Source File: Table.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public Table(Namespace namespace, String subselect, boolean isAbstract) {
	this.catalog = namespace.getPhysicalName().getCatalog();
	this.schema = namespace.getPhysicalName().getSchema();
	this.subselect = subselect;
	this.isAbstract = isAbstract;
}
 
Example #22
Source File: DenormalizedTable.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public DenormalizedTable(Namespace namespace, Identifier physicalTableName, boolean isAbstract, Table includedTable) {
	super( namespace, physicalTableName, isAbstract );
	this.includedTable = includedTable;
	includedTable.setHasDenormalizedTables();
}
 
Example #23
Source File: DenormalizedTable.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public DenormalizedTable(Namespace namespace, String subselect, boolean isAbstract, Table includedTable) {
	super( namespace, subselect, isAbstract );
	this.includedTable = includedTable;
	includedTable.setHasDenormalizedTables();
}
 
Example #24
Source File: MultipleHiLoPerTableGenerator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void registerExportables(Database database) {
	final Namespace namespace = database.locateNamespace(
			qualifiedTableName.getCatalogName(),
			qualifiedTableName.getSchemaName()
	);

	Table table = namespace.locateTable( qualifiedTableName.getObjectName() );
	if ( table == null ) {
		table = namespace.createTable( qualifiedTableName.getObjectName(), false );

		// todo : note sure the best solution here.  do we add the columns if missing?  other?
		table.setPrimaryKey( new PrimaryKey( table ) );

		final Column pkColumn = new ExportableColumn(
				database,
				table,
				segmentColumnName,
				StringType.INSTANCE,
				database.getDialect().getTypeName( Types.VARCHAR, keySize, 0, 0 )
		);
		pkColumn.setNullable( false );
		table.addColumn( pkColumn );
		table.getPrimaryKey().addColumn( pkColumn );

		final Column valueColumn = new ExportableColumn(
				database,
				table,
				valueColumnName,
				LongType.INSTANCE
		);
		table.addColumn( valueColumn );
	}

	final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment();

	// allow physical naming strategies a chance to kick in
	tableName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
			table.getQualifiedTableName(),
			jdbcEnvironment.getDialect()
	);

	query = "select " +
			valueColumnName +
			" from " +
			jdbcEnvironment.getDialect().appendLockHint( LockMode.PESSIMISTIC_WRITE, tableName ) +
			" where " + segmentColumnName + " = '" + segmentName + "'" +
			jdbcEnvironment.getDialect().getForUpdateString();

	update = "update " +
			tableName +
			" set " +
			valueColumnName +
			" = ? where " +
			valueColumnName +
			" = ? and " +
			segmentColumnName +
			" = '" +
			segmentName
			+ "'";

	insert = "insert into " + tableName +
			"(" + segmentColumnName + ", " + valueColumnName + ") " +
			"values('" + segmentName + "', ?)";



}
 
Example #25
Source File: TableStructure.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void registerExportables(Database database) {
	final JdbcEnvironment jdbcEnvironment = database.getJdbcEnvironment();
	final Dialect dialect = jdbcEnvironment.getDialect();

	final Namespace namespace = database.locateNamespace(
			logicalQualifiedTableName.getCatalogName(),
			logicalQualifiedTableName.getSchemaName()
	);

	Table table = namespace.locateTable( logicalQualifiedTableName.getObjectName() );
	if ( table == null ) {
		table = namespace.createTable( logicalQualifiedTableName.getObjectName(), false );
	}

	this.tableNameText = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
			table.getQualifiedTableName(),
			dialect
	);

	this.valueColumnNameText = logicalValueColumnNameIdentifier.render( dialect );


	this.selectQuery = "select " + valueColumnNameText + " as id_val" +
			" from " + dialect.appendLockHint( LockMode.PESSIMISTIC_WRITE, tableNameText ) +
			dialect.getForUpdateString();

	this.updateQuery = "update " + tableNameText +
			" set " + valueColumnNameText + "= ?" +
			" where " + valueColumnNameText + "=?";

	ExportableColumn valueColumn = new ExportableColumn(
			database,
			table,
			valueColumnNameText,
			LongType.INSTANCE
	);
	table.addColumn( valueColumn );

	table.addInitCommand(
			new InitCommand( "insert into " + tableNameText + " values ( " + initialValue + " )" )
	);
}
 
Example #26
Source File: TableGenerator.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void registerExportables(Database database) {
	final Dialect dialect = database.getJdbcEnvironment().getDialect();

	final Namespace namespace = database.locateNamespace(
			qualifiedTableName.getCatalogName(),
			qualifiedTableName.getSchemaName()
	);

	Table table = namespace.locateTable( qualifiedTableName.getObjectName() );
	if ( table == null ) {
		table = namespace.createTable( qualifiedTableName.getObjectName(), false );

		// todo : note sure the best solution here.  do we add the columns if missing?  other?
		final Column segmentColumn = new ExportableColumn(
				database,
				table,
				segmentColumnName,
				StringType.INSTANCE,
				dialect.getTypeName( Types.VARCHAR, segmentValueLength, 0, 0 )
		);
		segmentColumn.setNullable( false );
		table.addColumn( segmentColumn );

		// lol
		table.setPrimaryKey( new PrimaryKey( table ) );
		table.getPrimaryKey().addColumn( segmentColumn );

		final Column valueColumn = new ExportableColumn(
				database,
				table,
				valueColumnName,
				LongType.INSTANCE
		);
		table.addColumn( valueColumn );
	}

	// allow physical naming strategies a chance to kick in
	this.renderedTableName = database.getJdbcEnvironment().getQualifiedObjectNameFormatter().format(
			table.getQualifiedTableName(),
			dialect
	);
	table.addInitCommand( generateInsertInitCommand() );

	this.selectQuery = buildSelectQuery( dialect );
	this.updateQuery = buildUpdateQuery();
	this.insertQuery = buildInsertQuery();
}
 
Example #27
Source File: DatabaseInformationImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean schemaExists(Namespace.Name namespace) {
	return extractor.schemaExists( namespace.getCatalog(), namespace.getSchema() );
}
 
Example #28
Source File: DatabaseInformationImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public TableInformation getTableInformation(
		Namespace.Name namespace,
		Identifier tableName) {
	return getTableInformation( new QualifiedTableName( namespace, tableName ) );
}
 
Example #29
Source File: DatabaseInformationImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public NameSpaceTablesInformation getTablesInformation(Namespace namespace) {
	return extractor.getTables( namespace.getPhysicalName().getCatalog(), namespace.getPhysicalName().getSchema() );
}
 
Example #30
Source File: DefaultSchemaFilter.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean includeNamespace( Namespace namespace ) {
	return true;
}