001: package org.dbbrowser.ui;
002:
003: import java.sql.Connection;
004: import java.sql.SQLException;
005: import java.util.List;
006: import org.dbbrowser.db.engine.exception.DBEngineException;
007: import org.dbbrowser.db.engine.model.DBTable;
008: import org.dbbrowser.db.engine.model.View;
009: import org.dbbrowser.db.engine.model.filter.Filter;
010: import org.dbbrowser.db.engine.queryengine.DBQueryEngine;
011: import org.dbbrowser.db.engine.queryengine.MsSQLDBQueryEngine;
012: import org.dbbrowser.db.engine.queryengine.MySQLDBQueryEngine;
013: import org.dbbrowser.db.engine.queryengine.Oracle9iDBQueryEngine;
014: import org.dbbrowser.drivermanager.ConnectionInfo;
015: import org.dbbrowser.drivermanager.DBBrowserDriverManager;
016: import org.dbbrowser.drivermanager.DriverManagerException;
017:
018: /**
019: * Controller for the UI layer. Delegates to other classes
020: * @author amangat
021: *
022: */
023: public class UIControllerForQueries {
024: public static final String ORACLE_DBMS = "Oracle";
025: public static final String MYSQL_DBMS = "MySQL";
026: public static final String MSSQL_DBMS = "MS-SQL";
027:
028: public static final long version = 1l;
029: private ConnectionInfo connectionInfo = null;
030: private DBQueryEngine dbQueryEngine = null;
031:
032: /**
033: * Connects to the database using the supplied connection info. DirverManager caches the connection. Start a DBQueryEngine
034: * @param connectionInfo
035: * @param masterPassword
036: * @author amangat
037: */
038: public void setup(ConnectionInfo connectionInfo,
039: String masterPassword) throws DriverManagerException,
040: DBEngineException {
041: this .connectionInfo = connectionInfo;
042: Connection connection = DBBrowserDriverManager.getInstance()
043: .getConnection(connectionInfo, masterPassword);
044:
045: if (ORACLE_DBMS.equals(connectionInfo.getDBMSType())) {
046: try {
047: this .dbQueryEngine = new Oracle9iDBQueryEngine(
048: connection.createStatement());
049: } catch (SQLException exc) {
050: throw new DBEngineException(exc.getMessage());
051: }
052: }
053:
054: if (MYSQL_DBMS.equals(connectionInfo.getDBMSType())) {
055: try {
056: this .dbQueryEngine = new MySQLDBQueryEngine(connection
057: .createStatement());
058: } catch (SQLException exc) {
059: throw new DBEngineException(exc.getMessage());
060: }
061: }
062:
063: if (MSSQL_DBMS.equals(connectionInfo.getDBMSType())) {
064: try {
065: this .dbQueryEngine = new MsSQLDBQueryEngine(connection
066: .createStatement());
067: } catch (SQLException exc) {
068: throw new DBEngineException(exc.getMessage());
069: }
070: }
071: }
072:
073: /**
074: * Return the connection info representing the connection
075: * @return
076: */
077: public ConnectionInfo getConnectionInfo() {
078: return this .connectionInfo;
079: }
080:
081: /**
082: * Returns a list of table spaces in the Database. It is empty if there are no table spaces for the database.
083: * @return - a list of Strings
084: * @throws DBEngineException
085: */
086: public List listSchemas() throws DBEngineException {
087: return this .dbQueryEngine.listSchemas();
088: }
089:
090: /**
091: * Lists the indexes in the schema
092: * @return
093: * @throws DBEngineException
094: */
095: public DBTable listIndexes() throws DBEngineException {
096: return this .dbQueryEngine.listIndexes();
097: }
098:
099: /**
100: * Lists the constraints in the schema
101: * @return
102: * @throws DBEngineException
103: */
104: public DBTable listConstraints() throws DBEngineException {
105: return this .dbQueryEngine.listConstraints();
106: }
107:
108: /**
109: * Returns a list of views accessible to the user. It is empty if there are no views for the user.
110: * @return - a list of View objects
111: * @throws DBEngineException
112: */
113: public List listViews() throws DBEngineException {
114: return this .dbQueryEngine.listViews();
115: }
116:
117: /**
118: * Returns the SQL used to create a view
119: * @return - a String
120: * @throws DBEngineException
121: */
122: public String getSQLForView(View view) throws DBEngineException {
123: return this .dbQueryEngine.getSQLForView(view);
124: }
125:
126: /**
127: * Returns a list of tables in the table space
128: * @param schemaName
129: * @return - a list of Strings
130: * @throws DBEngineException
131: */
132: public List listTablesInSchema(String schemaName)
133: throws DBEngineException {
134: return this .dbQueryEngine.listTablesInSchema(schemaName);
135: }
136:
137: /**
138: * Returns a list of columns in the table
139: * @param schemaName
140: * @param tableName
141: * @return - a list of ColumnInfo objects
142: * @throws DBEngineException
143: */
144: public List listColumnsInATable(String schemaName, String tableName)
145: throws DBEngineException {
146: return this .dbQueryEngine.listColumnsInATable(schemaName,
147: tableName);
148: }
149:
150: /**
151: * Get all the data in a table
152: * @param schemaName
153: * @param tableName
154: * @return
155: * @throws DBEngineException
156: */
157: public DBTable getAllDataInATable(String schemaName,
158: String tableName, Integer offset,
159: Integer numberOfRowsToReturn) throws DBEngineException {
160: return this .dbQueryEngine.getAllDataInATable(schemaName,
161: tableName, offset, numberOfRowsToReturn);
162: }
163:
164: /**
165: * Get all the data in a table
166: * @param schemaName
167: * @param tableName
168: * @return
169: * @throws DBEngineException
170: */
171: public DBTable getFilteredDataInATable(String schemaName,
172: String tableName, Filter filter) throws DBEngineException {
173: return this .dbQueryEngine.getFilteredDataInATable(schemaName,
174: tableName, filter);
175: }
176:
177: /**
178: * Lists the sequence in an Oracle database. For other databases, throws an UnsupportedOperationException
179: * @return - results as a dbtable
180: * @throws DBEngineException
181: */
182: public DBTable listSequences() throws DBEngineException {
183: //Only oracle has sequences
184: if (!ORACLE_DBMS.equals(connectionInfo.getDBMSType())) {
185: throw new UnsupportedOperationException(
186: "*** MySQL does not support sequences. Sequences cant be listed for MySQL - UIController.listSequences ***");
187: }
188:
189: //If it is oracle dbms, list sequences
190: Oracle9iDBQueryEngine oracle9iDBQueryEngine = (Oracle9iDBQueryEngine) this.dbQueryEngine;
191: return oracle9iDBQueryEngine.listSequences();
192: }
193: }
|