-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
HHH-19103 add SchemaManager.populate() #9893
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
196 changes: 196 additions & 0 deletions
196
hibernate-core/src/main/java/org/hibernate/tool/schema/internal/AbstractSchemaPopulator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| /* | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| * Copyright Red Hat Inc. and Hibernate Authors | ||
| */ | ||
| package org.hibernate.tool.schema.internal; | ||
|
|
||
| import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; | ||
| import org.hibernate.dialect.Dialect; | ||
| import org.hibernate.engine.jdbc.internal.FormatStyle; | ||
| import org.hibernate.engine.jdbc.internal.Formatter; | ||
| import org.hibernate.internal.util.StringHelper; | ||
| import org.hibernate.tool.schema.internal.exec.ScriptSourceInputFromUrl; | ||
| import org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl; | ||
| import org.hibernate.tool.schema.spi.ExecutionOptions; | ||
| import org.hibernate.tool.schema.spi.GenerationTarget; | ||
| import org.hibernate.tool.schema.spi.SchemaManagementException; | ||
| import org.hibernate.tool.schema.spi.ScriptSourceInput; | ||
| import org.hibernate.tool.schema.spi.SqlScriptCommandExtractor; | ||
|
|
||
| import java.net.URL; | ||
| import java.util.Map; | ||
|
|
||
| import static org.hibernate.cfg.SchemaToolingSettings.HBM2DDL_CHARSET_NAME; | ||
| import static org.hibernate.cfg.SchemaToolingSettings.HBM2DDL_IMPORT_FILES; | ||
| import static org.hibernate.cfg.SchemaToolingSettings.HBM2DDL_LOAD_SCRIPT_SOURCE; | ||
| import static org.hibernate.cfg.SchemaToolingSettings.HBM2DDL_SKIP_DEFAULT_IMPORT_FILE; | ||
| import static org.hibernate.cfg.SchemaToolingSettings.JAKARTA_HBM2DDL_LOAD_SCRIPT_SOURCE; | ||
| import static org.hibernate.internal.util.config.ConfigurationHelper.getBoolean; | ||
| import static org.hibernate.internal.util.config.ConfigurationHelper.getString; | ||
| import static org.hibernate.tool.schema.internal.Helper.applyScript; | ||
| import static org.hibernate.tool.schema.internal.Helper.interpretScriptSourceSetting; | ||
|
|
||
| /** | ||
| * Handles population from {@value #DEFAULT_IMPORT_FILE} and other scripts. | ||
| */ | ||
| public abstract class AbstractSchemaPopulator { | ||
|
|
||
| public static final String DEFAULT_IMPORT_FILE = "/import.sql"; | ||
|
|
||
| abstract ClassLoaderService getClassLoaderService(); | ||
|
|
||
| void applyImportSources( | ||
| ExecutionOptions options, | ||
| SqlScriptCommandExtractor commandExtractor, | ||
| boolean format, | ||
| Dialect dialect, | ||
| GenerationTarget... targets) { | ||
|
|
||
| final Formatter formatter = getImportScriptFormatter(format); | ||
|
|
||
| boolean hasDefaultImportFileScriptBeenExecuted = applyImportScript( | ||
| options, | ||
| commandExtractor, | ||
| dialect, | ||
| formatter, | ||
| targets | ||
| ); | ||
| applyImportFiles( | ||
| options, | ||
| commandExtractor, | ||
| dialect, | ||
| formatter, | ||
| hasDefaultImportFileScriptBeenExecuted ? "" : getDefaultImportFile( options ), | ||
| targets | ||
| ); | ||
| } | ||
|
|
||
| private String getDefaultImportFile(ExecutionOptions options) { | ||
| return skipDefaultFileImport( options ) ? "" : DEFAULT_IMPORT_FILE; | ||
| } | ||
|
|
||
| private static boolean skipDefaultFileImport(ExecutionOptions options) { | ||
| return getBoolean( HBM2DDL_SKIP_DEFAULT_IMPORT_FILE, options.getConfigurationValues(), false ); | ||
| } | ||
|
|
||
| /** | ||
| * In principle, we should format the commands in the import script if the | ||
| * {@code format} parameter is {@code true}, and since it's supposed to be | ||
| * a list of DML statements, we should use the {@linkplain FormatStyle#BASIC | ||
| * basic DML formatter} to do that. However, in practice we don't really know | ||
| * much about what this file contains, and we have never formatted it in the | ||
| * past, so there's no compelling reason to start now. In fact, if we have | ||
| * lists of many {@code insert} statements on the same table, which is what | ||
| * we typically expect, it's probably better to not format. | ||
| */ | ||
| private static Formatter getImportScriptFormatter(boolean format) { | ||
| // return format ? FormatStyle.BASIC.getFormatter() : FormatStyle.NONE.getFormatter(); | ||
| return FormatStyle.NONE.getFormatter(); | ||
| } | ||
|
|
||
| /** | ||
| * Handles import scripts specified using | ||
| * {@link org.hibernate.cfg.AvailableSettings#HBM2DDL_IMPORT_FILES}. | ||
| * | ||
| * @return {@code true} if the legacy {@linkplain #DEFAULT_IMPORT_FILE default import file} | ||
| * was one of the listed imported files that were executed | ||
| */ | ||
| private boolean applyImportScript( | ||
| ExecutionOptions options, | ||
| SqlScriptCommandExtractor commandExtractor, | ||
| Dialect dialect, | ||
| Formatter formatter, | ||
| GenerationTarget[] targets) { | ||
| final Object importScriptSetting = getImportScriptSetting( options ); | ||
| if ( importScriptSetting != null ) { | ||
| final ScriptSourceInput importScriptInput = | ||
| interpretScriptSourceSetting( importScriptSetting, getClassLoaderService(), getCharsetName( options ) ); | ||
| applyScript( | ||
| options, | ||
| commandExtractor, | ||
| dialect, | ||
| importScriptInput, | ||
| formatter, | ||
| targets | ||
| ); | ||
| return containsDefaultImportFile( importScriptInput, options ); | ||
| } | ||
| else { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private boolean containsDefaultImportFile(ScriptSourceInput importScriptInput,ExecutionOptions options ) { | ||
| if ( skipDefaultFileImport( options ) ) { | ||
| return false; | ||
| } | ||
| final URL defaultImportFileUrl = getClassLoaderService().locateResource( DEFAULT_IMPORT_FILE ); | ||
| return defaultImportFileUrl != null && importScriptInput.containsScript( defaultImportFileUrl ); | ||
| } | ||
|
|
||
| /** | ||
| * Handles import scripts specified using | ||
| * {@link org.hibernate.cfg.AvailableSettings#JAKARTA_HBM2DDL_LOAD_SCRIPT_SOURCE}. | ||
| */ | ||
| private void applyImportFiles( | ||
| ExecutionOptions options, | ||
| SqlScriptCommandExtractor commandExtractor, | ||
| Dialect dialect, | ||
| Formatter formatter, | ||
| String defaultImportFile, | ||
| GenerationTarget[] targets) { | ||
| final String[] importFiles = | ||
| StringHelper.split( ",", | ||
| getString( HBM2DDL_IMPORT_FILES, options.getConfigurationValues(), defaultImportFile ) ); | ||
| final String charsetName = getCharsetName( options ); | ||
| final ClassLoaderService classLoaderService = getClassLoaderService(); | ||
| for ( String currentFile : importFiles ) { | ||
| final String resourceName = currentFile.trim(); | ||
| if ( !resourceName.isEmpty() ) { //skip empty resource names | ||
| applyScript( | ||
| options, | ||
| commandExtractor, | ||
| dialect, | ||
| interpretLegacyImportScriptSetting( resourceName, classLoaderService, charsetName ), | ||
| formatter, | ||
| targets | ||
| ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private ScriptSourceInput interpretLegacyImportScriptSetting( | ||
| String resourceName, | ||
| ClassLoaderService classLoaderService, | ||
| String charsetName) { | ||
| try { | ||
| final URL resourceUrl = classLoaderService.locateResource( resourceName ); | ||
| return resourceUrl == null | ||
| ? ScriptSourceInputNonExistentImpl.INSTANCE | ||
| : new ScriptSourceInputFromUrl( resourceUrl, charsetName ); | ||
| } | ||
| catch (Exception e) { | ||
| throw new SchemaManagementException( "Error resolving legacy import resource : " + resourceName, e ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @see org.hibernate.cfg.AvailableSettings#HBM2DDL_CHARSET_NAME | ||
| */ | ||
| private static String getCharsetName(ExecutionOptions options) { | ||
| return (String) options.getConfigurationValues().get( HBM2DDL_CHARSET_NAME ); | ||
| } | ||
|
|
||
| /** | ||
| * @see org.hibernate.cfg.AvailableSettings#JAKARTA_HBM2DDL_LOAD_SCRIPT_SOURCE | ||
| * | ||
| * @return a {@link java.io.Reader} or a string URL | ||
| */ | ||
| private static Object getImportScriptSetting(ExecutionOptions options) { | ||
| final Map<String, Object> configuration = options.getConfigurationValues(); | ||
| final Object importScriptSetting = configuration.get( HBM2DDL_LOAD_SCRIPT_SOURCE ); | ||
| return importScriptSetting == null | ||
| ? configuration.get( JAKARTA_HBM2DDL_LOAD_SCRIPT_SOURCE ) | ||
| : importScriptSetting; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check notice
Code scanning / CodeQL
Useless parameter Note