Skip to Content

Talking with Perl, PHP, Python, Ruby to IDS

Creating a Powerful Mixture

This article describes the database interfaces of popular script languages like Perl, PHP, Python or Ruby which provide direct access to data stored in Informix Dynamic Server.


Powerful Script Languages

Script languages like Perl, PHP, Python and Ruby are getting more and more popular. Beside rapid prototyping capabilities they also allow the development of complex software projects in a time-saving and efficient manner.


Powerful Informix Capabilities

Inormix Dynamic Server is a strategic data server inside the IBM Information Management Division that is well known for it's outstanding capabilities:

  • Impressive Transaction Performance
  • High Reliability and First Class Support
  • Rich, sophisticated Extensibility Features
  • Backward Compatibility and Painless Version Upgrades
  • Powerful Replication Solutions inside the DBMS Kernel
  • Hardware gentle, scalable Multithreading Architecture
  • Extremely low Administration Overhead


Script Languages + IDS = Powerful Mixture

Combining the advantages of Perl, PHP, Python or Ruby with IBM's cult database Informix Dynamic Server results in an extremly efficient software platform. A platform that is well primed for increasing data volumes and the most demanding high availability requirements.


Database Abstraction Layer

All four script languages offer a database abstraction layer which allows the programming of portable database applications:

The Informix specific implementations of those database abstraction layers are all programmed in Esql/C. In order to compile the driver you need beside a C compiler the free of charge Informix Client SDK software installed. The Client SDK can be downloaded from here:

Free Download of Client SDK

Perl has a long history and is still one of the most popular scripting languages available today. Beside the development of web based applications, Perl is heavily used in the area of system administration automation on Unix and Linux systems. Another prominent Perl area is the extraction and conversion of data due to it's outstanding text processing capabilities.

Perl provides a rich offering of additional modules for almost every task, just take a look at the Comprehensive Perl Archive Network (CPAN). The DBI (DataBase Interface) is the well established standard for accessing databases from Perl. It defines a set of abstract methods which are implemented by the underlaying DataBase Driver (DBD). The driver for accessing an Informix database is called DBD::Informix and it has been written by Jonathan Leffler, a superb Informix expert and architect of Informix@IBM. DBD::Informix is well-engineered and contains an excellent documentation.

Installation steps for DB-API DBD::Informix

  • Download DBD::Informix
  • gzcat <perl_ifx_archive> | tar xvf -
  • cd <perl_ifx_archive_dir>
  • perl Makefile.PL
  • make
  • make test
  • make install

Example Program

#!/usr/bin/perl

use warnings;
use strict;
use DBI;

# ------------------------------------
# open connection to database 'stores'
# ------------------------------------
my $dbh = DBI->connect
          (
             "DBI:Informix:stores",
             '',
             '',
             { PrintError => 0, RaiseError => 1, AutoCommit => 0 }
          );

# ----------------
# open transaction
# ----------------
$dbh->do("begin");

# --------------------------------------
# prepare and execute 'select' statement
# --------------------------------------
my $sth1 =$dbh->prepare("select code, sname from state");
$sth1->execute();

# --------------------------
# prepare 'delete' statement
# --------------------------
my $sth2 = $dbh->prepare("delete from state where code = ?");

# ---------------------
# fetch thru result set
# ---------------------
while (my $row = $sth1->fetchrow_hashref())
{

    # -------------------------------------------
    # delete row if column 'code' begins with 'C'
    # -------------------------------------------
   if ($row->{code} =~ /^C/)
   {
      $sth2->bind_param(1, $row->{code});
      $sth2->execute();
      print sprintf("DELETED: %-2s %-15s\n", $row->{code}, $row->{sname});
      next;
   }

   # --------
   # show row
   # --------
   print sprintf("%-2s %-15s\n", $row->{code}, $row->{sname});

}

# ---------------------------------------
# commit transaction and close connection
# ---------------------------------------
$dbh->do("commit");
$dbh->disconnect();

exit 0;

More Information on Perl and DBD::Informix

Resource Location/URL
Perl Home http://www.perl.org
Learning Perl http://learn.perl.org
Download DBD::Informix DBD::Informix
J. Leffler: IDS and Open Source ids_opensource_jleffler.ppt
D. Priest: Using Perl & DBI perl_dbi_dpriest.pdf

