001/*
002 * Stallion Core: A Modern Web Framework
003 *
004 * Copyright (C) 2015 - 2016 Stallion Software LLC.
005 *
006 * This program is free software: you can redistribute it and/or modify it under the terms of the
007 * GNU General Public License as published by the Free Software Foundation, either version 2 of
008 * the License, or (at your option) any later version. This program is distributed in the hope that
009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
011 * License for more details. You should have received a copy of the GNU General Public License
012 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
013 *
014 *
015 *
016 */
017
018package io.stallion.settings.childSections;
019
020import io.stallion.exceptions.ConfigException;
021import io.stallion.settings.SettingMeta;
022
023import static io.stallion.utils.Literals.empty;
024
025
026public class DbConfig implements SettingsSection {
027    @SettingMeta()
028    private String username;
029    @SettingMeta
030    private String url;
031    @SettingMeta
032    private String password;
033    @SettingMeta()
034    private String driverClass;
035    @SettingMeta()
036    private String implementationClass;
037
038    @Override
039    public void postLoad() {
040        if (empty(driverClass) && !empty(url)) {
041
042            if (url.contains("jdbc:mysql")) {
043                // Since MySql driver is GPL, have to package the maria db driver
044                //driverClass = "com.mysql.jdbc.Driver";
045                driverClass = "org.mariadb.jdbc.Driver";
046            } else if (url.contains("jdbc:mariadb")) {
047                driverClass = "org.mariadb.jdbc.Driver";
048            } else if (url.contains("jdbc:postgresql")) {
049                driverClass = "org.postgresql.Driver";
050            } else {
051                throw new ConfigException("No database driverClass defined, and could not guess driver class from url " + url);
052            }
053        }
054        if (empty(implementationClass) && !empty(url)) {
055            if (url.contains("jdbc:mysql") || url.contains("jdbc:mariadb")) {
056                implementationClass = "io.stallion.dataAccess.db.mysql.MySqlDbImplementation";
057            } else if (url.contains("jdbc:postgresql")) {
058                implementationClass = "io.stallion.dataAccess.db.postgres.PostgresDbImplementation";
059            } else {
060                throw new ConfigException("No database implementation class defined (implements io.stallion.dataAccess.db.DbImplementation), and could not guess class from url " + url);
061            }
062
063        }
064    }
065
066    public String getUsername() {
067        return username;
068    }
069
070    public void setUsername(String username) {
071        this.username = username;
072    }
073
074    public String getUrl() {
075        return url;
076    }
077
078    public void setUrl(String url) {
079        this.url = url;
080    }
081
082    public String getPassword() {
083        return password;
084    }
085
086    public void setPassword(String password) {
087        this.password = password;
088    }
089
090    public String getDbAccessorClass() {
091        return dbAccessorClass;
092    }
093
094    public DbConfig setDbAccessorClass(String dbAccessorClass) {
095        this.dbAccessorClass = dbAccessorClass;
096        return this;
097    }
098
099    @SettingMeta(val = "io.stallion.dataAccess.db.DB")
100    private String dbAccessorClass;
101
102    public String getDriverClass() {
103        return driverClass;
104    }
105
106    public DbConfig setDriverClass(String driverClass) {
107        this.driverClass = driverClass;
108        return this;
109    }
110
111    public String getImplementationClass() {
112        return implementationClass;
113    }
114
115    public DbConfig setImplementationClass(String implementationClass) {
116        this.implementationClass = implementationClass;
117        return this;
118    }
119}