org.hibernate.engine.jdbc.env.spi.JdbcEnvironment Java Examples

The following examples show how to use org.hibernate.engine.jdbc.env.spi.JdbcEnvironment. 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 Project: lams   Author: lamsfoundation   File: DefaultIdentifierGeneratorFactory.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public void injectServices(ServiceRegistryImplementor serviceRegistry) {
	this.serviceRegistry = serviceRegistry;
	this.dialect = serviceRegistry.getService( JdbcEnvironment.class ).getDialect();
	final ConfigurationService configService = serviceRegistry.getService( ConfigurationService.class );

	final boolean useNewIdentifierGenerators = configService.getSetting(
			AvailableSettings.USE_NEW_ID_GENERATOR_MAPPINGS,
			StandardConverters.BOOLEAN,
			true
	);

	if(!useNewIdentifierGenerators) {
		register( "sequence", SequenceGenerator.class );
	}
}
 
Example #2
Source Project: lams   Author: lamsfoundation   File: Index.java    License: GNU General Public License v2.0 6 votes vote down vote up
public static String buildSqlCreateIndexString(
		Dialect dialect,
		String name,
		Table table,
		Iterator<Column> columns,
		java.util.Map<Column, String> columnOrderMap,
		boolean unique,
		Metadata metadata) {
	final JdbcEnvironment jdbcEnvironment = metadata.getDatabase().getJdbcEnvironment();

	final String tableName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
			table.getQualifiedTableName(),
			dialect
	);

	return buildSqlCreateIndexString(
			dialect,
			name,
			tableName,
			columns,
			columnOrderMap,
			unique
	);
}
 
Example #3
Source Project: lams   Author: lamsfoundation   File: DatabaseInformationImpl.java    License: 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 #4
Source Project: lams   Author: lamsfoundation   File: StandardIndexExporter.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public String[] getSqlDropStrings(Index index, Metadata metadata) {
	if ( !dialect.dropConstraints() ) {
		return NO_COMMANDS;
	}

	final JdbcEnvironment jdbcEnvironment = metadata.getDatabase().getJdbcEnvironment();
	final String tableName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
			index.getTable().getQualifiedTableName(),
			dialect
	);

	final String indexNameForCreation;
	if ( dialect.qualifyIndexName() ) {
		indexNameForCreation = StringHelper.qualify( tableName, index.getName() );
	}
	else {
		indexNameForCreation = index.getName();
	}

	return new String[] { "drop index " + indexNameForCreation };
}
 
Example #5
Source Project: lams   Author: lamsfoundation   File: MultipleHiLoPerTableGenerator.java    License: GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings({"StatementWithEmptyBody", "deprecation"})
public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException {
	returnClass = type.getReturnedClass();

	final JdbcEnvironment jdbcEnvironment = serviceRegistry.getService( JdbcEnvironment.class );

	qualifiedTableName = determineGeneratorTableName( params, jdbcEnvironment );

	segmentColumnName = determineSegmentColumnName( params, jdbcEnvironment );
	keySize = ConfigurationHelper.getInt( PK_LENGTH_NAME, params, DEFAULT_PK_LENGTH );
	segmentName = ConfigurationHelper.getString( PK_VALUE_NAME, params, params.getProperty( TABLE ) );

	valueColumnName = determineValueColumnName( params, jdbcEnvironment );

	//hilo config
	maxLo = ConfigurationHelper.getInt( MAX_LO, params, Short.MAX_VALUE );

	if ( maxLo >= 1 ) {
		hiloOptimizer = new LegacyHiLoAlgorithmOptimizer( returnClass, maxLo );
	}
}
 