PHP is the language of the Web and contains an awesome function library in this area. It is the foundation for many commercial and non-commerical websites today. PHP offers a Perl like syntax, is easy to learn and allows newbies to be quickly productive. In contrast to Perl the PHP programming instructions are directly embedded in the HTML page which seems to be more intuitive to most developers. However beyond the Web, languages like Perl and Python are more dominant than PHP.

PHP 5 offers a database abstraction layer called PDO (PHP Data Objects) which is included in PHP 5.1. PDO_INFORMIX is the Informix specific implementation of this abstraction layer developed by Krishna Raman and Kellen Bombardier.

Installation steps for DB-API PDO_INFORMIX with PHP 5.1

  • Download PDO_INFORMIX
  • Create PHP with PDO as shared extension
    • configure -with-zlib -enable-pdo=shared
  • Install Informix PDO Modul
    • pear install PDO_INFORMIX
  • Add entries to php.ini:
    • extension=pdo.so
    • extension=pdo_informix.so

More detailled instructions about installing and configuring PDO can be found here.

Example Program

<?php

# ------------------------------------
# open connection to database 'stores'
# ------------------------------------
$dbh = new PDO("informix:host=starship2; service=1753; database=stores; server=starship2; protocol=onsoctcp");

# ----------------
# open transaction
# ----------------
$dbh->beginTransaction();

# --------------------------------------
# prepare and execute 'select' statement
# --------------------------------------
$sth1 = $dbh->prepare("select code, sname from state");
$sth1->execute();

# --------------------------
# prepare 'delete' statement
# --------------------------
$sth2 = $dbh->prepare("delete from state where code = ?");

# ---------------------
# fetch thru result set
# ---------------------
while( $row = $sth1->fetch() )
{
    # -------------------------------------------
    # delete row if column 'code' begins with 'C'
    # -------------------------------------------
   if (strncmp($row['CODE'], "C", 1) == 0)
   {
      $sth2->BindParam(1, $row['CODE']);
      $sth2->execute();
      printf("DELETED: %-2s %-15s\n", $row['CODE'], $row['SNAME']);
      continue;
   }

    # --------
    # show row
    # --------
    printf("%-2s %-15s\n", $row['CODE'], $row['SNAME']);

}

# ---------------------------------------
# commit transaction and close connection
# ---------------------------------------
$dbh->commit();
$dbh = null;

?>

More Information on PHP and PDO_INFORMIX

Resource Location/URL
PHP Home http://www.php.net
Learning PHP http://www.w3schools.com/php/default.asp
Download PDO_INFORMIX PDO_INFORMIX
J-G. Perrin: PHP Development with IDS php_jpg.ppt
IBM Redbook about PHP Development with IDS http://www.redbooks.ibm.com/abstracts/sg247218.html
How to install, configure Apache,PHP,IDS on Linux IBM developerWorks article
Google Maps API and PHP/IDS on Linux IBM developerWorks article

Python is getting more and more popular. It has a clean, object-oriented design. Scripts written in Python have a clean structure and easy to read and maintain. The basic version of Python already contains a whole bunch of useful modules. This is called the Python batteries included philosophy, an interesting analogy to IDS Enterprise Edition which is also delivered with batteries included (Fragmentation, HDR, ER, HPL...).

There is also an interesting second implementation of Python called Jython. Jython is a pure Java implementation of the Python language and allows the developer to use Java class libraries (for example JDBC) directly in their Jython code. Check it out.

Python has quite a wide range of use. It is perfectly suited for developing ambitious software projects. However in the Web environment Perl and particulary PHP are more dominant.

The standard for accessing databases from Python is the DB-API. Carsten Haese has developed the Informix specific implemenation of this abstraction layer called the InformixDB driver.

Installation steps for DB-API InformixDB

  • Download DB-API InformixDB
  • gzcat <python_ifx_archiv> | tar xvf -
  • cd <python_ifx_dir>
  • python setup.py build_ext
  • python setup.py install

Example Program

#!/usr/bin/python

import sys
import informixdb  # import the InformixDB module

# ------------------------------------
# open connection to database 'stores'
# ------------------------------------
conn = informixdb.connect("stores")

# ----------------------------------
# allocate cursor and execute select
# ----------------------------------
cursor1 = conn.cursor(rowformat = informixdb.ROW_AS_DICT)
cursor1.execute('select code, sname from state')

# --------------------------
# prepare 'delete' statement
# --------------------------
cursor2 = conn.cursor()

