1 package com.quantum.sql;
3 import java.sql.Connection;
4 import java.sql.DatabaseMetaData;
5 import java.sql.Driver;
6 import java.sql.ResultSet;
7 import java.sql.SQLException;
8 import java.sql.Statement;
9 import java.util.Properties;
11 import com.quantum.Messages;
12 import com.quantum.adapters.DatabaseAdapter;
13 import com.quantum.model.Bookmark;
14 import com.quantum.model.ConnectionException;
15 import com.quantum.model.Entity;
16 import com.quantum.model.JDBCDriver;
17 import com.quantum.model.PasswordFinder;
18 import com.quantum.view.LogProxy;
22 * MultiSQLServer is a Singleton, used as a interface with the sql drivers.
23 * Use MultiSQLServer.getInstance() to get the object.
25 public class MultiSQLServer implements ConnectionEstablisher {
26 public static final String USERNAME = "user"; //$NON-NLS-1$
27 public static final String PASSWORD = "password"; //$NON-NLS-1$
28 private static MultiSQLServer instance = null;
30 private MultiSQLServer() {
32 public synchronized static MultiSQLServer getInstance() {
33 if (instance == null) {
34 instance = new MultiSQLServer();
39 public void commit(Connection con) throws SQLException {
40 LogProxy log = LogProxy.getInstance();
43 } catch (SQLException e) {
44 log.addText(LogProxy.ERROR, "Error commiting: " + e, e); //$NON-NLS-1$
49 public void rollback(Connection con) throws SQLException {
50 LogProxy log = LogProxy.getInstance();
53 } catch (SQLException e) {
54 log.addText(LogProxy.ERROR, "Error rolling back: " + e, e); //$NON-NLS-1$
59 public void setAutoCommit(Connection con, boolean enabled) {
60 LogProxy log = LogProxy.getInstance();
63 con.setAutoCommit(enabled);
65 log.addText(LogProxy.ERROR, "Please connect before setting autocommit"); //$NON-NLS-1$
67 } catch (SQLException e) {
68 log.addText(LogProxy.ERROR, "Error setting autocommit: " + e, e); //$NON-NLS-1$
72 public void disconnect(Connection connection) throws ConnectionException {
74 if (connection != null) {
77 } catch (SQLException e) {
78 throw new ConnectionException(e);
83 * Makes a connection to a JDBC driver based on the data from a bookmark
85 * The Bookmark with the data needed to make the connection
86 * @param passwordFinder -
87 * A utility class that can be invoked if the bookmark does not
89 * @return The Connection object if everything went OK
91 public Connection connect(Bookmark bookmark, PasswordFinder passwordFinder)
92 throws ConnectionException {
94 String password = bookmark.getPassword();
95 if (bookmark.getPromptForPassword()) {
96 password = passwordFinder.getPassword();
97 if (passwordFinder.isPasswordMeantToBeSaved()) {
98 bookmark.setPassword(password);
102 if (password != null) {
103 Connection connection = connect(bookmark, password);
104 if (connection != null) {
105 // Set the autoCommit state of the bookmark to the default on new connections
106 bookmark.setAutoCommit(bookmark.getDefaultAutoCommit());
107 // Set the autoCommit state of the JDBC connection to the bookmark autoCommit statec
108 setAutoCommit(connection, bookmark.isAutoCommit());
116 private Connection connect(Bookmark bookmark, String password)
117 throws ConnectionException {
118 LogProxy log = LogProxy.getInstance();
119 log.addText(LogProxy.QUERY, "Connecting to: " + bookmark.getName()); //$NON-NLS-1$
121 JDBCDriver jdbcDriver = bookmark.getJDBCDriver();
122 Driver driver = jdbcDriver.getDriver();
123 if (driver != null) {
124 Properties props = new Properties();
125 props.put(USERNAME, bookmark.getUsername());
126 props.put(PASSWORD, password);
127 Connection connection =
128 driver.connect(bookmark.getConnect(), props);
129 if (connection == null) {
130 throw new ConnectionException("Error: Driver returned a null connection: " + bookmark.toString()); //$NON-NLS-1$
133 DatabaseMetaData metaData = connection.getMetaData();
134 jdbcDriver.setName(metaData.getDriverName());
135 jdbcDriver.setVersion(metaData.getDriverVersion());
136 log.addText(LogProxy.RESULTS, "Connected to: " + bookmark.getName()); //$NON-NLS-1$
137 System.out.println("Connected"); //$NON-NLS-1$
140 throw new ConnectionException(Messages.getString(
141 ConnectionException.class, "couldNotInstaniateDriver",
142 new Object[] { jdbcDriver.getClassName(), bookmark.getName() }));
144 } catch (SQLException e) {
145 throw new ConnectionException(e);
148 public SQLResults execute(Bookmark bookmark, Connection con, Entity entity, String s)
149 throws SQLException {
150 return execute(bookmark, con, entity, s, 200);
153 public SQLResults execute(Bookmark bookmark, Connection con, String s)
154 throws SQLException {
155 return execute(bookmark, con, null, s, 200);
158 public SQLResultSetResults getMetaData(Entity entity, Connection connection) throws SQLException {
159 String query = "SELECT * FROM " + entity.getQuotedTableName() + " WHERE (1 = 0)"; //$NON-NLS-1$ //$NON-NLS-2$
160 SQLResultSetResults results = null;
161 if (connection != null) {
162 Statement statement = connection.createStatement();
164 ResultSet set = statement.executeQuery(query);
166 results = SQLMetaDataResults.create(entity.getBookmark(), set, query, entity);
177 public SQLResults execute(Bookmark bookmark, Connection con, String sql,
178 int numberOfRowsPerPage) throws SQLException {
179 return execute(bookmark, con, null, sql, numberOfRowsPerPage);
183 public SQLResults execute(
188 int numberOfRowsPerPage)
189 throws SQLException {
191 long startTime = System.currentTimeMillis();
192 System.out.println("Executing"); //$NON-NLS-1$
193 LogProxy log = LogProxy.getInstance();
194 log.addText(LogProxy.QUERY, "Executing Request [" + sql + "]"); //$NON-NLS-1$ //$NON-NLS-2$
195 Statement statement = con.createStatement();
198 if (statement.execute(sql)) {
199 ResultSet set = statement.getResultSet();
201 results = SQLStandardResultSetResults.create(set, bookmark, sql, entity, numberOfRowsPerPage);
206 int updates = statement.getUpdateCount();
207 results = new SQLUpdateResults(updates);
209 log.addText(LogProxy.RESULTS, "Success: result set displayed"); //$NON-NLS-1$
210 if (results != null) {
211 results.setTime(System.currentTimeMillis() - startTime);
219 public int getSize(Bookmark bookmark, Connection connection, String tableName, DatabaseAdapter adapter) throws SQLException {
220 SQLResultSetResults results = (SQLResultSetResults) execute(
221 bookmark, connection, adapter.getCountQuery(tableName));
222 if (results.getRowCount() > 0 && results.getColumnCount() > 0) {
223 return Integer.parseInt(results.getElement(1, 1).toString());