001/*
002 * Copyright 2012-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.autoconfigure.quartz;
018
019import javax.sql.DataSource;
020
021import org.springframework.boot.jdbc.AbstractDataSourceInitializer;
022import org.springframework.boot.jdbc.DataSourceInitializationMode;
023import org.springframework.core.io.ResourceLoader;
024import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
025import org.springframework.util.Assert;
026
027/**
028 * Initialize the Quartz Scheduler schema.
029 *
030 * @author Vedran Pavic
031 * @since 2.0.0
032 */
033public class QuartzDataSourceInitializer extends AbstractDataSourceInitializer {
034
035        private final QuartzProperties properties;
036
037        public QuartzDataSourceInitializer(DataSource dataSource,
038                        ResourceLoader resourceLoader, QuartzProperties properties) {
039                super(dataSource, resourceLoader);
040                Assert.notNull(properties, "QuartzProperties must not be null");
041                this.properties = properties;
042        }
043
044        @Override
045        protected void customize(ResourceDatabasePopulator populator) {
046                populator.setCommentPrefix(this.properties.getJdbc().getCommentPrefix());
047        }
048
049        @Override
050        protected DataSourceInitializationMode getMode() {
051                return this.properties.getJdbc().getInitializeSchema();
052        }
053
054        @Override
055        protected String getSchemaLocation() {
056                return this.properties.getJdbc().getSchema();
057        }
058
059        @Override
060        protected String getDatabaseName() {
061                String databaseName = super.getDatabaseName();
062                if ("db2".equals(databaseName)) {
063                        return "db2_v95";
064                }
065                if ("mysql".equals(databaseName)) {
066                        return "mysql_innodb";
067                }
068                if ("postgresql".equals(databaseName)) {
069                        return "postgres";
070                }
071                if ("sqlserver".equals(databaseName)) {
072                        return "sqlServer";
073                }
074                return databaseName;
075        }
076
077}