Christopher Jones


LIMS::Database::Util - Perl object layer for a LIMS database


LIMS::Database::Util is an object-oriented Perl module designed to be the object layer for a LIMS database. It inherits from LIMS::Base and provides automation for DBI services required by a LIMS database, enabling rapid development of Perl CGI scripts. See LIMS::Controller for information about setting up and using the LIMS modules.


DBI Functions

Most of these methods are simply wrappers for DBI calls, catching possible errors so that the way they are reported can be controlled in the CGI script. Why not use Class::DBI? Well you can if you prefer - table classes are already loaded via Class::DBI::Loader.

Simple SQL 'fetch' methods

Methods fetching the results of SELECT queries are offered in a variety of flavours, returning results in different Perl data structures. Pass these methods a string 'SELECT' query.


Return a single row, single value, as a scalar


Multiple rows of a single value are returned as an array reference


Wrapper for the DBI method fetchrow_arrayref(). A single row of multiple values is returned as an array reference


Wrapper for the DBI method fetchrow_array(). A single row of multiple values is returned as an array


Wrapper for the DBI method fetchall_arrayref(). Multiple rows of multiple values are returned as a reference to a 2D array


Special use; If you have a query that returns a row of two values, where the first value is a primary key or other unique index. This method will return a reference to a hash whose keys/values are the first/second values of each row


Wrapper for the DBI method fetchrow_hashref(). A single row of multiple values is returned as a reference to a hash, whose keys are the column names, and whose values are the row values


Multiple rows of multiple values are returned as a reference to an array of hashes


Utilises sql_fetch_singlefield(), but sets the DBI {LongReadLen} variable to a default value of 32Mb in order to return long BLOB fields

SQL Insert and Update methods

Please note: LIMS::Database::Util does not control database privileges - it is assumed that the database login used by the module is only Grant[ed] privileges necessary/suitable for your application. Therefore, if the login does not have update/insert privileges, these methods will return relevant database errors caught by DBI.


Pass this method the table name together with the values to be inserted as a pre-formatted string, in the correct table column order. The method will generate the field values which will not include an auto-increment primary key. If you need/want to specify the field values, you can pass them as a pre-formatted string after the insert values - in which case the order of the two should match but do not have to be the table column order


Use this method for inserts of multiple rows of data, or if you need to insert quoted strings. This method is called similar to insert_into_table(), except that the values are passed as a reference to a 2D array of values to be inserted. If you need to pass the table fields, then you also need to pass a string of the correct number of placeholders. OK, so this isn't so tidy, but I was lost for a better way to do it.

Unlike other methods, this will call kill_pipeline() if any errors are caught, or commit_session() upon successful completion of all inserts. If the insert statament returns an insert_id, and the call to this method requests an array, it will return a list of the insert_ids created by each executed statement. Otherwise, it will return the number of inserted rows.

        my $aaValues = [
                [ $value1, $value2 ],
                [ $value3, $value4 ]

Use this method to update a single value in a table row. Pass the table name, the field to be set, the new value, and finally a 'WHERE' clause.


Returns the value from $sth->rows for the last insert.

Handling Errors

One of the main reasons for writing the LIMS modules was because I wanted to be able to deal with all errors - Perl, CGI, DBI - in a more efficient manner, all at the same time. If you want your script to die straight away when an error is caught, you can set the object to be 'unrepentant' as described below. The default is that the object allows you to be sorry for your coding sins, and then explains nicely what's gone wrong.

Three methods, db_error(), standard_error() and any_error() handle the errors for us, and the kill_pipeline() method prints them out upon killing the script; db_error() returns any database (DBI) errors that have been caught; standard_error() can be used to set any error/complaint in a CGI script, or returns any standard_error that has already been set; while any_error() returns true if errors of any type have been caught. So one line of code handles most eventualities;

        $database->kill_pipeline if ($database->any_error);  

If you have a simple situation where you want to kill the script with an error you've caught in your script, you can combine the error with the kill_pipeline() method;

        $database->kill_pipeline('got a problem');

If you need to, you can clear errors using the methods clear_db_errors(), clear_standard_errors() or clear_all_errors().

Other Methods


Returns the embedded DBI database handle. Rarely required, since most DBI functions are handled within LIMS::Database::Util.


Causes the script to die if any errors are thrown, printing out all errors and issuing a rollback call to the database.


Disconnects the database handle.


LIMS::Base, LIMS::Controller, LIMS::Web::Interface


Christopher Jones and James Morris, Translational Research Laboratories, Institute for Women's Health, University College London.,


Copyright 2007 by Christopher Jones

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.