Example #6
Source Project: lams   Author: lamsfoundation   File: Helper.java    License: 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 #7
Source Project: hibernate-reactive   Author: hibernate   File: IdentifierGeneration.java    License: GNU Lesser General Public License v2.1 5 votes vote down vote up
static QualifiedName determineTableName(Properties params, ServiceRegistry serviceRegistry) {
	String fallbackTableName = TableGenerator.DEF_TABLE;
	final Boolean preferGeneratorNameAsDefaultName = serviceRegistry.getService( ConfigurationService.class )
			.getSetting( Settings.PREFER_GENERATOR_NAME_AS_DEFAULT_SEQUENCE_NAME, StandardConverters.BOOLEAN, true );
	if ( preferGeneratorNameAsDefaultName ) {
		final String generatorName = params.getProperty( IdentifierGenerator.GENERATOR_NAME );
		if ( StringHelper.isNotEmpty( generatorName ) ) {
			fallbackTableName = generatorName;
		}
	}

	String tableName = ConfigurationHelper.getString( TableGenerator.TABLE_PARAM, params, fallbackTableName );

	QualifiedNameParser.NameParts qualifiedTableName;
	if ( tableName.contains( "." ) ) {
		qualifiedTableName = QualifiedNameParser.INSTANCE.parse( tableName );
	}
	else {
		JdbcEnvironment jdbcEnvironment = serviceRegistry.getService( JdbcEnvironment.class );
		// todo : need to incorporate implicit catalog and schema names
		final Identifier catalog = jdbcEnvironment.getIdentifierHelper().toIdentifier(
				ConfigurationHelper.getString( CATALOG, params )
		);
		final Identifier schema = jdbcEnvironment.getIdentifierHelper().toIdentifier(
				ConfigurationHelper.getString( SCHEMA, params )
		);
		qualifiedTableName = new QualifiedNameParser.NameParts(
				catalog,
				schema,
				jdbcEnvironment.getIdentifierHelper().toIdentifier( tableName )
		);
	}
	return qualifiedTableName;
}
 
Example #8
Source Project: nomulus   Author: google   File: NomulusNamingStrategy.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Identifier toPhysicalColumnName(Identifier name, JdbcEnvironment jdbcEnvironment) {
  if (name.isQuoted()) {
    return name;
  }
  // Convert the lowerCamelCase field name into the snake_case column name
  return jdbcEnvironment
      .getIdentifierHelper()
      .toIdentifier(
          CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, name.getText()),
          /* quoted= */ false);
}
 
Example #9
Source Project: lams   Author: lamsfoundation   File: TableGenerator.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException {
	storeLastUsedValue = serviceRegistry.getService( ConfigurationService.class )
			.getSetting( AvailableSettings.TABLE_GENERATOR_STORE_LAST_USED, StandardConverters.BOOLEAN, true );
	identifierType = type;

	final JdbcEnvironment jdbcEnvironment = serviceRegistry.getService( JdbcEnvironment.class );

	qualifiedTableName = determineGeneratorTableName( params, jdbcEnvironment, serviceRegistry );
	segmentColumnName = determineSegmentColumnName( params, jdbcEnvironment );
	valueColumnName = determineValueColumnName( params, jdbcEnvironment );

	segmentValue = determineSegmentValue( params );

	segmentValueLength = determineSegmentColumnSize( params );
	initialValue = determineInitialValue( params );
	incrementSize = determineIncrementSize( params );

	final String optimizationStrategy = ConfigurationHelper.getString(
			OPT_PARAM,
			params,
			OptimizerFactory.determineImplicitOptimizerName( incrementSize, params )
	);
	int optimizerInitialValue = ConfigurationHelper.getInt( INITIAL_PARAM, params, -1 );
	optimizer = OptimizerFactory.buildOptimizer(
			optimizationStrategy,
			identifierType.getReturnedClass(),
			incrementSize,
			optimizerInitialValue
	);
}
 
Example #10
Source Project: lams   Author: lamsfoundation   File: AbstractMultiTableBulkIdStrategyImpl.java    License: GNU General Public License v2.0 5 votes vote down vote up
protected String buildIdTableDropStatement(Table idTable, JdbcServices jdbcServices) {
	final JdbcEnvironment jdbcEnvironment = jdbcServices.getJdbcEnvironment();
	final Dialect dialect = jdbcEnvironment.getDialect();

	return getIdTableSupport().getDropIdTableCommand() + " "
			+ jdbcEnvironment.getQualifiedObjectNameFormatter().format( idTable.getQualifiedTableName(), dialect );
}
 
