Presentation is loading. Please wait.

Presentation is loading. Please wait.

2001-01-08 14:30Perl, DBI and DBD::Informix Perl, Apache, DBI and DBD::Informix Jonathan Leffler Architect, Foundation Engineering.

Similar presentations


Presentation on theme: "2001-01-08 14:30Perl, DBI and DBD::Informix Perl, Apache, DBI and DBD::Informix Jonathan Leffler Architect, Foundation Engineering."— Presentation transcript:

1 2001-01-08 14:30Perl, DBI and DBD::Informix Perl, Apache, DBI and DBD::Informix Jonathan Leffler Architect, Foundation Engineering

2 2001-01-08 14:30 Perl, DBI and DBD::Informix2 Agenda ]Perl ]DBI ]DBD::Informix ]Apache ]mod_perl ]Questions and answers ]Perl ]DBI ]DBD::Informix ]Apache ]mod_perl ]Questions and answers

3 2001-01-08 14:30 Perl, DBI and DBD::Informix3 Perl - Practical Extraction and Report Language ]Originally written by Larry Wall »Version 1.0 in 1987 »Version 4.0 in 1991 under GPL and Artistic License »Version 5.0 in 1994 ]Very widely available ]Current stable versions: »5.6.1 »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 »Version 1.0 in 1987 »Version 4.0 in 1991 under GPL and Artistic License »Version 5.0 in 1994 ]Very widely available ]Current stable versions: »5.6.1 »5.005_03 (with optional thread support) »5.004_04 (without thread support) ]Obtain via CPAN »Comprehensive Perl Archive Network »http://www.cpan.org/

4 2001-01-08 14:30 Perl, DBI and DBD::Informix4 Perl ]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

5 2001-01-08 14:30 Perl, DBI and DBD::Informix5 Remove RCS Keyword Markers #!/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;’ $*

6 2001-01-08 14:30 Perl, DBI and DBD::Informix6 Remove HTML Markup #/usr/bin/perl -wp s/ [^<>]+ //go; s/ ]+>//go; s/\"/"/go; s/\</</go; s/\>/>/go; s/\&/&/go; s/\ / /go;

7 2001-01-08 14:30 Perl, DBI and DBD::Informix7 #!/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 = ; chop(@ARGV); } for (@ARGV){ $was = $_; eval $op; die $@ if $@; rename($was, $_) unless $was eq $_; } File Renaming

8 2001-01-08 14:30 Perl, DBI and DBD::Informix8 Perl Database Interface ]DBI written by Tim Bunce ]Standard way to access databases with Perl ]Many database drivers available »Including ODBC »And DB2 »And Oracle »And, of course, Informix »And many others ]Current version 1.15 ]Using Perl and a database? Use DBI! ]DBI written by Tim Bunce ]Standard way to access databases with Perl ]Many database drivers available »Including ODBC »And DB2 »And Oracle »And, of course, Informix »And many others ]Current version 1.15 ]Using Perl and a database? Use DBI!

9 2001-01-08 14:30 Perl, DBI and DBD::Informix9 The 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/

10 2001-01-08 14:30 Perl, DBI and DBD::Informix10 DBI - Database Handles ]Load DBI »use DBI; ]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; ]Load DBI »use DBI; ]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;

11 2001-01-08 14:30 Perl, DBI and DBD::Informix11 DBI - 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 »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 »Statement handle goes out of scope »Explicitly »undef $sth;

12 2001-01-08 14:30 Perl, DBI and DBD::Informix12 DBI - Handling SELECT ]Statement handles are used for SELECT too »$sth = $dbh->prepare(qq% SELECT * FROM Customer WHERE Fname = ? AND Lname = ? ORDER BY Lname, Fname%); »$sth->execute($firstname, $surname); »@results = $sth->fetchall_arrayref; »…process results »print $results[$rownum][$colnum], etc »undef $sth; ]SELECT is fairly simple ]Statement handles are used for SELECT too »$sth = $dbh->prepare(qq% SELECT * FROM Customer WHERE Fname = ? AND Lname = ? ORDER BY Lname, Fname%); »$sth->execute($firstname, $surname); »@results = $sth->fetchall_arrayref; »…process results »print $results[$rownum][$colnum], etc »undef $sth; ]SELECT is fairly simple

13 2001-01-08 14:30 Perl, DBI and DBD::Informix13 DBI - Handling SELECT ]Many ways to fetch rows »$sth->fetchrow_array »$sth->fetchrow_hashref »$sth->fetchrow_arrayref »$sth->fetchall_arrayref ]Also utility methods »$dbh->selectrow_array »$dbh->selectall_arrayref ]Many ways to fetch rows »$sth->fetchrow_array »$sth->fetchrow_hashref »$sth->fetchrow_arrayref »$sth->fetchall_arrayref ]Also utility methods »$dbh->selectrow_array »$dbh->selectall_arrayref

14 2001-01-08 14:30 Perl, DBI and DBD::Informix14 DBD::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 (eg ClientSDK) »ANSI C compiler (code uses prototypes) »Test database with DBA privileges »Perl version 5.004 or later »5.005_03 or 5.6.1 strongly recommended »DBI version 1.02 or later »Version 1.14 or later 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 (eg ClientSDK) »ANSI C compiler (code uses prototypes) »Test database with DBA privileges »Perl version 5.004 or later »5.005_03 or 5.6.1 strongly recommended »DBI version 1.02 or later »Version 1.14 or later strongly recommended

