View Javadoc

1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * 
4    * Copyright (C) 1999-2006, QOS.ch
5    * 
6    * This library is free software, you can redistribute it and/or modify it under
7    * the terms of the GNU Lesser General Public License as published by the Free
8    * Software Foundation.
9    */
10  package ch.qos.logback.core.db;
11  
12  import java.sql.Connection;
13  import java.sql.DatabaseMetaData;
14  import java.sql.SQLException;
15  
16  import ch.qos.logback.core.db.dialect.DBUtil;
17  import ch.qos.logback.core.db.dialect.SQLDialectCode;
18  import ch.qos.logback.core.spi.ContextAwareBase;
19  
20  
21  /**
22   * @author Ceki Gülcü
23   */
24  public abstract class ConnectionSourceBase extends ContextAwareBase implements ConnectionSource {
25    
26    private boolean started;
27    
28    private String user = null;
29    private String password = null;
30  
31    // initially we have an unkonw dialect
32    private SQLDialectCode dialectCode = SQLDialectCode.UNKNOWN_DIALECT;
33    private boolean supportsGetGeneratedKeys = false;
34    private boolean supportsBatchUpdates = false;
35  
36  
37    /**
38     * Learn relevant information about this connection source.
39     *
40     */
41    public void discoverConnnectionProperties() {
42      try {
43        Connection connection = getConnection();
44        if (connection == null) {
45          addWarn("Could not get a connection");
46          return;
47        }
48        DatabaseMetaData meta = connection.getMetaData();
49        DBUtil util = new DBUtil();
50        util.setContext(getContext());
51        supportsGetGeneratedKeys = util.supportsGetGeneratedKeys(meta);
52        supportsBatchUpdates = util.supportsBatchUpdates(meta);
53        dialectCode = DBUtil.discoverSQLDialect(meta);
54        System.out.println("Driver name="+meta.getDriverName());
55        System.out.println("Driver version="+meta.getDriverVersion());
56        System.out.println("supportsGetGeneratedKeys="+supportsGetGeneratedKeys);
57        
58      } catch (SQLException se) {
59        addWarn("Could not discover the dialect to use.", se);
60      }
61    }
62  
63    /**
64     * Does this connection support the JDBC Connection.getGeneratedKeys method?
65     */
66    public final boolean supportsGetGeneratedKeys() {
67      return supportsGetGeneratedKeys;
68    }
69  
70    public final SQLDialectCode getSQLDialectCode() {
71      return dialectCode;
72    }
73  
74    /**
75     * Get the password for this connection source.
76     */
77    public final String getPassword() {
78      return password;
79    }
80  
81    /**
82     * Sets the password.
83     * @param password The password to set
84     */
85    public final void setPassword(final String password) {
86      this.password = password;
87    }
88  
89    /**
90     * Get the user for this connection source.
91     */
92    public final String getUser() {
93      return user;
94    }
95  
96    /**
97     * Sets the username.
98     * @param username The username to set
99     */
100   public final void setUser(final String username) {
101     this.user = username;
102   }
103 
104   /**
105    * Does this connection support batch updates?
106    */
107   public final boolean supportsBatchUpdates() {
108     return supportsBatchUpdates;
109   }
110 
111   public boolean isStarted() {
112     return started;
113   }
114 
115   public void start() {
116     started = true;
117   }
118 
119   public void stop() {
120     started = false;
121   }
122   
123   
124 }