001: package org.dbbrowser.ui;
002:
003: import infrastructure.logging.Log;
004:
005: import java.sql.Connection;
006: import java.sql.SQLException;
007: import org.dbbrowser.db.engine.exception.DBEngineException;
008: import org.dbbrowser.db.engine.model.DBTable;
009: import org.dbbrowser.db.engine.rawsqlengine.DBRawSQLEngine;
010: import org.dbbrowser.db.engine.rawsqlengine.GenericRawSQLEngine;
011: import org.dbbrowser.drivermanager.ConnectionInfo;
012: import org.dbbrowser.drivermanager.DBBrowserDriverManager;
013: import org.dbbrowser.drivermanager.DriverManagerException;
014:
015: /**
016: * UI Controller for running raw SQL entered by the user
017: */
018: public class UIControllerForRawSQL {
019: private ConnectionInfo connectionInfo = null;
020: private DBRawSQLEngine dbRawSQLEngine = null;
021:
022: /**
023: * Connects to the database using the supplied connection info. DirverManager caches the connection. Start a DBQueryEngine
024: * @param connectionInfo
025: * @param masterPassword
026: */
027: public void setup(ConnectionInfo connectionInfo,
028: String masterPassword) throws DriverManagerException,
029: DBEngineException {
030: this .connectionInfo = connectionInfo;
031: Connection connection = DBBrowserDriverManager.getInstance()
032: .getConnection(connectionInfo, masterPassword);
033:
034: try {
035: this .dbRawSQLEngine = new GenericRawSQLEngine(connection
036: .createStatement());
037: } catch (SQLException exc) {
038: throw new DBEngineException(exc.getMessage());
039: }
040: }
041:
042: /**
043: * Return the connection info representing the connection
044: * @return
045: */
046: public ConnectionInfo getConnectionInfo() {
047: return this .connectionInfo;
048: }
049:
050: /**
051: * Set auto commit
052: * @param autoCommitFlag
053: * @throws DBEngineException
054: */
055: public void setAutoCommit(boolean autoCommitFlag)
056: throws DBEngineException {
057: try {
058: this .dbRawSQLEngine.getStatement().getConnection()
059: .setAutoCommit(autoCommitFlag);
060: } catch (SQLException exc) {
061: throw new DBEngineException(exc.getMessage());
062: }
063: }
064:
065: /**
066: * Commit
067: * @throws DBEngineException
068: */
069: public void commit() throws DBEngineException {
070: try {
071: this .dbRawSQLEngine.getStatement().getConnection().commit();
072: Log.getInstance().debugMessage("Commit complete",
073: this .getClass().getName());
074: } catch (SQLException exc) {
075: throw new DBEngineException(exc.getMessage());
076: }
077: }
078:
079: /**
080: * Rollback
081: * @throws DBEngineException
082: */
083: public void rollback() throws DBEngineException {
084: try {
085: this .dbRawSQLEngine.getStatement().getConnection()
086: .rollback();
087: Log.getInstance().debugMessage("Rollback complete",
088: this .getClass().getName());
089: } catch (SQLException exc) {
090: throw new DBEngineException(exc.getMessage());
091: }
092: }
093:
094: /**
095: * Run the raw SQL statement. Returns a DBTable if the query returns a result(SELECT queries)
096: * @param sql
097: * @return - DBTable or null if the query does not return anything
098: * @throws DBEngineException
099: */
100: public DBTable runRawSQL(String sql) throws DBEngineException {
101: return this.dbRawSQLEngine.runRawSQL(sql);
102: }
103: }
|