Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
3Activity
0 of .
Results for:
No results containing your search query
P. 1
Perl and the Oracle DBA

Perl and the Oracle DBA

Ratings: (0)|Views: 255|Likes:
Published by rockerabc123

More info:

Published by: rockerabc123 on Jun 28, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

09/29/2011

pdf

text

original

 
P
ERL
 
AND
 
THE
O
RACLE
DBA
Jared Still, RadiSys Corporation
Perl came quietly onto the IT scene in 1987. Its goal as described by its creator Larry Wall was to combine the best of C, sed,awk and the Bourne shell into an easy to use language for text manipulation. Since the time of its initial release Perl has gonethrough 4 major revisions and succeeded far beyond its authors expectations. The current stable version of Perl is 5.8.6 as of the time this writing. When you think about it, Perl is now old enough to have attained ‘legacy‘ status.Perl was designed to be easily extended through the use of m
odules
. Perl modules allow Perl code to be easily packaged for reuse. There are literally thousand of Perl modules available through
CPAN 
(Comprehensive Perl Archive Network) designedfor a variety of purposes. There are modules available for converting data between different formats, file handling, time anddate conversions, email, interfaces to commercial software, security, graphics and many other topics. The modules that makePerl most interesting to DBA’s are the
 DBI 
(DataBase Interface) and
 DBD
(DataBase Driver) modules.
Early Use of Perl With Oracle
In the early part of the 1990’s, Kevin Stock created a version of Perl that was linked with Oracle’s OCI libraries to allowdirect access to Oracle databases from Perl. This was prior to the release of Perl 5. Perl 4 modules did not have the extensivecapabilities that Perl 5 now has, requiring that the Oracle libraries be linked to Perl at compile time. The result was known asOraperl.Oraperl made possible to use the extensive capabilities of Perl while connected to an Oracle database. This doesn’t reallysound too revolutionary until you consider that prior to Oraperl it was necessary to write external programs in C or someother 3GL language. Oraperl provided most of the power of C, along with the powerful features of sh, sed and awk in aneasy to use scripting language. The powerful constructs of Perl made it possible to write programs using less code than in C,and did not require the edit, compile, link routine when developing, instead using a runtime pcode compiler Creating Oracle database utilities became much easier for Oracle DBA’s familiar with C. For those not familiar with C, Perlwas an easier language to learn than C.In the early 1990’s (at a previous employer) we were searching for a tool that would allow us to easily write reports based on pharmaceutical claims data. Some commercial products were reviewed and rejected as being either too costly or toounwieldy. SQL*Plus has excellent report formatting abilities, but was lacking in its ability to make many complexcomputations at runtime. Using PL/SQL was not an option, as the combination of PL/SQL and SQL*Plus lacked the performance ability required at that time. PL/SQL functions used inline in SQL statements performed somewhat poorly inOracle 7. There was also the issue of controlling printers from the report, as the printer chosen at runtime had to be sent thecorrect codes to set the printer fonts, something that could not easily be done from SQL*Plus.It was suggested that Perl be considered, as it appeared to have much of the reporting functionality of commercial tools thathad been examined, and contained many features that commercial packages were lacking. The ability to directly control printers was vital. The fact the multiple database connections could be made was a nice feature. Of economic importancewas the fact that Perl was freely available (this was a small company). Several contractors were hired to work on our project,and as they were all proficient in C, they had no trouble learning to use Perl.The manager of the project had been hesitant at first, but was swayed by the economic arguments.What has been described so far may not appear to be DBA duties, but I have found that what constitutes DBA duties varieswith the company where one is employed, with company size being a strong influence on that. Small shops often call on theDBA to provide functionality and expertise outside the realm of classically defined DBA duties.Our use of Perl was so successful that we began using it for many tasks. The next project was to create backup routines toregularly backup our Oracle databases, something more in line with traditional DBA tasks. We had been managing backupsusing shell scripts, but something more robust was needed. A fairly robust Perl module (Perl 4 at that time) was created to
www.rmoug.orgRMOUG Training Days 2005
 
