0001: /*
0002: * BrowserController.java
0003: *
0004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
0005: *
0006: * This program is free software; you can redistribute it and/or
0007: * modify it under the terms of the GNU General Public License
0008: * as published by the Free Software Foundation; either version 2
0009: * of the License, or any later version.
0010: *
0011: * This program is distributed in the hope that it will be useful,
0012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0014: * GNU General Public License for more details.
0015: *
0016: * You should have received a copy of the GNU General Public License
0017: * along with this program; if not, write to the Free Software
0018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
0019: *
0020: */
0021:
0022: package org.executequery.gui.browser;
0023:
0024: import java.sql.ResultSet;
0025: import java.sql.SQLException;
0026: import java.util.Hashtable;
0027: import java.util.List;
0028: import java.util.Map;
0029: import java.util.StringTokenizer;
0030: import java.util.Vector;
0031: import javax.swing.JOptionPane;
0032: import javax.swing.JPanel;
0033: import org.executequery.GUIUtilities;
0034: import org.executequery.SystemUtilities;
0035: import org.executequery.databasemediators.DatabaseConnection;
0036: import org.executequery.databasemediators.DatabaseProcedure;
0037: import org.executequery.databasemediators.MetaDataValues;
0038: import org.executequery.databasemediators.QuerySender;
0039: import org.executequery.databasemediators.SqlStatementResult;
0040: import org.underworldlabs.jdbc.DataSourceException;
0041: import org.executequery.gui.forms.FormObjectView;
0042: import org.executequery.util.Log;
0043: import org.underworldlabs.util.MiscUtils;
0044: import org.underworldlabs.swing.util.SwingWorker;
0045:
0046: /* ----------------------------------------------------------
0047: * CVS NOTE: Changes to the CVS repository prior to the
0048: * release of version 3.0.0beta1 has meant a
0049: * resetting of CVS revision numbers.
0050: * ----------------------------------------------------------
0051: */
0052:
0053: /**
0054: * Performs SQL execution tasks from browser components.
0055: *
0056: * @author Takis Diakoumis
0057: * @version $Revision: 1.13 $
0058: * @date $Date: 2006/09/05 12:01:24 $
0059: */
0060: public class BrowserController {
0061:
0062: public static final int UPDATE_CANCELLED = 99;
0063:
0064: /** the meta data retrieval object */
0065: private MetaDataValues metaData;
0066:
0067: /** query sender object */
0068: private QuerySender querySender;
0069:
0070: /** the connections tree panel */
0071: private ConnectionsTreePanel treePanel;
0072:
0073: /** the databse viewer panel */
0074: private BrowserViewPanel viewPanel;
0075:
0076: /** the swing worker thread */
0077: private SwingWorker worker;
0078:
0079: /** Creates a new instance of BorwserQueryExecuter */
0080: public BrowserController(ConnectionsTreePanel treePanel) {
0081: this .treePanel = treePanel;
0082: }
0083:
0084: /**
0085: * Connects/disconnects the specified connection.
0086: *
0087: * @param dc - the conn to connect/disconnect
0088: */
0089: protected void connect(DatabaseConnection dc, boolean reselectRoot) {
0090: try {
0091: if (dc.isConnected()) {
0092: treePanel.setRootSelectOnDisconnect(reselectRoot);
0093: SystemUtilities.disconnect(dc);
0094: } else {
0095: SystemUtilities.connect(dc);
0096: }
0097: } catch (DataSourceException e) {
0098: StringBuffer sb = new StringBuffer();
0099: sb
0100: .append("The connection to the database could not be established.");
0101: sb
0102: .append("\nPlease ensure all required fields have been entered ");
0103: sb
0104: .append("correctly and try again.\n\nThe system returned:\n");
0105: sb.append(e.getExtendedMessage());
0106: GUIUtilities.displayExceptionErrorDialog(sb.toString(), e);
0107: } finally {
0108: // reset the disconnect selection
0109: treePanel.setRootSelectOnDisconnect(false);
0110: }
0111: }
0112:
0113: /**
0114: * Performs the drop database object action.
0115: */
0116: protected void dropSelectedObject() {
0117: try {
0118: // make sure we are not on a type parent object
0119: if (treePanel.isTypeParentSelected()) {
0120: return;
0121: }
0122:
0123: DatabaseObject object = treePanel
0124: .getSelectedDatabaseObject();
0125: if (object == null) {
0126: return;
0127: }
0128:
0129: // display confirmation dialog
0130: int yesNo = GUIUtilities
0131: .displayConfirmDialog("Are you sure you want to drop "
0132: + object + "?");
0133: if (yesNo == JOptionPane.NO_OPTION) {
0134: return;
0135: }
0136:
0137: int result = dropObject(getDatabaseConnection(), object);
0138: if (result >= 0) {
0139: treePanel.removeSelectedNode();
0140: }
0141: } catch (SQLException e) {
0142: StringBuffer sb = new StringBuffer();
0143: sb
0144: .append(
0145: "An error occurred removing the selected object.")
0146: .append("\n\nThe system returned:\n").append(
0147: MiscUtils.formatSQLError(e));
0148: GUIUtilities.displayExceptionErrorDialog(sb.toString(), e);
0149: }
0150: }
0151:
0152: /**
0153: * Ensures we have a browser panel and that it is visible.
0154: */
0155: protected void checkBrowserPanel() {
0156: // check we have the browser view panel
0157: if (viewPanel == null) {
0158: viewPanel = new BrowserViewPanel(this );
0159: }
0160:
0161: // check the panel is in the pane
0162: JPanel _viewPanel = GUIUtilities
0163: .getCentralPane(BrowserViewPanel.TITLE);
0164: if (_viewPanel == null) {
0165: GUIUtilities.addCentralPane(BrowserViewPanel.TITLE,
0166: BrowserViewPanel.FRAME_ICON, viewPanel,
0167: BrowserViewPanel.TITLE, true);
0168: } else {
0169: GUIUtilities.setSelectedCentralPane(BrowserViewPanel.TITLE);
0170: }
0171: }
0172:
0173: /**
0174: * Saves the connection data to file.
0175: */
0176: public boolean saveConnections() {
0177: return viewPanel.saveConnections();
0178: }
0179:
0180: /**
0181: * Informs the view panel of a pending change.
0182: */
0183: protected void selectionChanging() {
0184: if (viewPanel != null) {
0185: viewPanel.selectionChanging();
0186: }
0187: }
0188:
0189: /**
0190: * Sets the selected connection tree node to the
0191: * specified database connection.
0192: *
0193: * @param dc - the database connection to select
0194: */
0195: protected void setSelectedConnection(DatabaseConnection dc) {
0196: treePanel.setSelectedConnection(dc, true);
0197: }
0198:
0199: /**
0200: * Reloads the database properties meta data table panel.
0201: */
0202: protected void updateDatabaseProperties() {
0203: FormObjectView view = viewPanel
0204: .getFormObjectView(HostPanel.NAME);
0205: if (view != null) {
0206: HostPanel panel = (HostPanel) view;
0207: panel.updateDatabaseProperties();
0208: }
0209: }
0210:
0211: /**
0212: * Adds a new connection.
0213: */
0214: protected void addNewConnection() {
0215: treePanel.newConnection();
0216: }
0217:
0218: /**
0219: * Indicates that a node name has changed and fires a call
0220: * to repaint the tree display.
0221: */
0222: protected void nodeNameValueChanged(ConnectionObject metaObject) {
0223: treePanel.nodeNameValueChanged(metaObject);
0224: }
0225:
0226: /**
0227: * Indicates a change in the tree selection value.<br>
0228: * This will determine and builds the object view panel to be
0229: * displayed based on the specified host node connection object
0230: * and the selected node as specified.
0231: *
0232: * @param the connection host parent object
0233: * @param the selected node
0234: */
0235: public synchronized void valueChanged(
0236: final ConnectionObject parent, final BrowserTreeNode node,
0237: final boolean reload) {
0238: worker = new SwingWorker() {
0239: public Object construct() {
0240: try {
0241: treePanel.setInProcess(true);
0242: return buildPanelView(parent, node, reload);
0243: } finally {
0244: treePanel.setInProcess(false);
0245: }
0246: }
0247:
0248: public void finished() {
0249: try {
0250: GUIUtilities.showWaitCursor();
0251: FormObjectView panel = (FormObjectView) get();
0252: if (panel != null) {
0253: viewPanel.setView(panel);
0254: checkBrowserPanel();
0255: }
0256: } finally {
0257: GUIUtilities.showNormalCursor();
0258: }
0259: }
0260: };
0261: worker.start();
0262: }
0263:
0264: /**
0265: * Determines and builds the object view panel to be
0266: * displayed based on the specified host node connection object
0267: * and the selected node as specified.
0268: *
0269: * @param the connection host parent object
0270: * @param the selected node
0271: */
0272: private FormObjectView buildPanelView(ConnectionObject parent,
0273: BrowserTreeNode node, boolean reload) {
0274:
0275: // if the parent is null - bail
0276: if (parent == null) {
0277: return null;
0278: }
0279:
0280: try {
0281: DatabaseConnection dc = parent.getDatabaseConnection();
0282: DatabaseObject databaseObject = node
0283: .getDatabaseUserObject();
0284:
0285: String catalog = parent.isCatalogsInUse() ? databaseObject
0286: .getCatalogName() : null;
0287: String schema = databaseObject.getSchemaName();
0288: String name = databaseObject.getName();
0289:
0290: // determine the type of selection
0291: int type = databaseObject.getType();
0292:
0293: //Log.debug("Node type selected: " + type);
0294:
0295: switch (type) {
0296:
0297: case BrowserConstants.HOST_NODE:
0298:
0299: HostPanel hostPanel = null;
0300: if (!viewPanel.containsPanel(HostPanel.NAME)) {
0301: hostPanel = new HostPanel(this );
0302: viewPanel.addToLayout(hostPanel);
0303: } else {
0304: hostPanel = (HostPanel) viewPanel
0305: .getFormObjectView(HostPanel.NAME);
0306: }
0307:
0308: hostPanel.setValues(parent);
0309: return hostPanel;
0310:
0311: // catalog node:
0312: // this will display the schema table list
0313: case BrowserConstants.CATALOG_NODE:
0314:
0315: CatalogPanel catalogPanel = null;
0316: if (!viewPanel.containsPanel(CatalogPanel.NAME)) {
0317: catalogPanel = new CatalogPanel(this );
0318: viewPanel.addToLayout(catalogPanel);
0319: } else {
0320: catalogPanel = (CatalogPanel) viewPanel
0321: .getFormObjectView(CatalogPanel.NAME);
0322: }
0323:
0324: catalogPanel.setValues(name, getCatalogSchemas(dc));
0325: return catalogPanel;
0326:
0327: case BrowserConstants.SCHEMA_NODE:
0328:
0329: SchemaPanel schemaPanel = null;
0330: if (!viewPanel.containsPanel(SchemaPanel.NAME)) {
0331: schemaPanel = new SchemaPanel(this );
0332: viewPanel.addToLayout(schemaPanel);
0333: } else {
0334: schemaPanel = (SchemaPanel) viewPanel
0335: .getFormObjectView(SchemaPanel.NAME);
0336: }
0337:
0338: schemaPanel.selected(parent, databaseObject);
0339: return schemaPanel;
0340:
0341: }
0342:
0343: // if not a leaf node
0344: if (!node.isLeaf()) {
0345:
0346: // if its a system object - need a meta table display
0347: if (databaseObject.isSystemObject()) {
0348:
0349: // use the same meta panel
0350:
0351: MetaKeyPanel metaKeyPanel = null;
0352: if (!viewPanel.containsPanel(MetaKeyPanel.NAME)) {
0353: metaKeyPanel = new MetaKeyPanel(this );
0354: viewPanel.addToLayout(metaKeyPanel);
0355: } else {
0356: metaKeyPanel = (MetaKeyPanel) viewPanel
0357: .getFormObjectView(MetaKeyPanel.NAME);
0358: }
0359:
0360: String[] values = null;
0361: switch (type) {
0362: case BrowserConstants.SYSTEM_STRING_FUNCTIONS_NODE:
0363: values = getSystemFunctions(dc,
0364: MetaDataValues.STRING_FUNCTIONS);
0365: break;
0366:
0367: case BrowserConstants.SYSTEM_NUMERIC_FUNCTIONS_NODE:
0368: values = getSystemFunctions(dc,
0369: MetaDataValues.NUMERIC_FUNCTIONS);
0370: break;
0371:
0372: case BrowserConstants.SYSTEM_DATE_TIME_FUNCTIONS_NODE:
0373: values = getSystemFunctions(dc,
0374: MetaDataValues.TIME_DATE_FUNCTIONS);
0375: break;
0376: }
0377: metaKeyPanel.setValues(name, values);
0378: return metaKeyPanel;
0379: }
0380:
0381: // if its a meta parent node
0382: else if (node.isTypeParent()) {
0383: MetaKeyPanel metaKeyPanel = null;
0384: if (!viewPanel.containsPanel(MetaKeyPanel.NAME)) {
0385: metaKeyPanel = new MetaKeyPanel(this );
0386: viewPanel.addToLayout(metaKeyPanel);
0387: } else {
0388: metaKeyPanel = (MetaKeyPanel) viewPanel
0389: .getFormObjectView(MetaKeyPanel.NAME);
0390: }
0391:
0392: // TODO: don't think this is using a cache!!!
0393:
0394: // if its the system function node - just add the functions types
0395: if (type == BrowserConstants.SYSTEM_FUNCTION_NODE) {
0396: String[] values = { "String Functions",
0397: "Numeric Functions",
0398: "Date/Time Functions" };
0399: metaKeyPanel.setValues(name, values);
0400: return metaKeyPanel;
0401: }
0402:
0403: if (!metaKeyPanel.hasObject(databaseObject)) {
0404: String[] values = getTables(dc, catalog,
0405: schema, name);
0406: // if we have no values here - see if proc or function
0407: if (values == null || values.length == 0) {
0408: values = checkProcedureTerm(dc,
0409: databaseObject);
0410: }
0411: metaKeyPanel.setValues(name, values);
0412: } else {
0413: metaKeyPanel.setValues(name);
0414: }
0415:
0416: return metaKeyPanel;
0417:
0418: }
0419: }
0420:
0421: // if we have nothing here - must be a specific type
0422:
0423: switch (type) {
0424: case BrowserConstants.FUNCTIONS_NODE:
0425: case BrowserConstants.PROCEDURE_NODE:
0426: case BrowserConstants.SYSTEM_STRING_FUNCTIONS_NODE:
0427: case BrowserConstants.SYSTEM_NUMERIC_FUNCTIONS_NODE:
0428: case BrowserConstants.SYSTEM_DATE_TIME_FUNCTIONS_NODE:
0429:
0430: BrowserProcedurePanel procsPanel = null;
0431: if (!viewPanel
0432: .containsPanel(BrowserProcedurePanel.NAME)) {
0433: procsPanel = new BrowserProcedurePanel(this );
0434: viewPanel.addToLayout(procsPanel);
0435: } else {
0436: procsPanel = (BrowserProcedurePanel) viewPanel
0437: .getFormObjectView(BrowserProcedurePanel.NAME);
0438: }
0439:
0440: // set the catalog and schema values to null if
0441: // its a system function type
0442: if (type == BrowserConstants.SYSTEM_STRING_FUNCTIONS_NODE
0443: || type == BrowserConstants.SYSTEM_NUMERIC_FUNCTIONS_NODE
0444: || type == BrowserConstants.SYSTEM_DATE_TIME_FUNCTIONS_NODE) {
0445: catalog = null;
0446: schema = null;
0447: }
0448:
0449: // maybe force a reload here
0450: if (reload) {
0451: procsPanel.removeObject(databaseObject);
0452: }
0453:
0454: if (!procsPanel.hasObject(databaseObject)) {
0455: procsPanel.setValues(databaseObject,
0456: getProcedureColumns(dc, catalog, schema,
0457: name));
0458: } else {
0459: procsPanel.setValues(databaseObject);
0460: }
0461: return procsPanel;
0462:
0463: case BrowserConstants.TABLE_NODE:
0464:
0465: if (databaseObject.isDefaultCatalog()) {
0466: BrowserTableEditingPanel editingPanel = viewPanel
0467: .getEditingPanel();
0468: editingPanel.selectionChanged(databaseObject,
0469: reload);
0470: return editingPanel;
0471: }
0472: break;
0473:
0474: case BrowserConstants.COLUMN_NODE:
0475: TableColumnPanel columnPanel = null;
0476: if (!viewPanel.containsPanel(TableColumnPanel.NAME)) {
0477: columnPanel = new TableColumnPanel(this );
0478: viewPanel.addToLayout(columnPanel);
0479: } else {
0480: columnPanel = (TableColumnPanel) viewPanel
0481: .getFormObjectView(TableColumnPanel.NAME);
0482: }
0483:
0484: Map columnMap = null;
0485: if (reload || !columnPanel.hasObject(databaseObject)) {
0486: String parentName = databaseObject.getParentName();
0487: columnMap = getColumnProperties(dc, schema,
0488: parentName, name);
0489: }
0490: columnPanel
0491: .setValues(databaseObject, columnMap, reload);
0492: return columnPanel;
0493:
0494: }
0495:
0496: ObjectDefinitionPanel objectDefnPanel = null;
0497: if (reload
0498: || !viewPanel
0499: .containsPanel(ObjectDefinitionPanel.NAME)) {
0500: objectDefnPanel = new ObjectDefinitionPanel(this );
0501: viewPanel.addToLayout(objectDefnPanel);
0502: } else {
0503: objectDefnPanel = (ObjectDefinitionPanel) viewPanel
0504: .getFormObjectView(ObjectDefinitionPanel.NAME);
0505: }
0506: objectDefnPanel.changeTable(databaseObject, reload);
0507: return objectDefnPanel;
0508:
0509: } catch (Exception e) {
0510: handleException(e);
0511: return null;
0512: }
0513: }
0514:
0515: /**
0516: * Selects the node that matches the specified prefix forward
0517: * from the currently selected node.
0518: *
0519: * @param prefix - the prefix of the node to select
0520: */
0521: protected void selectBrowserNode(String prefix) {
0522: treePanel.selectBrowserNode(prefix);
0523: }
0524:
0525: /**
0526: * Displays the root main view panel.
0527: */
0528: protected void displayRootPanel() {
0529: checkBrowserPanel();
0530: viewPanel.displayRootPanel();
0531: }
0532:
0533: /**
0534: * Applies the table alteration changes.
0535: */
0536: protected void applyTableChange(boolean valueChange) {
0537:
0538: BrowserTableEditingPanel editingPanel = viewPanel
0539: .getEditingPanel();
0540:
0541: // check we actually have something to apply
0542: if (!editingPanel.hasSQLText()) {
0543: return;
0544: }
0545:
0546: // retrieve the browser node
0547: BrowserTreeNode node = null;
0548: if (valueChange) {
0549: // if we are selecting a new node, get the previous selection
0550: node = treePanel.getOldBrowserNodeSelection();
0551: } else {
0552: // otherwise get the current selection
0553: node = treePanel.getSelectedBrowserNode();
0554: }
0555:
0556: try {
0557: treePanel.removeTreeListener();
0558:
0559: // if specified, ask the user again
0560: if (valueChange) {
0561: int yesNo = GUIUtilities
0562: .displayConfirmCancelDialog("Do you wish to apply your changes?");
0563: if (yesNo == JOptionPane.NO_OPTION) {
0564: node = treePanel.getSelectedBrowserNode();
0565: editingPanel.selectionChanged(node
0566: .getDatabaseUserObject(), true);
0567: editingPanel.resetSQLText();
0568: return;
0569: } else if (yesNo == JOptionPane.CANCEL_OPTION) {
0570: treePanel.setNodeSelected(node);
0571: return;
0572: }
0573: }
0574:
0575: // apply the changes to the database
0576: if (querySender == null) {
0577: querySender = new QuerySender();
0578: }
0579: querySender.setDatabaseConnection(getDatabaseConnection());
0580:
0581: SqlStatementResult result = null;
0582: StringTokenizer st = new StringTokenizer(editingPanel
0583: .getSQLText().trim(), ";\n");
0584:
0585: try {
0586: while (st.hasMoreTokens()) {
0587: result = querySender.updateRecords(st.nextToken());
0588: if (result.getUpdateCount() < 0) {
0589: editingPanel.setSQLText();
0590: SQLException e = result.getSqlException();
0591: if (e != null) {
0592: StringBuffer sb = new StringBuffer();
0593: sb
0594: .append(
0595: "An error occurred applying the specified changes.")
0596: .append(
0597: "\n\nThe system returned:\n")
0598: .append(MiscUtils.formatSQLError(e));
0599: GUIUtilities.displayExceptionErrorDialog(sb
0600: .toString(), e);
0601: } else {
0602: GUIUtilities.displayErrorMessage(result
0603: .getErrorMessage());
0604: }
0605: treePanel.setNodeSelected(node);
0606: return;
0607: }
0608: }
0609: } catch (SQLException e) {
0610: StringBuffer sb = new StringBuffer();
0611: sb
0612: .append(
0613: "An error occurred applying the specified changes.")
0614: .append("\n\nThe system returned:\n").append(
0615: MiscUtils.formatSQLError(e));
0616: GUIUtilities.displayExceptionErrorDialog(sb.toString(),
0617: e);
0618: treePanel.setNodeSelected(node);
0619: return;
0620: }
0621:
0622: // reset the current panel
0623: editingPanel.selectionChanged(node.getDatabaseUserObject(),
0624: true);
0625: editingPanel.resetSQLText();
0626: treePanel.setNodeSelected(node);
0627: } finally {
0628: treePanel.addTreeListener();
0629: }
0630: }
0631:
0632: /**
0633: * Returns whether a table alteration has occurred and
0634: * is actionable.
0635: *
0636: * @return true | false
0637: */
0638: protected boolean hasAlterTable() {
0639: if (viewPanel == null) {
0640: return false;
0641: }
0642: return viewPanel.getEditingPanel().hasSQLText();
0643: }
0644:
0645: /**
0646: * Checks the procedure term against a function or procedure node
0647: * when the returned results from getTables(...) is null or empty.
0648: *
0649: * @param dc - the database connection objeect
0650: * @param object - the meta object
0651: */
0652: protected String[] checkProcedureTerm(DatabaseConnection dc,
0653: DatabaseObject object) {
0654: int type = object.getType();
0655: if (type == BrowserConstants.FUNCTIONS_NODE
0656: || type == BrowserConstants.PROCEDURE_NODE) {
0657: // check the procedure term
0658: String procedureTerm = getProcedureTerm(dc);
0659: if (procedureTerm != null) {
0660: String catalog = object.getCatalogName();
0661: String schema = object.getSchemaName();
0662: String metaKey = object.getMetaDataKey();
0663: if (procedureTerm.toUpperCase().equals(metaKey)) {
0664: return getProcedureNames(dc, catalog, schema, null);
0665: }
0666: }
0667: }
0668: return new String[0];
0669: }
0670:
0671: // --------------------------------------------
0672: // Meta data propagation methods
0673: // --------------------------------------------
0674:
0675: /**
0676: * Generic exception handler.
0677: */
0678: private void handleException(Throwable e) {
0679: if (Log.isDebugEnabled()) {
0680: Log.debug("Error retrieving data.", e);
0681: }
0682:
0683: boolean isDataSourceException = (e instanceof DataSourceException);
0684: GUIUtilities
0685: .displayExceptionErrorDialog(
0686: "Error retrieving the selected database "
0687: + "object.\n\nThe system returned:\n"
0688: + (isDataSourceException ? ((DataSourceException) e)
0689: .getExtendedMessage()
0690: : e.getMessage()), e);
0691:
0692: if (isDataSourceException) {
0693: if (((DataSourceException) e).wasConnectionClosed()) {
0694: connect(treePanel.getSelectedDatabaseConnection(),
0695: false);
0696: }
0697: }
0698:
0699: }
0700:
0701: /**
0702: * Propagates the call to the meta data object.
0703: */
0704: protected String[] getSystemFunctions(DatabaseConnection dc,
0705: int type) {
0706: try {
0707: checkMetaDataObject();
0708: metaData.setDatabaseConnection(dc);
0709: return metaData.getSystemFunctions(type);
0710: } catch (DataSourceException e) {
0711: handleException(e);
0712: return null;
0713: }
0714: }
0715:
0716: /**
0717: * Propagates the call to the meta data object.
0718: */
0719: protected String[] getProcedureNames(DatabaseConnection dc,
0720: String catalog, String schema, String name) {
0721: try {
0722: checkMetaDataObject();
0723: metaData.setDatabaseConnection(dc);
0724: return metaData.getProcedureNames(catalog, schema, name);
0725: } catch (DataSourceException e) {
0726: handleException(e);
0727: return null;
0728: }
0729: }
0730:
0731: /**
0732: * Propagates the call to the meta data object.
0733: */
0734: protected String getProcedureTerm(DatabaseConnection dc) {
0735: try {
0736: checkMetaDataObject();
0737: metaData.setDatabaseConnection(dc);
0738: return metaData.getProcedureTerm();
0739: } catch (DataSourceException e) {
0740: handleException(e);
0741: return null;
0742: }
0743: }
0744:
0745: /**
0746: * Propagates the call to the meta data object.
0747: */
0748: protected String[] getTableTypes(DatabaseConnection dc) {
0749: try {
0750: checkMetaDataObject();
0751: metaData.setDatabaseConnection(dc);
0752: return metaData.getTableTypes();
0753: } catch (DataSourceException e) {
0754: handleException(e);
0755: return new String[0];
0756: }
0757: }
0758:
0759: /**
0760: * Propagates the call to the meta data object.
0761: */
0762: protected String[] getTables(DatabaseConnection dc, String catalog,
0763: String schema, String metaName) {
0764: try {
0765: checkMetaDataObject();
0766: metaData.setDatabaseConnection(dc);
0767: return metaData.getTables(catalog, schema, metaName);
0768: } catch (DataSourceException e) {
0769: handleException(e);
0770: return new String[0];
0771: }
0772: }
0773:
0774: /**
0775: * Propagates the call to the meta data object.
0776: */
0777: protected DatabaseObject[] getTables(DatabaseConnection dc,
0778: String catalog, String schema, String[] types) {
0779: checkMetaDataObject();
0780: metaData.setDatabaseConnection(dc);
0781: try {
0782: return metaData.getTables(catalog, schema, types);
0783: } catch (DataSourceException e) {
0784: handleException(e);
0785: return new DatabaseObject[0];
0786: }
0787: }
0788:
0789: protected ResultSet getDataTypesResultSet() {
0790: try {
0791: checkMetaDataObject();
0792: metaData.setDatabaseConnection(getDatabaseConnection());
0793: return metaData.getDataTypesResultSet();
0794: } catch (DataSourceException e) {
0795: handleException(e);
0796: return null;
0797: }
0798: }
0799:
0800: protected String[] getDatabaseKeywords() {
0801: try {
0802: checkMetaDataObject();
0803: metaData.setDatabaseConnection(getDatabaseConnection());
0804: return metaData.getDatabaseKeywords();
0805: } catch (DataSourceException e) {
0806: handleException(e);
0807: return new String[0];
0808: }
0809: }
0810:
0811: protected Hashtable getDatabaseProperties() {
0812: try {
0813: checkMetaDataObject();
0814: DatabaseConnection dc = getDatabaseConnection();
0815: if (dc != null) {
0816: metaData.setDatabaseConnection(getDatabaseConnection());
0817: return metaData.getDatabaseProperties();
0818: }
0819: } catch (DataSourceException e) {
0820: handleException(e);
0821: }
0822: return new Hashtable(0);
0823: }
0824:
0825: /**
0826: * Propagates the call to the meta data object.
0827: */
0828: protected String[] getColumnNames(DatabaseConnection dc,
0829: String table, String schema) {
0830: try {
0831: checkMetaDataObject();
0832: metaData.setDatabaseConnection(dc);
0833: return metaData.getColumnNames(table, schema);
0834: } catch (DataSourceException e) {
0835: handleException(e);
0836: return new String[0];
0837: }
0838: }
0839:
0840: /**
0841: * Propagates the call to the meta data object.
0842: */
0843: protected String getSchemaName(DatabaseConnection dc) {
0844: checkMetaDataObject();
0845: metaData.setDatabaseConnection(dc);
0846: return metaData.getSchemaName();
0847: }
0848:
0849: /**
0850: * Propagates the call to the meta data object.
0851: */
0852: protected List getCatalogSchemas(DatabaseConnection dc) {
0853: try {
0854: checkMetaDataObject();
0855: metaData.setDatabaseConnection(dc);
0856: return metaData.getHostedCatalogSchemas();
0857: } catch (DataSourceException e) {
0858: handleException(e);
0859: return null;
0860: }
0861: }
0862:
0863: /**
0864: * Propagates the call to the meta data object.
0865: */
0866: protected String getDataSourceName(DatabaseConnection dc) {
0867: checkMetaDataObject();
0868: metaData.setDatabaseConnection(dc);
0869: return metaData.getDataSourceName();
0870: }
0871:
0872: /**
0873: * Propagates the call to the meta data object.
0874: */
0875: protected Vector<String> getHostedCatalogs(DatabaseConnection dc) {
0876: try {
0877: checkMetaDataObject();
0878: metaData.setDatabaseConnection(dc);
0879: return metaData.getHostedCatalogsVector();
0880: } catch (DataSourceException e) {
0881: handleException(e);
0882: return new Vector(0);
0883: }
0884: }
0885:
0886: /**
0887: * Propagates the call to the meta data object.
0888: */
0889: protected Vector<String> getHostedSchemas(DatabaseConnection dc) {
0890: try {
0891: checkMetaDataObject();
0892: metaData.setDatabaseConnection(dc);
0893: return metaData.getHostedSchemasVector();
0894: } catch (DataSourceException e) {
0895: handleException(e);
0896: return new Vector(0);
0897: }
0898: }
0899:
0900: /**
0901: * Propagates the call to the meta data object.
0902: */
0903: protected Vector<String> getColumnNamesVector(
0904: DatabaseConnection dc, String table, String schema) {
0905: try {
0906: checkMetaDataObject();
0907: metaData.setDatabaseConnection(dc);
0908: return metaData.getColumnNamesVector(table, schema);
0909: } catch (DataSourceException e) {
0910: handleException(e);
0911: return new Vector(0);
0912: }
0913: }
0914:
0915: /**
0916: * Propagates the call to the meta data object.
0917: */
0918: protected Vector<String> getColumnNamesVector(String table,
0919: String schema) {
0920: return getColumnNamesVector(getDatabaseConnection(), table,
0921: schema);
0922: }
0923:
0924: /**
0925: * Propagates the call to the meta data object.
0926: */
0927: protected Vector<String> getHostedSchemas() {
0928: return getHostedSchemas(getDatabaseConnection());
0929: }
0930:
0931: /**
0932: * Propagates the call to the meta data object.
0933: */
0934: protected Vector<String> getTables(String schema) {
0935: return getTables(getDatabaseConnection(), schema);
0936: }
0937:
0938: /**
0939: * Propagates the call to the meta data object.
0940: */
0941: protected Vector<String> getTables(DatabaseConnection dc,
0942: String schema) {
0943: try {
0944: checkMetaDataObject();
0945: metaData.setDatabaseConnection(dc);
0946: return metaData.getSchemaTables(schema);
0947: } catch (DataSourceException e) {
0948: handleException(e);
0949: return new Vector(0);
0950: }
0951: }
0952:
0953: /**
0954: * Propagates the call to the meta data object.
0955: */
0956: protected String getCatalogName(DatabaseConnection dc) {
0957: checkMetaDataObject();
0958: metaData.setDatabaseConnection(dc);
0959: return metaData.getCatalogName();
0960: }
0961:
0962: /**
0963: * Propagates the call to the meta data object.
0964: */
0965: protected String[] getDataTypesArray() {
0966: try {
0967: checkMetaDataObject();
0968: metaData.setDatabaseConnection(getDatabaseConnection());
0969: return metaData.getDataTypesArray();
0970: } catch (DataSourceException e) {
0971: handleException(e);
0972: return new String[0];
0973: }
0974: }
0975:
0976: /**
0977: * Propagates the call to the meta data object.
0978: */
0979: protected Map getColumnProperties(DatabaseConnection dc,
0980: String schema, String table, String column) {
0981: try {
0982: checkMetaDataObject();
0983: metaData.setDatabaseConnection(dc);
0984: return metaData.getColumnProperties(schema, table, column);
0985: } catch (DataSourceException e) {
0986: handleException(e);
0987: return new Hashtable(0);
0988: }
0989: }
0990:
0991: /**
0992: * Propagates the call to the meta data object.
0993: */
0994: protected DatabaseProcedure getProcedureColumns(
0995: DatabaseConnection dc, String catalog, String schema,
0996: String name) {
0997: try {
0998: checkMetaDataObject();
0999: metaData.setDatabaseConnection(dc);
1000: return metaData.getProcedureColumns(catalog, schema, name);
1001: } catch (DataSourceException e) {
1002: handleException(e);
1003: return null;
1004: }
1005: }
1006:
1007: /**
1008: * Propagates the call to the meta data object.
1009: */
1010: protected String[] getExportedKeyTables(String catalog,
1011: String schema, String name) {
1012: try {
1013: checkMetaDataObject();
1014: metaData.setDatabaseConnection(getDatabaseConnection());
1015: return metaData.getExportedKeyTables(
1016: isUsingCatalogs() ? catalog : null, schema, name);
1017: } catch (DataSourceException e) {
1018: handleException(e);
1019: return new String[0];
1020: }
1021: }
1022:
1023: /**
1024: * Propagates the call to the meta data object.
1025: */
1026: protected String[] getImportedKeyTables(String catalog,
1027: String schema, String name) {
1028: try {
1029: checkMetaDataObject();
1030: metaData.setDatabaseConnection(getDatabaseConnection());
1031: return metaData.getImportedKeyTables(
1032: isUsingCatalogs() ? catalog : null, schema, name);
1033: } catch (DataSourceException e) {
1034: handleException(e);
1035: return new String[0];
1036: }
1037: }
1038:
1039: /**
1040: * Propagates the call to the meta object.
1041: */
1042: protected ResultSet getTableMetaData(String catalog, String schema,
1043: String name) {
1044: try {
1045: checkMetaDataObject();
1046: metaData.setDatabaseConnection(getDatabaseConnection());
1047: return metaData.getTableMetaData(
1048: isUsingCatalogs() ? catalog : null, schema, name);
1049: } catch (DataSourceException e) {
1050: handleException(e);
1051: return null;
1052: }
1053: }
1054:
1055: /**
1056: * Propagates the call to the meta data object.
1057: */
1058: protected Vector getTableIndexes(String catalog, String schema,
1059: String name) {
1060: try {
1061: checkMetaDataObject();
1062: metaData.setDatabaseConnection(getDatabaseConnection());
1063: return metaData.getTableIndexes(isUsingCatalogs() ? catalog
1064: : null, schema, name);
1065: } catch (DataSourceException e) {
1066: handleException(e);
1067: return new Vector(0);
1068: }
1069: }
1070:
1071: /**
1072: * Propagates the call to the meta data object.
1073: */
1074: protected TablePrivilege[] getPrivileges(String catalog,
1075: String schema, String name) {
1076: try {
1077: checkMetaDataObject();
1078: metaData.setDatabaseConnection(getDatabaseConnection());
1079: return metaData.getPrivileges(isUsingCatalogs() ? catalog
1080: : null, schema, name);
1081: } catch (DataSourceException e) {
1082: handleException(e);
1083: return new TablePrivilege[0];
1084: }
1085: }
1086:
1087: protected ColumnData[] getColumnData(String catalog, String schema,
1088: String name) {
1089: try {
1090: checkMetaDataObject();
1091: metaData.setDatabaseConnection(getDatabaseConnection());
1092: return metaData.getColumnMetaData(
1093: isUsingCatalogs() ? catalog : null, schema, name);
1094: } catch (DataSourceException e) {
1095: handleException(e);
1096: return new ColumnData[0];
1097: }
1098: }
1099:
1100: /**
1101: * Recycles the specified connection object for the browser.
1102: *
1103: * @param dc - the connection to be recycled
1104: */
1105: protected void recycleConnection(DatabaseConnection dc) {
1106: try {
1107: checkMetaDataObject();
1108: metaData.recycleConnection(dc);
1109: } catch (DataSourceException e) {
1110: handleException(e);
1111: }
1112: }
1113:
1114: /**
1115: * Ensures the meta data object is initialised.
1116: */
1117: private void checkMetaDataObject() {
1118: if (metaData == null) {
1119: metaData = new MetaDataValues(true);
1120: }
1121: }
1122:
1123: /**
1124: * Returns true if the currently selected connection is using
1125: * catalogs in meta data retrieval.
1126: *
1127: * @return true | false
1128: */
1129: protected boolean isUsingCatalogs() {
1130: return treePanel.getSelectedMetaObject().isCatalogsInUse();
1131: }
1132:
1133: /**
1134: * Retrieves the selected database connection properties object.
1135: */
1136: protected DatabaseConnection getDatabaseConnection() {
1137: ConnectionObject object = treePanel.getSelectedMetaObject();
1138: if (object != null) {
1139: return object.getDatabaseConnection();
1140: }
1141: return null;
1142: }
1143:
1144: /**
1145: * Retrieves the table data row count for the specified table.
1146: *
1147: * @param schema - the schema name (may be null)
1148: * @param table - the table name
1149: * @return the data row count as a String or a formatted error message
1150: */
1151: protected String getTableDataRowCount(String schema, String table) {
1152: try {
1153: checkMetaDataObject();
1154: metaData.setDatabaseConnection(getDatabaseConnection());
1155: return String.valueOf(metaData.getTableDataRowCount(schema,
1156: table));
1157: } catch (DataSourceException e) {
1158: return "Error: " + e.getMessage();
1159: }
1160: }
1161:
1162: /**
1163: * Retrieves the data in its entirety from the specified table.
1164: *
1165: * @param schema - the schema name (may be null)
1166: * @param table - the table name
1167: * @return the table data
1168: */
1169: public ResultSet getTableData(String schema, String table) {
1170: try {
1171: checkMetaDataObject();
1172: metaData.setDatabaseConnection(getDatabaseConnection());
1173: return metaData.getTableData(schema, table);
1174: } catch (DataSourceException e) {
1175: handleException(e);
1176: return null;
1177: }
1178: }
1179:
1180: /**
1181: * Drops the specified database object.
1182: *
1183: * @param dc - the database connection
1184: * @param object - the object to be dropped
1185: */
1186: public int dropObject(DatabaseConnection dc, DatabaseObject object)
1187: throws SQLException {
1188:
1189: String queryStart = null;
1190: int type = object.getType();
1191: switch (type) {
1192:
1193: case BrowserConstants.CATALOG_NODE:
1194: case BrowserConstants.SCHEMA_NODE:
1195: case BrowserConstants.OTHER_NODE:
1196: GUIUtilities
1197: .displayErrorMessage("Dropping objects of this type is not currently supported");
1198: return UPDATE_CANCELLED;
1199:
1200: case BrowserConstants.FUNCTIONS_NODE:
1201: queryStart = "DROP FUNCTION ";
1202: break;
1203:
1204: case BrowserConstants.INDEX_NODE:
1205: queryStart = "DROP INDEX ";
1206: break;
1207:
1208: case BrowserConstants.PROCEDURE_NODE:
1209: queryStart = "DROP PROCEDURE ";
1210: break;
1211:
1212: case BrowserConstants.SEQUENCE_NODE:
1213: queryStart = "DROP SEQUENCE ";
1214: break;
1215:
1216: case BrowserConstants.SYNONYM_NODE:
1217: queryStart = "DROP SYNONYM ";
1218: break;
1219:
1220: case BrowserConstants.SYSTEM_TABLE_NODE:
1221: case BrowserConstants.TABLE_NODE:
1222: queryStart = "DROP TABLE ";
1223: break;
1224:
1225: case BrowserConstants.TRIGGER_NODE:
1226: queryStart = "DROP TRIGGER ";
1227: break;
1228:
1229: case BrowserConstants.VIEW_NODE:
1230: queryStart = "DROP VIEW ";
1231: break;
1232:
1233: }
1234:
1235: if (querySender == null) {
1236: querySender = new QuerySender(dc);
1237: } else {
1238: querySender.setDatabaseConnection(dc);
1239: }
1240:
1241: String name = object.getName();
1242: return querySender.updateRecords(queryStart + name)
1243: .getUpdateCount();
1244: }
1245:
1246: /**
1247: * Propagates the call to the meta data object.
1248: */
1249: protected void closeConnection() {
1250: if (metaData != null) {
1251: metaData.closeConnection();
1252: }
1253: }
1254:
1255: }
|