2.3. Calling PL/SQL from Other LanguagesSooner or later, you will probably want to call PL/SQL from C, Java, Perl, PHP, or any number of other places. This seems like a reasonable request, but if you've ever done cross-language work before, you may be all too familiar with some of the intricacies of mating up language-specific I will show a few very brief examples of calling PL/SQL from the outside world. Let's say that I've written a PL/SQL function that accepts an ISBN expressed as a string and returns the corresponding book title:
In SQL*Plus, I could call this in several different ways. The shortest way would be as follows:
Let's see how I might call this function from the following environments:
These examples are very contrivedfor example, the username and password are hardcoded, and the programs simply display the output to stdout. Moreover, I'm not even going to pretend to describe every line of code. Still, these examples will give you an idea of some of the patterns you may encounter in different languages. 2.3.1. C: Using Oracle's Precompiler (Pro*C)Oracle supplies at least two different C-language interfaces to Oracle: one called OCI (Oracle Call Interface) Pro*C is a precompiler technology that allows you to construct source files containing a mix of C, SQL, and PL/SQL. You run the following through Oracle's proc program, and out will come C code.
As you can see, Pro*C is not an approach for which language purists will be pining away. And trust me, you don't want to mess with the C code that this generates. Nevertheless, many companies find that Pro*C (or Pro*Cobol or any of several other languages Oracle supports) serves a reasonable middle ground between, say, Visual Basic (too slow and clunky) and OCI (too hard). For more information about Pro*C, the best source is Oracle's own documentation. 2.3.2. Java: Using JDBCAs with C, Oracle provides a number of different approaches to connecting to the database. The embedded SQL approach, known as SQLJ
This particular example uses the thin driver, which provides great compatibility and ease of installation (all the network protocol smarts exists in a Java library), at some expense of communications performance. An alternative approach would be to use what's known as the OCI driver. Don't worry: there's no rocket scientist programming required to use it, despite the name! To learn more about Java programming with Oracle, see Java Programming with Oracle JDBC by Don Bales and Oracle in a Nutshell by Rick Greenwald and David C. Kreines, both from O'Reilly. 2.3.3. Perl: Using Perl DBI and DBD::OracleMuch beloved by the system administration community, Perl is something of the mother of all open source languages. Now nearly in Version 6, it does just about everything and seems to run everywhere. And, with nifty auto-configuration tools such as CPAN (Comprehensive Perl Archive Network), it's a cinch to install community-supplied modules such as the DataBase Interface (DBI) and the corresponding Oracle driver, DBD::Oracle.
Perl is one of those languages in which it is shamelessly easy to write code that is impossible to read. It's not a particularly fast or small language, either, but there are compiled versions that at least address the speed problem. For more information about Perl and Oracle, see Perl for Oracle DBAs by Andy Duncan and Jared Still, and Programming the Perl DBI by Alligator Descartes, both from O'Reilly. There are also many excellent books on the Perl language, not to mention http://www.perl.com (an O'Reilly site), http://www.perl.org, and http://www.cpan.org. 2.3.4. PHP: Using Oracle ExtensionsIf you are the kind of person who (like me) might use the free and wildly popular web server known as Apache, you might also enjoy using the free and wildly popular programming language known as PHP. Commonly employed to build dynamic web pages, PHP can also be used to build GUI applications or to run command-line programs. As you might expect, Oracle is one of many database environments that work with PHP; somewhat surprisingly, Oracle Corporation is itself distributing a version of PHP with Oracle Application Server 10g Release 2. This example uses the family of PHP functions known as OCI8. Don't let the "8" in the name fool you; it should work with everything from Oracle7 to Oracle Database 10g.
When executed at the command line, it looks something like this:
By the way, these Oracle OCI functions are not available in PHP by default, but it shouldn't be too difficult for your system administrator to rebuild PHP with the Oracle extensions. You can find more information about PHP at http://www.php.net or in one of O'Reilly's many books on the subject. For PHP tips specific to Oracle, visit the Oracle Technology Network web site: http://otn.oracle.com. 2.3.5. PL/SQL Server PagesAlthough the PL/SQL Server Pages (PSP
That <%= %> construct means "process this as PL/SQL and return the result to the page." When properly installed on a web server connected to an Oracle database, this page displays as in Figure 2-5. Figure 2-5. Output from a PL/SQL Server PageI'm rather fond of PL/SQL Server Pages as a good way to put together data-driven web sites fairly quickly. For more information about PL/SQL Server Pages, see Learning Oracle PL/SQL (O'Reilly) by the authors of the book you're reading now. 2.3.6. And Where Else?We've seen how to use PL/SQL in SQL*Plus and in a number of other common environments and programming languages. There are still more places and ways that you can use PL/SQL:
We are not able, (un)fortunately, to address all these topics in this book. |
Friday, October 23, 2009
Section 2.3. Calling PL/SQL from Other Languages
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment