Oracle APEX has a built-in Dynamic Action called "Execute PL/SQL Code" that executes a block of PL/SQL code on the server via an Ajax call (ie does not do a regular submit and reload of the whole page).
But what if you want to do something on the server AND also return some content back to the client? Back in 2012 I released an APEX Dynamic Action plugin called "Execute PL/SQL Code and Return Content" that allows you to do just that. Read the original blog post for more information.
Since then, the APEX framework has evolved and the old Ajax workhorse function called "htmldb_Get" has been deprecated and replaced with functions in the "apex.server" namespace. Hence I have updated my plugin to use the modern API.
You can find the updated version (1.1) of the plugin on my GitHub page for APEX plugins. Note that the new plugin has been exported from APEX 18.1 and therefore requires at least that version to import and use.
Enjoy! :-)
Showing posts with label database-centric architecture. Show all posts
Showing posts with label database-centric architecture. Show all posts
Wednesday, January 30, 2019
Thursday, August 9, 2018
Quick PL/SQL, a code generator for PL/SQL based on simple markup
Maybe you've heard about "Quick SQL", a utility (previously a separate application, now part of APEX 18.1 itself) that generates SQL scripts based on a simple markup language. Quick SQL is a real time-saver and allows you to go from idea to prototype to working application in an instant.
Inspired by Quick SQL, I've created a similar utility, called "Quick PL/SQL", that does the same thing, except it generates PL/SQL code (packages with functions and procedures, with standard comment blocks, formatting, etc.) based on a simple markup.
I've recorded a couple of videos to show the tool in action.
Here is an introduction which explains the basic input syntax and the output options:
And here is another video which explains how to create standard CRUD-style (Create, Read, Update, Delete) APIs for your tables:
Try it out for yourself here: https://apex.oracle.com/pls/apex/f?p=QUICKPLSQL:HOME&c=MULEDEV or via this shortcut: https://tinyurl.com/quickplsql2
Of course, I wouldn't mind if this functionality becomes available as part of the QuickSQL utility that is built into APEX! :-)
Inspired by Quick SQL, I've created a similar utility, called "Quick PL/SQL", that does the same thing, except it generates PL/SQL code (packages with functions and procedures, with standard comment blocks, formatting, etc.) based on a simple markup.
I've recorded a couple of videos to show the tool in action.
Here is an introduction which explains the basic input syntax and the output options:
And here is another video which explains how to create standard CRUD-style (Create, Read, Update, Delete) APIs for your tables:
Try it out for yourself here: https://apex.oracle.com/pls/apex/f?p=QUICKPLSQL:HOME&c=MULEDEV or via this shortcut: https://tinyurl.com/quickplsql2
Of course, I wouldn't mind if this functionality becomes available as part of the QuickSQL utility that is built into APEX! :-)
Thursday, July 30, 2015
PL/SQL: The Good Parts
Somewhat inspired by Douglas Crockford's Javascript: The Good Parts presentation (and book), I have created a presentation called PL/SQL: The Good Parts.
This is not intended as an introduction to PL/SQL, but rather an overview of features you should consider if you want to maximize your enjoyment of programming in the Oracle Database. Also, this is not an exhaustive list of all features, it's simply the ones I personally use frequently.
Note that unlike Javascript, I don't consider PL/SQL to have too many "bad parts" or bizarre quirks. There's a lot of good stuff, hence the title! :-)
Click here to view the presentation.
This is not intended as an introduction to PL/SQL, but rather an overview of features you should consider if you want to maximize your enjoyment of programming in the Oracle Database. Also, this is not an exhaustive list of all features, it's simply the ones I personally use frequently.
Note that unlike Javascript, I don't consider PL/SQL to have too many "bad parts" or bizarre quirks. There's a lot of good stuff, hence the title! :-)
Click here to view the presentation.
Labels:
database-centric architecture,
oracle,
pl/sql
Tuesday, November 6, 2012
Apex Plugin: Execute PL/SQL code and return content to page
Here is a Dynamic Action plugin for Oracle Application Express (Apex) that lets you execute PL/SQL code in the database, and then send output generated on the server (via calls to the "HTP.P" procedure) back to any element on the web page.
A more specific use case would be to respond to the clicking of a button by generating a fragment of HTML using PL/SQL, and then refresh a DIV on the page with that dynamically generated content. See the screenshot below for an example; notice that part of the first region has been updated with content generated on the server (using input from the user, in this case the name from a regular text input item). Every time the user clicks on the button, the line above the button is updated using an Ajax call to the server.
The second region illustrates another use case: Automatically refreshing part of the page at a fixed interval.
This is how the page has been set up:
The first region has a standard text input item, and a button. The button has a Dynamic Action that is triggered when the button is clicked. The click executes the PL/SQL code which generates the greeting.
The dynamic action is set up as follows:
We can see that the content generated by the PL/SQL code is returned into a page element called "my_div". This div is simply included as the static content of the HTML region:
The second dynamic action has been set up to trigger on "Page Load". The plugin is set up to call the PL/SQL code every 5 seconds (nice way to hammer your database with requests...) and return the content into another div, called "my_other_div". The mode has been set to "Append after", which means the content will be added to the end of the existing content, instead of replacing it.
Here is the setup for the second region:
Conclusion: This plugin combines the flexibility of Dynamic Actions with the power of PL/SQL for dynamic content generation.You can download the plugin here. Update 22.05.2016: This plugin is now in my plugin repository at GitHub.
A more specific use case would be to respond to the clicking of a button by generating a fragment of HTML using PL/SQL, and then refresh a DIV on the page with that dynamically generated content. See the screenshot below for an example; notice that part of the first region has been updated with content generated on the server (using input from the user, in this case the name from a regular text input item). Every time the user clicks on the button, the line above the button is updated using an Ajax call to the server.
The second region illustrates another use case: Automatically refreshing part of the page at a fixed interval.
This is how the page has been set up:
The first region has a standard text input item, and a button. The button has a Dynamic Action that is triggered when the button is clicked. The click executes the PL/SQL code which generates the greeting.
The dynamic action is set up as follows:
We can see that the content generated by the PL/SQL code is returned into a page element called "my_div". This div is simply included as the static content of the HTML region:
The second dynamic action has been set up to trigger on "Page Load". The plugin is set up to call the PL/SQL code every 5 seconds (nice way to hammer your database with requests...) and return the content into another div, called "my_other_div". The mode has been set to "Append after", which means the content will be added to the end of the existing content, instead of replacing it.
Here is the setup for the second region:
Conclusion: This plugin combines the flexibility of Dynamic Actions with the power of PL/SQL for dynamic content generation.
Labels:
Apex,
Apex plugins,
database-centric architecture
Monday, July 23, 2012
Simple database (schema) backup using PL/SQL
Usually, database backups are the responsibility of DBAs, and not something that developers really care or think too much about. However, if you are a lone developer, or part of a small team that doesn't have the luxury of having a dedicated DBA, then you might have to deal with backups yourself.
There are several ways to make Oracle backups; what I will be concentrating on here is the "Data Pump Export" method. You may already be familiar with the command-line "expdp" command which allows you to create a dump (.dmp) file containing your database objects (schemas, tables, procedures, etc.).
To perform a regular backup, one could create a simple batch file to run "expdp" and schedule it using the Windows Task Scheduler or a Unix Cron job. While this certainly works, it means that you have one more "moving part" to think about (the OS scheduler), and you may have to hardcode database passwords in the batch file unless you use OS authentication.
However, the "expdp" command-line utility is actually just a "front-end" to a PL/SQL package called DBMS_DATAPUMP, as can be seen in this diagram:
This means that we can call the DBMS_DATAPUMP package directly from our own PL/SQL code to create dump files, and use DBMS_JOB to schedule this backup at regular intervals. We avoid the dependence on batch files, and we don't have to expose any passwords.
The API for DBMS_DATAPUMP is flexible and, as mentioned, supports everything that can be done from the command line (both exports and imports), but it can sometimes be a bit tricky to get the (filtering) syntax correct.
I've written a wrapper package called DATAPUMP_UTIL_PKG, which you can download as part of the Alexandria PL/SQL Utility Library. This package makes it really easy to perform the most common use case: Exporting a single schema to a file on disk. This can be accomplished with the following code:
-- export current schema to file, use default file name, and make the export compatible with XE 10g
-- include a custom message
begin
debug_pkg.debug_on;
datapump_util_pkg.export_schema_to_file ('DEVTEST_TEMP_DIR', p_version => '10.2', p_log_message => 'it is possible to include custom messages in the log');
end;
/
There are several ways to make Oracle backups; what I will be concentrating on here is the "Data Pump Export" method. You may already be familiar with the command-line "expdp" command which allows you to create a dump (.dmp) file containing your database objects (schemas, tables, procedures, etc.).
To perform a regular backup, one could create a simple batch file to run "expdp" and schedule it using the Windows Task Scheduler or a Unix Cron job. While this certainly works, it means that you have one more "moving part" to think about (the OS scheduler), and you may have to hardcode database passwords in the batch file unless you use OS authentication.
However, the "expdp" command-line utility is actually just a "front-end" to a PL/SQL package called DBMS_DATAPUMP, as can be seen in this diagram:
This means that we can call the DBMS_DATAPUMP package directly from our own PL/SQL code to create dump files, and use DBMS_JOB to schedule this backup at regular intervals. We avoid the dependence on batch files, and we don't have to expose any passwords.
The API for DBMS_DATAPUMP is flexible and, as mentioned, supports everything that can be done from the command line (both exports and imports), but it can sometimes be a bit tricky to get the (filtering) syntax correct.
I've written a wrapper package called DATAPUMP_UTIL_PKG, which you can download as part of the Alexandria PL/SQL Utility Library. This package makes it really easy to perform the most common use case: Exporting a single schema to a file on disk. This can be accomplished with the following code:
-- export current schema to file, use default file name, and make the export compatible with XE 10g
-- include a custom message
begin
debug_pkg.debug_on;
datapump_util_pkg.export_schema_to_file ('DEVTEST_TEMP_DIR', p_version => '10.2', p_log_message => 'it is possible to include custom messages in the log');
end;
/
To schedule this as a regular job, use the following code:
declare
l_job number;
l_what varchar2(4000);
begin
l_what := 'datapump_util_pkg.export_schema_to_file(''DEVTEST_TEMP_DIR'', p_version => ''10.2'');';
-- run job at the end of each day
dbms_job.submit (job => l_job, what => l_what, next_date => sysdate, interval => 'trunc(sysdate + 1) + 23/24');
commit;
end;
/
For this to work, you also need to set up a directory on the database server:
-- run as SYS
create directory devtest_temp_dir as 'c:\some_folder\backup\';
grant read, write on directory devtest_temp_dir to your_database_schema;
Obviously, since the files are written to the database server itself, you need some kind of process to move the files to another server or storage location, so the backups don't disappear along with the database if the database server itself is somehow lost or corrupted. Unlike the "hot" database files, these dump files can be safely copied by normal backup software.
Another option is to upload the backup file from the database to some online ("cloud") storage service, such as Amazon S3. I will talk about that in my next blog post...
PS. Don't forget to regularly test a restore (import) of your backup files. The only thing worse than not having a backup is having a backup that can't be restored... !
Labels:
Alexandria,
database-centric architecture,
pl/sql
Thursday, April 14, 2011
Just give me a hash table and a shitload of RAM
In this interview, James Gosling, the "father of Java", says:
Right... just "do something to deal with failures". And maybe add some other useful stuff. Shouldn't take too long to implement and make sure it works as intended.
But Mr. Gosling, could you please give me a cost estimate and a delivery date for the data-centric business application I want you to build for me?
I’ve never got it when it comes to SQL databases. It’s like, why? Just give me a hash table and a shitload of RAM, and I’m happy. And then you do something to deal with failures.
Right... just "do something to deal with failures". And maybe add some other useful stuff. Shouldn't take too long to implement and make sure it works as intended.
But Mr. Gosling, could you please give me a cost estimate and a delivery date for the data-centric business application I want you to build for me?
Wednesday, January 19, 2011
Back to the Future
Come with me on a short trip through space and time, taking a look at the history of building (web sites).
The Stone Age, circa 1995
Although it involved a lot of manual labor, mighty Stonehenge and the pyramids were built with very simple tools (or by aliens, but let's deal with that topic another day).
Web development was pretty basic back then, but conceptually simple enough.
The Dark Ages, circa 1998
Some pretty amazing castles were built during this age, and they still stand today.
The basic tools had improved a lot, but they were still recognizable. Even an Neanderthal from the previous age could probably be effective with these tools.
The Age of Enlightenment, circa 2002
The gothic cathedrals from around this time look awesome, but were (and still are) inhabited by unpleasant religious fanatics.
At this point, great thinkers with lots of free time combined art and science to build magnificent works of beauty and elegance.
The Age of the Astronauts and the Race for Space (via the Clouds), circa 2008
A lot of very expensive spaceships were built during this age.
The great empires fought a cold, mostly ideological, war to see who could be the first to reach the moon.
Of course this all ended with a ka-boom and the eventual retirement of the space shuttle program.
A New Hope, 2011
Realizing that space tourism and new houses on Mars might be out of reach for most people in the foreseeable future, there is a renewed interest in more lightweight approaches to building and transportation. You might even call it eco-friendly.
Even the otherwise lethargic Microsoft jumps on the bandwagon, to the acclaim of professional developers.
That's cool. This is what web development looks like with WebMatrix:
Looks strangely familiar, doesn't it? A mix of HTML markup and code, and direct data access without any of that ORM stuff? Yes, that's right. For comparison, let's bring up that screenshot from the Dark Ages (1998) again:
Conclusions & Take-Aways
(Because there always needs to be a take-away.)
I am (definitely) not saying that "Frameworks are evil", or that "Progress is uncool". I'm just as lazy as any programmer, but I prefer the simplest thing that could possibly work.
I prefer frameworks that don't try to obscure that what we (most of us) are trying to do (most of the time) is to generate some HTML based on data from an SQL database.
Do we really need something like this
If something like this can do the job?
The golden path is probably somewhere in-between those two extremes. But keep in mind: Stonehenge has lasted for 5,000 years, while the space shuttle is being retired after less than 50 years.
The Stone Age, circa 1995
Although it involved a lot of manual labor, mighty Stonehenge and the pyramids were built with very simple tools (or by aliens, but let's deal with that topic another day).
Web development was pretty basic back then, but conceptually simple enough.
The Dark Ages, circa 1998
Some pretty amazing castles were built during this age, and they still stand today.
The basic tools had improved a lot, but they were still recognizable. Even an Neanderthal from the previous age could probably be effective with these tools.
The Age of Enlightenment, circa 2002
The gothic cathedrals from around this time look awesome, but were (and still are) inhabited by unpleasant religious fanatics.
At this point, great thinkers with lots of free time combined art and science to build magnificent works of beauty and elegance.
The Age of the Astronauts and the Race for Space (via the Clouds), circa 2008
A lot of very expensive spaceships were built during this age.
The great empires fought a cold, mostly ideological, war to see who could be the first to reach the moon.
Of course this all ended with a ka-boom and the eventual retirement of the space shuttle program.
A New Hope, 2011
Realizing that space tourism and new houses on Mars might be out of reach for most people in the foreseeable future, there is a renewed interest in more lightweight approaches to building and transportation. You might even call it eco-friendly.
Even the otherwise lethargic Microsoft jumps on the bandwagon, to the acclaim of professional developers.
"WebMatrix is full 180 from the highly abstracted cathedral that is ASP.NET. (...) WebMatrix is focused on simplicity and the "Get It Done" developer. Which, to me, is a massive undersell as we're all "Get it Done" developers."
"... you have to use raw SQL to query the database. This is going to turn off the Ivory Tower crowd who prefer to wade through XML Soup and tedious designers - but that's to their detriment. As I've always said - the best DSL I've ever seen for working with data is SQL."
That's cool. This is what web development looks like with WebMatrix:
Looks strangely familiar, doesn't it? A mix of HTML markup and code, and direct data access without any of that ORM stuff? Yes, that's right. For comparison, let's bring up that screenshot from the Dark Ages (1998) again:
Conclusions & Take-Aways
(Because there always needs to be a take-away.)
I am (definitely) not saying that "Frameworks are evil", or that "Progress is uncool". I'm just as lazy as any programmer, but I prefer the simplest thing that could possibly work.
I prefer frameworks that don't try to obscure that what we (most of us) are trying to do (most of the time) is to generate some HTML based on data from an SQL database.
Do we really need something like this
If something like this can do the job?
The golden path is probably somewhere in-between those two extremes. But keep in mind: Stonehenge has lasted for 5,000 years, while the space shuttle is being retired after less than 50 years.
Tuesday, June 16, 2009
The Fat Database (or Thick Database) Approach
I'm a big believer in the so-called "Fat Database" paradigm for data-centric business applications.
I think I first heard the term "Thick Database" in a presentation by Dr. Paul Dorsey at the ODTUG conference in 2007.
I prefer the slightly more hip term "Fat Database", and offer my own definition of the term:
Benefits of the Fat Database approach include reduced cost and complexity, increased performance, and a degree of immunity against the need to constantly rewrite code in a rapidly changing technology landscape.
Here is a collection of links to presentations and papers related to the Fat Database approach:
Dr. Paul Dorsey, co-author of seven Oracle Press books on Designer, Database Design, Developer, and JDeveloper
Others
I think I first heard the term "Thick Database" in a presentation by Dr. Paul Dorsey at the ODTUG conference in 2007.
I prefer the slightly more hip term "Fat Database", and offer my own definition of the term:
"Building applications using the Fat Database approach means leveraging the full potential of the database engine and its features, rather than treating the database as a bit bucket. If a problem can be solved using the database, it should be solved using the database, rather than in a programming language outside the database."
In other words, the exact opposite of the current trend, which is to avoid any database feature except basic tables. The enterprise architecture astronauts would rather reinvent the wheel over and over again, using the latest silver bullet in the endless stream of "new and improved" languages and frameworks that appear (and disappear) every few years.
Benefits of the Fat Database approach include reduced cost and complexity, increased performance, and a degree of immunity against the need to constantly rewrite code in a rapidly changing technology landscape.
Here is a collection of links to presentations and papers related to the Fat Database approach:
Dr. Paul Dorsey, co-author of seven Oracle Press books on Designer, Database Design, Developer, and JDeveloper
- Thick Database Techniques for Fusion (and other Web) Developers
- Examining the Logic behind Database Independence
- Oracle Fusion Middleware: Tales from the Trenches
- The Helsinki Declaration (blog)
- Building Robust Applications in a DB-Centric Way
- A Database Centric Approach to J2EE Application Development
Others
- Lucas Jellema (Oracle ACE): Optimal Use of Oracle Database 10g and Oracle Database 11g for Modern Application Development
- Mike Ault: The Myth of Database Independence
- Tom Kyte (AskTom): "Why [do we need an] application server?"
Labels:
database-centric architecture,
fat database,
oracle
Subscribe to:
Posts (Atom)

























