Perl, Apache, DBI Perl, Apache, DBI and DBD::Informixand DBD::Informix
Jonathan LefflerOpen Source Architect
Classic Database Engineering
useruser .conference.conferenceInformixInformix
AgendaAgenda
• Perl
• DBI
• DBD::Informix
• Apache
• mod_perl
• Questions and Answers
• Perl
• DBI
• DBD::Informix
• Apache
• mod_perl
• Questions and Answers
DBD::Informix - Perl’s Route to InformixDBD::Informix - Perl’s Route to Informix
useruser .conference.conferenceInformixInformix
Practical Extraction and Report LanguagePractical Extraction and Report Language
PerlPerl
• Originally written by Larry Wall
• Very widely available
• Current stable versions:• 5.6.0
• some packages (HTML::Mason 0.89) do not work with this
• 5.005_03 (with optional thread support)• 5.004_04 (without thread support)
• Obtain via CPAN• Comprehensive Perl Archive Network• http://www.cpan.org/
• Originally written by Larry Wall
• Very widely available
• Current stable versions:• 5.6.0
• some packages (HTML::Mason 0.89) do not work with this
• 5.005_03 (with optional thread support)• 5.004_04 (without thread support)
• Obtain via CPAN• Comprehensive Perl Archive Network• http://www.cpan.org/
useruser .conference.conferenceInformixInformix
PerlPerl
• Script Language• Does not require compilation
• Complex looking code
• Can be incredibly terse
• Can be quite legible
• Excellent at string handling
• Excellent access to operating system
• Script Language• Does not require compilation
• Complex looking code
• Can be incredibly terse
• Can be quite legible
• Excellent at string handling
• Excellent access to operating system
Do You Need Anything Else?Do You Need Anything Else?
useruser .conference.conferenceInformixInformix
Perl: A Two-LinerPerl: A Two-Liner
#!/usr/bin/perl -wp
s/\$([A-Z][a-z]+|RCSfile): ([^\$]+) \$/$2/g;
• TMTOWTDI• There’s more than one way to do it!• (Yes, you could use sed for this)
perl -wp -e ’s%\$\w+: ([^\$]+) \$%$1%go;’ $*
#!/usr/bin/perl -wp
s/\$([A-Z][a-z]+|RCSfile): ([^\$]+) \$/$2/g;
• TMTOWTDI• There’s more than one way to do it!• (Yes, you could use sed for this)
perl -wp -e ’s%\$\w+: ([^\$]+) \$%$1%go;’ $*
Remove RCS Markers for RCS KeywordsRemove RCS Markers for RCS Keywords
useruser .conference.conferenceInformixInformix
Another ExampleAnother Example
File RenamingFile Renaming
#!/usr/bin/perl# @(#)$Id: rename.pl,v 1.1 1992/01/05 22:33:47 jl Exp $# Rename files using a Perl substitute command
($op = shift) || die "Usage: $0 perlexpr [filenames]\n";if (!@ARGV) {
@ARGV = <STDIN>;chop(@ARGV);
}for (@ARGV){
$was = $_;eval $op;die $@ if $@;rename($was, $_) unless $was eq $_;
}
#!/usr/bin/perl# @(#)$Id: rename.pl,v 1.1 1992/01/05 22:33:47 jl Exp $# Rename files using a Perl substitute command
($op = shift) || die "Usage: $0 perlexpr [filenames]\n";if (!@ARGV) {
@ARGV = <STDIN>;chop(@ARGV);
}for (@ARGV){
$was = $_;eval $op;die $@ if $@;rename($was, $_) unless $was eq $_;
}
useruser .conference.conferenceInformixInformix
Using Perl and a Database? Use DBI!Using Perl and a Database? Use DBI!
Perl Database InterfacePerl Database Interface
• DBI written by Tim Bunce
• Standard way to access databases with Perl
• Many database drivers available• Including ODBC• And Oracle• And, of course, Informix• And many others
• Current version 1.14
• DBI written by Tim Bunce
• Standard way to access databases with Perl
• Many database drivers available• Including ODBC• And Oracle• And, of course, Informix• And many others
• Current version 1.14
8
useruser .conference.conferenceInformixInformix
The Cheetah BookThe Cheetah Book
• The ‘bible’ for Perl DBI
• Authors:• Alligator Descartes • Tim Bunce
• O’Reilly, February 2000
• ISBN 1-56592-699-4
• http://www.oreilly.com/
• The ‘bible’ for Perl DBI
• Authors:• Alligator Descartes • Tim Bunce
• O’Reilly, February 2000
• ISBN 1-56592-699-4
• http://www.oreilly.com/
useruser .conference.conferenceInformixInformix
Database Handles are CrucialDatabase Handles are Crucial
DBI: Database HandlesDBI: Database Handles
• Create database handles• $dbh = DBI->connect(‘DBI:Informix:stores7’);
• Database methods• $dbh->do(‘DELETE FROM Customer’);
• Transaction control• $dbh->rollback;• $dbh->commit;
• Disconnect• $dbh->disconnect;
• Create database handles• $dbh = DBI->connect(‘DBI:Informix:stores7’);
• Database methods• $dbh->do(‘DELETE FROM Customer’);
• Transaction control• $dbh->rollback;• $dbh->commit;
• Disconnect• $dbh->disconnect;
useruser .conference.conferenceInformixInformix
Statement Handles Are Important TooStatement Handles Are Important Too
DBI: Statement HandlesDBI: Statement Handles
• Create statement handles• $sth = $dbh->prepare(qq{ DELETE FROM Customer WHERE Lname LIKE ‘%$name%’ AND ZipCode IS NULL });
• Statements can be executed• $sth->execute();
• Statement handles can be released• Implicitly
• When statement handle goes out of scope
• Explicitly• undef $sth;
• Create statement handles• $sth = $dbh->prepare(qq{ DELETE FROM Customer WHERE Lname LIKE ‘%$name%’ AND ZipCode IS NULL });
• Statements can be executed• $sth->execute();
• Statement handles can be released• Implicitly
• When statement handle goes out of scope
• Explicitly• undef $sth;
useruser .conference.conferenceInformixInformix
SELECT is Fairly SimpleSELECT is Fairly Simple
DBI: Handling SELECTDBI: Handling SELECT
• Statement handles are used for SELECT too• $sth = $dbh->prepare(q% SELECT * FROM Customer WHERE Fname = ? AND Lname = ? ORDER BY Lname, Fname%);
• $sth->execute($firstname, $surname);• @results = $sth->fetchall_arrayref;• …process results• undef $sth;
• Statement handles are used for SELECT too• $sth = $dbh->prepare(q% SELECT * FROM Customer WHERE Fname = ? AND Lname = ? ORDER BY Lname, Fname%);
• $sth->execute($firstname, $surname);• @results = $sth->fetchall_arrayref;• …process results• undef $sth;
useruser .conference.conferenceInformixInformix
TMTOWTDI!TMTOWTDI!
DBI: Handling SELECTDBI: Handling SELECT
• Many ways to fetch rows• $sth->fetchrow_array• $sth->fetchrow_hashref• $sth->fetchrow_arrayref• $sth->fetchall_arrayref
• And some utility functions• $dbh->selectall_arrayref• $dbh->selectrow_arrayref
• Many ways to fetch rows• $sth->fetchrow_array• $sth->fetchrow_hashref• $sth->fetchrow_arrayref• $sth->fetchall_arrayref
• And some utility functions• $dbh->selectall_arrayref• $dbh->selectrow_arrayref
useruser .conference.conferenceInformixInformix
Using DBD::Informix is Using DBIUsing DBD::Informix is Using DBI
DBD::Informix: At LastDBD::Informix: At Last
• Using DBD::Informix is using DBI• All the examples work with DBD::Informix
• Current version is 1.00.PC1
• Building DBD::Informix is easy• Requires working ESQL/C 5.00 or later• ANSI C compiler (code uses prototypes)• Test database with DBA privileges• Perl version 5.004 or later
• 5.005_03 or 5.6.0 strongly recommended
• DBI version 1.02 or later• Version 1.14 strongly recommended
• Using DBD::Informix is using DBI• All the examples work with DBD::Informix
• Current version is 1.00.PC1
• Building DBD::Informix is easy• Requires working ESQL/C 5.00 or later• ANSI C compiler (code uses prototypes)• Test database with DBA privileges• Perl version 5.004 or later
• 5.005_03 or 5.6.0 strongly recommended
• DBI version 1.02 or later• Version 1.14 strongly recommended
useruser .conference.conferenceInformixInformix
Building Perl Modules is Easy!Building Perl Modules is Easy!
Installing DBD::InformixInstalling DBD::Informix
• Download software from CPAN• cd DBD-Informix-1.00.PC1• more README• perl Makefile.PL• make• make test• make install• Have fun!
• Same rules apply to all Perl modules
• Download software from CPAN• cd DBD-Informix-1.00.PC1• more README• perl Makefile.PL• make• make test• make install• Have fun!
• Same rules apply to all Perl modules
useruser .conference.conferenceInformixInformix
#! /usr/bin/perl -w
use DBI;
$dbh = DBI->connect(‘DBI:Informix:stores7’,’’,’’, {RaiseError => 1, PrintError=>1});
$sth = $dbh->prepare(q%SELECT Fname, Lname, Phone
FROM Customer WHERE Customer_num = ? %);
$sth->execute(106);
$ref = $sth->fetchall_arrayref();
for $row (@$ref) {
print “Name: $$row[0] $$row[1], Phone: $$row[2]\n”;
}
$dbh->disconnect;
#! /usr/bin/perl -w
use DBI;
$dbh = DBI->connect(‘DBI:Informix:stores7’,’’,’’, {RaiseError => 1, PrintError=>1});
$sth = $dbh->prepare(q%SELECT Fname, Lname, Phone
FROM Customer WHERE Customer_num = ? %);
$sth->execute(106);
$ref = $sth->fetchall_arrayref();
for $row (@$ref) {
print “Name: $$row[0] $$row[1], Phone: $$row[2]\n”;
}
$dbh->disconnect;
DBD::Informix: ExampleDBD::Informix: Example
Error Checking Automated Error Checking Automated
useruser .conference.conferenceInformixInformix
AutoCommit Controls TransactionsAutoCommit Controls Transactions
DBD::Informix and AutoCommitDBD::Informix and AutoCommit
• AutoCommit is on by default• To comply with DBI requirements• Cannot be unset on unlogged databases• Simulates MODE ANSI on logged databases• Explicit BEGIN WORK is possible
• $dbh->do(‘begin work’);• $dbh->{AutoCommit} = 0;•$dbh = DBI->connect(‘DBI:Informix:stores7’, ‘’, ‘’, { AutoCommit => 0 });
• AutoCommit is on by default• To comply with DBI requirements• Cannot be unset on unlogged databases• Simulates MODE ANSI on logged databases• Explicit BEGIN WORK is possible
• $dbh->do(‘begin work’);• $dbh->{AutoCommit} = 0;•$dbh = DBI->connect(‘DBI:Informix:stores7’, ‘’, ‘’, { AutoCommit => 0 });
useruser .conference.conferenceInformixInformix
Standard DBI InformationStandard DBI Information
• Standard database attributes• $dbh->{Driver}• $dbh->{AutoCommit}• $dbh->{PrintError}• $dbh->{RaiseError}• $dbh->{ChopBlanks}
• Standard database attributes• $dbh->{Driver}• $dbh->{AutoCommit}• $dbh->{PrintError}• $dbh->{RaiseError}• $dbh->{ChopBlanks}
Standard DBI AttributesStandard DBI Attributes
useruser .conference.conferenceInformixInformix
Standard DBI InformationStandard DBI Information
• Standard statement attributes• $sth->{Statement}• $sth->{CursorName}• $sth->{NUM_OF_FIELDS}• $sth->{NUM_OF_PARAMS}• $sth->{NAME}• $sth->{TYPE}• $sth->{NULLABLE}
• Standard statement attributes• $sth->{Statement}• $sth->{CursorName}• $sth->{NUM_OF_FIELDS}• $sth->{NUM_OF_PARAMS}• $sth->{NAME}• $sth->{TYPE}• $sth->{NULLABLE}
Standard DBI AttributesStandard DBI Attributes
useruser .conference.conferenceInformixInformix
Standard DBI InformationStandard DBI Information
• Standard handle attributes• $h->err• $h->errstr• $h->state
• Standard handle methods• $h->trace($trace_level)• $h->trace_msg(“message”)
• Standard handle attributes• $h->err• $h->errstr• $h->state
• Standard handle methods• $h->trace($trace_level)• $h->trace_msg(“message”)
Standard Handle AttributesStandard Handle Attributes
useruser .conference.conferenceInformixInformix
Informix-only InformationInformix-only Information
• SQLCA is available• $sth->{ix_sqlcode}• $sth->{ix_sqlerrd} - an array• $sth->{ix_sqlerrm}• $sth->{ix_sqlerrp}• $sth->{ix_sqlwarn} - an array
• SQLCA is available• $sth->{ix_sqlcode}• $sth->{ix_sqlerrd} - an array• $sth->{ix_sqlerrm}• $sth->{ix_sqlerrp}• $sth->{ix_sqlwarn} - an array
Access to SQLCAAccess to SQLCA
useruser .conference.conferenceInformixInformix
Informix Database AttributesInformix Database Attributes
Informix-only InformationInformix-only Information
• Non-standard attributes• $dbh->{ix_InformixOnline}• $dbh->{ix_LoggedDatabase}• $dbh->{ix_ModeAnsiDatabase}• $dbh->{ix_InTransaction}• $dbh->{ix_ConnectionName}
• Standard attribute• $dbh->{Name} — database name
• Non-standard attributes• $dbh->{ix_InformixOnline}• $dbh->{ix_LoggedDatabase}• $dbh->{ix_ModeAnsiDatabase}• $dbh->{ix_InTransaction}• $dbh->{ix_ConnectionName}
• Standard attribute• $dbh->{Name} — database name
useruser .conference.conferenceInformixInformix
Informix Driver AttributesInformix Driver Attributes
Informix-only InformationInformix-only Information
• Non-standard attributes• $drh->{ix_ProductVersion}
• a version number for ESQL/C
• $drh->{ix_ProductName}• $drh->{ix_MultipleConnections}• $drh->{ix_ActiveConnections}• $drh->{ix_CurrentConnection}• $drh->{ix_ServerVersion}
• a version number for the database server
• All these attributes can also be found via the database handle, $dbh
• Non-standard attributes• $drh->{ix_ProductVersion}
• a version number for ESQL/C
• $drh->{ix_ProductName}• $drh->{ix_MultipleConnections}• $drh->{ix_ActiveConnections}• $drh->{ix_CurrentConnection}• $drh->{ix_ServerVersion}
• a version number for the database server
• All these attributes can also be found via the database handle, $dbh
useruser .conference.conferenceInformixInformix
Statement AttributesStatement Attributes
Informix-only InformationInformix-only Information
• Non-standard attributes• $sth->{ix_NativeTypeName}• $sth->{ix_ColType}• $sth->{ix_ColLength}
• Non-standard attributes• $sth->{ix_NativeTypeName}• $sth->{ix_ColType}• $sth->{ix_ColLength}
useruser .conference.conferenceInformixInformix
DBD::Informix is Not Perfect DBD::Informix is Not Perfect
DBD::InformixDBD::Informix
• Known Limitations• Not yet fully aware of 9.x collection types or UDTs• Doesn’t handle blobs in UPDATE statements
• Coded in DBD::Informix 1.10.PC1
• At least one memory leak• No support for bind_param_inout methods
• Version 1.00.PC1 is an Informix product• Officially “Informix Database Driver for Perl”• The support channel is [email protected]
• Known Limitations• Not yet fully aware of 9.x collection types or UDTs• Doesn’t handle blobs in UPDATE statements
• Coded in DBD::Informix 1.10.PC1
• At least one memory leak• No support for bind_param_inout methods
• Version 1.00.PC1 is an Informix product• Officially “Informix Database Driver for Perl”• The support channel is [email protected]
useruser .conference.conferenceInformixInformix
Please Read the README File!Please Read the README File!
DBD::Informix DocumentsDBD::Informix Documents
• Primary References• Pre-install
• README file• Informix.Licence file
• Post-install• perldoc DBI• perldoc DBD::Informix
• Books• Programming Perl, 3rd Edition• Programming the Perl DBI• Perl Resource Kits (Unix, Win32) - O’Reilly
• Primary References• Pre-install
• README file• Informix.Licence file
• Post-install• perldoc DBI• perldoc DBD::Informix
• Books• Programming Perl, 3rd Edition• Programming the Perl DBI• Perl Resource Kits (Unix, Win32) - O’Reilly
useruser .conference.conferenceInformixInformix
Help Yourself - Join the Mailing List!Help Yourself - Join the Mailing List!
DBD::Informix: Web SitesDBD::Informix: Web Sites
• Use CPAN to obtain the software• www.cpan.org
• DBI web sites• www.symbolstone.org/technology/perl/DBI• www.isc.org/dbi-lists.html
• Sign up for [email protected] mailing list
• eskimo.tamu.edu/~jbaker/dbi-examples.html
• Use CPAN to obtain the software• www.cpan.org
• DBI web sites• www.symbolstone.org/technology/perl/DBI• www.isc.org/dbi-lists.html
• Sign up for [email protected] mailing list
• eskimo.tamu.edu/~jbaker/dbi-examples.html
useruser .conference.conferenceInformixInformix
Apache Apache
• Apache Web Server• Most widely used web server• Version 1.3.14
• unless there’s another new version this week
• Modular structure• Allows special purpose modules to be added
• mod_jserv - Java Server• mod_perl - Perl Interpreter
• Apache Web Server• Most widely used web server• Version 1.3.14
• unless there’s another new version this week
• Modular structure• Allows special purpose modules to be added
• mod_jserv - Java Server• mod_perl - Perl Interpreter
Why Aren’t You Using Apache?Why Aren’t You Using Apache?
useruser .conference.conferenceInformixInformix
Apache, Perl and CGIApache, Perl and CGI
• CGI scripts drive the web
• Many of them are Perl scripts
• Most of those use the CGI module
• http://www.somewhere.com/cgi-bin/script.pl• Giveaway that there’s a Perl script in use• Often not that easy to spot
• Can be slow on heavily loaded servers• New Perl interpreter loaded for every hit
• CGI scripts drive the web
• Many of them are Perl scripts
• Most of those use the CGI module
• http://www.somewhere.com/cgi-bin/script.pl• Giveaway that there’s a Perl script in use• Often not that easy to spot
• Can be slow on heavily loaded servers• New Perl interpreter loaded for every hit
Perl Drives a Lot of Web SitesPerl Drives a Lot of Web Sites
useruser .conference.conferenceInformixInformix
Apache and mod_perlApache and mod_perl
• Use mod_perl version 1.24_01
• Requires Perl 5.004 or later
• Can automatically download pre-requisites• perl -MCPAN -e ‘install Bundle::Apache’• Downloads, compiles, tests, install software
• Experimental DBI version available• perl -MCPAN -e ‘install Bundle::DBI’• perl -MCPAN -e ‘install Bundle::DBD::Informix’
• Use mod_perl version 1.24_01
• Requires Perl 5.004 or later
• Can automatically download pre-requisites• perl -MCPAN -e ‘install Bundle::Apache’• Downloads, compiles, tests, install software
• Experimental DBI version available• perl -MCPAN -e ‘install Bundle::DBI’• perl -MCPAN -e ‘install Bundle::DBD::Informix’
If You Weren’t Convinced by Perl, Try This!If You Weren’t Convinced by Perl, Try This!
useruser .conference.conferenceInformixInformix
Greased LightningGreased Lightning
• Apache with mod_perl• Uses same CGI scripts
• Cleaned up for multiple use
• Loads scripts once and reuses them• Without starting a separate process each hit• Can even re-use database connections
• Apache::DBI module
• BUT• httpd is much bigger
• Also consider FastCGI
• Apache with mod_perl• Uses same CGI scripts
• Cleaned up for multiple use
• Loads scripts once and reuses them• Without starting a separate process each hit• Can even re-use database connections
• Apache::DBI module
• BUT• httpd is much bigger
• Also consider FastCGI
Much Faster Web ServiceMuch Faster Web Service
useruser .conference.conferenceInformixInformix
There’s a Lot of It AboutThere’s a Lot of It About
Apache DocumentationApache Documentation
• http://perl.apache.org/
• Use perldoc for mod_perl info• mod_perl_traps• mod_perl_tuning• Apache::DBI• cgi_to_mod_perl• CGI
• HOWTO for Linux at IIUG web site• http://www.iiug.org/
• http://perl.apache.org/
• Use perldoc for mod_perl info• mod_perl_traps• mod_perl_tuning• Apache::DBI• cgi_to_mod_perl• CGI
• HOWTO for Linux at IIUG web site• http://www.iiug.org/
useruser .conference.conferenceInformixInformix
Java-enabled Web ServersJava-enabled Web Servers
Apache and mod_javaApache and mod_java
• Apache-Jserv• Currently version 1.1.2
• Similar to Apache & Perl
• Requires JDK 1.1.x and JSDK 2.0
• Obtain from Apache web site• http://java.apache.org
• HOWTO in D4GL corner of IDN web site
• Apache-Jserv• Currently version 1.1.2
• Similar to Apache & Perl
• Requires JDK 1.1.x and JSDK 2.0
• Obtain from Apache web site• http://java.apache.org
• HOWTO in D4GL corner of IDN web site
useruser .conference.conferenceInformixInformix
Your Turn! Don’t forget to check out
http://www.informix.com/idn
http://www.iiug.org/
http://www.perl.com/
http://www.apache.org/
Thank You for Listening
Your Turn! Don’t forget to check out
http://www.informix.com/idn
http://www.iiug.org/
http://www.perl.com/
http://www.apache.org/
Thank You for Listening
useruser .conference.conferenceInformixInformix
Questions and AnswersQuestions and Answers