Java Code Examples for org.springframework.jdbc.datasource.init.ResourceDatabasePopulator#addScript()

The following examples show how to use org.springframework.jdbc.datasource.init.ResourceDatabasePopulator#addScript() . 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: HermesDataSourceInitializer.java    From hermes with Apache License 2.0 7 votes vote down vote up
/**
 * DriverName: 
 * 1:MySQL Connector Java; 
 * 2:Oracle JDBC driver; 
 * 3:H2 JDBC Driver
 * @throws SQLException
 */
@PostConstruct
public void initData() throws SQLException {
	String dbDriverName = getDBDriveName();
	Logger.info("当前数据库驱动名称:"+dbDriverName);
	Logger.info("检查是否需要初始化脚本");
	Query query = new Query("from User");
	Long userCount = commonRepository.count(query.getCount(), new HashMap<String, Object>());
	if (userCount == 0) {
		Logger.info("开始进行初始化脚本");
		ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
		populator.setSqlScriptEncoding("utf-8");
		populator.addScript(dataScript);
		populator.populate(dataSource.getConnection());
		Logger.info("初始化脚本已完成");
		return;
	}else{
		Logger.info("不需要初始化脚本");
	}
}
 
Example 2
Source File: ElasticsearchConfig.java    From spring-content with Apache License 2.0 6 votes vote down vote up
@Bean
DataSourceInitializer datasourceInitializer(DataSource dataSource) {
    ResourceDatabasePopulator databasePopulator =
            new ResourceDatabasePopulator();

    databasePopulator.addScript(dropReopsitoryTables);
    databasePopulator.addScript(dataReopsitorySchema);
    databasePopulator.setIgnoreFailedDrops(true);

    DataSourceInitializer initializer = new DataSourceInitializer();
    initializer.setDataSource(dataSource);
    initializer.setDatabasePopulator(databasePopulator);

    return initializer;
}
 
Example 3
Source File: DaoEnvTestSpringModuleConfig.java    From herd with Apache License 2.0 6 votes vote down vote up
/**
 * Get a new herd data source based on an in-memory HSQLDB database. This data source is used for loading the configuration table as an environment property
 * source as well as for the JPA entity manager. It will therefore create/re-create the configuration table which is required for the former. It also
 * inserts required values for both scenarios.
 *
 * @return the test herd data source.
 */
@Bean
public static DataSource herdDataSource()
{
    // Create and return a data source that can connect directly to a JDBC URL.
    BasicDataSource basicDataSource = new BasicDataSource();
    basicDataSource.setDriverClassName(org.h2.Driver.class.getName());
    basicDataSource.setUsername("");
    basicDataSource.setPassword("");
    basicDataSource.setUrl("jdbc:h2:mem:herdTestDb");

    // Create and populate the configuration table.
    // This is needed for all data source method calls since it is used to create the environment property source which happens before
    // JPA and other non-static beans are initialized.
    ResourceDatabasePopulator resourceDatabasePopulator = new ResourceDatabasePopulator();
    resourceDatabasePopulator.addScript(new ClassPathResource("createConfigurationTableAndData.sql"));
    DatabasePopulatorUtils.execute(resourceDatabasePopulator, basicDataSource); // This is what the DataSourceInitializer does.

    return basicDataSource;
}
 
Example 4
Source File: JobConfig.java    From spring4-sandbox with Apache License 2.0 5 votes vote down vote up
@PostConstruct
public void initialize(){
	ResourceDatabasePopulator populator=new ResourceDatabasePopulator();
	populator.setContinueOnError(true);
	populator.addScript(resourceLoader.getResource("classpath:/org/springframework/batch/core/schema-h2.sql"));
	
	DatabasePopulatorUtils.execute(populator, dataSource);
}
 
Example 5
Source File: MasterDomain.java    From syncope with Apache License 2.0 5 votes vote down vote up
@Bean(name = "MasterResourceDatabasePopulator")
@ConditionalOnMissingBean(name = "MasterResourceDatabasePopulator")
public ResourceDatabasePopulator masterResourceDatabasePopulator() {
    ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
    databasePopulator.setContinueOnError(true);
    databasePopulator.setIgnoreFailedDrops(true);
    databasePopulator.setSqlScriptEncoding("UTF-8");
    databasePopulator.addScript(auditSql);
    return databasePopulator;
}
 
