Gantry::Utils::DB - Database wrapper functions, specfic to PostgreSQL


    db_commit( $dbh );

    $dbh = db_connect( $db_type, $user, $pass, $server, $db, $commit );
    $dbh = db_connect( %config_hash );

    db_disconnect( $dbh );

    db_finish( $sth );

    $last_value = db_lastseq( $dbh, $sequence_name );

    ( @values ) = db_next( $sth );

    $hash_reference = db_nextvals( $handle );

    $sth = db_query_bp( $dbh, $description, \@sql_query, \@params );
    $sth = db_query( $dbh, $description, @sql_query );*
    * Not safe and should not be used.

    db_rollback( $dbh );

    $rows = db_rowcount( $sth );

    db_run( $dbh, $description, @sql_query );


These functions wrap the common DBI calls to Databases with error checking.


db_commit( $dbh )

Takes a database handle and commits all pending transactions if AutoCommit is not enabled, otherwise does nothing. Returns no value.

$dbh = db_connect( %config_hash )
$dbh = db_connect( $db_type, $user, $pass, $server, $db, $commit )

Creates a connection to the database specified by $db on host $server. It then returns a $dbh variable containing the connection. The hash has the values db_type, usr, pwd, db, srv, commit for the respective variables. Commit should be specified as the text 'on' or 'off', case does not matter. 'db_type' should be a valid DBI database type ( eg. 'Pg' for postgres. ).

db_disconnect( $dbh )

Takes a database handle and disconnects that connection to the database, it will also rollback any pending transactions that have not been commited with db_commit(). Returns no value.

db_finish( $sth )

Finishes a statement handle after a db_query() is completed. Returns nothing.

$last_value = db_lastseq( $dbh, $sequence_name )

Takes a database handle and the name of the sequence. It returns the last value that the sequence handed out. Usefully during transactions when the id of the last inserted SQL is needed. Will croak() if there is no database handle passed in or if no sequence is passed in. If no sequence is passed in, before croak()ing it will preform a rollback.

( @values ) = db_next( $sth )

Takes a statement handle and returns the next row as an array. The function will croak() if there is no statement handle passed in.

$hash_reference = db_nextvals( $sth )

This function takes a sql statement handle, $sth, and returns the next row from the statement as a hash reference with the column names as the keys and the values set from the row in the query.

$sth = db_query( $dbh, $description, \@sql_query, \@params )

This function takes a database handle, $dbh, a description of the call, $description, a sql query, \@sql_query, and a list of parameters, \@params to bind. The query is then run against the database specified in $dbh using the specified bound parameters \@params. The function will return a statment handle, $sth, or if there is an error while executing the sql query it will croak().

$sth = db_query( $dbh, $description, @sql_query )

Calling db_query in this way is unsafe. It is vulnerable to sql injection attacks. Please use the alternate calling method listed above instead.

db_rollback( $dbh )

Takes a database handle and preforms a rollback on the handle. Returns nothing.

$rows = db_rowcount( $sth )

Takes a statement handle and returns an integer count of the number of rows affected in the statement handle ( ie. the number of rows in a select ).

db_run( $dbh, $description, @sql_query )

This function behaves identcally to db_query(), save it uses the DBI->do vs the DBI->execute method to run the sql query. This means this function will never return a statement handle.



Not currently used, since there are no other methods to act on the object.


Gantry::Utils::SQL(3), DBI(3), DBD::Pg(3)


This library is untested with databases other than Postgresql.


Nicholas Studt <>


Copyright (c) 2005, Nicholas Studt.

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.