# ---------------------
# fetch thru result set
# ---------------------
for row in cursor1:

    # -------------------------------------------
    # delete row if column 'code' begins with 'C'
    # -------------------------------------------
    if row['code'][0] == 'C':
        cursor2.execute('delete from state where code = ?', (row['code'],))
        print "DELETED: %-2s %-15s" % (row['code'], row['sname'])
        continue

   # --------
   # show row
   # --------
   print "%-2s %-15s" % (row['code'], row['sname'])


# ---------------------------------------
# commit transaction and close connection
# ---------------------------------------
conn.commit()
conn.close()

sys.exit(0);

More Information on Python and InformixDB

Resource Location/URL
Python Home http://www.python.org
Learning Python http://docs.python.org/tut/tut.html
Download DB-API InformixDB DB-API InformixDB
C. Haese: The Python InformixDB API python_dbi_khaese.pdf

Ruby seems to be an interesting alternative to the well established script languages like Perl, PHP and Python. With the Ruby on Rails web framework, Ruby is getting more and more important in the professional web development space.

Ruby does also provide a database independent API called DBI. The actual Ruby/Informix driver written by Gerardo Santana Gomez Garrido does currently not use the DBI. Ruby/Informix is a Ruby extension written in Esql/C. Even without the DBI/DBD wrapper, it can be perfectly used to access Informix databases from the Ruby programming language. An Informix adapter for Ruby on Rails is also available (check the link section below).

Unfortunately I didn't have the time to test Ruby myself, but Gerado was so kind to translate my example program to Ruby/Informix.

Installation steps for DB-API Ruby-Informix

Here are two documents that might help:

Example Program

#!/usr/local/bin/ruby -w

require 'informix'

# ------------------------------------
# open connection to database 'stores'
# ------------------------------------
db = Informix.connect('stores')

# ----------------
# open transaction
# ----------------
db.transaction {

# --------------------------------------
# prepare cursor for 'select' statement
# --------------------------------------
  cursor = db.cursor("select code, sname from state")

# --------------------------
# prepare 'delete' statement
# --------------------------
  stmt_delete = db.prepare("delete from state where code = ?")

# ---------------------
# open cursor and fetch thru result set
# ---------------------
  cursor.open.each_hash {|row|
    # -------------------------------------------
    # delete row if column 'code' begins with 'C'
    # -------------------------------------------
    if row['code'][0].chr == 'C'
      stmt_delete.execute(row['code'])
      printf("DELETED: %-2s %-15s\n", row['code'], row['sname'])
      next
    end

    # --------
    # show row
    # --------
    printf("%-2s %-15s\n", row['code'], row['sname'])
  }.close

# ---------------------------------------
# commit transaction
# ---------------------------------------
}

# ---------------------------------------
# close connection
# ---------------------------------------
db.close

More Information on Ruby and InformixDB

Resource Location/URL
Ruby Home http://www.ruby-lang.org/en
Learning Ruby http://sitekreator.com/satishtalim/toc.html
Download Ruby/Informix API DB-API Ruby-Informix
Download Informix/Rails Adapter Informix Adapter for Ruby Rails
Informix on Rails Guy Bowerman's Summary

Ruby driver installation

Since December 13th, the Ruby driver is the easiest one to install (assuming you have Rubygems installed, the Ruby's package manager).

1. Type:

gem install ruby-informix

2. There's no step two.

--
Gerardo Santana

Python clarifications

Thanks for this nice write-up on open-source "scripting" languages, or as we like to call them, dynamic languages. I'd simply like to point out a few details on Python.

1) Not that I wouldn't like to take all the credit, but saying that I developed InformixDB gives me more credit than I deserve. I am the current maintainer, true, and I have added a few improvements in the past 18 months or so, but I am simply building on a foundation that other people have laid long before me.

2) It's true that PHP is more dominant than Python in the space of web applications, but Python is definitely gaining traction in that area. I've recently read that YouTube's web interface is written in Python, for example. There are a lot of good Python web development frameworks out there, for example TurboGears, Django, and Zope. Thanks to Mark Rees, all of them have DB adapters to Informix (via "my" InformixDB module) in varying stages of maturity.

Regards,

Carsten Haese.

Thank you Gerado.

Just want to say thank you to Gerardo Santana Gomez Garrido, the creator of the Ruby/Informix driver, for translating my example program to Ruby.

Your work is highly appreciated by the Informix community.

Copyrightstory |