EADS-1456 The Find Usages functionality in Oracle is implemented in a slightly backwards way
1.1 --- a/PLSQL/Execution/src/org/netbeans/modules/plsql/execution/PlsqlFileExecutor.java Mon Feb 13 15:34:02 2012 +0530
1.2 +++ b/PLSQL/Execution/src/org/netbeans/modules/plsql/execution/PlsqlFileExecutor.java Mon Feb 13 16:35:09 2012 +0530
1.3 @@ -489,20 +489,6 @@
1.4
1.5 stm = con.createStatement();
1.6 stm.setEscapeProcessing(false);
1.7 - if (connectionProvider.isUsagesEnabled() && validator.isValidPackage(dataObj)) {
1.8 - String plsqlText = "ALTER SYSTEM SET PLSCOPE_SETTINGS = 'IDENTIFIERS:ALL' \t\n";
1.9 - try {
1.10 - //io.getOut().println((new StringBuilder()).append("> Enabling Usages for ").append(fileName));
1.11 - stm.execute(plsqlText);
1.12 - } catch (SQLException sqlEx) {
1.13 - int errLine = getLineNumberFromMsg(sqlEx.getMessage());
1.14 - int outLine = errLine - 1;
1.15 - String msg = getmodifiedErorrMsg(sqlEx.getMessage(), outLine);
1.16 - // io.getErr().println((new StringBuilder()).append("!!!Error Creating View ").append(exeObjName).toString());
1.17 - io.getOut().println(plsqlText);
1.18 - deploymentOk = false;
1.19 - }
1.20 - }
1.21 boolean firstSelectStatement = true;
1.22 for (int i = 0; i < executableObjs.size(); i++) {
1.23 if (cancel) {
2.1 --- a/PLSQL/Usage/src/org/netbeans/modules/plsql/usage/FindUsagesAction.java Mon Feb 13 15:34:02 2012 +0530
2.2 +++ b/PLSQL/Usage/src/org/netbeans/modules/plsql/usage/FindUsagesAction.java Mon Feb 13 16:35:09 2012 +0530
2.3 @@ -110,53 +110,54 @@
2.4 RP.post(this);
2.5 }
2.6
2.7 - @Override
2.8 - public void run() {
2.9 -// final EditorCookie editorCookie = activatedNodes[0].getLookup().lookup(EditorCookie.class);
2.10 - final DataObject dataObject = activatedNodes[0].getLookup().lookup(DataObject.class);
2.11 + @Override
2.12 + public void run() {
2.13 + final DataObject dataObject = activatedNodes[0].getLookup().lookup(DataObject.class);
2.14
2.15 - final ProgressHandle progressHandle = ProgressHandleFactory.createHandle("Searching for usages...");
2.16 + final ProgressHandle progressHandle = ProgressHandleFactory.createHandle("Searching for usages...");
2.17
2.18 - if (dataObject != null) {
2.19 + if (dataObject != null) {
2.20
2.21 - final Project project = FileOwnerQuery.getOwner(dataObject.getPrimaryFile());//activatedNodes[0].getLookup().lookup(Project.class);
2.22 - if (project != null) {
2.23 - final DatabaseConnectionManager provider = DatabaseConnectionManager.getInstance(dataObject);
2.24 - final DatabaseConnection connection = provider.getPooledDatabaseConnection(false, true);
2.25 + final Project project = FileOwnerQuery.getOwner(dataObject.getPrimaryFile());
2.26 + if (project != null) {
2.27 + final DatabaseConnectionManager provider = DatabaseConnectionManager.getInstance(dataObject);
2.28 + final DatabaseConnection connection = provider.getPooledDatabaseConnection(false, true);
2.29
2.30 - if (connection == null || connection.getJDBCConnection() == null) {
2.31 - return;
2.32 + if (connection == null || connection.getJDBCConnection() == null) {
2.33 + return;
2.34 + }
2.35 +
2.36 + try {
2.37 + progressHandle.start();
2.38 +
2.39 + signature = getSignature(connection, packageName, object_type, context, false);
2.40 +
2.41 + final List<PlsqlElement> list = getUsageList(connection, project);
2.42 + SwingUtilities.invokeLater(new Runnable() {
2.43 +
2.44 + @Override
2.45 + public void run() {
2.46 + PlsqlUsagePanel up = new PlsqlUsagePanel(list, selectedName, usageCount, project);
2.47 + }
2.48 + });
2.49 +
2.50 + } catch (SQLException ex) {
2.51 + if (provider.testConnection(connection)) {
2.52 + final NotifyDescriptor d = new NotifyDescriptor.Message("Find usages not supported in this database. Please refer to the documentation for details.",
2.53 + NotifyDescriptor.INFORMATION_MESSAGE);
2.54 + DialogDisplayer.getDefault().notify(d);
2.55 + } else {
2.56 + final NotifyDescriptor d = new NotifyDescriptor.Message("You are not connected to a database.",
2.57 + NotifyDescriptor.INFORMATION_MESSAGE);
2.58 + DialogDisplayer.getDefault().notify(d);
2.59 + }
2.60 + } finally {
2.61 + provider.releaseDatabaseConnection(connection);
2.62 + progressHandle.finish();
2.63 + }
2.64 }
2.65 - try {
2.66 - progressHandle.start();
2.67 - signature = getSignature(connection, packageName, object_type, context, false);
2.68 -
2.69 - final List<PlsqlElement> list = getUsageList(connection, project);
2.70 - SwingUtilities.invokeLater(new Runnable() {
2.71 -
2.72 - @Override
2.73 - public void run() {
2.74 - PlsqlUsagePanel up = new PlsqlUsagePanel(list, selectedName, usageCount, project);
2.75 - }
2.76 - });
2.77 -
2.78 - } catch (SQLException ex) {
2.79 - if (provider.testConnection(connection)) {
2.80 - final NotifyDescriptor d = new NotifyDescriptor.Message("Find usages not supported in this database. Please refer to the documentation for details.",
2.81 - NotifyDescriptor.INFORMATION_MESSAGE);
2.82 - DialogDisplayer.getDefault().notify(d);
2.83 - } else {
2.84 - final NotifyDescriptor d = new NotifyDescriptor.Message("You are not connected to a database.",
2.85 - NotifyDescriptor.INFORMATION_MESSAGE);
2.86 - DialogDisplayer.getDefault().notify(d);
2.87 - }
2.88 - } finally {
2.89 - provider.releaseDatabaseConnection(connection);
2.90 - progressHandle.finish();
2.91 - }
2.92 - }
2.93 - }
2.94 - }
2.95 + }
2.96 + }
2.97
2.98 @Override
2.99 protected int mode() {
2.100 @@ -365,33 +366,67 @@
2.101 return list;
2.102 }
2.103
2.104 - protected String getSignature(final DatabaseConnection connection, final String packageName, final String object_type, final String context, final boolean typeKnown) throws SQLException {
2.105 - ResultSet rs = null;
2.106 - PreparedStatement stmt = null;
2.107 - String sqlSelect = null;
2.108 - try {
2.109 - sqlSelect = "select a.signature, a.type from all_identifiers a, all_identifiers b "
2.110 - + "where a.USAGE_CONTEXT_ID = b.USAGE_ID "
2.111 - + "AND a.name='" + selectedName.toUpperCase(Locale.ENGLISH) + "' "
2.112 - + "AND a.Object_Name='" + packageName.toUpperCase(Locale.ENGLISH) + "' "
2.113 - + "AND a.OBJECT_NAME = b.OBJECT_NAME "
2.114 - + "AND a.object_type='" + object_type.toUpperCase(Locale.ENGLISH) + "' "
2.115 - + "AND a.OBJECT_TYPE = b.OBJECT_TYPE "
2.116 - + "AND (b.usage = 'DEFINITION' OR b.usage = 'DECLARATION') "
2.117 - + ((isFunctionOrProcedure) ? "AND (a.type ='FUNCTION' OR a.type='PROCEDURE')" : "AND b.name = '" + context.toUpperCase(Locale.ENGLISH) + "'");
2.118 + protected String getSignature(final DatabaseConnection connection, final String packageName, final String object_type, final String context, final boolean typeKnown) throws SQLException {
2.119 + ResultSet rs = null;
2.120 + PreparedStatement stmt = null;
2.121 + String sqlSelect = null;
2.122 + try {
2.123 + sqlSelect = "select a.signature, a.type from all_identifiers a, all_identifiers b "
2.124 + + "where a.USAGE_CONTEXT_ID = b.USAGE_ID "
2.125 + + "AND a.name='" + selectedName.toUpperCase(Locale.ENGLISH) + "' "
2.126 + + "AND a.Object_Name='" + packageName.toUpperCase(Locale.ENGLISH) + "' "
2.127 + + "AND a.OBJECT_NAME = b.OBJECT_NAME "
2.128 + + "AND a.object_type='" + object_type.toUpperCase(Locale.ENGLISH) + "' "
2.129 + + "AND a.OBJECT_TYPE = b.OBJECT_TYPE "
2.130 + + "AND (b.usage = 'DEFINITION' OR b.usage = 'DECLARATION') "
2.131 + + ((isFunctionOrProcedure) ? "AND (a.type ='FUNCTION' OR a.type='PROCEDURE')" : "AND b.name = '" + context.toUpperCase(Locale.ENGLISH) + "'");
2.132
2.133 - stmt = connection.getJDBCConnection().prepareStatement(sqlSelect);
2.134 - rs = stmt.executeQuery();
2.135 - if (rs.next()) {
2.136 - return rs.getString("SIGNATURE");
2.137 - }
2.138 - } finally {
2.139 - if (stmt != null) {
2.140 - stmt.close();
2.141 - }
2.142 - }
2.143 - return null;
2.144 - }
2.145 + stmt = connection.getJDBCConnection().prepareStatement(sqlSelect);
2.146 + rs = stmt.executeQuery();
2.147 + if (rs.next()) {
2.148 + return rs.getString("SIGNATURE");
2.149 + } else if (!isFindUsagesEnabled(connection)) {
2.150 + final NotifyDescriptor d = new NotifyDescriptor.Message("Find usages disabled. The database setting PLSCOPE_SETTINGS must be set to 'IDENTIFIERS:ALL' to enable the functionality.",
2.151 + NotifyDescriptor.INFORMATION_MESSAGE);
2.152 + DialogDisplayer.getDefault().notify(d);
2.153 + }
2.154 + } finally {
2.155 + if (stmt != null) {
2.156 + stmt.close();
2.157 + }
2.158 + }
2.159 + return null;
2.160 + }
2.161 +
2.162 + private boolean isFindUsagesEnabled(DatabaseConnection connection) {
2.163 + ResultSet rs = null;
2.164 + PreparedStatement stmt = null;
2.165 + String setting = null;
2.166 +
2.167 + try {
2.168 + String sqlSelect = "SELECT PLSCOPE_SETTINGS FROM ALL_PLSQL_OBJECT_SETTINGS WHERE NAME = \'SECURITY_SYS\' AND TYPE = \'PACKAGE\'";
2.169 + stmt = connection.getJDBCConnection().prepareStatement(sqlSelect);
2.170 + rs = stmt.executeQuery();
2.171 + if (rs.next()) {
2.172 + setting = rs.getString(1);
2.173 + }
2.174 +
2.175 + if (setting != null && setting.equalsIgnoreCase("IDENTIFIERS:ALL")) {
2.176 + return true;
2.177 + }
2.178 + } catch (SQLException ex) {
2.179 + // do nothing
2.180 + } finally {
2.181 + if (stmt != null) {
2.182 + try {
2.183 + stmt.close();
2.184 + } catch (SQLException ex) {
2.185 + // do nothing
2.186 + }
2.187 + }
2.188 + }
2.189 + return false;
2.190 + }
2.191
2.192 private String getPackageName(final DataObject dataObject) {
2.193 String packName = "";