User-Defined Context Menus in SQLDeveloper

“What are those birds ?”, Deb asked as we lay drowsing by the pool. “Must be seagulls”, I replied confidently.
We may have been in Tenerife rather than Southend, but they were definitely gulls, and we were right next to the sea.
“They’re not seagulls”, Deb stated emphatically, “if they were, they’d be squawking like a bunch of Welsh women fighting over the last pair of shoes in the sale!”
I forbore to comment on this. After all, I’d had recent, painful, experience of Welsh squawking the previous evening.
We’d arrived at the hotel, just in time to watch England come second in the Rugby…to Wales.
Let me tell you squawking wasn’t the half of it.
As usual my emergency backup nationality ( born in Auckland) was no defence against the joyous derision pouring forth from my better half.

Fortunately, the Spanish aren’t big on Rugby so I’ve had a week of relative quiet on that front.
As you’ve no doubt noticed by now, I’m not much of an ornithologist, so I’ve had to find something else to help while away the long hours beside the pool.

Step forward something that I’ve been puzzling over for some time, namely, just what options are available if you want to put together a User-Defined Context Menu for SQLDeveloper.

About now, someone is bound to mention Java. However, my Java is more rusty than a 1973 Ford Cortina that’s been parked in Cardiff Bay for two weeks.

Besides, it’s perfectly possible to knock up some fairly respectable SQLDeveloper extensions using just XML and some SQL and PL/SQL. However, when it comes to Context Menus there doesn’t seem to be one single place for definitive documentation.
Therefore, after much trial and error, I’m going to take this opportunity to set out the options that I have managed to get working, together with examples.

What I’m going to cover is :

  • How to add a context menu to SQLDeveloper
  • Where on the Navigator Tree you can add it
  • How to get more than one item on a Context Menu
  • The various types of input field you can code
  • How to use Context Menus to execute SQL statements and PL/SQL blocks

Before we go any further, I think I should state that these examples were written and tested on SQLDeveloper 4.0.3 running against an Oracle XE 11g database.
Continue reading

Triggers on Views in SQLDeveloper

So, you’ve noticed that SQLDeveloper ( any version prior to 2.1.1) doesn’t show triggers on views. At the same time SQLDeveloper 2.1.1 doesn’t show package bodies unless you have a very highly privileged account.
If you’re determined to press-on with 2.1.1, you can see a workaround for the package body problem here.
If however, you’d prefer to wait until they’ve ironed out this particular kink before taking the plunge, but want a solution to the invisible triggers, read on … Continue reading

Adding your own tabs in SQLDeveloper

As I said in my previous post, one of the really cool things about SQLDeveloper is that it is easy to add your own custom extensions.

Confession – I’ve knocked this together using SQLDeveloper 1.5.4 running against Oracle XE on…er…Windows Vista.

I know, after all that stuff about how great Ubuntu is. Look, I just haven’t got round to installing Oracle yet. All these annoying little things like going to work keep getting in the way. Look It’s on the list OK ?

Anyway, back to SQLDeveloper.

Take table information as an example. By default SQLDeveloper will show constraint information for a table, but it won’t display the relationship between columns in the child and parent tables of a Foreign Key relationship.

Standard SQLDeveloper Table view

Getting this information from the data dictionary can be done with the following query :-

SELECT cons.constraint_name, fkcols.column_name,
    pkcols.table_name as "Referenced Table",
    pkcols.column_name as "Referenced Column"
FROM all_constraints cons, all_cons_columns fkcols,
    all_cons_columns pkcols
WHERE cons.owner = fkcols.owner
AND cons.table_name = fkcols.table_name
AND cons.constraint_name = fkcols.constraint_name
AND cons.r_owner = pkcols.owner
AND cons.r_constraint_name = pkcols.constraint_name
AND fkcols.position = pkcols.position
AND cons.constraint_type = 'R'
AND cons.owner = :OBJECT_OWNER
AND cons.table_name = :OBJECT_NAME
ORDER BY fkcols.column_name, fkcols.position

Simple enough, but it would be nice to see this for every table without having to type in and run this query every time.
To add this query as a permanent tab in SQLDeveloper, all you need to do is create an XML file containing the text of the query.

<items>
    <item type="editor" node="TableNode" vertical="true">
    <title><![CDATA[FK Columns]]></title>
    <query>
        <sql>
            <![CDATA[SELECT cons.constraint_name, fkcols.column_name,
                    pkcols.table_name as "Referenced Table",
                    pkcols.column_name as "Referenced Column"
                    FROM all_constraints cons, all_cons_columns fkcols,
                    all_cons_columns pkcols
                    WHERE cons.owner = fkcols.owner
                    AND cons.table_name = fkcols.table_name
                    AND cons.constraint_name = fkcols.constraint_name
                    AND cons.r_owner = pkcols.owner
                    AND cons.r_constraint_name = pkcols.constraint_name
                    AND fkcols.position = pkcols.position
                    AND cons.constraint_type = 'R'
                    AND cons.owner = :OBJECT_OWNER
                    AND cons.table_name = :OBJECT_NAME
                    ORDER BY fkcols.column_name, fkcols.position]]>
        </sql>
    </query>
    </item>
</items>

The are only two bits of this file you need to change :-

<title><![CDATA[FK Columns]]></title>

FK Columns will be the name of the tab displayed in SQLDeveloper.

The other bit obviously, is the SQL statement.

On the subject of the SQL, we’re using two standard SQLDeveloper placeholders – :OBJECT_OWNER and :OBJECT_NAME.

To add this extension into SQLDeveloper

1) Go to the Tools Menu and select Preferences

You will then be presented with the Preferences Dialog

2) From the Menu tree in the left-hand pane, select Database and then User Defined Extensions – as shown below

Preferences Dialog

Preferences Dialog

3) Click on the Add Row button.

4) Click on the row under the Type column and select EDITOR from the drop-down list.

5) Click on the row under the Location column and click the Browse button that appears.

6) Navigate to the XML file you’ve created and click OK.

You should now be looking at something like this

New Tab definition

New Tab definition

7) Now shutdown and re-start SQLDeveloper.

Now if you view a table, you should see your new tab ( FK Columns in this case).

New Table Tab

New Table Tab ( FK Columns)

And that’s it.

If you need any further information, there’s a fairly good ( and rather more comprehensive) tutorial at http://www.oracle.com/technology/obe/sqldev_obe/extension/extensions.htm
Update – a big thanks to Adrian Dillon ( see comment below), for pointing out my omission in the SQL on this one, which I’ve now corrected.