Perl and the Oracle DBAStill
manage the backup of Oracle database, both cold and hot backups. Known as
hotback 
1
,it was in use for several years at thissite.
DBI and DBD
The release of Perl 5 in October 1994 brought about many improvements. Among them was the ability to dynamically loadshared libraries at runtime. Also released in 1994 were DBI and DBD::Oracle, authored by Tim Bunce. The new release of Perl also permitted object oriented programming, making it possible to write easily reusable Perl modules.DBI was written as a generic database interface. This allowed for Perl scripts that used DBI to be written to the sameinterface regardless of database vendor. A separate DBD driver module would be written for each database vendor’s product,enabling the architecture seen in Figure 1.
Figure 1. Perl DBI and DBD::Architecture
Fortunately for Oracle users, the DBD::Oracle interface was also released in 1994, as the author of the DBI also wrote theDBD::Oracle module
2
. The use of Perl with databases, and in particular Oracle databases, began to increase dramaticallywith this new release of Perl and the release of the DBI and DBD::Oracle modules.What does all this mean for the DBA?
Why You Should Use Perl
A DBA is required to use many different technical skills. Besides being conversant in SQL, PL/SQL, database design andimplementation, there are also many tasks that require scripting skills. The tools of choice have traditionally been the Bourneor Korn shell
3
with their built in programmatic elements, along with such tools as sed, awk and other command line utilities.The DBA that works on the Win32 platform has a somewhat more limited choice of tools, at least as supplied with theoperating system.
1
Still available athttp://www.jaredstill.com/at the time of this writing.
2
Tim Bunce is still maintaining DBI and DBD::Oracle, as of December 2004.
3
And increasingly the Bash shell with the proliferation of Linux.
www.rmoug.orgRMOUG Training Days 2005
DBIDBD::OracleDBD::pgDBD::mysqlMySQLOraclePostgreSQL
 
Perl and the Oracle DBAStill
Perl has made possible the creation of DBA utilities that previously required more traditional languages such as C. Complexdata structures unavailable in shell scripting as well as more complex control structures make it an ideal scripting languagefor utility use. The fact that many Perl scripts can work on UNIX, Linux and Win32 without modification is also a valuableasset. Many a DBA must support Oracle on multiple platforms. No wonder Perl is referred to as the “Swiss Army Knife” of  programming languages.Though there are many features that separate Perl from the more traditional scripting tools, there are two features of DBD::Oracle that may be particularly attractive to a DBA.
Multiple connections to multiple databases.
The ability to work with LONG data types.When faced with querying data from another database in SQL*Plus while maintaining the current session databaseconnection, it is necessary to use a database link. While this usually works well its intended use in applications, it issomewhat unwieldy for the DBA maintaining many databases. The database connections may not be known until runtime. Itis very convenient to simply be able to open another connection to another database.Another limitation of SQL*Plus and PL/SQL is when dealing with LONG data types. PL/SQL can manipulate a LONGcolumn only up to 32k in length. Using Perl with DBI it is possible to manipulate LONG data types without resorting to C.Though modern applications should be using the more modern LOB data types, there are still many applications that rely onthe long data type.Here are a few of the many features of Perl that make it a powerful addition to the DBA tool box:
Powerful command line argument parsing
Direct control of many OS features and protocols such as ockets, ftp, ssh, file handling, semaphores and IPC
Interaction with the user 
Speed
The Nitty Gritty
Let’s take a look at a simple but complete Perl script makes a connection to a database and queries the DBA_USERSview. Though this is a relatively short script at ~120 lines, it is fairly robust. The following elements are included in thisscript:
Parses command line arguments
Creates a database connection
Creates calls to Oracle to create a cursor, execute it and fetch data.
Displays a usage screen if there is an error on the command line
Will produce either a CSV output or a formatted report
Includes documentation available via the
 perldoc
scriptThe first 16 lines of the script appear in Example 1. If you are unfamiliar with Unix systems, line 1 may appear somewhatstrange. This known as the
magic cookie
in shell scripting, and is used to inform the Unix kernel which shell is used toexecute a script, in this case the shell is Perl. The
 –w
tells Perl to include warnings.Lines 5-10 make up some simple internal documentation. This can be viewed by using the
 perldoc
command. Many Perlmodules contain extensive internal documentation. To view the documentation for the sample script you would use thecommand
 perldoc simple.pl
.At lines 12-15 the Perl modules required for this script are loaded. Notice that DBD::Oracle is not mentioned. The DBI willautomatically load the correct driver at the time the database connection is made.
1: #!/usr/bin/perl -w
www.rmoug.orgRMOUG Training Days 2005

Activity (3)

You've already reviewed this. Edit your review.
1 thousand reads
1 hundred reads
Hanse69 liked this

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->