001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.db;
015
016import java.sql.Connection;
017import java.sql.DatabaseMetaData;
018import java.sql.SQLException;
019
020import ch.qos.logback.core.db.dialect.DBUtil;
021import ch.qos.logback.core.db.dialect.SQLDialectCode;
022import ch.qos.logback.core.spi.ContextAwareBase;
023
024/**
025 * @author Ceki Gülcü
026 */
027public abstract class ConnectionSourceBase extends ContextAwareBase implements ConnectionSource {
028
029    private boolean started;
030
031    private String user = null;
032    private String password = null;
033
034    // initially we have an unknown dialect
035    private SQLDialectCode dialectCode = SQLDialectCode.UNKNOWN_DIALECT;
036    private boolean supportsGetGeneratedKeys = false;
037    private boolean supportsBatchUpdates = false;
038
039    /**
040     * Learn relevant information about this connection source.
041     *
042     */
043    public void discoverConnectionProperties() {
044        Connection connection = null;
045        try {
046            connection = getConnection();
047            if (connection == null) {
048                addWarn("Could not get a connection");
049                return;
050            }
051            DatabaseMetaData meta = connection.getMetaData();
052            DBUtil util = new DBUtil();
053            util.setContext(getContext());
054            supportsGetGeneratedKeys = util.supportsGetGeneratedKeys(meta);
055            supportsBatchUpdates = util.supportsBatchUpdates(meta);
056            dialectCode = DBUtil.discoverSQLDialect(meta);
057            addInfo("Driver name=" + meta.getDriverName());
058            addInfo("Driver version=" + meta.getDriverVersion());
059            addInfo("supportsGetGeneratedKeys=" + supportsGetGeneratedKeys);
060
061        } catch (SQLException se) {
062            addWarn("Could not discover the dialect to use.", se);
063        } finally {
064            DBHelper.closeConnection(connection);
065        }
066    }
067
068    /**
069     * Does this connection support the JDBC Connection.getGeneratedKeys method?
070     */
071    public final boolean supportsGetGeneratedKeys() {
072        return supportsGetGeneratedKeys;
073    }
074
075    public final SQLDialectCode getSQLDialectCode() {
076        return dialectCode;
077    }
078
079    /**
080     * Get the password for this connection source.
081     */
082    public final String getPassword() {
083        return password;
084    }
085
086    /**
087     * Sets the password.
088     * @param password The password to set
089     */
090    public final void setPassword(final String password) {
091        this.password = password;
092    }
093
094    /**
095     * Get the user for this connection source.
096     */
097    public final String getUser() {
098        return user;
099    }
100
101    /**
102     * Sets the username.
103     * @param username The username to set
104     */
105    public final void setUser(final String username) {
106        this.user = username;
107    }
108
109    /**
110     * Does this connection support batch updates?
111     */
112    public final boolean supportsBatchUpdates() {
113        return supportsBatchUpdates;
114    }
115
116    public boolean isStarted() {
117        return started;
118    }
119
120    public void start() {
121        started = true;
122    }
123
124    public void stop() {
125        started = false;
126    }
127
128}