Platform: IBM Informix Dynamic Server 10.00 Connecting to IBM Informix Dynamic Server – the Open Source Way
Jonathan Leffler STSM, IBM Informix Database Engineering
Session: K12 2005-05-26 08:30 Agenda
• Open Source • Underlying connectivity • ESQL/C • ODBC • JDBC • Perl, DBI, and DBD::Informix • Tcl/Tk, isqltcl • PHP • Python • Ruby
2 Open Source
• What is Open Source? • Which rock have you been hiding under? • Software released under an Open Source license • Conformant with the Open Source Definition • Found at http://www.opensource.org/ • Free Redistribution • Source Code • Derived Works Permitted • No Discrimination Against People or Groups • No Discrimination Against Fields of Endeavour • Distribution of License
3 Open Source Licenses
• There are many alternative Open Source licenses • GPL – GNU Public License • LGPL – Lesser GNU Public License • BSD – Berkeley Systems Distribution • MIT – Massachussetts Institute of Technology (X) • MPL – Mozilla Public License • Academic Free License • Open Software License • Nearly 60 licenses listed at the Open Source Initiative!
4 Informix Database Connectivity
• ESQL/C • The original connectivity. • Standardized in SQL standards (ISO/IEC 9075:1992 onwards). • ODBC • Originally defined by Microsoft. • Standardized (as CLI) by ISO/IEC 9075-3:1996. • JDBC • Java analogue of ODBC. • Standardized by Sun. • All of these are proprietary. • But can be used with Open Source software.
5 ESQL/C
• Preprocessor which converts extended C into pure C. • Links with specific libraries. • Permits separation of static and dynamic SQL. • Even though Informix does not really do so. int main(void) { EXEC SQL WHENEVER ERROR STOP; EXEC SQL DATABASE Stores; EXEC SQL BEGIN WORK; EXEC SQL DROP TABLE Customer; EXEC SQL ROLLBACK WORK; return(0); }
6 ODBC
• Database agnostic. • Separates driver manager from drivers. • Different drivers can be loaded at run time. • With care, you can avoid database-specific features. • But sometimes you want to use them. • All statements are dynamic. • De-emphasized by Microsoft • In favour of newer technologies • ADO • .NET
7 JDBC
• Database agnostic. • Drivers have different levels of Java-ness. • Type 4: pure Java – usually the best type to use. • The only other way to connect is ESQL/J. • Not widely accepted. • JDBC is the lingua franca of the Java database world.
8 Perl, DBI, and DBD::Informix
• Perl • DBI • DBD::Informix • Apache • mod_perl
9 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 • Current stable versions: • 5.8.6 — November 2004 • 5.6.1 — August 2001 • 5.005_03 — April 2000 • Obtain via CPAN • Comprehensive Perl Archive Network • http://www.cpan.org/
10 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
11 Remove RCS Keyword Markers
• #!/usr/bin/perl -wp • s/\$([A-Z][a-z]+|RCSfile): ([^\$]+) \$/$2/go;
• TMTOWTDI • There’s more than one way to do it! • (Yes, you could use sed for this)
• perl -wp -e ’s/\$\w+: ([^\$]+) \$/$1/go;’ $*
12 File 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 =
13 Perl Database Interface
• DBI written by Tim Bunce. • Standard way to access databases with Perl. • Many database drivers available. • Including ODBC, DB2, and Oracle. • And, of course, Informix. • And many others. • Current version 1.48 – March 2005. • Requires Perl 5.6.1 or later. • Using Perl and a database? Use DBI! • Or a module built on top of DBI.
14 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/
15 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;
16 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;
17 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
18 DBI - Handling SELECT
• Many ways to fetch rows • $sth->fetchrow_array • $sth->fetchrow_hashref • $sth->fetchrow_arrayref • $sth->fetchall_arrayref • All rows
• Also utility methods • $dbh->selectrow_array • First row only • $dbh->selectall_arrayref
19 DBD::Informix - at last
• Using DBD::Informix is using DBI • All the examples work with DBD::Informix • Current version is 2005.01 – March 2005 • 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.6.0 or later • 5.8.6 (or, at a pinch, 5.6.1) strongly recommended • DBI version 1.38 or later • Version 1.48 or later strongly recommended
20 Installing DBD::Informix
• Download software from CPAN • cd DBD-Informix-2005.01 • more README • perl Makefile.PL • make • make test • make install • Have fun! • Same rules apply to all Perl modules • Building Perl modules is easy!
21 DBD::Informix - example #! /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;
22 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 });
23 Standard DBI Information
• Standard database attributes • $dbh->{Driver} • $dbh->{AutoCommit} • $dbh->{PrintError} • $dbh->{RaiseError} • $dbh->{ChopBlanks} • There are others, too.
24 Standard DBI Information
• Standard statement attributes • $sth->{Statement} • $sth->{CursorName} • $sth->{NUM_OF_FIELDS} • $sth->{NUM_OF_PARAMS} • $sth->{NAME} • $sth->{TYPE} • $sth->{NULLABLE}
25 Standard DBI Information
• Standard handle attributes • $h->err • $h->errstr • $h->state • Standard handle methods • $h->trace($trace_level) • $h->trace_msg(“message”)
26 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
27 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
28 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
29 Informix-only Information
• Non-standard attributes • $sth->{ix_NativeTypeName} • $sth->{ix_ColType} • $sth->{ix_ColLength}
30 DBD::Informix
• Known Limitations: • Not yet fully aware of 9.x collection types or UDTs. • No support for bind_param_inout method. • No handling for most new DBI features since v1.14. • Version 1.00.PC2 was an official Informix product. • Officially “IBM Informix Database Driver for Perl”. • Version 2003.04 was officially not an IBM product. • The support channel is the mailing list: • [email protected] • [email protected] • [email protected]
31 DBD::Informix Documents
• Primary References • Pre-install • README file • Files in Notes sub-directory • Post-install • perldoc DBI • perldoc DBD::Informix • Books • Programming Perl, 3rd Edition • Programming the Perl DBI
32 DBD::Informix - Web Sites
• Use CPAN to obtain the software • http://www.cpan.org/ • http://search.cpan.org/ • perl –MCPAN –e ‘install Bundle::DBD::Informix’ • DBI web site • http://dbi.perl.org/ • Sign up for [email protected] mailing list • Help yourself - join the mailing list!
33 Apache
• Apache Web Server • Most widely used web server • Version 1.3.33 • unless there’s a new version this week • Or Version 2.0.52 • Modular structure • Allows special purpose modules to be added • mod_jserv - Java Server • mod_perl - Perl Interpreter
34 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
35 Apache and mod_perl
• Use mod_perl version 1.29 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/
36 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
37 Apache Documentation
• http://httpd.apache.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/
38 Tcl/Tk and isqltcl
• Tcl – Tool Control Language – invented by John Ousterhout • Tk – Tool Kit (GUI) • Tcl/Tk – at http://www.tcl.tk/ • Current version 8.4.9 – December 2004. • Ported to most systems. • isqltcl – Informix SQL access to Tcl. • Available at http://isqltcl.sourceforge.net/ • Version 5.0 – released February 2002. • Not actively developed. • Still works.
39 Tcl/Tk Extensions
• Tcl/Tk is designed to be easily extended • There are many extensions available for all jobs • For example • Expect • Designed to handle scripting of processes • Used for automating testing • ftp:://expect.nist.gov/ • Incr Tcl • Providing ‘object extensions’ to Tcl • TclPro • Not Open Source • And many more...
40 ISQLTCL - A Work in Progress
• Including ISQLTCL • Version 4.00 is available at Tcl/Tk repositories • Version 5.00 is under development by • Kumar Srinivas, the original author • Jonathan Leffler, resident Open Source Advocate • The hosting of ISQLTCL has just changed • CVS repository is now at SourceForge • Widely used Open Source project hosting system • More than 10,500 projects hosted • ISQLTCL is accessible via: • http://sourceforge.net/projects/isqltcl • Setup is still in progress
41 ISQLTCL - Changes from 4.00 to 5.00
• Version 4.00 was released in February 1998 • Only builds into a custom shell • Not dynamically loadable • Not auto-configuring • Version 5.00 has not yet been released • Builds into a shared library • Dynamically loadable • No custom shell to build • No longer accepts misspellings and abbreviations
42 Using ISQLTCL - Loading
• Load the ISQLTCL extension • load isql.so • Adds the command ‘sql’ to Tcl/Tk • tclsh • wish
43 Using ISQLTCL - Connections
• Connect to a database • sql connect dbase as conn1 \ • user $username password $password • Connect to given database • WITH CONCURRENT TRANSACTIONS available • sql disconnect [current|default|all|conn1] • Close database connection • sql setconnection [default|conn1] [dormant] • Sets the specified connection • Dormant is important in threaded programs
44 Using ISQLTCL - Statements
• Executable statements • Anything except: • SELECT • Other than SELECT … INTO TEMP • EXECUTE PROCEDURE or FUNCTION • Where procedure returns values • sql run {delete from sometable where pkcol = ?} $pkval • Prepares and executes the statement • Optionally takes a number of arguments for placeholders • Returns zero if it succeeded, or non-zero on failure • No way to reuse the statement
45 Using ISQLTCL - Cursors
• SELECT, EXECUTE PROCEDURE • set stmt [sql open {select * from sometable}] • Does PREPARE, DECLARE, and OPEN • Returns a statement number (id) or a negative error • Optionally takes arguments for placeholders • set row [sql fetch $stmt 1] • Collects one row of data • As a Tcl list • into the variable ‘row’ • The 1 is optional and means strip trailing blanks • The list is empty if there is no more data
46 Using ISQLTCL - Cursors
• sql reopen $stmt ?arg1? ?arg2? • Reopens the statement, with new parameters • sql close $stmt • Indicates you have no further use for the statement • It frees both the cursor and statement!
47 Installing ISQLTCL 5.00
• With Automatic Configuration • ./configure --with-tcl=/home/src/tcl8.3.2 \ --with-tk=/home/src/tk8.3.2 \ --prefix=/usr/tcl • Assuming you still have build areas for Tcl/Tk • If you don’t have the build areas • specify the install directory • ./configure --with-tcl=/usr/tcl \ --with-tk=/usr/tcl \ --prefix=/usr/tcl • Currently requires info about Tk • but probably should not need it
48 PHP
• What is PHP • Building and Installing PHP • Basic PHP Programming • Access to IBM Informix Branded Databases • Web Programming
49 What is PHP?
• Hypertext Processor • Was once ‘Personal Home Page’ • Version 4.3.10 released December 2004 • Version 5.0.3 released December 2004 • Yes – the same day, in fact. • An HTML scripting language • Server-side • Cross-platform • Embedded in HTML documents • Extensible
50 What is PHP?
• Built into the Apache Web Server • Apache version 1.3.33 was released 2004-10-28 • Apache version 2.0.53 was released 2005-02-07 • Using DSO (dynamic shared objects) • mod_php • Or as a CGI binary • With any web server
51 What is PHP?
• Built-in access to • Email • FTP • PDF • Graphics files • XML • LDAP • HTTP (cookies, sessions)
52 What is PHP?
• And databases: • ODBC • DB2, Adabas-D, Empress, Solid, Velocis • mSQL, MySQL, Postgres • Sybase • Oracle • Informix
53 Building and Installing PHP
• Obtain source code for: • PHP – http://www.php.net/ • Apache – http://httpd.apache.org/ • Obtain: • Informix Client SDK • Access to Informix Database • Not needed for build, of course
54 Building and Installing PHP
• Extract Apache source into a directory • mkdir /work/apache • cd /work/apache • tar -xzf /tmp/apache_1.3.33.tar.gz • Configure it with DSO • cd /work/apache/apache_1.3.33 • sh configure --enable-module=so \ --prefix=/usr/apache
55 Building and Installing PHP
• Build Apache first • make • Install It • make install • You need apxs in the correct (installed) location to build PHP
56 Building and Installing PHP
• Extract PHP source into a directory • cd /work/apache • tar -xzf /tmp/php-4.3.10.tar.gz
57 Building and Installing PHP
• Configure it with Apache and Informix • cd /work/apache/php-4.3.10 Uses $INFORMIXDIR by • sh configure --with-informix \ default – override if necessary --with-apxs=/usr/apache/bin/apxs \ --prefix=/usr/php \ --exec-dir=/usr/php/bin \ --with-config-file-path=/usr/php/lib/php.ini
58 Building and Installing PHP
• Compile PHP • make • Install PHP • make install • If you omit --with-exec-dir, the install tries to place files under /usr/local. • Ditto for --with-config-file-path. • You need to manually copy php.ini-dist to /usr/php/lib/php.ini and edit the contents
59 Building and Installing PHP
• Verify that the httpd.conf file has PHP enabled. • AddType application/x-httpd-php .php • There’s a prototype line in the file • Restart the Apache server • /usr/apache/bin/apachectl restart • Create a simple PHP page in the document root directory (next slide) • Access it!
60 Simple PHP Script
61 Informative PHP Script
62 Basic PHP Programming
• Enclose PHP information in • There are alternatives: • • Unless you’ve got XML around • <% … %> • Has to be activated; useful for ASP migration •
63 Basic PHP Programming
• Three comment styles • // C++ comment to end of line • # Shell comment to end of line • /* C comments on multiple lines */ • Two quoted string styles • Double quotes (with expansion of variables) • Single quotes (verbatim) • Multiline quotes OK.
64 Basic PHP Programming
• Statements terminated by semi-colon • Variables prefixed by $ • Automatically supports 5 types • Integer • Double • String • Array • Object
65 Basic PHP Programming
• All arrays are associative • $sqlerrd[“sqlerrd1”] • $array[1] • Indirect variables supported • $varname = “x”; • $$varname yields value $x • Substrings supported • $x[1] yields first character of $x string.
66 Basic PHP Programming
• Including PHP source files • include “filename.php”; • require “filename.php”;
67 Basic PHP Programming
• Looping and conditional structures similar to C: • if (…) { … } elseif (…) { … } else { … } • while (…) { … } • for (s1; s2; s3) { …} • do { … } while (…); • switch (…) { case …: …; break; default: …; break; }
68 Basic PHP Programming
• Functions simply referenced by name • $dbh = ifx_connect(“mpsc@anubis_53”); • Simple way to iterate over elements in an array: • reset($array); • while (list($key, $value) = each($array)) { … } • With all extensions built in, about 700 functions available!
69 Basic PHP Programming
• Functions definable function soundcheck($a=1, $b=2, $c=3) { return “Testing, $a, $b, $c”; } • Function variables are local • Use “global $globalname;” to make a global visible
70 Basic PHP Programming
• Function arguments can be by value or by reference: • call_by_value($x, $y) • call_by_reference(&$x, &$y) • Beware: there isn’t an obvious way to find out in the function whether you have a value or a reference
71 Basic PHP Programming
• C style operators • With similar precedences • Error control operator, ‘@’ • Suppresses error messages from expression. • Execution operator ` • back tick, as in shell • String operators: • . (concatenation) • .= (concatenation assignment)
72 Access to Informix Databases
• Code provided as standard part of PHP. • But has not been maintained for a couple of years. • Must be explicitly compiled into PHP. • 30 core functions. • 8 functions to manipulate SBLOBs.
73 Access to IBM Informix Dynamic Server
• Connection management • ifx_connect • ifx_pconnect • ifx_close • Basic Operations • ifx_prepare • ifx_query • ifx_fetch_row • ifx_do • ifx_free_result
74 Access to IBM Informix Dynamic Server
• Status and Error Handling • ifx_getsqlca • ifx_error • ifx_errormsg • ifx_affected_rows • Attribute Queries • Blob handling • Utility functions • ifx_htmltbl_result
75 Access to IBM Informix Dynamic Server
• Problems? • ifx_close() fails if transaction open • Leads to persistent connections • Blocking problems • Limited support for UDTs • Exact extent unknown • No place holders in prepared SQL • Except for handling blobs • Performance issue
76 Web Programming
• PHP provides automatic access to • HTTP environment • GET or POST data • Cookies • Form data • PHP has an extensive repertoire of support functions. • PHP has a reputation for being insecure. • Largely a question of how it is used. • See PHP Security Consortium – http://phpsec.org/
77 Python
• http://www.python.org/ • Version 2.4 – November 2004. • No current Informix modules • informixdb – created 1999 • informixmodule – created 1999 • kinfxdb – created 1998
78 Ruby
• http://www.ruby-lang.org/ • Version 1.8.2 – December 2004. • No known Informix support • Not clear that there’s much database support.
79 http://www.ibm.com/software/data/informix http://www.iiug.org/software
80 http://www.ibm.com/software/data/informix http://www.iiug.org/software
81 Connecting to IBM Informix Dynamic Server – the Open Source Way Session: K12
Jonathan Leffler IBM [email protected]
82