Example #11
Source Project: lams   Author: lamsfoundation   File: SequenceGenerator.java    License: 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 #12
Source Project: teiid-spring-boot   Author: teiid   File: SchemaBuilderUtility.java    License: Apache License 2.0 5 votes vote down vote up
public static Metadata generateHbmModel(ConnectionProvider provider, Dialect dialect) throws SQLException {
    MetadataSources metadataSources = new MetadataSources();
    ServiceRegistry registry = metadataSources.getServiceRegistry();

    StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder(
            (BootstrapServiceRegistry) registry).applySetting(AvailableSettings.DIALECT, TeiidDialect.class)
            .addService(ConnectionProvider.class, provider).addService(JdbcEnvironment.class,
                    new JdbcEnvironmentImpl(provider.getConnection().getMetaData(), dialect))
            .build();

    MetadataBuildingOptions options = new MetadataBuildingOptionsImpl(serviceRegistry);
    BootstrapContext bootstrapContext = new BootstrapContextImpl( serviceRegistry, options );

    ReverseEngineeringStrategy strategy = new DefaultReverseEngineeringStrategy();

    InFlightMetadataCollectorImpl metadataCollector =  new InFlightMetadataCollectorImpl(bootstrapContext, options);
    MetadataBuildingContext buildingContext = new MetadataBuildingContextRootImpl(bootstrapContext, options,
            metadataCollector);

    JDBCBinder binder = new JDBCBinder(serviceRegistry, new Properties(), buildingContext, strategy, false);
    Metadata metadata = metadataCollector.buildMetadataInstance(buildingContext);
    binder.readFromDatabase(null, null, buildMapping(metadata));
    HibernateMappingExporter exporter = new HibernateMappingExporter() {
        @Override
        public Metadata getMetadata() {
            return metadata;
        }
    };
    exporter.start();
    return metadata;
}
 
Example #13
Source Project: lams   Author: lamsfoundation   File: AbstractCollectionPersister.java    License: GNU General Public License v2.0 5 votes vote down vote up
protected String determineTableName(Table table, JdbcEnvironment jdbcEnvironment) {
	if ( table.getSubselect() != null ) {
		return "( " + table.getSubselect() + " )";
	}

	return jdbcEnvironment.getQualifiedObjectNameFormatter().format(
			table.getQualifiedTableName(),
			jdbcEnvironment.getDialect()
	);
}
 
Example #14
Source Project: lams   Author: lamsfoundation   File: ProcedureParameterImpl.java    License: GNU General Public License v2.0 5 votes vote down vote up
private boolean canDoNameParameterBinding(Type hibernateType) {
	final ExtractedDatabaseMetaData databaseMetaData = procedureCall.getSession()
			.getJdbcCoordinator()
			.getJdbcSessionOwner()
			.getJdbcSessionContext()
			.getServiceRegistry().getService( JdbcEnvironment.class )
			.getExtractedDatabaseMetaData();
	return
			databaseMetaData.supportsNamedParameters()
					&& ProcedureParameterNamedBinder.class.isInstance( hibernateType )
					&& ((ProcedureParameterNamedBinder) hibernateType).canDoSetting();
}
 
Example #15
Source Project: lams   Author: lamsfoundation   File: SequenceStyleGenerator.java    License: GNU General Public License v2.0 5 votes vote down vote up
protected DatabaseStructure buildSequenceStructure(
		Type type,
		Properties params,
		JdbcEnvironment jdbcEnvironment,
		QualifiedName sequenceName,
		int initialValue,
		int incrementSize) {
	return new SequenceStructure( jdbcEnvironment, sequenceName, initialValue, incrementSize, type.getReturnedClass() );
}
 
Example #16
Source Project: lams   Author: lamsfoundation   File: ExtractionContextImpl.java    License: GNU General Public License v2.0 5 votes vote down vote up
public ExtractionContextImpl(
		ServiceRegistry serviceRegistry,
		JdbcEnvironment jdbcEnvironment,
		JdbcConnectionAccess jdbcConnectionAccess,
		DatabaseObjectAccess registeredTableAccess,
		Identifier defaultCatalogName,
		Identifier defaultSchemaName) {
	this.serviceRegistry = serviceRegistry;
	this.jdbcEnvironment = jdbcEnvironment;
	this.jdbcConnectionAccess = jdbcConnectionAccess;
	this.registeredTableAccess = registeredTableAccess;
	this.defaultCatalogName = defaultCatalogName;
	this.defaultSchemaName = defaultSchemaName;
}
 
