 |
|
EnterpriseDB: PL/TCL
Oracle Tips by
Burleson
|
The Tool
Command Language, better known as TCL or tickle, is one of my favorite
embedded languages. If you don't know TCL, you don't need to
learn it to use EnterpriseDB. However, if you do know it, it can
help you extend EnterpriseDB and make your life easier in many ways.
I will
cover using TCL to extend EnterpriseDB in the chapter on Oracle
compatibility. For now, I will show that PL/TCL follows the same
structure as PL/pgSQL and PL/Perl in the declaration and use of
function in EnterpriseDB.
Before
you can use PL/TCL in EnterpriseDB, you must first have a working copy
of TCL running on your operating system. Many versions of Linux
come with TCL pre-installed. You can download ActiveTCL for
Windows from ActiveState.com. ActiveState provides a complete,
ready-to-install version of TCL for Linux, Solaris and Windows.
Once you
have TCL installed on your operating system, you need to run
CREATELANG as described above to add it to your EnterpriseDB
distribution. Don't forger to get your dynamic wrappers from
EnterpriseDB support.
Below is
the exact same functionality that I showed above in the PL/pgSQL
function but translated to PL/TCL. TCL is not a block oriented
language like SPL or PL/pgSQL. It is a highly dynamic language
that does not require advance variable declaration.
CREATE OR REPLACE FUNCTION get_emp_names_tcl(integer)
RETURNS text AS $$
set p_empno $1
spi_exec "SELECT ename FROM emp WHERE empno = $p_empno"
return $ename;
END;
$$ LANGUAGE pltcl;
If I run
this as I did above for the PL/pgSQL function, it works fine if it
finds a record but, because it is dynamic, $ename does not exist if a
record is not found. In that case, return $ename will return an
error.
I can
recode it to return a null value in the case where $ename is null.
How you chose to handle it, via an exception as above or as a null
value, is up to you and depends on your requirements.
CREATE OR REPLACE FUNCTION get_emp_names_tcl(integer)
RETURNS text AS $$
set p_empno $1
set ename null
spi_exec "SELECT ename FROM emp WHERE empno = $p_empno"
if {$ename == "null"} {
return_null; }
return $ename;
END;
$$ LANGUAGE pltcl;
spi_exec
is the TCL interface to running queries within EnterpriseDB.
There are various flavors of spi_exec but the one I used above is the
most simple and takes only a string parameter. That parameter is
a valid query.
There
are other ways to do the test for null, but a simple if statement
works for me. In addition, the way to return a null value from a
TCL function is to use the return_null procedure.
I can
call this in exactly the same manner as I did the PL/pgSQL function
(or for that matter like an SPL function, using a SELECT or a
procedural call.
SELECT get_emp_names_tcl(7788);
get_emp_names_tcl
-------------------
SCOTT
(1 row)
SELECT get_emp_names_tcl(1);
get_emp_names_tcl
-------------------
(1 row)
In the
Chapter on Oracle Compatibility, I will show you how to use TCL to
expand your options for using XML in EnterpriseDB.
This
is an excerpt from the book "EnterpriseDB:
The Definitive Reference" by Rampant TechPress. |