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.ColumnInfo;
009: import org.dbbrowser.db.engine.model.DBRow;
010: import org.dbbrowser.db.engine.model.View;
011: import org.dbbrowser.db.engine.updateengine.DBUpdateEngine;
012: import org.dbbrowser.db.engine.updateengine.GenericDBUpdateEngine;
013: import org.dbbrowser.db.engine.updateengine.MySQLDBUpdateEngine;
014: import org.dbbrowser.db.engine.updateengine.OracleDBUpdateEngine;
015: import org.dbbrowser.drivermanager.ConnectionInfo;
016: import org.dbbrowser.drivermanager.DBBrowserDriverManager;
017: import org.dbbrowser.drivermanager.DriverManagerException;
018:
019: /**
020: * UI Controller for updating the database
021: */
022: public class UIControllerForUpdates {
023: public static final String ORACLE_DBMS = "Oracle";
024: public static final String MYSQL_DBMS = "MySQL";
025: public static final String MSSQL_DBMS = "MS-SQL";
026:
027: private ConnectionInfo connectionInfo = null;
028: private DBUpdateEngine dbUpdateEngine = null;
029:
030: /**
031: * Connects to the database using the supplied connection info. DirverManager caches the connection. Start a DBQueryEngine
032: * @param connectionInfo
033: * @param masterPassword
034: */
035: public void setup(ConnectionInfo connectionInfo,
036: String masterPassword) throws DriverManagerException,
037: DBEngineException {
038: this .connectionInfo = connectionInfo;
039: Connection connection = DBBrowserDriverManager.getInstance()
040: .getConnection(connectionInfo, masterPassword);
041:
042: if (ORACLE_DBMS.equals(connectionInfo.getDBMSType())) {
043: try {
044: this .dbUpdateEngine = new OracleDBUpdateEngine(
045: connection.createStatement());
046: } catch (SQLException exc) {
047: throw new DBEngineException(exc.getMessage());
048: }
049: }
050:
051: if (MYSQL_DBMS.equals(connectionInfo.getDBMSType())) {
052: try {
053: this .dbUpdateEngine = new MySQLDBUpdateEngine(
054: connection.createStatement());
055: } catch (SQLException exc) {
056: throw new DBEngineException(exc.getMessage());
057: }
058: }
059:
060: if (MSSQL_DBMS.equals(connectionInfo.getDBMSType())) {
061: try {
062: this .dbUpdateEngine = new GenericDBUpdateEngine(
063: connection.createStatement());
064: } catch (SQLException exc) {
065: throw new DBEngineException(exc.getMessage());
066: }
067: }
068: }
069:
070: /**
071: * Set autocommit
072: * @param autoCommitFlag
073: * @throws DBEngineException
074: */
075: public void setAutoCommit(boolean autoCommitFlag)
076: throws DBEngineException {
077: try {
078: this .dbUpdateEngine.getStatement().getConnection()
079: .setAutoCommit(autoCommitFlag);
080: Log.getInstance().debugMessage(
081: "Autocommit " + autoCommitFlag,
082: this .getClass().getName());
083: } catch (SQLException exc) {
084: throw new DBEngineException(exc.getMessage());
085: }
086: }
087:
088: /**
089: * Commit
090: * @throws DBEngineException
091: */
092: public void commit() throws DBEngineException {
093: try {
094: this .dbUpdateEngine.getStatement().getConnection().commit();
095: Log.getInstance().debugMessage("Commit complete",
096: this .getClass().getName());
097: } catch (SQLException exc) {
098: throw new DBEngineException(exc.getMessage());
099: }
100: }
101:
102: /**
103: * Rollback
104: * @throws DBEngineException
105: */
106: public void rollback() throws DBEngineException {
107: try {
108: this .dbUpdateEngine.getStatement().getConnection()
109: .rollback();
110: Log.getInstance().debugMessage("Rollback complete",
111: this .getClass().getName());
112: } catch (SQLException exc) {
113: throw new DBEngineException(exc.getMessage());
114: }
115: }
116:
117: /**
118: * Return the connection info representing the connection
119: * @return
120: */
121: public ConnectionInfo getConnectionInfo() {
122: return this .connectionInfo;
123: }
124:
125: /**
126: * Update the view definition for the view
127: * @param view
128: * @throws DBEngineException
129: */
130: public void updateViewDefinition(View view)
131: throws DBEngineException {
132: this .dbUpdateEngine.updateViewDefinition(view);
133: }
134:
135: /**
136: * Add new column to the table
137: * @param schemaName
138: * @param tableName
139: * @param columnInfo
140: * @throws DBEngineException
141: */
142: public void addNewColumn(String schemaName, String tableName,
143: ColumnInfo columnInfo) throws DBEngineException {
144: this .dbUpdateEngine.addNewColumn(schemaName, tableName,
145: columnInfo);
146: }
147:
148: /**
149: * Drop the column from the table
150: * @param schemaName
151: * @param tableName
152: * @param columnInfo
153: * @throws DBEngineException
154: */
155: public void dropColumn(String schemaName, String tableName,
156: ColumnInfo columnInfo) throws DBEngineException {
157: this .dbUpdateEngine.dropColumn(schemaName, tableName,
158: columnInfo);
159: }
160:
161: /**
162: * Add a new row to the database
163: * @param schemaName
164: * @param tableName
165: * @param dbRow
166: * @throws DBEngineException
167: */
168: public void addNewRow(String schemaName, String tableName,
169: DBRow dbRow) throws DBEngineException {
170: this .dbUpdateEngine.addNewRow(schemaName, tableName, dbRow);
171: }
172:
173: /**
174: * Update the database using the data from the DBRow
175: * @param schemaName
176: * @param tableName
177: * @param dbRow
178: * @throws DBEngineException
179: */
180: public void update(String schemaName, String tableName, DBRow dbRow)
181: throws DBEngineException {
182: this .dbUpdateEngine.update(schemaName, tableName, dbRow);
183: }
184:
185: /**
186: * Delete the row
187: * @param schemaName
188: * @param tableName
189: * @param dbRow
190: * @throws DBEngineException
191: */
192: public void deleteRow(String schemaName, String tableName,
193: DBRow dbRow) throws DBEngineException {
194: this.dbUpdateEngine.deleteRow(schemaName, tableName, dbRow);
195: }
196: }
|