|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.tool.schema.internal; |
| 6 | + |
| 7 | +import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; |
| 8 | +import org.hibernate.dialect.Dialect; |
| 9 | +import org.hibernate.engine.jdbc.internal.FormatStyle; |
| 10 | +import org.hibernate.engine.jdbc.internal.Formatter; |
| 11 | +import org.hibernate.internal.util.StringHelper; |
| 12 | +import org.hibernate.tool.schema.internal.exec.ScriptSourceInputFromUrl; |
| 13 | +import org.hibernate.tool.schema.internal.exec.ScriptSourceInputNonExistentImpl; |
| 14 | +import org.hibernate.tool.schema.spi.ExecutionOptions; |
| 15 | +import org.hibernate.tool.schema.spi.GenerationTarget; |
| 16 | +import org.hibernate.tool.schema.spi.SchemaManagementException; |
| 17 | +import org.hibernate.tool.schema.spi.ScriptSourceInput; |
| 18 | +import org.hibernate.tool.schema.spi.SqlScriptCommandExtractor; |
| 19 | + |
| 20 | +import java.net.URL; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import static org.hibernate.cfg.SchemaToolingSettings.HBM2DDL_CHARSET_NAME; |
| 24 | +import static org.hibernate.cfg.SchemaToolingSettings.HBM2DDL_IMPORT_FILES; |
| 25 | +import static org.hibernate.cfg.SchemaToolingSettings.HBM2DDL_LOAD_SCRIPT_SOURCE; |
| 26 | +import static org.hibernate.cfg.SchemaToolingSettings.HBM2DDL_SKIP_DEFAULT_IMPORT_FILE; |
| 27 | +import static org.hibernate.cfg.SchemaToolingSettings.JAKARTA_HBM2DDL_LOAD_SCRIPT_SOURCE; |
| 28 | +import static org.hibernate.internal.util.config.ConfigurationHelper.getBoolean; |
| 29 | +import static org.hibernate.internal.util.config.ConfigurationHelper.getString; |
| 30 | +import static org.hibernate.tool.schema.internal.Helper.applyScript; |
| 31 | +import static org.hibernate.tool.schema.internal.Helper.interpretScriptSourceSetting; |
| 32 | + |
| 33 | +/** |
| 34 | + * Handles population from {@value #DEFAULT_IMPORT_FILE} and other scripts. |
| 35 | + */ |
| 36 | +public abstract class AbstractSchemaPopulator { |
| 37 | + |
| 38 | + public static final String DEFAULT_IMPORT_FILE = "/import.sql"; |
| 39 | + |
| 40 | + abstract ClassLoaderService getClassLoaderService(); |
| 41 | + |
| 42 | + void applyImportSources( |
| 43 | + ExecutionOptions options, |
| 44 | + SqlScriptCommandExtractor commandExtractor, |
| 45 | + boolean format, |
| 46 | + Dialect dialect, |
| 47 | + GenerationTarget... targets) { |
| 48 | + |
| 49 | + final Formatter formatter = getImportScriptFormatter(format); |
| 50 | + |
| 51 | + boolean hasDefaultImportFileScriptBeenExecuted = applyImportScript( |
| 52 | + options, |
| 53 | + commandExtractor, |
| 54 | + dialect, |
| 55 | + formatter, |
| 56 | + targets |
| 57 | + ); |
| 58 | + applyImportFiles( |
| 59 | + options, |
| 60 | + commandExtractor, |
| 61 | + dialect, |
| 62 | + formatter, |
| 63 | + hasDefaultImportFileScriptBeenExecuted ? "" : getDefaultImportFile( options ), |
| 64 | + targets |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + private String getDefaultImportFile(ExecutionOptions options) { |
| 69 | + return skipDefaultFileImport( options ) ? "" : DEFAULT_IMPORT_FILE; |
| 70 | + } |
| 71 | + |
| 72 | + private static boolean skipDefaultFileImport(ExecutionOptions options) { |
| 73 | + return getBoolean( HBM2DDL_SKIP_DEFAULT_IMPORT_FILE, options.getConfigurationValues(), false ); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * In principle, we should format the commands in the import script if the |
| 78 | + * {@code format} parameter is {@code true}, and since it's supposed to be |
| 79 | + * a list of DML statements, we should use the {@linkplain FormatStyle#BASIC |
| 80 | + * basic DML formatter} to do that. However, in practice we don't really know |
| 81 | + * much about what this file contains, and we have never formatted it in the |
| 82 | + * past, so there's no compelling reason to start now. In fact, if we have |
| 83 | + * lists of many {@code insert} statements on the same table, which is what |
| 84 | + * we typically expect, it's probably better to not format. |
| 85 | + */ |
| 86 | + private static Formatter getImportScriptFormatter(boolean format) { |
| 87 | +// return format ? FormatStyle.BASIC.getFormatter() : FormatStyle.NONE.getFormatter(); |
| 88 | + return FormatStyle.NONE.getFormatter(); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Handles import scripts specified using |
| 93 | + * {@link org.hibernate.cfg.AvailableSettings#HBM2DDL_IMPORT_FILES}. |
| 94 | + * |
| 95 | + * @return {@code true} if the legacy {@linkplain #DEFAULT_IMPORT_FILE default import file} |
| 96 | + * was one of the listed imported files that were executed |
| 97 | + */ |
| 98 | + private boolean applyImportScript( |
| 99 | + ExecutionOptions options, |
| 100 | + SqlScriptCommandExtractor commandExtractor, |
| 101 | + Dialect dialect, |
| 102 | + Formatter formatter, |
| 103 | + GenerationTarget[] targets) { |
| 104 | + final Object importScriptSetting = getImportScriptSetting( options ); |
| 105 | + if ( importScriptSetting != null ) { |
| 106 | + final ScriptSourceInput importScriptInput = |
| 107 | + interpretScriptSourceSetting( importScriptSetting, getClassLoaderService(), getCharsetName( options ) ); |
| 108 | + applyScript( |
| 109 | + options, |
| 110 | + commandExtractor, |
| 111 | + dialect, |
| 112 | + importScriptInput, |
| 113 | + formatter, |
| 114 | + targets |
| 115 | + ); |
| 116 | + return containsDefaultImportFile( importScriptInput, options ); |
| 117 | + } |
| 118 | + else { |
| 119 | + return false; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private boolean containsDefaultImportFile(ScriptSourceInput importScriptInput,ExecutionOptions options ) { |
| 124 | + if ( skipDefaultFileImport( options ) ) { |
| 125 | + return false; |
| 126 | + } |
| 127 | + final URL defaultImportFileUrl = getClassLoaderService().locateResource( DEFAULT_IMPORT_FILE ); |
| 128 | + return defaultImportFileUrl != null && importScriptInput.containsScript( defaultImportFileUrl ); |
| 129 | + } |
| 130 | + |
| 131 | + /** |
| 132 | + * Handles import scripts specified using |
| 133 | + * {@link org.hibernate.cfg.AvailableSettings#JAKARTA_HBM2DDL_LOAD_SCRIPT_SOURCE}. |
| 134 | + */ |
| 135 | + private void applyImportFiles( |
| 136 | + ExecutionOptions options, |
| 137 | + SqlScriptCommandExtractor commandExtractor, |
| 138 | + Dialect dialect, |
| 139 | + Formatter formatter, |
| 140 | + String defaultImportFile, |
| 141 | + GenerationTarget[] targets) { |
| 142 | + final String[] importFiles = |
| 143 | + StringHelper.split( ",", |
| 144 | + getString( HBM2DDL_IMPORT_FILES, options.getConfigurationValues(), defaultImportFile ) ); |
| 145 | + final String charsetName = getCharsetName( options ); |
| 146 | + final ClassLoaderService classLoaderService = getClassLoaderService(); |
| 147 | + for ( String currentFile : importFiles ) { |
| 148 | + final String resourceName = currentFile.trim(); |
| 149 | + if ( !resourceName.isEmpty() ) { //skip empty resource names |
| 150 | + applyScript( |
| 151 | + options, |
| 152 | + commandExtractor, |
| 153 | + dialect, |
| 154 | + interpretLegacyImportScriptSetting( resourceName, classLoaderService, charsetName ), |
| 155 | + formatter, |
| 156 | + targets |
| 157 | + ); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + private ScriptSourceInput interpretLegacyImportScriptSetting( |
| 163 | + String resourceName, |
| 164 | + ClassLoaderService classLoaderService, |
| 165 | + String charsetName) { |
| 166 | + try { |
| 167 | + final URL resourceUrl = classLoaderService.locateResource( resourceName ); |
| 168 | + return resourceUrl == null |
| 169 | + ? ScriptSourceInputNonExistentImpl.INSTANCE |
| 170 | + : new ScriptSourceInputFromUrl( resourceUrl, charsetName ); |
| 171 | + } |
| 172 | + catch (Exception e) { |
| 173 | + throw new SchemaManagementException( "Error resolving legacy import resource : " + resourceName, e ); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * @see org.hibernate.cfg.AvailableSettings#HBM2DDL_CHARSET_NAME |
| 179 | + */ |
| 180 | + private static String getCharsetName(ExecutionOptions options) { |
| 181 | + return (String) options.getConfigurationValues().get( HBM2DDL_CHARSET_NAME ); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * @see org.hibernate.cfg.AvailableSettings#JAKARTA_HBM2DDL_LOAD_SCRIPT_SOURCE |
| 186 | + * |
| 187 | + * @return a {@link java.io.Reader} or a string URL |
| 188 | + */ |
| 189 | + private static Object getImportScriptSetting(ExecutionOptions options) { |
| 190 | + final Map<String, Object> configuration = options.getConfigurationValues(); |
| 191 | + final Object importScriptSetting = configuration.get( HBM2DDL_LOAD_SCRIPT_SOURCE ); |
| 192 | + return importScriptSetting == null |
| 193 | + ? configuration.get( JAKARTA_HBM2DDL_LOAD_SCRIPT_SOURCE ) |
| 194 | + : importScriptSetting; |
| 195 | + } |
| 196 | +} |
0 commit comments