Thursday, May 7, 2009

Schema Refactoring

Ever wanted to change a column or table name? I'm pretty sure there aint no way to globally update changes like this throughout your application via the APEX interface. So here's a little tip on how you can get away with it and the gotchas to look out for.

Simply export the application, make a copy of the exported SQL file and then open it in your favourite text editor. Perform a search and replace for all instances of the required change. I recommend reviewing each replacement one by one. It may seem like a tedious option, but imagine trying to search the application manually for all these references via the web interface.

Keep track of the pages, particularly reports, that you are altering. You'll want to test them all after the import. You can usually find the page id at the top of the section you're messing with, or else an item name prefix in the vicinity will give it away.

Import the application, then check each of the pages you've altered for workingness. Often I've noticed that report regions may throw a "no data found" exception for no obvious reason (the report itself will not render at all). The resolution to this is to:
  • duplicate the region by using the region copy tool
  • move any region items to the copy
  • confirm the copy works as expected
  • delete the original
This is a handy workaround to know about for when regions start behaving oddly in general. This problem is not only triggered by editing an export file. It's some kind of bug that tends to crop up when you make major changes to a report region's query. I've seen plenty of people complaining about it on the forum, but no official explanation of how or why it occurs. Perhaps some cache like mechanism is at play somewhere.

You could also try deleting the application before importing it, I've not tried it. But if you intend to eventually migrate to production and you want to minimise your outage, I wouldn't recommend that as an option. I've had weird bugs appear after deploying to production that didn't exist in the exact same application in dev or test. Better to annihilate the risk of delaying the inevitable by getting your self a new region ID.

Perhaps I could put together a groovy tool which intelligently identifies all database objects in an export file, allows you to change them and also manages the assignment of new region IDs for altered parts of the application. That would rock!

Monday, January 19, 2009

Clickable Active Tabs

All the default templates in Application Express seem to have disabled active tabs (ie. you can't select the tab that is already selected). To enable these tabs, edit the page template and copy the anchor tag around #TAB_LABEL# from the "Non Current Standard Tab" section and paste it over the #TAB_LABEL# place holder in the "Current Tab" section.

Wednesday, January 7, 2009

Redirection Process Follow Up

It seems there are a few undocumented features you can add to the mix to get the redirect to work more efficiently. I'm lead to believe that htp.init clears any headers that may have been set previously and setting apex_application.g_unrecoverable_error to true will tell APEX to stop processing the page after the redirect process is complete. Though both are not officially documented, there is a comment in the package next to apex_application.g_unrecoverable_error that reads:
indicates error has occured which requires error page

So here's what I'm using now:

htp.init;
owa_util.redirect_url('f?p='||:APP_ID||':XYZ:'||:APP_SESSION||'::'||:DEBUG||':::');
owa_util.http_header_close;
apex_application.g_unrecoverable_error := true;

According to the doco, this should result in the following raw HTTP response where <curl> is the resultant url:
Location: <curl>\n\n

Here's some references:
http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28419/w_util.htm
http://www.psoug.org/reference/htp.html
http://download.oracle.com/docs/cd/B28359_01/appdev.111/b28419/w_htp.htm
http://www.google.com/search?q=apex_application.g_unrecoverable_error

Update: After inspecting the response, seems we don't need to explicitly close headers.
Update: See my post about HTTP Headers for more details about htp.init behaviour.

Avoiding Varchar(4000)

I usually never use varchar(4000) unless there is a damn good reason to do so. Most people don't plan to use the 4000 character limit, but it does seem to be the default in APEX. If you are planning to use varchar(4000) you should probably use a clob instead. Why place an arbitrary limit on a column if you don't want one and you can avoid it?

Perhaps the need to query the column is outweighed by the risk that the user will want to enter more than 4000 characters of data, usually you don't need to directly query large blocks of text. If you don't ever need to search the block of text then just use a clob. Trust me, it will save you much grief in future.

Many times in the past I've peer reviewed data models, systematically replacing varchar(4000) with clob columns. But looks like I didn't follow my own advice on my current project. Now I'm smashing my head against the desk. The users of this shiny new system are all academics, so I should have known it was just a matter of time before someone tried to post a 10 page rant. Now I'm trying to convert the columns to clobs and APEX is being less than forgiving.

So far my approach was to create a temporary clob column, move the varchar data into the clob, drop the varchar and rename the clob. APEX then throws "ORA-01008: not all variables bound" exceptions when I visit the form that previously used the varchar version of the column. I've tried deleting the related page items and the exception went away. Creating new clob based page items keeps giving me that exception though. Gah! I'm this close to just rebuilding the form from scratch.

Take my advice, don't use varchar(4000)!

Update: I now know the trick to resolving ORA-01008 is usually to make a copy of the region and delete the original.

Sunday, November 30, 2008

Redirection Process

In Application Express, it's not obvious how you would redirect to another page if the requested one fails an assertion. You can do so quite easily by creating a conditional 'On Load - Before Header' page process like this...
owa_util.redirect_url('f?p='||:APP_ID||':XYZ:'||:APP_SESSION||'::'||:DEBUG||':::');
owa_util.http_header_close;
Where 'XYZ' is the page you desire to redirect to and the condition is the assertion fail state.

If you would like to display a message on said page you can set a hidden page variable in the process using v('PXYZ_VAR') := 'my message';. Then have an 'After Header' process on that destination page which displays the contents of the page variable if it's not null (use an 'item not null' condition to determine this). Here's the code that will give you a nice Apex style message...
apex_application.g_print_success_message := :PXYZ_VAR;
:PXYZ_VAR := '';
Simple as that!

Update: More details can be found in this follow up post.