Example 6
Source File: TestDatabase.java    From we-cmdb with Apache License 2.0 5 votes vote down vote up
static public void prepareDatabase(DataSource dataSource) {
    ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.setContinueOnError(false);
    populator.setIgnoreFailedDrops(false);
    populator.setSeparator(";");
    populator.addScript(new ClassPathResource("/db/data_model.sql"));
    populator.execute(dataSource);
}
 
Example 7
Source File: SpringConfig.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public DataSourceInitializer dataSourceInitializer(DataSource dataSource) throws MalformedURLException {
    ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();

    databasePopulator.addScript(dropReopsitoryTables);
    databasePopulator.addScript(dataReopsitorySchema);
    databasePopulator.setIgnoreFailedDrops(true);

    DataSourceInitializer initializer = new DataSourceInitializer();
    initializer.setDataSource(dataSource);
    initializer.setDatabasePopulator(databasePopulator);

    return initializer;
}
 
Example 8
Source File: TaskRepositoryInitializer.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
	boolean isInitializeEnabled = (this.taskProperties.isInitializeEnabled() != null)
			? this.taskProperties.isInitializeEnabled()
			: this.taskInitializationEnabled;
	if (this.dataSource != null && isInitializeEnabled && this.taskProperties
			.getTablePrefix().equals(TaskProperties.DEFAULT_TABLE_PREFIX)) {
		String platform = getDatabaseType(this.dataSource);
		if ("hsql".equals(platform)) {
			platform = "hsqldb";
		}
		if ("postgres".equals(platform)) {
			platform = "postgresql";
		}
		if ("oracle".equals(platform)) {
			platform = "oracle10g";
		}
		if ("mysql".equals(platform)) {
			platform = "mysql";
		}
		if ("sqlserver".equals(platform)) {
			platform = "sqlserver";
		}
		ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
		String schemaLocation = schema;
		schemaLocation = schemaLocation.replace("@@platform@@", platform);
		populator.addScript(this.resourceLoader.getResource(schemaLocation));
		populator.setContinueOnError(true);
		logger.debug(
				String.format("Initializing task schema for %s database", platform));
		DatabasePopulatorUtils.execute(populator, this.dataSource);
	}
}
 
Example 9
Source File: DataflowRdbmsInitializer.java    From spring-cloud-dashboard with Apache License 2.0 5 votes vote down vote up
@Override
public void afterPropertiesSet() throws Exception {
	if (dataSource != null && definitionInitializationEnable) {
		String platform = getDatabaseType(dataSource);
		if ("hsql".equals(platform)) {
			platform = "hsqldb";
		}
		if ("postgres".equals(platform)) {
			platform = "postgresql";
		}
		if ("oracle".equals(platform)) {
			platform = "oracle10g";
		}
		ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
		String schemaLocation = schema;
		schemaLocation = schemaLocation.replace("@@platform@@", platform);
		String commonSchemaLocation = schemaLocation;
		commonSchemaLocation = commonSchemaLocation.replace("@@suffix@@", COMMON_SCHEMA_SUFFIX);
		logger.info(String.format("Adding dataflow schema %s for %s database", commonSchemaLocation,
				platform));
		populator.addScript(resourceLoader.getResource(commonSchemaLocation));

		String applicationssSchemaLocation = schemaLocation;
		applicationssSchemaLocation = applicationssSchemaLocation.replace("@@suffix@@", APPLICATIONS_SCHEMA_SUFFIX);
		logger.info(String.format("Adding dataflow schema %s for %s database", applicationssSchemaLocation,
				platform));
		populator.addScript(resourceLoader.getResource(applicationssSchemaLocation));

		String deploymentSchemaLocation = schemaLocation;
		deploymentSchemaLocation = deploymentSchemaLocation.replace("@@suffix@@", DEPLOYMENT_SCHEMA_SUFFIX);
		logger.info(String.format("Adding dataflow schema %s for %s database", deploymentSchemaLocation,
				platform));
		populator.addScript(resourceLoader.getResource(deploymentSchemaLocation));

		populator.setContinueOnError(true);
		logger.debug(String.format("Initializing dataflow schema for %s database", platform));
		DatabasePopulatorUtils.execute(populator, dataSource);
	}
}
 