15 2001-01-08 14:30 Perl, DBI and DBD::Informix15 Installing 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 ]Building Perl modules is easy! ]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 ]Building Perl modules is easy!

16 2001-01-08 14:30 Perl, DBI and DBD::Informix16 #! /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 - example Error Checking Automated

17 2001-01-08 14:30 Perl, DBI and DBD::Informix17 DBD::Informix & 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 });

18 2001-01-08 14:30 Perl, DBI and DBD::Informix18 Standard DBI Information ]Standard database attributes »$dbh->{Driver} »$dbh->{AutoCommit} »$dbh->{PrintError} »$dbh->{RaiseError} »$dbh->{ChopBlanks} ]There are others, too. ]Standard database attributes »$dbh->{Driver} »$dbh->{AutoCommit} »$dbh->{PrintError} »$dbh->{RaiseError} »$dbh->{ChopBlanks} ]There are others, too.

19 2001-01-08 14:30 Perl, DBI and DBD::Informix19 Standard 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}

20 2001-01-08 14:30 Perl, DBI and DBD::Informix20 Standard 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”)

21 2001-01-08 14:30 Perl, DBI and DBD::Informix21 Informix-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

22 2001-01-08 14:30 Perl, DBI and DBD::Informix22 Informix-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

23 2001-01-08 14:30 Perl, DBI and DBD::Informix23 Informix-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

24 2001-01-08 14:30 Perl, DBI and DBD::Informix24 Informix-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}

25 2001-01-08 14:30 Perl, DBI and DBD::Informix25 DBD::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.01.PC1 » »$sth->bind_param(3, $blobval, {ix_type=>IX_TEXT}) »No support for bind_param_inout method ]Version 1.10.PC1 is an official Informix product »Officially “Informix Database Driver for Perl” »The support channel is the mailing list »dbd-informix@informix.com ]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.01.PC1 » »$sth->bind_param(3, $blobval, {ix_type=>IX_TEXT}) »No support for bind_param_inout method ]Version 1.10.PC1 is an official Informix product »Officially “Informix Database Driver for Perl” »The support channel is the mailing list »dbd-informix@informix.com

26 2001-01-08 14:30 Perl, DBI and DBD::Informix26 DBD::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 ]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

27 2001-01-08 14:30 Perl, DBI and DBD::Informix27 DBD::Informix - Web Sites ]Use CPAN to obtain the software »http://www.cpan.org ]DBI web sites »http://www.symbolstone.org/technology/perl/DBI »http://www.perl.org/dbi-lists.html »Sign up for dbi-users@iperl.org mailing list »http://eskimo.tamu.edu/~jbaker/dbi-examples.html ]Help yourself - join the mailing list! ]Use CPAN to obtain the software »http://www.cpan.org ]DBI web sites »http://www.symbolstone.org/technology/perl/DBI »http://www.perl.org/dbi-lists.html »Sign up for dbi-users@iperl.org mailing list »http://eskimo.tamu.edu/~jbaker/dbi-examples.html ]Help yourself - join the mailing list!

28 2001-01-08 14:30 Perl, DBI and DBD::Informix28 Apache ]Apache Web Server »Most widely used web server »Version 1.3.19 »unless there’s a 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.19 »unless there’s a new version this week ]Modular structure »Allows special purpose modules to be added »mod_jserv - Java Server »mod_perl - Perl Interpreter

29 2001-01-08 14:30 Perl, DBI and DBD::Informix29 Apache, 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

30 2001-01-08 14:30 Perl, DBI and DBD::Informix30 Apache and mod_perl ]Use mod_perl version 1.24_01 or later ]Requires Perl 5.004 or later ]Can automatically download pre-requisites »perl -MCPAN -e ‘install Bundle::Apache’ »Downloads, compiles, tests, installs software ]Simlarly for DBI and DBD::Informix »perl -MCPAN -e ‘install Bundle::DBD::Informix’ »You need to see this in action to believe it! ]Also consider FastCGI technology »http://www.fastcgi.com/ ]Use mod_perl version 1.24_01 or later ]Requires Perl 5.004 or later ]Can automatically download pre-requisites »perl -MCPAN -e ‘install Bundle::Apache’ »Downloads, compiles, tests, installs software ]Simlarly for DBI and DBD::Informix »perl -MCPAN -e ‘install Bundle::DBD::Informix’ »You need to see this in action to believe it! ]Also consider FastCGI technology »http://www.fastcgi.com/

31 2001-01-08 14:30 Perl, DBI and DBD::Informix31 Much faster web service Greased 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 ]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

32 2001-01-08 14:30 Perl, DBI and DBD::Informix32 There’s a lot of it about Apache 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/

33 2001-01-08 14:30 Perl, DBI and DBD::Informix33 Apache and mod_java ]Apache-Jserv »Version 1.1.2 or later ]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 »Version 1.1.2 or later ]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

34 2001-01-08 14:30 Perl, DBI and DBD::Informix34 Questions and Answers Your Turn! Don’t forget to check out http://www.iiug.org/http://www.perl.com/http://www.apache.org/ Thank You for Listening

35 2001-01-08 14:30 Perl, DBI and DBD::Informix35 Questions and Answers


Download ppt "2001-01-08 14:30Perl, DBI and DBD::Informix Perl, Apache, DBI and DBD::Informix Jonathan Leffler Architect, Foundation Engineering."

Similar presentations


Ads by Google