Using XML Extensions in SQL Developer to Extend SYNONYM Support

This is a pretty old topic as far as SQL Developer goes. There are public articles going back to 2007 that show you how to extend the functionality in SQL Developer via SQL and XML. But I’m going to guess that for many of you that this is a new subject.

Knowing that extensions exist versus being able to create one is an important distinction. I want this post to help you realize both! We’re going to build two very simple extensions:

These extensions will allow you to create a SYNONYM (PUBLIC or otherwise) with just a right-click and then also view all synonyms associated with the current table.

How XML Extensions Work

The SQL Developer product has a framework that allows for an XML file to be used to define or extend functionality. Your custom XML extension is ‘sucked into’ SQL Developer at startup after supplying the XML file location in the Preferences dialog –

After you have created your extension XML file, you'll 'load' it here

After you have developed your XML source, save it to a file, then tell SQL Developer where to find it. On a restart, SQL Developer will see your new extension and load it into the tool. If you have made any serious mistakes such that SQL Developer can’t parse the XML, the extension will fail to load.

Adding a Context Menu Item

Context menu is a fancy dancy developer term for the menu you see when you right-click on something. The context of the menu is determined by WHERE you click. Anyways, you can add your own operations to these menus in SQL Developer.

For example I am very lazy and I want the tool to automatically create synonyms for me. This is handy when you have very long or difficult to type schema and table strings. So I’m going to take 10 minutes to code an extension so I can save 30 seconds every time I want to build a synonym. How often do you build a synonym?

The good news is that I don’t need to know any JAVA code to build this, and I can do all of the development directly inside of SQL Developer.

You can also provide a list of values to choose from in an input. That’s achieved with a VALUE tag inside the PROMPT section. We’ll cover all of the different permutations of scripting and form development in later posts.

Input values are assigned variables and are numbered 0..N. So if you have 3 prompts, the values of each would be #0#, #1#, and #2#. These can be referenced in the SQL that is generated and executed via the form we are developing. #OBJECT_OWNER# and #OBJECT_NAME# are system variables that SQLDev uses to refer to the object that the context menu is fired from.

You can code confirmation and help dialogs if you want to be nice. Try to be nice.

Note that I did not quote the new synonym name – I don’t believe in case-sensitive object names in Oracle. But I DID quote them in the SCHEMA.TABLE portion of the statement as I could be needing to create a synonym on an object that IS case sensitive or contains a reserved word.

The value of #0# will be NULL if the box isn’t checked. So when I build my CREATE statement I can use the #0# value in there regardless. If the input is checked, the value will be assigned to ‘PUBLIC’, otherwise NULL text is inserted and it works just fine.

Ok, so assuming you don’t see any bugs in my code, save your worksheet to an XML file in a directory you can remember for at least 30 seconds.

Now go into the Preferences and add this file to the User Defined Extensions as an ‘Action.’

Restart SQL Developer and right-click on a table. You should see this

Click that and you’ll get the following modal dialog

There's even help if you need it!

And if you want to see the SQL being generated before it runs

The SQL behind the SQL and XML

You’ll get the normal confirmation dialogs. Success if it works, ORA- if it doesn’t with the chance to copy out your error text.

Now I want to see all the synonyms I have on my table

Now I could walk the SQLDev connection tree to the SYNONYM and PUBLIC SYNONYM items and filter on TABLE_NAME. But no, I’m lazy, remember? So I’m going to code up a new page attached to my table viewer called SYNONYMS that lists both my private and public synonyms AND lets me auto-navigate to said synonym if I want to drop or rename it.

And now some notes – we’re creating an ‘Editor’ for ‘Tables.’ This will create a new page called ‘SYNONYMS’ on any table I click on in SQL Developer. It’s basically just going to give me a grid and all I need to supply is the query to feed the rows into it. And I could do that with a quick SELECT, but let’s step it up a notch and make the SYNONYM names hot-clickable. So if I click on a name, SQLDev will automatically open that synonym. We just need to apply the lessons learned in my SQL Developer Query & Grid Tricks post.

File > Save to XML.

Go back to the preferences and add another user defined extension, but this time as an ‘Editor.’

Restart SQL Developer, click on a table. You should see a new page or tab at the very end.

Click on the synonym name to open that synonym in SQLDev

You can do much, much more with these extensions

I just got my hands on the source and I’m going through all of it to pick and choose what makes sense to share here and what makes sense to include in a ‘cookbook’ that we publish for our 3rd party developers that want to build their own extensions and share or sell them. So stay tuned! If you have a question about how to implement a specific feature via extension feel free to post that here or just email me.

Seems like this feature can be pretty useful – but, is it documented? Personally, I’d like to take a good look at extending the editor functions, since I see this possibility at dropdown list, and a list of valid XML items for extensions with brief descriptions would also be nice. Can I find this info somewhere?

Cool stuff! Trying to convert our devs to use SQLdev (although plsql dev from AA is quite good aswell).
One small thing: I would avoid using dba_ views (use all_) as not everybody has these privs.
Cheers, Tony

I tried the first one (create synonym from context menu) and it worked like a charm. I can’t get the 2nd one (show all synonyms for the table) to work. I don’t get the extra tab. Please help. I am using SQL Developer 3.1.07.

Could this functionality be leveraged to auto-format integers to show commas a thousand grouping characters in data browser tabs and query result grids? Best would be if all of the existing functionality of these could be retained and this would be just and add-on…
Thoughts?
Thanks.

Figuring out SQL Developer extensions has eluded me thus far. I can’t seem to find up-to-date documentation that relates to what I want to do. I’m starting to wonder if it’s possible.

I guarantee I’m not the only one who has wanted some kind of auto-generated filter based on selected data from a table or result-set.

A.K.A. I would like to highlight some data in the object viewer (Data view), and based on that selection, generate a “WHERE clause” or a filter that I can paste into the filter input of the current table or another table.

If not on a copy event, a paste event would do perfectly. Or post-paste. I don’t really care. I would like to auto generate some kind of WHERE clause formatted like:

[COLUMN NAME] IN (‘firstvalue’, ‘secondvalue’, ‘thirdvalue’)

Or even without the column name part. Just a way to quickly format an IN (”,”) query.

I can’t make sense of the documentation that I’ve found so far. Any help would be extremely appreciated.

Get my best SQL Developer and database advice delivered to your inbox.

Welcome

Here you will find tips and tricks for using Oracle SQL Developer, Oracle SQL Developer Data Modeler, and Oracle Database here. If you can't find what you're looking for, use the 'Ask a Question' link up top.

I'm so glad you're here! -Jeff Smith

Weekly Webcasts

I host informal webinars with the folks that build your database development tools and technologies. Recorded and available for playback on demand on YouTube.

I mostly write about working with Oracle databases, mostly. I currently work for Oracle, but I'm not an official spokesman, nor am I authorized to speak on behalf of Oracle Corp. In other words, what you read here are my words and ramblings.

If you've detected a bit of snark, that means you've been paying attention. +1 bonus credits for you!