Example 10
Source File: JdbcSinkConfiguration.java    From spring-cloud-stream-app-starters with Apache License 2.0 5 votes vote down vote up
@ConditionalOnProperty("jdbc.initialize")
@Bean
public DataSourceInitializer nonBootDataSourceInitializer(DataSource dataSource, ResourceLoader resourceLoader) {
	DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
	dataSourceInitializer.setDataSource(dataSource);
	ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
	databasePopulator.setIgnoreFailedDrops(true);
	dataSourceInitializer.setDatabasePopulator(databasePopulator);
	if ("true".equals(properties.getInitialize())) {
		databasePopulator.addScript(new DefaultInitializationScriptResource(properties));
	} else {
		databasePopulator.addScript(resourceLoader.getResource(properties.getInitialize()));
	}
	return dataSourceInitializer;
}
 
Example 11
Source File: OAuth2AuthorizationServerConfig.java    From spring-boot with Apache License 2.0 4 votes vote down vote up
private DatabasePopulator databasePopulator() {
    final ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.addScript(schemaScript);
    return populator;
}
 
Example 12
Source File: DatabaseInitializer.java    From lemonaid with MIT License 4 votes vote down vote up
private void runScript(Resource resource) {
    ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.setContinueOnError(true);
    populator.addScript(resource);
    DatabasePopulatorUtils.execute(populator, dataSource);
}
 
Example 13
Source File: SocialDatabasePopulator.java    From Spring-Security-Third-Edition with MIT License 4 votes vote down vote up
private void runScript(final Resource resource) {
    ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.setContinueOnError(true);
    populator.addScript(resource);
    DatabasePopulatorUtils.execute(populator, dataSource);
}
 
Example 14
Source File: SocialDatabasePopulator.java    From Spring-Security-Third-Edition with MIT License 4 votes vote down vote up
private void executeSql(final Resource resource) {
    ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.setContinueOnError(true);
    populator.addScript(resource);
    DatabasePopulatorUtils.execute(populator, dataSource);
}
 
Example 15
Source File: AppConfig.java    From Building-Web-Apps-with-Spring-5-and-Angular with MIT License 4 votes vote down vote up
private DatabasePopulator databasePopulator() {
    final ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.addScript(schemaScript);
    populator.addScript(dataScript);
    return populator;
}
 
Example 16
Source File: OAuth2AuthorizationServerConfigInMemory.java    From spring-security-oauth with MIT License 4 votes vote down vote up
private DatabasePopulator databasePopulator() {
    final ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.addScript(schemaScript);
    return populator;
}
 
Example 17
Source File: DatabaseInititializer.java    From flowable-engine with Apache License 2.0 4 votes vote down vote up
private DatabasePopulator databasePopulator() {
    ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.addScript(schemaScript);
    populator.addScript(dataScript);
    return populator;
}
 
Example 18
Source File: DatabaseInititializer.java    From activiti6-boot2 with Apache License 2.0 4 votes vote down vote up
private DatabasePopulator databasePopulator() {
  ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
  populator.addScript(schemaScript);
  populator.addScript(dataScript);
  return populator;
}
 
Example 19
Source File: OAuth2AuthorizationServerConfig.java    From oauth2lab with MIT License 4 votes vote down vote up
private DatabasePopulator databasePopulator() {
    final ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.addScript(schemaScript);
    populator.addScript(dataScript);
    return populator;
}
 
Example 20
Source File: TestDataConfig.java    From Spring with Apache License 2.0 4 votes vote down vote up
private DatabasePopulator databasePopulator() {
    final ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.addScript(schemaScript);
    populator.addScript(dataScript);
    return populator;
}