Example #17
Source Project: lams   Author: lamsfoundation   File: SchemaCreatorImpl.java    License: GNU General Public License v2.0 5 votes vote down vote up
public void doCreation(
		Metadata metadata,
		final ServiceRegistry serviceRegistry,
		final Map settings,
		final boolean manageNamespaces,
		GenerationTarget... targets) {
	doCreation(
			metadata,
			serviceRegistry.getService( JdbcEnvironment.class ).getDialect(),
			new ExecutionOptions() {
				@Override
				public boolean shouldManageNamespaces() {
					return manageNamespaces;
				}

				@Override
				public Map getConfigurationValues() {
					return settings;
				}

				@Override
				public ExceptionHandler getExceptionHandler() {
					return ExceptionHandlerLoggedImpl.INSTANCE;
				}
			},
			new SourceDescriptor() {
				@Override
				public SourceType getSourceType() {
					return SourceType.METADATA;
				}

				@Override
				public ScriptSourceInput getScriptSourceInput() {
					return null;
				}
			},
			targets
	);
}
 
Example #18
Source Project: lams   Author: lamsfoundation   File: StandardSequenceExporter.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public String[] getSqlCreateStrings(Sequence sequence, Metadata metadata) {
	final JdbcEnvironment jdbcEnvironment = metadata.getDatabase().getJdbcEnvironment();
	return dialect.getCreateSequenceStrings(
			jdbcEnvironment.getQualifiedObjectNameFormatter().format(
					sequence.getName(),
					jdbcEnvironment.getDialect()
			),
			sequence.getInitialValue(),
			sequence.getIncrementSize()
	);
}
 
Example #19
Source Project: lams   Author: lamsfoundation   File: DefaultUniqueDelegate.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public String getAlterTableToAddUniqueKeyCommand(UniqueKey uniqueKey, Metadata metadata) {
	final JdbcEnvironment jdbcEnvironment = metadata.getDatabase().getJdbcEnvironment();

	final String tableName = jdbcEnvironment.getQualifiedObjectNameFormatter().format(
			uniqueKey.getTable().getQualifiedTableName(),
			dialect
	);

	final String constraintName = dialect.quote( uniqueKey.getName() );
	return dialect.getAlterTableString( tableName )
			+ " add constraint " + constraintName + " " + uniqueConstraintSql( uniqueKey );
}
 
Example #20
Source Project: lams   Author: lamsfoundation   File: Database.java    License: GNU General Public License v2.0 5 votes vote down vote up
public Database(MetadataBuildingOptions buildingOptions, JdbcEnvironment jdbcEnvironment) {
	this.buildingOptions = buildingOptions;

	this.jdbcEnvironment = jdbcEnvironment;

	this.dialect = determineDialect( buildingOptions );

	this.implicitNamespace = makeNamespace(
			new Namespace.Name(
					toIdentifier( buildingOptions.getMappingDefaults().getImplicitCatalogName() ),
					toIdentifier( buildingOptions.getMappingDefaults().getImplicitSchemaName() )
			)
	);
}
 
Example #21
Source Project: lams   Author: lamsfoundation   File: SchemaDropperImpl.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * For testing...
 *
 * @param metadata The metadata for which to generate the creation commands.
 *
 * @return The generation commands
 */
public List<String> generateDropCommands(Metadata metadata, final boolean manageNamespaces) {
	final JournalingGenerationTarget target = new JournalingGenerationTarget();

	final ServiceRegistry serviceRegistry = ( (MetadataImplementor) metadata ).getMetadataBuildingOptions()
			.getServiceRegistry();
	final Dialect dialect = serviceRegistry.getService( JdbcEnvironment.class ).getDialect();

	final ExecutionOptions options = new ExecutionOptions() {
		@Override
		public boolean shouldManageNamespaces() {
			return manageNamespaces;
		}

		@Override
		public Map getConfigurationValues() {
			return Collections.emptyMap();
		}

		@Override
		public ExceptionHandler getExceptionHandler() {
			return ExceptionHandlerHaltImpl.INSTANCE;
		}
	};

	dropFromMetadata( metadata, options, dialect, FormatStyle.NONE.getFormatter(), target );

	return target.commands;
}
 
Example #22
Source Project: lams   Author: lamsfoundation   File: PersistentTableBulkIdStrategy.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
protected QualifiedTableName determineIdTableName(
		JdbcEnvironment jdbcEnvironment,
		PersistentClass entityBinding) {
	return new QualifiedTableName(
			catalog,
			schema,
			super.determineIdTableName( jdbcEnvironment, entityBinding ).getTableName()
	);
}
 
Example #23
Source Project: lams   Author: lamsfoundation   File: IncrementGenerator.java    License: GNU General Public License v2.0 4 votes vote down vote up
@Override
public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException {
	returnClass = type.getReturnedClass();

	final JdbcEnvironment jdbcEnvironment = serviceRegistry.getService( JdbcEnvironment.class );
	final ObjectNameNormalizer normalizer =
			(ObjectNameNormalizer) params.get( PersistentIdentifierGenerator.IDENTIFIER_NORMALIZER );

	String column = params.getProperty( "column" );
	if ( column == null ) {
		column = params.getProperty( PersistentIdentifierGenerator.PK );
	}
	column = normalizer.normalizeIdentifierQuoting( column ).render( jdbcEnvironment.getDialect() );

	String tableList = params.getProperty( "tables" );
	if ( tableList == null ) {
		tableList = params.getProperty( PersistentIdentifierGenerator.TABLES );
	}
	String[] tables = StringHelper.split( ", ", tableList );

	final String schema = normalizer.toDatabaseIdentifierText(
			params.getProperty( PersistentIdentifierGenerator.SCHEMA )
	);
	final String catalog = normalizer.toDatabaseIdentifierText(
			params.getProperty( PersistentIdentifierGenerator.CATALOG )
	);

	StringBuilder buf = new StringBuilder();
	for ( int i = 0; i < tables.length; i++ ) {
		final String tableName = normalizer.toDatabaseIdentifierText( tables[i] );
		if ( tables.length > 1 ) {
			buf.append( "select max(" ).append( column ).append( ") as mx from " );
		}
		buf.append( Table.qualify( catalog, schema, tableName ) );
		if ( i < tables.length - 1 ) {
			buf.append( " union " );
		}
	}
	if ( tables.length > 1 ) {
		buf.insert( 0, "( " ).append( " ) ids_" );
		column = "ids_.mx";
	}

	sql = "select max(" + column + ") from " + buf.toString();
}
 
Example #24
Source Project: WeBASE-Collect-Bee   Author: WeBankFinTech   File: DatabaseNamingStrategy.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Identifier toPhysicalSchemaName(Identifier identifier, JdbcEnvironment jdbcEnvironment) {
    return identifier;
}
 
Example #25
Source Project: tutorials   Author: eugenp   File: CustomPhysicalNamingStrategy.java    License: MIT License 4 votes vote down vote up
@Override
public Identifier toPhysicalCatalogName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
    return convertToSnakeCase(identifier);
}
 
Example #26
Source Project: WeBASE-Collect-Bee   Author: WeBankFinTech   File: DatabaseNamingStrategy.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Identifier toPhysicalColumnName(Identifier identifier, JdbcEnvironment jdbcEnvironment) {
    return identifier;
}
 
Example #27
Source Project: hibernate-types   Author: vladmihalcea   File: CamelCaseToSnakeCaseNamingStrategy.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public Identifier toPhysicalSequenceName(Identifier name, JdbcEnvironment context) {
    return formatIdentifier(super.toPhysicalSequenceName(name, context));
}
 
Example #28
Source Project: robe   Author: robeio   File: RobeHibernateNamingStrategy.java    License: GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public Identifier toPhysicalCatalogName(Identifier identifier, JdbcEnvironment jdbcEnv) {
    return convert(identifier);
}
 
Example #29
Source Project: tutorials   Author: eugenp   File: CustomPhysicalNamingStrategy.java    License: MIT License 4 votes vote down vote up
@Override
public Identifier toPhysicalSequenceName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
    return convertToSnakeCase(identifier);
}
 
Example #30
protected String determineSegmentColumnName(Properties params, JdbcEnvironment jdbcEnvironment) {
	final String name = ConfigurationHelper.getString( SEGMENT_COLUMN_PARAM, params, DEF_SEGMENT_COLUMN );
	return jdbcEnvironment.getIdentifierHelper().toIdentifier( name ).render( jdbcEnvironment.getDialect() );
}